@geastack/engine 0.1.15 → 0.1.17

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.
package/touch_runtime.cpp CHANGED
@@ -8,9 +8,16 @@
8
8
  #include "touch.h"
9
9
  #include "ui/tree_internal.h"
10
10
  #include "ui/virtual_keyboard.h"
11
+ #include "ui/debugger_picker.h"
11
12
 
12
13
  #include <cstring>
13
14
 
15
+ #if defined(GEA_EMBEDDED_COMPARISON_BENCHMARK) && GEA_EMBEDDED_COMPARISON_BENCHMARK
16
+ // Optional target diagnostic: actual consumption, after move-cache coalescing.
17
+ extern "C" void gea_touch_trace_consumed(int phase, bool touching, int x, int y, int pointerId, int handlerX,
18
+ int handlerY) __attribute__((weak));
19
+ #endif
20
+
14
21
  // Weak default for the optional multi-finger observer. Single-touch controllers
15
22
  // (every target except the GT911 ones) don't override it, so registering a
16
23
  // pointer observer is a harmless no-op there and they keep using the single
@@ -99,18 +106,31 @@ public:
99
106
  // at the same coordinates) returns false so a static hold can't busy-render.
100
107
  bool dispatch(const Event &event)
101
108
  {
109
+ #if defined(GEA_NATIVE_DEBUGGER) && GEA_NATIVE_DEBUGGER
110
+ auto &picker = gea::embedded::ui::debuggerPicker;
111
+ if (picker.capturesInput()) {
112
+ int x = event.x, y = event.y;
113
+ if (event.touchPhase == TouchPhase::Move && event.pointerId == 0) {
114
+ gea::platform::touch::Touchscreen::consumeLatestMove(&x, &y);
115
+ TouchRuntime::transformTouchToLogical(&x, &y);
116
+ }
117
+ if (picker.consume(event.touchPhase, x, y, event.pointerId)) return true;
118
+ }
119
+ #endif
102
120
  // Immediate-mode apps (Display.ctx games) mount no UI tree: hitTestNode
103
121
  // returns -1 and there is no root to bind document listeners onto, so the
104
122
  // tree dispatch below can never reach the app. Deliver pointer events
105
123
  // straight to document-level listeners, exactly like the DOM document, so
106
124
  // every finger (pointerId 0..N) reaches document.addEventListener().
107
125
  if (gea::embedded::ui::Tree::instance().mountedRoot() < 0) {
126
+ recordConsumed(event, event.touching, event.x, event.y, event.x, event.y);
108
127
  return dispatchToDocument(event);
109
128
  }
110
129
  // Additional simultaneous fingers are routed to independent per-pointer
111
130
  // slots so they can hold/tap their own buttons (e.g. move + jump) without
112
131
  // disturbing the primary pointer's scroll / focus / keyboard gesture.
113
132
  if (event.pointerId >= 1) {
133
+ recordConsumed(event, event.touching, event.x, event.y, event.x, event.y);
114
134
  return dispatchSecondary(event);
115
135
  }
116
136
 
@@ -124,6 +144,10 @@ public:
124
144
  TouchRuntime::transformTouchToLogical(&tx, &ty);
125
145
  touching = true;
126
146
  }
147
+ // Up callbacks inherit the last consumed move, while tx/ty retain the
148
+ // consumed edge coordinates. Expose both without changing routing.
149
+ recordConsumed(event, touching, tx, ty, !touching && wasActive_ ? lastX_ : tx,
150
+ !touching && wasActive_ ? lastY_ : ty);
127
151
 
128
152
  if (touching && !wasActive_) {
129
153
  changed = true;
@@ -238,184 +262,213 @@ public:
238
262
  }
239
263
 
240
264
  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]{};
