yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
canvas_touch_handler.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cmath>
5
6namespace yaze {
7namespace gui {
8
9void CanvasTouchHandler::Initialize(const std::string& canvas_id) {
10 canvas_id_ = canvas_id;
11 Reset();
12
13 // Initialize the global touch input system if not already done
15}
16
18 // Clear per-frame gesture flags
19 was_tapped_ = false;
20 was_double_tapped_ = false;
21 was_long_pressed_ = false;
22
23 // Store previous state for delta calculations
26
27 // Get current gesture state from TouchInput
28 auto gesture = TouchInput::GetCurrentGesture();
29
30 // Process the gesture
31 if (gesture.gesture != TouchGesture::kNone && canvas_hovered_) {
32 ProcessGesture(gesture);
33 }
34
35 // Process inertia for smooth scrolling
38 }
39
40 // Smooth zoom animation
41 if (config_.enable_smooth_zoom && std::abs(target_scale_ - current_scale_) > 0.001f) {
43 } else {
45 }
46
47 // Clamp pan to bounds if configured
48 if (config_.max_pan_x != 0.0f || config_.max_pan_y != 0.0f) {
50 }
51}
52
56
60
62 scroll_offset_ = offset;
63 prev_pan_offset_ = offset;
64}
65
71
73 scroll_offset_.x += delta.x;
74 scroll_offset_.y += delta.y;
75}
76
77void CanvasTouchHandler::ApplyZoomDelta(float delta, ImVec2 center) {
78 float new_scale = target_scale_ * (1.0f + delta);
79 new_scale = std::clamp(new_scale, config_.min_scale, config_.max_scale);
80
81 // Zoom towards center point
82 if (std::abs(new_scale - target_scale_) > 0.001f) {
83 float scale_ratio = new_scale / target_scale_;
84
85 // Adjust pan to zoom towards the center point
86 scroll_offset_.x = center.x - (center.x - scroll_offset_.x) * scale_ratio;
87 scroll_offset_.y = center.y - (center.y - scroll_offset_.y) * scale_ratio;
88
89 target_scale_ = new_scale;
90 zoom_center_ = center;
91 }
92}
93
95 scroll_offset_ = ImVec2(0, 0);
96 current_scale_ = 1.0f;
97 target_scale_ = 1.0f;
98 zoom_center_ = ImVec2(0, 0);
99 inertia_velocity_ = ImVec2(0, 0);
100 inertia_active_ = false;
101 prev_pan_offset_ = ImVec2(0, 0);
102 prev_scale_ = 1.0f;
103
104 // Also reset the global touch state
106}
107
108void CanvasTouchHandler::ProcessForCanvas(ImVec2 canvas_p0, ImVec2 canvas_sz,
109 bool is_hovered) {
110 canvas_p0_ = canvas_p0;
111 canvas_sz_ = canvas_sz;
112 canvas_hovered_ = is_hovered;
113}
114
116 gesture_position_ = gesture.position;
117
118 switch (gesture.gesture) {
120 if (gesture.phase == TouchPhase::kEnded) {
121 was_tapped_ = true;
122 }
123 break;
124
126 if (gesture.phase == TouchPhase::kEnded) {
127 was_double_tapped_ = true;
128
129 // Optional: Zoom to fit on double-tap
130 // This could be made configurable
131 }
132 break;
133
135 if (gesture.phase == TouchPhase::kBegan) {
136 was_long_pressed_ = true;
137 }
138 break;
139
141 if (config_.enable_pan) {
142 // Get pan offset from TouchInput
143 ImVec2 global_pan = TouchInput::GetPanOffset();
144
145 // Calculate delta since last frame
146 ImVec2 pan_delta = ImVec2(global_pan.x - prev_pan_offset_.x,
147 global_pan.y - prev_pan_offset_.y);
148
149 // Apply to local scroll offset
150 scroll_offset_.x += pan_delta.x;
151 scroll_offset_.y += pan_delta.y;
152
153 // Track velocity for inertia
154 if (gesture.phase == TouchPhase::kChanged) {
155 inertia_velocity_ = gesture.velocity;
156 }
157
158 // Start inertia when pan ends
160 float velocity_mag = std::sqrt(
163 if (velocity_mag > config_.inertia_min_velocity) {
164 inertia_active_ = true;
165 }
166 }
167 }
168 break;
169
171 if (config_.enable_zoom) {
172 // Get zoom level from TouchInput
173 float global_zoom = TouchInput::GetZoomLevel();
175
176 // Calculate zoom delta
177 float zoom_delta = global_zoom - prev_scale_;
178
179 if (std::abs(zoom_delta) > 0.001f) {
180 // Apply zoom with pivot at gesture center
181 float new_scale = current_scale_ + zoom_delta;
182 new_scale = std::clamp(new_scale, config_.min_scale, config_.max_scale);
183
184 // Calculate offset adjustment to zoom towards the pinch center
185 // Convert gesture center from screen to canvas space
186 ImVec2 center_in_canvas = ImVec2(
189
190 float scale_ratio = new_scale / current_scale_;
191
192 // Adjust scroll to keep the pinch center stationary
194 center_in_canvas.x * scale_ratio;
196 center_in_canvas.y * scale_ratio;
197
198 target_scale_ = new_scale;
199 current_scale_ = new_scale; // Immediate update during pinch
200 }
201 }
202 break;
203
205 // Rotation is optional and handled separately by canvas if needed
206 break;
207
209 default:
210 break;
211 }
212
213 // Update previous values
216}
217
219 float velocity_mag = std::sqrt(
222
223 if (velocity_mag < config_.inertia_min_velocity) {
224 inertia_active_ = false;
225 inertia_velocity_ = ImVec2(0, 0);
226 return;
227 }
228
229 // Apply velocity to scroll
232
233 // Decay velocity
236}
237
239 // Apply pan limits if configured
240 if (config_.min_pan_x != 0.0f || config_.max_pan_x != 0.0f) {
241 scroll_offset_.x = std::clamp(scroll_offset_.x,
243 }
244
245 if (config_.min_pan_y != 0.0f || config_.max_pan_y != 0.0f) {
246 scroll_offset_.y = std::clamp(scroll_offset_.y,
248 }
249}
250
251} // namespace gui
252} // namespace yaze
void SetScrollOffset(ImVec2 offset)
Set the scroll offset (e.g., from Canvas state)
void SetScale(float scale)
Set the zoom scale.
bool IsTouchMode() const
Check if we're in touch mode (vs mouse)
void ApplyScrollDelta(ImVec2 delta)
Apply a scroll delta (add to current offset)
void ProcessGesture(const GestureState &gesture)
void ApplyZoomDelta(float delta, ImVec2 center)
Apply a zoom delta around a center point.
void Update()
Update touch state each frame.
void Initialize(const std::string &canvas_id="")
Initialize the touch handler.
void Reset()
Reset to default state (no pan, 1.0 scale)
bool IsTouchActive() const
Check if touch input is currently active.
void ProcessForCanvas(ImVec2 canvas_p0, ImVec2 canvas_sz, bool is_hovered)
Process touch gestures for the current canvas bounds.
static ImVec2 GetPanOffset()
Get cumulative pan offset from touch gestures.
static bool IsTouchMode()
Check if we're in touch mode (vs mouse mode)
static void Initialize()
Initialize the touch input system.
static ImVec2 GetZoomCenter()
Get the zoom center point in screen coordinates.
static bool IsTouchActive()
Check if touch input is currently being used.
static float GetZoomLevel()
Get cumulative zoom level from pinch gestures.
static GestureState GetCurrentGesture()
Get the current gesture state.
static void ResetCanvasState()
Reset canvas transform state.
Gesture recognition result.
Definition touch_input.h:52