phi-engine 0.0.0-stage → 0.4.0

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 (79) hide show
  1. package/README.md +60 -2
  2. package/audio/Audio.ts +3 -0
  3. package/audio/AudioService.ts +15 -0
  4. package/audio/SoundName.ts +1 -0
  5. package/audio/index.ts +3 -0
  6. package/game/Game.ts +3 -0
  7. package/game/GameService.ts +57 -0
  8. package/game/PauseReason.ts +6 -0
  9. package/game/Time.ts +3 -0
  10. package/game/TimeService.ts +17 -0
  11. package/game/index.ts +5 -0
  12. package/input/Input.ts +3 -0
  13. package/input/InputDevice.ts +2 -0
  14. package/input/InputService.ts +71 -0
  15. package/input/index.ts +3 -0
  16. package/math/Color.ts +46 -0
  17. package/math/Vector2.ts +59 -0
  18. package/math/Vector3.ts +63 -0
  19. package/math/index.ts +3 -0
  20. package/package.json +23 -4
  21. package/render/Camera.ts +3 -0
  22. package/render/CameraService.ts +19 -0
  23. package/render/PostProcess.ts +3 -0
  24. package/render/PostProcessService.ts +14 -0
  25. package/render/index.ts +4 -0
  26. package/runtime/EngineOutputBus.ts +17 -0
  27. package/runtime/EntityValues.ts +103 -0
  28. package/runtime/PhiRuntime.ts +517 -0
  29. package/runtime/UiBridge.ts +178 -0
  30. package/runtime/UiCommand.ts +14 -0
  31. package/runtime/UiEventWire.ts +6 -0
  32. package/runtime/ViewportWire.ts +6 -0
  33. package/runtime/interfaceBridge.ts +3 -0
  34. package/runtime/protocol.ts +5 -0
  35. package/runtime/runtime.ts +9 -0
  36. package/scene/Component.ts +108 -0
  37. package/scene/Entity.ts +118 -0
  38. package/scene/Mesh3D.ts +37 -0
  39. package/scene/PointLight.ts +15 -0
  40. package/scene/Scene.ts +3 -0
  41. package/scene/SceneAccessor.ts +45 -0
  42. package/scene/SceneService.ts +21 -0
  43. package/scene/Sprite2D.ts +20 -0
  44. package/scene/Transform2D.ts +43 -0
  45. package/scene/Transform3D.ts +56 -0
  46. package/scene/index.ts +16 -0
  47. package/scene/registerComponents.ts +6 -0
  48. package/ui/Align.ts +2 -0
  49. package/ui/Anchor.ts +11 -0
  50. package/ui/Bar.ts +100 -0
  51. package/ui/Button.ts +93 -0
  52. package/ui/Choice.ts +80 -0
  53. package/ui/Column.ts +10 -0
  54. package/ui/ComponentUi.ts +70 -0
  55. package/ui/Container.ts +39 -0
  56. package/ui/ContainerOptions.ts +18 -0
  57. package/ui/Control.ts +94 -0
  58. package/ui/Element.ts +64 -0
  59. package/ui/ElementKind.ts +12 -0
  60. package/ui/Group.ts +110 -0
  61. package/ui/Icon.ts +63 -0
  62. package/ui/IconShape.ts +1 -0
  63. package/ui/Layer.ts +105 -0
  64. package/ui/Row.ts +10 -0
  65. package/ui/Settings.ts +3 -0
  66. package/ui/SettingsService.ts +48 -0
  67. package/ui/Slider.ts +158 -0
  68. package/ui/Spacer.ts +46 -0
  69. package/ui/Text.ts +62 -0
  70. package/ui/TextSize.ts +5 -0
  71. package/ui/Theme.ts +3 -0
  72. package/ui/ThemeValues.ts +58 -0
  73. package/ui/Toggle.ts +46 -0
  74. package/ui/Viewport.ts +11 -0
  75. package/ui/WireValue.ts +5 -0
  76. package/ui/finiteColor.ts +11 -0
  77. package/ui/finiteNumber.ts +10 -0
  78. package/ui/index.ts +26 -0
  79. package/ui/wholeNumber.ts +12 -0
