@margoengine/ui 0.0.0-stage → 1.0.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1224 -2
- package/dist/basic.d.ts +87 -0
- package/dist/collections.d.ts +180 -0
- package/dist/color.d.ts +24 -0
- package/dist/controls.d.ts +130 -0
- package/dist/curved.d.ts +41 -0
- package/dist/curved.js +105 -0
- package/dist/dom-CWJhpotq.js +340 -0
- package/dist/dom-host.d.ts +67 -0
- package/dist/dom-host.js +563 -0
- package/dist/dom-spec.d.ts +21 -0
- package/dist/dom.d.ts +63 -0
- package/dist/draw.d.ts +107 -0
- package/dist/editor.d.ts +83 -0
- package/dist/editor.js +323 -0
- package/dist/errors-CDgOgzCd.js +7 -0
- package/dist/focus.d.ts +12 -0
- package/dist/forms.d.ts +114 -0
- package/dist/forms.js +354 -0
- package/dist/gl-bb768Mje.js +165 -0
- package/dist/gl-load-wwKmojhb.js +5 -0
- package/dist/gl-load.d.ts +3 -0
- package/dist/gl.d.ts +30 -0
- package/dist/i18n.d.ts +69 -0
- package/dist/i18n.js +117 -0
- package/dist/image.d.ts +74 -0
- package/dist/image.js +295 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +4 -0
- package/dist/input-dom.d.ts +14 -0
- package/dist/input.d.ts +92 -0
- package/dist/input.js +315 -0
- package/dist/interaction.d.ts +106 -0
- package/dist/keyboard.d.ts +40 -0
- package/dist/keyboard.js +132 -0
- package/dist/layout.d.ts +116 -0
- package/dist/markup.d.ts +89 -0
- package/dist/markup.js +369 -0
- package/dist/meters-yaZRtwsX.js +1401 -0
- package/dist/meters.d.ts +72 -0
- package/dist/mirror.d.ts +30 -0
- package/dist/overlays.d.ts +89 -0
- package/dist/painter.d.ts +115 -0
- package/dist/pass-CAn29pqc.js +15 -0
- package/dist/react.d.ts +99 -0
- package/dist/react.js +246 -0
- package/dist/renderer-Vs-YtiWv.js +1741 -0
- package/dist/renderer.d.ts +138 -0
- package/dist/rsc/react.js +3 -0
- package/dist/shader.d.ts +6 -0
- package/dist/theme.d.ts +104 -0
- package/dist/ui.d.ts +328 -0
- package/dist/virtual.d.ts +81 -0
- package/dist/widget.d.ts +196 -0
- package/dist/world-gl.d.ts +32 -0
- package/dist/world-gl.js +195 -0
- package/dist/world.d.ts +216 -0
- package/dist/world.js +561 -0
- package/package.json +117 -4
package/dist/basic.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Widget, type ColorRef, type DrawContext, type WidgetOptions } from './widget.js';
|
|
2
|
+
/** Which surface colour a {@link Panel} draws. `'none'` draws nothing, so the panel is only a layout box. */
|
|
3
|
+
export type PanelBackground = 'none' | 'surface' | 'raised' | 'sunken' | 'scrim' | (string & {});
|
|
4
|
+
/** Options for {@link Panel}. */
|
|
5
|
+
export interface PanelOptions extends WidgetOptions {
|
|
6
|
+
/** `'surface'` (default), `'raised'`, `'sunken'`, `'scrim'`, `'none'` or a `#rrggbb[aa]` colour. Default `'none'`. */
|
|
7
|
+
background?: PanelBackground;
|
|
8
|
+
/** Draw the theme's outline. Default false. */
|
|
9
|
+
border?: boolean;
|
|
10
|
+
/** Draw a soft drop shadow (the theme's blur; none in high contrast). Default false. */
|
|
11
|
+
shadow?: boolean;
|
|
12
|
+
/** Corner radius in UI units. Default the theme's. */
|
|
13
|
+
radius?: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A container with an optional background. Children stack in a column by default; set `direction: 'row'`, `'stack'`
|
|
17
|
+
* or `'grid'`. A panel with a background takes pointer events, so clicks on it do not reach the game.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ui.add(new Panel({ background: 'surface', padding: 16, gap: 8, anchor: 'top-left', margin: 16, position: 'absolute',
|
|
21
|
+
* children: [new Label({ text: 'Inventory', size: 'large' })] }));
|
|
22
|
+
*/
|
|
23
|
+
export declare class Panel extends Widget {
|
|
24
|
+
private _background;
|
|
25
|
+
private _border;
|
|
26
|
+
private _shadow;
|
|
27
|
+
private _radius?;
|
|
28
|
+
constructor(options?: PanelOptions);
|
|
29
|
+
get background(): PanelBackground;
|
|
30
|
+
set background(value: PanelBackground);
|
|
31
|
+
get border(): boolean;
|
|
32
|
+
set border(value: boolean);
|
|
33
|
+
get shadow(): boolean;
|
|
34
|
+
set shadow(value: boolean);
|
|
35
|
+
draw(context: DrawContext): void;
|
|
36
|
+
}
|
|
37
|
+
/** Text sizes by name: the theme's `small`, `body`, `large`, `title` and `display`; or a number of UI units. */
|
|
38
|
+
export type TextSize = 'small' | 'body' | 'large' | 'title' | 'display' | number;
|
|
39
|
+
/** Options for {@link Label}. */
|
|
40
|
+
export interface LabelOptions extends WidgetOptions {
|
|
41
|
+
/** The text. Newlines break lines. Default ''. */
|
|
42
|
+
text?: string;
|
|
43
|
+
/** Default `'body'`. */
|
|
44
|
+
size?: TextSize;
|
|
45
|
+
/** A colour role such as `'textMuted'` or `'danger'`, or `#rrggbb[aa]`. Default `'text'`. */
|
|
46
|
+
color?: ColorRef;
|
|
47
|
+
/** Alignment of the text inside the label's box; `'start'` is the reading start (the right edge of right-to-left text). Default `'start'`. */
|
|
48
|
+
align?: 'start' | 'center' | 'end';
|
|
49
|
+
/** Wrap at the box's width. Default true; false keeps one line per newline. */
|
|
50
|
+
wrap?: boolean;
|
|
51
|
+
/** Draw a dark outline round the glyphs so the text reads over any game scene. Default false. */
|
|
52
|
+
outline?: boolean;
|
|
53
|
+
/** Line height as a multiple of the size. Default 1.25. */
|
|
54
|
+
lineHeight?: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Text. It wraps to the width it is given, scales with the user's text scale and is drawn with the text package's
|
|
58
|
+
* signed distance field glyphs, so it stays sharp at any resolution.
|
|
59
|
+
*/
|
|
60
|
+
export declare class Label extends Widget {
|
|
61
|
+
private _text;
|
|
62
|
+
private _size;
|
|
63
|
+
private _color;
|
|
64
|
+
private _align;
|
|
65
|
+
private _wrap;
|
|
66
|
+
private _outline;
|
|
67
|
+
private _lineHeight;
|
|
68
|
+
constructor(options?: LabelOptions);
|
|
69
|
+
get text(): string;
|
|
70
|
+
/** Set the text; the layout updates on the next frame. */
|
|
71
|
+
set text(value: string);
|
|
72
|
+
get size(): TextSize;
|
|
73
|
+
set size(value: TextSize);
|
|
74
|
+
get color(): ColorRef;
|
|
75
|
+
set color(value: ColorRef);
|
|
76
|
+
get align(): 'start' | 'center' | 'end';
|
|
77
|
+
set align(value: 'start' | 'center' | 'end');
|
|
78
|
+
get wrap(): boolean;
|
|
79
|
+
set wrap(value: boolean);
|
|
80
|
+
get outline(): boolean;
|
|
81
|
+
set outline(value: boolean);
|
|
82
|
+
measure(maxWidth: number): {
|
|
83
|
+
width: number;
|
|
84
|
+
height: number;
|
|
85
|
+
};
|
|
86
|
+
draw({ list, ui }: DrawContext): void;
|
|
87
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { Panel, type PanelOptions } from './basic.js';
|
|
2
|
+
import { type ButtonOptions } from './controls.js';
|
|
3
|
+
import { Widget, type ColorRef, type DrawContext, type UiEvent, type WidgetOptions } from './widget.js';
|
|
4
|
+
/** What a row tells when it is chosen: a {@link List} or a virtual list. */
|
|
5
|
+
export interface ListOwner {
|
|
6
|
+
select(index: number, emit: boolean): void;
|
|
7
|
+
}
|
|
8
|
+
/** One row of a {@link List}. A plain string is the same as `{ label: string }`. */
|
|
9
|
+
export interface ListEntry {
|
|
10
|
+
label: string;
|
|
11
|
+
/** Your data; passed back in `'select'` events as `item`. Default the entry itself. */
|
|
12
|
+
value?: unknown;
|
|
13
|
+
/** Muted text at the right edge, for example a price or a count. */
|
|
14
|
+
detail?: string;
|
|
15
|
+
/** Disabled rows draw dimmed and cannot be selected. */
|
|
16
|
+
disabled?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/** @internal A selectable row of a list. */
|
|
19
|
+
export declare class ListItem extends Widget {
|
|
20
|
+
readonly list: ListOwner;
|
|
21
|
+
readonly index: number;
|
|
22
|
+
readonly entry: ListEntry;
|
|
23
|
+
selected: boolean;
|
|
24
|
+
constructor(list: ListOwner, index: number, entry: ListEntry);
|
|
25
|
+
get text(): string;
|
|
26
|
+
activate(): void;
|
|
27
|
+
measure(maxWidth: number): {
|
|
28
|
+
width: number;
|
|
29
|
+
height: number;
|
|
30
|
+
};
|
|
31
|
+
draw(context: DrawContext): void;
|
|
32
|
+
}
|
|
33
|
+
/** Options for {@link List}. */
|
|
34
|
+
export interface ListOptions extends PanelOptions {
|
|
35
|
+
/** The rows. */
|
|
36
|
+
items?: readonly (string | ListEntry)[];
|
|
37
|
+
/** Index of the selected row. Default -1 (none). */
|
|
38
|
+
selected?: number;
|
|
39
|
+
/** Called with `index` and `item` when a row is chosen by click, tap, Enter or the gamepad. */
|
|
40
|
+
onSelect?: (event: UiEvent) => void;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A vertical list of selectable rows that scrolls when it is taller than its box (give it a `height` or `maxHeight`).
|
|
44
|
+
* Focus moves between rows with the arrow keys and the d-pad and scrolls the focused row into view.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const saves = new List({ items: ['Slot 1', 'Slot 2', 'Slot 3'], height: 180, onSelect: (e) => load(e.index!) });
|
|
48
|
+
*/
|
|
49
|
+
export declare class List extends Panel {
|
|
50
|
+
private _items;
|
|
51
|
+
private _selected;
|
|
52
|
+
constructor(options?: ListOptions);
|
|
53
|
+
get items(): readonly ListEntry[];
|
|
54
|
+
/** Replace the rows and clear the selection. @throws TypeError for an entry that is not a string or `{ label }`. */
|
|
55
|
+
set items(items: readonly (string | ListEntry)[]);
|
|
56
|
+
get selected(): number;
|
|
57
|
+
/** Index of the selected row, or -1. */
|
|
58
|
+
get selectedItem(): ListEntry | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Select a row (-1 clears). With `emit` it also fires `'select'`.
|
|
61
|
+
* @throws RangeError for an index outside -1..length-1.
|
|
62
|
+
*/
|
|
63
|
+
select(index: number, emit?: boolean): void;
|
|
64
|
+
}
|
|
65
|
+
/** An item in an {@link Inventory} slot. */
|
|
66
|
+
export interface InventoryItem {
|
|
67
|
+
/** Your identifier, for example `'potion-health'`. */
|
|
68
|
+
id: string;
|
|
69
|
+
/** Shown in the tooltip and read by screen readers. */
|
|
70
|
+
name: string;
|
|
71
|
+
/** A symbol or emoji drawn in the slot. Default the first letter of `name`. */
|
|
72
|
+
icon?: string;
|
|
73
|
+
/** Stack size; shown when above 1. */
|
|
74
|
+
count?: number;
|
|
75
|
+
/** Second line of the tooltip. */
|
|
76
|
+
description?: string;
|
|
77
|
+
/** Accent colour of the slot's rim (a theme role or `#rrggbb`). */
|
|
78
|
+
color?: ColorRef;
|
|
79
|
+
}
|
|
80
|
+
/** Options for {@link Inventory}. */
|
|
81
|
+
export interface InventoryOptions extends WidgetOptions {
|
|
82
|
+
/** Columns of the grid. Default 5. */
|
|
83
|
+
columns?: number;
|
|
84
|
+
/** Number of slots. Default `columns` times 4. */
|
|
85
|
+
slots?: number;
|
|
86
|
+
/** Slot edge length in UI units. Default 56. */
|
|
87
|
+
slotSize?: number;
|
|
88
|
+
/** Space between slots. Default 6. */
|
|
89
|
+
slotGap?: number;
|
|
90
|
+
/** The items, one per slot, `null` for an empty slot. */
|
|
91
|
+
items?: readonly (InventoryItem | null)[];
|
|
92
|
+
/** `'select'` when an item is picked up, with `index` and `item`. */
|
|
93
|
+
onSelect?: (event: UiEvent) => void;
|
|
94
|
+
/** `'move'` when an item is dropped on another slot, with `from` and `index`. The inventory has already swapped them. */
|
|
95
|
+
onMove?: (event: UiEvent) => void;
|
|
96
|
+
/** `'use'` when a picked-up item is activated again, with `index` and `item`. */
|
|
97
|
+
onUse?: (event: UiEvent) => void;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* A grid of item slots. Drag an item to another slot with a mouse or finger; or with the keyboard or a gamepad, press
|
|
101
|
+
* accept on an item to pick it up, move to another slot and press accept to put it there (accept on the same slot
|
|
102
|
+
* again uses it; cancel puts it back). Hovering or focusing a slot shows the item's name and description.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* const bag = new Inventory({ columns: 4, slots: 12, items: [{ id: 'potion', name: 'Potion', icon: '🧪', count: 3 }] });
|
|
106
|
+
* bag.on('use', (e) => drink(e.item));
|
|
107
|
+
*/
|
|
108
|
+
export declare class Inventory extends Widget {
|
|
109
|
+
private _items;
|
|
110
|
+
private slotWidgets;
|
|
111
|
+
/** The slot being dragged with a pointer, or -1. */
|
|
112
|
+
dragging: number;
|
|
113
|
+
private dragStart;
|
|
114
|
+
private dragPoint;
|
|
115
|
+
private _selected;
|
|
116
|
+
private _carried;
|
|
117
|
+
/** @throws RangeError for a non-integer `columns` or `slots`, or `slots` below 0. */
|
|
118
|
+
constructor(options?: InventoryOptions);
|
|
119
|
+
get slots(): number;
|
|
120
|
+
get columns(): number;
|
|
121
|
+
get items(): readonly (InventoryItem | null)[];
|
|
122
|
+
/** Replace all items; extra items beyond the slots are ignored. */
|
|
123
|
+
set items(items: readonly (InventoryItem | null)[]);
|
|
124
|
+
/** The slot widget at `index`, for tests and custom focus. */
|
|
125
|
+
slot(index: number): Widget;
|
|
126
|
+
/** Index of the slot holding the selection, or -1. */
|
|
127
|
+
get selected(): number;
|
|
128
|
+
/** Index of the picked-up item, or -1. */
|
|
129
|
+
get carried(): number;
|
|
130
|
+
/** The item in a slot, or null. */
|
|
131
|
+
get(index: number): InventoryItem | null;
|
|
132
|
+
/** Put an item (or null) in a slot. @throws RangeError for a slot that does not exist. */
|
|
133
|
+
set(index: number, item: InventoryItem | null): void;
|
|
134
|
+
/** Swap the contents of two slots and emit `'move'`. */
|
|
135
|
+
swap(from: number, to: number): void;
|
|
136
|
+
private sync;
|
|
137
|
+
/** Keyboard, gamepad and tap behaviour of a slot: pick up, put down or use. */
|
|
138
|
+
activateSlot(index: number): void;
|
|
139
|
+
/** Put a picked-up item back. Returns true if one was picked up. */
|
|
140
|
+
cancelCarry(): boolean;
|
|
141
|
+
/** @internal Called by a slot on pointer down. */
|
|
142
|
+
beginDrag(index: number, x: number, y: number): boolean;
|
|
143
|
+
private pendingDrag;
|
|
144
|
+
/** @internal */
|
|
145
|
+
dragTo(x: number, y: number): void;
|
|
146
|
+
/** @internal Drop on the slot under the pointer, or treat a short press as an activation. */
|
|
147
|
+
endDrag(x: number, y: number): void;
|
|
148
|
+
private slotAt;
|
|
149
|
+
draw(): void;
|
|
150
|
+
/** The dragged item follows the pointer above every slot. */
|
|
151
|
+
drawAfter(context: DrawContext): void;
|
|
152
|
+
}
|
|
153
|
+
/** One entry of a {@link Menu}. */
|
|
154
|
+
export interface MenuItem extends Pick<ButtonOptions, 'icon' | 'variant'> {
|
|
155
|
+
label: string;
|
|
156
|
+
/** Called when the entry is chosen. */
|
|
157
|
+
onSelect?: () => void;
|
|
158
|
+
disabled?: boolean;
|
|
159
|
+
}
|
|
160
|
+
/** Options for {@link Menu}. */
|
|
161
|
+
export interface MenuOptions extends PanelOptions {
|
|
162
|
+
/** Heading above the entries. */
|
|
163
|
+
title?: string;
|
|
164
|
+
/** The entries, top to bottom. The first gets the `primary` look unless it sets `variant`. */
|
|
165
|
+
items?: readonly MenuItem[];
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* A titled column of full-width buttons: a main menu, pause menu or context menu. It is centred on screen by default;
|
|
169
|
+
* the first entry takes focus when the keyboard or gamepad is first used.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ui.add(new Menu({ title: 'Paused', items: [{ label: 'Resume', onSelect: resume }, { label: 'Quit', onSelect: quit }] }));
|
|
173
|
+
*/
|
|
174
|
+
export declare class Menu extends Panel {
|
|
175
|
+
private readonly heading;
|
|
176
|
+
constructor(options?: MenuOptions);
|
|
177
|
+
/** The heading text; also the menu's accessible name. */
|
|
178
|
+
get title(): string;
|
|
179
|
+
set title(value: string);
|
|
180
|
+
}
|
package/dist/color.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Straight (non-premultiplied) sRGB colour, each channel in 0..1. */
|
|
2
|
+
export type Rgba = readonly [number, number, number, number];
|
|
3
|
+
/**
|
|
4
|
+
* Parse `#rgb`, `#rrggbb` or `#rrggbbaa` into sRGB channels in 0..1. An {@link Rgba} passes through after a
|
|
5
|
+
* range check. Throws a TypeError for any other string and a RangeError for a channel outside 0..1.
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseRgba(value: string | Rgba): Rgba;
|
|
8
|
+
/** `color` with its alpha multiplied by `alpha` (0..1). */
|
|
9
|
+
export declare function withAlpha(color: Rgba, alpha: number): Rgba;
|
|
10
|
+
/** Linear blend from `a` to `b` in sRGB, `t` in 0..1. */
|
|
11
|
+
export declare function mixColors(a: Rgba, b: Rgba, t: number): Rgba;
|
|
12
|
+
/** WCAG 2 relative luminance of an opaque colour. */
|
|
13
|
+
export declare function luminance(color: string | Rgba): number;
|
|
14
|
+
/**
|
|
15
|
+
* WCAG 2 contrast ratio of two colours, from 1 (identical) to 21 (black on white). Text needs 4.5 (AA) or 7 (AAA);
|
|
16
|
+
* the boundary of a control against its surroundings needs 3.
|
|
17
|
+
*/
|
|
18
|
+
export declare function contrastRatio(a: string | Rgba, b: string | Rgba): number;
|
|
19
|
+
/** The kinds of colour-vision deficiency {@link simulateColorBlindness} can show. */
|
|
20
|
+
export type ColorBlindness = 'protanopia' | 'deuteranopia' | 'tritanopia';
|
|
21
|
+
/** How `color` looks to a viewer with the given dichromacy (a Machado et al. 2009 model at full severity). */
|
|
22
|
+
export declare function simulateColorBlindness(color: string | Rgba, kind: ColorBlindness): Rgba;
|
|
23
|
+
/** CIE76 colour difference of two opaque sRGB colours (D65). About 2.3 is just noticeable; 20 or more reads as different colours. */
|
|
24
|
+
export declare function colorDifference(a: string | Rgba, b: string | Rgba): number;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { Widget, type DrawContext, type UiCommand, type UiEvent, type WidgetOptions } from './widget.js';
|
|
2
|
+
import type { TextSize } from './basic.js';
|
|
3
|
+
/** Look of a {@link Button}. */
|
|
4
|
+
export type ButtonVariant = 'primary' | 'secondary' | 'danger' | 'ghost';
|
|
5
|
+
/** Options for {@link Button}. */
|
|
6
|
+
export interface ButtonOptions extends WidgetOptions {
|
|
7
|
+
/** The caption. */
|
|
8
|
+
text?: string;
|
|
9
|
+
/** A symbol or emoji drawn before the caption. */
|
|
10
|
+
icon?: string;
|
|
11
|
+
/** Default `'secondary'`. */
|
|
12
|
+
variant?: ButtonVariant;
|
|
13
|
+
/** Default `'body'`. */
|
|
14
|
+
size?: TextSize;
|
|
15
|
+
/** Called on click, tap, Enter, Space and the gamepad's accept button. */
|
|
16
|
+
onClick?: (event: UiEvent) => void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A push button. It reacts to a pointer, a tap, Enter or Space when focused, and a gamepad's accept button. The
|
|
20
|
+
* minimum height is 44 UI units, the usual touch target size.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const play = new Button({ text: 'Play', variant: 'primary', onClick: () => startGame() });
|
|
24
|
+
*/
|
|
25
|
+
export declare class Button extends Widget {
|
|
26
|
+
private _text;
|
|
27
|
+
private _icon;
|
|
28
|
+
private _variant;
|
|
29
|
+
private _size;
|
|
30
|
+
constructor(options?: ButtonOptions);
|
|
31
|
+
get text(): string;
|
|
32
|
+
set text(value: string);
|
|
33
|
+
get icon(): string;
|
|
34
|
+
set icon(value: string);
|
|
35
|
+
get variant(): ButtonVariant;
|
|
36
|
+
set variant(value: ButtonVariant);
|
|
37
|
+
private get caption();
|
|
38
|
+
measure(maxWidth: number): {
|
|
39
|
+
width: number;
|
|
40
|
+
height: number;
|
|
41
|
+
};
|
|
42
|
+
activate(): void;
|
|
43
|
+
draw(context: DrawContext): void;
|
|
44
|
+
}
|
|
45
|
+
/** Options for {@link Toggle}. */
|
|
46
|
+
export interface ToggleOptions extends WidgetOptions {
|
|
47
|
+
/** Text beside the control. */
|
|
48
|
+
text?: string;
|
|
49
|
+
/** Initial state. Default false. */
|
|
50
|
+
checked?: boolean;
|
|
51
|
+
/** `'switch'` (default) or `'checkbox'`. */
|
|
52
|
+
variant?: 'switch' | 'checkbox';
|
|
53
|
+
/** Called with `value` set to the new state. */
|
|
54
|
+
onChange?: (event: UiEvent) => void;
|
|
55
|
+
}
|
|
56
|
+
/** A switch or checkbox. The state shows by position (switch) or a filled box (checkbox) as well as by colour. */
|
|
57
|
+
export declare class Toggle extends Widget {
|
|
58
|
+
private _text;
|
|
59
|
+
private _checked;
|
|
60
|
+
private readonly variant;
|
|
61
|
+
private slide;
|
|
62
|
+
constructor(options?: ToggleOptions);
|
|
63
|
+
get text(): string;
|
|
64
|
+
set text(value: string);
|
|
65
|
+
get checked(): boolean;
|
|
66
|
+
/** Set the state without emitting `'change'`. */
|
|
67
|
+
set checked(value: boolean);
|
|
68
|
+
/** Flip the state and emit `'change'`. */
|
|
69
|
+
activate(): void;
|
|
70
|
+
private get track();
|
|
71
|
+
measure(maxWidth: number): {
|
|
72
|
+
width: number;
|
|
73
|
+
height: number;
|
|
74
|
+
};
|
|
75
|
+
tick(dt: number): boolean;
|
|
76
|
+
draw(context: DrawContext): void;
|
|
77
|
+
}
|
|
78
|
+
/** Options for {@link Slider}. */
|
|
79
|
+
export interface SliderOptions extends WidgetOptions {
|
|
80
|
+
/** Default 0. */
|
|
81
|
+
min?: number;
|
|
82
|
+
/** Default 1. */
|
|
83
|
+
max?: number;
|
|
84
|
+
/** Snap values to multiples of `step` from `min`. 0 is continuous. Default 0. */
|
|
85
|
+
step?: number;
|
|
86
|
+
/** Initial value. Default `min`. */
|
|
87
|
+
value?: number;
|
|
88
|
+
/** Text to the left of the track. */
|
|
89
|
+
text?: string;
|
|
90
|
+
/** Show the value to the right of the track. Default true. */
|
|
91
|
+
showValue?: boolean;
|
|
92
|
+
/** Turn the value into the text shown. Default two significant decimals. */
|
|
93
|
+
format?: (value: number) => string;
|
|
94
|
+
/** Called with `value` set to the new value, while dragging and on every key step. */
|
|
95
|
+
onChange?: (event: UiEvent) => void;
|
|
96
|
+
}
|
|
97
|
+
/** A horizontal slider. Drag it, tap on the track, or use the left and right keys or the gamepad's d-pad when focused. */
|
|
98
|
+
export declare class Slider extends Widget {
|
|
99
|
+
readonly min: number;
|
|
100
|
+
readonly max: number;
|
|
101
|
+
readonly step: number;
|
|
102
|
+
private _value;
|
|
103
|
+
private _text;
|
|
104
|
+
private readonly showValue;
|
|
105
|
+
private readonly format;
|
|
106
|
+
private dragging;
|
|
107
|
+
/** @throws RangeError when `min >= max`, `step` is negative, or `value` is not finite. */
|
|
108
|
+
constructor(options?: SliderOptions);
|
|
109
|
+
get text(): string;
|
|
110
|
+
get value(): number;
|
|
111
|
+
/** Set the value (clamped and snapped) without emitting `'change'`. */
|
|
112
|
+
set value(next: number);
|
|
113
|
+
/** The value as text, as drawn and read by screen readers. */
|
|
114
|
+
get valueText(): string;
|
|
115
|
+
private snap;
|
|
116
|
+
private change;
|
|
117
|
+
/** Set the value and emit `'change'` if it changed. */
|
|
118
|
+
setValue(next: number): void;
|
|
119
|
+
command(command: UiCommand): boolean;
|
|
120
|
+
measure(): {
|
|
121
|
+
width: number;
|
|
122
|
+
height: number;
|
|
123
|
+
};
|
|
124
|
+
private get trackRect();
|
|
125
|
+
private fromPointer;
|
|
126
|
+
pointerDown(x: number): boolean;
|
|
127
|
+
pointerMove(x: number): void;
|
|
128
|
+
pointerUp(): void;
|
|
129
|
+
draw(context: DrawContext): void;
|
|
130
|
+
}
|
package/dist/curved.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { WorldPanel, type WorldPanelOptions, type PanelHit, type UiRay } from '@margoengine/ui/world';
|
|
2
|
+
import type { Ui } from '@margoengine/ui';
|
|
3
|
+
/** Surface dimensions are arc lengths in metres, with the centre tangent to local Z=0. */
|
|
4
|
+
export interface CurvedPanelOptions extends WorldPanelOptions {
|
|
5
|
+
/** Default 'cylinder'. A sphere curves vertically as well. */
|
|
6
|
+
shape?: 'cylinder' | 'sphere';
|
|
7
|
+
/** Radius in metres, default 2. Width must be below one circumference; sphere height below pi*radius. */
|
|
8
|
+
radius?: number;
|
|
9
|
+
/** Tessellation per curved axis, default 64, integer 4..512. Rays always hit the analytic surface. */
|
|
10
|
+
segments?: number;
|
|
11
|
+
}
|
|
12
|
+
/** Textured cylinder or sphere patch, usable by either world-panel renderer and its XR ray router. */
|
|
13
|
+
export declare class CurvedPanel extends WorldPanel {
|
|
14
|
+
readonly shape: 'cylinder' | 'sphere';
|
|
15
|
+
readonly radius: number;
|
|
16
|
+
readonly segments: number;
|
|
17
|
+
constructor(ui: Ui, options?: CurvedPanelOptions);
|
|
18
|
+
get surface(): readonly [number, number, number, number];
|
|
19
|
+
/** Curved patches require per-fragment sidedness, rather than a single plane-side rejection. */
|
|
20
|
+
side(_eye: ArrayLike<number>): number;
|
|
21
|
+
/** Local position at normalized UI coordinates; also defines the render and hit parameterization. */
|
|
22
|
+
pointAt(u: number, v: number): [number, number, number];
|
|
23
|
+
intersect(ray: UiRay): PanelHit | null;
|
|
24
|
+
}
|
|
25
|
+
/** Options for a row-major XR menu arrangement. Existing panels retain their dimensions. */
|
|
26
|
+
export interface PanelLayoutOptions {
|
|
27
|
+
/** Default 'plane'. */
|
|
28
|
+
shape?: 'plane' | 'cylinder' | 'sphere';
|
|
29
|
+
/** Default ceil(sqrt(panel count)). */
|
|
30
|
+
columns?: number;
|
|
31
|
+
/** Metres between panel centres along each arc, default [1.2, .8]. */
|
|
32
|
+
spacing?: readonly [number, number];
|
|
33
|
+
/** Radius, default 2. */
|
|
34
|
+
radius?: number;
|
|
35
|
+
/** Centre of the arrangement's tangent plane, default [0,0,0]. */
|
|
36
|
+
position?: readonly [number, number, number];
|
|
37
|
+
/** Face the centre of curvature, default false (outward-facing). */
|
|
38
|
+
inward?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/** Arrange any world panels on a plane, cylinder or sphere with tangent orientations. */
|
|
41
|
+
export declare function layoutPanels(panels: readonly WorldPanel[], options?: PanelLayoutOptions): void;
|
package/dist/curved.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { n as e } from "./errors-CDgOgzCd.js";
|
|
2
|
+
import { WorldPanel as t } from "@margoengine/ui/world";
|
|
3
|
+
//#region src/curved.ts
|
|
4
|
+
var n = class extends t {
|
|
5
|
+
constructor(t, n = {}) {
|
|
6
|
+
if (super(t, n), this.shape = n.shape ?? "cylinder", this.radius = n.radius ?? 2, this.segments = n.segments ?? 64, !["cylinder", "sphere"].includes(this.shape) || !(this.radius > 0) || !Number.isFinite(this.radius) || !Number.isInteger(this.segments) || this.segments < 4 || this.segments > 512 || this.width >= 2 * Math.PI * this.radius || this.shape === "sphere" && this.height >= Math.PI * this.radius) throw e("Use cylinder or sphere, positive radius, 4..512 segments and arc sizes below a full wrap", "ui");
|
|
7
|
+
}
|
|
8
|
+
get surface() {
|
|
9
|
+
return [
|
|
10
|
+
this.radius,
|
|
11
|
+
this.shape === "sphere" ? 2 : 1,
|
|
12
|
+
this.segments,
|
|
13
|
+
this.shape === "sphere" ? this.segments : 1
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
side(e) {
|
|
17
|
+
return 1;
|
|
18
|
+
}
|
|
19
|
+
pointAt(e, t) {
|
|
20
|
+
let n = this.radius, r = (e - .5) * this.width / n, i = (.5 - t) * this.height / n;
|
|
21
|
+
return [
|
|
22
|
+
n * Math.sin(r) * (this.shape === "sphere" ? Math.cos(i) : 1),
|
|
23
|
+
this.shape === "sphere" ? n * Math.sin(i) : (.5 - t) * this.height,
|
|
24
|
+
n * (Math.cos(r) * (this.shape === "sphere" ? Math.cos(i) : 1) - 1)
|
|
25
|
+
];
|
|
26
|
+
}
|
|
27
|
+
intersect(e) {
|
|
28
|
+
let t = this.matrix, n = this.shape === "sphere", r = this.radius, i = [
|
|
29
|
+
0,
|
|
30
|
+
1,
|
|
31
|
+
2
|
|
32
|
+
].map((n) => e.origin[n] - t[12 + n]), a = [
|
|
33
|
+
0,
|
|
34
|
+
4,
|
|
35
|
+
8
|
|
36
|
+
].map((e) => i[0] * t[e] + i[1] * t[e + 1] + i[2] * t[e + 2]), o = [
|
|
37
|
+
0,
|
|
38
|
+
4,
|
|
39
|
+
8
|
|
40
|
+
].map((n) => e.direction[0] * t[n] + e.direction[1] * t[n + 1] + e.direction[2] * t[n + 2]);
|
|
41
|
+
a[2] += r;
|
|
42
|
+
let s = n ? [
|
|
43
|
+
0,
|
|
44
|
+
1,
|
|
45
|
+
2
|
|
46
|
+
] : [0, 2], c = s.reduce((e, t) => e + o[t] * o[t], 0), l = 2 * s.reduce((e, t) => e + a[t] * o[t], 0), u = s.reduce((e, t) => e + a[t] * a[t], -r * r), d = l * l - 4 * c * u;
|
|
47
|
+
if (!(c > 0) || !(d >= 0) || !Number.isFinite(d)) return null;
|
|
48
|
+
let f = -.5 * (l + (l < 0 ? -1 : 1) * Math.sqrt(d)), p = f === 0 ? [-l / (2 * c)] : [f / c, u / f].sort((e, t) => e - t), m = null;
|
|
49
|
+
for (let t of p) {
|
|
50
|
+
if (t < 0) continue;
|
|
51
|
+
let i = a.map((e, n) => e + t * o[n]), c = s.reduce((e, t) => e + i[t] * o[t], 0) > 0;
|
|
52
|
+
if (c && !this.doubleSided) continue;
|
|
53
|
+
let l = .5 + Math.atan2(i[0], i[2]) * r / this.width, u = .5 - (n ? Math.asin(Math.max(-1, Math.min(1, i[1] / r))) * r : i[1]) / this.height;
|
|
54
|
+
c && (l = 1 - l);
|
|
55
|
+
let d = {
|
|
56
|
+
distance: t,
|
|
57
|
+
point: [
|
|
58
|
+
e.origin[0] + t * e.direction[0],
|
|
59
|
+
e.origin[1] + t * e.direction[1],
|
|
60
|
+
e.origin[2] + t * e.direction[2]
|
|
61
|
+
],
|
|
62
|
+
x: l * this.cssWidth,
|
|
63
|
+
y: u * this.cssHeight,
|
|
64
|
+
back: c,
|
|
65
|
+
inside: l >= 0 && l <= 1 && u >= 0 && u <= 1
|
|
66
|
+
};
|
|
67
|
+
if (d.inside) return d;
|
|
68
|
+
m ??= d;
|
|
69
|
+
}
|
|
70
|
+
return m;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
function r(t, n = {}) {
|
|
74
|
+
let r = n.shape ?? "plane", i = n.columns ?? Math.max(1, Math.ceil(Math.sqrt(t.length))), [a, o] = n.spacing ?? [1.2, .8], s = n.radius ?? 2, c = n.position ?? [
|
|
75
|
+
0,
|
|
76
|
+
0,
|
|
77
|
+
0
|
|
78
|
+
];
|
|
79
|
+
if (![
|
|
80
|
+
"plane",
|
|
81
|
+
"cylinder",
|
|
82
|
+
"sphere"
|
|
83
|
+
].includes(r) || !Number.isInteger(i) || i < 1 || ![
|
|
84
|
+
a,
|
|
85
|
+
o,
|
|
86
|
+
s
|
|
87
|
+
].every((e) => e > 0 && Number.isFinite(e)) || !c.every(Number.isFinite)) throw e("Use a plane, cylinder or sphere, positive integer columns, positive spacing/radius and finite position", "ui");
|
|
88
|
+
let l = Math.ceil(t.length / i);
|
|
89
|
+
if (r !== "plane" && ((Math.min(i, t.length) - 1) * a >= 2 * Math.PI * s || r === "sphere" && (l - 1) * o >= Math.PI * s)) throw e("Panel arrangement wraps onto itself; increase radius or reduce spacing", "ui");
|
|
90
|
+
t.forEach((e, u) => {
|
|
91
|
+
let d = Math.min(i, t.length - Math.floor(u / i) * i), f = (u % i - (d - 1) / 2) * a, p = ((l - 1) / 2 - Math.floor(u / i)) * o, m = r === "plane" ? 0 : f / s, h = r === "sphere" ? p / s : 0, g = m + (n.inward ? Math.PI : 0), _ = n.inward ? h : -h, v = Math.sin(g / 2), y = Math.cos(g / 2), b = Math.sin(_ / 2), x = Math.cos(_ / 2);
|
|
92
|
+
e.setPose([
|
|
93
|
+
c[0] + (r === "plane" ? f : s * Math.sin(m) * Math.cos(h)),
|
|
94
|
+
c[1] + (r === "sphere" ? s * Math.sin(h) : p),
|
|
95
|
+
c[2] + (r === "plane" ? 0 : s * (Math.cos(m) * Math.cos(h) - 1))
|
|
96
|
+
], [
|
|
97
|
+
y * b,
|
|
98
|
+
v * x,
|
|
99
|
+
-v * b,
|
|
100
|
+
y * x
|
|
101
|
+
]);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
export { n as CurvedPanel, r as layoutPanels };
|