plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
01551fd
— Peter John
2026-11-16T20:42:21+05:30
U5: struct and variant fields in grouped lines; the formatter keeps the grouping as written
- compiler/core/tests/core/formatter_test.rs +23 -0
- compiler/game/tests/fixtures/main_game_fixture.plum +4 -1
- compiler/game/web-test/fixtures/stress_state_view.plum +4 -1
- compiler/tooling/tree-sitter-plum/queries/plum/format.scm +12 -3
- docs/game.md +4 -2
- docs/language.md +11 -0
- examples/games/balloonpop/actors/Balloon.plum +3 -1
- examples/games/balloonpop/screens/PlaygroundScreen.plum +5 -1
- examples/games/clockwork/actors/Spinner.plum +5 -2
- examples/games/clockwork/actors/Walker.plum +3 -1
- examples/games/clockwork/screens/OrreryScreen.plum +2 -1
- examples/games/orbitrunner/actors/Ship.plum +4 -1
- examples/games/orbitrunner/screens/PlayingScreen.plum +4 -2
- std/Decimal.plum +5 -1
- std/collections/Bvh.plum +5 -1
- std/collections/IntervalTree.plum +5 -1
- std/game/Blend.plum +5 -1
- std/game/Box.plum +5 -1
- std/game/Button.plum +5 -1
- std/game/Circle.plum +4 -1
- std/game/Group.plum +4 -1
- std/game/History.plum +9 -2
- std/game/Input.plum +8 -2
- std/game/Label.plum +5 -1
- std/game/Line.plum +5 -1
- std/game/Overlay.plum +5 -1
- std/game/Screen.plum +4 -1
- std/game/ScreenSlice.plum +4 -1
- std/game/Sprite.plum +6 -1
- std/game/Viewport.plum +6 -1
- std/math/Affine.plum +4 -1
- tests/field_defaults.plum +5 -1
- tests/port/game/determinism.plum +5 -1
- tests/port/game/replay.plum +6 -1
- tests/port/game/routing.plum +11 -2
- tests/port/game/screens.plum +4 -1
- tests/std/collections/SegmentTree.plum +4 -1
- tests/std/game/View.plum +7 -1
compiler/core/tests/core/formatter_test.rs
CHANGED
|
@@ -388,3 +388,26 @@ fn attributesKeepTheirPlace() {
|
|
|
388
388
|
let once = formatSource(input).expect("first pass");
|
|
389
389
|
assert_eq!(once, input, "got: {once}");
|
|
390
390
|
}
|
|
391
|
+
|
|
392
|
+
/// A multi-line field list keeps the user's grouping and line breaks.
|
|
393
|
+
#[test]
|
|
394
|
+
fn format_grouped_struct_fields_keep_line_breaks() {
|
|
395
|
+
assertFormatsUnchanged("struct Play(\n a: Int, b: Int,\n c: Float,\n d: Str, e: Bool\n)\n");
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
#[test]
|
|
399
|
+
fn format_grouped_struct_fields_with_defaults_and_generics() {
|
|
400
|
+
assertFormatsUnchanged("struct Box<T>(\n x: Float = 1.0, y: Float = 2.0,\n mut items: List<T>,\n name: Str\n) =\n fun f(self) -> Int =\n 1\n");
|
|
401
|
+
assertFormatsUnchanged("struct Screen[ScreenSlice<Act>](\n dice: Dice, ticks: Int,\n score: Int,\n assets: Assets\n) =\n fun f(self) -> Int =\n 1\n");
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
#[test]
|
|
405
|
+
fn format_grouped_struct_fields_reflow_spacing() {
|
|
406
|
+
let out = formatSource("struct P(\n a:Int,b:Int,\n c:Int\n)\n").unwrap();
|
|
407
|
+
assert_eq!(out, "struct P(\n a: Int, b: Int,\n c: Int\n)\n");
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
#[test]
|
|
411
|
+
fn format_grouped_enum_variant_fields() {
|
|
412
|
+
assertFormatsUnchanged("enum S =\n | Big(\n a: Int, b: Int,\n c: Int, d: Int,\n e: Int\n )\n | Small(a: Int, b: Int)\n");
|
|
413
|
+
}
|
compiler/game/tests/fixtures/main_game_fixture.plum
CHANGED
|
@@ -26,7 +26,10 @@ import std/App
|
|
|
26
26
|
# what is held at its last tick and moves while Right is held.
|
|
27
27
|
enum Step = Heard | Saw(held: Held) | Moved(dt: Float)
|
|
28
28
|
|
|
29
|
-
struct Orbs[ScreenSlice<Step>](
|
|
29
|
+
struct Orbs[ScreenSlice<Step>](
|
|
30
|
+
x: Float, ship: Texture, held: Held,
|
|
31
|
+
heard: Int, events: Int, frame: Int
|
|
32
|
+
) =
|
|
30
33
|
fun subscriptions(self) -> List<Sub<Step>> = [
|
|
31
34
|
OnKeyDown(key: Key.KeyA, act: Heard)
|
|
32
35
|
OnPointerDown(|at| Heard)
|
compiler/game/web-test/fixtures/stress_state_view.plum
CHANGED
|
@@ -88,7 +88,10 @@ struct Scaled[View](scale: Float, children: List<View>) =
|
|
|
88
88
|
enum StressAction = Ticked(dt: Float) | Clicked(i: Int)
|
|
89
89
|
|
|
90
90
|
# The one screen: its clock, its frame count and the clicks so far.
|
|
91
|
-
struct ViewScreen[ScreenSlice<StressAction>](
|
|
91
|
+
struct ViewScreen[ScreenSlice<StressAction>](
|
|
92
|
+
clock: Float, frames: Int, clicks: Int,
|
|
93
|
+
sheet: Texture, white: Texture, anim: Anim
|
|
94
|
+
) =
|
|
92
95
|
fun create(sheet: Texture, white: Texture) -> ViewScreen =
|
|
93
96
|
ViewScreen(clock: 0.0, frames: 0, clicks: 0, sheet: sheet, white: white, anim: StressStateView.pulseAnim())
|
|
94
97
|
|
compiler/tooling/tree-sitter-plum/queries/plum/format.scm
CHANGED
|
@@ -408,15 +408,24 @@
|
|
|
408
408
|
|
|
409
409
|
; `struct` field lists have no node of their own (the `(`, fields and
|
|
410
410
|
; `)` sit directly under the type, whose body always spans lines), so a scope
|
|
411
|
-
; from `(` to `)` decides:
|
|
411
|
+
; from `(` to `)` decides: a list written across lines stays across lines, and a
|
|
412
|
+
; line break after a comma is kept as written (fields may be grouped on a line).
|
|
412
413
|
(struct
|
|
413
414
|
"(" @append_begin_scope @append_empty_scoped_softline @append_indent_start
|
|
414
415
|
")" @prepend_end_scope @prepend_empty_scoped_softline @prepend_indent_end
|
|
415
416
|
(#scope_id! "ctor_fields"))
|
|
416
417
|
|
|
417
418
|
(struct
|
|
418
|
-
"," @
|
|
419
|
+
"," @append_input_softline)
|
|
420
|
+
|
|
421
|
+
; An enum variant's field list follows the same rule.
|
|
422
|
+
(field
|
|
423
|
+
"(" @append_begin_scope @append_empty_scoped_softline @append_indent_start
|
|
424
|
+
")" @prepend_end_scope @prepend_empty_scoped_softline @prepend_indent_end
|
|
419
|
-
(#scope_id! "
|
|
425
|
+
(#scope_id! "variant_fields"))
|
|
426
|
+
|
|
427
|
+
(field
|
|
428
|
+
"," @append_input_softline)
|
|
420
429
|
|
|
421
430
|
; `mut x: T` fields and `export fun`.
|
|
422
431
|
"mut" @append_space
|
docs/game.md
CHANGED
|
@@ -163,8 +163,10 @@ Its motion is a `Body` (below), which `Steps` steps for it: its own `step` is it
|
|
|
163
163
|
|
|
164
164
|
```plum
|
|
165
165
|
struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
166
|
+
dice: Dice, ticks: Int, assets: Assets,
|
|
166
|
-
|
|
167
|
+
score: Int, best: Int, lives: Int, over: Bool, clock: Float,
|
|
167
|
-
ship: Ship, orbs: List<Orb>, mines: List<Mine>, rings: List<Ring>
|
|
168
|
+
ship: Ship, orbs: List<Orb>, mines: List<Mine>, rings: List<Ring>
|
|
169
|
+
)
|
|
168
170
|
```
|
|
169
171
|
|
|
170
172
|
is stepped as if it had written
|
docs/language.md
CHANGED
|
@@ -181,6 +181,17 @@ primitive Demo[App] =
|
|
|
181
181
|
|
|
182
182
|
Fields can't change unless they are marked `mut`. A spread, `User(..ann, age: 32)`, makes an updated value, and the compiler writes it in place when the old value isn't used again. The source of a spread can be any expression, such as a call, a constant or a field (`User(..load(), age: 32)`). It runs once, before the values written after it. Constructor values run in the order they are written, not the order the fields are declared.
|
|
183
183
|
|
|
184
|
+
**Layout:** a struct with many fields is written across lines: after `(` a line break, the fields grouped on lines by what they are (state, game objects, assets), a comma after every field but the last, and `)` on its own line. The formatter keeps the line breaks as written; a field list written on one line stays on one line. An enum variant's fields can be written the same way.
|
|
185
|
+
|
|
186
|
+
```plum
|
|
187
|
+
struct Ship(
|
|
188
|
+
x: Float, y: Float, angle: Float,
|
|
189
|
+
hp: Int, shield: Int,
|
|
190
|
+
tex: Texture
|
|
191
|
+
) =
|
|
192
|
+
const MaxHp = 10
|
|
193
|
+
```
|
|
194
|
+
|
|
184
195
|
**Field defaults:** a field may have a default, `name: T = value`. A construction names its fields, and may leave out one with a default: it gets a fresh copy of the default, evaluated at that construction after the values written there (several defaults in the order their fields are declared), so a `Slice<Int>()` default is a new slice for every value. A construction that leaves out everything is written `Tag()`.
|
|
185
196
|
|
|
186
197
|
```plum
|
examples/games/balloonpop/actors/Balloon.plum
CHANGED
|
@@ -23,7 +23,9 @@ import BalloonPop
|
|
|
23
23
|
import screens/PlaygroundScreen
|
|
24
24
|
|
|
25
25
|
struct Balloon[Actor, Sized, Touch, Presses<PopAction>, Steps](
|
|
26
|
+
id: Int, body: Body,
|
|
26
|
-
|
|
27
|
+
hue: Int = 0, phase: Float = 0.0, clock: Float = 0.0, poppedAt: Float = -1.0
|
|
28
|
+
) =
|
|
27
29
|
const Radius = 40.0
|
|
28
30
|
const MinSpeed = 35.0
|
|
29
31
|
const MaxSpeed = 70.0
|
examples/games/balloonpop/screens/PlaygroundScreen.plum
CHANGED
|
@@ -34,7 +34,11 @@ enum PopAction[Show] =
|
|
|
34
34
|
| Tick(dt: Float)
|
|
35
35
|
|
|
36
36
|
struct PlaygroundScreen[ScreenSlice<PopAction>](
|
|
37
|
+
dice: Dice, ticks: Int,
|
|
38
|
+
assets: Assets,
|
|
37
|
-
|
|
39
|
+
pops: Int, partyAt: Float, clock: Float,
|
|
40
|
+
balloons: List<Balloon>
|
|
41
|
+
) =
|
|
38
42
|
const Count = 6
|
|
39
43
|
const PartyEvery = 10
|
|
40
44
|
const PartyTime = 2.5
|
examples/games/clockwork/actors/Spinner.plum
CHANGED
|
@@ -21,8 +21,11 @@ import Clockwork
|
|
|
21
21
|
import screens/OrreryScreen
|
|
22
22
|
|
|
23
23
|
struct Spinner[Actor, Sized, Touch, Presses<SkyAction>, Steps](
|
|
24
|
+
id: Int, name: Str, size: Float,
|
|
25
|
+
body: Body, tex: Texture,
|
|
24
|
-
|
|
26
|
+
srcX: Float = 0.0, srcY: Float = 0.0, fps: Float = 0.0, frames: Int = 1,
|
|
25
|
-
clock: Float = 0.0, lit: Bool = false
|
|
27
|
+
clock: Float = 0.0, lit: Bool = false
|
|
28
|
+
) =
|
|
26
29
|
fun make(id: Int, name: Str, row: Int, frame: Int, size: Float, spin: Float, tex: Texture) -> Spinner =
|
|
27
30
|
Spinner(id: id, name: name, size: size, body: Body(pos: Vec2.Zero, turn: spin), tex: tex, srcX: Float(frame) * Sheet.Cell, srcY: Float(row) * Sheet.Cell)
|
|
28
31
|
|
examples/games/clockwork/actors/Walker.plum
CHANGED
|
@@ -17,7 +17,9 @@ import std/game/Button
|
|
|
17
17
|
import Clockwork
|
|
18
18
|
import screens/OrreryScreen
|
|
19
19
|
|
|
20
|
-
struct Walker[Actor, Sized, Touch, Presses<SkyAction>, Steps](
|
|
20
|
+
struct Walker[Actor, Sized, Touch, Presses<SkyAction>, Steps](
|
|
21
|
+
id: Int, body: Body, phase: Float, tex: Texture
|
|
22
|
+
) =
|
|
21
23
|
const Min = 500.0
|
|
22
24
|
const Max = 1300.0
|
|
23
25
|
const Size = 24.0
|
examples/games/clockwork/screens/OrreryScreen.plum
CHANGED
|
@@ -35,7 +35,8 @@ enum SkyAction[Show] =
|
|
|
35
35
|
|
|
36
36
|
struct OrreryScreen[ScreenSlice<SkyAction>](
|
|
37
37
|
sun: Spinner, orbits: List<Orbit>, walkers: List<Walker>,
|
|
38
|
-
paused: Bool = false, selected: Int = -1, front: Int = -1
|
|
38
|
+
paused: Bool = false, selected: Int = -1, front: Int = -1
|
|
39
|
+
) =
|
|
39
40
|
const Names = ["Ember", "Cinder", "Ashen", "Glacier"]
|
|
40
41
|
const Moons = [0, 1, 2, 1]
|
|
41
42
|
const Walkers = 60
|
examples/games/orbitrunner/actors/Ship.plum
CHANGED
|
@@ -16,7 +16,10 @@ import std/game/Sprite
|
|
|
16
16
|
import std/game/Round
|
|
17
17
|
import OrbitRunner
|
|
18
18
|
|
|
19
|
+
struct Ship[Actor, Round, Steps](
|
|
20
|
+
tex: Texture, body: Body,
|
|
19
|
-
|
|
21
|
+
hurtUntil: Float = 0.0, clock: Float = 0.0
|
|
22
|
+
) =
|
|
20
23
|
const Radius = 10.0
|
|
21
24
|
const Thrust = 900.0
|
|
22
25
|
const Drag = 2.2
|
examples/games/orbitrunner/screens/PlayingScreen.plum
CHANGED
|
@@ -38,8 +38,10 @@ enum PlayAction[Show] =
|
|
|
38
38
|
| StartOver
|
|
39
39
|
|
|
40
40
|
struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
41
|
+
dice: Dice, ticks: Int, assets: Assets,
|
|
41
|
-
|
|
42
|
+
score: Int, best: Int, lives: Int, over: Bool, clock: Float,
|
|
42
|
-
ship: Ship, orbs: List<Orb>, mines: List<Mine>, rings: List<Ring>
|
|
43
|
+
ship: Ship, orbs: List<Orb>, mines: List<Mine>, rings: List<Ring>
|
|
44
|
+
) =
|
|
43
45
|
const OrbCount = 3
|
|
44
46
|
const StartLives = 3
|
|
45
47
|
const DealRoll = 0
|
std/Decimal.plum
CHANGED
|
@@ -13,7 +13,11 @@ import std/math/Math
|
|
|
13
13
|
|
|
14
14
|
# What `Decimal.parse` reads off the text as it goes: the significant digits (no point),
|
|
15
15
|
# the power of ten they are scaled by, the sign, and whether the text is a number at all.
|
|
16
|
+
struct DecimalScan(
|
|
16
|
-
|
|
17
|
+
mut digits: Buffer, mut exp: Int,
|
|
18
|
+
mut neg: Bool, mut ok: Bool,
|
|
19
|
+
mut seen: Int, mut pos: Int
|
|
20
|
+
)
|
|
17
21
|
|
|
18
22
|
# A Decimal is an exact decimal number: `digits * 10^exp`, negated when `neg`. `digits` is
|
|
19
23
|
# a string of decimal digits with no leading zero (`"0"` for zero, which keeps its sign),
|
std/collections/Bvh.plum
CHANGED
|
@@ -82,7 +82,11 @@ struct Bvh<V>(root: BvhNode<V>, size: Int) =
|
|
|
82
82
|
|
|
83
83
|
# One quickselect partition of a list by centre key: the items below, equal to and
|
|
84
84
|
# above the pivot, with the first two counted.
|
|
85
|
+
struct BvhParts<V>(
|
|
85
|
-
|
|
86
|
+
lt: List<Pair<Aabb, V>>, nlt: Int,
|
|
87
|
+
eq: List<Pair<Aabb, V>>, neq: Int,
|
|
88
|
+
gt: List<Pair<Aabb, V>>
|
|
89
|
+
)
|
|
86
90
|
|
|
87
91
|
# A node of a `Bvh`: a leaf holds one box and its value, a branch holds the union
|
|
88
92
|
# of its two children's boxes.
|
std/collections/IntervalTree.plum
CHANGED
|
@@ -111,7 +111,11 @@ struct IvEntry<V>(lo: Int, hi: Int, value: V) =
|
|
|
111
111
|
# on either side.
|
|
112
112
|
enum IvBranch<V> =
|
|
113
113
|
| IvLeaf
|
|
114
|
+
| IvNode(
|
|
115
|
+
lo: Int, hi: Int, value: V,
|
|
116
|
+
maxHi: Int, height: Int,
|
|
114
|
-
|
|
117
|
+
left: IvBranch<V>, right: IvBranch<V>
|
|
118
|
+
)
|
|
115
119
|
|
|
116
120
|
fun isEmpty(self) -> Bool = match self
|
|
117
121
|
IvLeaf => true
|
std/game/Blend.plum
CHANGED
|
@@ -25,7 +25,11 @@ import std/game/Touch
|
|
|
25
25
|
# in: a `Blend` under another `Blend` returns to the outer one's mode (std/game/View's
|
|
26
26
|
# `paint`). Like a `Group` it is never hit itself (`touch` is `ChildrenOnly` unless given;
|
|
27
27
|
# `Enabled` is the same, as it has no bounds) and has no bounds.
|
|
28
|
+
struct Blend[View, Touch](
|
|
29
|
+
x: Float = 0.0, y: Float = 0.0,
|
|
30
|
+
mode: Int,
|
|
28
|
-
|
|
31
|
+
touch: Touchable = ChildrenOnly, children: List<View>
|
|
32
|
+
) =
|
|
29
33
|
const Normal = 0
|
|
30
34
|
const Add = 1
|
|
31
35
|
const Multiply = 2
|
std/game/Box.plum
CHANGED
|
@@ -17,7 +17,11 @@ import std/game/Touch
|
|
|
17
17
|
# A filled `w` by `h` box in `color` (painted at its alpha), its top-left at (x, y), where
|
|
18
18
|
# its place starts. It is decoration unless `touch: Enabled` (std/game/Touch), and then
|
|
19
19
|
# hit inside it.
|
|
20
|
+
struct Box[View, Sized, Touch](
|
|
21
|
+
x: Float = 0.0, y: Float = 0.0,
|
|
22
|
+
w: Float, h: Float,
|
|
20
|
-
|
|
23
|
+
color: Color, touch: Touchable = Disabled
|
|
24
|
+
) =
|
|
21
25
|
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
22
26
|
|
|
23
27
|
fun bounds(self) -> Aabb = Aabb.of(0.0, 0.0, self.w, self.h)
|
std/game/Button.plum
CHANGED
|
@@ -30,7 +30,11 @@ trait Presses<A> =
|
|
|
30
30
|
# It is hit inside its box, edges included, unless a child under the press takes it
|
|
31
31
|
# (a child is decoration unless it takes presses itself). It is an `Actor` whose step
|
|
32
32
|
# steps the actors among its children (`Views.stepIn`), as a `Group`'s are stepped.
|
|
33
|
-
struct Button<A>[Actor, Sized, Touch, Presses<A>](
|
|
33
|
+
struct Button<A>[Actor, Sized, Touch, Presses<A>](
|
|
34
|
+
x: Float = 0.0, y: Float = 0.0,
|
|
35
|
+
w: Float, h: Float,
|
|
36
|
+
on: A, children: List<View> = Nil
|
|
37
|
+
) =
|
|
34
38
|
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
35
39
|
|
|
36
40
|
fun step(self, dt: Float) -> Button<A> = Button(..self, children: Views.stepIn(self.children, dt))
|
std/game/Circle.plum
CHANGED
|
@@ -21,7 +21,10 @@ import std/game/Touch
|
|
|
21
21
|
# (its bounds are -r to r in its own coordinates), painted at its colour's alpha. It is
|
|
22
22
|
# decoration unless `touch: Enabled` (std/game/Touch), and then hit inside the circle, not
|
|
23
23
|
# its whole box.
|
|
24
|
+
struct Circle[View, Sized, Touch](
|
|
25
|
+
x: Float = 0.0, y: Float = 0.0,
|
|
24
|
-
|
|
26
|
+
r: Float, color: Color, touch: Touchable = Disabled
|
|
27
|
+
) =
|
|
25
28
|
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
26
29
|
|
|
27
30
|
fun bounds(self) -> Aabb = Aabb.of(0.0 - self.r, 0.0 - self.r, 2.0 * self.r, 2.0 * self.r)
|
std/game/Group.plum
CHANGED
|
@@ -21,7 +21,10 @@ import std/game/Touch
|
|
|
21
21
|
# decoration the pointer should pass through). `Enabled` is the same as `ChildrenOnly`: a
|
|
22
22
|
# group has no bounds to be hit in. A panel that takes the presses on it, so that none
|
|
23
23
|
# reaches what is under it, is an `Overlay` (std/game/Overlay), which has a size.
|
|
24
|
+
struct Group[View, Touch](
|
|
24
|
-
|
|
25
|
+
x: Float = 0.0, y: Float = 0.0, touch: Touchable = ChildrenOnly,
|
|
26
|
+
children: List<View>
|
|
27
|
+
) =
|
|
25
28
|
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
26
29
|
|
|
27
30
|
fun view(self) -> List<View> = self.children
|
std/game/History.plum
CHANGED
|
@@ -39,13 +39,20 @@ import std/game/Screen
|
|
|
39
39
|
#
|
|
40
40
|
# `cap` bounds it: once the timeline would hold `cap` ticks the history is full, lets its
|
|
41
41
|
# ticks go, and every rewind, undo and step is refused from then on (the game plays on).
|
|
42
|
+
struct HistoryTick(
|
|
42
|
-
|
|
43
|
+
input: Input, pixels: Vec2, dt: Float, user: Bool,
|
|
44
|
+
apply: Option<Dispatch> = None
|
|
45
|
+
)
|
|
43
46
|
|
|
44
47
|
# An action dispatched outside a tick, kept in a history: the delivery it gives of the
|
|
45
48
|
# screen shown.
|
|
46
49
|
struct Dispatch(deliver: fn(Screen) -> Delivered)
|
|
47
50
|
|
|
51
|
+
struct History(
|
|
52
|
+
cap: Int, pixels: Vec2,
|
|
48
|
-
|
|
53
|
+
ticks: List<HistoryTick>, count: Int, future: List<HistoryTick>, fork: Int,
|
|
54
|
+
full: Bool, over: Int
|
|
55
|
+
) =
|
|
49
56
|
# 30 minutes of ticks.
|
|
50
57
|
const Cap = 108000
|
|
51
58
|
|
std/game/Input.plum
CHANGED
|
@@ -221,7 +221,10 @@ enum InputEvent =
|
|
|
221
221
|
# keys that are down, the other mouse buttons that are down (`Mouse.Right`,
|
|
222
222
|
# `Mouse.Middle`), and where the pointer is, in screen pixels; nothing unless given
|
|
223
223
|
# (`Held()`, `Held(keys: keys)`). A slice's `OnHeld` (std/game/ScreenSlice) maps it.
|
|
224
|
+
struct Held(
|
|
225
|
+
mouse: Bool = false, keys: List<Int> = Nil,
|
|
224
|
-
|
|
226
|
+
buttons: List<Int> = Nil, pointer: Vec2 = Vec2(x: 0.0, y: 0.0)
|
|
227
|
+
) =
|
|
225
228
|
# Whether `o` holds the same: the mouse buttons, and the same keys in any order (where
|
|
226
229
|
# the pointer is aside).
|
|
227
230
|
fun same(self, o: Held) -> Bool =
|
|
@@ -265,7 +268,10 @@ struct Held(mouse: Bool = false, keys: List<Int> = Nil, buttons: List<Int> = Nil
|
|
|
265
268
|
# host. `screen` is the window's size in pixels (0 by 0 unless given, or `sized`): the
|
|
266
269
|
# host's when the `Ticker` reads it, the recorded one in a replay; the game takes its
|
|
267
270
|
# window from it (`Game.update`).
|
|
271
|
+
struct Input(
|
|
268
|
-
|
|
272
|
+
events: List<InputEvent> = Nil, held: Held = Held(),
|
|
273
|
+
screen: Vec2 = Vec2(x: 0.0, y: 0.0)
|
|
274
|
+
) =
|
|
269
275
|
# This input in a window `w` by `h` pixels.
|
|
270
276
|
fun sized(self, w: Float, h: Float) -> Input = Input(..self, screen: Vec2(x: w, y: h))
|
|
271
277
|
|
std/game/Label.plum
CHANGED
|
@@ -37,7 +37,11 @@ enum Align =
|
|
|
37
37
|
# again each frame, so its text is read from the state as it is made
|
|
38
38
|
# (`Label(x: 16.0, y: 12.0, text: "Score {self.score}", size: 28.0)`). Unless given, it is
|
|
39
39
|
# 20 high, white, `Left` and decoration (`Disabled`).
|
|
40
|
+
struct Label[View, Sized, Touch](
|
|
41
|
+
x: Float = 0.0, y: Float = 0.0,
|
|
40
|
-
|
|
42
|
+
text: Str, size: Float = 20.0, color: Color = Color.White,
|
|
43
|
+
align: Align = Left, touch: Touchable = Disabled
|
|
44
|
+
) =
|
|
41
45
|
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
42
46
|
|
|
43
47
|
# How far left of its place its text starts.
|
std/game/Line.plum
CHANGED
|
@@ -21,7 +21,11 @@ import std/game/Touch
|
|
|
21
21
|
# by that scale). Its bounds are the box around the segment, widened by half its width.
|
|
22
22
|
# It is decoration unless `touch: Enabled` (std/game/Touch), and then hit within half its
|
|
23
23
|
# width of the segment (at least half a unit), not its whole box.
|
|
24
|
+
struct Line[View, Sized, Touch](
|
|
25
|
+
x: Float = 0.0, y: Float = 0.0,
|
|
26
|
+
from: Vec2, to: Vec2,
|
|
24
|
-
|
|
27
|
+
width: Float = 1.0, color: Color = Color.White, touch: Touchable = Disabled
|
|
28
|
+
) =
|
|
25
29
|
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
26
30
|
|
|
27
31
|
fun bounds(self) -> Aabb =
|
std/game/Overlay.plum
CHANGED
|
@@ -34,7 +34,11 @@ import std/game/Label
|
|
|
34
34
|
# std/game/Touch) inside its box, so no item under it there is pressed; its children are
|
|
35
35
|
# hit first. Keys still go to the screen's subscriptions (`OnKeyDown`, std/game/ScreenSlice),
|
|
36
36
|
# which say what they do while it shows (Enter plays again).
|
|
37
|
+
struct Overlay[View, Sized, Touch](
|
|
38
|
+
x: Float = 0.0, y: Float = 0.0,
|
|
39
|
+
w: Float, h: Float,
|
|
37
|
-
|
|
40
|
+
color: Color = Overlay.Shade, touch: Touchable = Enabled, children: List<View>
|
|
41
|
+
) =
|
|
38
42
|
const Shade = Color(r: 0.0, g: 0.0, b: 0.0, a: 0.6)
|
|
39
43
|
|
|
40
44
|
fun place(self) -> Place = Place.at(self.x, self.y)
|
std/game/Screen.plum
CHANGED
|
@@ -130,7 +130,10 @@ trait Screen =
|
|
|
130
130
|
# the game (std/game/Cmd), in order, the actions stepped, and whether the tick was a user
|
|
131
131
|
# action (std/game/History): a subscription or a press gave an action for one of its
|
|
132
132
|
# events, or `OnHeld` gave one while what is held differs from the screen's tick before.
|
|
133
|
+
struct Delivered(
|
|
134
|
+
screen: Screen, cmds: List<Cmd> = Nil,
|
|
133
|
-
|
|
135
|
+
sent: List<Dispatched> = Nil, acted: Bool = false
|
|
136
|
+
)
|
|
134
137
|
|
|
135
138
|
# How often a screen gets its ticks and is drawn: every frame (`Always`), or only when
|
|
136
139
|
# something happened (`OnDemand`).
|
std/game/ScreenSlice.plum
CHANGED
|
@@ -188,7 +188,10 @@ struct Trail<A>(actions: List<A> = Nil, cmds: List<Cmd> = Nil, acted: Bool = fal
|
|
|
188
188
|
# was held at the screen's tick before (`before`), the camera the screen is seen through
|
|
189
189
|
# (window pixels to the world's coordinates), the window's size in pixels, and whether a
|
|
190
190
|
# transition runs.
|
|
191
|
+
struct Delivery(
|
|
192
|
+
input: Input, dt: Float, before: Held,
|
|
191
|
-
|
|
193
|
+
camera: Viewport, pixels: Vec2, shifting: Bool
|
|
194
|
+
) =
|
|
192
195
|
# The item a press at the window pixel `p`, the world point `at`, lands on: in the
|
|
193
196
|
# screen's `hud`, else in its `view`; None for none.
|
|
194
197
|
fun pressed(hud: List<View>, view: List<View>, p: Vec2, at: Vec2) -> Option<View> =
|
std/game/Sprite.plum
CHANGED
|
@@ -27,7 +27,12 @@ import std/game/Touch
|
|
|
27
27
|
# is read from the actor's own clock:
|
|
28
28
|
#
|
|
29
29
|
# Sprite(tex: sheet, w: 48.0, h: 48.0, srcX: 0.0, srcY: 0.0, srcW: 16.0, srcH: 16.0, frame: Sprite.frameAt(self.clock, 8.0, 4))
|
|
30
|
-
struct Sprite[View, Sized, Touch](
|
|
30
|
+
struct Sprite[View, Sized, Touch](
|
|
31
|
+
x: Float = 0.0, y: Float = 0.0,
|
|
32
|
+
tex: Texture, w: Float, h: Float,
|
|
33
|
+
srcX: Float, srcY: Float, srcW: Float, srcH: Float,
|
|
34
|
+
frame: Int = 0, tint: Color = Color.White, touch: Touchable = Disabled
|
|
35
|
+
) =
|
|
31
36
|
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
32
37
|
|
|
33
38
|
# The frame of a strip of `count` frames played at `fps` frames a second, `clock`
|
std/game/Viewport.plum
CHANGED
|
@@ -30,7 +30,12 @@ enum Scaling = Fit | Fill | Stretch | Extend | Pixels
|
|
|
30
30
|
#
|
|
31
31
|
# v := Viewport.of(Fit, 800.0, 600.0).update(1600.0, 600.0) # x = -400, y = 0, sx = sy = 1
|
|
32
32
|
# v.toWorld(800.0, 300.0) # (400, 300)
|
|
33
|
+
struct Viewport(
|
|
34
|
+
scaling: Scaling,
|
|
33
|
-
|
|
35
|
+
w: Float, h: Float, screenW: Float, screenH: Float,
|
|
36
|
+
x: Float, y: Float, sx: Float, sy: Float,
|
|
37
|
+
moved: Bool
|
|
38
|
+
) =
|
|
34
39
|
# The world `w` by `h` on a screen as large as itself, which every scaling shows
|
|
35
40
|
# unscaled at (0, 0).
|
|
36
41
|
fun of(scaling: Scaling, w: Float, h: Float) -> Viewport =
|
std/math/Affine.plum
CHANGED
|
@@ -6,7 +6,10 @@ import std/math/Vec2
|
|
|
6
6
|
import std/Option
|
|
7
7
|
|
|
8
8
|
# A 2D affine transform: x' = a*x + c*y + e, y' = b*x + d*y + f.
|
|
9
|
+
struct Affine(
|
|
9
|
-
|
|
10
|
+
a: Float, b: Float, c: Float,
|
|
11
|
+
d: Float, e: Float, f: Float
|
|
12
|
+
) =
|
|
10
13
|
|
|
11
14
|
const Identity = Affine(a: 1.0, b: 0.0, c: 0.0, d: 1.0, e: 0.0, f: 0.0)
|
|
12
15
|
|
tests/field_defaults.plum
CHANGED
|
@@ -14,7 +14,11 @@ enum Side =
|
|
|
14
14
|
| Start
|
|
15
15
|
| End
|
|
16
16
|
|
|
17
|
+
struct Tag(
|
|
18
|
+
x: Float, text: Str,
|
|
19
|
+
size: Float = Tag.Base, side: Side = Start,
|
|
17
|
-
|
|
20
|
+
weight: Int = Tag.heavy(), scaled: Float = Big * 2.0
|
|
21
|
+
) =
|
|
18
22
|
const Base = 20.0
|
|
19
23
|
const Big = 3.0
|
|
20
24
|
|
tests/port/game/determinism.plum
CHANGED
|
@@ -26,7 +26,11 @@ enum Saw =
|
|
|
26
26
|
|
|
27
27
|
# The one screen of a `Game` for these tests: it counts ticks, adds up and bounds `dt`,
|
|
28
28
|
# counts the ticks Space is held, and on each press of Space draws from its `Random`.
|
|
29
|
+
struct Probe[ScreenSlice<Saw>](
|
|
30
|
+
rng: Random, ticks: Int,
|
|
29
|
-
|
|
31
|
+
time: Float, minDt: Float, maxDt: Float,
|
|
32
|
+
presses: Int, held: Int, roll: Int
|
|
33
|
+
) =
|
|
30
34
|
fun seeded(seed: Int) -> Probe =
|
|
31
35
|
Probe(rng: Random.seeded(seed), ticks: 0, time: 0.0, minDt: 1.0, maxDt: 0.0, presses: 0, held: 0, roll: 0)
|
|
32
36
|
|
tests/port/game/replay.plum
CHANGED
|
@@ -29,7 +29,12 @@ enum Saw =
|
|
|
29
29
|
# The one screen of a `Game` for these tests: it counts ticks, follows the pointer, counts
|
|
30
30
|
# the ticks Space is held, and on each press of Space or click draws from its `Random`.
|
|
31
31
|
# `px` and `py` are the pointer the tick saw, followed on its tick.
|
|
32
|
+
struct Probe[ScreenSlice<Saw>](
|
|
32
|
-
|
|
33
|
+
rng: Random, ticks: Int, presses: Int,
|
|
34
|
+
held: Int, roll: Int,
|
|
35
|
+
x: Float, y: Float,
|
|
36
|
+
px: Float, py: Float
|
|
37
|
+
) =
|
|
33
38
|
const View = Vec2(x: 800.0, y: 600.0)
|
|
34
39
|
|
|
35
40
|
fun seeded(seed: Int) -> Probe =
|
tests/port/game/routing.plum
CHANGED
|
@@ -28,7 +28,12 @@ import std/Test
|
|
|
28
28
|
# boxes that overlap. A point within 1e-6 of a box's edge is skipped.
|
|
29
29
|
|
|
30
30
|
# A `w` by `h` box node with children: a view item that takes presses as `touch` says.
|
|
31
|
-
struct RNode[View, Sized, Touch](
|
|
31
|
+
struct RNode[View, Sized, Touch](
|
|
32
|
+
id: Int, at: Place,
|
|
33
|
+
w: Float, h: Float,
|
|
34
|
+
touch: Touchable, shows: Bool,
|
|
35
|
+
children: List<View>
|
|
36
|
+
) =
|
|
32
37
|
fun place(self) -> Place = self.at
|
|
33
38
|
|
|
34
39
|
fun visible(self) -> Bool = self.shows
|
|
@@ -41,7 +46,11 @@ struct RNode[View, Sized, Touch](id: Int, at: Place, w: Float, h: Float, touch:
|
|
|
41
46
|
|
|
42
47
|
# What was built, one entry per node in draw order (a node's id is its index): the
|
|
43
48
|
# reference reads only this.
|
|
49
|
+
struct Spec(
|
|
50
|
+
parent: Int, at: Place,
|
|
51
|
+
w: Float, h: Float,
|
|
44
|
-
|
|
52
|
+
touch: Touchable, visible: Bool
|
|
53
|
+
)
|
|
45
54
|
|
|
46
55
|
# A small generator the tests can read: the same seed gives the same views everywhere.
|
|
47
56
|
struct Gen(mut s: Int) =
|
tests/port/game/screens.plum
CHANGED
|
@@ -177,7 +177,10 @@ enum WalkAct = Saw(x: Float) | Walked(dt: Float) | TakeBack | Pause | Follow | H
|
|
|
177
177
|
# A screen of one walker: `OnHeld` notes where it is before the step and the frame action
|
|
178
178
|
# after it; P pauses the stepping, F has the camera follow the walker, two times as
|
|
179
179
|
# close; its hud is a button at the window's bottom-right corner; U asks for an undo.
|
|
180
|
-
struct WalkScreen[ScreenSlice<WalkAct>](
|
|
180
|
+
struct WalkScreen[ScreenSlice<WalkAct>](
|
|
181
|
+
walker: Walker, seen: List<Float>,
|
|
182
|
+
paused: Bool, follow: Bool, hudPresses: Int
|
|
183
|
+
) =
|
|
181
184
|
fun create() -> WalkScreen = WalkScreen(walker: Walker(x: 0.0, speed: 60.0), seen: Nil, paused: false, follow: false, hudPresses: 0)
|
|
182
185
|
|
|
183
186
|
fun size(self) -> Vec2 = Vec2(x: 800.0, y: 600.0)
|
tests/std/collections/SegmentTree.plum
CHANGED
|
@@ -216,7 +216,10 @@ primitive SegmentTreeTest[UnitTest] =
|
|
|
216
216
|
assert z.query(0, m) == 0
|
|
217
217
|
|
|
218
218
|
# The random test's state: the generator and the four trees under test.
|
|
219
|
+
struct SegRun(
|
|
220
|
+
mut x: Int, mut sum: SegmentTree<Int>,
|
|
219
|
-
|
|
221
|
+
mut mn: SegmentTree<Int>, mut mx: SegmentTree<Int>, mut cat: SegmentTree<Str>
|
|
222
|
+
)
|
|
220
223
|
|
|
221
224
|
# The brute-force answers over `brute[lo, hi)`: sum, min, max and concatenation.
|
|
222
225
|
struct SegWant(sum: Int, mn: Int, mx: Int, cat: Str)
|
tests/std/game/View.plum
CHANGED
|
@@ -256,7 +256,13 @@ primitive ViewHelpers =
|
|
|
256
256
|
fun probeAt(x: Float, log: Slice<Float>) -> View = ViewProbe(x: x, log: log, fade: 1.0)
|
|
257
257
|
|
|
258
258
|
# A screen whose actors are stepped for it, of each kind a field can hold them in.
|
|
259
|
+
struct Stage[Screen](
|
|
260
|
+
n: Int,
|
|
261
|
+
one: Stepper, many: List<Stepper>,
|
|
259
|
-
|
|
262
|
+
maybe: Option<Stepper>, nothing: Option<Stepper>,
|
|
263
|
+
anyOne: Actor, anyMany: List<Actor>,
|
|
264
|
+
loose: View, scene: List<View>
|
|
265
|
+
)
|
|
260
266
|
|
|
261
267
|
# A screen that steps its actor twice as fast itself.
|
|
262
268
|
struct Owned[Screen](one: Stepper) =
|