265
+ static void recordConsumed(const Event &event, bool touching, int x, int y, int handlerX, int handlerY)
266
+ {
267
+ #if defined(GEA_EMBEDDED_COMPARISON_BENCHMARK) && GEA_EMBEDDED_COMPARISON_BENCHMARK
268
+ if (gea_touch_trace_consumed) {
269
+ gea_touch_trace_consumed(static_cast<int>(event.touchPhase), touching, x, y, event.pointerId, handlerX,
270
+ handlerY);
271
+ }
272
+ #else
273
+ (void)event;
274
+ (void)touching;
275
+ (void)x;
276
+ (void)y;
277
+ (void)handlerX;
278
+ (void)handlerY;
279
+ #endif
280
+ }
281
+
282
+ // Lightweight per-pointer handler for non-primary fingers. Independently
283
+ // hit-tests and fires touch*/click on its own target, but does NOT touch
284
+ // scroll / focus / keyboard / the hardware Touchscreen state — those stay
285
+ // owned by the primary pointer (pointerId 0).
286
+ bool dispatchSecondary(const Event &event)
287
+ {
288
+ const int slot = event.pointerId - 1;
289
+ if (slot < 0 || slot >= kMaxSecondaryPointers)
290
+ return false;
291
+ Secondary &s = secondary_[slot];
292
+ const int tx = event.x;
293
+ const int ty = event.y;
294
+ const bool touching = event.touching || event.touchPhase == TouchPhase::Move;
295
+ // Web convention: pointerId 1 is the primary pointer, so extra fingers
296
+ // are numbered from 2 upward.
297
+ const int webPointerId = event.pointerId + 1;
298
+
299
+ if (touching && !s.active) {
300
+ s.active = true;
301
+ s.startX = tx;
302
+ s.startY = ty;
303
+ s.lastX = tx;
304
+ s.lastY = ty;
305
+ s.dragged = false;
306
+ s.defaultPrevented = false;
307
+ s.targetId = gea::embedded::ui::Tree::instance().hitTestNode(tx, ty);
308
+ if (s.targetId >= 0) {
309
+ s.defaultPrevented =
310
+ dispatchPointer(PointerEventType::TouchStart, s.targetId, tx, ty, true, webPointerId, false);
311
+ }
312
+ return true;
313
+ }
314
+
315
+ if (touching && s.active) {
316
+ const bool moved = tx != s.lastX || ty != s.lastY;
317
+ if (moved) {
318
+ if (!s.dragged) {
319
+ const int dx = tx - s.startX;
320
+ const int dy = ty - s.startY;
321
+ if (dx > 16 || dx < -16 || dy > 16 || dy < -16)
322
+ s.dragged = true;
323
+ }
324
+ if (s.targetId >= 0) {
325
+ dispatchPointer(PointerEventType::TouchMove, s.targetId, tx, ty, true, webPointerId, false);
326
+ }
327
+ s.lastX = tx;
328
+ s.lastY = ty;
329
+ }
330
+ return moved;
331
+ }
332
+
333
+ if (!touching && s.active) {
334
+ if (s.targetId >= 0) {
335
+ dispatchPointer(PointerEventType::TouchEnd, s.targetId, s.lastX, s.lastY, false, webPointerId, false);
336
+ if (!s.dragged && !s.defaultPrevented) {
337
+ dispatchPointer(PointerEventType::Click, s.targetId, s.lastX, s.lastY, false, webPointerId, false);
338
+ }
339
+ }
340
+ s.active = false;
341
+ s.targetId = -1;
342
+ s.dragged = false;
343
+ s.defaultPrevented = false;
344
+ return true;
345
+ }
346
+ return false;
347
+ }
348
+
349
+ // Immediate-mode (no UI tree) path: synthesize the pointer event from the raw
350
+ // touch and hand it straight to document-level listeners. No node target, no
351
+ // tree walk — `document` sees every pointer event, exactly like the DOM.
352
+ static bool dispatchToDocument(const Event &event)
353
+ {
354
+ PointerEventType type;
355
+ switch (event.touchPhase) {
356
+ case TouchPhase::Down:
357
+ type = PointerEventType::TouchStart;
358
+ break;
359
+ case TouchPhase::Move:
360
+ type = PointerEventType::TouchMove;
361
+ break;
362
+ case TouchPhase::Up:
363
+ type = PointerEventType::TouchEnd;
364
+ break;
365
+ default:
366
+ return false;
367
+ }
368
+ PointerEvent pe{};
369
+ pe.type = type;
370
+ pe.targetId = -1;
371
+ // Match the mounted-tree path and the public TypeScript surface:
372
+ // web-facing pointer ids start at 1, while internal touch slots start at 0.
373
+ const int webPointerId = event.pointerId + 1;
374
+ pe.pointerId = webPointerId;
375
+ pe.x = event.x;
376
+ pe.y = event.y;
377
+ pe.clientX = event.x;
378
+ pe.clientY = event.y;
379
+ pe.pageX = event.x;
380
+ pe.pageY = event.y;
381
+ pe.screenX = event.x;
382
+ pe.screenY = event.y;
383
+ pe.primary = event.pointerId == 0;
384
+ pe.touchesLength = event.touching ? 1 : 0;
385
+ pe.targetTouchesLength = event.touching ? 1 : 0;
386
+ pe.changedTouchesLength = 1;
387
+ pe.touches[0] = makeTouchPoint(-1, event.x, event.y, webPointerId);
388
+ pe.targetTouches[0] = pe.touches[0];
389
+ pe.changedTouches[0] = pe.touches[0];
390
+ return gea::embedded::ui::dispatchDocumentPointer(pe);
391
+ }
392
+
393
+ static bool dispatchPointer(PointerEventType type, int targetId, int x, int y, bool activeTouch, int pointerId = 1,
394
+ bool primary = true)
395
+ {
396
+ PointerEvent event{};
397
+ event.type = type;
398
+ // Skip entirely when no node listens for this type — don't build the
399
+ // event payload or walk the tree. Apps that bind only touch* never pay
400
+ // for the paired pointer* dispatch (and vice versa).
401
+ if (!gea::embedded::ui::Tree::instance().hasListenersForType(event.typeName()))
402
+ return false;
403
+ event.targetId = targetId;
404
+ event.pointerId = pointerId;
405
+ event.x = x;
406
+ event.y = y;
407
+ event.clientX = x;
408
+ event.clientY = y;
409
+ event.pageX = x;
410
+ event.pageY = y;
411
+ event.screenX = x;
412
+ event.screenY = y;
413
+ event.primary = primary;
414
+ event.touchesLength = activeTouch ? 1 : 0;
415
+ event.targetTouchesLength = activeTouch ? 1 : 0;
416
+ event.changedTouchesLength = 1;
417
+ event.touches[0] = makeTouchPoint(targetId, x, y, pointerId);
418
+ event.targetTouches[0] = event.touches[0];
419
+ event.changedTouches[0] = event.touches[0];
420
+ gea::embedded::ui::Tree::instance().dispatchEvent(event);
421
+ return event.defaultPrevented;
422
+ }
423
+
424
+ static TouchPoint makeTouchPoint(int targetId, int x, int y, int identifier = 1)
425
+ {
426
+ TouchPoint point{};
427
+ point.identifier = identifier;
428
+ point.target = EventTarget(targetId);
429
+ point.screenX = x;
430
+ point.screenY = y;
431
+ point.clientX = x;
432
+ point.clientY = y;
433
+ point.pageX = x;
434
+ point.pageY = y;
435
+ return point;
436
+ }
437
+
438
+ static int inputAncestor(int nodeId)
439
+ {
440
+ auto &tree = gea::embedded::ui::Tree::instance();
441
+ for (int n = nodeId; n >= 0 && n < tree.nodeCount(); n = tree.node(n).parent) {
442
+ if (std::strcmp(tree.tagName(n), "input") == 0)
443
+ return n;
444
+ }
445
+ return -1;
446
+ }
447
+
448
+ bool dispatchEnabled_ = true;
449
+ bool wasActive_ = false;
450
+ int lastActivityMs_ = 0;
451
+ bool activeDefaultPrevented_ = false;
452
+ int lastX_ = 0;
453
+ int lastY_ = 0;
454
+ int startX_ = 0;
455
+ int startY_ = 0;
456
+ bool dragged_ = false;
457
+ int activeTargetId_ = -1;
458
+
459
+ // Per-pointer state for non-primary (pointerId >= 1) simultaneous fingers.
460
+ struct Secondary {
461
+ bool active = false;
462
+ bool dragged = false;
463
+ bool defaultPrevented = false;
464
+ int targetId = -1;
465
+ int startX = 0;
466
+ int startY = 0;
467
+ int lastX = 0;
468
+ int lastY = 0;
469
+ };
470
+ static constexpr int kMaxSecondaryPointers = 9;
471
+ Secondary secondary_[kMaxSecondaryPointers]{};
419
472
  };
420
473
 
421
474
  } // namespace