yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
touch_input.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GUI_CORE_TOUCH_INPUT_H
2#define YAZE_APP_GUI_CORE_TOUCH_INPUT_H
3
4#include <array>
5#include <functional>
6#include <memory>
7
8#include "imgui/imgui.h"
9
10namespace yaze {
11namespace gui {
12
16enum class TouchGesture {
17 kNone, // No gesture active
18 kTap, // Single finger tap (click)
19 kDoubleTap, // Double tap (context menu or zoom to fit)
20 kLongPress, // Long press (500ms+) for context menu
21 kPan, // Two-finger pan/scroll
22 kPinchZoom, // Two-finger pinch to zoom
23 kRotate // Two-finger rotation (optional)
24};
25
29enum class TouchPhase {
30 kBegan, // Gesture started
31 kChanged, // Gesture in progress with updated values
32 kEnded, // Gesture completed successfully
33 kCancelled // Gesture was cancelled
34};
35
39struct TouchPoint {
40 int id = -1; // Unique identifier for this touch
41 ImVec2 position = ImVec2(0, 0); // Current position
42 ImVec2 start_position = ImVec2(0, 0); // Position when touch started
43 ImVec2 previous_position = ImVec2(0, 0); // Previous frame position
44 float pressure = 1.0f; // Touch pressure (0.0 - 1.0)
45 double timestamp = 0.0; // Time when touch started
46 bool active = false; // Whether this touch is currently active
47};
48
55
56 // Position data
57 ImVec2 position = ImVec2(0, 0); // Center position of gesture
58 ImVec2 start_position = ImVec2(0, 0); // Where gesture started
59 ImVec2 translation = ImVec2(0, 0); // Pan/drag offset
60 ImVec2 velocity = ImVec2(0, 0); // Movement velocity for inertia
61
62 // Scale/rotation data
63 float scale = 1.0f; // Pinch zoom scale factor
64 float scale_delta = 0.0f; // Change in scale this frame
65 float rotation = 0.0f; // Rotation angle in radians
66 float rotation_delta = 0.0f; // Change in rotation this frame
67
68 // Touch count
69 int touch_count = 0;
70
71 // Timing
72 double duration = 0.0; // How long the gesture has been active
73};
74
79 // Timing thresholds (in seconds)
80 float tap_max_duration = 0.3f; // Max duration for a tap
81 float double_tap_max_delay = 0.3f; // Max delay between taps for double-tap
82 float long_press_duration = 0.5f; // Duration to trigger long press
83
84 // Distance thresholds (in pixels)
85 float tap_max_movement = 10.0f; // Max movement for a tap
86 float pan_threshold = 10.0f; // Min movement to start pan
87 float pinch_threshold = 5.0f; // Min scale change to start pinch
88 float rotation_threshold = 0.1f; // Min rotation to start rotate (radians)
89
90 // Feature toggles
91 bool enable_pan_zoom = true; // Enable two-finger pan/zoom
92 bool enable_rotation = false; // Enable two-finger rotation
93 bool enable_inertia = true; // Enable inertia scrolling after swipe
94
95 // Inertia settings
96 float inertia_deceleration = 0.95f; // Velocity multiplier per frame
97 float inertia_min_velocity = 0.5f; // Minimum velocity to continue inertia
98
99 // Scale limits
100 float min_zoom = 0.25f;
101 float max_zoom = 4.0f;
102};
103
138 public:
142 static constexpr int kMaxTouchPoints = 10;
143
151 static void Initialize();
152
156 static void Shutdown();
157
164 static void Update();
165
170 static bool IsTouchActive();
171
179 static bool IsTouchMode();
180
186
192 static TouchPoint GetTouchPoint(int index);
193
198 static int GetActiveTouchCount();
199
200 // === Canvas Integration ===
201
206 static void SetPanZoomEnabled(bool enabled);
207
212 static bool IsPanZoomEnabled();
213
220 static ImVec2 GetPanOffset();
221
228 static float GetZoomLevel();
229
234 static float GetRotation();
235
242 static ImVec2 GetZoomCenter();
243
248 static void ApplyPanOffset(ImVec2 offset);
249
254 static void SetZoomLevel(float zoom);
255
260 static void SetPanOffset(ImVec2 offset);
261
267 static void ResetCanvasState();
268
269 // === Configuration ===
270
275 static TouchConfig& GetConfig();
276
283 static void SetGestureCallback(std::function<void(const GestureState&)> callback);
284
285 // === Platform-Specific Hooks ===
286
287#ifdef __EMSCRIPTEN__
299 static void OnTouchEvent(int type, int id, float x, float y,
300 float pressure, double timestamp);
301
313 static void OnGestureEvent(int type, int phase, float x, float y,
314 float scale, float rotation);
315#endif
316
317 private:
318 TouchInput() = delete; // Static-only class
319
320 // Internal state management
321 static void UpdateGestureRecognition();
322 static void ProcessInertia();
323 static void TranslateToImGuiEvents();
324 static double GetCurrentTime();
325
326 // Platform-specific initialization
327 static void InitializePlatform();
328 static void ShutdownPlatform();
329};
330
331} // namespace gui
332} // namespace yaze
333
334#endif // YAZE_APP_GUI_CORE_TOUCH_INPUT_H
Touch input handling system for iPad and tablet browsers.
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 ApplyPanOffset(ImVec2 offset)
Apply pan offset to the current value.
static void Initialize()
Initialize the touch input system.
static TouchConfig & GetConfig()
Get the current touch configuration.
static int GetActiveTouchCount()
Get number of active touch points.
static void Shutdown()
Shutdown and cleanup touch input system.
static ImVec2 GetZoomCenter()
Get the zoom center point in screen coordinates.
static void ProcessInertia()
static TouchPoint GetTouchPoint(int index)
Get raw touch point data.
static void UpdateGestureRecognition()
static void SetZoomLevel(float zoom)
Set the zoom level directly.
static void InitializePlatform()
static void SetGestureCallback(std::function< void(const GestureState &)> callback)
Set gesture callback.
static bool IsTouchActive()
Check if touch input is currently being used.
static double GetCurrentTime()
static void SetPanOffset(ImVec2 offset)
Set the pan offset directly.
static float GetZoomLevel()
Get cumulative zoom level from pinch gestures.
static GestureState GetCurrentGesture()
Get the current gesture state.
static constexpr int kMaxTouchPoints
Maximum number of simultaneous touch points supported.
static void TranslateToImGuiEvents()
static float GetRotation()
Get rotation angle from two-finger rotation.
static void ShutdownPlatform()
static bool IsPanZoomEnabled()
Check if pan/zoom is enabled.
static void Update()
Process touch events for the current frame.
static void SetPanZoomEnabled(bool enabled)
Enable or disable pan/zoom gestures for canvas.
static void ResetCanvasState()
Reset canvas transform state.
TouchGesture
Gesture types recognized by the touch input system.
Definition touch_input.h:16
TouchPhase
Phase of a touch gesture.
Definition touch_input.h:29
Gesture recognition result.
Definition touch_input.h:52
Touch input configuration.
Definition touch_input.h:78
Individual touch point data.
Definition touch_input.h:39