Touch.h 3.5 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. #pragma once
  24. #include "Types.h"
  25. #include "Vector2.h"
  26. namespace crown
  27. {
  28. const uint32_t MAX_POINTER_IDS = 4;
  29. /// Interface for accessing mouse input device.
  30. class Touch
  31. {
  32. public:
  33. //-----------------------------------------------------------------------------
  34. Touch()
  35. : m_last_pointer(0xFF)
  36. {
  37. memset(m_last_state, 0, MAX_POINTER_IDS);
  38. memset(m_current_state, 0, MAX_POINTER_IDS);
  39. }
  40. /// Returns whether the @a p pointer is pressed in the current frame.
  41. bool pointer_down(uint8_t p)
  42. {
  43. if (p >= MAX_POINTER_IDS) return false;
  44. return bool(~m_last_state[p] & m_current_state[p]);
  45. }
  46. /// Returns whether the @a p pointer is released in the current frame.
  47. bool pointer_up(uint8_t p)
  48. {
  49. if (p >= MAX_POINTER_IDS) return false;
  50. return bool(m_last_state[p] & ~m_current_state[p]);
  51. }
  52. /// Returns wheter any pointer is pressed in the current frame.
  53. bool any_down()
  54. {
  55. return pointer_down(m_last_pointer);
  56. }
  57. /// Returns whether any pointer is released in the current frame.
  58. bool any_up()
  59. {
  60. return pointer_up(m_last_pointer);
  61. }
  62. /// Returns the position of the pointer @a p in window space.
  63. /// @note
  64. /// Coordinates in window space have the origin at the
  65. /// upper-left corner of the window. +X extends from left
  66. /// to right and +Y extends from top to bottom.
  67. Vector2 pointer_xy(uint8_t p)
  68. {
  69. if (p >= MAX_POINTER_IDS) return Vector2::ZERO;
  70. return Vector2(m_x[p], m_y[p]);
  71. }
  72. //-----------------------------------------------------------------------------
  73. void set_position(uint8_t p, uint16_t x, uint16_t y)
  74. {
  75. if (p >= MAX_POINTER_IDS) return;
  76. m_x[p] = x;
  77. m_y[p] = y;
  78. }
  79. //-----------------------------------------------------------------------------
  80. void set_metrics(uint16_t width, uint16_t height)
  81. {
  82. m_width = width;
  83. m_height = height;
  84. }
  85. //-----------------------------------------------------------------------------
  86. void set_pointer_state(uint16_t x, uint16_t y, uint8_t p, bool state)
  87. {
  88. set_position(p, x, y);
  89. m_last_pointer = p;
  90. m_current_state[p] = state;
  91. }
  92. //-----------------------------------------------------------------------------
  93. void update()
  94. {
  95. memcpy(m_last_state, m_current_state, MAX_POINTER_IDS);
  96. }
  97. public:
  98. uint8_t m_last_pointer;
  99. uint8_t m_last_state[MAX_POINTER_IDS];
  100. uint8_t m_current_state[MAX_POINTER_IDS];
  101. uint16_t m_x[MAX_POINTER_IDS];
  102. uint16_t m_y[MAX_POINTER_IDS];
  103. // Window size
  104. uint16_t m_width;
  105. uint16_t m_height;
  106. };
  107. } // namespace crown