Touch.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include "Types.h"
  25. #include "Vec2.h"
  26. namespace crown
  27. {
  28. const uint32_t MAX_POINTER_IDS = 4;
  29. struct PointerData
  30. {
  31. uint32_t x;
  32. uint32_t y;
  33. float relative_x;
  34. float relative_y;
  35. bool up;
  36. };
  37. struct TouchEvent
  38. {
  39. uint32_t pointer_id;
  40. uint32_t x;
  41. uint32_t y;
  42. };
  43. /// Interface for managing touch input device.
  44. class TouchListener
  45. {
  46. public:
  47. virtual void touch_down(const TouchEvent& event) { (void)event; }
  48. virtual void touch_up(const TouchEvent& event) { (void)event; }
  49. virtual void touch_move(const TouchEvent& event) { (void)event; }
  50. virtual void touch_cancel(const TouchEvent& event) { (void)event; }
  51. };
  52. /// Interface for accessing touch input device.
  53. class Touch
  54. {
  55. public:
  56. Touch();
  57. /// Returns whether the touch pointer @a id is up.
  58. bool touch_up(uint16_t id) const;
  59. /// Returns whether the touch pointer @a id is down.
  60. bool touch_down(uint16_t id) const;
  61. /// Returns the position of the pointer @a id in windows 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 touch_xy(uint16_t id) const;
  67. /// Returns the relative position of the pointer @a id 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. /// @note
  73. /// Relative coordinates are mapped to a float varying
  74. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  75. /// maximum extent of the cosidered axis.
  76. Vec2 touch_relative_xy(uint16_t id);
  77. private:
  78. PointerData m_pointers[MAX_POINTER_IDS];
  79. friend class InputManager;
  80. };
  81. }