Mouse.h 4.9 KB

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