touch.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "types.h"
  7. #include "vector2.h"
  8. #include <cstring> // mem*
  9. namespace crown
  10. {
  11. /// Maximum number of pointers supported by Touch.
  12. ///
  13. /// @ingroup Input
  14. const uint32_t MAX_POINTER_IDS = 4;
  15. /// Interface for accessing touch panel input device.
  16. ///
  17. /// @ingroup Input
  18. struct Touch
  19. {
  20. Touch()
  21. : _last_pointer(0xFF)
  22. {
  23. memset(_last_state, 0, MAX_POINTER_IDS);
  24. memset(_current_state, 0, MAX_POINTER_IDS);
  25. }
  26. /// Returns whether the @a p pointer is pressed in the current frame.
  27. bool pointer_down(uint8_t p)
  28. {
  29. if (p >= MAX_POINTER_IDS) return false;
  30. return (~_last_state[p] & _current_state[p]) != 0;
  31. }
  32. /// Returns whether the @a p pointer is released in the current frame.
  33. bool pointer_up(uint8_t p)
  34. {
  35. if (p >= MAX_POINTER_IDS) return false;
  36. return (_last_state[p] & ~_current_state[p]) != 0;
  37. }
  38. /// Returns wheter any pointer is pressed in the current frame.
  39. bool any_down()
  40. {
  41. return pointer_down(_last_pointer);
  42. }
  43. /// Returns whether any pointer is released in the current frame.
  44. bool any_up()
  45. {
  46. return pointer_up(_last_pointer);
  47. }
  48. /// Returns the position of the pointer @a p in window space.
  49. /// @note
  50. /// Coordinates in window space have the origin at the
  51. /// upper-left corner of the window. +X extends from left
  52. /// to right and +Y extends from top to bottom.
  53. Vector2 pointer_xy(uint8_t p)
  54. {
  55. if (p >= MAX_POINTER_IDS) return vector2::ZERO;
  56. return Vector2(_x[p], _y[p]);
  57. }
  58. void set_position(uint8_t p, uint16_t x, uint16_t y)
  59. {
  60. if (p >= MAX_POINTER_IDS) return;
  61. _x[p] = x;
  62. _y[p] = y;
  63. }
  64. void set_metrics(uint16_t width, uint16_t height)
  65. {
  66. _width = width;
  67. _height = height;
  68. }
  69. void set_pointer_state(uint16_t x, uint16_t y, uint8_t p, bool state)
  70. {
  71. set_position(p, x, y);
  72. _last_pointer = p;
  73. _current_state[p] = state;
  74. }
  75. void update()
  76. {
  77. memcpy(_last_state, _current_state, MAX_POINTER_IDS);
  78. }
  79. public:
  80. uint8_t _last_pointer;
  81. uint8_t _last_state[MAX_POINTER_IDS];
  82. uint8_t _current_state[MAX_POINTER_IDS];
  83. uint16_t _x[MAX_POINTER_IDS];
  84. uint16_t _y[MAX_POINTER_IDS];
  85. // Window size
  86. uint16_t _width;
  87. uint16_t _height;
  88. };
  89. } // namespace crown