2
0

mouse.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. Mouse()
  49. : _last_button(MouseButton::NONE)
  50. {
  51. memset(_last_state, 0, MouseButton::COUNT);
  52. memset(_current_state, 0, MouseButton::COUNT);
  53. }
  54. /// Returns whether the @a b button is pressed in the current frame.
  55. bool button_pressed(MouseButton::Enum b)
  56. {
  57. return (~_last_state[b] & _current_state[b]) != 0;
  58. }
  59. /// Returns whether the @a b button is released in the current frame.
  60. bool button_released(MouseButton::Enum b)
  61. {
  62. return (_last_state[b] & ~_current_state[b]) != 0;
  63. }
  64. /// Returns wheter any button is pressed in the current frame.
  65. bool any_pressed()
  66. {
  67. return button_pressed(_last_button);
  68. }
  69. /// Returns whether any button is released in the current frame.
  70. bool any_released()
  71. {
  72. return button_released(_last_button);
  73. }
  74. /// Returns the position of the cursor in window space.
  75. /// @note
  76. /// Coordinates in window space have the origin at the
  77. /// upper-left corner of the window. +X extends from left
  78. /// to right and +Y extends from top to bottom.
  79. Vector2 cursor_xy()
  80. {
  81. return Vector2(_x, _y);
  82. }
  83. /// Sets the position of the cursor in window space.
  84. /// @note
  85. /// Coordinates in window space have the origin at the
  86. /// upper-left corner of the window. +X extends from left
  87. /// to right and +Y extends from top to bottom.
  88. void set_cursor_xy(const Vector2& position)
  89. {
  90. _x = (uint16_t) position.x;
  91. _y = (uint16_t) position.y;
  92. }
  93. /// Returns the relative position of the cursor in window space.
  94. /// @note
  95. /// Coordinates in window space have the origin at the
  96. /// upper-left corner of the window. +X extends from left
  97. /// to right and +Y extends from top to bottom.
  98. /// @note
  99. /// Relative coordinates are mapped to a float varying
  100. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  101. /// maximum extent of the cosidered axis.
  102. Vector2 cursor_relative_xy()
  103. {
  104. return Vector2((float) _x / _width, (float) _y / _height);
  105. }
  106. /// Sets the relative position of the cursor in window space.
  107. /// @note
  108. /// Coordinates in window space have the origin at the
  109. /// upper-left corner of the window. +X extends from left
  110. /// to right and +Y extends from top to bottom.
  111. /// @note
  112. /// Relative coordinates are mapped to a float varying
  113. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  114. /// maximum extent of the cosidered axis.
  115. void set_cursor_relative_xy(const Vector2& position)
  116. {
  117. set_cursor_xy(Vector2(position.x * (float) _width, position.y * (float) _height));
  118. }
  119. void set_position(uint16_t x, uint16_t y)
  120. {
  121. _x = x;
  122. _y = y;
  123. }
  124. void set_metrics(uint16_t width, uint16_t height)
  125. {
  126. _width = width;
  127. _height = height;
  128. }
  129. void set_button_state(uint16_t x, uint16_t y, MouseButton::Enum b, bool state)
  130. {
  131. set_position(x, y);
  132. _last_button = b;
  133. _current_state[b] = state;
  134. }
  135. void update()
  136. {
  137. memcpy(_last_state, _current_state, MouseButton::COUNT);
  138. }
  139. public:
  140. MouseButton::Enum _last_button;
  141. uint8_t _last_state[MouseButton::COUNT];
  142. uint8_t _current_state[MouseButton::COUNT];
  143. // Position within the window
  144. uint16_t _x;
  145. uint16_t _y;
  146. // Window size
  147. uint16_t _width;
  148. uint16_t _height;
  149. };
  150. } // namespace crown