2
0

TUIOInputModule.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "TUIOInputModule.h"
  2. #include <Polycode.h>
  3. TUIOInputModule::TUIOInputModule(int port) : PolycodeModule(), TuioListener() {
  4. tuioClient = new TuioClient(port);
  5. tuioClient->addTuioListener(this);
  6. tuioClient->connect();
  7. eventMutex = CoreServices::getInstance()->getCore()->createMutex();
  8. _requiresUpdate = true;
  9. }
  10. TUIOInputModule::~TUIOInputModule() {
  11. tuioClient->removeAllTuioListeners();
  12. tuioClient->disconnect();
  13. delete tuioClient;
  14. delete eventMutex;
  15. }
  16. void TUIOInputModule::addTuioObject(TuioObject *tobj) {
  17. }
  18. void TUIOInputModule::updateTuioObject(TuioObject *tobj) {
  19. }
  20. void TUIOInputModule::removeTuioObject(TuioObject *tobj) {
  21. }
  22. void TUIOInputModule::addTuioCursor(TuioCursor *tcur) {
  23. std::vector<TouchInfo> touches;
  24. std::list<TuioCursor*> cursorList = tuioClient->getTuioCursors();
  25. tuioClient->lockCursorList();
  26. for (std::list<TuioCursor*>::iterator iter = cursorList.begin(); iter!=cursorList.end(); iter++) {
  27. TuioCursor *tuioCursor = (*iter);
  28. TouchInfo touch;
  29. touch.position.x = tuioCursor->getX();
  30. touch.position.y = tuioCursor->getY();
  31. touch.id= tuioCursor->getCursorID();
  32. touches.push_back(touch);
  33. }
  34. tuioClient->unlockCursorList();
  35. TUIOEvent event;
  36. event.type = InputEvent::EVENT_TOUCHES_BEGAN;
  37. event.touches = touches;
  38. event.touch.position.x = tcur->getX();
  39. event.touch.position.y = tcur->getY();
  40. event.touch.id = tcur->getCursorID();
  41. CoreServices::getInstance()->getCore()->lockMutex(eventMutex);
  42. events.push_back(event);
  43. CoreServices::getInstance()->getCore()->unlockMutex(eventMutex);
  44. }
  45. void TUIOInputModule::updateTuioCursor(TuioCursor *tcur) {
  46. std::vector<TouchInfo> touches;
  47. std::list<TuioCursor*> cursorList = tuioClient->getTuioCursors();
  48. tuioClient->lockCursorList();
  49. for (std::list<TuioCursor*>::iterator iter = cursorList.begin(); iter!=cursorList.end(); iter++) {
  50. TuioCursor *tuioCursor = (*iter);
  51. TouchInfo touch;
  52. touch.position.x = tuioCursor->getX();
  53. touch.position.y = tuioCursor->getY();
  54. touch.id= tuioCursor->getCursorID();
  55. touches.push_back(touch);
  56. }
  57. tuioClient->unlockCursorList();
  58. TUIOEvent event;
  59. event.type = InputEvent::EVENT_TOUCHES_MOVED;
  60. event.touches = touches;
  61. event.touch.position.x = tcur->getX();
  62. event.touch.position.y = tcur->getY();
  63. event.touch.id = tcur->getCursorID();
  64. CoreServices::getInstance()->getCore()->lockMutex(eventMutex);
  65. events.push_back(event);
  66. CoreServices::getInstance()->getCore()->unlockMutex(eventMutex);
  67. }
  68. void TUIOInputModule::removeTuioCursor(TuioCursor *tcur) {
  69. std::vector<TouchInfo> touches;
  70. std::list<TuioCursor*> cursorList = tuioClient->getTuioCursors();
  71. tuioClient->lockCursorList();
  72. for (std::list<TuioCursor*>::iterator iter = cursorList.begin(); iter!=cursorList.end(); iter++) {
  73. TuioCursor *tuioCursor = (*iter);
  74. TouchInfo touch;
  75. touch.position.x = tuioCursor->getX();
  76. touch.position.y = tuioCursor->getY();
  77. touch.id= tuioCursor->getCursorID();
  78. touches.push_back(touch);
  79. }
  80. tuioClient->unlockCursorList();
  81. TUIOEvent event;
  82. event.type = InputEvent::EVENT_TOUCHES_ENDED;
  83. event.touches = touches;
  84. event.touch.position.x = tcur->getX();
  85. event.touch.position.y = tcur->getY();
  86. event.touch.id = tcur->getCursorID();
  87. CoreServices::getInstance()->getCore()->lockMutex(eventMutex);
  88. events.push_back(event);
  89. CoreServices::getInstance()->getCore()->unlockMutex(eventMutex);
  90. }
  91. void TUIOInputModule::refresh(TuioTime frameTime) {
  92. }
  93. void TUIOInputModule::Update(Number elapsed) {
  94. Core *core = CoreServices::getInstance()->getCore();
  95. core->lockMutex(core->eventMutex);
  96. CoreServices::getInstance()->getCore()->lockMutex(eventMutex);
  97. for(int i=0; i < events.size(); i++) {
  98. for(int j=0; j < events[i].touches.size(); j++) {
  99. events[i].touches[j].position.x = events[i].touches[j].position.x * core->getXRes();
  100. events[i].touches[j].position.y = events[i].touches[j].position.y * core->getYRes();
  101. }
  102. events[i].touch.position.x = events[i].touch.position.x * core->getXRes();
  103. events[i].touch.position.y = events[i].touch.position.y * core->getYRes();
  104. switch(events[i].type) {
  105. case InputEvent::EVENT_TOUCHES_BEGAN:
  106. CoreServices::getInstance()->getCore()->getInput()->touchesBegan(events[i].touch, events[i].touches, core->getTicks());
  107. break;
  108. case InputEvent::EVENT_TOUCHES_MOVED:
  109. CoreServices::getInstance()->getCore()->getInput()->touchesMoved(events[i].touch, events[i].touches, core->getTicks());
  110. break;
  111. case InputEvent::EVENT_TOUCHES_ENDED:
  112. CoreServices::getInstance()->getCore()->getInput()->touchesEnded(events[i].touch, events[i].touches, core->getTicks());
  113. break;
  114. }
  115. }
  116. events.clear();
  117. core->unlockMutex(core->eventMutex);
  118. CoreServices::getInstance()->getCore()->unlockMutex(eventMutex);
  119. }