Gesture: taps, long presses, drags and flings from pointer phases
What a finger or a mouse did, decided from raw Down/Move/Up/Cancel phases over the app's own point type: a short press within the slop is a tap at the press position, a press held past the long-press time (the host's timer) a long press, movement past the slop a drag with its deltas, a fast release a fling with a velocity (the last 100 ms of movement before it: a finger held still before letting go flings nothing). With it the scroll physics of a list (clamped offset, a decaying fling that stops at the edges) and the topmost hit region under a point. Tested: tests/Gesture.
This commit is contained in:
parent
985519b970
commit
f360a746c2
5 changed files with 348 additions and 9 deletions
233
interfaces/ImsdMessages-Gesture.cppm
Normal file
233
interfaces/ImsdMessages-Gesture.cppm
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
/*
|
||||
ImsdMessages:Gesture — what a finger or a mouse did, decided from raw
|
||||
pointer phases, and the scroll physics of a list. Pure arithmetic over the
|
||||
app's own point and box types, so the tests feed it by hand.
|
||||
*/
|
||||
export module ImsdMessages:Gesture;
|
||||
import std;
|
||||
|
||||
export namespace ImsdMessages {
|
||||
struct Point {
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
friend bool operator==(const Point&, const Point&) = default;
|
||||
};
|
||||
|
||||
struct Box {
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
float w = 0;
|
||||
float h = 0;
|
||||
bool Contains(Point p) const noexcept { return p.x >= x && p.x < x + w && p.y >= y && p.y < y + h; }
|
||||
float Right() const noexcept { return x + w; }
|
||||
float Bottom() const noexcept { return y + h; }
|
||||
friend bool operator==(const Box&, const Box&) = default;
|
||||
};
|
||||
|
||||
enum class PointerPhase : std::uint8_t { Down, Move, Up, Cancel };
|
||||
|
||||
struct GestureEvent {
|
||||
enum class Kind : std::uint8_t { None, Tap, LongPress, DragStart, Drag, Fling, Release };
|
||||
Kind kind = Kind::None;
|
||||
Point at; // the press for Tap, LongPress and DragStart; the current position for Drag
|
||||
Point delta; // Drag: the movement since the previous event
|
||||
Point velocity; // Fling: pixels per second
|
||||
};
|
||||
|
||||
// One pointer at a time: a press that ends within the slop and before
|
||||
// the long-press time is a tap; a press held past that time without
|
||||
// moving is a long press (the host runs the timer and calls Timeout);
|
||||
// movement past the slop starts a drag whose release is a fling when it
|
||||
// was fast enough.
|
||||
class GestureDetector {
|
||||
public:
|
||||
float slopPx = 8;
|
||||
std::int64_t longPressMs = 500;
|
||||
float flingMinPxPerS = 60;
|
||||
|
||||
GestureEvent Feed(PointerPhase phase, Point p, std::int64_t tMs) {
|
||||
GestureEvent e;
|
||||
switch (phase) {
|
||||
case PointerPhase::Down:
|
||||
pressed = true;
|
||||
dragging = false;
|
||||
longPressed = false;
|
||||
down = p;
|
||||
downAt = tMs;
|
||||
last = p;
|
||||
samples.clear();
|
||||
samples.push_back({ p, tMs });
|
||||
break;
|
||||
case PointerPhase::Move: {
|
||||
if (!pressed) break;
|
||||
if (!dragging && !longPressed && (std::abs(p.x - down.x) > slopPx || std::abs(p.y - down.y) > slopPx)) {
|
||||
dragging = true;
|
||||
e.kind = GestureEvent::Kind::DragStart;
|
||||
e.at = down;
|
||||
last = down;
|
||||
}
|
||||
if (dragging) {
|
||||
if (e.kind == GestureEvent::Kind::DragStart) {
|
||||
e.delta = { p.x - down.x, p.y - down.y };
|
||||
} else {
|
||||
e.kind = GestureEvent::Kind::Drag;
|
||||
e.delta = { p.x - last.x, p.y - last.y };
|
||||
e.at = p;
|
||||
}
|
||||
}
|
||||
last = p;
|
||||
samples.push_back({ p, tMs });
|
||||
while (samples.size() > 1 && tMs - samples.front().tMs > 100) samples.erase(samples.begin());
|
||||
break;
|
||||
}
|
||||
case PointerPhase::Up: {
|
||||
if (!pressed) break;
|
||||
pressed = false;
|
||||
if (dragging) {
|
||||
// The velocity is the last 100 ms of movement before
|
||||
// the release: a finger held still before letting go
|
||||
// has none, whatever it did before (no event comes
|
||||
// while it is still).
|
||||
while (samples.size() > 1 && tMs - samples.front().tMs > 100) samples.erase(samples.begin());
|
||||
const Point v = Velocity(p, tMs);
|
||||
if (std::hypot(v.x, v.y) >= flingMinPxPerS) {
|
||||
e.kind = GestureEvent::Kind::Fling;
|
||||
e.velocity = v;
|
||||
} else {
|
||||
e.kind = GestureEvent::Kind::Release;
|
||||
}
|
||||
e.at = p;
|
||||
} else if (!longPressed && tMs - downAt < longPressMs) {
|
||||
e.kind = GestureEvent::Kind::Tap;
|
||||
e.at = down;
|
||||
} else {
|
||||
e.kind = GestureEvent::Kind::Release;
|
||||
e.at = p;
|
||||
}
|
||||
dragging = false;
|
||||
break;
|
||||
}
|
||||
case PointerPhase::Cancel:
|
||||
pressed = false;
|
||||
dragging = false;
|
||||
longPressed = false;
|
||||
break;
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
// The host's timer at Down + longPressMs.
|
||||
GestureEvent Timeout(std::int64_t tMs) {
|
||||
GestureEvent e;
|
||||
if (!pressed || dragging || longPressed || tMs - downAt < longPressMs) return e;
|
||||
longPressed = true;
|
||||
e.kind = GestureEvent::Kind::LongPress;
|
||||
e.at = down;
|
||||
return e;
|
||||
}
|
||||
|
||||
bool Pressed() const noexcept { return pressed; }
|
||||
bool Dragging() const noexcept { return dragging; }
|
||||
|
||||
private:
|
||||
struct Sample {
|
||||
Point p;
|
||||
std::int64_t tMs;
|
||||
};
|
||||
|
||||
Point Velocity(Point p, std::int64_t tMs) const {
|
||||
if (samples.empty()) return {};
|
||||
const Sample& oldest = samples.front();
|
||||
const std::int64_t dt = tMs - oldest.tMs;
|
||||
if (dt <= 0) return {};
|
||||
return { (p.x - oldest.p.x) * 1000.0f / static_cast<float>(dt), (p.y - oldest.p.y) * 1000.0f / static_cast<float>(dt) };
|
||||
}
|
||||
|
||||
bool pressed = false;
|
||||
bool dragging = false;
|
||||
bool longPressed = false;
|
||||
Point down;
|
||||
Point last;
|
||||
std::int64_t downAt = 0;
|
||||
std::vector<Sample> samples;
|
||||
};
|
||||
|
||||
// A scrollable extent: the offset is how far the content moved up.
|
||||
struct ScrollState {
|
||||
float offset = 0;
|
||||
float velocity = 0; // px/s, positive scrolls further down
|
||||
float content = 0;
|
||||
float viewport = 0;
|
||||
|
||||
float Max() const noexcept { return std::max(0.0f, content - viewport); }
|
||||
|
||||
void Clamp() noexcept { offset = std::clamp(offset, 0.0f, Max()); }
|
||||
|
||||
void ScrollBy(float dy) noexcept {
|
||||
offset += dy;
|
||||
velocity = 0;
|
||||
Clamp();
|
||||
}
|
||||
|
||||
void Fling(float pxPerS) noexcept { velocity = pxPerS; }
|
||||
|
||||
// Advances the fling by dt seconds; true while still moving.
|
||||
bool Tick(float dt) noexcept {
|
||||
if (std::abs(velocity) < 20.0f) {
|
||||
velocity = 0;
|
||||
return false;
|
||||
}
|
||||
const float before = offset;
|
||||
offset += velocity * dt;
|
||||
Clamp();
|
||||
if (offset == before || offset == 0.0f || offset == Max()) {
|
||||
velocity = 0;
|
||||
return false;
|
||||
}
|
||||
velocity *= std::exp(-4.0f * dt);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
struct HitRegion {
|
||||
Box box;
|
||||
std::uint32_t id = 0;
|
||||
};
|
||||
|
||||
// The topmost region (the last drawn) under a point.
|
||||
std::optional<std::uint32_t> HitTest(std::span<const HitRegion> regions, Point p) {
|
||||
for (auto it = regions.rbegin(); it != regions.rend(); ++it) {
|
||||
if (it->box.Contains(p)) return it->id;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// What a point names on a drawn frame, by what the region stood for
|
||||
// rather than its index: a control's id, or — for the regions of a
|
||||
// list (`rowBase` + the row's index into `keys`) and of a conversation
|
||||
// (`messageBase` + the bubble's index into `messages`) — the
|
||||
// conversation or the message. A press is bound to this: a frame
|
||||
// drawn while the finger is down (a new text snaps the conversation,
|
||||
// the list re-sorts) that moves another row or bubble under it names
|
||||
// something else at the release, which then acts on nothing.
|
||||
struct HitTarget {
|
||||
std::uint32_t id = 0;
|
||||
std::string key;
|
||||
std::uint64_t message = 0;
|
||||
friend bool operator==(const HitTarget&, const HitTarget&) = default;
|
||||
};
|
||||
|
||||
std::optional<HitTarget> TargetAt(std::span<const HitRegion> regions, Point p, std::uint32_t rowBase, std::span<const std::string> keys, std::uint32_t messageBase, std::span<const std::uint64_t> messages) {
|
||||
const std::optional<std::uint32_t> id = HitTest(regions, p);
|
||||
if (!id) return std::nullopt;
|
||||
if (*id >= messageBase) {
|
||||
if (const std::size_t i = *id - messageBase; i < messages.size()) return HitTarget{ messageBase, "", messages[i] };
|
||||
} else if (*id >= rowBase) {
|
||||
if (const std::size_t i = *id - rowBase; i < keys.size()) return HitTarget{ rowBase, keys[i] };
|
||||
}
|
||||
return HitTarget{ *id };
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue