@driftengine/tools 4.0.0 → 4.1.1
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 +64 -2
- package/dist/index.d.ts +7 -3
- package/dist/index.js +4 -3
- package/dist/inspector.d.ts +42 -0
- package/dist/inspector.js +55 -0
- package/dist/network.d.ts +42 -0
- package/dist/network.js +43 -0
- package/dist/overlay.d.ts +70 -0
- package/dist/overlay.js +275 -0
- package/dist/profiler.d.ts +29 -1
- package/dist/profiler.js +40 -1
- package/package.json +6 -6
- package/src/index.ts +30 -0
- package/src/inspector.ts +77 -0
- package/src/network.ts +68 -0
- package/src/overlay.ts +342 -0
- package/src/profiler.ts +52 -1
package/dist/profiler.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { UiNode } from '@driftengine/ui2d';
|
|
2
|
-
import { type PassTimings } from '@driftengine/core';
|
|
2
|
+
import { type GpuSample, type PassTimings } from '@driftengine/core';
|
|
3
3
|
import { type ResidencyTable } from '@driftengine/texture';
|
|
4
4
|
import { type Panel } from './panel.ts';
|
|
5
5
|
export interface ProfilerRow {
|
|
@@ -54,3 +54,31 @@ export declare function createProfilerView(options: {
|
|
|
54
54
|
rowHeight?: number;
|
|
55
55
|
}): ProfilerView;
|
|
56
56
|
export declare const profilerPanel: Panel<ProfilerWorld, ProfilerView>;
|
|
57
|
+
/**
|
|
58
|
+
* A `PassTimings` labelled with the engine's own GPU brackets.
|
|
59
|
+
*
|
|
60
|
+
* **The engine fills no timings for anybody.** `PassTimings` is a container and
|
|
61
|
+
* `RendererApi.gpuTimer` is a source, and joining the two is the same twenty lines wherever it is
|
|
62
|
+
* written — the slots are `GPU_SLOTS` and never the game's, because a second backend measuring the
|
|
63
|
+
* same frame owes the same three numbers. So it is written here once rather than in every consumer
|
|
64
|
+
* that wants a frame readout, which is the argument that put the overlay in this package.
|
|
65
|
+
*
|
|
66
|
+
* Labelled and unsampled, so every row shows `—` until a frame resolves. That is the distinction
|
|
67
|
+
* `profilerRows` exists to keep: a bracket nothing has measured yet is not a bracket that ran in no
|
|
68
|
+
* time.
|
|
69
|
+
*/
|
|
70
|
+
export declare function createGpuPassTimings(): PassTimings;
|
|
71
|
+
/**
|
|
72
|
+
* Write one resolved sample over the three rows.
|
|
73
|
+
*
|
|
74
|
+
* **It resets first, and the first draft did not.** `recordPassSample` accumulates on purpose,
|
|
75
|
+
* because a pass may be sampled several times in one frame — a shadow pass runs per cascade — and
|
|
76
|
+
* the row a reader wants is what that pass cost altogether. A `GpuSample` is the opposite: it is
|
|
77
|
+
* one resolved frame's three totals, already summed by the timer. Adding it to what was there left
|
|
78
|
+
* the rows climbing forever, which reads as a leak in whatever the game last changed rather than as
|
|
79
|
+
* a defect in the readout. `resetPassTimings` is how the engine's own frame starts, and the labels
|
|
80
|
+
* survive it, which is why the rows do not flicker.
|
|
81
|
+
*
|
|
82
|
+
* A caller wanting an average across frames has `FrameHistory` for it.
|
|
83
|
+
*/
|
|
84
|
+
export declare function recordGpuSample(timings: PassTimings, sample: GpuSample): void;
|
package/dist/profiler.js
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* a frame it was making worse.
|
|
17
17
|
*/
|
|
18
18
|
import { addUiChild, createUiNode } from '@driftengine/ui2d';
|
|
19
|
-
import { passLabel, passMs } from '@driftengine/core';
|
|
19
|
+
import { GPU_SLOTS, createPassTimings, passLabel, passMs, recordPassLabel, recordPassSample, resetPassTimings, } from '@driftengine/core';
|
|
20
20
|
import { residentCount } from '@driftengine/texture';
|
|
21
21
|
import { createPanelRoot, emptyPanel } from './panel.js';
|
|
22
22
|
/** `—` for unmeasured. Two decimals, because a pass under a hundredth is not the one to look at. */
|
|
@@ -140,3 +140,42 @@ export const profilerPanel = {
|
|
|
140
140
|
return null;
|
|
141
141
|
},
|
|
142
142
|
};
|
|
143
|
+
/**
|
|
144
|
+
* A `PassTimings` labelled with the engine's own GPU brackets.
|
|
145
|
+
*
|
|
146
|
+
* **The engine fills no timings for anybody.** `PassTimings` is a container and
|
|
147
|
+
* `RendererApi.gpuTimer` is a source, and joining the two is the same twenty lines wherever it is
|
|
148
|
+
* written — the slots are `GPU_SLOTS` and never the game's, because a second backend measuring the
|
|
149
|
+
* same frame owes the same three numbers. So it is written here once rather than in every consumer
|
|
150
|
+
* that wants a frame readout, which is the argument that put the overlay in this package.
|
|
151
|
+
*
|
|
152
|
+
* Labelled and unsampled, so every row shows `—` until a frame resolves. That is the distinction
|
|
153
|
+
* `profilerRows` exists to keep: a bracket nothing has measured yet is not a bracket that ran in no
|
|
154
|
+
* time.
|
|
155
|
+
*/
|
|
156
|
+
export function createGpuPassTimings() {
|
|
157
|
+
const timings = createPassTimings(GPU_SLOTS.length);
|
|
158
|
+
for (let slot = 0; slot < GPU_SLOTS.length; slot += 1) {
|
|
159
|
+
recordPassLabel(timings, slot, GPU_SLOTS[slot] ?? '');
|
|
160
|
+
}
|
|
161
|
+
return timings;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Write one resolved sample over the three rows.
|
|
165
|
+
*
|
|
166
|
+
* **It resets first, and the first draft did not.** `recordPassSample` accumulates on purpose,
|
|
167
|
+
* because a pass may be sampled several times in one frame — a shadow pass runs per cascade — and
|
|
168
|
+
* the row a reader wants is what that pass cost altogether. A `GpuSample` is the opposite: it is
|
|
169
|
+
* one resolved frame's three totals, already summed by the timer. Adding it to what was there left
|
|
170
|
+
* the rows climbing forever, which reads as a leak in whatever the game last changed rather than as
|
|
171
|
+
* a defect in the readout. `resetPassTimings` is how the engine's own frame starts, and the labels
|
|
172
|
+
* survive it, which is why the rows do not flicker.
|
|
173
|
+
*
|
|
174
|
+
* A caller wanting an average across frames has `FrameHistory` for it.
|
|
175
|
+
*/
|
|
176
|
+
export function recordGpuSample(timings, sample) {
|
|
177
|
+
resetPassTimings(timings);
|
|
178
|
+
recordPassSample(timings, 0, sample.shadows);
|
|
179
|
+
recordPassSample(timings, 1, sample.reflection);
|
|
180
|
+
recordPassSample(timings, 2, sample.rest);
|
|
181
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@driftengine/tools",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.1",
|
|
4
4
|
"description": "The editor panels a shipped game can carry, and the command stack that makes their edits undoable",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
],
|
|
28
28
|
"sideEffects": false,
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@driftengine/core": "4.
|
|
31
|
-
"@driftengine/editor": "4.
|
|
32
|
-
"@driftengine/network": "4.
|
|
33
|
-
"@driftengine/texture": "4.
|
|
34
|
-
"@driftengine/ui2d": "4.
|
|
30
|
+
"@driftengine/core": "4.1.1",
|
|
31
|
+
"@driftengine/editor": "4.1.1",
|
|
32
|
+
"@driftengine/network": "4.1.1",
|
|
33
|
+
"@driftengine/texture": "4.1.1",
|
|
34
|
+
"@driftengine/ui2d": "4.1.1"
|
|
35
35
|
},
|
|
36
36
|
"author": "Drift Technologies",
|
|
37
37
|
"repository": {
|
package/src/index.ts
CHANGED
|
@@ -58,10 +58,12 @@ export type {
|
|
|
58
58
|
InspectorWorld,
|
|
59
59
|
RowKind,
|
|
60
60
|
} from './inspector.ts';
|
|
61
|
+
export type { EntityWorldLike, InspectableComponentType } from './inspector.ts';
|
|
61
62
|
export {
|
|
62
63
|
MIXED,
|
|
63
64
|
addComponentCommand,
|
|
64
65
|
createInspectorView,
|
|
66
|
+
entitiesInspectable,
|
|
65
67
|
fieldEditable,
|
|
66
68
|
inspectorPanel,
|
|
67
69
|
inspectorRows,
|
|
@@ -95,6 +97,7 @@ export {
|
|
|
95
97
|
export type { FrameHistory, ProfilerRow, ProfilerView, ProfilerWorld } from './profiler.ts';
|
|
96
98
|
export {
|
|
97
99
|
createFrameHistory,
|
|
100
|
+
createGpuPassTimings,
|
|
98
101
|
createProfilerView,
|
|
99
102
|
formatMs,
|
|
100
103
|
historyValues,
|
|
@@ -102,14 +105,41 @@ export {
|
|
|
102
105
|
profilerPanel,
|
|
103
106
|
profilerRows,
|
|
104
107
|
pushFrame,
|
|
108
|
+
recordGpuSample,
|
|
105
109
|
} from './profiler.ts';
|
|
106
110
|
|
|
107
111
|
export type { NetworkReadout, NetworkView, NetworkWorld, RollbackHistory } from './network.ts';
|
|
112
|
+
export type { SessionLike, SessionRecorder } from './network.ts';
|
|
108
113
|
export {
|
|
109
114
|
componentDivergence,
|
|
110
115
|
createNetworkView,
|
|
111
116
|
createRollbackHistory,
|
|
117
|
+
createSessionRecorder,
|
|
112
118
|
networkPanel,
|
|
119
|
+
observeSession,
|
|
113
120
|
pushRollback,
|
|
114
121
|
rollbackValues,
|
|
122
|
+
sessionReadout,
|
|
115
123
|
} from './network.ts';
|
|
124
|
+
|
|
125
|
+
/*
|
|
126
|
+
* What mounts the four panels over a running game, on a key.
|
|
127
|
+
*
|
|
128
|
+
* **The panels shipped in 4.0.0 with nothing to show them**, which is the gap this closes: every
|
|
129
|
+
* panel above builds a `UiNode` tree and stops there, and the dock, layout, painting and routing
|
|
130
|
+
* that turn a tree into something a person sees lived only in the editor application, which is
|
|
131
|
+
* private. A consumer wanting an in-game inspector had to write that half again, and the six
|
|
132
|
+
* writing it six times is the drift `AGENTS.md` opens by describing.
|
|
133
|
+
*
|
|
134
|
+
* `OverlayPainter` is four calls, so a host with a 2D context, a host drawing through a sprite
|
|
135
|
+
* pass and a host with no screen at all each satisfy it — which is the platform rule applied to
|
|
136
|
+
* drawing, and what keeps this testable with no graphics device anywhere near it.
|
|
137
|
+
*/
|
|
138
|
+
export type {
|
|
139
|
+
OverlayPainter,
|
|
140
|
+
OverlaySite,
|
|
141
|
+
PanelBinding,
|
|
142
|
+
ToolsOverlay,
|
|
143
|
+
ToolsOverlayOptions,
|
|
144
|
+
} from './overlay.ts';
|
|
145
|
+
export { bindPanel, createToolsOverlay, paintOverlay } from './overlay.ts';
|
package/src/inspector.ts
CHANGED
|
@@ -348,3 +348,80 @@ export const inspectorPanel: Panel<InspectorWorld, InspectorView> = {
|
|
|
348
348
|
return null;
|
|
349
349
|
},
|
|
350
350
|
};
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* The shape of a component type, as this package needs it.
|
|
354
|
+
*
|
|
355
|
+
* **Structural rather than imported.** `@driftengine/entities`' `ComponentType` satisfies it, and
|
|
356
|
+
* so does anything else a consumer describes its state with — which is the arrangement
|
|
357
|
+
* `@driftengine/drft` already has with `@driftengine/texture`'s DTEX types across a boundary the
|
|
358
|
+
* one package must not depend across. This package gains no dependency for the adapter below, so
|
|
359
|
+
* the release's place count does not move.
|
|
360
|
+
*/
|
|
361
|
+
export interface InspectableComponentType {
|
|
362
|
+
readonly name: string;
|
|
363
|
+
readonly schema: { readonly fields: readonly { readonly name: string; readonly type: string }[] };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** The five methods the adapter forwards to. `World` has all of them. */
|
|
367
|
+
export interface EntityWorldLike<T> {
|
|
368
|
+
has(entity: number, type: T): boolean;
|
|
369
|
+
add(entity: number, type: T): void;
|
|
370
|
+
remove(entity: number, type: T): boolean;
|
|
371
|
+
read(entity: number, type: T, field: string): unknown;
|
|
372
|
+
write(entity: number, type: T, field: string, value: unknown): void;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* An entity world, as something the inspector can read and edit.
|
|
377
|
+
*
|
|
378
|
+
* **Written once here because every consumer with a world was about to write it.** The inspector
|
|
379
|
+
* addresses a component by name, since a name is what a row carries; a world addresses it by the
|
|
380
|
+
* type object, since that is what indexes its stores. Bridging the two is seven forwarding methods
|
|
381
|
+
* and a lookup, identical wherever it is written.
|
|
382
|
+
*
|
|
383
|
+
* **A name with no type behind it does nothing rather than guessing.** A row built against a stale
|
|
384
|
+
* schema would otherwise write a field into a component the world does not have and report
|
|
385
|
+
* success, which is a silent loss of the edit a person just made and watched apply.
|
|
386
|
+
*
|
|
387
|
+
* `componentsOf` answers in the order the types were given, so a panel's rows do not reorder
|
|
388
|
+
* between frames.
|
|
389
|
+
*/
|
|
390
|
+
export function entitiesInspectable<T extends InspectableComponentType>(
|
|
391
|
+
world: EntityWorldLike<T>,
|
|
392
|
+
types: readonly T[],
|
|
393
|
+
): InspectableWorld {
|
|
394
|
+
const byName = new Map<string, T>();
|
|
395
|
+
for (const type of types) byName.set(type.name, type);
|
|
396
|
+
|
|
397
|
+
return {
|
|
398
|
+
componentsOf(entity: number): readonly string[] {
|
|
399
|
+
const out: string[] = [];
|
|
400
|
+
for (const type of types) if (world.has(entity, type)) out.push(type.name);
|
|
401
|
+
return out;
|
|
402
|
+
},
|
|
403
|
+
schemaOf(component: string): readonly { readonly name: string; readonly type: string }[] {
|
|
404
|
+
return byName.get(component)?.schema.fields ?? [];
|
|
405
|
+
},
|
|
406
|
+
read(entity: number, component: string, field: string): unknown {
|
|
407
|
+
const type = byName.get(component);
|
|
408
|
+
return type === undefined ? undefined : world.read(entity, type, field);
|
|
409
|
+
},
|
|
410
|
+
write(entity: number, component: string, field: string, value: unknown): void {
|
|
411
|
+
const type = byName.get(component);
|
|
412
|
+
if (type !== undefined) world.write(entity, type, field, value);
|
|
413
|
+
},
|
|
414
|
+
hasComponent(entity: number, component: string): boolean {
|
|
415
|
+
const type = byName.get(component);
|
|
416
|
+
return type !== undefined && world.has(entity, type);
|
|
417
|
+
},
|
|
418
|
+
addComponent(entity: number, component: string): void {
|
|
419
|
+
const type = byName.get(component);
|
|
420
|
+
if (type !== undefined) world.add(entity, type);
|
|
421
|
+
},
|
|
422
|
+
removeComponent(entity: number, component: string): void {
|
|
423
|
+
const type = byName.get(component);
|
|
424
|
+
if (type !== undefined) world.remove(entity, type);
|
|
425
|
+
},
|
|
426
|
+
};
|
|
427
|
+
}
|
package/src/network.ts
CHANGED
|
@@ -183,3 +183,71 @@ export const networkPanel: Panel<NetworkWorld, NetworkView> = {
|
|
|
183
183
|
return null;
|
|
184
184
|
},
|
|
185
185
|
};
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* A session, as this package needs it.
|
|
189
|
+
*
|
|
190
|
+
* Structural rather than imported, so nothing here loads `@driftengine/network` at run time and a
|
|
191
|
+
* consumer whose session is its own shape can still be read.
|
|
192
|
+
*/
|
|
193
|
+
export interface SessionLike {
|
|
194
|
+
readonly participants: number;
|
|
195
|
+
readonly inputDelay: number;
|
|
196
|
+
/** The disagreement the session is currently holding, or null. Latched, not an event. */
|
|
197
|
+
readonly desync: Desync | null;
|
|
198
|
+
readonly loop: { readonly depth: number };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** What a recorder keeps: the disagreements it has seen, oldest first. */
|
|
202
|
+
export interface SessionRecorder {
|
|
203
|
+
readonly desyncs: Desync[];
|
|
204
|
+
readonly limit: number;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function createSessionRecorder(limit = 32): SessionRecorder {
|
|
208
|
+
return { desyncs: [], limit: Math.max(1, limit) };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Watch a session for one frame.
|
|
213
|
+
*
|
|
214
|
+
* **`desync` is latched rather than an event**, which is the whole reason this is not one line at
|
|
215
|
+
* the call site. A session keeps answering with the same disagreement on every frame until the
|
|
216
|
+
* next one arrives, so appending what it reads turns one divergence into sixty a second and buries
|
|
217
|
+
* the tick somebody is looking for under copies of itself. A disagreement is new when its tick and
|
|
218
|
+
* peer differ from the last one kept.
|
|
219
|
+
*
|
|
220
|
+
* Bounded, because a session that desyncs every tick would otherwise grow an array forever behind
|
|
221
|
+
* a panel nobody has open.
|
|
222
|
+
*/
|
|
223
|
+
export function observeSession(recorder: SessionRecorder, session: SessionLike): void {
|
|
224
|
+
const seen = session.desync;
|
|
225
|
+
if (seen === null) return;
|
|
226
|
+
const last = recorder.desyncs[recorder.desyncs.length - 1];
|
|
227
|
+
if (last !== undefined && last.tick === seen.tick && last.peer === seen.peer) return;
|
|
228
|
+
recorder.desyncs.push(seen);
|
|
229
|
+
if (recorder.desyncs.length > recorder.limit) recorder.desyncs.shift();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* The session and what has been seen of it, as the panel's readout.
|
|
234
|
+
*
|
|
235
|
+
* **`snapshotBytes` is the caller's, because the engine cannot know it.** `RewindLoop` is generic
|
|
236
|
+
* over the state it snapshots, so its depth is knowable here and the size of one is not. A caller
|
|
237
|
+
* passing zero is saying it has not measured, which the panel renders as such.
|
|
238
|
+
*/
|
|
239
|
+
export function sessionReadout(
|
|
240
|
+
session: SessionLike,
|
|
241
|
+
recorder: SessionRecorder,
|
|
242
|
+
snapshotBytes: number,
|
|
243
|
+
componentHashes: NetworkReadout['componentHashes'] = null,
|
|
244
|
+
): NetworkReadout {
|
|
245
|
+
return {
|
|
246
|
+
inputDelay: session.inputDelay,
|
|
247
|
+
participants: session.participants,
|
|
248
|
+
snapshotBytes,
|
|
249
|
+
snapshotCount: session.loop.depth,
|
|
250
|
+
desyncs: recorder.desyncs,
|
|
251
|
+
componentHashes,
|
|
252
|
+
};
|
|
253
|
+
}
|
package/src/overlay.ts
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The panels, on a key, over a running game.
|
|
3
|
+
*
|
|
4
|
+
* **The panels shipped without anything to mount them, and that was the gap.** `@driftengine/tools`
|
|
5
|
+
* exported an inspector, a console, a profiler and a network panel, each of which builds a `UiNode`
|
|
6
|
+
* tree and nothing more. What turned a tree into something a person could see lived in the editor
|
|
7
|
+
* application — a dock, a layout, a painter and the event routing — and that workspace is private
|
|
8
|
+
* and ships to nobody. So every consumer wanting an in-game inspector had to write that half again,
|
|
9
|
+
* and six of them writing it six times is the drift `AGENTS.md` opens by describing.
|
|
10
|
+
*
|
|
11
|
+
* **A column, not a dock.** The editor's dock splits, drags and persists because somebody arranges
|
|
12
|
+
* an editor and then keeps that arrangement. Nobody arranges a debug overlay: they press a key,
|
|
13
|
+
* read a number and press it again. So the panels stack down one edge in the order they were
|
|
14
|
+
* given, and what a dock would have bought is not bought.
|
|
15
|
+
*
|
|
16
|
+
* **The painter is supplied, never reached for.** Four calls — a rectangle, a line of text, a clip
|
|
17
|
+
* and its close — which is every operation `paintOverlay` makes. A host with a 2D context
|
|
18
|
+
* implements them in four lines, a host drawing through `@driftengine/ui2d`'s sprite pass
|
|
19
|
+
* implements them over a batch, and a host with no screen at all implements them into an array,
|
|
20
|
+
* which is what this module's own tests do. That is `AGENTS.md`'s platform rule applied to
|
|
21
|
+
* drawing: take the capability as a parameter, and let the consumer decide what it is.
|
|
22
|
+
*
|
|
23
|
+
* **Nothing here needs a graphics device**, which is the property that makes the whole of it
|
|
24
|
+
* testable. The geometry is arithmetic over a tree, the routing is arithmetic over rectangles, and
|
|
25
|
+
* the drawing is somebody else's four functions. An overlay that could only run against a live
|
|
26
|
+
* adapter is an overlay whose behaviour nobody can assert.
|
|
27
|
+
*
|
|
28
|
+
* **Hidden costs a frame nothing.** While it is closed, `frame` returns before it builds anything
|
|
29
|
+
* and `route` refuses every event but the one that opens it, so a game that never presses the key
|
|
30
|
+
* pays for a boolean.
|
|
31
|
+
*/
|
|
32
|
+
import { createUiNode, layoutUiTree } from '@driftengine/ui2d';
|
|
33
|
+
import type { UiNode } from '@driftengine/ui2d';
|
|
34
|
+
import { createUndoStack, type Command, type UndoStack } from './command.ts';
|
|
35
|
+
import { createPanelRoot, type Panel, type UiEvent } from './panel.ts';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A panel with its world and its view already attached.
|
|
39
|
+
*
|
|
40
|
+
* **The generics are closed here rather than carried by the host.** `Panel<W, V>` is two type
|
|
41
|
+
* parameters per panel, and a host holding several at once cannot express them in one list without
|
|
42
|
+
* erasing to `unknown` and casting on the way out. Binding closes them where both types are known
|
|
43
|
+
* and hands back something with no parameters at all, so the host never casts and the panel keeps
|
|
44
|
+
* the signature that makes its own rules checkable.
|
|
45
|
+
*/
|
|
46
|
+
export interface PanelBinding {
|
|
47
|
+
readonly id: string;
|
|
48
|
+
readonly title: string;
|
|
49
|
+
build(root: UiNode): void;
|
|
50
|
+
route(event: UiEvent): Command | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function bindPanel<W, V>(
|
|
54
|
+
panel: Panel<W, V>,
|
|
55
|
+
world: () => Readonly<W>,
|
|
56
|
+
view: V,
|
|
57
|
+
): PanelBinding {
|
|
58
|
+
return {
|
|
59
|
+
id: panel.id,
|
|
60
|
+
title: panel.title,
|
|
61
|
+
build: (root: UiNode): void => panel.build(world(), view, root),
|
|
62
|
+
route: (event: UiEvent): Command | null => panel.route(world(), view, event),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Where one panel came out. Read rather than recomputed, so a hit test cannot disagree with a draw. */
|
|
67
|
+
export interface OverlaySite {
|
|
68
|
+
readonly id: string;
|
|
69
|
+
readonly x: number;
|
|
70
|
+
readonly y: number;
|
|
71
|
+
readonly w: number;
|
|
72
|
+
readonly h: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** What a host draws with. Colours are CSS, `#rrggbb` or `#rrggbbaa`. */
|
|
76
|
+
export interface OverlayPainter {
|
|
77
|
+
rect(x: number, y: number, w: number, h: number, colour: string): void;
|
|
78
|
+
/** Text on a baseline at `y`, twelve pixels tall. */
|
|
79
|
+
text(content: string, x: number, y: number, colour: string): void;
|
|
80
|
+
clip(x: number, y: number, w: number, h: number): void;
|
|
81
|
+
unclip(): void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface ToolsOverlayOptions {
|
|
85
|
+
readonly panels: readonly PanelBinding[];
|
|
86
|
+
/** `KeyboardEvent.key` of the key that opens and closes it. Defaults to `F3`. */
|
|
87
|
+
readonly key?: string;
|
|
88
|
+
readonly side?: 'left' | 'right';
|
|
89
|
+
readonly width?: number;
|
|
90
|
+
readonly undoLimit?: number;
|
|
91
|
+
readonly visible?: boolean;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface ToolsOverlay {
|
|
95
|
+
/** The tree the whole overlay is in. One absolutely placed child per panel. */
|
|
96
|
+
readonly root: UiNode;
|
|
97
|
+
readonly undo: UndoStack;
|
|
98
|
+
readonly visible: boolean;
|
|
99
|
+
/** How many times the tree has been rebuilt and laid out. Read by tests, and by nothing else. */
|
|
100
|
+
readonly layoutCount: number;
|
|
101
|
+
sites(): readonly OverlaySite[];
|
|
102
|
+
setVisible(visible: boolean): void;
|
|
103
|
+
toggle(): void;
|
|
104
|
+
resize(width: number, height: number): void;
|
|
105
|
+
frame(now: number): void;
|
|
106
|
+
/** Deliver one event. True where the overlay took it, so a host knows not to pass it on. */
|
|
107
|
+
route(event: UiEvent): boolean;
|
|
108
|
+
/** Say the world changed, so the next frame rebuilds. A profiler calls this every frame. */
|
|
109
|
+
invalidate(): void;
|
|
110
|
+
dispose(): void;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const DEFAULT_WIDTH = 320;
|
|
114
|
+
const DEFAULT_KEY = 'F3';
|
|
115
|
+
|
|
116
|
+
export function createToolsOverlay(options: ToolsOverlayOptions): ToolsOverlay {
|
|
117
|
+
const root = createUiNode({ name: 'tools-overlay', width: 'grow', height: 'grow' });
|
|
118
|
+
const undo = createUndoStack(options.undoLimit ?? 100);
|
|
119
|
+
const key = options.key ?? DEFAULT_KEY;
|
|
120
|
+
const side = options.side ?? 'right';
|
|
121
|
+
const columnWidth = options.width ?? DEFAULT_WIDTH;
|
|
122
|
+
|
|
123
|
+
const panels = options.panels;
|
|
124
|
+
const roots = new Map<string, UiNode>();
|
|
125
|
+
/* Pooled and refilled: this is read every frame the overlay is open and must not allocate. */
|
|
126
|
+
const sites: OverlaySite[] = [];
|
|
127
|
+
|
|
128
|
+
let width = 0;
|
|
129
|
+
let height = 0;
|
|
130
|
+
let visible = options.visible ?? false;
|
|
131
|
+
let dirty = true;
|
|
132
|
+
let disposed = false;
|
|
133
|
+
let layoutCount = 0;
|
|
134
|
+
let focused: string | null = null;
|
|
135
|
+
|
|
136
|
+
const relayout = (): void => {
|
|
137
|
+
const x = side === 'left' ? 0 : Math.max(0, width - columnWidth);
|
|
138
|
+
const w = Math.min(columnWidth, width);
|
|
139
|
+
const each = panels.length === 0 ? 0 : Math.floor(height / panels.length);
|
|
140
|
+
|
|
141
|
+
root.absolute = true;
|
|
142
|
+
root.x = x;
|
|
143
|
+
root.y = 0;
|
|
144
|
+
root.width = w;
|
|
145
|
+
root.height = height;
|
|
146
|
+
root.children.length = 0;
|
|
147
|
+
sites.length = 0;
|
|
148
|
+
|
|
149
|
+
for (let at = 0; at < panels.length; at += 1) {
|
|
150
|
+
const panel = panels[at];
|
|
151
|
+
if (panel === undefined) continue;
|
|
152
|
+
/* The last panel takes the remainder, so a height that does not divide leaves no gap. */
|
|
153
|
+
const top = each * at;
|
|
154
|
+
const tall = at === panels.length - 1 ? height - top : each;
|
|
155
|
+
|
|
156
|
+
let panelRoot = roots.get(panel.id);
|
|
157
|
+
if (panelRoot === undefined) {
|
|
158
|
+
panelRoot = createPanelRoot(panel);
|
|
159
|
+
roots.set(panel.id, panelRoot);
|
|
160
|
+
}
|
|
161
|
+
panelRoot.absolute = true;
|
|
162
|
+
panelRoot.x = 0;
|
|
163
|
+
panelRoot.y = top;
|
|
164
|
+
panelRoot.width = w;
|
|
165
|
+
panelRoot.height = tall;
|
|
166
|
+
panel.build(panelRoot);
|
|
167
|
+
root.children.push(panelRoot);
|
|
168
|
+
sites.push({ id: panel.id, x: 0, y: top, w, h: tall });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
layoutUiTree(root, x, 0, w, height);
|
|
172
|
+
layoutCount += 1;
|
|
173
|
+
dirty = false;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
root,
|
|
178
|
+
undo,
|
|
179
|
+
get visible(): boolean {
|
|
180
|
+
return visible;
|
|
181
|
+
},
|
|
182
|
+
get layoutCount(): number {
|
|
183
|
+
return layoutCount;
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
sites(): readonly OverlaySite[] {
|
|
187
|
+
if (visible && dirty) relayout();
|
|
188
|
+
return sites;
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
setVisible(next: boolean): void {
|
|
192
|
+
if (disposed || next === visible) return;
|
|
193
|
+
visible = next;
|
|
194
|
+
dirty = true;
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
toggle(): void {
|
|
198
|
+
this.setVisible(!visible);
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
resize(next: number, nextHeight: number): void {
|
|
202
|
+
if (disposed) return;
|
|
203
|
+
if (next === width && nextHeight === height) return;
|
|
204
|
+
width = next;
|
|
205
|
+
height = nextHeight;
|
|
206
|
+
dirty = true;
|
|
207
|
+
},
|
|
208
|
+
|
|
209
|
+
frame(_now: number): void {
|
|
210
|
+
if (disposed || !visible) return;
|
|
211
|
+
if (dirty) relayout();
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
route(event: UiEvent): boolean {
|
|
215
|
+
if (disposed) return false;
|
|
216
|
+
/*
|
|
217
|
+
* The key is answered whether the overlay is open or closed, and it is the only thing that
|
|
218
|
+
* is. A host that had to know which state it was in to decide whether to forward a press
|
|
219
|
+
* would be holding the same boolean twice.
|
|
220
|
+
*/
|
|
221
|
+
if (event.kind === 'key' && event.key === key && !event.ctrl) {
|
|
222
|
+
visible = !visible;
|
|
223
|
+
dirty = true;
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
if (!visible) return false;
|
|
227
|
+
|
|
228
|
+
if (event.kind === 'key') {
|
|
229
|
+
if (event.ctrl && event.key === 'z') {
|
|
230
|
+
const moved = event.shift ? undo.redo() : undo.undo();
|
|
231
|
+
if (moved) dirty = true;
|
|
232
|
+
return moved;
|
|
233
|
+
}
|
|
234
|
+
if (event.ctrl && event.key === 'y') {
|
|
235
|
+
const moved = undo.redo();
|
|
236
|
+
if (moved) dirty = true;
|
|
237
|
+
return moved;
|
|
238
|
+
}
|
|
239
|
+
const target = panelWith(focused);
|
|
240
|
+
return target === null ? false : take(target.route(event));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (dirty) relayout();
|
|
244
|
+
const site = siteAt(event.x, event.y);
|
|
245
|
+
if (site === null) return false;
|
|
246
|
+
if (event.kind === 'pointer' && event.phase === 'down') focused = site.id;
|
|
247
|
+
const panel = panelWith(site.id);
|
|
248
|
+
if (panel === null) return false;
|
|
249
|
+
/* In the panel's own space: a panel never learns where on screen it is. */
|
|
250
|
+
return take(panel.route(shift(event, -(root.rect.x + site.x), -site.y)));
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
invalidate(): void {
|
|
254
|
+
dirty = true;
|
|
255
|
+
},
|
|
256
|
+
|
|
257
|
+
dispose(): void {
|
|
258
|
+
if (disposed) return;
|
|
259
|
+
disposed = true;
|
|
260
|
+
visible = false;
|
|
261
|
+
undo.clear();
|
|
262
|
+
root.children.length = 0;
|
|
263
|
+
roots.clear();
|
|
264
|
+
sites.length = 0;
|
|
265
|
+
focused = null;
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
function panelWith(id: string | null): PanelBinding | null {
|
|
270
|
+
if (id === null) return null;
|
|
271
|
+
for (const panel of panels) if (panel.id === id) return panel;
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function siteAt(x: number, y: number): OverlaySite | null {
|
|
276
|
+
const localX = x - root.rect.x;
|
|
277
|
+
if (localX < 0 || localX >= root.rect.w) return null;
|
|
278
|
+
for (const site of sites) {
|
|
279
|
+
if (y >= site.y && y < site.y + site.h) return site;
|
|
280
|
+
}
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/*
|
|
285
|
+
* **`push` applies, so this must not.** `createUndoStack.push` calls `apply` on the way in — and
|
|
286
|
+
* on the merge path it says in so many words that an absorbed command has already been applied
|
|
287
|
+
* and must not be applied again. Applying here as well ran every panel edit twice, which the
|
|
288
|
+
* row panel's test saw as two rows added by one press and a real inspector would have shown as a
|
|
289
|
+
* field moved twice as far as the pointer.
|
|
290
|
+
*/
|
|
291
|
+
function take(command: Command | null): boolean {
|
|
292
|
+
if (command === null) return false;
|
|
293
|
+
undo.push(command);
|
|
294
|
+
dirty = true;
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** The same event moved by a vector. Returns the event itself where there is nothing to move. */
|
|
300
|
+
function shift(event: UiEvent, dx: number, dy: number): UiEvent {
|
|
301
|
+
if (event.kind === 'pointer' || event.kind === 'wheel') {
|
|
302
|
+
return { ...event, x: event.x + dx, y: event.y + dy };
|
|
303
|
+
}
|
|
304
|
+
return event;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Draw the overlay, or draw nothing because it is closed.
|
|
309
|
+
*
|
|
310
|
+
* A free function rather than a method, so the overlay itself never holds a painter and a host may
|
|
311
|
+
* draw the same overlay into two surfaces — a screen and a capture — without either knowing.
|
|
312
|
+
*/
|
|
313
|
+
export function paintOverlay(painter: OverlayPainter, overlay: ToolsOverlay): void {
|
|
314
|
+
if (!overlay.visible) return;
|
|
315
|
+
paintNode(painter, overlay.root);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** Where a twelve-pixel line sits inside a row. Rows here are sixteen to eighteen pixels tall. */
|
|
319
|
+
const TEXT_BASELINE = 12;
|
|
320
|
+
|
|
321
|
+
function paintNode(painter: OverlayPainter, node: UiNode): void {
|
|
322
|
+
if (node.hidden) return;
|
|
323
|
+
const { x, y, w, h } = node.rect;
|
|
324
|
+
if (node.background !== null) painter.rect(x, y, w, h, cssColour(node.background));
|
|
325
|
+
if (node.text.length > 0) {
|
|
326
|
+
painter.text(node.text, x, y + TEXT_BASELINE, cssColour(node.tint));
|
|
327
|
+
}
|
|
328
|
+
if (node.children.length === 0) return;
|
|
329
|
+
if (node.clip) painter.clip(x, y, w, h);
|
|
330
|
+
for (const child of node.children) paintNode(painter, child);
|
|
331
|
+
if (node.clip) painter.unclip();
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function cssColour(rgba: Float32Array | null): string {
|
|
335
|
+
if (rgba === null) return '#ffffff';
|
|
336
|
+
let out = '#';
|
|
337
|
+
for (let at = 0; at < 3; at += 1) {
|
|
338
|
+
const channel = Math.round(Math.min(1, Math.max(0, rgba[at] ?? 0)) * 255);
|
|
339
|
+
out += channel.toString(16).padStart(2, '0');
|
|
340
|
+
}
|
|
341
|
+
return out;
|
|
342
|
+
}
|