touch.h 3.2 KB

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