Skip to content

Commit

Permalink
Merge pull request #587 from openmobilemaps/bugfix/interaction-duplic…
Browse files Browse the repository at this point in the history
…ation

fix interaction
  • Loading branch information
maerki authored Jan 29, 2024
2 parents 85bfc50 + 826a3e1 commit e8866be
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 25 deletions.
87 changes: 62 additions & 25 deletions shared/src/map/controls/DefaultTouchHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ void DefaultTouchHandler::removeListener(const std::shared_ptr<TouchInterface> &

void DefaultTouchHandler::onTouchEvent(const TouchEvent &touchEvent) {

std::lock_guard<std::recursive_mutex> lock(stateMutex);

if (touchEvent.pointers.size() == 1) {

switch (touchEvent.touchAction) {
Expand Down Expand Up @@ -151,6 +153,9 @@ bool multiTouchMoved(std::tuple<Vec2F, Vec2F> &pointer1, std::tuple<Vec2F, Vec2F
}

void DefaultTouchHandler::handleTouchDown(Vec2F position) {

std::lock_guard<std::recursive_mutex> lock(stateMutex);

if (state == ONE_FINGER_UP_AFTER_CLICK && stateTime >= DateHelper::currentTimeMillis() - DOUBLE_TAP_TIMEOUT) {
state = ONE_FINGER_DOUBLE_CLICK_DOWN;
} else {
Expand All @@ -177,6 +182,9 @@ void DefaultTouchHandler::handleTouchDown(Vec2F position) {
}

void DefaultTouchHandler::handleMove(Vec2F delta) {

std::lock_guard<std::recursive_mutex> lock(stateMutex);

#ifdef ENABLE_TOUCH_LOGGING
LogDebug <<= "TouchHandler: handle move";
#endif
Expand Down Expand Up @@ -209,6 +217,9 @@ void DefaultTouchHandler::handleMove(Vec2F delta) {
}

void DefaultTouchHandler::handleTouchUp() {

std::lock_guard<std::recursive_mutex> lock(stateMutex);

if (state == ONE_FINGER_DOUBLE_CLICK_MOVE) {
#ifdef ENABLE_TOUCH_LOGGING
LogDebug <<= "TouchHandler: double click move ended";
Expand Down Expand Up @@ -289,6 +300,9 @@ void DefaultTouchHandler::handleTouchUp() {
}

void DefaultTouchHandler::handleTouchCancel() {

std::lock_guard<std::recursive_mutex> lock(stateMutex);

state = IDLE;
{
std::lock_guard<std::recursive_mutex> lock(listenerMutex);
Expand All @@ -300,6 +314,9 @@ void DefaultTouchHandler::handleTouchCancel() {
}

void DefaultTouchHandler::handleTwoFingerDown() {

std::lock_guard<std::recursive_mutex> lock(stateMutex);

if (state == ONE_FINGER_MOVING) {
{
std::lock_guard<std::recursive_mutex> lock(listenerMutex);
Expand Down Expand Up @@ -327,6 +344,9 @@ void DefaultTouchHandler::handleTwoFingerDown() {
}

void DefaultTouchHandler::handleTwoFingerMove(std::tuple<Vec2F, Vec2F> oldPointer, std::tuple<Vec2F, Vec2F> newpointer) {

std::lock_guard<std::recursive_mutex> lock(stateMutex);

if (state == ONE_FINGER_MOVING) {
{
std::lock_guard<std::recursive_mutex> lock(listenerMutex);
Expand All @@ -353,6 +373,9 @@ void DefaultTouchHandler::handleTwoFingerMove(std::tuple<Vec2F, Vec2F> oldPointe
}

void DefaultTouchHandler::handleTwoFingerUp(std::tuple<Vec2F, Vec2F> doubleTouchPointer) {

std::lock_guard<std::recursive_mutex> lock(stateMutex);

if (state != TWO_FINGER_DOWN) {
state = IDLE;
stateTime = DateHelper::currentTimeMillis();
Expand All @@ -368,6 +391,9 @@ void DefaultTouchHandler::handleTwoFingerUp(std::tuple<Vec2F, Vec2F> doubleTouch
}

void DefaultTouchHandler::handleMoreThanTwoFingers() {

std::lock_guard<std::recursive_mutex> lock(stateMutex);

if (state == ONE_FINGER_MOVING) {
{
std::lock_guard<std::recursive_mutex> lock(listenerMutex);
Expand All @@ -389,36 +415,47 @@ void DefaultTouchHandler::handleMoreThanTwoFingers() {
}

void DefaultTouchHandler::checkState() {
if (state == ONE_FINGER_UP_AFTER_CLICK && stateTime <= DateHelper::currentTimeMillis() - DOUBLE_TAP_TIMEOUT) {

bool onClickConfirmed = false;
bool onLongPress = false;

{ // begin lock stateMutex
std::lock_guard<std::recursive_mutex> lock(stateMutex);

if (state == ONE_FINGER_UP_AFTER_CLICK && stateTime <= DateHelper::currentTimeMillis() - DOUBLE_TAP_TIMEOUT) {
#ifdef ENABLE_TOUCH_LOGGING
LogDebug <<= "TouchHandler: confirmed click detected";
LogDebug <<= "TouchHandler: confirmed click detected";
#endif
{
std::lock_guard<std::recursive_mutex> lock(listenerMutex);
for (auto &[index, listener]: listeners) {
if (listener->onClickConfirmed(touchPosition)) {
break;
}
}
}
state = IDLE;
stateTime = DateHelper::currentTimeMillis();
} else if (state == ONE_FINGER_DOWN && stateTime <= DateHelper::currentTimeMillis() - LONG_PRESS_TIMEOUT) {
onClickConfirmed = true;
state = IDLE;
stateTime = DateHelper::currentTimeMillis();
} else if (state == ONE_FINGER_DOWN && stateTime <= DateHelper::currentTimeMillis() - LONG_PRESS_TIMEOUT) {
#ifdef ENABLE_TOUCH_LOGGING
LogDebug <<= "TouchHandler: long press detected";
LogDebug <<= "TouchHandler: long press detected";
#endif
{
std::lock_guard<std::recursive_mutex> lock(listenerMutex);
for (auto &[index, listener]: listeners) {
if (listener->onLongPress(touchPosition)) {
break;
}
onLongPress = true;
state = ONE_FINGER_MOVING; // prevents further single click and allows to transition from long press to moving
stateTime = DateHelper::currentTimeMillis();
} else if (state == TWO_FINGER_DOWN && stateTime <= DateHelper::currentTimeMillis() - LONG_PRESS_TIMEOUT) {
state = TWO_FINGER_MOVING;
stateTime = DateHelper::currentTimeMillis();
}
} // end lock stateMutex

if (onClickConfirmed) {
std::lock_guard<std::recursive_mutex> lock(listenerMutex);
for (auto &[index, listener]: listeners) {
if (listener->onClickConfirmed(touchPosition)) {
break;
}
}
}
else if (onLongPress) {
std::lock_guard<std::recursive_mutex> lock(listenerMutex);
for (auto &[index, listener]: listeners) {
if (listener->onLongPress(touchPosition)) {
break;
}
}
state = ONE_FINGER_MOVING; // prevents further single click and allows to transition from long press to moving
stateTime = DateHelper::currentTimeMillis();
} else if (state == TWO_FINGER_DOWN && stateTime <= DateHelper::currentTimeMillis() - LONG_PRESS_TIMEOUT) {
state = TWO_FINGER_MOVING;
stateTime = DateHelper::currentTimeMillis();
}
}
2 changes: 2 additions & 0 deletions shared/src/map/controls/DefaultTouchHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class DefaultTouchHandler : public TouchHandlerInterface {

void handleMoreThanTwoFingers();

std::recursive_mutex stateMutex;

void checkState();

int32_t TWO_FINGER_TOUCH_TIMEOUT = 100;
Expand Down

0 comments on commit e8866be

Please sign in to comment.