mouse.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "types.h"
  25. #include "vector2.h"
  26. #include <cstring> // mem*
  27. namespace crown
  28. {
  29. /// Enumerates mouse buttons.
  30. ///
  31. /// @ingroup Input
  32. struct MouseButton
  33. {
  34. enum Enum
  35. {
  36. NONE,
  37. LEFT,
  38. MIDDLE,
  39. RIGHT,
  40. COUNT
  41. };
  42. };
  43. /// Interface for accessing mouse input device.
  44. ///
  45. /// @ingroup Input
  46. struct Mouse
  47. {
  48. //-----------------------------------------------------------------------------
  49. Mouse()
  50. : _last_button(MouseButton::NONE)
  51. {
  52. memset(_last_state, 0, MouseButton::COUNT);
  53. memset(_current_state, 0, MouseButton::COUNT);
  54. }
  55. /// Returns whether the @a b button is pressed in the current frame.
  56. bool button_pressed(MouseButton::Enum b)
  57. {
  58. return (~_last_state[b] & _current_state[b]) != 0;
  59. }
  60. /// Returns whether the @a b button is released in the current frame.
  61. bool button_released(MouseButton::Enum b)
  62. {
  63. return (_last_state[b] & ~_current_state[b]) != 0;
  64. }
  65. /// Returns wheter any button is pressed in the current frame.
  66. bool any_pressed()
  67. {
  68. return button_pressed(_last_button);
  69. }
  70. /// Returns whether any button is released in the current frame.
  71. bool any_released()
  72. {
  73. return button_released(_last_button);
  74. }
  75. /// Returns the position of the cursor in window space.
  76. /// @note
  77. /// Coordinates in window space have the origin at the
  78. /// upper-left corner of the window. +X extends from left
  79. /// to right and +Y extends from top to bottom.
  80. Vector2 cursor_xy()
  81. {
  82. return Vector2(_x, _y);
  83. }
  84. /// Sets the position of the cursor in window space.
  85. /// @note
  86. /// Coordinates in window space have the origin at the
  87. /// upper-left corner of the window. +X extends from left
  88. /// to right and +Y extends from top to bottom.
  89. void set_cursor_xy(const Vector2& position)
  90. {
  91. _x = (uint16_t) position.x;
  92. _y = (uint16_t) position.y;
  93. }
  94. /// Returns the relative position of the cursor in window space.
  95. /// @note
  96. /// Coordinates in window space have the origin at the
  97. /// upper-left corner of the window. +X extends from left
  98. /// to right and +Y extends from top to bottom.
  99. /// @note
  100. /// Relative coordinates are mapped to a float varying
  101. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  102. /// maximum extent of the cosidered axis.
  103. Vector2 cursor_relative_xy()
  104. {
  105. return Vector2((float) _x / _width, (float) _y / _height);
  106. }
  107. /// Sets the relative position of the cursor in window space.
  108. /// @note
  109. /// Coordinates in window space have the origin at the
  110. /// upper-left corner of the window. +X extends from left
  111. /// to right and +Y extends from top to bottom.
  112. /// @note
  113. /// Relative coordinates are mapped to a float varying
  114. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  115. /// maximum extent of the cosidered axis.
  116. void set_cursor_relative_xy(const Vector2& position)
  117. {
  118. set_cursor_xy(Vector2(position.x * (float) _width, position.y * (float) _height));
  119. }
  120. //-----------------------------------------------------------------------------
  121. void set_position(uint16_t x, uint16_t y)
  122. {
  123. _x = x;
  124. _y = y;
  125. }
  126. //-----------------------------------------------------------------------------
  127. void set_metrics(uint16_t width, uint16_t height)
  128. {
  129. _width = width;
  130. _height = height;
  131. }
  132. //-----------------------------------------------------------------------------
  133. void set_button_state(uint16_t x, uint16_t y, MouseButton::Enum b, bool state)
  134. {
  135. set_position(x, y);
  136. _last_button = b;
  137. _current_state[b] = state;
  138. }
  139. //-----------------------------------------------------------------------------
  140. void update()
  141. {
  142. memcpy(_last_state, _current_state, MouseButton::COUNT);
  143. }
  144. public:
  145. MouseButton::Enum _last_button;
  146. uint8_t _last_state[MouseButton::COUNT];
  147. uint8_t _current_state[MouseButton::COUNT];
  148. // Position within the window
  149. uint16_t _x;
  150. uint16_t _y;
  151. // Window size
  152. uint16_t _width;
  153. uint16_t _height;
  154. };
  155. } // namespace crown