@geastack/engine 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +29 -0
  3. package/bitmap_font.cpp +165 -0
  4. package/canvas.cpp +4608 -0
  5. package/components/Button.d.ts +4 -0
  6. package/components/Button.tsx +32 -0
  7. package/components/Image.d.ts +3 -0
  8. package/components/Image.tsx +17 -0
  9. package/components/Text.d.ts +3 -0
  10. package/components/Text.tsx +9 -0
  11. package/components/View.d.ts +3 -0
  12. package/components/View.tsx +24 -0
  13. package/components/index.d.ts +8 -0
  14. package/components/index.ts +4 -0
  15. package/css/animation.h +252 -0
  16. package/css/declarative.h +131 -0
  17. package/css/easing.h +160 -0
  18. package/css/engine.h +223 -0
  19. package/css/interpolate.h +38 -0
  20. package/image_store.cpp +807 -0
  21. package/package.json +62 -0
  22. package/rasterized_font.cpp +570 -0
  23. package/touch_runtime.cpp +513 -0
  24. package/ui/absolute_leaf_refresh.cpp +279 -0
  25. package/ui/absolute_leaf_refresh.h +12 -0
  26. package/ui/canvas_element.cpp +2314 -0
  27. package/ui/canvas_element.h +353 -0
  28. package/ui/canvas_store.cpp +188 -0
  29. package/ui/canvas_store.h +59 -0
  30. package/ui/core.cpp +135 -0
  31. package/ui/css_atom.cpp +155 -0
  32. package/ui/css_atom.h +21 -0
  33. package/ui/dirty_regions.cpp +66 -0
  34. package/ui/dirty_regions.h +34 -0
  35. package/ui/display_invalidation.cpp +101 -0
  36. package/ui/display_invalidation.h +15 -0
  37. package/ui/document.cpp +356 -0
  38. package/ui/document.h +101 -0
  39. package/ui/image.h +16 -0
  40. package/ui/image_element.cpp +13 -0
  41. package/ui/image_node.cpp +165 -0
  42. package/ui/input.cpp +936 -0
  43. package/ui/input_render.cpp +213 -0
  44. package/ui/internal.h +567 -0
  45. package/ui/layout.cpp +2025 -0
  46. package/ui/layout_snapshot.cpp +82 -0
  47. package/ui/layout_snapshot.h +12 -0
  48. package/ui/mirror.h +25 -0
  49. package/ui/node.cpp +243 -0
  50. package/ui/node.h +154 -0
  51. package/ui/node_lifecycle.cpp +137 -0
  52. package/ui/node_lifecycle.h +13 -0
  53. package/ui/node_model.h +597 -0
  54. package/ui/refresh_perf.h +235 -0
  55. package/ui/refresh_trace.h +68 -0
  56. package/ui/render.cpp +12097 -0
  57. package/ui/root_scroll_refresh.cpp +859 -0
  58. package/ui/root_scroll_refresh.h +30 -0
  59. package/ui/state_init.h +21 -0
  60. package/ui/style.cpp +14994 -0
  61. package/ui/style.h +727 -0
  62. package/ui/style_values.h +31 -0
  63. package/ui/text.cpp +2010 -0
  64. package/ui/text.h +15 -0
  65. package/ui/text_element.cpp +15 -0
  66. package/ui/tree_events.cpp +621 -0
  67. package/ui/tree_events.h +69 -0
  68. package/ui/tree_internal.h +191 -0
  69. package/ui/tree_nodes.cpp +628 -0
  70. package/ui/tree_render.cpp +3138 -0
  71. package/ui/tree_scroll_mirror.cpp +101 -0
  72. package/ui/tree_state.cpp +574 -0
  73. package/ui/tree_state.h +235 -0
  74. package/ui/tree_style.cpp +698 -0
  75. package/ui/ui.h +9 -0
  76. package/ui/view.cpp +2232 -0
  77. package/ui/view.h +14 -0
  78. package/ui/view_element.cpp +13 -0
  79. package/ui/viewport_region.cpp +36 -0
  80. package/ui/viewport_region.h +12 -0
  81. package/ui/virtual_keyboard.cpp +908 -0
  82. package/ui/virtual_keyboard.h +59 -0
  83. package/vendor/AnimatedGIF/AnimatedGIF.c +982 -0
  84. package/vendor/AnimatedGIF/AnimatedGIF.h +216 -0
  85. package/vendor/stb/stb_image.h +7988 -0
  86. package/vendor/stb/stb_truetype.h +5079 -0
