mouse.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "types.h"
  7. #include "vector2.h"
  8. #include <cstring> // mem*
  9. namespace crown
  10. {
  11. /// Enumerates mouse buttons.
  12. ///
  13. /// @ingroup Input
  14. struct MouseButton
  15. {
  16. enum Enum
  17. {
  18. NONE,
  19. LEFT,
  20. MIDDLE,
  21. RIGHT,
  22. COUNT
  23. };
  24. };
  25. /// Interface for accessing mouse input device.
  26. ///
  27. /// @ingroup Input
  28. struct Mouse
  29. {
  30. Mouse()
  31. : _last_button(MouseButton::NONE)
  32. , _wheel(0.0f)
  33. {
  34. memset(_last_state, 0, MouseButton::COUNT);
  35. memset(_current_state, 0, MouseButton::COUNT);
  36. }
  37. /// Returns whether the @a b button is pressed in the current frame.
  38. bool button_pressed(MouseButton::Enum b)
  39. {
  40. return (~_last_state[b] & _current_state[b]) != 0;
  41. }
  42. /// Returns whether the @a b button is released in the current frame.
  43. bool button_released(MouseButton::Enum b)
  44. {
  45. return (_last_state[b] & ~_current_state[b]) != 0;
  46. }
  47. /// Returns wheter any button is pressed in the current frame.
  48. bool any_pressed()
  49. {
  50. return button_pressed(_last_button);
  51. }
  52. /// Returns whether any button is released in the current frame.
  53. bool any_released()
  54. {
  55. return button_released(_last_button);
  56. }
  57. /// Returns the position of the cursor in window space.
  58. /// @note
  59. /// Coordinates in window space have the origin at the
  60. /// upper-left corner of the window. +X extends from left
  61. /// to right and +Y extends from top to bottom.
  62. Vector2 cursor_xy()
  63. {
  64. return Vector2(_x, _y);
  65. }
  66. /// Sets the position of the cursor in window space.
  67. /// @note
  68. /// Coordinates in window space have the origin at the
  69. /// upper-left corner of the window. +X extends from left
  70. /// to right and +Y extends from top to bottom.
  71. void set_cursor_xy(const Vector2& position)
  72. {
  73. _x = (uint16_t) position.x;
  74. _y = (uint16_t) position.y;
  75. }
  76. /// Returns the relative 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. /// @note
  82. /// Relative coordinates are mapped to a float varying
  83. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  84. /// maximum extent of the cosidered axis.
  85. Vector2 cursor_relative_xy()
  86. {
  87. return Vector2((float) _x / _width, (float) _y / _height);
  88. }
  89. /// Sets the relative position of the cursor in window space.
  90. /// @note
  91. /// Coordinates in window space have the origin at the
  92. /// upper-left corner of the window. +X extends from left
  93. /// to right and +Y extends from top to bottom.
  94. /// @note
  95. /// Relative coordinates are mapped to a float varying
  96. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  97. /// maximum extent of the cosidered axis.
  98. void set_cursor_relative_xy(const Vector2& position)
  99. {
  100. set_cursor_xy(Vector2(position.x * (float) _width, position.y * (float) _height));
  101. }
  102. /// Returns the mouse wheel state in the current frame.
  103. /// A positive or negative value is returned when the wheel is up or down
  104. /// respectively, 0.0 otherwise.
  105. float wheel()
  106. {
  107. return _wheel;
  108. }
  109. void set_position(uint16_t x, uint16_t y)
  110. {
  111. _x = x;
  112. _y = y;
  113. }
  114. void set_metrics(uint16_t width, uint16_t height)
  115. {
  116. _width = width;
  117. _height = height;
  118. }
  119. void set_button_state(uint16_t x, uint16_t y, MouseButton::Enum b, bool state)
  120. {
  121. set_position(x, y);
  122. _last_button = b;
  123. _current_state[b] = state;
  124. }
  125. void set_wheel(uint16_t x, uint16_t y, float wheel)
  126. {
  127. set_position(x, y);
  128. _wheel = wheel;
  129. }
  130. void update()
  131. {
  132. _wheel = 0.0f;
  133. memcpy(_last_state, _current_state, MouseButton::COUNT);
  134. }
  135. public:
  136. MouseButton::Enum _last_button;
  137. float _wheel;
  138. uint8_t _last_state[MouseButton::COUNT];
  139. uint8_t _current_state[MouseButton::COUNT];
  140. // Position within the window
  141. uint16_t _x;
  142. uint16_t _y;
  143. // Window size
  144. uint16_t _width;
  145. uint16_t _height;
  146. };
  147. } // namespace crown