Mouse.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include "Mouse.h"
  24. #include "Device.h"
  25. #include "OsWindow.h"
  26. #undef MB_RIGHT
  27. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. Mouse::Mouse() :
  31. m_current_frame(0),
  32. m_last_button(MB_LEFT)
  33. {
  34. m_buttons[MB_LEFT] = ~0;
  35. m_buttons[MB_MIDDLE] = ~0;
  36. m_buttons[MB_RIGHT] = ~0;
  37. m_state[MB_LEFT] = false;
  38. m_state[MB_MIDDLE] = false;
  39. m_state[MB_RIGHT] = false;
  40. }
  41. //-----------------------------------------------------------------------------
  42. bool Mouse::button_pressed(MouseButton button) const
  43. {
  44. CE_ASSERT(button >= 0 && button < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", button);
  45. return (m_state[button] == true) && (m_buttons[button] == m_current_frame);
  46. }
  47. //-----------------------------------------------------------------------------
  48. bool Mouse::button_released(MouseButton button) const
  49. {
  50. CE_ASSERT(button >= 0 && button < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", button);
  51. return (m_state[button] == false) && (m_buttons[button] == m_current_frame);
  52. }
  53. //-----------------------------------------------------------------------------
  54. bool Mouse::any_pressed() const
  55. {
  56. return button_pressed(m_last_button);
  57. }
  58. //-----------------------------------------------------------------------------
  59. bool Mouse::any_released() const
  60. {
  61. return button_released(m_last_button);
  62. }
  63. //-----------------------------------------------------------------------------
  64. Vec2 Mouse::cursor_xy() const
  65. {
  66. int32_t x, y;
  67. device()->window()->get_cursor_xy(x, y);
  68. return Vec2(x, y);
  69. }
  70. //-----------------------------------------------------------------------------
  71. void Mouse::set_cursor_xy(const Vec2& position)
  72. {
  73. device()->window()->set_cursor_xy((int32_t) position.x, (int32_t) position.y);
  74. }
  75. //-----------------------------------------------------------------------------
  76. Vec2 Mouse::cursor_relative_xy() const
  77. {
  78. uint32_t window_width;
  79. uint32_t window_height;
  80. device()->window()->get_size(window_width, window_height);
  81. Vec2 pos = cursor_xy();
  82. pos.x = pos.x / (float) window_width;
  83. pos.y = pos.y / (float) window_height;
  84. return pos;
  85. }
  86. //-----------------------------------------------------------------------------
  87. void Mouse::set_cursor_relative_xy(const Vec2& position)
  88. {
  89. uint32_t window_width;
  90. uint32_t window_height;
  91. device()->window()->get_size(window_width, window_height);
  92. set_cursor_xy(Vec2(position.x * (float) window_width, position.y * (float) window_height));
  93. }
  94. //-----------------------------------------------------------------------------
  95. void Mouse::update(uint64_t frame, MouseButton b, bool state)
  96. {
  97. CE_ASSERT(b >= 0 && b < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", b);
  98. m_last_button = b;
  99. m_buttons[b] = frame;
  100. m_state[b] = state;
  101. }
  102. } // namespace crown