@tioniq/sceniq 0.0.0-stage → 0.1.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/CHANGELOG.md +38 -0
- package/LICENSE +22 -0
- package/README.md +471 -2
- package/dist/chunk-BX7YQ44O.js +2470 -0
- package/dist/chunk-CVQBUUHT.js +1328 -0
- package/dist/chunk-HKPEAHCX.cjs +279 -0
- package/dist/chunk-J4XB4OPH.js +279 -0
- package/dist/chunk-KRCM4WYZ.cjs +2470 -0
- package/dist/chunk-OCHCBJJE.cjs +1328 -0
- package/dist/color-IY199Sua.d.cts +34 -0
- package/dist/color-IY199Sua.d.ts +34 -0
- package/dist/index.cjs +5636 -0
- package/dist/index.d.cts +1627 -0
- package/dist/index.d.ts +1627 -0
- package/dist/index.js +5636 -0
- package/dist/react/index.cjs +88 -0
- package/dist/react/index.d.cts +64 -0
- package/dist/react/index.d.ts +64 -0
- package/dist/react/index.js +88 -0
- package/dist/rect-bounds-Bd7-7ERA.d.cts +726 -0
- package/dist/rect-bounds-Bd7-7ERA.d.ts +726 -0
- package/dist/scene-77s46CRy.d.ts +590 -0
- package/dist/scene-Dd7ExNOl.d.cts +590 -0
- package/dist/svg/index.cjs +1122 -0
- package/dist/svg/index.d.cts +213 -0
- package/dist/svg/index.d.ts +213 -0
- package/dist/svg/index.js +1122 -0
- package/package.json +142 -4
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
import { EventObserver, VarOrVal, LazyEventDispatcher, Var, EqualityComparer, Vary, IVary } from '@tioniq/eventiq';
|
|
2
|
+
import { IDisposable, Disposiq, CanBeDisposable, DisposableLike } from '@tioniq/disposiq';
|
|
3
|
+
|
|
4
|
+
/** An immutable 2D point or vector. Operations return new points. */
|
|
5
|
+
declare class Point {
|
|
6
|
+
readonly x: number;
|
|
7
|
+
readonly y: number;
|
|
8
|
+
constructor(x: number, y: number);
|
|
9
|
+
get isZero(): boolean;
|
|
10
|
+
get isOne(): boolean;
|
|
11
|
+
get length(): number;
|
|
12
|
+
add(point: Point): Point;
|
|
13
|
+
subtract(point: Point): Point;
|
|
14
|
+
multiply(scalar: number | Point): Point;
|
|
15
|
+
divide(scalar: number | Point): Point;
|
|
16
|
+
distance(point: Point): number;
|
|
17
|
+
/** The point mirrored through the origin: `(-x, -y)`. */
|
|
18
|
+
negate(): Point;
|
|
19
|
+
/** The point with its coordinates swapped: `(y, x)`. */
|
|
20
|
+
swap(): Point;
|
|
21
|
+
shift(x: number, y: number): Point;
|
|
22
|
+
shift(point: Point): Point;
|
|
23
|
+
shiftTowards(point: Point, distance: number): Point;
|
|
24
|
+
floor(): Point;
|
|
25
|
+
ceil(): Point;
|
|
26
|
+
with(overrides?: Partial<Point>): Point;
|
|
27
|
+
distanceTo(other: Point): number;
|
|
28
|
+
normalize(): Point;
|
|
29
|
+
dot(point: Point): number;
|
|
30
|
+
equals(point: Point): boolean;
|
|
31
|
+
toString(): string;
|
|
32
|
+
static readonly zero: Point;
|
|
33
|
+
static readonly one: Point;
|
|
34
|
+
static fromDomPoint(domPoint: DOMPoint): Point;
|
|
35
|
+
static from(data: {
|
|
36
|
+
x: number;
|
|
37
|
+
y: number;
|
|
38
|
+
}): Point;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface SizeLike {
|
|
42
|
+
readonly width: number;
|
|
43
|
+
readonly height: number;
|
|
44
|
+
}
|
|
45
|
+
/** An immutable width and height. */
|
|
46
|
+
declare class Size implements SizeLike {
|
|
47
|
+
readonly width: number;
|
|
48
|
+
readonly height: number;
|
|
49
|
+
constructor(width: number, height: number);
|
|
50
|
+
get aspectRatio(): number;
|
|
51
|
+
get isLandscape(): boolean;
|
|
52
|
+
get isPortrait(): boolean;
|
|
53
|
+
get isSquare(): boolean;
|
|
54
|
+
get isWide(): boolean;
|
|
55
|
+
get isTall(): boolean;
|
|
56
|
+
get isZero(): boolean;
|
|
57
|
+
equals(other: Size): boolean;
|
|
58
|
+
scale(factor: number): Size;
|
|
59
|
+
scale(factor: Point): Size;
|
|
60
|
+
scale(factorX: number, factorY: number): Size;
|
|
61
|
+
add(size: Size): Size;
|
|
62
|
+
subtract(size: Size): Size;
|
|
63
|
+
toPoint(): Point;
|
|
64
|
+
with(overrides?: Partial<Size>): Size;
|
|
65
|
+
round(): Size;
|
|
66
|
+
withPadding(all: number): Size;
|
|
67
|
+
withPadding(vertical: number, horizontal: number): Size;
|
|
68
|
+
toString(): string;
|
|
69
|
+
static readonly zero: Size;
|
|
70
|
+
static fromPoint(point: Point): Size;
|
|
71
|
+
static fromPoints(p1: Point, p2: Point): Size;
|
|
72
|
+
static from(values: {
|
|
73
|
+
width: number;
|
|
74
|
+
height: number;
|
|
75
|
+
}): Size;
|
|
76
|
+
static square(size: number): Size;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A plain painter for a {@link Layer}: draws in the layer's coordinates and has no layout. Lighter
|
|
81
|
+
* than a {@link View}; use it for backgrounds, grids and other things that need no bounds, layout or
|
|
82
|
+
* input. Call {@link invalidate} when it must be drawn again.
|
|
83
|
+
*/
|
|
84
|
+
declare abstract class Drawable {
|
|
85
|
+
private readonly _onChanged;
|
|
86
|
+
get onChanged(): EventObserver;
|
|
87
|
+
canDraw(_context: ShouldDrawContext): boolean;
|
|
88
|
+
getVisibleBounds(_context: CanvasRenderingContext2D): RectBounds | null;
|
|
89
|
+
abstract draw(context: CanvasRenderingContext2D): void;
|
|
90
|
+
toCached(size?: Size): Drawable;
|
|
91
|
+
/** This drawable drawn through a blur filter. A variable radius is followed only while the result is drawn. */
|
|
92
|
+
toBlurred(radius: VarOrVal<number>): Drawable;
|
|
93
|
+
handleMouseAction(_action: PointerActionEvent): boolean;
|
|
94
|
+
protected invalidate(): void;
|
|
95
|
+
}
|
|
96
|
+
interface ShouldDrawContext {
|
|
97
|
+
readonly context: CanvasRenderingContext2D;
|
|
98
|
+
readonly visibleBounds: RectBounds;
|
|
99
|
+
}
|
|
100
|
+
declare abstract class DrawableContent {
|
|
101
|
+
private readonly _onChanged;
|
|
102
|
+
get onChanged(): EventObserver;
|
|
103
|
+
abstract get size(): Size | null;
|
|
104
|
+
abstract draw(context: CanvasRenderingContext2D): void;
|
|
105
|
+
protected invalidate(): void;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
type PointerKind = "mouse" | "touch" | "pen" | (string & {});
|
|
109
|
+
interface PointerObserveEvent {
|
|
110
|
+
readonly altKey: boolean;
|
|
111
|
+
readonly ctrlKey: boolean;
|
|
112
|
+
readonly shiftKey: boolean;
|
|
113
|
+
readonly metaKey: boolean;
|
|
114
|
+
/** The button that changed: 0 primary, 1 middle, 2 secondary; -1 for a move. */
|
|
115
|
+
readonly button: number;
|
|
116
|
+
/** The buttons held, as a bit mask (1 primary, 2 secondary, 4 middle). */
|
|
117
|
+
readonly buttons: number;
|
|
118
|
+
/** Position in CSS pixels from the observed element's top-left (inside its border). */
|
|
119
|
+
readonly viewportPosition: Point;
|
|
120
|
+
/** How far this pointer moved since its previous event, in CSS pixels. */
|
|
121
|
+
readonly movement: Point;
|
|
122
|
+
readonly pointerId: number;
|
|
123
|
+
readonly pointerType: PointerKind;
|
|
124
|
+
/** Whether this is the first pointer of a multi-touch gesture, or the mouse. */
|
|
125
|
+
readonly isPrimary: boolean;
|
|
126
|
+
readonly pressure: number;
|
|
127
|
+
/** Stops the browser's own handling of the underlying DOM event. */
|
|
128
|
+
preventDefault(): void;
|
|
129
|
+
}
|
|
130
|
+
interface WheelObserveEvent {
|
|
131
|
+
readonly altKey: boolean;
|
|
132
|
+
readonly ctrlKey: boolean;
|
|
133
|
+
readonly shiftKey: boolean;
|
|
134
|
+
readonly metaKey: boolean;
|
|
135
|
+
readonly viewportPosition: Point;
|
|
136
|
+
readonly delta: Point;
|
|
137
|
+
/** `WheelEvent.deltaMode`: 0 pixels, 1 lines, 2 pages. */
|
|
138
|
+
readonly deltaMode: number;
|
|
139
|
+
/**
|
|
140
|
+
* Stops the page from scrolling or zooming. Wheel events are not prevented by default, so a
|
|
141
|
+
* canvas that does not handle the wheel leaves the page scrollable; whoever handles one calls this.
|
|
142
|
+
*/
|
|
143
|
+
preventDefault(): void;
|
|
144
|
+
}
|
|
145
|
+
interface AddExternalElementOptions {
|
|
146
|
+
/** Forward pointer events (default true). */
|
|
147
|
+
mouseEvents?: boolean;
|
|
148
|
+
/** Forward wheel events (default true). */
|
|
149
|
+
wheelEvents?: boolean;
|
|
150
|
+
}
|
|
151
|
+
interface PointerObserverOptions {
|
|
152
|
+
/**
|
|
153
|
+
* Captures a pointer on the element when it goes down, so a drag that leaves the element keeps
|
|
154
|
+
* sending moves and its release until the pointer goes up. Default true.
|
|
155
|
+
*/
|
|
156
|
+
readonly capture?: boolean;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Pointer input of one element: mouse, touch and pen through Pointer Events, with pointer capture,
|
|
160
|
+
* plus the wheel and the context menu. Listeners are attached only while someone subscribes.
|
|
161
|
+
*/
|
|
162
|
+
declare class PointerObserver implements IDisposable {
|
|
163
|
+
private readonly _element;
|
|
164
|
+
/** Every DOM listener attached, so {@link dispose} can remove them whoever still subscribes. */
|
|
165
|
+
private readonly _listeners;
|
|
166
|
+
private readonly _capture;
|
|
167
|
+
private readonly _activePointers;
|
|
168
|
+
private readonly _lastPositions;
|
|
169
|
+
private _coreUsers;
|
|
170
|
+
private _coreSubscription;
|
|
171
|
+
readonly onPointerDown: LazyEventDispatcher<PointerObserveEvent>;
|
|
172
|
+
readonly onPointerMove: LazyEventDispatcher<PointerObserveEvent>;
|
|
173
|
+
readonly onPointerUp: LazyEventDispatcher<PointerObserveEvent>;
|
|
174
|
+
/** The browser took the pointer away (a scroll gesture, a lost capture, the window losing focus). */
|
|
175
|
+
readonly onPointerCancel: LazyEventDispatcher<PointerObserveEvent>;
|
|
176
|
+
readonly onPointerEnter: LazyEventDispatcher<PointerObserveEvent>;
|
|
177
|
+
readonly onPointerLeave: LazyEventDispatcher<PointerObserveEvent>;
|
|
178
|
+
readonly onWheel: LazyEventDispatcher<WheelObserveEvent>;
|
|
179
|
+
/** Subscribing prevents the browser's context menu on the element. */
|
|
180
|
+
readonly onContextMenu: LazyEventDispatcher<PointerObserveEvent>;
|
|
181
|
+
constructor(element: HTMLElement, options?: PointerObserverOptions);
|
|
182
|
+
/** Whether {@link dispose} was called. */
|
|
183
|
+
get disposed(): boolean;
|
|
184
|
+
/**
|
|
185
|
+
* Removes every DOM listener, including those kept by subscribers that never unsubscribed (a
|
|
186
|
+
* controller whose disposable was dropped); nothing is raised afterwards. The scene calls it.
|
|
187
|
+
*/
|
|
188
|
+
dispose(): void;
|
|
189
|
+
/** Adds a DOM listener that {@link dispose} removes; none once disposed. */
|
|
190
|
+
private _listen;
|
|
191
|
+
get element(): HTMLElement;
|
|
192
|
+
/** The pointers currently down (or touching), by id, at their last position. */
|
|
193
|
+
get activePointers(): ReadonlyMap<number, Point>;
|
|
194
|
+
/**
|
|
195
|
+
* Forwards pointer and wheel events of an element laid over this one (a toolbar, a label) as if
|
|
196
|
+
* they happened on this element. Events outside this element's box are dropped.
|
|
197
|
+
*/
|
|
198
|
+
addExternalElement(element: HTMLElement, options?: AddExternalElementOptions): Disposiq;
|
|
199
|
+
/** Raises a leave as if the pointer left the element, for example when an overlay covers it. */
|
|
200
|
+
simulateMouseLeave(): void;
|
|
201
|
+
/** A dispatcher whose subscription keeps the shared pointer listeners attached. */
|
|
202
|
+
private _createCoreDispatcher;
|
|
203
|
+
private _acquireCore;
|
|
204
|
+
private _releaseCore;
|
|
205
|
+
private _movement;
|
|
206
|
+
/** The event's position from the element's content box, where a canvas shows its bitmap. */
|
|
207
|
+
private _viewportPosition;
|
|
208
|
+
private _isInside;
|
|
209
|
+
private _convertMouseEvent;
|
|
210
|
+
private _convertWheelEvent;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
type PointerActionEventType = "down" | "up" | "move" | "enter" | "leave";
|
|
214
|
+
type PointerActionTarget = View | Drawable | DrawableContent | null | undefined;
|
|
215
|
+
/**
|
|
216
|
+
* Pointer input as views receive it. `viewportPosition` is in the surface's device pixels and
|
|
217
|
+
* `position` in the coordinates of the layer being hit-tested (the camera already undone). A view
|
|
218
|
+
* that claims the event sets `target`, and may set `cursor`; both are shared with the event it was
|
|
219
|
+
* derived from.
|
|
220
|
+
*/
|
|
221
|
+
interface PointerActionEvent {
|
|
222
|
+
readonly type: PointerActionEventType;
|
|
223
|
+
readonly context: CanvasRenderingContext2D;
|
|
224
|
+
readonly altKey: boolean;
|
|
225
|
+
readonly ctrlKey: boolean;
|
|
226
|
+
readonly shiftKey: boolean;
|
|
227
|
+
readonly metaKey: boolean;
|
|
228
|
+
readonly button: number;
|
|
229
|
+
readonly viewportPosition: Point;
|
|
230
|
+
readonly movement: Point;
|
|
231
|
+
readonly position: Point;
|
|
232
|
+
readonly pointerId: number;
|
|
233
|
+
readonly pointerType: PointerKind;
|
|
234
|
+
cursor: string | null | undefined;
|
|
235
|
+
target: PointerActionTarget;
|
|
236
|
+
/**
|
|
237
|
+
* How far, in the surface's device pixels, a pointer may move between going down and up and still
|
|
238
|
+
* click. The scene sets it from its `clickSlop` (CSS pixels) and the device pixel ratio; default 5.
|
|
239
|
+
*/
|
|
240
|
+
readonly clickSlop: number;
|
|
241
|
+
/**
|
|
242
|
+
* Delivers a click: at once, or, when the scene holds clicks back to tell them from double-clicks
|
|
243
|
+
* (`clickDelay`), once no second press followed; the clicks of a double-click are not delivered.
|
|
244
|
+
* Views call it for their `onClick`; a custom view raising its own click events should too.
|
|
245
|
+
*/
|
|
246
|
+
deliverClick(dispatch: () => void): void;
|
|
247
|
+
with(overrides: {
|
|
248
|
+
readonly context?: CanvasRenderingContext2D;
|
|
249
|
+
readonly viewportPosition?: Point;
|
|
250
|
+
readonly type?: PointerActionEventType;
|
|
251
|
+
/** Overrides the position computed from the context (a view mapping into its own transform). */
|
|
252
|
+
readonly position?: Point;
|
|
253
|
+
}): PointerActionEvent;
|
|
254
|
+
}
|
|
255
|
+
type ObservedPointer = Omit<PointerObserveEvent, "pointerId" | "pointerType" | "isPrimary" | "pressure" | "buttons" | "preventDefault"> & Partial<Pick<PointerObserveEvent, "pointerId" | "pointerType">>;
|
|
256
|
+
declare function createPointerActionEvent(params: {
|
|
257
|
+
context: CanvasRenderingContext2D;
|
|
258
|
+
type: PointerActionEventType;
|
|
259
|
+
observeEvent: ObservedPointer;
|
|
260
|
+
viewportPosition: Point;
|
|
261
|
+
position?: Point;
|
|
262
|
+
parent?: PointerActionEvent | null;
|
|
263
|
+
/** See {@link PointerActionEvent.clickSlop}; taken from `parent` when it is given. Default 5. */
|
|
264
|
+
clickSlop?: number;
|
|
265
|
+
/** Where clicks go; see {@link PointerActionEvent.deliverClick}. Taken from `parent`. Default: at once. */
|
|
266
|
+
deliverClick?: (dispatch: () => void) => void;
|
|
267
|
+
}): PointerActionEvent;
|
|
268
|
+
|
|
269
|
+
type ViewSize = number | "max" | "min";
|
|
270
|
+
/**
|
|
271
|
+
* How big a view wants to be, per axis: a number of units, `"min"` (as small as its content, like
|
|
272
|
+
* CSS `fit-content`) or `"max"` (as large as its parent allows, like `stretch`), within a minimum and a
|
|
273
|
+
* maximum. A parent narrows its children's boxes with {@link constrainBy} before measuring them.
|
|
274
|
+
*
|
|
275
|
+
* Usual boxes: {@link strictSize} (a fixed size), {@link wrap} (fit the content), {@link stretch}
|
|
276
|
+
* (fill the parent) and {@link fromOptional} (a size with optional limits).
|
|
277
|
+
*/
|
|
278
|
+
declare class ViewBox {
|
|
279
|
+
static readonly min: ViewBox;
|
|
280
|
+
static readonly max: ViewBox;
|
|
281
|
+
static readonly zero: ViewBox;
|
|
282
|
+
readonly minWidth: number;
|
|
283
|
+
readonly minHeight: number;
|
|
284
|
+
readonly width: ViewSize;
|
|
285
|
+
readonly height: ViewSize;
|
|
286
|
+
readonly maxWidth: number;
|
|
287
|
+
readonly maxHeight: number;
|
|
288
|
+
constructor(width: ViewSize, height: ViewSize, minWidth: number, minHeight: number, maxWidth: number, maxHeight: number);
|
|
289
|
+
getRealWidth(preferred?: number): number;
|
|
290
|
+
getRealHeight(preferred?: number): number;
|
|
291
|
+
getRealSize(preferred?: Size): Size;
|
|
292
|
+
cut(width: number, height: number): ViewBox;
|
|
293
|
+
with(overrides: {
|
|
294
|
+
width?: ViewSize;
|
|
295
|
+
height?: ViewSize;
|
|
296
|
+
minWidth?: number;
|
|
297
|
+
minHeight?: number;
|
|
298
|
+
maxWidth?: number;
|
|
299
|
+
maxHeight?: number;
|
|
300
|
+
}): ViewBox;
|
|
301
|
+
constrainBy(container: ViewBox): ViewBox;
|
|
302
|
+
equals(other: ViewBox): boolean;
|
|
303
|
+
static strict(width: ViewSize, height: ViewSize): ViewBox;
|
|
304
|
+
static stretch(minWidth?: number, minHeight?: number): ViewBox;
|
|
305
|
+
static wrap(minWidth?: number, minHeight?: number): ViewBox;
|
|
306
|
+
static strictSize(size: Size): ViewBox;
|
|
307
|
+
static wrapSize(size: Size): ViewBox;
|
|
308
|
+
static simple(width: ViewSize, height: ViewSize): ViewBox;
|
|
309
|
+
static fromOptionalObject(box: {
|
|
310
|
+
width: ViewSize;
|
|
311
|
+
height: ViewSize;
|
|
312
|
+
minWidth?: number;
|
|
313
|
+
minHeight?: number;
|
|
314
|
+
maxWidth?: number;
|
|
315
|
+
maxHeight?: number;
|
|
316
|
+
}): ViewBox;
|
|
317
|
+
static fromOptional(width: ViewSize, height: ViewSize, minWidth?: number, minHeight?: number, maxWidth?: number, maxHeight?: number): ViewBox;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* What a parent hands a child to lay it out: the drawing context (for measuring), the box the child
|
|
322
|
+
* may take, where it lies in scene coordinates, and the part of the scene on screen.
|
|
323
|
+
*/
|
|
324
|
+
declare class LayoutContext {
|
|
325
|
+
private readonly _context;
|
|
326
|
+
private readonly _viewBox;
|
|
327
|
+
private readonly _offset;
|
|
328
|
+
private readonly _screenBounds;
|
|
329
|
+
constructor(context: CanvasRenderingContext2D, viewBox: ViewBox, offset: Point, screenBounds: RectBounds);
|
|
330
|
+
get context(): CanvasRenderingContext2D;
|
|
331
|
+
get viewBox(): ViewBox;
|
|
332
|
+
get offset(): Point;
|
|
333
|
+
get screenBounds(): RectBounds;
|
|
334
|
+
isVisible(bounds: RectBounds): boolean;
|
|
335
|
+
static create(context: CanvasRenderingContext2D): LayoutContext;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* What {@link LiveObject.handleSubscribed} may return: something to dispose when the object stops
|
|
340
|
+
* being live, or nothing.
|
|
341
|
+
*/
|
|
342
|
+
type SubscribedResult = CanBeDisposable | null | undefined | void;
|
|
343
|
+
/**
|
|
344
|
+
* An object whose properties are eventiq variables and which raises {@link onChanged} when any of
|
|
345
|
+
* them changes. It subscribes to its variables only while someone subscribes to `onChanged`, and
|
|
346
|
+
* drops those subscriptions when the last subscriber leaves: a view in a layer is live, a view taken
|
|
347
|
+
* out is inert and can be collected.
|
|
348
|
+
*
|
|
349
|
+
* Subclasses declare their state with {@link createVar} (a variable of their own), {@link createVary}
|
|
350
|
+
* (a value or someone else's variable, followed), {@link registerVar} and {@link registerEvent}.
|
|
351
|
+
*/
|
|
352
|
+
declare class LiveObject {
|
|
353
|
+
/**
|
|
354
|
+
* What raises onChanged while the object is live: variables (followed silently) and events. Kept as
|
|
355
|
+
* the objects themselves, with one shared callback, rather than two closures each: a view has a
|
|
356
|
+
* dozen variables and there may be tens of thousands of views.
|
|
357
|
+
*/
|
|
358
|
+
private readonly _vars;
|
|
359
|
+
private readonly _events;
|
|
360
|
+
private readonly _custom;
|
|
361
|
+
private readonly _invalidatorDisposables;
|
|
362
|
+
private readonly _onChanged;
|
|
363
|
+
private readonly _dispatch;
|
|
364
|
+
constructor();
|
|
365
|
+
/** Raised when any registered variable or event changes. Subscribing makes the object live. */
|
|
366
|
+
get onChanged(): EventObserver;
|
|
367
|
+
protected invalidate(): void;
|
|
368
|
+
protected handleSubscribed(): SubscribedResult;
|
|
369
|
+
protected handleUnsubscribed(): void;
|
|
370
|
+
/** Follows an existing variable: a change of it raises {@link onChanged}. */
|
|
371
|
+
protected registerVar<T, TVar extends Var<T>>(variable: TVar): TVar;
|
|
372
|
+
/** A new variable owned by this object; setting it raises {@link onChanged}. */
|
|
373
|
+
protected createVar<T>(value: T, equalityComparer?: EqualityComparer<T>): Vary<T>;
|
|
374
|
+
protected createVar<T extends {
|
|
375
|
+
equals(other: T): boolean;
|
|
376
|
+
}>(value: T, useEqualsMethodOrProvideEqualityComparer?: true | EqualityComparer<T>): Vary<T>;
|
|
377
|
+
/**
|
|
378
|
+
* A variable for an option that may be a plain value or a variable: a plain value starts a variable
|
|
379
|
+
* of its own, a variable is followed while this object is live. Either way a change raises
|
|
380
|
+
* {@link onChanged}.
|
|
381
|
+
*/
|
|
382
|
+
protected createVary<T>(value: VarOrVal<T>, equalityComparer?: EqualityComparer<T>): IVary<T>;
|
|
383
|
+
protected createVary<T extends {
|
|
384
|
+
equals(other: T): boolean;
|
|
385
|
+
}>(value: VarOrVal<T>, useEqualsMethodOrProvideEqualityComparer?: true | EqualityComparer<T>): IVary<T>;
|
|
386
|
+
protected registerEvent<T, TObserver extends EventObserver<T>>(event: TObserver): TObserver;
|
|
387
|
+
protected registerTempEvent<T, TObserver extends EventObserver<T>>(event: TObserver): Disposiq;
|
|
388
|
+
private _addVar;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
interface ViewOptions {
|
|
392
|
+
readonly enabled?: VarOrVal<boolean>;
|
|
393
|
+
readonly visible?: VarOrVal<boolean>;
|
|
394
|
+
readonly location?: VarOrVal<Point>;
|
|
395
|
+
readonly rotation?: VarOrVal<number>;
|
|
396
|
+
readonly scale?: VarOrVal<number>;
|
|
397
|
+
readonly hoverScale?: VarOrVal<number>;
|
|
398
|
+
readonly hoverCursor?: VarOrVal<string | null>;
|
|
399
|
+
readonly hittable?: VarOrVal<boolean>;
|
|
400
|
+
readonly alpha?: VarOrVal<number>;
|
|
401
|
+
readonly viewBox?: ViewBox;
|
|
402
|
+
readonly size?: Size;
|
|
403
|
+
readonly onClick?: (event: ViewClickEvent) => void;
|
|
404
|
+
readonly onMounted?: () => DisposableLike;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* The base of everything a `Layer` lays out, draws and hit-tests: a rectangle with a location,
|
|
408
|
+
* a size given by its {@link ViewBox}, alpha, scale and rotation, hover and press state, and click
|
|
409
|
+
* events.
|
|
410
|
+
*
|
|
411
|
+
* Every property is an eventiq variable. Options take either a plain value or a variable; a view
|
|
412
|
+
* bound to a variable follows it, and a change marks the view changed, so its layer redraws on the
|
|
413
|
+
* next frame. A view subscribes to its variables only while it is in a layer (see {@link LiveObject}),
|
|
414
|
+
* so a view that is no longer shown holds no subscriptions.
|
|
415
|
+
*
|
|
416
|
+
* Each frame a layer calls {@link getDrawState} (layout: where the view is, how big, whether it is on
|
|
417
|
+
* screen), then {@link draw}. A custom view usually overrides {@link doDraw}, which draws in the view's
|
|
418
|
+
* own coordinates with its location, scale, rotation and alpha already applied, and perhaps
|
|
419
|
+
* {@link doGetDrawBounds} and {@link hitTest}:
|
|
420
|
+
*
|
|
421
|
+
* ```ts
|
|
422
|
+
* class Dot extends View {
|
|
423
|
+
* protected override doDraw(context: CanvasRenderingContext2D, state: DrawState) {
|
|
424
|
+
* const { width, height } = state.localBounds
|
|
425
|
+
* context.beginPath()
|
|
426
|
+
* context.arc(width / 2, height / 2, width / 2, 0, Math.PI * 2)
|
|
427
|
+
* context.fill()
|
|
428
|
+
* }
|
|
429
|
+
* }
|
|
430
|
+
* layer.add(new Dot({ location: new Point(10, 10), size: new Size(8, 8) }))
|
|
431
|
+
* ```
|
|
432
|
+
*
|
|
433
|
+
* A view object costs a few kilobytes, so tens of thousands of small shapes are better drawn by a
|
|
434
|
+
* few custom views that draw many shapes each.
|
|
435
|
+
*/
|
|
436
|
+
declare class View extends LiveObject {
|
|
437
|
+
private readonly _onMounted;
|
|
438
|
+
private readonly _onClick;
|
|
439
|
+
private readonly _visible;
|
|
440
|
+
private readonly _enabled;
|
|
441
|
+
private readonly _hovered;
|
|
442
|
+
private readonly _pressed;
|
|
443
|
+
private readonly _location;
|
|
444
|
+
private _alphaValue;
|
|
445
|
+
private _alphaVar;
|
|
446
|
+
private _rotationValue;
|
|
447
|
+
private _rotationVar;
|
|
448
|
+
private _scaleValue;
|
|
449
|
+
private _scaleVar;
|
|
450
|
+
private _hoverScaleValue;
|
|
451
|
+
private _hoverScaleVar;
|
|
452
|
+
private _hoverCursorValue;
|
|
453
|
+
private _hoverCursorVar;
|
|
454
|
+
private _hittableValue;
|
|
455
|
+
private _hittableVar;
|
|
456
|
+
private readonly _viewBox;
|
|
457
|
+
private readonly _attached;
|
|
458
|
+
/** Whether hover and press changes repaint the view; see {@link repaintOnPointerState}. */
|
|
459
|
+
private _pointerStateRepaints;
|
|
460
|
+
constructor(options?: ViewOptions | null);
|
|
461
|
+
/**
|
|
462
|
+
* Makes changes of {@link hovered} and {@link pressed} repaint the view's layer. Without it, hovering
|
|
463
|
+
* a view costs nothing to draw. It is turned on by itself for a view with a `hoverScale`, and for one
|
|
464
|
+
* whose layout or drawing reads `hovered` or `pressed`; call it from a subclass that draws its hover
|
|
465
|
+
* state some other way.
|
|
466
|
+
*/
|
|
467
|
+
protected repaintOnPointerState(): void;
|
|
468
|
+
protected handleSubscribed(): SubscribedResult;
|
|
469
|
+
protected handleUnsubscribed(): void;
|
|
470
|
+
get visible(): Var<boolean>;
|
|
471
|
+
set visible(value: boolean);
|
|
472
|
+
get enabled(): Var<boolean>;
|
|
473
|
+
set enabled(value: boolean);
|
|
474
|
+
get location(): Var<Point>;
|
|
475
|
+
set location(value: Point);
|
|
476
|
+
get viewBox(): Var<ViewBox>;
|
|
477
|
+
set viewBox(value: ViewBox);
|
|
478
|
+
/** Opacity from 0 to 1, multiplied into the parent's; values outside are clamped. */
|
|
479
|
+
get alpha(): Var<number>;
|
|
480
|
+
set alpha(value: number);
|
|
481
|
+
/** Rotation in radians about the centre of the view's bounds. */
|
|
482
|
+
get rotation(): Var<number>;
|
|
483
|
+
set rotation(value: number);
|
|
484
|
+
/** Uniform scale about the centre of the view's bounds. */
|
|
485
|
+
get scale(): Var<number>;
|
|
486
|
+
set scale(value: number);
|
|
487
|
+
/** The scale applied on top of {@link scale} while hovered. */
|
|
488
|
+
get hoverScale(): Var<number>;
|
|
489
|
+
set hoverScale(value: number);
|
|
490
|
+
/** Whether the view takes pointer input. */
|
|
491
|
+
get hittable(): Var<boolean>;
|
|
492
|
+
set hittable(value: boolean);
|
|
493
|
+
/** The cursor shown while the pointer is over the view; null leaves it to the scene. */
|
|
494
|
+
get hoverCursor(): Var<string | null>;
|
|
495
|
+
set hoverCursor(value: string | null);
|
|
496
|
+
private get _alphaNow();
|
|
497
|
+
private get _rotationNow();
|
|
498
|
+
private get _scaleNow();
|
|
499
|
+
private get _hoverScaleNow();
|
|
500
|
+
/** Whether the pointer is over the view. Read while laying out or drawing, its changes repaint the view. */
|
|
501
|
+
get hovered(): Var<boolean>;
|
|
502
|
+
/** Whether the primary button is down on the view. Read while laying out or drawing, its changes repaint the view. */
|
|
503
|
+
get pressed(): Var<boolean>;
|
|
504
|
+
get attached(): Var<boolean>;
|
|
505
|
+
get onClick(): EventObserver<ViewClickEvent>;
|
|
506
|
+
/**
|
|
507
|
+
* Lays the view out in `context`: its bounds in its parent's coordinates and whether it is on
|
|
508
|
+
* screen. Null when the view is hidden. Layouts override this to place their children.
|
|
509
|
+
*/
|
|
510
|
+
getDrawState(context: LayoutContext): DrawState | null;
|
|
511
|
+
getDrawBounds(measuredBox: ViewBox): RectBounds;
|
|
512
|
+
protected _getDrawBounds(viewBox: ViewBox): RectBounds;
|
|
513
|
+
/** The view's size within the box its parent offers, before its location is added. */
|
|
514
|
+
protected doGetDrawBounds(measuredBox: ViewBox): RectBounds;
|
|
515
|
+
/**
|
|
516
|
+
* Draws the view as laid out in `state`: applies its location, scale, rotation and alpha, calls
|
|
517
|
+
* {@link doDraw}, and restores the context.
|
|
518
|
+
*/
|
|
519
|
+
draw(context: CanvasRenderingContext2D, state: DrawState): void;
|
|
520
|
+
private _downAction;
|
|
521
|
+
handleMouseLeave(): void;
|
|
522
|
+
/**
|
|
523
|
+
* The event with its position mapped into this view's untransformed frame: undoes the scale
|
|
524
|
+
* (including the hover scale while hovered) and the rotation that {@link draw} applies about the
|
|
525
|
+
* centre of the view's bounds. The same event when the view is not transformed.
|
|
526
|
+
*/
|
|
527
|
+
protected toLocalAction(action: PointerActionEvent, state: DrawState): PointerActionEvent;
|
|
528
|
+
handleMouseAction(action: PointerActionEvent, state: DrawState): boolean;
|
|
529
|
+
/**
|
|
530
|
+
* Draws the view's content in its own coordinates: `(0, 0)` is its top-left corner and
|
|
531
|
+
* `state.localBounds.size` its size. Override this in a custom view.
|
|
532
|
+
*/
|
|
533
|
+
protected doDraw(_context: CanvasRenderingContext2D, _state: DrawState): void;
|
|
534
|
+
/**
|
|
535
|
+
* Whether a pointer inside the view's bounds hits it; `context.position` is relative to the view's
|
|
536
|
+
* parent, like its location. Default true: the whole rectangle.
|
|
537
|
+
*/
|
|
538
|
+
protected hitTest(_context: HitTestContext): boolean;
|
|
539
|
+
/**
|
|
540
|
+
* How far outside its bounds, in its own units, a pointer may still reach {@link hitTest}: room for
|
|
541
|
+
* a hit tolerance around thin shapes. Default 0.
|
|
542
|
+
*/
|
|
543
|
+
protected hitSlop(_action: PointerActionEvent): number;
|
|
544
|
+
}
|
|
545
|
+
declare function inverseRotateScale(point: Point, pivotX: number, pivotY: number, rotation: number, scale: number): Point;
|
|
546
|
+
interface HitTestContext {
|
|
547
|
+
readonly context: CanvasRenderingContext2D;
|
|
548
|
+
readonly viewportPosition: Point;
|
|
549
|
+
/** The pointer, in the coordinates of the view's parent (like its location). */
|
|
550
|
+
readonly position: Point;
|
|
551
|
+
/** The view's bounds as laid out, in its parent's coordinates: where it lies and how big it is. */
|
|
552
|
+
readonly localBounds: RectBounds;
|
|
553
|
+
}
|
|
554
|
+
interface ViewClickEvent {
|
|
555
|
+
readonly target: View;
|
|
556
|
+
readonly position: Point;
|
|
557
|
+
readonly viewportPosition: Point;
|
|
558
|
+
readonly localBounds: RectBounds;
|
|
559
|
+
readonly localPosition: Point;
|
|
560
|
+
}
|
|
561
|
+
declare function updateOptions<T extends ViewOptions, TParentOptions extends ViewOptions = ViewOptions>(options: T | null | undefined, change: TParentOptions | ((options: T) => TParentOptions)): TParentOptions | undefined;
|
|
562
|
+
interface DrawState {
|
|
563
|
+
readonly view: View;
|
|
564
|
+
readonly globalOffset: Point;
|
|
565
|
+
readonly localBounds: RectBounds;
|
|
566
|
+
readonly measuredBox: ViewBox;
|
|
567
|
+
readonly visibleInScreen: boolean;
|
|
568
|
+
readonly screenBounds: RectBounds;
|
|
569
|
+
readonly children?: DrawState[];
|
|
570
|
+
readonly childrenOffsets?: Point[];
|
|
571
|
+
shiftGlobal(offset: Point): DrawState;
|
|
572
|
+
}
|
|
573
|
+
/** The same state naming `view`; parents draw `state.view`, so a wrapper passes it its own. */
|
|
574
|
+
declare function retargetDrawState(state: DrawState, view: View): DrawState;
|
|
575
|
+
|
|
576
|
+
interface SymmetricIndent {
|
|
577
|
+
horizontal: number;
|
|
578
|
+
vertical: number;
|
|
579
|
+
}
|
|
580
|
+
type IndentOptions = number | Partial<Indent> | Partial<SymmetricIndent>;
|
|
581
|
+
type Padding = IndentOptions;
|
|
582
|
+
type Align = "start" | "center" | "end";
|
|
583
|
+
interface LayoutOptions extends ViewOptions {
|
|
584
|
+
children?: View[];
|
|
585
|
+
padding?: Padding;
|
|
586
|
+
background?: View | null;
|
|
587
|
+
verticalAlign?: VarOrVal<Align>;
|
|
588
|
+
horizontalAlign?: VarOrVal<Align>;
|
|
589
|
+
clickable?: VarOrVal<boolean>;
|
|
590
|
+
interceptMotionEvents?: VarOrVal<boolean>;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* A view that holds other views. The base layout stacks its children at its own origin (plus their
|
|
594
|
+
* locations and its padding), sizes itself to the largest of them, and aligns them with
|
|
595
|
+
* `horizontalAlign` and `verticalAlign` when it is larger. A `background` view is laid out at the size
|
|
596
|
+
* of the content box (inside the padding) and drawn first.
|
|
597
|
+
*
|
|
598
|
+
* Children are drawn in the order given, and hit-tested from the last to the first, in the layout's
|
|
599
|
+
* own scale and rotation. A layout takes the pointer itself only when `clickable` is true. See
|
|
600
|
+
* {@link LinearLayout} for rows and columns.
|
|
601
|
+
*/
|
|
602
|
+
declare class Layout extends View {
|
|
603
|
+
protected readonly _children: View[];
|
|
604
|
+
protected readonly _padding: IndentImpl;
|
|
605
|
+
protected readonly _verticalAlign: IVary<Align>;
|
|
606
|
+
protected readonly _horizontalAlign: IVary<Align>;
|
|
607
|
+
protected readonly _clickable: IVary<boolean>;
|
|
608
|
+
protected readonly _interceptMotionEvents: IVary<boolean>;
|
|
609
|
+
protected _background: View | null;
|
|
610
|
+
/** The children given to the constructor, followed while the layout is visible; see {@link changesWhileVisible}. */
|
|
611
|
+
private readonly _initialChildren;
|
|
612
|
+
/** What follows each child added later, disposed when it is removed. */
|
|
613
|
+
private readonly _childSubscriptions;
|
|
614
|
+
constructor(options?: LayoutOptions);
|
|
615
|
+
private _backgroundSubscription;
|
|
616
|
+
set background(value: View | null);
|
|
617
|
+
get background(): View | null;
|
|
618
|
+
get padding(): Indent;
|
|
619
|
+
get verticalAlign(): Var<Align>;
|
|
620
|
+
set verticalAlign(value: Align);
|
|
621
|
+
get horizontalAlign(): Var<Align>;
|
|
622
|
+
set horizontalAlign(value: Align);
|
|
623
|
+
addChild(child: View): Disposiq;
|
|
624
|
+
/** Removes a child, and with it everything this layout subscribed to on its behalf. */
|
|
625
|
+
removeChild(child: View): void;
|
|
626
|
+
private _followChild;
|
|
627
|
+
addChildren(children: View[]): Disposiq;
|
|
628
|
+
addChildrenToBegin(children: View[]): Disposiq;
|
|
629
|
+
private _followChildren;
|
|
630
|
+
getDrawState(context: LayoutContext): DrawState | null;
|
|
631
|
+
protected addBackgroundChildState(contentBoxSize: Size, context: LayoutContext, paddingOffset: Point, childrenState: DrawState[], childrenOffsets: Point[]): void;
|
|
632
|
+
protected doGetDrawBounds(measuredBox: ViewBox): RectBounds;
|
|
633
|
+
protected doDraw(context: CanvasRenderingContext2D, state: DrawState): void;
|
|
634
|
+
handleMouseAction(action: PointerActionEvent, state: DrawState): boolean;
|
|
635
|
+
protected hitTest(_context: HitTestContext): boolean;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
type Indent = {
|
|
639
|
+
left: number;
|
|
640
|
+
right: number;
|
|
641
|
+
top: number;
|
|
642
|
+
bottom: number;
|
|
643
|
+
};
|
|
644
|
+
declare class IndentImpl implements Indent {
|
|
645
|
+
private readonly _onChanged;
|
|
646
|
+
private _left;
|
|
647
|
+
private _right;
|
|
648
|
+
private _top;
|
|
649
|
+
private _bottom;
|
|
650
|
+
constructor(left: number, right: number, top: number, bottom: number);
|
|
651
|
+
get left(): number;
|
|
652
|
+
set left(value: number);
|
|
653
|
+
get right(): number;
|
|
654
|
+
set right(value: number);
|
|
655
|
+
get top(): number;
|
|
656
|
+
set top(value: number);
|
|
657
|
+
get bottom(): number;
|
|
658
|
+
set bottom(value: number);
|
|
659
|
+
get onChanged(): EventObserver;
|
|
660
|
+
set value(value: number);
|
|
661
|
+
get horizontal(): number;
|
|
662
|
+
get vertical(): number;
|
|
663
|
+
get size(): Size;
|
|
664
|
+
set horizontal(value: number);
|
|
665
|
+
set vertical(value: number);
|
|
666
|
+
copy(): IndentImpl;
|
|
667
|
+
static readonly zero: IndentImpl;
|
|
668
|
+
static from(indent: Indent): IndentImpl;
|
|
669
|
+
static all(value: number): IndentImpl;
|
|
670
|
+
static symmetric(horizontal: number, vertical: number): IndentImpl;
|
|
671
|
+
static horizontal(left: number, right: number): IndentImpl;
|
|
672
|
+
static vertical(top: number, bottom: number): IndentImpl;
|
|
673
|
+
static determine(indent?: IndentOptions): IndentImpl;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/** An immutable axis-aligned rectangle: `x`, `y`, `width`, `height`. */
|
|
677
|
+
declare class RectBounds {
|
|
678
|
+
x: number;
|
|
679
|
+
y: number;
|
|
680
|
+
width: number;
|
|
681
|
+
height: number;
|
|
682
|
+
constructor(x: number, y: number, width: number, height: number);
|
|
683
|
+
get left(): number;
|
|
684
|
+
get right(): number;
|
|
685
|
+
get top(): number;
|
|
686
|
+
get bottom(): number;
|
|
687
|
+
get size(): Size;
|
|
688
|
+
get area(): number;
|
|
689
|
+
get centerX(): number;
|
|
690
|
+
get centerY(): number;
|
|
691
|
+
get center(): Point;
|
|
692
|
+
get topLeft(): Point;
|
|
693
|
+
get topRight(): Point;
|
|
694
|
+
get bottomLeft(): Point;
|
|
695
|
+
get bottomRight(): Point;
|
|
696
|
+
contains(x: number, y: number): boolean;
|
|
697
|
+
contains(point: Point): boolean;
|
|
698
|
+
intersects(other: RectBounds): boolean;
|
|
699
|
+
union(other: RectBounds): RectBounds;
|
|
700
|
+
limit(bounds: RectBounds): RectBounds;
|
|
701
|
+
shift(x: number, y: number): RectBounds;
|
|
702
|
+
shift(point: Point): RectBounds;
|
|
703
|
+
scale(factor: number): RectBounds;
|
|
704
|
+
scale(xFactor: number, yFactor: number): RectBounds;
|
|
705
|
+
withPadding(padding: Indent): RectBounds;
|
|
706
|
+
withPadding(all: number): RectBounds;
|
|
707
|
+
withPadding(vertical: number, horizontal: number): RectBounds;
|
|
708
|
+
withSize(size: SizeLike): RectBounds;
|
|
709
|
+
addSize(size: SizeLike): RectBounds;
|
|
710
|
+
equals(other: RectBounds): boolean;
|
|
711
|
+
toString(): string;
|
|
712
|
+
static readonly zero: RectBounds;
|
|
713
|
+
static fromPoints(x1: number, y1: number, x2: number, y2: number): RectBounds;
|
|
714
|
+
static fromTwoPoints(point1: Point, point2: Point): RectBounds;
|
|
715
|
+
static fromCenter(centerX: number, centerY: number, width: number, height: number): RectBounds;
|
|
716
|
+
static fromBounds(bounds: {
|
|
717
|
+
x: number;
|
|
718
|
+
y: number;
|
|
719
|
+
width: number;
|
|
720
|
+
height: number;
|
|
721
|
+
}): RectBounds;
|
|
722
|
+
static from(location: Point, size: SizeLike): RectBounds;
|
|
723
|
+
static fromSize(size: SizeLike): RectBounds;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
export { type AddExternalElementOptions as A, updateOptions as B, type DrawState as D, type HitTestContext as H, type Indent as I, LayoutContext as L, Point as P, RectBounds as R, Size as S, View as V, type WheelObserveEvent as W, type PointerObserveEvent as a, type ViewOptions as b, ViewBox as c, type SubscribedResult as d, type PointerActionEvent as e, Layout as f, DrawableContent as g, type Padding as h, type LayoutOptions as i, Drawable as j, type Align as k, type IndentOptions as l, LiveObject as m, type PointerActionEventType as n, type PointerActionTarget as o, type PointerKind as p, PointerObserver as q, type PointerObserverOptions as r, type ShouldDrawContext as s, type SizeLike as t, type SymmetricIndent as u, type ViewClickEvent as v, type ViewSize as w, createPointerActionEvent as x, inverseRotateScale as y, retargetDrawState as z };
|