Touch.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "Vector2.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. /// Interface for accessing touch input device.
  38. struct Touch
  39. {
  40. /// Returns whether the touch pointer @a id is up.
  41. bool touch_up(uint16_t id) const
  42. {
  43. return m_pointers[id].up == true;
  44. }
  45. /// Returns whether the touch pointer @a id is down.
  46. bool touch_down(uint16_t id) const
  47. {
  48. return m_pointers[id].up == false;
  49. }
  50. /// Returns the position of the pointer @a id in windows space.
  51. /// @note
  52. /// Coordinates in window space have the origin at the
  53. /// upper-left corner of the window. +X extends from left
  54. /// to right and +Y extends from top to bottom.
  55. Vector2 touch_xy(uint16_t id) const
  56. {
  57. const PointerData& data = m_pointers[id];
  58. return Vector2(data.x, data.y);
  59. }
  60. /// Returns the relative position of the pointer @a id in window space.
  61. /// @note
  62. /// Coordinates in window space have the origin at the
  63. /// upper-left corner of the window. +X extends from left
  64. /// to right and +Y extends from top to bottom.
  65. /// @note
  66. /// Relative coordinates are mapped to a float varying
  67. /// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
  68. /// maximum extent of the cosidered axis.
  69. Vector2 touch_relative_xy(uint16_t id)
  70. {
  71. const PointerData& data = m_pointers[id];
  72. return Vector2(data.relative_x, data.relative_y);
  73. }
  74. public:
  75. PointerData m_pointers[MAX_POINTER_IDS];
  76. };
  77. }