phi-engine 0.0.0-stage → 0.3.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.
- package/README.md +60 -2
- package/audio/Audio.ts +3 -0
- package/audio/AudioService.ts +15 -0
- package/audio/SoundName.ts +1 -0
- package/audio/index.ts +3 -0
- package/game/Game.ts +3 -0
- package/game/GameService.ts +57 -0
- package/game/PauseReason.ts +6 -0
- package/game/Time.ts +3 -0
- package/game/TimeService.ts +17 -0
- package/game/index.ts +5 -0
- package/input/Input.ts +3 -0
- package/input/InputDevice.ts +2 -0
- package/input/InputService.ts +71 -0
- package/input/index.ts +3 -0
- package/math/Color.ts +46 -0
- package/math/Vector2.ts +59 -0
- package/math/Vector3.ts +63 -0
- package/math/index.ts +3 -0
- package/package.json +23 -4
- package/render/Camera.ts +3 -0
- package/render/CameraService.ts +19 -0
- package/render/PostProcess.ts +3 -0
- package/render/PostProcessService.ts +14 -0
- package/render/index.ts +4 -0
- package/runtime/EngineOutputBus.ts +17 -0
- package/runtime/EntityValues.ts +103 -0
- package/runtime/PhiRuntime.ts +517 -0
- package/runtime/UiBridge.ts +178 -0
- package/runtime/UiCommand.ts +14 -0
- package/runtime/UiEventWire.ts +6 -0
- package/runtime/ViewportWire.ts +6 -0
- package/runtime/interfaceBridge.ts +3 -0
- package/runtime/protocol.ts +5 -0
- package/runtime/runtime.ts +9 -0
- package/scene/Component.ts +108 -0
- package/scene/Entity.ts +118 -0
- package/scene/Mesh3D.ts +37 -0
- package/scene/PointLight.ts +15 -0
- package/scene/Scene.ts +3 -0
- package/scene/SceneAccessor.ts +45 -0
- package/scene/SceneService.ts +21 -0
- package/scene/Sprite2D.ts +20 -0
- package/scene/Transform2D.ts +43 -0
- package/scene/Transform3D.ts +56 -0
- package/scene/index.ts +16 -0
- package/scene/registerComponents.ts +6 -0
- package/ui/Align.ts +2 -0
- package/ui/Anchor.ts +11 -0
- package/ui/Bar.ts +100 -0
- package/ui/Button.ts +93 -0
- package/ui/Choice.ts +80 -0
- package/ui/Column.ts +10 -0
- package/ui/ComponentUi.ts +70 -0
- package/ui/Container.ts +39 -0
- package/ui/ContainerOptions.ts +18 -0
- package/ui/Control.ts +94 -0
- package/ui/Element.ts +64 -0
- package/ui/ElementKind.ts +12 -0
- package/ui/Group.ts +110 -0
- package/ui/Icon.ts +63 -0
- package/ui/IconShape.ts +1 -0
- package/ui/Layer.ts +105 -0
- package/ui/Row.ts +10 -0
- package/ui/Settings.ts +3 -0
- package/ui/SettingsService.ts +48 -0
- package/ui/Slider.ts +158 -0
- package/ui/Spacer.ts +46 -0
- package/ui/Text.ts +62 -0
- package/ui/TextSize.ts +5 -0
- package/ui/Theme.ts +3 -0
- package/ui/ThemeValues.ts +58 -0
- package/ui/Toggle.ts +46 -0
- package/ui/Viewport.ts +11 -0
- package/ui/WireValue.ts +5 -0
- package/ui/finiteColor.ts +11 -0
- package/ui/finiteNumber.ts +10 -0
- package/ui/index.ts +26 -0
- package/ui/wholeNumber.ts +12 -0
package/ui/Button.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Element } from './Element';
|
|
2
|
+
import { Text } from './Text';
|
|
3
|
+
import { Theme } from './Theme';
|
|
4
|
+
import { finiteColor } from './finiteColor';
|
|
5
|
+
import type { Color } from '../math/Color';
|
|
6
|
+
import type { WireValue } from './WireValue';
|
|
7
|
+
|
|
8
|
+
export interface ButtonOptions {
|
|
9
|
+
/** A disabled button cannot take the focus or be pressed. The default is false. */
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
/** The default is `Theme.buttonBackground`. */
|
|
12
|
+
background?: Color;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A button with a label. The player presses it with accept (Enter, Space or South) while it
|
|
17
|
+
* has the focus, or with a click, which is a press and a release of the left mouse button on
|
|
18
|
+
* it. The arrow keys, the D-pad and the left stick move the focus.
|
|
19
|
+
*/
|
|
20
|
+
export class Button extends Element {
|
|
21
|
+
/** Phi calls it when the player presses the button, before `onUpdate()` of the step. */
|
|
22
|
+
onPress: () => void;
|
|
23
|
+
|
|
24
|
+
private readonly labelText: Text;
|
|
25
|
+
|
|
26
|
+
private disabledValue: boolean;
|
|
27
|
+
|
|
28
|
+
private backgroundValue: Color;
|
|
29
|
+
|
|
30
|
+
private readonly cornerRadius = Theme.cornerRadius;
|
|
31
|
+
|
|
32
|
+
private readonly padding = Theme.buttonPadding;
|
|
33
|
+
|
|
34
|
+
constructor(label: string, onPress: () => void, options: ButtonOptions = {}) {
|
|
35
|
+
super('button');
|
|
36
|
+
this.onPress = onPress;
|
|
37
|
+
this.disabledValue = options.disabled ?? false;
|
|
38
|
+
this.backgroundValue = finiteColor(
|
|
39
|
+
'Button.background',
|
|
40
|
+
options.background ?? Theme.buttonBackground,
|
|
41
|
+
);
|
|
42
|
+
this.labelText = new Text(label, { color: this.labelColor() });
|
|
43
|
+
this.labelText.setParent(this);
|
|
44
|
+
this.items.push(this.labelText);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get label(): string {
|
|
48
|
+
return this.labelText.text;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
set label(value: string) {
|
|
52
|
+
this.labelText.text = value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get disabled(): boolean {
|
|
56
|
+
return this.disabledValue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
set disabled(value: boolean) {
|
|
60
|
+
this.disabledValue = value;
|
|
61
|
+
this.labelText.color = this.labelColor();
|
|
62
|
+
this.changed();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A copy of the background colour. Set a new colour to change it. */
|
|
66
|
+
get background(): Color {
|
|
67
|
+
return this.backgroundValue.clone();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
set background(value: Color) {
|
|
71
|
+
this.backgroundValue = finiteColor('Button.background', value);
|
|
72
|
+
this.changed();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @internal */
|
|
76
|
+
press(): void {
|
|
77
|
+
this.onPress();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private labelColor(): Color {
|
|
81
|
+
return this.disabledValue ? Theme.disabledText : Theme.text;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** @internal */
|
|
85
|
+
wireProps(): Record<string, WireValue> {
|
|
86
|
+
return {
|
|
87
|
+
disabled: this.disabledValue,
|
|
88
|
+
background: this.backgroundValue.toArray(),
|
|
89
|
+
corner_radius: this.cornerRadius,
|
|
90
|
+
padding: this.padding,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
package/ui/Choice.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Control, type ControlOptions } from './Control';
|
|
2
|
+
import type { WireValue } from './WireValue';
|
|
3
|
+
|
|
4
|
+
export interface ChoiceOptions extends ControlOptions {
|
|
5
|
+
/** The index of the selected option. The default is 0. */
|
|
6
|
+
selected?: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Gives the selection if it is the index of an option. Rust makes the same checks. */
|
|
10
|
+
function checkedSelection(options: readonly string[], selected: number): number {
|
|
11
|
+
if (options.length === 0) throw new Error('Choice.options must have at least one option.');
|
|
12
|
+
if (!Number.isInteger(selected) || selected < 0 || selected >= options.length) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`Choice.selected must be a whole number from 0 to ${options.length - 1}, not ${selected}.`,
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
return selected;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A choice with a label and a list of options, such as a text size of 100%, 150% or 200%. It
|
|
22
|
+
* shows the selected option between two arrows. While it has the focus, left and right select
|
|
23
|
+
* the option before or after it, and up and down move the focus. A click on an arrow selects
|
|
24
|
+
* the option in its direction. The list does not wrap, and an end has no arrow.
|
|
25
|
+
*/
|
|
26
|
+
export class Choice extends Control {
|
|
27
|
+
/** Phi calls it with the index of the new option when the player changes the choice. */
|
|
28
|
+
onChange: (selected: number) => void;
|
|
29
|
+
|
|
30
|
+
private optionList: readonly string[];
|
|
31
|
+
|
|
32
|
+
private selectedIndex: number;
|
|
33
|
+
|
|
34
|
+
constructor(
|
|
35
|
+
label: string,
|
|
36
|
+
options: readonly string[],
|
|
37
|
+
onChange: (selected: number) => void,
|
|
38
|
+
choiceOptions: ChoiceOptions = {},
|
|
39
|
+
) {
|
|
40
|
+
super('choice', 'Choice', label, choiceOptions);
|
|
41
|
+
this.onChange = onChange;
|
|
42
|
+
this.optionList = [...options];
|
|
43
|
+
this.selectedIndex = checkedSelection(this.optionList, choiceOptions.selected ?? 0);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A copy of the options. The selection must stay the index of an option. */
|
|
47
|
+
get options(): readonly string[] {
|
|
48
|
+
return [...this.optionList];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
set options(options: readonly string[]) {
|
|
52
|
+
checkedSelection(options, this.selectedIndex);
|
|
53
|
+
this.optionList = [...options];
|
|
54
|
+
this.changed();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The index of the selected option. */
|
|
58
|
+
get selected(): number {
|
|
59
|
+
return this.selectedIndex;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
set selected(selected: number) {
|
|
63
|
+
this.selectedIndex = checkedSelection(this.optionList, selected);
|
|
64
|
+
this.changed();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** @internal */
|
|
68
|
+
change(value: boolean | number): void {
|
|
69
|
+
// Rust made the event from the options of the last frame, and a script can change the
|
|
70
|
+
// options before the event arrives.
|
|
71
|
+
if (typeof value !== 'number' || value >= this.optionList.length) return;
|
|
72
|
+
this.selected = value;
|
|
73
|
+
this.onChange(value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @internal */
|
|
77
|
+
wireProps(): Record<string, WireValue> {
|
|
78
|
+
return { options: this.optionList, selected: this.selectedIndex, ...this.lookProps() };
|
|
79
|
+
}
|
|
80
|
+
}
|
package/ui/Column.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 top to bottom. */
|
|
6
|
+
export class Column extends Group {
|
|
7
|
+
constructor(options: ContainerOptions = {}, children: readonly Element[] = []) {
|
|
8
|
+
super('column', options, children);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { Element } from './Element';
|
|
2
|
+
import type { Layer } from './Layer';
|
|
3
|
+
import type { UiBridge } from '../runtime/UiBridge';
|
|
4
|
+
import type { Viewport } from './Viewport';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The interface elements of one component, as `this.ui`. The component owns the layers that
|
|
8
|
+
* it adds here, and Phi removes them when the component is destroyed.
|
|
9
|
+
*/
|
|
10
|
+
export class ComponentUi {
|
|
11
|
+
/** @internal */
|
|
12
|
+
readonly id: number;
|
|
13
|
+
|
|
14
|
+
private readonly layers: Layer[] = [];
|
|
15
|
+
|
|
16
|
+
private readonly bridge: UiBridge;
|
|
17
|
+
|
|
18
|
+
/** @internal */
|
|
19
|
+
constructor(bridge: UiBridge) {
|
|
20
|
+
this.bridge = bridge;
|
|
21
|
+
this.id = bridge.newOwnerId();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The window of the game in UI units, as it was at the start of this step. */
|
|
25
|
+
get viewport(): Viewport {
|
|
26
|
+
return this.bridge.viewport;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Shows a layer and its elements on the screen, and returns the layer. */
|
|
30
|
+
add<T extends Layer>(layer: T): T {
|
|
31
|
+
if (layer.owner !== null) {
|
|
32
|
+
throw new Error(`Layer ${layer.id} is already on the screen.`);
|
|
33
|
+
}
|
|
34
|
+
layer.setOwner(this);
|
|
35
|
+
this.layers.push(layer);
|
|
36
|
+
this.bridge.added(layer);
|
|
37
|
+
return layer;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Removes a layer and its elements from the screen. */
|
|
41
|
+
remove(layer: Layer): void {
|
|
42
|
+
const index = this.layers.indexOf(layer);
|
|
43
|
+
if (index < 0) {
|
|
44
|
+
throw new Error(`Layer ${layer.id} is not a layer of this component.`);
|
|
45
|
+
}
|
|
46
|
+
this.layers.splice(index, 1);
|
|
47
|
+
this.bridge.removed(layer);
|
|
48
|
+
layer.setOwner(null);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @internal */
|
|
52
|
+
removeAll(): void {
|
|
53
|
+
[...this.layers].forEach((layer) => this.remove(layer));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** @internal */
|
|
57
|
+
added(element: Element): void {
|
|
58
|
+
this.bridge.added(element);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @internal */
|
|
62
|
+
changed(element: Element): void {
|
|
63
|
+
this.bridge.changed(element);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** @internal */
|
|
67
|
+
removed(element: Element): void {
|
|
68
|
+
this.bridge.removed(element);
|
|
69
|
+
}
|
|
70
|
+
}
|
package/ui/Container.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Element } from './Element';
|
|
2
|
+
import type { ElementKind } from './ElementKind';
|
|
3
|
+
|
|
4
|
+
/** An element that holds other elements: a layer, a row or a column. */
|
|
5
|
+
export abstract class Container extends Element {
|
|
6
|
+
protected constructor(kind: ElementKind, children: readonly Element[]) {
|
|
7
|
+
super(kind);
|
|
8
|
+
children.forEach((child) => this.add(child));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Adds a child after the other children, and returns it. */
|
|
12
|
+
add<T extends Element>(child: T): T {
|
|
13
|
+
if (child.kind === 'layer') {
|
|
14
|
+
throw new Error('A layer cannot be in another element. Add it with this.ui.add().');
|
|
15
|
+
}
|
|
16
|
+
if (child.parent !== null) {
|
|
17
|
+
throw new Error(`Element ${child.id} is already in a ${child.parent.kind}.`);
|
|
18
|
+
}
|
|
19
|
+
child.setParent(this);
|
|
20
|
+
this.items.push(child);
|
|
21
|
+
if (this.owner) {
|
|
22
|
+
child.setOwner(this.owner);
|
|
23
|
+
this.owner.added(child);
|
|
24
|
+
}
|
|
25
|
+
return child;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Removes a child and its children. */
|
|
29
|
+
remove(child: Element): void {
|
|
30
|
+
const index = this.items.indexOf(child);
|
|
31
|
+
if (index < 0) {
|
|
32
|
+
throw new Error(`Element ${child.id} is not a child of this ${this.kind}.`);
|
|
33
|
+
}
|
|
34
|
+
this.items.splice(index, 1);
|
|
35
|
+
this.owner?.removed(child);
|
|
36
|
+
child.setOwner(null);
|
|
37
|
+
child.setParent(null);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Color } from '../math/Color';
|
|
2
|
+
import type { Align } from './Align';
|
|
3
|
+
|
|
4
|
+
/** The options of a row or a column. Lengths are in UI units. */
|
|
5
|
+
export interface ContainerOptions {
|
|
6
|
+
/** The space between the children. The default is `Theme.gap`. */
|
|
7
|
+
gap?: number;
|
|
8
|
+
/** The space inside the edges. The default is 0. */
|
|
9
|
+
padding?: number;
|
|
10
|
+
/** The position of the children across the direction. The default is `start`. */
|
|
11
|
+
align?: Align;
|
|
12
|
+
/** Moves children to a new line when they do not fit. The default is false. */
|
|
13
|
+
wrap?: boolean;
|
|
14
|
+
/** The default is none. */
|
|
15
|
+
background?: Color | null;
|
|
16
|
+
/** The default is `Theme.cornerRadius`. */
|
|
17
|
+
cornerRadius?: number;
|
|
18
|
+
}
|
package/ui/Control.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Element } from './Element';
|
|
2
|
+
import { Text } from './Text';
|
|
3
|
+
import { Theme } from './Theme';
|
|
4
|
+
import { finiteColor } from './finiteColor';
|
|
5
|
+
import type { Color } from '../math/Color';
|
|
6
|
+
import type { ElementKind } from './ElementKind';
|
|
7
|
+
import type { WireValue } from './WireValue';
|
|
8
|
+
|
|
9
|
+
/** The look of a toggle, a slider or a choice. */
|
|
10
|
+
export interface ControlOptions {
|
|
11
|
+
/** The default is `Theme.buttonBackground`. */
|
|
12
|
+
background?: Color;
|
|
13
|
+
/** The colour of the marks that show the value. The default is `Theme.text`. */
|
|
14
|
+
color?: Color;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The base of a control that has a label and a value: a toggle, a slider or a choice. The
|
|
19
|
+
* player changes the value of the focused control with the UI actions or the mouse. Phi then
|
|
20
|
+
* sets the new value and calls `onChange`, before `onUpdate()` of the step.
|
|
21
|
+
*/
|
|
22
|
+
export abstract class Control extends Element {
|
|
23
|
+
private readonly labelText: Text;
|
|
24
|
+
|
|
25
|
+
private backgroundValue: Color;
|
|
26
|
+
|
|
27
|
+
private colorValue: Color;
|
|
28
|
+
|
|
29
|
+
private readonly cornerRadius = Theme.cornerRadius;
|
|
30
|
+
|
|
31
|
+
private readonly padding = Theme.buttonPadding;
|
|
32
|
+
|
|
33
|
+
/** The name of the class in error messages, such as `Toggle`. */
|
|
34
|
+
protected readonly typeName: string;
|
|
35
|
+
|
|
36
|
+
protected constructor(
|
|
37
|
+
kind: ElementKind,
|
|
38
|
+
typeName: string,
|
|
39
|
+
label: string,
|
|
40
|
+
options: ControlOptions,
|
|
41
|
+
) {
|
|
42
|
+
super(kind);
|
|
43
|
+
this.typeName = typeName;
|
|
44
|
+
this.backgroundValue = finiteColor(
|
|
45
|
+
`${typeName}.background`,
|
|
46
|
+
options.background ?? Theme.buttonBackground,
|
|
47
|
+
);
|
|
48
|
+
this.colorValue = finiteColor(`${typeName}.color`, options.color ?? Theme.text);
|
|
49
|
+
this.labelText = new Text(label);
|
|
50
|
+
this.labelText.setParent(this);
|
|
51
|
+
this.items.push(this.labelText);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get label(): string {
|
|
55
|
+
return this.labelText.text;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
set label(value: string) {
|
|
59
|
+
this.labelText.text = value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A copy of the background colour. Set a new colour to change it. */
|
|
63
|
+
get background(): Color {
|
|
64
|
+
return this.backgroundValue.clone();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
set background(value: Color) {
|
|
68
|
+
this.backgroundValue = finiteColor(`${this.typeName}.background`, value);
|
|
69
|
+
this.changed();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A copy of the colour of the marks. Set a new colour to change it. */
|
|
73
|
+
get color(): Color {
|
|
74
|
+
return this.colorValue.clone();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
set color(value: Color) {
|
|
78
|
+
this.colorValue = finiteColor(`${this.typeName}.color`, value);
|
|
79
|
+
this.changed();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Sets the value that the player gave, and calls `onChange`. @internal */
|
|
83
|
+
abstract change(value: boolean | number): void;
|
|
84
|
+
|
|
85
|
+
/** The properties that all controls have, as Rust gets them. */
|
|
86
|
+
protected lookProps(): Record<string, WireValue> {
|
|
87
|
+
return {
|
|
88
|
+
background: this.backgroundValue.toArray(),
|
|
89
|
+
color: this.colorValue.toArray(),
|
|
90
|
+
corner_radius: this.cornerRadius,
|
|
91
|
+
padding: this.padding,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
package/ui/Element.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ComponentUi } from './ComponentUi';
|
|
2
|
+
import type { ElementKind } from './ElementKind';
|
|
3
|
+
import type { WireValue } from './WireValue';
|
|
4
|
+
|
|
5
|
+
let nextId = 1;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The base of each interface element. Phi sends an element to Rust only after it is in a
|
|
9
|
+
* layer that `this.ui` of a component holds. After that, Phi sends a property only when its
|
|
10
|
+
* value changes.
|
|
11
|
+
*/
|
|
12
|
+
export abstract class Element {
|
|
13
|
+
/** @internal */
|
|
14
|
+
readonly id: number;
|
|
15
|
+
|
|
16
|
+
/** @internal */
|
|
17
|
+
readonly kind: ElementKind;
|
|
18
|
+
|
|
19
|
+
/** The elements in this element, in order. */
|
|
20
|
+
protected readonly items: Element[] = [];
|
|
21
|
+
|
|
22
|
+
private parentElement: Element | null = null;
|
|
23
|
+
|
|
24
|
+
private ownerUi: ComponentUi | null = null;
|
|
25
|
+
|
|
26
|
+
protected constructor(kind: ElementKind) {
|
|
27
|
+
this.id = nextId;
|
|
28
|
+
nextId += 1;
|
|
29
|
+
this.kind = kind;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The element that holds this element, such as a row, or null. */
|
|
33
|
+
get parent(): Element | null {
|
|
34
|
+
return this.parentElement;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The elements in this element, in order. Phi sends them after this element. */
|
|
38
|
+
get children(): readonly Element[] {
|
|
39
|
+
return this.items;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The component interface that holds the tree of this element. @internal */
|
|
43
|
+
get owner(): ComponentUi | null {
|
|
44
|
+
return this.ownerUi;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @internal */
|
|
48
|
+
setParent(parent: Element | null): void {
|
|
49
|
+
this.parentElement = parent;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Gives this element and its descendants to a component interface. @internal */
|
|
53
|
+
setOwner(owner: ComponentUi | null): void {
|
|
54
|
+
this.ownerUi = owner;
|
|
55
|
+
this.children.forEach((child) => child.setOwner(owner));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** All the properties as Rust gets them. @internal */
|
|
59
|
+
abstract wireProps(): Record<string, WireValue>;
|
|
60
|
+
|
|
61
|
+
protected changed(): void {
|
|
62
|
+
this.ownerUi?.changed(this);
|
|
63
|
+
}
|
|
64
|
+
}
|
package/ui/Group.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Container } from './Container';
|
|
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 { Align } from './Align';
|
|
7
|
+
import type { ContainerOptions } from './ContainerOptions';
|
|
8
|
+
import type { Element } from './Element';
|
|
9
|
+
import type { WireValue } from './WireValue';
|
|
10
|
+
|
|
11
|
+
/** The properties that a row and a column share. */
|
|
12
|
+
export abstract class Group extends Container {
|
|
13
|
+
private gapValue: number;
|
|
14
|
+
|
|
15
|
+
private paddingValue: number;
|
|
16
|
+
|
|
17
|
+
private alignValue: Align;
|
|
18
|
+
|
|
19
|
+
private wrapValue: boolean;
|
|
20
|
+
|
|
21
|
+
private backgroundValue: Color | null;
|
|
22
|
+
|
|
23
|
+
private cornerRadiusValue: number;
|
|
24
|
+
|
|
25
|
+
protected constructor(
|
|
26
|
+
kind: 'row' | 'column',
|
|
27
|
+
options: ContainerOptions,
|
|
28
|
+
children: readonly Element[],
|
|
29
|
+
) {
|
|
30
|
+
super(kind, children);
|
|
31
|
+
this.gapValue = finiteNumber('gap', options.gap ?? Theme.gap);
|
|
32
|
+
this.paddingValue = finiteNumber('padding', options.padding ?? 0);
|
|
33
|
+
this.alignValue = options.align ?? 'start';
|
|
34
|
+
this.wrapValue = options.wrap ?? false;
|
|
35
|
+
this.backgroundValue = options.background
|
|
36
|
+
? finiteColor('background', options.background)
|
|
37
|
+
: null;
|
|
38
|
+
this.cornerRadiusValue = finiteNumber(
|
|
39
|
+
'cornerRadius',
|
|
40
|
+
options.cornerRadius ?? Theme.cornerRadius,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get gap(): number {
|
|
45
|
+
return this.gapValue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
set gap(value: number) {
|
|
49
|
+
this.gapValue = finiteNumber('gap', value);
|
|
50
|
+
this.changed();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get padding(): number {
|
|
54
|
+
return this.paddingValue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
set padding(value: number) {
|
|
58
|
+
this.paddingValue = finiteNumber('padding', value);
|
|
59
|
+
this.changed();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get align(): Align {
|
|
63
|
+
return this.alignValue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
set align(value: Align) {
|
|
67
|
+
this.alignValue = value;
|
|
68
|
+
this.changed();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
get wrap(): boolean {
|
|
72
|
+
return this.wrapValue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
set wrap(value: boolean) {
|
|
76
|
+
this.wrapValue = value;
|
|
77
|
+
this.changed();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A copy of the background colour. Set a new colour to change it. */
|
|
81
|
+
get background(): Color | null {
|
|
82
|
+
return this.backgroundValue?.clone() ?? null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
set background(value: Color | null) {
|
|
86
|
+
this.backgroundValue = value ? finiteColor('background', value) : null;
|
|
87
|
+
this.changed();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
get cornerRadius(): number {
|
|
91
|
+
return this.cornerRadiusValue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
set cornerRadius(value: number) {
|
|
95
|
+
this.cornerRadiusValue = finiteNumber('cornerRadius', value);
|
|
96
|
+
this.changed();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** @internal */
|
|
100
|
+
wireProps(): Record<string, WireValue> {
|
|
101
|
+
return {
|
|
102
|
+
gap: this.gapValue,
|
|
103
|
+
padding: this.paddingValue,
|
|
104
|
+
align: this.alignValue,
|
|
105
|
+
wrap: this.wrapValue,
|
|
106
|
+
background: this.backgroundValue?.toArray() ?? null,
|
|
107
|
+
corner_radius: this.cornerRadiusValue,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
package/ui/Icon.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Element } from './Element';
|
|
2
|
+
import { finiteColor } from './finiteColor';
|
|
3
|
+
import { finiteNumber } from './finiteNumber';
|
|
4
|
+
import { Theme } from './Theme';
|
|
5
|
+
import type { Color } from '../math/Color';
|
|
6
|
+
import type { IconShape } from './IconShape';
|
|
7
|
+
import type { WireValue } from './WireValue';
|
|
8
|
+
|
|
9
|
+
export interface IconOptions {
|
|
10
|
+
/** The width and the height in UI units. The default is `Theme.iconSize`. */
|
|
11
|
+
size?: number;
|
|
12
|
+
/** The default is `Theme.text`. */
|
|
13
|
+
color?: Color;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A simple shape, such as a lamp in a row of lamps. */
|
|
17
|
+
export class Icon extends Element {
|
|
18
|
+
private shapeValue: IconShape;
|
|
19
|
+
|
|
20
|
+
private sizeValue: number;
|
|
21
|
+
|
|
22
|
+
private colorValue: Color;
|
|
23
|
+
|
|
24
|
+
constructor(shape: IconShape, options: IconOptions = {}) {
|
|
25
|
+
super('icon');
|
|
26
|
+
this.shapeValue = shape;
|
|
27
|
+
this.sizeValue = finiteNumber('Icon.size', options.size ?? Theme.iconSize);
|
|
28
|
+
this.colorValue = finiteColor('Icon.color', options.color ?? Theme.text);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get shape(): IconShape {
|
|
32
|
+
return this.shapeValue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
set shape(value: IconShape) {
|
|
36
|
+
this.shapeValue = value;
|
|
37
|
+
this.changed();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get size(): number {
|
|
41
|
+
return this.sizeValue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
set size(value: number) {
|
|
45
|
+
this.sizeValue = finiteNumber('Icon.size', value);
|
|
46
|
+
this.changed();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A copy of the colour. Set a new colour to change it. */
|
|
50
|
+
get color(): Color {
|
|
51
|
+
return this.colorValue.clone();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
set color(value: Color) {
|
|
55
|
+
this.colorValue = finiteColor('Icon.color', value);
|
|
56
|
+
this.changed();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @internal */
|
|
60
|
+
wireProps(): Record<string, WireValue> {
|
|
61
|
+
return { shape: this.shapeValue, size: this.sizeValue, color: this.colorValue.toArray() };
|
|
62
|
+
}
|
|
63
|
+
}
|
package/ui/IconShape.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type IconShape = 'circle' | 'square' | 'diamond' | 'triangle' | 'check';
|