Toast Engine
Loading...
Searching...
No Matches
trackpad_math.hpp
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <cmath>
11#include <cstdint>
12
13namespace toast::input {
14
16 float scale;
17 float x;
18 float y;
19};
20
22 float dx;
23 float dy;
24 float zoom;
25};
26
28public:
29 static constexpr float pinch_scale = 125.0f;
30 static constexpr float epsilon = 3e-5f;
31
32 void reset() noexcept {
33 m_kind = Kind::none;
34 m_last = {pinch_scale, 0.0f, 0.0f};
35 }
36
37 [[nodiscard]]
38 auto started() const noexcept -> bool {
39 return m_kind != Kind::none;
40 }
41
42 auto accept(TrackpadTransform current) noexcept -> TrackpadDelta {
43 if (current.scale == 0.0f || (std::abs(current.scale - m_last.scale) <= epsilon &&
44 std::abs(current.x - m_last.x) <= epsilon && std::abs(current.y - m_last.y) <= epsilon)) {
45 return {0.0f, 0.0f, 0.0f};
46 }
47
48 if (m_kind == Kind::none) {
49 m_kind = Kind::pan;
50 }
51 if (m_kind == Kind::pan && std::abs(current.scale - pinch_scale) > epsilon) {
52 m_kind = Kind::pinch;
53 }
54
55 if (m_kind == Kind::pinch) {
56 const float zoom = current.scale - m_last.scale;
57 m_last.scale = current.scale;
58 return {0.0f, 0.0f, zoom};
59 }
60
61 const TrackpadDelta delta {current.x - m_last.x, current.y - m_last.y, 0.0f};
62 m_last.x = current.x;
63 m_last.y = current.y;
64 return delta;
65 }
66
67private:
68 enum class Kind : uint8_t {
69 none,
70 pan,
71 pinch
72 };
73
74 Kind m_kind = Kind::none;
75 TrackpadTransform m_last {pinch_scale, 0.0f, 0.0f};
76};
77
78}
Definition trackpad_math.hpp:27
Definition trackpad_math.hpp:21
Definition trackpad_math.hpp:15