package/ui/Layer.ts ADDED
@@ -0,0 +1,105 @@
1
+ import { Container } from './Container';
2
+ import { finiteNumber } from './finiteNumber';
3
+ import { wholeNumber } from './wholeNumber';
4
+ import type { Anchor } from './Anchor';
5
+ import type { Element } from './Element';
6
+ import type { WireValue } from './WireValue';
7
+
8
+ export interface LayerOptions {
9
+ /** The point of the screen where the layer stays. The default is `top-left`. */
10
+ anchor?: Anchor;
11
+ /** A move from the anchor in UI units. Layers also stay 32 UI units from the screen edges. */
12
+ offset?: [number, number];
13
+ /** Layers with a higher order draw over layers with a lower order. The default is 0. */
14
+ order?: number;
15
+ /**
16
+ * A visible modal layer keeps the focus, and hides the game actions from the scripts until
17
+ * the player releases them. The default is false.
18
+ */
19
+ modal?: boolean;
20
+ /** The default is true. */
21
+ visible?: boolean;
22
+ }
23
+
24
+ /** The root of a tree of elements. Add it to the screen with `this.ui.add()` in a component. */
25
+ export class Layer extends Container {
26
+ private anchorValue: Anchor;
27
+
28
+ private offsetValue: [number, number];
29
+
30
+ private orderValue: number;
31
+
32
+ private modalValue: boolean;
33
+
34
+ private visibleValue: boolean;
35
+
36
+ constructor(options: LayerOptions = {}, children: readonly Element[] = []) {
37
+ super('layer', children);
38
+ this.anchorValue = options.anchor ?? 'top-left';
39
+ this.offsetValue = Layer.finiteOffset(options.offset ?? [0, 0]);
40
+ this.orderValue = wholeNumber('Layer.order', options.order ?? 0);
41
+ this.modalValue = options.modal ?? false;
42
+ this.visibleValue = options.visible ?? true;
43
+ }
44
+
45
+ get anchor(): Anchor {
46
+ return this.anchorValue;
47
+ }
48
+
49
+ set anchor(value: Anchor) {
50
+ this.anchorValue = value;
51
+ this.changed();
52
+ }
53
+
54
+ /** A copy of the offset. Set a new offset to change it. */
55
+ get offset(): [number, number] {
56
+ return [...this.offsetValue];
57
+ }
58
+
59
+ set offset(value: [number, number]) {
60
+ this.offsetValue = Layer.finiteOffset(value);
61
+ this.changed();
62
+ }
63
+
64
+ get order(): number {
65
+ return this.orderValue;
66
+ }
67
+
68
+ set order(value: number) {
69
+ this.orderValue = wholeNumber('Layer.order', value);
70
+ this.changed();
71
+ }
72
+
73
+ get modal(): boolean {
74
+ return this.modalValue;
75
+ }
76
+
77
+ set modal(value: boolean) {
78
+ this.modalValue = value;
79
+ this.changed();
80
+ }
81
+
82
+ get visible(): boolean {
83
+ return this.visibleValue;
84
+ }
85
+
86
+ set visible(value: boolean) {
87
+ this.visibleValue = value;
88
+ this.changed();
89
+ }
90
+
91
+ private static finiteOffset([x, y]: [number, number]): [number, number] {
92
+ return [finiteNumber('Layer.offset x', x), finiteNumber('Layer.offset y', y)];
93
+ }
94
+
95
+ /** @internal */
96
+ wireProps(): Record<string, WireValue> {
97
+ return {
98
+ anchor: this.anchorValue,
99
+ offset: this.offsetValue,
100
+ order: this.orderValue,
101
+ modal: this.modalValue,
102
+ visible: this.visibleValue,
103
+ };
104
+ }
105
+ }
package/ui/Row.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { Group } from './Group';
2
+ import type { ContainerOptions } from './ContainerOptions';
3
+ import type { Element } from './Element';
4
+
5
+ /** Puts its children from left to right. */
6
+ export class Row extends Group {
7
+ constructor(options: ContainerOptions = {}, children: readonly Element[] = []) {
8
+ super('row', options, children);
9
+ }
10
+ }
package/ui/Settings.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { SettingsService } from './SettingsService';
2
+
3
+ export const Settings = new SettingsService();
@@ -0,0 +1,48 @@
1
+ import type { EngineOutputBus } from '../runtime/EngineOutputBus';
2
+
3
+ /** The smallest text scale that the player can select. Rust makes the same check. */
4
+ const MIN_TEXT_SCALE = 1;
5
+ /** The largest text scale that the player can select. Rust makes the same check. */
6
+ const MAX_TEXT_SCALE = 2;
7
+
8
+ /**
9
+ * The settings of the player that Rust keeps, so that they stay when the scripts reload or
10
+ * the scene changes.
11
+ */
12
+ export class SettingsService {
13
+ private bus: EngineOutputBus | null = null;
14
+
15
+ private textScaleValue = 1;
16
+
17
+ /** The scale that a script set in this step, until Rust gives it back in the next step. */
18
+ private pendingTextScale: number | null = null;
19
+
20
+ /** @internal */
21
+ attach(bus: EngineOutputBus): void {
22
+ this.bus = bus;
23
+ }
24
+
25
+ /** @internal */
26
+ sync(textScale: number): void {
27
+ this.textScaleValue = textScale;
28
+ this.pendingTextScale = null;
29
+ }
30
+
31
+ /**
32
+ * The text size that the player selects, from 1 to 2. All text in the interface grows by
33
+ * this scale. A menu, such as a `Choice` of 100%, 150% and 200%, sets it.
34
+ */
35
+ get textScale(): number {
36
+ return this.pendingTextScale ?? this.textScaleValue;
37
+ }
38
+
39
+ set textScale(value: number) {
40
+ if (!Number.isFinite(value) || value < MIN_TEXT_SCALE || value > MAX_TEXT_SCALE) {
41
+ throw new Error(
42
+ `Settings.textScale must be from ${MIN_TEXT_SCALE} to ${MAX_TEXT_SCALE}, not ${value}.`,
43
+ );
44
+ }
45
+ this.pendingTextScale = value;
46
+ this.bus?.setTextScale(value);
47
+ }
48
+ }
package/ui/Slider.ts ADDED
@@ -0,0 +1,158 @@
1
+ import { Control, type ControlOptions } from './Control';
2
+ import { Theme } from './Theme';
3
+ import { finiteColor } from './finiteColor';
4
+ import { finiteNumber } from './finiteNumber';
5
+ import type { Color } from '../math/Color';
6
+ import type { WireValue } from './WireValue';
7
+
8
+ export interface SliderOptions extends ControlOptions {
9
+ /** The default is 0. */
10
+ min?: number;
11
+ /** The default is 1. */
12
+ max?: number;
13
+ /** The change of one press of left or right. The default is 0.1. */
14
+ step?: number;
15
+ /** From `min` to `max`. The default is `min`. */
16
+ value?: number;
17
+ /** The width of the track in UI units. The default is `Theme.sliderWidth`. */
18
+ width?: number;
19
+ /** The colour of the empty part of the track. The default is `Theme.sliderTrack`. */
20
+ track?: Color;
21
+ }
22
+
23
+ interface Range {
24
+ min: number;
25
+ max: number;
26
+ step: number;
27
+ value: number;
28
+ }
29
+
30
+ /** Gives the range if it makes a slider. Rust makes the same checks. */
31
+ function checkedRange(range: Range): Range {
32
+ const { min, max, step, value } = range;
33
+ (['min', 'max', 'step', 'value'] as const).forEach((name) => {
34
+ finiteNumber(`Slider.${name}`, range[name]);
35
+ });
36
+ if (min >= max) {
37
+ throw new Error(`Slider.min must be less than Slider.max, not ${min} and ${max}.`);
38
+ }
39
+ if (step <= 0) {
40
+ throw new Error(`Slider.step must be more than 0, not ${step}.`);
41
+ }
42
+ if (value < min || value > max) {
43
+ throw new Error(`Slider.value must be from ${min} to ${max}, not ${value}.`);
44
+ }
45
+ return range;
46
+ }
47
+
48
+ /**
49
+ * A slider with a label, such as a volume from 0 to 100. The thumb on the track shows the
50
+ * value. While it has the focus, left and right change the value by one step, and up and down
51
+ * move the focus. A click or a drag on the track sets the value at the nearest step.
52
+ */
53
+ export class Slider extends Control {
54
+ /** Phi calls it with the new value when the player changes the slider. */
55
+ onChange: (value: number) => void;
56
+
57
+ private range: Range;
58
+
59
+ private widthValue: number;
60
+
61
+ private trackValue: Color;
62
+
63
+ constructor(label: string, onChange: (value: number) => void, options: SliderOptions = {}) {
64
+ super('slider', 'Slider', label, options);
65
+ this.onChange = onChange;
66
+ const min = options.min ?? 0;
67
+ this.range = checkedRange({
68
+ min,
69
+ max: options.max ?? 1,
70
+ step: options.step ?? 0.1,
71
+ value: options.value ?? min,
72
+ });
73
+ this.widthValue = finiteNumber('Slider.width', options.width ?? Theme.sliderWidth);
74
+ this.trackValue = finiteColor('Slider.track', options.track ?? Theme.sliderTrack);
75
+ }
76
+
77
+ get min(): number {
78
+ return this.range.min;
79
+ }
80
+
81
+ /** The value must stay from `min` to `max`, so change the value first when it would not. */
82
+ set min(min: number) {
83
+ this.setRange({ ...this.range, min });
84
+ }
85
+
86
+ get max(): number {
87
+ return this.range.max;
88
+ }
89
+
90
+ /** The value must stay from `min` to `max`, so change the value first when it would not. */
91
+ set max(max: number) {
92
+ this.setRange({ ...this.range, max });
93
+ }
94
+
95
+ get step(): number {
96
+ return this.range.step;
97
+ }
98
+
99
+ set step(step: number) {
100
+ this.setRange({ ...this.range, step });
101
+ }
102
+
103
+ /** From `min` to `max`. */
104
+ get value(): number {
105
+ return this.range.value;
106
+ }
107
+
108
+ set value(value: number) {
109
+ this.setRange({ ...this.range, value });
110
+ }
111
+
112
+ /** The width of the track in UI units. */
113
+ get width(): number {
114
+ return this.widthValue;
115
+ }
116
+
117
+ set width(value: number) {
118
+ this.widthValue = finiteNumber('Slider.width', value);
119
+ this.changed();
120
+ }
121
+
122
+ /** A copy of the colour of the empty part of the track. Set a new colour to change it. */
123
+ get track(): Color {
124
+ return this.trackValue.clone();
125
+ }
126
+
127
+ set track(value: Color) {
128
+ this.trackValue = finiteColor('Slider.track', value);
129
+ this.changed();
130
+ }
131
+
132
+ /** @internal */
133
+ change(value: boolean | number): void {
134
+ // Rust made the event from the range of the last frame, and a script can change the
135
+ // range before the event arrives.
136
+ if (typeof value !== 'number' || value < this.range.min || value > this.range.max) return;
137
+ this.value = value;
138
+ this.onChange(value);
139
+ }
140
+
141
+ private setRange(range: Range): void {
142
+ this.range = checkedRange(range);
143
+ this.changed();
144
+ }
145
+
146
+ /** @internal */
147
+ wireProps(): Record<string, WireValue> {
148
+ return {
149
+ min: this.range.min,
150
+ max: this.range.max,
151
+ step: this.range.step,
152
+ value: this.range.value,
153
+ track_width: this.widthValue,
154
+ track: this.trackValue.toArray(),
155
+ ...this.lookProps(),
156
+ };
157
+ }
158
+ }
package/ui/Spacer.ts ADDED
@@ -0,0 +1,46 @@
1
+ import { Element } from './Element';
2
+ import { finiteNumber } from './finiteNumber';
3
+ import type { WireValue } from './WireValue';
4
+
5
+ export interface SpacerOptions {
6
+ /** The length along the direction of the parent, in UI units. The default is 0. */
7
+ size?: number;
8
+ /** Also takes the free space of the parent. The default is false. */
9
+ grow?: boolean;
10
+ }
11
+
12
+ /** Empty space in a row or a column. */
13
+ export class Spacer extends Element {
14
+ private sizeValue: number;
15
+
16
+ private growValue: boolean;
17
+
18
+ constructor(options: SpacerOptions = {}) {
19
+ super('spacer');
20
+ this.sizeValue = finiteNumber('Spacer.size', options.size ?? 0);
21
+ this.growValue = options.grow ?? false;
22
+ }
23
+
24
+ get size(): number {
25
+ return this.sizeValue;
26
+ }
27
+
28
+ set size(value: number) {
29
+ this.sizeValue = finiteNumber('Spacer.size', value);
30
+ this.changed();
31
+ }
32
+
33
+ get grow(): boolean {
34
+ return this.growValue;
35
+ }
36
+
37
+ set grow(value: boolean) {
38
+ this.growValue = value;
39
+ this.changed();
40
+ }
41
+
42
+ /** @internal */
43
+ wireProps(): Record<string, WireValue> {
44
+ return { size: this.sizeValue, grow: this.growValue };
45
+ }
46
+ }
package/ui/Text.ts ADDED
@@ -0,0 +1,62 @@
1
+ import { Element } from './Element';
2
+ import { finiteColor } from './finiteColor';
3
+ import { Theme } from './Theme';
4
+ import type { Color } from '../math/Color';
5
+ import type { TextSize } from './TextSize';
6
+ import type { WireValue } from './WireValue';
7
+
8
+ export interface TextOptions {
9
+ /** The default is `body`. */
10
+ size?: TextSize;
11
+ /** The default is `Theme.text`. */
12
+ color?: Color;
13
+ }
14
+
15
+ /** Text, such as a label or the night number. `\n` starts a new line. */
16
+ export class Text extends Element {
17
+ private textValue: string;
18
+
19
+ private sizeValue: TextSize;
20
+
21
+ private colorValue: Color;
22
+
23
+ constructor(text: string, options: TextOptions = {}) {
24
+ super('text');
25
+ this.textValue = text;
26
+ this.sizeValue = options.size ?? 'body';
27
+ this.colorValue = finiteColor('Text.color', options.color ?? Theme.text);
28
+ }
29
+
30
+ get text(): string {
31
+ return this.textValue;
32
+ }
33
+
34
+ set text(value: string) {
35
+ this.textValue = value;
36
+ this.changed();
37
+ }
38
+
39
+ get size(): TextSize {
40
+ return this.sizeValue;
41
+ }
42
+
43
+ set size(value: TextSize) {
44
+ this.sizeValue = value;
45
+ this.changed();
46
+ }
47
+
48
+ /** A copy of the colour. Set a new colour to change it. */
49
+ get color(): Color {
50
+ return this.colorValue.clone();
51
+ }
52
+
53
+ set color(value: Color) {
54
+ this.colorValue = finiteColor('Text.color', value);
55
+ this.changed();
56
+ }
57
+
58
+ /** @internal */
59
+ wireProps(): Record<string, WireValue> {
60
+ return { text: this.textValue, size: this.sizeValue, color: this.colorValue.toArray() };
61
+ }
62
+ }
package/ui/TextSize.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The text sizes. `body` has a letter height of 18 UI units, `heading` 1.35 times that and
3
+ * `title` 1.8 times. There are no smaller sizes, so all text stays readable.
4
+ */
5
+ export type TextSize = 'body' | 'heading' | 'title';
package/ui/Theme.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { ThemeValues } from './ThemeValues';
2
+
3
+ export const Theme = new ThemeValues();
@@ -0,0 +1,58 @@
1
+ import { Color } from '../math/Color';
2
+
3
+ /** An opaque sRGB colour from a CSS hex value, such as `0x15191b`. */
4
+ function hex(rgb: number): Color {
5
+ return new Color(
6
+ Math.floor(rgb / 0x10000) / 255,
7
+ (Math.floor(rgb / 0x100) % 0x100) / 255,
8
+ (rgb % 0x100) / 255,
9
+ );
10
+ }
11
+
12
+ /**
13
+ * The default colours and sizes of new elements. Change them at the start of the game, before
14
+ * a component makes its elements. Colours are sRGB, as in CSS. Sizes are in UI units.
15
+ */
16
+ export class ThemeValues {
17
+ /** The colour of text. It has a contrast of 15.3:1 on `panel`. */
18
+ text = hex(0xf2eee6);
19
+
20
+ /** A background colour for a row or a column, such as a HUD panel. */
21
+ panel = hex(0x15191b);
22
+
23
+ /** The filled part of a bar. It has a contrast of 4.8:1 on `barTrack`. */
24
+ barFill = hex(0xd98a4e);
25
+
26
+ /** The empty part of a bar. */
27
+ barTrack = hex(0x2b3134);
28
+
29
+ /**
30
+ * The background of a button, a toggle, a slider and a choice. `text` has a contrast of
31
+ * 11.4:1 on it.
32
+ */
33
+ buttonBackground = hex(0x2b3134);
34
+
35
+ /** The empty part of the track of a slider. `text` has a contrast of 15.3:1 on it. */
36
+ sliderTrack = hex(0x15191b);
37
+
38
+ /** The label of a disabled button. It has a contrast of 5:1 on `buttonBackground`. */
39
+ disabledText = hex(0x9aa29d);
40
+
41
+ /** The space between the edge of a button or another control and its label. */
42
+ buttonPadding = 12;
43
+
44
+ /** The space between the children of a row or a column. */
45
+ gap = 8;
46
+
47
+ /** The corner radius of a row or a column with a background. */
48
+ cornerRadius = 8;
49
+
50
+ barWidth = 240;
51
+
52
+ barHeight = 16;
53
+
54
+ /** The width of the track of a slider. */
55
+ sliderWidth = 240;
56
+
57
+ iconSize = 24;
58
+ }
package/ui/Toggle.ts ADDED
@@ -0,0 +1,46 @@
1
+ import { Control, type ControlOptions } from './Control';
2
+ import type { WireValue } from './WireValue';
3
+
4
+ export interface ToggleOptions extends ControlOptions {
5
+ /** The default is false. */
6
+ value?: boolean;
7
+ }
8
+
9
+ /**
10
+ * A toggle with a label, such as Sound on or off. A check mark in its box shows that it is
11
+ * on. The player changes it with accept (Enter, Space or South) while it has the focus, or
12
+ * with a click.
13
+ */
14
+ export class Toggle extends Control {
15
+ /** Phi calls it with the new value when the player changes the toggle. */
16
+ onChange: (value: boolean) => void;
17
+
18
+ private isOn: boolean;
19
+
20
+ constructor(label: string, onChange: (value: boolean) => void, options: ToggleOptions = {}) {
21
+ super('toggle', 'Toggle', label, options);
22
+ this.onChange = onChange;
23
+ this.isOn = options.value ?? false;
24
+ }
25
+
26
+ get value(): boolean {
27
+ return this.isOn;
28
+ }
29
+
30
+ set value(value: boolean) {
31
+ this.isOn = value;
32
+ this.changed();
33
+ }
34
+
35
+ /** @internal */
36
+ change(value: boolean | number): void {
37
+ if (typeof value !== 'boolean') return;
38
+ this.value = value;
39
+ this.onChange(value);
40
+ }
41
+
42
+ /** @internal */
43
+ wireProps(): Record<string, WireValue> {
44
+ return { value: this.isOn, ...this.lookProps() };
45
+ }
46
+ }
package/ui/Viewport.ts ADDED
@@ -0,0 +1,11 @@
1
+ /** The window of the game. A UI unit is one pixel on a 1920 by 1080 window. */
2
+ export interface Viewport {
3
+ /** The width in UI units. */
4
+ readonly width: number;
5
+ /** The height in UI units. */
6
+ readonly height: number;
7
+ /** Physical pixels for each UI unit. */
8
+ readonly scale: number;
9
+ /** The text size that the player selects, from 1 to 2. */
10
+ readonly textScale: number;
11
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * A property value as Rust gets it. A colour is `[r, g, b, a]`, an offset is `[x, y]` and the
3
+ * options of a choice are a list of strings.
4
+ */
5
+ export type WireValue = number | string | boolean | null | readonly number[] | readonly string[];
@@ -0,0 +1,11 @@
1
+ import { finiteNumber } from './finiteNumber';
2
+ import type { Color } from '../math/Color';
3
+
4
+ /** Gives a copy of the colour if each channel is finite. */
5
+ export function finiteColor(property: string, color: Color): Color {
6
+ finiteNumber(`${property}.r`, color.r);
7
+ finiteNumber(`${property}.g`, color.g);
8
+ finiteNumber(`${property}.b`, color.b);
9
+ finiteNumber(`${property}.a`, color.a);
10
+ return color.clone();
11
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Gives the value if it is finite. Rust cannot read NaN or Infinity, so an error here points
3
+ * to the script line that set the value.
4
+ */
5
+ export function finiteNumber(property: string, value: number): number {
6
+ if (!Number.isFinite(value)) {
7
+ throw new Error(`${property} must be a finite number, not ${value}.`);
8
+ }
9
+ return value;
10
+ }
package/ui/index.ts ADDED
@@ -0,0 +1,26 @@
1
+ export type { Align } from './Align';
2
+ export type { Anchor } from './Anchor';
3
+ export { Bar, type BarOptions } from './Bar';
4
+ export { Button, type ButtonOptions } from './Button';
5
+ export { Choice, type ChoiceOptions } from './Choice';
6
+ export { Column } from './Column';
7
+ export type { ComponentUi } from './ComponentUi';
8
+ export type { Control, ControlOptions } from './Control';
9
+ export type { Container } from './Container';
10
+ export type { ContainerOptions } from './ContainerOptions';
11
+ export type { Element } from './Element';
12
+ export type { Group } from './Group';
13
+ export { Icon, type IconOptions } from './Icon';
14
+ export type { IconShape } from './IconShape';
15
+ export { Layer, type LayerOptions } from './Layer';
16
+ export { Row } from './Row';
17
+ export { Settings } from './Settings';
18
+ export type { SettingsService } from './SettingsService';
19
+ export { Slider, type SliderOptions } from './Slider';
20
+ export { Spacer, type SpacerOptions } from './Spacer';
21
+ export { Text, type TextOptions } from './Text';
22
+ export type { TextSize } from './TextSize';
23
+ export { Theme } from './Theme';
24
+ export type { ThemeValues } from './ThemeValues';
25
+ export { Toggle, type ToggleOptions } from './Toggle';
26
+ export type { Viewport } from './Viewport';
@@ -0,0 +1,12 @@
1
+ const I32_MIN = -(2 ** 31);
2
+ const I32_MAX = 2 ** 31 - 1;
3
+
4
+ /** Gives the value if it is a whole number that Rust can keep in an `i32`. */
5
+ export function wholeNumber(property: string, value: number): number {
6
+ if (!Number.isInteger(value) || value < I32_MIN || value > I32_MAX) {
7
+ throw new Error(
8
+ `${property} must be a whole number from ${I32_MIN} to ${I32_MAX}, not ${value}.`,
9
+ );
10
+ }
11
+ return value;
12
+ }