plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


bcc1149 — Peter John 2026-11-10T22:12:15+05:30
SceneTree: hit tests and pointer routing run in constant stack along a sibling chain
Files changed (1) hide show
  1. std/game/SceneTree.plum +117 -59
std/game/SceneTree.plum CHANGED
@@ -442,75 +442,98 @@ fun toLocal(pl: Place, p: Vec2) -> Vec2 =
442
442
  # `i`-th of its siblings): its index among its siblings at each depth, from the chain down.
443
443
  # Only a `Clickable` actor can be hit: any other is skipped with no transform computed,
444
444
  # unless it has children, whose coordinates its place gives.
445
- fun hitPath<M>(t: RoseTree<Actor<M>>, p: Vec2, i: Int) -> Option<List<Int>> = match t
445
+ fun hitPath<M>(t: RoseTree<Actor<M>>, p: Vec2, i: Int) -> Option<List<Int>> =
446
+ none: Option<List<Int>> := None
447
+ hitPathFrom(t, p, i, none)
448
+
449
+ # `hitPath`'s walk along the chain, one tail call per sibling (recursion only into
450
+ # children), so a chain of any length runs in constant stack. A later sibling is on top of
451
+ # an earlier one, so the last one hit wins: `best` is the path found so far.
452
+ fun hitPathFrom<M>(t: RoseTree<Actor<M>>, p: Vec2, i: Int, best: Option<List<Int>>) -> Option<List<Int>> = match t
446
- RoseEmpty => None
453
+ RoseEmpty => best
447
- RoseNode(a, k, n) => match hitPath(n, p, i + 1)
454
+ RoseNode(a, k, n) =>
455
+ found := hitPathAt(a, k, p, i)
456
+ hitPathFrom(n, p, i + 1, if found.isSome() then found else best)
457
+
458
+ # The path to the topmost hit in the node of `a` (the `i`-th of its siblings, with the
459
+ # children `k`): in its children first, then itself.
460
+ fun hitPathAt<M>(a: Actor<M>, k: RoseTree<Actor<M>>, p: Vec2, i: Int) -> Option<List<Int>> =
461
+ # One tag test per node; a flag computed at spawn would replace it once comptime lands.
462
+ match a
463
+ Clickable as c =>
464
+ if !isShown(a)
465
+ None
466
+ else
467
+ q := toLocal(a.place(), p)
468
+ below := if k.isEmpty() then None else hitPath(k, q, 0)
469
+ match below
448
- Some(path) => Some(path)
470
+ Some(path) => Some(Cons(head: i, tail: path))
449
- None =>
471
+ None =>
472
+ nil: List<Int> := Nil
450
- # One tag test per node; a flag computed at spawn would replace it once comptime lands.
473
+ if c.hit(q) then Some(Cons(head: i, tail: nil)) else None
451
- match a
474
+ _ =>
452
- Clickable as c =>
453
- if !isShown(a)
475
+ if k.isEmpty() || !isShown(a)
454
- None
476
+ None
455
- else
477
+ else
456
- q := toLocal(a.place(), p)
478
+ match hitPath(k, toLocal(a.place(), p), 0)
457
- below := if k.isEmpty() then None else hitPath(k, q, 0)
458
- match below
459
- Some(path) => Some(Cons(head: i, tail: path))
479
+ Some(path) => Some(Cons(head: i, tail: path))
460
- None =>
461
- nil: List<Int> := Nil
462
- if c.hit(q) then Some(Cons(head: i, tail: nil)) else None
463
- _ =>
464
- if k.isEmpty() || !isShown(a)
465
- None
466
- else
467
- match hitPath(k, toLocal(a.place(), p), 0)
468
- Some(path) => Some(Cons(head: i, tail: path))
469
- None => None
480
+ None => None
470
481
 
471
482
  # The chain `t` after the pointer event `e` at `p` (in the chain's parent's coordinates)
472
483
  # went to the actor at `i`, then `path` below it (as `hitPath` found it): that actor's
473
484
  # `input`, then its parents' on the way back up, each under its parent's context (`ctx`
474
485
  # for this chain). A node whose actor and children come back as they were is kept, and so
