Mouse.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #undef MB_RIGHT
  25. #include "Types.h"
  26. #include "Vec2.h"
  27. #include "Device.h"
  28. #include "OsWindow.h"
  29. namespace crown
  30. {
  31. const uint32_t MAX_MOUSE_BUTTONS = 3;
  32. /// Enumerates mouse buttons.
  33. enum MouseButton
  34. {
  35. MB_LEFT = 0,
  36. MB_MIDDLE = 1,
  37. MB_RIGHT = 2
  38. };
  39. /// Interface for accessing mouse input device.
  40. class Mouse
  41. {
  42. public:
  43. //-----------------------------------------------------------------------------
  44. Mouse()
  45. : m_current_frame(0), m_last_button(MB_LEFT)
  46. {
  47. m_buttons[MB_LEFT] = ~0;
  48. m_buttons[MB_MIDDLE] = ~0;
  49. m_buttons[MB_RIGHT] = ~0;
  50. m_state[MB_LEFT] = false;
  51. m_state[MB_MIDDLE] = false;
  52. m_state[MB_RIGHT] = false;
  53. }
  54. /// Returns whether @a button is pressed in the current frame.
  55. bool button_pressed(MouseButton button) const
  56. {
  57. CE_ASSERT(button >= 0 && button < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", button);
  58. return (m_state[button] == true) && (m_buttons[button] == m_current_frame);
  59. }
  60. /// Returns whether @a button is released in the current frame.
  61. bool button_released(MouseButton button) const
  62. {
  63. CE_ASSERT(button >= 0 && button < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", button);
  64. return (m_state[button] == false) && (m_buttons[button] == m_current_frame);
  65. }
  66. /// Returns wheter any button is pressed in the current frame.
  67. bool any_pressed() const
  68. {
  69. return button_pressed(m_last_button);
  70. }
  71. /// Returns whether any button is released in the current frame.
  72. bool any_released() const
  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. Vec2 cursor_xy() const
  82. {
  83. int32_t x, y;
  84. device()->window()->get_cursor_xy(x, y);
  85. return Vec2(x, y);
  86. }
  87. /// Sets the position of the cursor in window space.
  88. /// @note
  89. /// Coordinates in window space have the origin at the
  90. /// upper-left corner of the window. +X extends from left
  91. /// to right and +Y extends from top to bottom.
  92. void set_cursor_xy(const Vec2& position)
  93. {
  94. device()->window()->set_cursor_xy((int32_t) position.x, (int32_t) position.y);
  95. }
  96. /// Returns the relative position of the cursor in window space.
  97. /// @note
  98. /// Coordinates in window space have the origin at the
  99. /// upper-left corner of the window. +X extends from left
  100. /// to right and +Y extends from top to bottom.
  101. /// @note
  102. /// Relative coordinates are mapped to a float varying
  103. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  104. /// maximum extent of the cosidered axis.
  105. Vec2 cursor_relative_xy() const
  106. {
  107. uint32_t window_width;
  108. uint32_t window_height;
  109. device()->window()->get_size(window_width, window_height);
  110. Vec2 pos = cursor_xy();
  111. pos.x = pos.x / (float) window_width;
  112. pos.y = pos.y / (float) window_height;
  113. return pos;
  114. }
  115. /// Sets the relative position of the cursor in window space.
  116. /// @note
  117. /// Coordinates in window space have the origin at the
  118. /// upper-left corner of the window. +X extends from left
  119. /// to right and +Y extends from top to bottom.
  120. /// @note
  121. /// Relative coordinates are mapped to a float varying
  122. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  123. /// maximum extent of the cosidered axis.
  124. void set_cursor_relative_xy(const Vec2& position)
  125. {
  126. uint32_t window_width;
  127. uint32_t window_height;
  128. device()->window()->get_size(window_width, window_height);
  129. set_cursor_xy(Vec2(position.x * (float) window_width, position.y * (float) window_height));
  130. }
  131. //-----------------------------------------------------------------------------
  132. void update(uint64_t frame, MouseButton b, bool state)
  133. {
  134. CE_ASSERT(b >= 0 && b < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", b);
  135. m_last_button = b;
  136. m_buttons[b] = frame;
  137. m_state[b] = state;
  138. }
  139. public:
  140. // The current frame number
  141. uint64_t m_current_frame;
  142. // Last button updated
  143. MouseButton m_last_button;
  144. uint64_t m_buttons[MAX_MOUSE_BUTTONS];
  145. bool m_state[MAX_MOUSE_BUTTONS];
  146. friend class Device;
  147. };
  148. } // namespace crown