TUIOInputModule.cpp 4.6 KB

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