486
+ # is every node off the path. The walk to the `i`-th node and the relinking of the nodes
475
- # is every node off the path.
487
+ # before it are loops: constant stack along a chain.
476
- fun routeChain<M>(t: RoseTree<Actor<M>>, ctx: Ctx<M>, p: Vec2, e: InputEvent, i: Int, path: List<Int>) -> RoseTree<Actor<M>> = match t
488
+ fun routeChain<M>(t: RoseTree<Actor<M>>, ctx: Ctx<M>, p: Vec2, e: InputEvent, i: Int, path: List<Int>) -> RoseTree<Actor<M>> = match chainFromNth(t, i)
477
489
  RoseEmpty => t
478
490
  RoseNode(a, k, n) =>
479
- if i > 0
480
- n2 := routeChain(n, ctx, p, e, i - 1, path)
481
- if @sameRef(n2, n) then t else RoseNode(value: a, firstChild: k, nextSibling: n2)
482
- else
483
- pl := a.place()
491
+ pl := a.place()
484
- q := toLocal(pl, p)
492
+ q := toLocal(pl, p)
485
- match path
493
+ k2 := match path
486
- Cons(j, rest) =>
487
- k2 := routeChain(k, ctx.under(pl.local(), a.opacity()), q, e, j, rest)
494
+ Cons(j, rest) => routeChain(k, ctx.under(pl.local(), a.opacity()), q, e, j, rest)
495
+ Nil => k
488
- na := a.input(ctx, InputEvent.pointerAt(e, q))
496
+ na := a.input(ctx, InputEvent.pointerAt(e, q))
489
- if @sameRef(na, a) && @sameRef(k2, k) then t else RoseNode(value: na, firstChild: k2, nextSibling: n)
497
+ if @sameRef(na, a) && @sameRef(k2, k) then t else relinked(t, i, RoseNode(value: na, firstChild: k2, nextSibling: n))
498
+
499
+ # The chain `t` from its `i`-th node on (empty if it is shorter).
500
+ fun chainFromNth<T>(t: RoseTree<T>, i: Int) -> RoseTree<T> = match t
490
- Nil =>
501
+ RoseEmpty => t
502
+ RoseNode(_, _, n) => if i <= 0 then t else chainFromNth(n, i - 1)
503
+
504
+ # The chain `t` with its first `i` nodes copied in front of `node` (which stands for the
491
- na := a.input(ctx, InputEvent.pointerAt(e, q))
505
+ # `i`-th and brings the later ones).
506
+ fun relinked<T>(t: RoseTree<T>, i: Int, node: RoseTree<T>) -> RoseTree<T> = match t
507
+ RoseEmpty => node
492
- if @sameRef(na, a) then t else RoseNode(value: na, firstChild: k, nextSibling: n)
508
+ RoseNode(a, k, n) => if i <= 0 then node else RoseNode(value: a, firstChild: k, nextSibling: relinked(n, i - 1, node))
493
509
 
494
510
  # The topmost shown `Clickable` actor under `p` (a point in the chain's parent's
495
- # coordinates) that says it is hit: later siblings first, and in each, its children before
511
+ # coordinates) that says it is hit: later siblings before earlier ones, and in each, its
496
- # itself. Other actors are skipped as `hitPath` skips them.
512
+ # children before itself. Other actors are skipped as `hitPath` skips them.
497
- fun hitChain<M>(t: RoseTree<Actor<M>>, p: Vec2) -> Option<Actor<M>> = match t
513
+ fun hitChain<M>(t: RoseTree<Actor<M>>, p: Vec2) -> Option<Actor<M>> =
514
+ none: Option<Actor<M>> := None
515
+ hitFrom(t, p, none)
516
+
517
+ # `hitChain`'s walk along the chain, as `hitPathFrom`: the last sibling hit wins.
518
+ fun hitFrom<M>(t: RoseTree<Actor<M>>, p: Vec2, best: Option<Actor<M>>) -> Option<Actor<M>> = match t
498
- RoseEmpty => None
519
+ RoseEmpty => best
499
520
  RoseNode(a, k, n) =>
