TUIOInputModule.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. CoreServices::getInstance()->getCore()->lockMutex(eventMutex);
  39. events.push_back(event);
  40. CoreServices::getInstance()->getCore()->unlockMutex(eventMutex);
  41. }
  42. void TUIOInputModule::updateTuioCursor(TuioCursor *tcur) {
  43. std::vector<TouchInfo> touches;
  44. std::list<TuioCursor*> cursorList = tuioClient->getTuioCursors();
  45. tuioClient->lockCursorList();
  46. for (std::list<TuioCursor*>::iterator iter = cursorList.begin(); iter!=cursorList.end(); iter++) {
  47. TuioCursor *tuioCursor = (*iter);
  48. TouchInfo touch;
  49. touch.position.x = tuioCursor->getX();
  50. touch.position.y = tuioCursor->getY();
  51. touch.id= tuioCursor->getCursorID();
  52. touches.push_back(touch);
  53. }
  54. tuioClient->unlockCursorList();
  55. TUIOEvent event;
  56. event.type = InputEvent::EVENT_TOUCHES_MOVED;
  57. event.touches = touches;
  58. CoreServices::getInstance()->getCore()->lockMutex(eventMutex);
  59. events.push_back(event);
  60. CoreServices::getInstance()->getCore()->unlockMutex(eventMutex);
  61. }
  62. void TUIOInputModule::removeTuioCursor(TuioCursor *tcur) {
  63. std::vector<TouchInfo> touches;
  64. std::list<TuioCursor*> cursorList = tuioClient->getTuioCursors();
  65. tuioClient->lockCursorList();
  66. for (std::list<TuioCursor*>::iterator iter = cursorList.begin(); iter!=cursorList.end(); iter++) {
  67. TuioCursor *tuioCursor = (*iter);
  68. TouchInfo touch;
  69. touch.position.x = tuioCursor->getX();
  70. touch.position.y = tuioCursor->getY();
  71. touch.id= tuioCursor->getCursorID();
  72. touches.push_back(touch);
  73. }
  74. tuioClient->unlockCursorList();
  75. TUIOEvent event;
  76. event.type = InputEvent::EVENT_TOUCHES_ENDED;
  77. event.touches = touches;
  78. CoreServices::getInstance()->getCore()->lockMutex(eventMutex);
  79. events.push_back(event);
  80. CoreServices::getInstance()->getCore()->unlockMutex(eventMutex);
  81. }
  82. void TUIOInputModule::refresh(TuioTime frameTime) {
  83. }
  84. void TUIOInputModule::Update(Number elapsed) {
  85. CoreServices::getInstance()->getCore()->lockMutex(eventMutex);
  86. for(int i=0; i < events.size(); i++) {
  87. for(int j=0; j < events[i].touches.size(); j++) {
  88. events[i].touches[j].position.x = events[i].touches[j].position.x * CoreServices::getInstance()->getCore()->getXRes();
  89. events[i].touches[j].position.y = events[i].touches[j].position.y * CoreServices::getInstance()->getCore()->getYRes();
  90. }
  91. switch(events[i].type) {
  92. case InputEvent::EVENT_TOUCHES_BEGAN:
  93. CoreServices::getInstance()->getCore()->getInput()->touchesBegan(events[i].touches);
  94. break;
  95. case InputEvent::EVENT_TOUCHES_MOVED:
  96. CoreServices::getInstance()->getCore()->getInput()->touchesMoved(events[i].touches);
  97. break;
  98. case InputEvent::EVENT_TOUCHES_ENDED:
  99. CoreServices::getInstance()->getCore()->getInput()->touchesEnded(events[i].touches);
  100. break;
  101. }
  102. }
  103. events.clear();
  104. CoreServices::getInstance()->getCore()->unlockMutex(eventMutex);
  105. }