Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multi-touch touchscreens to SimpleSimulator #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 47 additions & 66 deletions SimpleSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,33 @@ void SimpleSimulator::processEvents()
tuioServer->resetTuioCursors();
stickyCursorList.clear();
jointCursorList.clear();
activeCursorList.clear();
activeCursorMap.clear();
}
break;

case SDL_MOUSEMOTION:
if (event.button.button == SDL_BUTTON_LEFT) mouseDragged((float)event.button.x/width, (float)event.button.y/height);
if (event.button.button == SDL_BUTTON_LEFT && event.motion.which != SDL_TOUCH_MOUSEID){
mouseDragged((float)event.button.x/width, (float)event.button.y/height, MOUSE_FINGER_ID);
}
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) mousePressed((float)event.button.x/width, (float)event.button.y/height);
if (event.button.button == SDL_BUTTON_LEFT && event.button.which != SDL_TOUCH_MOUSEID){
mousePressed((float)event.button.x/width, (float)event.button.y/height, MOUSE_FINGER_ID);
}
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT) mouseReleased((float)event.button.x/width, (float)event.button.y/height);
if (event.button.button == SDL_BUTTON_LEFT && event.button.which != SDL_TOUCH_MOUSEID){
mouseReleased((float)event.button.x/width, (float)event.button.y/height, MOUSE_FINGER_ID);
}
break;
case SDL_FINGERMOTION:
mouseDragged((float)event.tfinger.x, (float)event.tfinger.y, event.tfinger.fingerId);
break;
case SDL_FINGERDOWN:
mousePressed((float)event.tfinger.x, (float)event.tfinger.y, event.tfinger.fingerId);
break;
case SDL_FINGERUP:
mouseReleased((float)event.tfinger.x, (float)event.tfinger.y, event.tfinger.fingerId);
break;
case SDL_QUIT:
running = false;
Expand All @@ -176,7 +191,7 @@ void SimpleSimulator::processEvents()
}
}

void SimpleSimulator::mousePressed(float x, float y) {
void SimpleSimulator::mousePressed(float x, float y, long long int activeId) {
//printf("pressed %f %f\n",x,y);

TuioCursor *match = NULL;
Expand All @@ -189,101 +204,67 @@ void SimpleSimulator::mousePressed(float x, float y) {
match = tcur;
}
}

const Uint8 *keystate = SDL_GetKeyboardState(NULL);

const Uint8 *keystate = SDL_GetKeyboardState(NULL);
if ((keystate[SDL_SCANCODE_LSHIFT]) || (keystate[SDL_SCANCODE_RSHIFT])) {

if (match!=NULL) {
std::list<TuioCursor*>::iterator joint = std::find( jointCursorList.begin(), jointCursorList.end(), match );
if( joint != jointCursorList.end() ) {
jointCursorList.erase( joint );
}
stickyCursorList.remove(match);
activeCursorList.remove(match);
tuioServer->removeTuioCursor(match);
} else {
TuioCursor *cursor = tuioServer->addTuioCursor(x,y);
stickyCursorList.push_back(cursor);
activeCursorList.push_back(cursor);
activeCursorMap[activeId] = cursor;
}
} else if ((keystate[SDL_SCANCODE_LCTRL]) || (keystate[SDL_SCANCODE_RCTRL])) {

if (match!=NULL) {
std::list<TuioCursor*>::iterator joint = std::find( jointCursorList.begin(), jointCursorList.end(), match );
if( joint != jointCursorList.end() ) {
jointCursorList.remove( match );
} else jointCursorList.push_back(match);
}
} else if (match==NULL) {
TuioCursor *cursor = tuioServer->addTuioCursor(x,y);
activeCursorMap[activeId] = cursor;
} else {
if (match==NULL) {
TuioCursor *cursor = tuioServer->addTuioCursor(x,y);
activeCursorList.push_back(cursor);
} else activeCursorList.push_back(match);
activeCursorMap[activeId] = match;
}
}

