Mouse.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. namespace crown
  28. {
  29. const uint32_t MAX_MOUSE_BUTTONS = 3;
  30. /// Enumerates mouse buttons.
  31. enum MouseButton
  32. {
  33. MB_LEFT = 0,
  34. MB_MIDDLE = 1,
  35. MB_RIGHT = 2
  36. };
  37. struct MouseEvent
  38. {
  39. MouseButton button;
  40. int32_t x;
  41. int32_t y;
  42. float wheel;
  43. };
  44. /// Interface for managing mouse input.
  45. class MouseListener
  46. {
  47. public:
  48. virtual void button_pressed(const MouseEvent& event) { (void)event; }
  49. virtual void button_released(const MouseEvent& event) { (void)event; }
  50. virtual void cursor_moved(const MouseEvent& event) { (void)event; }
  51. };
  52. /// Interface for accessing mouse input device.
  53. class Mouse
  54. {
  55. public:
  56. Mouse();
  57. /// Returns whether @a button is pressed.
  58. bool button_pressed(MouseButton button) const;
  59. /// Returns whether @a button is released.
  60. bool button_released(MouseButton button) const;
  61. /// Returns the position of the cursor in window space.
  62. /// @note
  63. /// Coordinates in window space have the origin at the
  64. /// upper-left corner of the window. +X extends from left
  65. /// to right and +Y extends from top to bottom.
  66. Vec2 cursor_xy() const;
  67. /// Sets the position of the cursor in window space.
  68. /// @note
  69. /// Coordinates in window space have the origin at the
  70. /// upper-left corner of the window. +X extends from left
  71. /// to right and +Y extends from top to bottom.
  72. void set_cursor_xy(const Vec2& position);
  73. /// Returns the relative position of the cursor in window space.
  74. /// @note
  75. /// Coordinates in window space have the origin at the
  76. /// upper-left corner of the window. +X extends from left
  77. /// to right and +Y extends from top to bottom.
  78. /// @note
  79. /// Relative coordinates are mapped to a float varying
  80. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  81. /// maximum extent of the cosidered axis.
  82. Vec2 cursor_relative_xy() const;
  83. /// Sets the relative position of the cursor in window space.
  84. /// @note
  85. /// Coordinates in window space have the origin at the
  86. /// upper-left corner of the window. +X extends from left
  87. /// to right and +Y extends from top to bottom.
  88. /// @note
  89. /// Relative coordinates are mapped to a float varying
  90. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  91. /// maximum extent of the cosidered axis.
  92. void set_cursor_relative_xy(const Vec2& position);
  93. private:
  94. // True if correspondig button is pressed, false otherwise.
  95. bool m_buttons[MAX_MOUSE_BUTTONS];
  96. friend class InputManager;
  97. };
  98. } // namespace crown