@@ -0,0 +1,513 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "app.h"
3
+ #include "ui/state_init.h"
4
+ #include "events.h"
5
+ #include "event.h"
6
+ #include "services/frame_scheduler.h"
7
+ #include "host/display_orientation.h"
8
+ #include "touch.h"
9
+ #include "ui/tree_internal.h"
10
+ #include "ui/virtual_keyboard.h"
11
+
12
+ #include <cstring>
13
+
14
+ // Weak default for the optional multi-finger observer. Single-touch controllers
15
+ // (every target except the GT911 ones) don't override it, so registering a
16
+ // pointer observer is a harmless no-op there and they keep using the single
17
+ // Observer. Controllers that support simultaneous touches provide a strong
18
+ // override (see the P4-7 touch.cpp).
19
+ __attribute__((weak)) void gea::platform::touch::Touchscreen::setPointerObserver(
20
+ gea::platform::touch::Touchscreen::PointerObserver) {}
21
+ __attribute__((weak)) void gea::platform::touch::Touchscreen::poll(int) {}
22
+
23
+ namespace gea::framework::events {
24
+
25
+ namespace {
26
+
27
+ class TouchEventDispatcher {
28
+ public:
29
+ static TouchEventDispatcher &instance()
30
+ {
31
+ static TouchEventDispatcher dispatcher;
32
+ return dispatcher;
33
+ }
34
+ #if GEA_EMBEDDED_UI_STATE_DYNAMIC_INIT
35
+ // Not inlined: with the constant member initializers folded in, the
36
+ // dispatcher is a .data object (see ui/state_init.h).
37
+ __attribute__((noinline)) TouchEventDispatcher() {}
38
+ #endif
39
+
40
+ void queue(TouchPhase phase, bool touching, int x, int y, int pointerId)
41
+ {
42
+ // The latest-move cache (consumeLatestMove) is the PRIMARY finger's only;
43
+ // secondary fingers (pointerId >= 1) carry their coords in the event.
44
+ if (!dispatchEnabled_ || !gea::framework::services::FrameScheduler::eventQueue()) {
45
+ if (phase == TouchPhase::Move && pointerId == 0) {
46
+ gea::platform::touch::Touchscreen::consumeLatestMove(nullptr, nullptr);
47
+ }
48
+ return;
49
+ }
50
+
51
+ Event event{};
52
+ event.type = EventType::Touch;
53
+ event.touchPhase = phase;
54
+ event.touching = touching;
55
+ event.x = x;
56
+ event.y = y;
57
+ event.pointerId = pointerId;
58
+
59
+ // Move events are deliberately lossy: the controller keeps the freshest
60
+ // coordinate in consumeLatestMove(), so a full queue may drop the queued
61
+ // marker and let the next sample re-arm it. Down and Up are gesture edges;
62
+ // dropping either leaves wasActive_ in the wrong state and makes a later
63
+ // city/button tap appear to select the previous target. Give those edges
64
+ // enough time to enter the queue even while a long render is completing.
65
+ const int waitMs = phase == TouchPhase::Move ? 0 : 250;
66
+ const bool sent = gea::framework::services::FrameScheduler::sendEvent(event, waitMs);
67
+ if (!sent &&
68
+ phase == TouchPhase::Move && pointerId == 0) {
69
+ gea::platform::touch::Touchscreen::consumeLatestMove(nullptr, nullptr);
70
+ }
71
+ }
72
+
73
+ void setDispatchEnabled(bool enabled)
74
+ {
75
+ dispatchEnabled_ = enabled;
76
+ }
77
+
78
+ bool dispatchEnabled() const
79
+ {
80
+ return dispatchEnabled_;
81
+ }
82
+
83
+ void resetGesture()
84
+ {
85
+ wasActive_ = false;
86
+ lastX_ = 0;
87
+ lastY_ = 0;
88
+ startX_ = 0;
89
+ startY_ = 0;
90
+ dragged_ = false;
91
+ activeDefaultPrevented_ = false;
92
+ activeTargetId_ = -1;
93
+ for (Secondary &s : secondary_) s = Secondary{};
94
+ }
95
+
96
+ // Returns true if this event may have changed on-screen state (a real
97
+ // down/move/up that ran handlers), so the runtime can render immediately
98
+ // instead of waiting for the next timer frame. A redundant move (finger held
99
+ // at the same coordinates) returns false so a static hold can't busy-render.
100
+ bool dispatch(const Event &event)
101
+ {
102
+ // Immediate-mode apps (Display.ctx games) mount no UI tree: hitTestNode
103
+ // returns -1 and there is no root to bind document listeners onto, so the
104
+ // tree dispatch below can never reach the app. Deliver pointer events
105
+ // straight to document-level listeners, exactly like the DOM document, so
106
+ // every finger (pointerId 0..N) reaches document.addEventListener().
107
+ if (gea::embedded::ui::Tree::instance().mountedRoot() < 0) {
108
+ return dispatchToDocument(event);
109
+ }
110
+ // Additional simultaneous fingers are routed to independent per-pointer
111
+ // slots so they can hold/tap their own buttons (e.g. move + jump) without
112
+ // disturbing the primary pointer's scroll / focus / keyboard gesture.
113
+ if (event.pointerId >= 1) {
114
+ return dispatchSecondary(event);
115
+ }
116
+
117
+ bool changed = false;
118
+
119
+ int tx = event.x;
120
+ int ty = event.y;
121
+ bool touching = event.touching;
122
+ if (event.touchPhase == TouchPhase::Move) {
123
+ gea::platform::touch::Touchscreen::consumeLatestMove(&tx, &ty);
124
+ TouchRuntime::transformTouchToLogical(&tx, &ty);
125
+ touching = true;
126
+ }
127
+
128
+ if (touching && !wasActive_) {
129
+ changed = true;
130
+ startX_ = tx;
131
+ startY_ = ty;
132
+ dragged_ = false;
133
+ activeDefaultPrevented_ = false;
134
+ gea::embedded::ui::Tree::instance().pointerDown(tx, ty);
135
+ const int targetId = gea::embedded::ui::Tree::instance().hitTestNode(tx, ty);
136
+ if (targetId >= 0) {
137
+ activeTargetId_ = targetId;
138
+ activeDefaultPrevented_ = dispatchPointer(PointerEventType::TouchStart, targetId, tx, ty, true);
139
+ }
140
+ }
141
+
142
+ if (touching && wasActive_ && (tx != lastX_ || ty != lastY_)) {
143
+ changed = true;
144
+ if (!dragged_) {
145
+ int dx = tx - startX_;
146
+ int dy = ty - startY_;
147
+ // 10 px was too tight on a 410-px capacitive panel — finger
148
+ // jitter during a stationary tap routinely reported 8-14 px of
149
+ // drift, tripping the drag flag and suppressing the press
150
+ // (visible as "tap on launcher tile sometimes does nothing").
151
+ // 16 px is roughly Android's ViewConfiguration touch-slop, and
152
+ // only delays intentional scroll initiation by 6 px.
153
+ if (dx > 16 || dx < -16 || dy > 16 || dy < -16) {
154
+ dragged_ = true;
155
+ }
156
+ }
157
+ bool defaultPrevented = activeDefaultPrevented_;
158
+ if (activeTargetId_ >= 0) {
159
+ defaultPrevented = dispatchPointer(PointerEventType::TouchMove, activeTargetId_, tx, ty, true) || defaultPrevented;
160
+ }
161
+ if (!defaultPrevented) gea::embedded::ui::Tree::instance().pointerMove(tx, ty);
162
+ }
163
+
164
+ if (!touching && wasActive_) {
165
+ changed = true;
166
+ // Captured BEFORE any click handler runs: if a handler
167
+ // programmatically focuses an input (element.focus()), the
168
+ // focus-management default below must not clear it.
169
+ const int focusBeforeUp = gea::embedded::ui::Tree::instance().activeInputId();
170
+ gea::embedded::ui::Tree::instance().pointerUp();
171
+ const int focusTarget = activeTargetId_ >= 0 ? inputAncestor(activeTargetId_) : -1;
172
+ const bool targetInsideKeyboard =
173
+ activeTargetId_ >= 0 && gea::embedded::ui::VirtualKeyboard::instance().containsNode(activeTargetId_);
174
+ const bool keyboardDismissTap =
175
+ !dragged_ &&
176
+ !activeDefaultPrevented_ &&
177
+ activeTargetId_ >= 0 &&
178
+ gea::embedded::ui::VirtualKeyboard::instance().active() &&
179
+ !targetInsideKeyboard &&
180
+ focusTarget < 0;
181
+ if (keyboardDismissTap) {
182
+ auto &tree = gea::embedded::ui::Tree::instance();
183
+ tree.setActiveInput(-1);
184
+ gea::embedded::ui::VirtualKeyboard::instance().sync();
185
+ } else if (activeTargetId_ >= 0) {
186
+ dispatchPointer(PointerEventType::TouchEnd, activeTargetId_, lastX_, lastY_, false);
187
+ }
188
+ if (!dragged_ && !activeDefaultPrevented_ && activeTargetId_ >= 0) {
189
+ if (!keyboardDismissTap) dispatchPointer(PointerEventType::Click, activeTargetId_, lastX_, lastY_, false);
190
+ // Focus management — set the active input if the tap
191
+ // landed on one (or the closest input ancestor); clear
192
+ // focus on taps outside any input.
193
+ //
194
+ // Skip the focus update entirely when the tap was on
195
+ // a keyboard key: the keyboard's click handler called
196
+ // preventDefault, AND we don't want the act of typing
197
+ // to immediately close the keyboard. The keyboard's
198
+ // keys carry pressIds in the reserved
199
+ // VirtualKeyboard::keyboardPressIdRange — if the tap
200
+ // went there, the keyboard already routed it and we
201
+ // must leave activeInputId untouched.
202
+ auto &tree = gea::embedded::ui::Tree::instance();
203
+ // Don't run focus-clearing logic when the tap landed
204
+ // anywhere inside the keyboard subtree — hitTestNode
205
+ // often returns a key's child label, not the key
206
+ // itself, so a pressId range check misses those.
207
+ // Walking the ancestor chain is reliable.
208
+ // A click handler that PROGRAMMATICALLY focused an input
209
+ // (element.focus()) wins over the tap-position default — clearing
210
+ // it here would close the keyboard the handler just opened.
211
+ const bool handlerTookFocus = tree.activeInputId() != focusBeforeUp;
212
+ if (!targetInsideKeyboard && !keyboardDismissTap && !handlerTookFocus) {
213
+ tree.setActiveInput(focusTarget);
214
+ gea::embedded::ui::VirtualKeyboard::instance().sync();
215
+ }
216
+ }
217
+ activeTargetId_ = -1;
218
+ activeDefaultPrevented_ = false;
219
+ }
220
+
221
+ wasActive_ = touching;
222
+ if (touching) {
223
+ lastX_ = tx;
224
+ lastY_ = ty;
225
+ }
226
+ if (changed) lastActivityMs_ = gea::framework::services::FrameScheduler::nowMs();
227
+ return changed;
228
+ }
229
+
230
+ // Finger down now AND a down/move/up fired within the last windowMs. Used by
231
+ // the frame loop to render a drag back-to-back; a static hold posts no moves,
232
+ // so this lapses to false after the window and rendering quiesces.
233
+ bool activeWithin(int nowMs, int windowMs) const
234
+ {
235
+ if (!wasActive_) return false;
236
+ const int dt = nowMs - lastActivityMs_;
237
+ return dt >= 0 && dt <= windowMs;
238
+ }
239
+
240
+ private:
241
+ // Lightweight per-pointer handler for non-primary fingers. Independently
242
+ // hit-tests and fires touch*/click on its own target, but does NOT touch
243
+ // scroll / focus / keyboard / the hardware Touchscreen state — those stay
244
+ // owned by the primary pointer (pointerId 0).
245
+ bool dispatchSecondary(const Event &event)
246
+ {
247
+ const int slot = event.pointerId - 1;
248
+ if (slot < 0 || slot >= kMaxSecondaryPointers) return false;
249
+ Secondary &s = secondary_[slot];
250
+ const int tx = event.x;
251
+ const int ty = event.y;
252
+ const bool touching = event.touching || event.touchPhase == TouchPhase::Move;
253
+ // Web convention: pointerId 1 is the primary pointer, so extra fingers
254
+ // are numbered from 2 upward.
255
+ const int webPointerId = event.pointerId + 1;
256
+
257
+ if (touching && !s.active) {
258
+ s.active = true;
259
+ s.startX = tx;
260
+ s.startY = ty;
261
+ s.lastX = tx;
262
+ s.lastY = ty;
263
+ s.dragged = false;
264
+ s.defaultPrevented = false;
265
+ s.targetId = gea::embedded::ui::Tree::instance().hitTestNode(tx, ty);
266
+ if (s.targetId >= 0) {
267
+ s.defaultPrevented = dispatchPointer(PointerEventType::TouchStart, s.targetId, tx, ty, true, webPointerId, false);
268
+ }
269
+ return true;
270
+ }
271
+
272
+ if (touching && s.active) {
273
+ const bool moved = tx != s.lastX || ty != s.lastY;
274
+ if (moved) {
275
+ if (!s.dragged) {
276
+ const int dx = tx - s.startX;
277
+ const int dy = ty - s.startY;
278
+ if (dx > 16 || dx < -16 || dy > 16 || dy < -16) s.dragged = true;
279
+ }
280
+ if (s.targetId >= 0) {
281
+ dispatchPointer(PointerEventType::TouchMove, s.targetId, tx, ty, true, webPointerId, false);
282
+ }
283
+ s.lastX = tx;
284
+ s.lastY = ty;
285
+ }
286
+ return moved;
287
+ }
288
+
289
+ if (!touching && s.active) {
290
+ if (s.targetId >= 0) {
291
+ dispatchPointer(PointerEventType::TouchEnd, s.targetId, s.lastX, s.lastY, false, webPointerId, false);
292
+ if (!s.dragged && !s.defaultPrevented) {
293
+ dispatchPointer(PointerEventType::Click, s.targetId, s.lastX, s.lastY, false, webPointerId, false);
294
+ }
295
+ }
296
+ s.active = false;
297
+ s.targetId = -1;
298
+ s.dragged = false;
299
+ s.defaultPrevented = false;
300
+ return true;
301
+ }
302
+ return false;
303
+ }
304
+
305
+ // Immediate-mode (no UI tree) path: synthesize the pointer event from the raw
306
+ // touch and hand it straight to document-level listeners. No node target, no
307
+ // tree walk — `document` sees every pointer event, exactly like the DOM.
308
+ static bool dispatchToDocument(const Event &event)
309
+ {
310
+ PointerEventType type;
311
+ switch (event.touchPhase) {
312
+ case TouchPhase::Down: type = PointerEventType::TouchStart; break;
313
+ case TouchPhase::Move: type = PointerEventType::TouchMove; break;
314
+ case TouchPhase::Up: type = PointerEventType::TouchEnd; break;
315
+ default: return false;
316
+ }
317
+ PointerEvent pe{};
318
+ pe.type = type;
319
+ pe.targetId = -1;
320
+ // Match the mounted-tree path and the public TypeScript surface:
321
+ // web-facing pointer ids start at 1, while internal touch slots start at 0.
322
+ const int webPointerId = event.pointerId + 1;
323
+ pe.pointerId = webPointerId;
324
+ pe.x = event.x;
325
+ pe.y = event.y;
326
+ pe.clientX = event.x;
327
+ pe.clientY = event.y;
328
+ pe.pageX = event.x;
329
+ pe.pageY = event.y;
330
+ pe.screenX = event.x;
331
+ pe.screenY = event.y;
332
+ pe.primary = event.pointerId == 0;
333
+ pe.touchesLength = event.touching ? 1 : 0;
334
+ pe.targetTouchesLength = event.touching ? 1 : 0;
335
+ pe.changedTouchesLength = 1;
336
+ pe.touches[0] = makeTouchPoint(-1, event.x, event.y, webPointerId);
337
+ pe.targetTouches[0] = pe.touches[0];
338
+ pe.changedTouches[0] = pe.touches[0];
339
+ return gea::embedded::ui::dispatchDocumentPointer(pe);
340
+ }
341
+
342
+ static bool dispatchPointer(PointerEventType type, int targetId, int x, int y, bool activeTouch,
343
+ int pointerId = 1, bool primary = true)
344
+ {
345
+ PointerEvent event{};
346
+ event.type = type;
347
+ // Skip entirely when no node listens for this type — don't build the
348
+ // event payload or walk the tree. Apps that bind only touch* never pay
349
+ // for the paired pointer* dispatch (and vice versa).
350
+ if (!gea::embedded::ui::Tree::instance().hasListenersForType(event.typeName())) return false;
351
+ event.targetId = targetId;
352
+ event.pointerId = pointerId;
353
+ event.x = x;
354
+ event.y = y;
355
+ event.clientX = x;
356
+ event.clientY = y;
357
+ event.pageX = x;
358
+ event.pageY = y;
359
+ event.screenX = x;
360
+ event.screenY = y;
361
+ event.primary = primary;
362
+ event.touchesLength = activeTouch ? 1 : 0;
363
+ event.targetTouchesLength = activeTouch ? 1 : 0;
364
+ event.changedTouchesLength = 1;
365
+ event.touches[0] = makeTouchPoint(targetId, x, y, pointerId);
366
+ event.targetTouches[0] = event.touches[0];
367
+ event.changedTouches[0] = event.touches[0];
368
+ gea::embedded::ui::Tree::instance().dispatchEvent(event);
369
+ return event.defaultPrevented;
370
+ }
371
+
372
+ static TouchPoint makeTouchPoint(int targetId, int x, int y, int identifier = 1)
373
+ {
374
+ TouchPoint point{};
375
+ point.identifier = identifier;
376
+ point.target = EventTarget(targetId);
377
+ point.screenX = x;
378
+ point.screenY = y;
379
+ point.clientX = x;
380
+ point.clientY = y;
381
+ point.pageX = x;
382
+ point.pageY = y;
383
+ return point;
384
+ }
385
+
386
+ static int inputAncestor(int nodeId)
387
+ {
388
+ auto &tree = gea::embedded::ui::Tree::instance();
389
+ for (int n = nodeId; n >= 0 && n < tree.nodeCount(); n = tree.node(n).parent) {
390
+ if (std::strcmp(tree.tagName(n), "input") == 0) return n;
391
+ }
392
+ return -1;
393
+ }
394
+
395
+ bool dispatchEnabled_ = true;
396
+ bool wasActive_ = false;
397
+ int lastActivityMs_ = 0;
398
+ bool activeDefaultPrevented_ = false;
399
+ int lastX_ = 0;
400
+ int lastY_ = 0;
401
+ int startX_ = 0;
402
+ int startY_ = 0;
403
+ bool dragged_ = false;
404
+ int activeTargetId_ = -1;
405
+
406
+ // Per-pointer state for non-primary (pointerId >= 1) simultaneous fingers.
407
+ struct Secondary {
408
+ bool active = false;
409
+ bool dragged = false;
410
+ bool defaultPrevented = false;
411
+ int targetId = -1;
412
+ int startX = 0;
413
+ int startY = 0;
414
+ int lastX = 0;
415
+ int lastY = 0;
416
+ };
417
+ static constexpr int kMaxSecondaryPointers = 9;
418
+ Secondary secondary_[kMaxSecondaryPointers]{};
419
+ };
420
+
421
+ } // namespace
422
+
423
+ // Touch controllers report PANEL-NATIVE coordinates; the UI hit-tests in
424
+ // LOGICAL (orientation-rotated) space. Inverse of the display's
425
+ // panelForSourcePixel mapping. Applied to QUEUED events AND to the raw
426
+ // latest-move cache at consumption (the dispatcher overrides Move coords
427
+ // from that cache, and Up inherits the last Move — leaving those raw made
428
+ // rail taps land in dead zones on rotated apps).
429
+ // (When INJECTING test events, pass panel-frame coordinates: px = ly,
430
+ // py = nativeH-1-lx — injecting logical coords double-transforms.)
431
+ void TouchRuntime::transformTouchToLogical(int *x, int *y)
432
+ {
433
+ using gea::framework::display::DisplayOrientation;
434
+ namespace od = gea::framework::display::detail;
435
+ const int nativeW = od::DisplayOrientationState::nativeWidth();
436
+ const int nativeH = od::DisplayOrientationState::nativeHeight();
437
+ const int px = *x;
438
+ const int py = *y;
439
+ switch (od::DisplayOrientationState::orientation()) {
440
+ case DisplayOrientation::LandscapePrimary:
441
+ // panel = {srcY, nativeH - 1 - srcX} => src = {nativeH - 1 - py, px}
442
+ *x = nativeH - 1 - py;
443
+ *y = px;
444
+ break;
445
+ case DisplayOrientation::LandscapeSecondary:
446
+ // panel = {nativeW - 1 - srcY, srcX} => src = {py, nativeW - 1 - px}
447
+ *x = py;
448
+ *y = nativeW - 1 - px;
449
+ break;
450
+ case DisplayOrientation::PortraitSecondary:
451
+ *x = nativeW - 1 - px;
452
+ *y = nativeH - 1 - py;
453
+ break;
454
+ case DisplayOrientation::PortraitPrimary:
455
+ default:
456
+ break;
457
+ }
458
+ }
459
+
460
+ void TouchRuntime::queueTouchEvent(TouchPhase phase, bool touching, int x, int y, int pointerId)
461
+ {
462
+ TouchRuntime::transformTouchToLogical(&x, &y);
463
+ TouchEventDispatcher::instance().queue(phase, touching, x, y, pointerId);
464
+ }
465
+
466
+ bool TouchRuntime::start()
467
+ {
468
+ // Single-finger path (all controllers): primary pointer, id 0.
469
+ gea::platform::touch::Touchscreen::setObserver(
470
+ [](gea::platform::touch::Phase phase, bool touching, int x, int y) {
471
+ TouchRuntime::queueTouchEvent(static_cast<TouchPhase>(phase), touching, x, y, 0);
472
+ });
473
+ // Multi-finger path (controllers that implement it, e.g. GT911): each finger
474
+ // arrives with its index. A controller fires EITHER this or the single
475
+ // observer, never both, so there's no double dispatch.
476
+ gea::platform::touch::Touchscreen::setPointerObserver(
477
+ [](gea::platform::touch::Phase phase, bool touching, int x, int y, int pointerId) {
478
+ TouchRuntime::queueTouchEvent(static_cast<TouchPhase>(phase), touching, x, y, pointerId);
479
+ });
480
+ return gea::platform::touch::Touchscreen::init();
481
+ }
482
+
483
+ void TouchRuntime::poll(int nowMs)
484
+ {
485
+ gea::platform::touch::Touchscreen::poll(nowMs);
486
+ }
487
+
488
+ void TouchRuntime::setDispatchEnabled(bool enabled)
489
+ {
490
+ TouchEventDispatcher::instance().setDispatchEnabled(enabled);
491
+ }
492
+
493
+ bool TouchRuntime::dispatchEnabled()
494
+ {
495
+ return TouchEventDispatcher::instance().dispatchEnabled();
496
+ }
497
+
498
+ void TouchRuntime::resetGestureState()
499
+ {
500
+ TouchEventDispatcher::instance().resetGesture();
501
+ }
502
+
503
+ bool TouchRuntime::dispatchEvent(const Event &event)
504
+ {
505
+ return TouchEventDispatcher::instance().dispatch(event);
506
+ }
507
+
508
+ bool TouchRuntime::gestureActiveWithin(int nowMs, int windowMs)
509
+ {
510
+ return TouchEventDispatcher::instance().activeWithin(nowMs, windowMs);
511
+ }
512
+
513
+ } // namespace gea::framework::events