| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #pragma once
- #include "types.h"
- #include "vector2.h"
- #include <cstring> // mem*
- namespace crown
- {
- /// Enumerates mouse buttons.
- ///
- /// @ingroup Input
- struct MouseButton
- {
- enum Enum
- {
- NONE,
- LEFT,
- MIDDLE,
- RIGHT,
- COUNT
- };
- };
- /// Interface for accessing mouse input device.
- ///
- /// @ingroup Input
- struct Mouse
- {
- Mouse()
- : _last_button(MouseButton::NONE)
- {
- memset(_last_state, 0, MouseButton::COUNT);
- memset(_current_state, 0, MouseButton::COUNT);
- }
- /// Returns whether the @a b button is pressed in the current frame.
- bool button_pressed(MouseButton::Enum b)
- {
- return (~_last_state[b] & _current_state[b]) != 0;
- }
- /// Returns whether the @a b button is released in the current frame.
- bool button_released(MouseButton::Enum b)
- {
- return (_last_state[b] & ~_current_state[b]) != 0;
- }
- /// Returns wheter any button is pressed in the current frame.
- bool any_pressed()
- {
- return button_pressed(_last_button);
- }
- /// Returns whether any button is released in the current frame.
- bool any_released()
- {
- return button_released(_last_button);
- }
- /// Returns the position of the cursor in window space.
- /// @note
- /// Coordinates in window space have the origin at the
- /// upper-left corner of the window. +X extends from left
- /// to right and +Y extends from top to bottom.
- Vector2 cursor_xy()
- {
- return Vector2(_x, _y);
- }
- /// Sets the position of the cursor in window space.
- /// @note
- /// Coordinates in window space have the origin at the
- /// upper-left corner of the window. +X extends from left
- /// to right and +Y extends from top to bottom.
- void set_cursor_xy(const Vector2& position)
- {
- _x = (uint16_t) position.x;
- _y = (uint16_t) position.y;
- }
- /// Returns the relative position of the cursor in window space.
- /// @note
- /// Coordinates in window space have the origin at the
- /// upper-left corner of the window. +X extends from left
- /// to right and +Y extends from top to bottom.
- /// @note
- /// Relative coordinates are mapped to a float varying
- /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
- /// maximum extent of the cosidered axis.
- Vector2 cursor_relative_xy()
- {
- return Vector2((float) _x / _width, (float) _y / _height);
- }
- /// Sets the relative position of the cursor in window space.
- /// @note
- /// Coordinates in window space have the origin at the
- /// upper-left corner of the window. +X extends from left
- /// to right and +Y extends from top to bottom.
- /// @note
- /// Relative coordinates are mapped to a float varying
- /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
- /// maximum extent of the cosidered axis.
- void set_cursor_relative_xy(const Vector2& position)
- {
- set_cursor_xy(Vector2(position.x * (float) _width, position.y * (float) _height));
- }
- void set_position(uint16_t x, uint16_t y)
- {
- _x = x;
- _y = y;
- }
- void set_metrics(uint16_t width, uint16_t height)
- {
- _width = width;
- _height = height;
- }
- void set_button_state(uint16_t x, uint16_t y, MouseButton::Enum b, bool state)
- {
- set_position(x, y);
- _last_button = b;
- _current_state[b] = state;
- }
- void update()
- {
- memcpy(_last_state, _current_state, MouseButton::COUNT);
- }
- public:
- MouseButton::Enum _last_button;
- uint8_t _last_state[MouseButton::COUNT];
- uint8_t _current_state[MouseButton::COUNT];
- // Position within the window
- uint16_t _x;
- uint16_t _y;
- // Window size
- uint16_t _width;
- uint16_t _height;
- };
- } // namespace crown
|