void SimpleSimulator::mouseDragged(float x, float y) {
void SimpleSimulator::mouseDragged(float x, float y, long long int activeId) {
//printf("dragged %f %f\n",x,y);

TuioCursor *cursor = NULL;
float distance = width;
for (std::list<TuioCursor*>::iterator iter = activeCursorList.begin(); iter!=activeCursorList.end(); iter++) {
TuioCursor *tcur = (*iter);
float test = tcur->getDistance(x,y);
if (test<distance) {
distance = test;
cursor = tcur;
}
}
auto cursorIterator = activeCursorMap.find( activeId );
if( cursorIterator != activeCursorMap.end() ){
TuioCursor *cursor = cursorIterator->second;

if (cursor==NULL) return;
if (cursor->getTuioTime()==frameTime) return;
if (cursor->getTuioTime()==frameTime) return;

std::list<TuioCursor*>::iterator joint = std::find( jointCursorList.begin(), jointCursorList.end(), cursor );
if( joint != jointCursorList.end() ) {
float dx = x-cursor->getX();
float dy = y-cursor->getY();
for (std::list<TuioCursor*>::iterator iter = jointCursorList.begin(); iter!=jointCursorList.end(); iter++) {
TuioCursor *jointCursor = (*iter);
tuioServer->updateTuioCursor(jointCursor,jointCursor->getX()+dx,jointCursor->getY()+dy);
}
} else tuioServer->updateTuioCursor(cursor,x,y);
bool joint = ( std::find(jointCursorList.begin(), jointCursorList.end(), cursor) != jointCursorList.end() );
if( joint ) {
float dx = x-cursor->getX();
float dy = y-cursor->getY();
for (TuioCursor* jointCursor : jointCursorList) {
tuioServer->updateTuioCursor(jointCursor,jointCursor->getX()+dx,jointCursor->getY()+dy);
}
} else tuioServer->updateTuioCursor(cursor,x,y);
}
}

void SimpleSimulator::mouseReleased(float x, float y) {
void SimpleSimulator::mouseReleased(float x, float y, long long int activeId) {
//printf("released %f %f\n",x,y);

TuioCursor *cursor = NULL;
float distance = 0.01f;
for (std::list<TuioCursor*>::iterator iter = stickyCursorList.begin(); iter!=stickyCursorList.end(); iter++) {
TuioCursor *tcur = (*iter);
float test = tcur->getDistance(x,y);
if (test<distance) {
distance = test;
cursor = tcur;
}
}

if (cursor!=NULL) {
activeCursorList.remove(cursor);
return;
}

distance = 0.01f;
for (std::list<TuioCursor*>::iterator iter = activeCursorList.begin(); iter!=activeCursorList.end(); iter++) {
TuioCursor *tcur = (*iter);
float test = tcur->getDistance(x,y);
if (test<distance) {
distance = test;
cursor = tcur;
auto cursorIterator = activeCursorMap.find( activeId );
if( cursorIterator != activeCursorMap.end() ){
activeCursorMap.erase(cursorIterator);
bool sticky = ( std::find(stickyCursorList.begin(), stickyCursorList.end(), cursorIterator->second) != stickyCursorList.end());
if( !sticky ){
tuioServer->removeTuioCursor(cursorIterator->second);
}
}

if (cursor!=NULL) {
activeCursorList.remove(cursor);
tuioServer->removeTuioCursor(cursor);
}
}

SimpleSimulator::SimpleSimulator(TuioServer *server)
Expand Down
11 changes: 6 additions & 5 deletions SimpleSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "TuioCursor.h"
#include "osc/OscTypes.h"
#include <list>
#include <map>
#include <math.h>

#include "FlashSender.h"
Expand Down Expand Up @@ -54,7 +55,7 @@ class SimpleSimulator {
TuioServer *tuioServer;
std::list<TuioCursor*> stickyCursorList;
std::list<TuioCursor*> jointCursorList;
std::list<TuioCursor*> activeCursorList;
std::map<long long int, TuioCursor*> activeCursorMap;

private:
void drawFrame();
Expand All @@ -65,16 +66,16 @@ class SimpleSimulator {
SDL_Window *window;
SDL_Renderer *renderer;
bool verbose, fullupdate, periodic, fullscreen, running, help;
const static long long int MOUSE_FINGER_ID = 0x93BB922B64699C8C;

int width, height;
int screen_width, screen_height;
int window_width, window_height;
TuioTime frameTime;

void mousePressed(float x, float y);
void mouseReleased(float x, float y);
void mouseDragged(float x, float y);
//int s_id;
void mousePressed(float x, float y, long long int activeId);
void mouseReleased(float x, float y, long long int activeId);
void mouseDragged(float x, float y, long long int activeId);
};

#endif /* INCLUDED_SimpleSimulator_H */