Mouse.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "Mouse.h"
  23. #include "OS.h"
  24. namespace crown
  25. {
  26. //-----------------------------------------------------------------------------
  27. Mouse::Mouse()
  28. {
  29. m_buttons[MB_LEFT] = false;
  30. m_buttons[MB_MIDDLE] = false;
  31. m_buttons[MB_RIGHT] = false;
  32. }
  33. //-----------------------------------------------------------------------------
  34. bool Mouse::button_pressed(MouseButton button) const
  35. {
  36. return m_buttons[button] == true;
  37. }
  38. //-----------------------------------------------------------------------------
  39. bool Mouse::button_released(MouseButton button) const
  40. {
  41. return m_buttons[button] == false;
  42. }
  43. //-----------------------------------------------------------------------------
  44. Vec2 Mouse::cursor_xy() const
  45. {
  46. int32_t x, y;
  47. os::get_cursor_xy(x, y);
  48. return Vec2(x, y);
  49. }
  50. //-----------------------------------------------------------------------------
  51. void Mouse::set_cursor_xy(const Vec2& position)
  52. {
  53. os::set_cursor_xy(position.x, position.y);
  54. }
  55. //-----------------------------------------------------------------------------
  56. Vec2 Mouse::cursor_relative_xy() const
  57. {
  58. uint32_t window_width;
  59. uint32_t window_height;
  60. os::get_render_window_metrics(window_width, window_height);
  61. Vec2 pos = cursor_xy();
  62. pos.x = pos.x / (float) window_width;
  63. pos.y = pos.y / (float) window_height;
  64. return pos;
  65. }
  66. //-----------------------------------------------------------------------------
  67. void Mouse::set_cursor_relative_xy(const Vec2& position)
  68. {
  69. uint32_t window_width;
  70. uint32_t window_height;
  71. os::get_render_window_metrics(window_width, window_height);
  72. set_cursor_xy(Vec2(position.x * (float) window_width, position.y * (float) window_height));
  73. }
  74. } // namespace crown