521
+ found := hitAt(a, k, p)
522
+ hitFrom(n, p, if found.isSome() then found else best)
523
+
524
+ # The topmost hit in the node of `a` (with the children `k`): in its children first, then
525
+ # itself, as `hitPathAt`.
526
+ fun hitAt<M>(a: Actor<M>, k: RoseTree<Actor<M>>, p: Vec2) -> Option<Actor<M>> =
527
+ match a
528
+ Clickable as c =>
529
+ if isShown(a)
530
+ q := toLocal(a.place(), p)
500
- match hitChain(n, p)
531
+ match hitChain(k, q)
501
- Some(found) => Some(found)
532
+ Some(found) => Some(found)
502
- None =>
503
- # One tag test per node; a flag computed at spawn would replace it once comptime lands.
504
- match a
505
- Clickable as c =>
506
- if isShown(a)
507
- q := toLocal(a.place(), p)
508
- match hitChain(k, q)
509
- Some(found) => Some(found)
510
- None => if c.hit(q) then Some(a) else None
533
+ None => if c.hit(q) then Some(a) else None
511
- else
534
+ else
512
- None
535
+ None
513
- _ => if k.isEmpty() || !isShown(a) then None else hitChain(k, toLocal(a.place(), p))
536
+ _ => if k.isEmpty() || !isShown(a) then None else hitChain(k, toLocal(a.place(), p))
514
537
 
515
538
  # --- tests ---
516
539
 
@@ -948,3 +971,38 @@ test "a kill among 200000 siblings runs in constant stack and keeps the others"
948
971
  assert k.count() == 200000 && k.find(100000).isNone() && k.find(100001).isSome()
949
972
  assert wide.kill(-7).count() == 200001
950
973
  assert wide.kidsAs<TBody>(0).length() == 200000
974
+
975
+ test "hit, input, step, apply and the draw list among 200000 siblings run in constant stack"
976
+ kids := 0.until(200000).fold(Slice<RoseTree<Actor<Int>>>(), |acc, i|
977
+ acc.push(RoseTree.leaf(TClicker(id: i + 1, x: Float(i) * 20.0, clicks: 0)))
978
+ acc
979
+ )
980
+ wide := SceneTree.of(tbody(0, 0.0, 0.0, 0.0)).withRoot(RoseTree.of(tbody(0, 0.0, 0.0, 0.0), List.fromSlice(kids)))
981
+ # the first, one in the middle and the last (the deepest walk)
982
+ assert wide.hitId(5.0, 5.0).unwrapOr(-1) == 1
983
+ assert wide.hitId(2000005.0, 5.0).unwrapOr(-1) == 100001
984
+ assert wide.hitId(3999985.0, 5.0).unwrapOr(-1) == 200000
985
+ assert wide.hitId(15.0, 5.0).isNone()
986
+ t := wide.input(Ctx<Int>(), PointerDown(x: 3999985.0, y: 5.0))
987
+ assert t.count() == 200001
988
+ assert t.findAs<TClicker>(200000).unwrap().clicks == 1 && t.findAs<TClicker>(199999).unwrap().clicks == 0
989
+ assert wide.findAs<TClicker>(200000).unwrap().clicks == 0
990
+ ctx := Ctx<Int>()
991
+ assert wide.step(ctx, 1.0).count() == 200001
992
+ ctx.out.push(Kill(id: 100000))
993
+ ctx.out.push(Spawn(parent: 0, node: tclicker(300000, 0.0)))
994
+ s := wide.step(ctx, 1.0)
995
+ assert s.count() == 200001 && s.find(100000).isNone() && s.find(300000).isSome()
996
+ assert wide.flatten().length() == 200001
997
+
998
+ test "later siblings are on top: the last of the overlapping ones is hit, and gets the event"
999
+ s := ttree().add(0, tclicker(1, 0.0)).add(0, tclicker(2, 5.0)).add(0, tclicker(3, 8.0)).add(0, tclicker(4, 100.0))
1000
+ assert s.hitId(9.0, 5.0).unwrapOr(-1) == 3
1001
+ assert s.hitId(6.0, 5.0).unwrapOr(-1) == 2
1002
+ assert s.hitId(1.0, 5.0).unwrapOr(-1) == 1
1003
+ s1 := s.input(Ctx<Int>(), PointerDown(x: 9.0, y: 5.0))
1004
+ assert s1.findAs<TClicker>(3).unwrap().clicks == 1
1005
+ assert s1.findAs<TClicker>(1).unwrap().clicks == 0 && s1.findAs<TClicker>(2).unwrap().clicks == 0
1006
+ # the nodes after the hit one are the same nodes, those before it only relinked
1007
+ assert @sameRef(nodeOf(s1.root, 4), nodeOf(s.root, 4))
1008
+ assert @sameRef(s1.find(1).unwrap(), s.find(1).unwrap())