plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
3955e74
— Peter John
2026-11-11T00:10:11+05:30
game: hover goes to the button a click reaches; a non-screen spawned under the root crashes
- compiler/cli/tests/cli/game_entry_test.rs +44 -0
- docs/game.md +3 -3
- std/game/Ctx.plum +30 -2
- std/game/Game.plum +30 -1
- std/game/SceneTree.plum +3 -2
- std/game/ui.plum +30 -2
- tests/port/game/routing.plum +5 -4
compiler/cli/tests/cli/game_entry_test.rs
CHANGED
|
@@ -112,3 +112,47 @@ fn aTypeWithOnlyUpdateOrDrawIsAProgram() {
|
|
|
112
112
|
let _ = std::fs::remove_dir_all(&dir);
|
|
113
113
|
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
|
|
114
114
|
}
|
|
115
|
+
|
|
116
|
+
/// Runs `text` with `plum run` and returns (success, stderr).
|
|
117
|
+
fn runSource(name: &str, text: &str) -> (bool, String) {
|
|
118
|
+
let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_gameentry_{}_{name}", std::process::id())));
|
|
119
|
+
let src = dir.join("app.plum");
|
|
120
|
+
std::fs::write(&src, text).unwrap();
|
|
121
|
+
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).ancestors().nth(2).unwrap();
|
|
122
|
+
let out = Command::new(plumBin()).current_dir(root).args(["run", src.to_str().unwrap()]).output().unwrap();
|
|
123
|
+
let _ = std::fs::remove_dir_all(&dir);
|
|
124
|
+
(out.status.success(), String::from_utf8_lossy(&out.stderr).into_owned())
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/// A game whose one actor spawns `spawned` under `parent` on its first step; `main` runs
|
|
128
|
+
/// one tick and prints how many actors the screen has.
|
|
129
|
+
fn spawningGame(parent: &str, spawned: &str) -> String {
|
|
130
|
+
format!(
|
|
131
|
+
"module app\nimport std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/math/Vec2\nimport std/game/input\n\
|
|
132
|
+
import std/game/Store\nimport std/game/Game\nimport std/game/Place\nimport std/game/Actor\nimport std/game/Ctx\nimport std/game/SceneTree\nimport std/collections/RoseTree\n\n\
|
|
133
|
+
struct Count[Reducer<Int>](n: Int) =\n fun update(self, msg: Int) -> Count = Count(n: self.n + msg)\n\n\
|
|
134
|
+
struct Dot[Actor<Int>](id: Int) =\n fun key(self) -> Int = self.id\n fun place(self) -> Place = place.at(0.0, 0.0)\n\n\
|
|
135
|
+
struct Spawner[Actor<Int>](id: Int, done: Bool) =\n fun key(self) -> Int = self.id\n fun place(self) -> Place = place.at(0.0, 0.0)\n \
|
|
136
|
+
fun step(self, ctx: Ctx<Int>, dt: Float) -> Spawner =\n if !self.done\n ctx.out.push(Spawn(parent: {parent}, node: RoseTree.leaf({spawned})))\n Spawner(..self, done: true)\n\n\
|
|
137
|
+
struct OnlyScreen[Screen<Count, Int>](id: Int) =\n fun key(self) -> Int = self.id\n fun place(self) -> Place = place.at(0.0, 0.0)\n\n\
|
|
138
|
+
struct OtherScreen[Screen<Count, Int>](id: Int) =\n fun key(self) -> Int = self.id\n fun place(self) -> Place = place.at(0.0, 0.0)\n\n\
|
|
139
|
+
fun main(io: Io) =\n kid: RoseTree<Actor<Int>> := RoseTree.leaf(Spawner(id: 0, done: false))\n only: RoseTree<Actor<Int>> := RoseTree.of(OnlyScreen(id: -2), [kid])\n \
|
|
140
|
+
g := Game.start(SceneTree.ofScreens([only]), Count(n: 0))\n g1 := g.step(io, Input.create(), 0.1, Vec2(x: 800.0, y: 600.0))\n \
|
|
141
|
+
io.printLn(\"screen {{g1.screenTree().count()}} tree {{g1.tree.count()}}\")\n"
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// The root's children are the screens: a `Spawn` of a plain actor under the root stops the
|
|
146
|
+
/// program and names the screen to spawn it under; one under the screen joins it; a screen
|
|
147
|
+
/// spawned under the root joins the screens.
|
|
148
|
+
#[test]
|
|
149
|
+
fn aSpawnUnderTheRootMustBeAScreen() {
|
|
150
|
+
let (ok, err) = runSource("root_spawn_plain", &spawningGame("scenetree.ScreensRootId", "Dot(id: 5)"));
|
|
151
|
+
assert!(!ok, "expected a crash; stderr: {err}");
|
|
152
|
+
assert!(err.contains("game: a Spawn put the actor 5 under the root, and it is not a screen"), "{err}");
|
|
153
|
+
assert!(err.contains("Spawn(parent: -2, ..)"), "{err}");
|
|
154
|
+
let (ok, err) = runSource("screen_spawn", &spawningGame("-2", "Dot(id: 5)"));
|
|
155
|
+
assert!(ok, "{err}");
|
|
156
|
+
let (ok, err) = runSource("root_spawn_screen", &spawningGame("scenetree.ScreensRootId", "OtherScreen(id: -3)"));
|
|
157
|
+
assert!(ok, "{err}");
|
|
158
|
+
}
|
docs/game.md
CHANGED
|
@@ -100,7 +100,7 @@ Each tick, `Game.update(io, input, dt)` does this, in this order:
|
|
|
100
100
|
|
|
101
101
|
1. Each pointer event of the tick goes to the topmost shown `Clickable` actor of the current screen under it that says it is hit (`hit`), through its `input`, then to each of its parents up to the root. Each gets the event in its own coordinates. A `PointerMove` that leaves the pointer where it was goes to no actor.
|
|
102
102
|
2. The root and the current screen's actors step (`step`), parents before children, then the screen's `rules` run. Both see the tick's input in `ctx.input` (held keys, the pointer) and the screen's size in `ctx.view`.
|
|
103
|
-
3. The spawns and kills apply to the root and the current screen (`ctx.out.push(Spawn(parent, node))`, `ctx.out.push(Kill(id))`): those pushed by `input`, then by the steps, then by the rules.
|
|
103
|
+
3. The spawns and kills apply to the root and the current screen (`ctx.out.push(Spawn(parent, node))`, `ctx.out.push(Kill(id))`): those pushed by `input`, then by the steps, then by the rules. The root's children are the screens, so a node spawned under the root itself must be a `Screen` (it joins the others); any other actor there stops the program with a message naming the current screen to spawn it under, since it would never be stepped or drawn.
|
|
104
104
|
4. Each message sent, in order: the store reduces it (`update`), then the current screen `react`s to the new state.
|
|
105
105
|
5. After the tick: `handle(io, msg)` runs for each message, in order, with the state after the tick. Then, if `state.screen()` names another screen, that one `enter`s and becomes current, coming in by its `transition`. Then `ctx.out.push(Restart)` starts the game over from its first tree and state, and `ctx.out.push(Exit)` ends it: no tick runs after it, and the last frame stays drawn.
|
|
106
106
|
|
|
@@ -270,7 +270,7 @@ A game's actors live in a scene tree from [`std/game`](../std/game): a `SceneTre
|
|
|
270
270
|
|
|
271
271
|
Each kind of actor is its own struct, with only its own fields, that implements `Actor<M>` ([`Actor.plum`](../std/game/Actor.plum)). It must say which actor it is (`key`, the id the tree finds it by) and where it is in its parent (`place`, a `Place` with a position, scale, rotation and origin). Everything else has a default that it may override:
|
|
272
272
|
|
|
273
|
-
- `step(self, ctx, dt)` returns its next version, once a tick. `ctx` ([`Ctx.plum`](../std/game/Ctx.plum)) has its parent's world transform and alpha, the tick's input (`ctx.input`)
|
|
273
|
+
- `step(self, ctx, dt)` returns its next version, once a tick. `ctx` ([`Ctx.plum`](../std/game/Ctx.plum)) has its parent's world transform and alpha, the tick's input (`ctx.input`), the screen's size (`ctx.view`) and, during a tree's step, the key of the actor a pointer event would go to (`ctx.pointedAt()`, an `Option`), and takes commands for after the step: `ctx.out.push(Spawn(parent, node))`, `ctx.out.push(Kill(id))`, `ctx.send(msg)`, `ctx.out.push(Restart)` and `ctx.out.push(Exit)`.
|
|
274
274
|
- `input(self, ctx, e)` returns its next version after a pointer event on it or on one of its children, before the tick's steps. `e` is in its own coordinates.
|
|
275
275
|
- `paint(self, io, m, alpha)` draws it in its own coordinates seen through `m`. `Actor.plum` has helpers for boxes, circles, lines, sprites and text (`actor.boxPaint(io, m, alpha, w, h, color)`, ...).
|
|
276
276
|
- `shown`, `gone` and `opacity`: a hidden actor isn't drawn or hit, a gone one is dropped with its subtree, and opacities multiply down the tree.
|
|
@@ -303,7 +303,7 @@ struct Target[Actor<Msg>, Clickable](id: Int, x: Float, y: Float) =
|
|
|
303
303
|
_ => self
|
|
304
304
|
```
|
|
305
305
|
|
|
306
|
-
Keep an actor's position in plain fields (`x: Float, y: Float`) and build its `Place` in `place`, as `place.at(self.x, self.y)`: a `Place` field costs a step about 1.7 times as much. An actor that doesn't change should return `self` from `step` and `input` (the defaults do), not a copy: an `input` that returns the actor as it was leaves the tree as it was (`@sameRef`, see [Built-ins](language.md#reference-identity)), and a screen's `react` that returns its tree as it was (the default) leaves the game's tree as it was. std gives `Group` (a parent that paints nothing) and `Box` (`Clickable`, hit inside it); [`std/game/ui.plum`](../std/game/ui.plum) gives `Label` and `Button` (`Clickable`) (`ui.button(id, x, y, w, h, label, onClick)`). A button shows the pointer over it and a press on it, and a press and a release on it send its `onClick` message.
|
|
306
|
+
Keep an actor's position in plain fields (`x: Float, y: Float`) and build its `Place` in `place`, as `place.at(self.x, self.y)`: a `Place` field costs a step about 1.7 times as much. An actor that doesn't change should return `self` from `step` and `input` (the defaults do), not a copy: an `input` that returns the actor as it was leaves the tree as it was (`@sameRef`, see [Built-ins](language.md#reference-identity)), and a screen's `react` that returns its tree as it was (the default) leaves the game's tree as it was. std gives `Group` (a parent that paints nothing) and `Box` (`Clickable`, hit inside it); [`std/game/ui.plum`](../std/game/ui.plum) gives `Label` and `Button` (`Clickable`) (`ui.button(id, x, y, w, h, label, onClick)`). A button shows the pointer over it and a press on it, and a press and a release on it send its `onClick` message. It counts as under the pointer only when a pointer event there would go to it (`ctx.pointedAt()`, the key of the topmost hit `Clickable` actor, found once per step), so of two overlapping buttons only the top one, the one a click reaches, shows hovered.
|
|
307
307
|
|
|
308
308
|
The tree's `step(ctx, dt)` steps every actor, parents before children, then applies the spawns and kills; `input(ctx, e)` routes one pointer event. `Game` calls both. `add`, `update`, `kill` and `find` work by id, and `updateAs<T>(id, f)` and `findAs<T>(id)` act on an actor of the type `T` only, which is how a screen's `rules` and `react` read and change one kind's own fields in its subtree. `allAs<T>()` is every actor of the type `T`, in tree order, and `kidsAs<T>(parent)` the `T`s among one actor's children (a layer: `t.kidsAs<Balloon>(BalloonLayer)`), which is how `rules` reads one kind's actors. `subtree(id)` is an actor with its subtree as a tree of its own, and `withTop(a)` a tree with its root actor replaced. A pattern `Orb as o` on an `Actor<M>` matches an `Orb`.
|
|
309
309
|
|
std/game/Ctx.plum
CHANGED
|
@@ -10,6 +10,8 @@ import std/math/Affine
|
|
|
10
10
|
import std/game/input
|
|
11
11
|
import std/collections/RoseTree
|
|
12
12
|
import std/game/Actor
|
|
13
|
+
import std/game/SceneTree
|
|
14
|
+
import std/Option
|
|
13
15
|
|
|
14
16
|
# What a step changes besides the actors. `Spawn` and `Kill` are the tree's: they are
|
|
15
17
|
# applied once the step's walk is over (`SceneTree.apply`): a new node under the actor
|
|
@@ -46,9 +48,13 @@ enum Cmd<M> =
|
|
|
46
48
|
# ctx.out.push(Spawn(parent: 0, node: RoseTree.leaf(Bullet(id: self.next, x: self.x, y: self.y))))
|
|
47
49
|
# Gun(..self, next: self.next + 1)
|
|
48
50
|
#
|
|
51
|
+
# `scene` is the tree being stepped (`SceneTree.step` sets it; empty for a context made
|
|
52
|
+
# by hand), so an actor can ask which actor the pointer is on (`pointedAt`); `pointed`
|
|
53
|
+
# keeps the answer once found, shared by every context of one step's walk.
|
|
54
|
+
#
|
|
49
55
|
# A context is a value. The walk makes one for each parent with children (`under`), with
|
|
50
56
|
# one multiply, and hands it to every child.
|
|
51
|
-
struct Ctx<M>(up: Affine, alpha: Float, input: Input, view: Vec2, out: Slice<Cmd<M>>) =
|
|
57
|
+
struct Ctx<M>(up: Affine, alpha: Float, input: Input, view: Vec2, out: Slice<Cmd<M>>, scene: RoseTree<Actor<M>>, pointed: Slice<Int>) =
|
|
52
58
|
|
|
53
59
|
# The context above a tree's root: the identity transform, full opacity, no input, no
|
|
54
60
|
# screen and no commands.
|
|
@@ -56,7 +62,26 @@ struct Ctx<M>(up: Affine, alpha: Float, input: Input, view: Vec2, out: Slice<Cmd
|
|
|
56
62
|
|
|
57
63
|
# The context above a tree's root for a tick with `input` on a screen of `view` pixels.
|
|
58
64
|
fun of(input: Input, view: Vec2) -> Ctx<M> =
|
|
59
|
-
Ctx(up: Affine.Identity, alpha: 1.0, input: input, view: view, out: Slice<Cmd<M>>())
|
|
65
|
+
Ctx(up: Affine.Identity, alpha: 1.0, input: input, view: view, out: Slice<Cmd<M>>(), scene: RoseEmpty, pointed: Slice<Int>())
|
|
66
|
+
|
|
67
|
+
# The context for a step's walk over the tree `root`: the same input, view and commands,
|
|
68
|
+
# with `root` as the scene `pointedAt` looks in.
|
|
69
|
+
fun over(self, root: RoseTree<Actor<M>>) -> Ctx<M> =
|
|
70
|
+
Ctx(..self, scene: root, pointed: Slice<Int>())
|
|
71
|
+
|
|
72
|
+
# The key of the actor a pointer event at the tick's pointer would go to: the topmost
|
|
73
|
+
# shown `Clickable` actor there that says it is hit (as `SceneTree.input` routes one),
|
|
74
|
+
# or None. A widget shows itself hovered only when it is this one, so of two overlapping
|
|
75
|
+
# buttons only the one a click reaches is hovered. Found once per step's walk, the first
|
|
76
|
+
# time an actor asks; None when the context has no scene.
|
|
77
|
+
fun pointedAt(self) -> Option<Int> =
|
|
78
|
+
if self.scene.isEmpty()
|
|
79
|
+
None
|
|
80
|
+
else
|
|
81
|
+
if self.pointed.length() == 0
|
|
82
|
+
self.pointed.push(scenetree.hitChain(self.scene, self.input.pos).map(|a| a.key()).unwrapOr(NoPointedActor))
|
|
83
|
+
k := self.pointed.at(0)
|
|
84
|
+
if k == NoPointedActor then None else Some(k)
|
|
60
85
|
|
|
61
86
|
# The world transform of a child whose own transform is `local`.
|
|
62
87
|
fun worldOf(self, local: Affine) -> Affine = self.up.mul(local)
|
|
@@ -100,6 +125,9 @@ struct Ctx<M>(up: Affine, alpha: Float, input: Input, view: Vec2, out: Slice<Cmd
|
|
|
100
125
|
self.out.clear()
|
|
101
126
|
self.out.add(kept)
|
|
102
127
|
|
|
128
|
+
# `Ctx.pointed`'s mark for "the pointer is on no actor".
|
|
129
|
+
NoPointedActor = -1000003
|
|
130
|
+
|
|
103
131
|
# Whether each command of `cmds` from index `i` on is the game's (`game`) or each the
|
|
104
132
|
# tree's (not `game`).
|
|
105
133
|
fun allGame<M>(cmds: Slice<Cmd<M>>, game: Bool, i: Int) -> Bool =
|
std/game/Game.plum
CHANGED
|
@@ -61,7 +61,9 @@ import std/game/SceneTree
|
|
|
61
61
|
# `ctx.input`.
|
|
62
62
|
# 3. `Spawn` and `Kill` apply to the root and the current screen: those pushed by
|
|
63
63
|
# `input` in the step's own walk, those of the steps after it, those of the rules
|
|
64
|
-
# after that (`SceneTree.step`, `apply`).
|
|
64
|
+
# after that (`SceneTree.step`, `apply`). A node spawned under the root itself must
|
|
65
|
+
# be a screen, since the root's children are the screens: any other actor there
|
|
66
|
+
# stops the program (it would never be stepped or drawn).
|
|
65
67
|
# 4. Each `Send(msg)`, in the order pushed: the store reduces it (`update`), then the
|
|
66
68
|
# current screen `react`s to the new state.
|
|
67
69
|
# 5. After the tick: the store's `flush(io)` runs `handle(io, msg)` for each message in
|
|
@@ -260,6 +262,8 @@ struct Game<S: Reducer<M>, M>(
|
|
|
260
262
|
applied := ruled.apply(ctx.out)
|
|
261
263
|
ctx.keepOnly(true)
|
|
262
264
|
now := keyNow<S, M>(applied.root.kids(), stepKey, name)
|
|
265
|
+
if Some(e) := rootSpawnError<S, M>(liveExtras(applied.root.kids(), now), now)
|
|
266
|
+
crash e
|
|
263
267
|
back := self.tree.withRoot(spliced(self.tree.root, applied.root, key, now))
|
|
264
268
|
begin := Turn(tree: back, store: self.store, key: now, restart: false, exit: false)
|
|
265
269
|
turn := ctx.out.fold(begin, |tu, c| tu.take(c, name))
|
|
@@ -458,6 +462,19 @@ fun liveExtras<M>(lk: RoseTree<Actor<M>>, key: Int) -> RoseTree<Actor<M>> = matc
|
|
|
458
462
|
else
|
|
459
463
|
RoseNode(value: a, firstChild: k, nextSibling: liveExtras(n, key))
|
|
460
464
|
|
|
465
|
+
# What is wrong with the nodes spawned under the root this tick (`extras`, as
|
|
466
|
+
# `liveExtras` finds them; the current screen is keyed `screen`), if anything: the root's
|
|
467
|
+
# children are the screens, so each must be a `Screen<S, M>`, which joins the others. An
|
|
468
|
+
# actor that isn't one would be kept under the root but never stepped or drawn, so it
|
|
469
|
+
# stops the program, naming the screen to spawn it under instead.
|
|
470
|
+
fun rootSpawnError<S, M>(extras: RoseTree<Actor<M>>, screen: Int) -> Option<Str> = match extras
|
|
471
|
+
RoseEmpty => None
|
|
472
|
+
RoseNode(a, _, n) =>
|
|
473
|
+
if nameOf<S, M>(a) == ""
|
|
474
|
+
Some("game: a Spawn put the actor {a.key()} under the root, and it is not a screen (the root's children are the screens): spawn it under the current screen, Spawn(parent: {screen}, ..), or under one of its actors")
|
|
475
|
+
else
|
|
476
|
+
rootSpawnError<S, M>(n, screen)
|
|
477
|
+
|
|
461
478
|
# The chain `kids` with the node keyed `key` replaced by the node `by` (removed if `by` is
|
|
462
479
|
# empty).
|
|
463
480
|
fun replaceKid<M>(kids: RoseTree<Actor<M>>, key: Int, by: RoseTree<Actor<M>>) -> RoseTree<Actor<M>> = match kids
|
|
@@ -738,6 +755,18 @@ test "one tick: input up the parents, steps, rules, then each message reduced an
|
|
|
738
755
|
assert g.state().n == 0
|
|
739
756
|
assert gonScreen(g) == "title 0"
|
|
740
757
|
|
|
758
|
+
test "a node spawned under the root must be a screen"
|
|
759
|
+
log := Slice<Str>()
|
|
760
|
+
plain: RoseTree<Actor<GMsg>> := RoseTree.leaf(GProbe(id: 77, x: 0.0, log: log, go: false))
|
|
761
|
+
e := rootSpawnError<GState, GMsg>(plain, 1000)
|
|
762
|
+
assert e.isSome() && e!.contains("actor 77 under the root") && e!.contains("Spawn(parent: 1000, ..)")
|
|
763
|
+
assert rootSpawnError<GState, GMsg>(gplayNode(log, Cut, Nil), 1000).isNone()
|
|
764
|
+
empty: RoseTree<Actor<GMsg>> := RoseEmpty
|
|
765
|
+
assert rootSpawnError<GState, GMsg>(empty, 1000).isNone()
|
|
766
|
+
# a screen first, then a plain actor: the plain one is named
|
|
767
|
+
both: RoseTree<Actor<GMsg>> := RoseNode(value: GPlayScreen(log: log, how: Cut), firstChild: empty, nextSibling: plain)
|
|
768
|
+
assert rootSpawnError<GState, GMsg>(both, 1000)!.contains("actor 77")
|
|
769
|
+
|
|
741
770
|
test "only the current screen is shown and drawn"
|
|
742
771
|
log := Slice<Str>()
|
|
743
772
|
g := gstart(log, false)
|
std/game/SceneTree.plum
CHANGED
|
@@ -148,12 +148,13 @@ struct SceneTree<M>(root: RoseTree<Actor<M>>, camX: Float, camY: Float, camScale
|
|
|
148
148
|
# A tick whose commands are all the game's (most ticks) walks with no batch at all, and
|
|
149
149
|
# one whose actors push no `Spawn` or `Kill` applies nothing after it.
|
|
150
150
|
fun step(self, ctx: Ctx<M>, dt: Float) -> SceneTree<M> =
|
|
151
|
+
c := ctx.over(self.root)
|
|
151
152
|
r := if gameOnly(ctx.out, 0)
|
|
152
|
-
stepPlain(self.root,
|
|
153
|
+
stepPlain(self.root, c, dt)
|
|
153
154
|
else
|
|
154
155
|
w := TreeWalk.ofTree(ctx.out)
|
|
155
156
|
ctx.keepOnly(true)
|
|
156
|
-
stepChain(self.root,
|
|
157
|
+
stepChain(self.root, c, dt, w, RoseEmpty)
|
|
157
158
|
s := self.withRoot(r)
|
|
158
159
|
if gameOnly(ctx.out, 0)
|
|
159
160
|
s
|
std/game/ui.plum
CHANGED
|
@@ -48,9 +48,15 @@ struct Button<M>[Actor<M>, Clickable](id: Int, x: Float, y: Float, w: Float, h:
|
|
|
48
48
|
Button(..self, armed: false, look: 1)
|
|
49
49
|
_ => self
|
|
50
50
|
|
|
51
|
-
# Its look from the tick's pointer; a release anywhere disarms it.
|
|
51
|
+
# Its look from the tick's pointer; a release anywhere disarms it. It is over the
|
|
52
|
+
# pointer when a pointer event there would go to it (`ctx.pointedAt()`), so of
|
|
53
|
+
# overlapping buttons only the top one shows hovered or pressed; stepped outside a
|
|
54
|
+
# tree's step, by its own `hit`.
|
|
52
55
|
fun step(self, ctx: Ctx<M>, dt: Float) -> Button<M> =
|
|
56
|
+
over := if ctx.scene.isEmpty()
|
|
53
|
-
|
|
57
|
+
self.hit(ctx.worldOf(self.place().local()).inverse().apply(ctx.input.pos))
|
|
58
|
+
else
|
|
59
|
+
ctx.pointedAt().map(|k| k == self.id).unwrapOr(false)
|
|
54
60
|
armed := self.armed && ctx.input.held.mouse
|
|
55
61
|
look := if armed && over then 2 else if over then 1 else 0
|
|
56
62
|
if armed == self.armed && look == self.look then self else Button(..self, armed: armed, look: look)
|
|
@@ -158,6 +164,28 @@ test "buttons show the pointer and are hit in their box"
|
|
|
158
164
|
assert t.hitId(125.0, 5.0).unwrapOr(-1) == 4
|
|
159
165
|
assert t.hitId(115.0, 5.0).isNone()
|
|
160
166
|
|
|
167
|
+
test "of two overlapping buttons only the top one, which a click reaches, shows the pointer"
|
|
168
|
+
out := Slice<Int>()
|
|
169
|
+
# 4 (x 105 to 115) is on top of 3 (x 100 to 110): they overlap from 105 to 110
|
|
170
|
+
root: RoseTree<Actor<Int>> := RoseTree.of(Group(id: 0, x: 100.0, y: 0.0), [uiButton(3, 30, "Under", 0.0, 0.0), uiButton(4, 40, "Top", 5.0, 0.0)])
|
|
171
|
+
t := SceneTree(root: root, camX: 0.0, camY: 0.0, camScale: 1.0)
|
|
172
|
+
assert t.hitId(107.0, 5.0).unwrapOr(-1) == 4
|
|
173
|
+
hov := uiTick(t, 107.0, false, [PointerMove(x: 107.0, y: 5.0)], out)
|
|
174
|
+
assert uiLook(hov, 4) == 1 && uiLook(hov, 3) == 0
|
|
175
|
+
down := uiTick(hov, 107.0, true, [PointerDown(x: 107.0, y: 5.0)], out)
|
|
176
|
+
assert uiLook(down, 4) == 2 && uiLook(down, 3) == 0
|
|
177
|
+
up := uiTick(down, 107.0, false, [PointerUp(x: 107.0, y: 5.0)], out)
|
|
178
|
+
assert out.length() == 1 && out.at(0) == 40
|
|
179
|
+
# off the top one but still on the one under it, that one shows the pointer
|
|
180
|
+
under := uiTick(up, 102.0, false, [PointerMove(x: 102.0, y: 5.0)], out)
|
|
181
|
+
assert uiLook(under, 3) == 1 && uiLook(under, 4) == 0
|
|
182
|
+
|
|
183
|
+
test "a button stepped outside a tree's step shows the pointer by its own hit"
|
|
184
|
+
b: Button<Int> := button(1, 0.0, 0.0, 10.0, 10.0, "b", 7)
|
|
185
|
+
i := Input(held: Held(mouse: false, keys: Slice<Int>()), events: Slice<InputEvent>(), pos: Vec2(x: 5.0, y: 5.0), wheel: 0.0)
|
|
186
|
+
ctx: Ctx<Int> := Ctx.of(i, Vec2(x: 800.0, y: 600.0))
|
|
187
|
+
assert b.step(ctx, 0.1).look == 1
|
|
188
|
+
|
|
161
189
|
test "a label relabels and its alpha is its opacity"
|
|
162
190
|
l: Label<Int> := Label(id: 1, x: 0.0, y: 0.0, text: "a", size: 10.0, color: Color.White.withAlpha(0.5))
|
|
163
191
|
assert l.relabel("b").text == "b"
|
tests/port/game/routing.plum
CHANGED
|
@@ -316,19 +316,20 @@ fun kept(specs: Slice<Spec>, i: Int) -> Bool =
|
|
|
316
316
|
if i < 0 then true else !specs.at(i).dead && kept(specs, specs.at(i).parent)
|
|
317
317
|
|
|
318
318
|
# A button's look after a step with the pointer at `p` and the mouse up, against the
|
|
319
|
-
# reference: hovered (1) exactly when
|
|
319
|
+
# reference: hovered (1) exactly when it is the reference's topmost hit at `p` (the one a
|
|
320
|
-
#
|
|
320
|
+
# pointer event there goes to), so a button drawn over by another one, or under a hidden
|
|
321
|
-
#
|
|
321
|
+
# parent, is not hovered. "" if every kept button agrees.
|
|
322
322
|
fun hoverError(t: SceneTree<Got>, specs: Slice<Spec>, w: World, p: Vec2) -> Str =
|
|
323
323
|
i := Input(held: Held(mouse: false, keys: Slice<Int>()), events: Slice<InputEvent>(), pos: p, wheel: 0.0)
|
|
324
324
|
ctx: Ctx<Got> := Ctx.of(i, Vec2(x: 800.0, y: 600.0))
|
|
325
325
|
s := t.step(ctx, 1.0 / 60.0)
|
|
326
|
+
top := refHit(specs, w, p)
|
|
326
327
|
0.until(specs.length()).fold("", |err, n|
|
|
327
328
|
sp := specs.at(n)
|
|
328
329
|
if err != "" || !sp.clickable || !kept(specs, n)
|
|
329
330
|
err
|
|
330
331
|
else
|
|
331
|
-
want := if
|
|
332
|
+
want := if top == n then 1 else 0
|
|
332
333
|
match s.findAs<Button<Got>>(n)
|
|
333
334
|
Some(b) => if b.look == want then "" else "button {n} looks {b.look}, the reference says {want}"
|
|
334
335
|
None => "button {n} is not on the stepped tree"
|