Skip to content

Commit

Permalink
FIX: Single and double click touch gestures
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurnishimoto committed Apr 6, 2019
1 parent d56b53d commit ec3bd09
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
5 changes: 5 additions & 0 deletions include/omicron/TouchGestureManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ namespace omicron {
bool threeFingerGestureTriggered;
bool bigTouchGestureTriggered;
bool zoomGestureTriggered;
bool singleClickTriggered;
bool doubleClickTriggered;
public:
TouchGroup(TouchGestureManager*, int);
Expand Down Expand Up @@ -140,6 +141,9 @@ namespace omicron {
float getZoomDelta();
float getDiameter();
float getLongRangeDiameter();

bool isSingleClickTriggered();
bool isDoubleClickTriggered();

void removeTouch(int id);
Event::Type getEventType();
Expand Down Expand Up @@ -167,6 +171,7 @@ namespace omicron {
private:
Service* pqsInstance;
Lock* touchListLock;
map<int, Touch> rawTouchList;
Lock* touchGroupListLock;
map<int,TouchGroup*> touchGroupList;
set<int> groupedIDs;
Expand Down
73 changes: 64 additions & 9 deletions src/omicron/omicron/TouchGestureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ TouchGroup::TouchGroup(TouchGestureManager* gm, int ID){
threeFingerGestureTriggered = false;
zoomGestureTriggered = false;
bigTouchGestureTriggered = false;
singleClickTriggered = false;
doubleClickTriggered = false;
}

Expand Down Expand Up @@ -137,14 +138,14 @@ void TouchGroup::addTouch( Event::Type eventType, float x, float y, int touchID,
if (tg != NULL)
{
tg->removeTouch(touchID);
ofmsg("TouchGroup %1% absorbed touch ID %2%", %ID %touchID);
// ofmsg("TouchGroup %1% absorbed touch ID %2%", %ID %touchID);
}
}

if( eventType == Event::Up )
{
removeTouch(touchID);
ofmsg("TouchGroup %1% removed touch ID %2% new size: %3%", %ID %touchID %getTouchCount() );
// ofmsg("TouchGroup %1% removed touch ID %2% new size: %3%", %ID %touchID %getTouchCount() );
}
else
{
Expand Down Expand Up @@ -183,7 +184,7 @@ void TouchGroup::addTouch( Event::Type eventType, float x, float y, int touchID,
centerTouch = t;
gestureManager->generatePQServiceEvent(Event::Down, this, GESTURE_SINGLE_TOUCH);
}
ofmsg("TouchGroup %1% added touch ID %2% new size: %3%", %ID %touchID %getTouchCount());
// ofmsg("TouchGroup %1% added touch ID %2% new size: %3%", %ID %touchID %getTouchCount());
}
else
{
Expand Down Expand Up @@ -312,8 +313,13 @@ void TouchGroup::process(){

unlockTouchList();

centerTouch.xPos = newCenterX;
centerTouch.yPos = newCenterY;
// Don't update center if list is empty to preserve touch group's position
// for double click detection
if (touchList.size() > 0)
{
centerTouch.xPos = newCenterX;
centerTouch.yPos = newCenterY;
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -480,7 +486,21 @@ int TouchGroup::getGestureFlag(){
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Returns the remove flag
bool TouchGroup::isRemovable(){
return remove;
timeb tb;
ftime(&tb);
int curTime = tb.millitm + (tb.time & 0xfffff) * 1000;
int timeSinceLastUpdate = curTime - lastUpdated;

// Allow a small delay between when the group was marked for removed (due to touch group size 0)
// and when it is really removed to allow for double click detection
if (timeSinceLastUpdate > touchGroupTimeout)
{
return remove;
}
else
{
return false;
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -523,12 +543,22 @@ float TouchGroup::getLongRangeDiameter() {
return longRangeDiameter;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TouchGroup::isSingleClickTriggered() {
return singleClickTriggered;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TouchGroup::isDoubleClickTriggered() {
return doubleClickTriggered;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TouchGroup::removeTouch(int id) {
touchListLock->lock();
touchList.erase(id);

ofmsg("TouchGroup %1% removed touch ID %2%", %ID %id);
// ofmsg("TouchGroup %1% removed touch ID %2%", %ID %id);
if (id == mainID)
{
// Main ID (controlling group) has been removed
Expand All @@ -538,13 +568,25 @@ void TouchGroup::removeTouch(int id) {
{
Touch t = (*it).second;
mainID = t.ID;
ofmsg(" Remaining IDs: %1%", %t.ID);
}
}
touchListLock->unlock();
ofmsg(" Remaining IDs count %1%", %getTouchCount());

if (getTouchCount() == 0)
{
// If touch group size and zero and inital position < clickMaxDistance, trigger click gesture
float distFromInitPos = sqrt(abs(mainTouch.initXPos - mainTouch.xPos) * abs(mainTouch.initXPos - mainTouch.xPos) + abs(mainTouch.initYPos - mainTouch.yPos) * abs(mainTouch.initYPos - mainTouch.yPos));
if (distFromInitPos <= clickMaxDistance)
{
if (!singleClickTriggered)
{
singleClickTriggered = true;
}
else
{
doubleClickTriggered = true;
}
}
setRemove();
}
}
Expand Down Expand Up @@ -616,6 +658,19 @@ void TouchGestureManager::poll()
}
else
{
Touch mainTouch = tg->getMainTouch();

if (tg->isDoubleClickTriggered())
{
ofmsg("TouchGestureManager: TouchGroup %1% double click", %tg->getID());
generatePQServiceEvent(Event::Down, tg, GESTURE_DOUBLE_CLICK);
}
else if (tg->isSingleClickTriggered())
{
ofmsg("TouchGestureManager: TouchGroup %1% single click", %tg->getID());
generatePQServiceEvent(Event::Down, tg, GESTURE_SINGLE_CLICK);
}

ofmsg("TouchGestureManager: TouchGroup %1% empty. Removed.", %tg->getID());
generatePQServiceEvent( Event::Up, tg, tg->getGestureFlag() );
}
Expand Down

0 comments on commit ec3bd09

Please sign in to comment.