Mouse.h 4.8 KB

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