Input.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "Input.h"
  2. #include "Stage.h"
  3. #include "core/log.h"
  4. #include <string.h>
  5. //#define LOGD(...) oxygine::log::message("input: "); oxygine::log::messageln(__VA_ARGS__)
  6. #define LOGD(...) ((void)0)
  7. namespace oxygine
  8. {
  9. Input Input::instance;
  10. const PointerState* TouchEvent::getPointer() const
  11. {
  12. return Input::instance.getTouchByIndex(index);
  13. }
  14. void Input::sendPointerButtonEvent(spStage stage, MouseButton button, float x, float y, float pressure, int type, PointerState* ps)
  15. {
  16. if (!_multiTouch && ps->getIndex() != 1 && ps != &_pointerMouse)
  17. return;
  18. Vector2 p(x, y);
  19. TouchEvent me(type, true, p);
  20. me.index = ps->getIndex();
  21. me.mouseButton = button;
  22. me.pressure = pressure;
  23. if (type == TouchEvent::TOUCH_DOWN)
  24. ps->_pressed |= 1 << button;
  25. else if (type == TouchEvent::TOUCH_UP)
  26. ps->_pressed &= ~(1 << button);
  27. ps->_position = p;
  28. LOGD("sendPointerButtonEvent %d - (%.2f, %.2f), %d", me.index, p.x, p.y, type);
  29. stage->handleEvent(&me);
  30. if (type == TouchEvent::TOUCH_UP)
  31. {
  32. _ids[ps->getIndex() - 1] = 0;
  33. }
  34. }
  35. void Input::sendPointerMotionEvent(spStage stage, float x, float y, float pressure, PointerState* ps)
  36. {
  37. if (!_multiTouch && ps->getIndex() != 1 && ps != &_pointerMouse)
  38. return;
  39. TouchEvent me(TouchEvent::MOVE, true, Vector2(x, y));
  40. me.index = ps->getIndex();
  41. me.pressure = pressure;
  42. ps->_position = Vector2(x, y);
  43. LOGD("sendPointerMotionEvent %d - (%.2f, %.2f)", me.index, x, y);
  44. stage->handleEvent(&me);
  45. }
  46. void Input::sendPointerWheelEvent(spStage stage, int scroll, PointerState* ps)
  47. {
  48. TouchEvent me(scroll > 0 ? TouchEvent::WHEEL_UP : TouchEvent::WHEEL_DOWN, true);
  49. me.index = ps->getIndex();
  50. ps->_position = Vector2(0, 0);
  51. stage->handleEvent(&me);
  52. }
  53. Input::Input()
  54. {
  55. _pointerMouse.init(MAX_TOUCHES + 1);
  56. for (int i = 0; i < MAX_TOUCHES; ++i)
  57. _pointers[i].init(i + 1);
  58. memset(_ids, 0, sizeof(_ids));
  59. _multiTouch = true;
  60. }
  61. Input::~Input()
  62. {
  63. }
  64. void Input::cleanup()
  65. {
  66. }
  67. void Input::multiTouchEnabled(bool en)
  68. {
  69. _multiTouch = en;
  70. }
  71. PointerState* Input::getTouchByIndex(pointer_index index_)
  72. {
  73. int index = index_;
  74. if (index == MAX_TOUCHES + 1)
  75. return &_pointerMouse;
  76. index -= 1;
  77. OX_ASSERT(index >= 0 && index < MAX_TOUCHES);
  78. index = std::min(std::max(index, 0), MAX_TOUCHES);
  79. return &_pointers[index];
  80. }
  81. #ifndef __S3E__
  82. int Input::touchID2index(int64 id)
  83. {
  84. // We can't be sure that SDL's fingerId is not 0,
  85. // but 0 is reserved for empty slot, so increment id by one:
  86. id += 1;
  87. int firstEmptySlotIndex = -1;
  88. for (int i = 0; i < MAX_TOUCHES; ++i)
  89. {
  90. int64& d = _ids[i];
  91. if (d == id)
  92. return i + 1;
  93. if (d == 0 && firstEmptySlotIndex == -1)
  94. firstEmptySlotIndex = i;
  95. }
  96. if (firstEmptySlotIndex != -1)
  97. {
  98. _ids[firstEmptySlotIndex] = id;
  99. return firstEmptySlotIndex + 1;
  100. }
  101. //log::warning("can't find touch id %d", id);
  102. return -1;
  103. }
  104. PointerState* Input::getTouchByID(int64 id)
  105. {
  106. int i = touchID2index(id);
  107. if (i == -1)
  108. return 0;
  109. return getTouchByIndex(i);
  110. }
  111. #endif
  112. }