Changed (and simplified) some internal logic of the touch class

This commit is contained in:
Mikael Pettersson
2020-03-10 09:52:30 +01:00
parent 4e9aae3f78
commit b9210a65fd
2 changed files with 23 additions and 10 deletions

View File

@@ -46,7 +46,7 @@ struct TouchInput {
float y;
float dx = 0.f; // movement in x direction since last touch input
float dy = 0.f; // movement in y direction since last touch input
double timestamp; // timestamp in seconds from global touch initialization
double timestamp; // timestamp in seconds from global touch initialization
};
class TouchInputHolder {
@@ -56,6 +56,7 @@ public:
// tryAddInput:
// Succeeds upon a different input than last.
// Fails upon a too similar input as last.
// Updates time for the last input if same position.
bool tryAddInput(TouchInput input);
void clearInputs();
@@ -72,6 +73,7 @@ public:
double gestureTime() const;
size_t numInputs() const;
const TouchInput& firstInput() const;
const TouchInput& latestInput() const;
const std::deque<TouchInput>& peekInputs() const;

View File

@@ -78,27 +78,35 @@ TouchInputHolder::TouchInputHolder(TouchInput input)
{}
bool TouchInputHolder::tryAddInput(TouchInput input) {
if(_inputs.empty()) {
_inputs.emplace_front(input);
return true;
}
constexpr const double ONE_MS = 0.001;
const TouchInput& lastInput = latestInput();
input.dx = input.x - lastInput.x;
input.dy = input.y - lastInput.y;
const bool sameTimeAsLastInput = (input.timestamp - lastInput.timestamp) > ONE_MS;
bool wasInserted = false;
if (isMoving()) {
const bool sameTimeAsLastInput = (input.timestamp - lastInput.timestamp) < ONE_MS;
bool successful = false;
if (!sameTimeAsLastInput && isMoving()) {
_inputs.emplace_front(input);
wasInserted = true;
successful = true;
}
else if (sameTimeAsLastInput && input.isMoving()) {
else if (!sameTimeAsLastInput && input.isMoving()) {
_inputs.emplace_front(input);
wasInserted = true;
successful = true;
}
else if (!sameTimeAsLastInput){
_inputs.front().timestamp = input.timestamp;
successful = true;
}
constexpr const int MaxInputs = 128;
if (_inputs.size() > MaxInputs) {
_inputs.pop_back();
}
return wasInserted;
return successful;
}
void TouchInputHolder::clearInputs() {
@@ -142,8 +150,7 @@ bool TouchInputHolder::isMoving() const {
if (_inputs.size() <= 1) {
return false;
}
const TouchInput& currentInput = _inputs[0];
return currentInput.dx != 0.f || currentInput.dy != 0.f;
return latestInput().isMoving();
}
float TouchInputHolder::gestureDistance() const {
@@ -174,6 +181,10 @@ size_t TouchInputHolder::numInputs() const {
return _inputs.size();
}
const TouchInput& TouchInputHolder::firstInput() const {
return _inputs.back();
}
const TouchInput& TouchInputHolder::latestInput() const {
return _inputs.front();
}