@flare-engine/devtools 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +25 -9
- package/dist/index.d.mts +120 -107
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +120 -107
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +390 -370
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +380 -334
- package/dist/index.mjs.map +1 -1
- package/package.json +20 -13
package/dist/index.mjs
CHANGED
|
@@ -1,350 +1,396 @@
|
|
|
1
|
-
|
|
1
|
+
import { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { Platform, Pressable, Text, View } from "react-native";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
//#region src/context.ts
|
|
5
|
+
const DevtoolsContext = createContext(null);
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/dev-mode.ts
|
|
2
8
|
function isDevMode() {
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
// src/ring-buffer.ts
|
|
7
|
-
function createSnapshotRingBuffer(size, source) {
|
|
8
|
-
if (size <= 0 || !Number.isFinite(size)) {
|
|
9
|
-
throw new Error(`SnapshotRingBuffer size must be a positive finite number, got ${size}`);
|
|
10
|
-
}
|
|
11
|
-
const capacity = Math.floor(size);
|
|
12
|
-
const slots = new Array(capacity).fill(null);
|
|
13
|
-
let head = 0;
|
|
14
|
-
let count = 0;
|
|
15
|
-
return {
|
|
16
|
-
size: capacity,
|
|
17
|
-
get length() {
|
|
18
|
-
return count;
|
|
19
|
-
},
|
|
20
|
-
get latest() {
|
|
21
|
-
if (count === 0) return null;
|
|
22
|
-
const idx = (head - 1 + capacity) % capacity;
|
|
23
|
-
return slots[idx] ?? null;
|
|
24
|
-
},
|
|
25
|
-
capture() {
|
|
26
|
-
slots[head] = source();
|
|
27
|
-
head = (head + 1) % capacity;
|
|
28
|
-
if (count < capacity) count++;
|
|
29
|
-
},
|
|
30
|
-
rewind(framesAgo) {
|
|
31
|
-
if (framesAgo < 0 || !Number.isFinite(framesAgo)) return null;
|
|
32
|
-
const offset = Math.floor(framesAgo);
|
|
33
|
-
if (offset >= count) return null;
|
|
34
|
-
const idx = (head - 1 - offset + capacity * 2) % capacity;
|
|
35
|
-
return slots[idx] ?? null;
|
|
36
|
-
},
|
|
37
|
-
reset() {
|
|
38
|
-
for (let i = 0; i < capacity; i++) slots[i] = null;
|
|
39
|
-
head = 0;
|
|
40
|
-
count = 0;
|
|
41
|
-
}
|
|
42
|
-
};
|
|
9
|
+
return typeof __DEV__ !== "undefined" && Boolean(__DEV__);
|
|
43
10
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
loop;
|
|
48
|
-
buffer;
|
|
49
|
-
restore;
|
|
50
|
-
_stepRequested = false;
|
|
51
|
-
_scrubFrames = 0;
|
|
52
|
-
constructor(options) {
|
|
53
|
-
this.loop = options.loop;
|
|
54
|
-
this.buffer = options.buffer;
|
|
55
|
-
this.restore = options.restore;
|
|
56
|
-
}
|
|
57
|
-
/** Whether the loop is currently paused. */
|
|
58
|
-
get paused() {
|
|
59
|
-
return this.loop.clock.timescale === 0;
|
|
60
|
-
}
|
|
61
|
-
/** Whether the scrubber is currently displaying a historical snapshot. */
|
|
62
|
-
get scrubbing() {
|
|
63
|
-
return this._scrubFrames > 0;
|
|
64
|
-
}
|
|
65
|
-
/** Current scrub offset (0 = latest, N = N frames back). */
|
|
66
|
-
get scrubOffset() {
|
|
67
|
-
return this._scrubFrames;
|
|
68
|
-
}
|
|
69
|
-
/** Pause the loop (timescale → 0). */
|
|
70
|
-
pause() {
|
|
71
|
-
this.loop.clock.timescale = 0;
|
|
72
|
-
}
|
|
73
|
-
/** Resume the loop at normal speed and clear any scrub state. */
|
|
74
|
-
resume() {
|
|
75
|
-
this.loop.clock.timescale = 1;
|
|
76
|
-
this._scrubFrames = 0;
|
|
77
|
-
this._stepRequested = false;
|
|
78
|
-
}
|
|
79
|
-
/** Request a single-frame step. Caller polls `consumeStep()` each frame. */
|
|
80
|
-
step() {
|
|
81
|
-
this._stepRequested = true;
|
|
82
|
-
}
|
|
83
|
-
/** Ring-buffer depth available for rewind. */
|
|
84
|
-
get available() {
|
|
85
|
-
return this.buffer.length;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Pause the loop and restore the snapshot from `framesAgo` captures back.
|
|
89
|
-
* Clamps to available history. Returns the snapshot applied, or `null`
|
|
90
|
-
* when the buffer is empty.
|
|
91
|
-
*/
|
|
92
|
-
rewind(framesAgo) {
|
|
93
|
-
if (framesAgo < 0 || !Number.isFinite(framesAgo)) return null;
|
|
94
|
-
const max = this.buffer.length - 1;
|
|
95
|
-
if (max < 0) return null;
|
|
96
|
-
const offset = Math.min(Math.floor(framesAgo), max);
|
|
97
|
-
const snap = this.buffer.rewind(offset);
|
|
98
|
-
if (snap === null) return null;
|
|
99
|
-
this.pause();
|
|
100
|
-
this._scrubFrames = offset;
|
|
101
|
-
if (this.restore) this.restore(snap);
|
|
102
|
-
return snap;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Returns true once per `step()` call, then resets. Frame-driver code is
|
|
106
|
-
* expected to advance the simulation by one fixed tick when this is true.
|
|
107
|
-
*/
|
|
108
|
-
consumeStep() {
|
|
109
|
-
if (!this._stepRequested) return false;
|
|
110
|
-
this._stepRequested = false;
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
// src/context.ts
|
|
116
|
-
import { createContext } from "react";
|
|
117
|
-
var DevtoolsContext = createContext(null);
|
|
118
|
-
|
|
119
|
-
// src/provider.tsx
|
|
120
|
-
import { useEffect, useMemo, useRef, useState } from "react";
|
|
121
|
-
import { jsx } from "react/jsx-runtime";
|
|
122
|
-
var DEFAULT_CONFIG = {
|
|
123
|
-
ringSize: 120,
|
|
124
|
-
captureInterval: 1,
|
|
125
|
-
enabledOnNative: true
|
|
126
|
-
};
|
|
127
|
-
function DevtoolsProvider({
|
|
128
|
-
world,
|
|
129
|
-
loop,
|
|
130
|
-
snapshot,
|
|
131
|
-
restore,
|
|
132
|
-
config,
|
|
133
|
-
children
|
|
134
|
-
}) {
|
|
135
|
-
const ringSize = config?.ringSize ?? DEFAULT_CONFIG.ringSize;
|
|
136
|
-
const captureInterval = config?.captureInterval ?? DEFAULT_CONFIG.captureInterval;
|
|
137
|
-
const [selectedEntity, select] = useState(null);
|
|
138
|
-
const snapshotRef = useRef(snapshot);
|
|
139
|
-
snapshotRef.current = snapshot;
|
|
140
|
-
const restoreRef = useRef(restore);
|
|
141
|
-
restoreRef.current = restore;
|
|
142
|
-
const { buffer, scrubber } = useMemo(() => {
|
|
143
|
-
const buf = createSnapshotRingBuffer(ringSize, () => snapshotRef.current());
|
|
144
|
-
const scrub = new TimeScrubber({
|
|
145
|
-
loop,
|
|
146
|
-
buffer: buf,
|
|
147
|
-
restore: (s) => restoreRef.current?.(s)
|
|
148
|
-
});
|
|
149
|
-
return { buffer: buf, scrubber: scrub };
|
|
150
|
-
}, [loop, ringSize]);
|
|
151
|
-
useEffect(() => {
|
|
152
|
-
let ticks = 0;
|
|
153
|
-
const unsub = loop.onUpdate.add(() => {
|
|
154
|
-
ticks++;
|
|
155
|
-
if (ticks % captureInterval === 0) buffer.capture();
|
|
156
|
-
});
|
|
157
|
-
return () => {
|
|
158
|
-
unsub();
|
|
159
|
-
};
|
|
160
|
-
}, [loop, buffer, captureInterval]);
|
|
161
|
-
const value = {
|
|
162
|
-
world,
|
|
163
|
-
loop,
|
|
164
|
-
buffer,
|
|
165
|
-
scrubber,
|
|
166
|
-
selectedEntity,
|
|
167
|
-
select,
|
|
168
|
-
pause: () => scrubber.pause(),
|
|
169
|
-
step: () => scrubber.step(),
|
|
170
|
-
resume: () => scrubber.resume()
|
|
171
|
-
};
|
|
172
|
-
return /* @__PURE__ */ jsx(DevtoolsContext.Provider, { value, children });
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// src/hooks.ts
|
|
176
|
-
import { useContext } from "react";
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/hooks.ts
|
|
13
|
+
/** Returns the active devtools context, or throws if no provider is mounted. */
|
|
177
14
|
function useDevtools() {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
return ctx;
|
|
15
|
+
const ctx = useContext(DevtoolsContext);
|
|
16
|
+
if (!ctx) throw new Error("useDevtools must be used inside a <DevtoolsProvider>");
|
|
17
|
+
return ctx;
|
|
183
18
|
}
|
|
19
|
+
/** Entity-inspector hook. Never mutates the world — read-only snapshot. */
|
|
184
20
|
function useEntityInspector() {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
21
|
+
const { world, selectedEntity, select } = useDevtools();
|
|
22
|
+
const entities = [];
|
|
23
|
+
world.forEachEntity((id) => entities.push(id));
|
|
24
|
+
return {
|
|
25
|
+
selected: selectedEntity,
|
|
26
|
+
select,
|
|
27
|
+
entities
|
|
28
|
+
};
|
|
192
29
|
}
|
|
30
|
+
/** Time-scrub hook — exposes the scrubber's imperative controls. */
|
|
193
31
|
function useTimeScrub() {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
32
|
+
const { scrubber } = useDevtools();
|
|
33
|
+
return {
|
|
34
|
+
paused: scrubber.paused,
|
|
35
|
+
scrubbing: scrubber.scrubbing,
|
|
36
|
+
available: scrubber.available,
|
|
37
|
+
pause: () => scrubber.pause(),
|
|
38
|
+
resume: () => scrubber.resume(),
|
|
39
|
+
step: () => scrubber.step(),
|
|
40
|
+
rewind: (framesAgo) => {
|
|
41
|
+
scrubber.rewind(framesAgo);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
206
44
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/hud.tsx
|
|
47
|
+
/**
|
|
48
|
+
* DevtoolsHUD — compact native overlay. Shows buffer depth + pause/step/resume
|
|
49
|
+
* buttons. No-op on web (renders `null`).
|
|
50
|
+
*/
|
|
51
|
+
function DevtoolsHUD() {
|
|
52
|
+
if (Platform.OS === "web") return null;
|
|
53
|
+
return /* @__PURE__ */ jsx(HUDBody, {});
|
|
214
54
|
}
|
|
215
|
-
function
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
"#",
|
|
256
|
-
id
|
|
257
|
-
] }) }, id))
|
|
258
|
-
] })
|
|
259
|
-
]
|
|
260
|
-
}
|
|
261
|
-
);
|
|
55
|
+
function HUDBody() {
|
|
56
|
+
const { buffer } = useDevtools();
|
|
57
|
+
const scrub = useTimeScrub();
|
|
58
|
+
return /* @__PURE__ */ jsxs(View, {
|
|
59
|
+
style: {
|
|
60
|
+
position: "absolute",
|
|
61
|
+
bottom: 16,
|
|
62
|
+
right: 16,
|
|
63
|
+
flexDirection: "row",
|
|
64
|
+
alignItems: "center",
|
|
65
|
+
padding: 6,
|
|
66
|
+
backgroundColor: "rgba(0,0,0,0.75)",
|
|
67
|
+
borderRadius: 4
|
|
68
|
+
},
|
|
69
|
+
children: [
|
|
70
|
+
/* @__PURE__ */ jsxs(Text, {
|
|
71
|
+
style: {
|
|
72
|
+
color: "#fff",
|
|
73
|
+
marginRight: 8
|
|
74
|
+
},
|
|
75
|
+
children: [
|
|
76
|
+
buffer.length,
|
|
77
|
+
"/",
|
|
78
|
+
buffer.size
|
|
79
|
+
]
|
|
80
|
+
}),
|
|
81
|
+
/* @__PURE__ */ jsx(HudButton, {
|
|
82
|
+
label: "||",
|
|
83
|
+
onPress: scrub.pause
|
|
84
|
+
}),
|
|
85
|
+
/* @__PURE__ */ jsx(HudButton, {
|
|
86
|
+
label: ">|",
|
|
87
|
+
onPress: scrub.step
|
|
88
|
+
}),
|
|
89
|
+
/* @__PURE__ */ jsx(HudButton, {
|
|
90
|
+
label: ">",
|
|
91
|
+
onPress: scrub.resume
|
|
92
|
+
})
|
|
93
|
+
]
|
|
94
|
+
});
|
|
262
95
|
}
|
|
263
|
-
function
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
children: /* @__PURE__ */ jsx2(Text, { style: { color: "#fff" }, children: label })
|
|
279
|
-
}
|
|
280
|
-
);
|
|
96
|
+
function HudButton({ label, onPress }) {
|
|
97
|
+
return /* @__PURE__ */ jsx(Pressable, {
|
|
98
|
+
onPress,
|
|
99
|
+
style: {
|
|
100
|
+
paddingHorizontal: 6,
|
|
101
|
+
paddingVertical: 2,
|
|
102
|
+
marginLeft: 4,
|
|
103
|
+
backgroundColor: "#444",
|
|
104
|
+
borderRadius: 2
|
|
105
|
+
},
|
|
106
|
+
children: /* @__PURE__ */ jsx(Text, {
|
|
107
|
+
style: { color: "#fff" },
|
|
108
|
+
children: label
|
|
109
|
+
})
|
|
110
|
+
});
|
|
281
111
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/panel.tsx
|
|
114
|
+
/**
|
|
115
|
+
* DevtoolsPanel — full web-side panel. Renders the entity tree, scrub
|
|
116
|
+
* controls, and buffer depth. No-op on native (renders `null`).
|
|
117
|
+
*/
|
|
118
|
+
function DevtoolsPanel() {
|
|
119
|
+
if (Platform.OS !== "web") return null;
|
|
120
|
+
return /* @__PURE__ */ jsx(PanelBody, {});
|
|
289
121
|
}
|
|
290
|
-
function
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
122
|
+
function PanelBody() {
|
|
123
|
+
const { buffer } = useDevtools();
|
|
124
|
+
const { entities, selected, select } = useEntityInspector();
|
|
125
|
+
const scrub = useTimeScrub();
|
|
126
|
+
return /* @__PURE__ */ jsxs(View, {
|
|
127
|
+
style: {
|
|
128
|
+
position: "absolute",
|
|
129
|
+
top: 8,
|
|
130
|
+
right: 8,
|
|
131
|
+
padding: 8,
|
|
132
|
+
backgroundColor: "rgba(0,0,0,0.75)",
|
|
133
|
+
borderRadius: 4,
|
|
134
|
+
minWidth: 200
|
|
135
|
+
},
|
|
136
|
+
children: [
|
|
137
|
+
/* @__PURE__ */ jsx(Text, {
|
|
138
|
+
style: {
|
|
139
|
+
color: "#fff",
|
|
140
|
+
fontWeight: "bold"
|
|
141
|
+
},
|
|
142
|
+
children: "Devtools"
|
|
143
|
+
}),
|
|
144
|
+
/* @__PURE__ */ jsxs(Text, {
|
|
145
|
+
style: { color: "#ccc" },
|
|
146
|
+
children: [
|
|
147
|
+
"buffer ",
|
|
148
|
+
buffer.length,
|
|
149
|
+
"/",
|
|
150
|
+
buffer.size
|
|
151
|
+
]
|
|
152
|
+
}),
|
|
153
|
+
/* @__PURE__ */ jsxs(Text, {
|
|
154
|
+
style: { color: "#ccc" },
|
|
155
|
+
children: [scrub.paused ? "paused" : "running", scrub.scrubbing ? " (scrub)" : ""]
|
|
156
|
+
}),
|
|
157
|
+
/* @__PURE__ */ jsxs(View, {
|
|
158
|
+
style: {
|
|
159
|
+
flexDirection: "row",
|
|
160
|
+
marginTop: 4
|
|
161
|
+
},
|
|
162
|
+
children: [
|
|
163
|
+
/* @__PURE__ */ jsx(DevButton, {
|
|
164
|
+
label: "Pause",
|
|
165
|
+
onPress: scrub.pause
|
|
166
|
+
}),
|
|
167
|
+
/* @__PURE__ */ jsx(DevButton, {
|
|
168
|
+
label: "Step",
|
|
169
|
+
onPress: scrub.step
|
|
170
|
+
}),
|
|
171
|
+
/* @__PURE__ */ jsx(DevButton, {
|
|
172
|
+
label: "Resume",
|
|
173
|
+
onPress: scrub.resume
|
|
174
|
+
})
|
|
175
|
+
]
|
|
176
|
+
}),
|
|
177
|
+
/* @__PURE__ */ jsxs(View, {
|
|
178
|
+
style: { marginTop: 8 },
|
|
179
|
+
children: [/* @__PURE__ */ jsxs(Text, {
|
|
180
|
+
style: { color: "#fff" },
|
|
181
|
+
children: [
|
|
182
|
+
"entities (",
|
|
183
|
+
entities.length,
|
|
184
|
+
")"
|
|
185
|
+
]
|
|
186
|
+
}), entities.slice(0, 20).map((id) => /* @__PURE__ */ jsx(Pressable, {
|
|
187
|
+
onPress: () => select(id),
|
|
188
|
+
children: /* @__PURE__ */ jsxs(Text, {
|
|
189
|
+
style: { color: id === selected ? "#ff0" : "#fff" },
|
|
190
|
+
children: ["#", id]
|
|
191
|
+
})
|
|
192
|
+
}, id))]
|
|
193
|
+
})
|
|
194
|
+
]
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
function DevButton({ label, onPress }) {
|
|
198
|
+
return /* @__PURE__ */ jsx(Pressable, {
|
|
199
|
+
onPress,
|
|
200
|
+
style: {
|
|
201
|
+
paddingHorizontal: 6,
|
|
202
|
+
paddingVertical: 2,
|
|
203
|
+
marginRight: 4,
|
|
204
|
+
backgroundColor: "#444",
|
|
205
|
+
borderRadius: 2
|
|
206
|
+
},
|
|
207
|
+
children: /* @__PURE__ */ jsx(Text, {
|
|
208
|
+
style: { color: "#fff" },
|
|
209
|
+
children: label
|
|
210
|
+
})
|
|
211
|
+
});
|
|
318
212
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
213
|
+
//#endregion
|
|
214
|
+
//#region src/ring-buffer.ts
|
|
215
|
+
function createSnapshotRingBuffer(size, source) {
|
|
216
|
+
if (size <= 0 || !Number.isFinite(size)) throw new Error(`SnapshotRingBuffer size must be a positive finite number, got ${size}`);
|
|
217
|
+
const capacity = Math.floor(size);
|
|
218
|
+
const slots = new Array(capacity).fill(null);
|
|
219
|
+
let head = 0;
|
|
220
|
+
let count = 0;
|
|
221
|
+
return {
|
|
222
|
+
size: capacity,
|
|
223
|
+
get length() {
|
|
224
|
+
return count;
|
|
225
|
+
},
|
|
226
|
+
get latest() {
|
|
227
|
+
if (count === 0) return null;
|
|
228
|
+
const idx = (head - 1 + capacity) % capacity;
|
|
229
|
+
return slots[idx] ?? null;
|
|
230
|
+
},
|
|
231
|
+
capture() {
|
|
232
|
+
slots[head] = source();
|
|
233
|
+
head = (head + 1) % capacity;
|
|
234
|
+
if (count < capacity) count++;
|
|
235
|
+
},
|
|
236
|
+
rewind(framesAgo) {
|
|
237
|
+
if (framesAgo < 0 || !Number.isFinite(framesAgo)) return null;
|
|
238
|
+
const offset = Math.floor(framesAgo);
|
|
239
|
+
if (offset >= count) return null;
|
|
240
|
+
const idx = (head - 1 - offset + capacity * 2) % capacity;
|
|
241
|
+
return slots[idx] ?? null;
|
|
242
|
+
},
|
|
243
|
+
reset() {
|
|
244
|
+
for (let i = 0; i < capacity; i++) slots[i] = null;
|
|
245
|
+
head = 0;
|
|
246
|
+
count = 0;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
337
249
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region src/time-scrubber.ts
|
|
252
|
+
/**
|
|
253
|
+
* TimeScrubber — imperative controller binding a `GameLoop` to a snapshot
|
|
254
|
+
* ring buffer. Exposes pause / step / resume plus `rewind(framesAgo)` which
|
|
255
|
+
* pauses the loop and hands the historical snapshot to the restore callback.
|
|
256
|
+
*/
|
|
257
|
+
var TimeScrubber = class {
|
|
258
|
+
loop;
|
|
259
|
+
buffer;
|
|
260
|
+
restore;
|
|
261
|
+
_stepRequested = false;
|
|
262
|
+
_scrubFrames = 0;
|
|
263
|
+
/** Timescale captured at `pause()`, restored by `resume()`. Defaults to 1. */
|
|
264
|
+
_savedTimescale = 1;
|
|
265
|
+
constructor(options) {
|
|
266
|
+
this.loop = options.loop;
|
|
267
|
+
this.buffer = options.buffer;
|
|
268
|
+
this.restore = options.restore;
|
|
269
|
+
}
|
|
270
|
+
/** Whether the loop is currently paused. */
|
|
271
|
+
get paused() {
|
|
272
|
+
return this.loop.clock.timescale === 0;
|
|
273
|
+
}
|
|
274
|
+
/** Whether the scrubber is currently displaying a historical snapshot. */
|
|
275
|
+
get scrubbing() {
|
|
276
|
+
return this._scrubFrames > 0;
|
|
277
|
+
}
|
|
278
|
+
/** Current scrub offset (0 = latest, N = N frames back). */
|
|
279
|
+
get scrubOffset() {
|
|
280
|
+
return this._scrubFrames;
|
|
281
|
+
}
|
|
282
|
+
/** Pause the loop (timescale → 0), remembering the running speed.
|
|
283
|
+
* The `!== 0` guard means a double pause (e.g. `rewind()` calls `pause()`
|
|
284
|
+
* while already paused) keeps the first saved value instead of storing 0. */
|
|
285
|
+
pause() {
|
|
286
|
+
if (this.loop.clock.timescale !== 0) this._savedTimescale = this.loop.clock.timescale;
|
|
287
|
+
this.loop.clock.timescale = 0;
|
|
288
|
+
}
|
|
289
|
+
/** Resume the loop at its pre-pause speed and clear any scrub state.
|
|
290
|
+
* Restores the timescale captured at `pause()` (defaults to 1 if never
|
|
291
|
+
* paused), so resuming an active slow-mo returns to that rate, not 1×. */
|
|
292
|
+
resume() {
|
|
293
|
+
this.loop.clock.timescale = this._savedTimescale;
|
|
294
|
+
this._scrubFrames = 0;
|
|
295
|
+
this._stepRequested = false;
|
|
296
|
+
}
|
|
297
|
+
/** Request a single-frame step. Caller polls `consumeStep()` each frame. */
|
|
298
|
+
step() {
|
|
299
|
+
this._stepRequested = true;
|
|
300
|
+
}
|
|
301
|
+
/** Ring-buffer depth available for rewind. */
|
|
302
|
+
get available() {
|
|
303
|
+
return this.buffer.length;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Pause the loop and restore the snapshot from `framesAgo` captures back.
|
|
307
|
+
* Clamps to available history. Returns the snapshot applied, or `null`
|
|
308
|
+
* when the buffer is empty.
|
|
309
|
+
*/
|
|
310
|
+
rewind(framesAgo) {
|
|
311
|
+
if (framesAgo < 0 || !Number.isFinite(framesAgo)) return null;
|
|
312
|
+
const max = this.buffer.length - 1;
|
|
313
|
+
if (max < 0) return null;
|
|
314
|
+
const offset = Math.min(Math.floor(framesAgo), max);
|
|
315
|
+
const snap = this.buffer.rewind(offset);
|
|
316
|
+
if (snap === null) return null;
|
|
317
|
+
this.pause();
|
|
318
|
+
this._scrubFrames = offset;
|
|
319
|
+
if (this.restore) this.restore(snap);
|
|
320
|
+
return snap;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Returns true once per `step()` call, then resets. Frame-driver code is
|
|
324
|
+
* expected to advance the simulation by one fixed tick when this is true.
|
|
325
|
+
*/
|
|
326
|
+
consumeStep() {
|
|
327
|
+
if (!this._stepRequested) return false;
|
|
328
|
+
this._stepRequested = false;
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
349
331
|
};
|
|
332
|
+
//#endregion
|
|
333
|
+
//#region src/provider.tsx
|
|
334
|
+
const DEFAULT_CONFIG = {
|
|
335
|
+
ringSize: 120,
|
|
336
|
+
captureInterval: 1,
|
|
337
|
+
enabledOnNative: true
|
|
338
|
+
};
|
|
339
|
+
/**
|
|
340
|
+
* DevtoolsProvider — mounts a devtools session for a given `World` + `GameLoop`.
|
|
341
|
+
* Subscribes to `loop.onUpdate` and captures snapshots into a pre-allocated
|
|
342
|
+
* ring buffer; exposes scrubber controls via `DevtoolsContext`.
|
|
343
|
+
*/
|
|
344
|
+
function DevtoolsProvider({ world, loop, snapshot, restore, config, children }) {
|
|
345
|
+
const ringSize = config?.ringSize ?? DEFAULT_CONFIG.ringSize;
|
|
346
|
+
const captureInterval = config?.captureInterval ?? DEFAULT_CONFIG.captureInterval;
|
|
347
|
+
const [selectedEntity, select] = useState(null);
|
|
348
|
+
const snapshotRef = useRef(snapshot);
|
|
349
|
+
snapshotRef.current = snapshot;
|
|
350
|
+
const restoreRef = useRef(restore);
|
|
351
|
+
restoreRef.current = restore;
|
|
352
|
+
const { buffer, scrubber } = useMemo(() => {
|
|
353
|
+
const buf = createSnapshotRingBuffer(ringSize, () => snapshotRef.current());
|
|
354
|
+
return {
|
|
355
|
+
buffer: buf,
|
|
356
|
+
scrubber: new TimeScrubber({
|
|
357
|
+
loop,
|
|
358
|
+
buffer: buf,
|
|
359
|
+
restore: (s) => restoreRef.current?.(s)
|
|
360
|
+
})
|
|
361
|
+
};
|
|
362
|
+
}, [loop, ringSize]);
|
|
363
|
+
useEffect(() => {
|
|
364
|
+
let ticks = 0;
|
|
365
|
+
const unsub = loop.onUpdate.add(() => {
|
|
366
|
+
ticks++;
|
|
367
|
+
if (ticks % captureInterval === 0) buffer.capture();
|
|
368
|
+
});
|
|
369
|
+
return () => {
|
|
370
|
+
unsub();
|
|
371
|
+
};
|
|
372
|
+
}, [
|
|
373
|
+
loop,
|
|
374
|
+
buffer,
|
|
375
|
+
captureInterval
|
|
376
|
+
]);
|
|
377
|
+
const value = {
|
|
378
|
+
world,
|
|
379
|
+
loop,
|
|
380
|
+
buffer,
|
|
381
|
+
scrubber,
|
|
382
|
+
selectedEntity,
|
|
383
|
+
select,
|
|
384
|
+
pause: () => scrubber.pause(),
|
|
385
|
+
step: () => scrubber.step(),
|
|
386
|
+
resume: () => scrubber.resume()
|
|
387
|
+
};
|
|
388
|
+
return /* @__PURE__ */ jsx(DevtoolsContext.Provider, {
|
|
389
|
+
value,
|
|
390
|
+
children
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
//#endregion
|
|
394
|
+
export { DevtoolsContext, DevtoolsHUD, DevtoolsPanel, DevtoolsProvider, TimeScrubber, createSnapshotRingBuffer, isDevMode, useDevtools, useEntityInspector, useTimeScrub };
|
|
395
|
+
|
|
350
396
|
//# sourceMappingURL=index.mjs.map
|