-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
|
||
#include <UIGestureRecognizer.h> | ||
#include <Timer.h> | ||
#include <Geometry.h> | ||
|
||
namespace NXKit { | ||
|
||
class UIPanGestureRecognizer: public UIGestureRecognizer { | ||
public: | ||
void touchesBegan(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) override; | ||
void touchesMoved(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) override; | ||
void touchesEnded(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) override; | ||
|
||
NXPoint translationInView(std::shared_ptr<UIView> view); | ||
void setTranslation(NXPoint translation, std::shared_ptr<UIView> inView); | ||
|
||
NXPoint velocityIn(std::shared_ptr<UIView> view); | ||
|
||
private: | ||
std::shared_ptr<UITouch> trackingTouch; | ||
NXPoint initialTouchPoint; | ||
Timer touchesMovedTimestamp; | ||
Timer previousTouchesMovedTimestamp; | ||
|
||
NXPoint velocityIn(std::shared_ptr<UIView> view, float timeDiffSeconds); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <UIPanGestureRecognizer.h> | ||
#include <UITouch.h> | ||
#include <UIWindow.h> | ||
|
||
namespace NXKit { | ||
|
||
#define THRESHOLD 10 | ||
|
||
NXPoint UIPanGestureRecognizer::translationInView(std::shared_ptr<UIView> view) { | ||
if (!trackingTouch) return NXPoint(); | ||
|
||
auto positionInTargetView = trackingTouch->locationIn(view); | ||
NXPoint initialPositionInTargetView; | ||
if (trackingTouch->window().expired()) initialPositionInTargetView = initialTouchPoint; | ||
else { initialPositionInTargetView = view ? trackingTouch->window().lock()->convertToView(initialTouchPoint, view) : initialTouchPoint; } | ||
|
||
return positionInTargetView - initialPositionInTargetView; | ||
} | ||
|
||
void UIPanGestureRecognizer::setTranslation(NXPoint translation, std::shared_ptr<UIView> inView) { | ||
if (!trackingTouch) return; | ||
|
||
auto positionInTargetView = trackingTouch->locationIn(nullptr); | ||
initialTouchPoint = positionInTargetView - translation; | ||
} | ||
|
||
NXPoint UIPanGestureRecognizer::velocityIn(std::shared_ptr<UIView> view, float timeDiffSeconds) { | ||
if (!trackingTouch || timeDiffSeconds == 0) return NXPoint(); | ||
|
||
NXPoint curPos = trackingTouch->locationIn(view); | ||
NXPoint prevPos = trackingTouch->previousLocationIn(view); | ||
|
||
return (curPos - prevPos) / (float)timeDiffSeconds; | ||
} | ||
|
||
NXPoint UIPanGestureRecognizer::velocityIn(std::shared_ptr<UIView> view) { | ||
float timeDiffSeconds = touchesMovedTimestamp - previousTouchesMovedTimestamp; | ||
timeDiffSeconds /= 100000.0f; | ||
return velocityIn(view, timeDiffSeconds); | ||
} | ||
|
||
void UIPanGestureRecognizer::touchesBegan(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) { | ||
UIGestureRecognizer::touchesBegan(touches, event); | ||
if (!trackingTouch) { | ||
trackingTouch = touches[0]; | ||
initialTouchPoint = trackingTouch->locationIn(nullptr); | ||
touchesMovedTimestamp = trackingTouch->timestamp(); | ||
previousTouchesMovedTimestamp = trackingTouch->timestamp(); | ||
onStateChanged(UIGestureRecognizerState::possible); | ||
} | ||
} | ||
|
||
void UIPanGestureRecognizer::touchesMoved(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) { | ||
UIGestureRecognizer::touchesMoved(touches, event); | ||
if (trackingTouch == touches[0]) { | ||
if (state() == UIGestureRecognizerState::possible) { | ||
NXPoint diff = initialTouchPoint - trackingTouch->locationIn(nullptr); | ||
// printf("x %f, y %f\n", diff.x, diff.y); | ||
if (abs(diff.x) >= THRESHOLD || abs(diff.y) >= THRESHOLD) { | ||
// Reset initial touch point to remove "jiggle" effect after start of recognition | ||
initialTouchPoint = trackingTouch->locationIn(nullptr); | ||
touchesMovedTimestamp = trackingTouch->timestamp(); | ||
previousTouchesMovedTimestamp = trackingTouch->timestamp(); | ||
setState(UIGestureRecognizerState::began); | ||
setState(UIGestureRecognizerState::changed); | ||
} | ||
} else if (state() == UIGestureRecognizerState::changed) { | ||
previousTouchesMovedTimestamp = touchesMovedTimestamp; | ||
touchesMovedTimestamp = trackingTouch->timestamp(); | ||
onStateChanged(UIGestureRecognizerState::changed); | ||
} | ||
} | ||
} | ||
|
||
void UIPanGestureRecognizer::touchesEnded(std::vector<std::shared_ptr<UITouch>> touches, std::shared_ptr<UIEvent> event) { | ||
UIGestureRecognizer::touchesEnded(touches, event); | ||
if (trackingTouch == touches[0]) { | ||
previousTouchesMovedTimestamp = touchesMovedTimestamp; | ||
touchesMovedTimestamp = trackingTouch->timestamp(); | ||
setState(UIGestureRecognizerState::ended); | ||
trackingTouch = nullptr; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters