@bloopjs/web 0.0.30 → 0.0.32

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/dist/mod.js CHANGED
@@ -1,4 +1,4 @@
1
- // node_modules/@bloopjs/engine/dist/engine.js
1
+ // ../bloop/dist/mod.js
2
2
  var __defProp = Object.defineProperty;
3
3
  var __export = (target, all) => {
4
4
  for (var name in all)
@@ -9,8 +9,42 @@ var __export = (target, all) => {
9
9
  set: (newValue) => all[name] = () => newValue
10
10
  });
11
11
  };
12
+ var exports_util = {};
13
+ __export(exports_util, {
14
+ unwrap: () => unwrap,
15
+ toHexString: () => toHexString,
16
+ assert: () => assert
17
+ });
18
+ function toHexString(dataView, length) {
19
+ length ??= dataView.byteLength;
20
+ let hexString = "";
21
+ for (let i = 0;i < length; i++) {
22
+ const byte = dataView.getUint8(i);
23
+ hexString += `${byte.toString(16).padStart(2, "0")} `;
24
+ }
25
+ return hexString.trim();
26
+ }
27
+ function assert(condition, message) {
28
+ if (condition == null || condition === false) {
29
+ throw new Error(message ?? "Assertion failed");
30
+ }
31
+ }
32
+ function unwrap(value, message) {
33
+ assert(value != null, message ?? `Unwrap failed: value is ${value}`);
34
+ return value;
35
+ }
36
+ var __defProp2 = Object.defineProperty;
37
+ var __export2 = (target, all) => {
38
+ for (var name in all)
39
+ __defProp2(target, name, {
40
+ get: all[name],
41
+ enumerable: true,
42
+ configurable: true,
43
+ set: (newValue) => all[name] = () => newValue
44
+ });
45
+ };
12
46
  var exports_enums = {};
13
- __export(exports_enums, {
47
+ __export2(exports_enums, {
14
48
  MouseButton: () => MouseButton,
15
49
  Key: () => Key,
16
50
  EventType: () => EventType
@@ -239,6 +273,17 @@ var Key;
239
273
  var MOUSE_OFFSET = 256;
240
274
  var MOUSE_BUTTONS_OFFSET = 16;
241
275
  var KEYBOARD_OFFSET = 0;
276
+ var EVENT_PAYLOAD_SIZE = 8;
277
+ var EVENT_PAYLOAD_ALIGN = 4;
278
+ function keyToKeyCode(key) {
279
+ return Key[key];
280
+ }
281
+ function keyCodeToKey(code) {
282
+ return Key[code];
283
+ }
284
+ function mouseButtonToMouseButtonCode(button) {
285
+ return MouseButton[button];
286
+ }
242
287
  function mouseButtonCodeToMouseButton(code) {
243
288
  return MouseButton[code];
244
289
  }
@@ -937,76 +982,1426 @@ class KeyboardContext {
937
982
  return state;
938
983
  }
939
984
  }
985
+
986
+ class TimeContext {
987
+ dataView;
988
+ constructor(dataView) {
989
+ this.dataView = dataView;
990
+ }
991
+ get frame() {
992
+ if (!this.dataView) {
993
+ throw new Error("TimeContext DataView is not initialized");
994
+ }
995
+ return this.dataView.getUint32(0, true);
996
+ }
997
+ get dt() {
998
+ if (!this.dataView) {
999
+ throw new Error("TimeContext DataView is not initialized");
1000
+ }
1001
+ return this.dataView.getUint32(4, true) / 1000;
1002
+ }
1003
+ get time() {
1004
+ if (!this.dataView) {
1005
+ throw new Error("TimeContext DataView is not initialized");
1006
+ }
1007
+ return this.dataView.getUint32(8, true) / 1000;
1008
+ }
1009
+ }
940
1010
  var DEFAULT_WASM_URL = new URL("../wasm/bloop.wasm", import.meta.url);
941
1011
  var TIME_CTX_OFFSET = 0;
942
1012
  var INPUT_CTX_OFFSET = TIME_CTX_OFFSET + 4;
943
1013
  var EVENTS_OFFSET = INPUT_CTX_OFFSET + 4;
1014
+ var SNAPSHOT_HEADER_USER_LEN_OFFSET = 4;
1015
+ var SNAPSHOT_HEADER_ENGINE_LEN_OFFSET = 8;
1016
+ var originalConsole = globalThis.console;
1017
+ var noop = () => {};
1018
+ var stubConsole = Object.fromEntries(Object.keys(originalConsole).map((key) => [key, noop]));
1019
+ function muteConsole() {
1020
+ globalThis.console = stubConsole;
1021
+ }
1022
+ function unmuteConsole() {
1023
+ globalThis.console = originalConsole;
1024
+ }
944
1025
 
945
- // src/mod.ts
946
- function connect(runtime) {
947
- let isPaused = true;
948
- let now = performance.now();
949
- let frameHandle = -1;
950
- const handleKeydown = (event) => {
951
- runtime.emit.keydown(event.key);
952
- };
953
- window.addEventListener("keydown", handleKeydown);
954
- const handleKeyup = (event) => {
955
- runtime.emit.keyup(event.key);
956
- };
957
- window.addEventListener("keyup", handleKeyup);
958
- const handleMousemove = (event) => {
959
- runtime.emit.mousemove(event.clientX, event.clientY);
960
- };
961
- window.addEventListener("mousemove", handleMousemove);
962
- const handleMousedown = (event) => {
963
- runtime.emit.mousedown(mouseButtonCodeToMouseButton(event.button + 1));
1026
+ class Sim {
1027
+ wasm;
1028
+ id;
1029
+ #memory;
1030
+ #time;
1031
+ #serialize;
1032
+ #isPaused = false;
1033
+ constructor(wasm, memory, opts) {
1034
+ this.wasm = wasm;
1035
+ this.#memory = memory;
1036
+ this.#time = new TimeContext(new DataView(this.#memory.buffer, this.wasm.get_time_ctx()));
1037
+ this.id = `${Math.floor(Math.random() * 1e6)}`;
1038
+ this.#serialize = opts?.serialize;
1039
+ }
1040
+ step(ms) {
1041
+ if (this.#isPaused && !this.isReplaying) {
1042
+ return;
1043
+ }
1044
+ this.wasm.step(ms ?? 16);
1045
+ }
1046
+ cloneSession(source) {
1047
+ this.loadTape(source.saveTape());
1048
+ this.restore(source.snapshot());
1049
+ }
1050
+ pause() {
1051
+ this.#isPaused = true;
1052
+ }
1053
+ unpause() {
1054
+ this.#isPaused = false;
1055
+ }
1056
+ get isPaused() {
1057
+ return this.#isPaused;
1058
+ }
1059
+ stepBack() {
1060
+ if (this.time.frame === 0) {
1061
+ return;
1062
+ }
1063
+ this.seek(this.time.frame - 1);
1064
+ }
1065
+ seek(frame, inclusive) {
1066
+ assert(this.hasHistory, "Not recording or playing back, can't seek to frame");
1067
+ const targetFrame = inclusive ? frame + 1 : frame;
1068
+ const shouldMute = frame < this.time.frame;
1069
+ if (shouldMute) {
1070
+ muteConsole();
1071
+ }
1072
+ this.wasm.seek(targetFrame);
1073
+ if (shouldMute) {
1074
+ unmuteConsole();
1075
+ }
1076
+ }
1077
+ record() {
1078
+ const serializer = this.#serialize ? this.#serialize() : null;
1079
+ const size = serializer ? serializer.size : 0;
1080
+ const result = this.wasm.start_recording(size, 1024);
1081
+ if (result !== 0) {
1082
+ throw new Error(`failed to start recording, error code=${result}`);
1083
+ }
1084
+ }
1085
+ snapshot() {
1086
+ const serializer = this.#serialize ? this.#serialize() : null;
1087
+ const size = serializer ? serializer.size : 0;
1088
+ const ptr = this.wasm.take_snapshot(size);
1089
+ const header = new Uint32Array(this.#memory.buffer, ptr, 4);
1090
+ const userLenIndex = SNAPSHOT_HEADER_USER_LEN_OFFSET / Uint32Array.BYTES_PER_ELEMENT;
1091
+ const engineLenIndex = SNAPSHOT_HEADER_ENGINE_LEN_OFFSET / Uint32Array.BYTES_PER_ELEMENT;
1092
+ assert(header[userLenIndex], `header user length missing`);
1093
+ assert(header[engineLenIndex], `header engine length missing`);
1094
+ const length = header[userLenIndex] + header[engineLenIndex];
1095
+ const memoryView = new Uint8Array(this.#memory.buffer, ptr, length);
1096
+ const copy = new Uint8Array(length);
1097
+ copy.set(memoryView);
1098
+ return copy;
1099
+ }
1100
+ saveTape() {
1101
+ const tapeLen = this.wasm.get_tape_len();
1102
+ const tapePtr = this.wasm.get_tape_ptr();
1103
+ const memoryView = new Uint8Array(this.#memory.buffer, tapePtr, tapeLen);
1104
+ const copy = new Uint8Array(tapeLen);
1105
+ copy.set(memoryView);
1106
+ return copy;
1107
+ }
1108
+ loadTape(tape) {
1109
+ const tapePtr = this.wasm.alloc(tape.byteLength);
1110
+ assert(tapePtr > 0, `failed to allocate ${tape.byteLength} bytes for tape load, pointer=${tapePtr}`);
1111
+ const memoryView = new Uint8Array(this.#memory.buffer, tapePtr, tape.byteLength);
1112
+ memoryView.set(tape);
1113
+ this.wasm.stop_recording();
1114
+ const result = this.wasm.load_tape(tapePtr, tape.byteLength);
1115
+ assert(result === 0, `failed to load tape, error code=${result}`);
1116
+ this.wasm.free(tapePtr, tape.byteLength);
1117
+ }
1118
+ restore(snapshot) {
1119
+ const dataPtr = this.wasm.alloc(snapshot.byteLength);
1120
+ assert(dataPtr > 0, `failed to allocate ${snapshot.byteLength} bytes for snapshot restore, pointer=${dataPtr}`);
1121
+ const memoryView = new Uint8Array(this.#memory.buffer, dataPtr, snapshot.byteLength);
1122
+ memoryView.set(snapshot);
1123
+ this.wasm.restore(dataPtr);
1124
+ this.wasm.free(dataPtr, snapshot.byteLength);
1125
+ }
1126
+ unmount() {
1127
+ this.wasm.deinit();
1128
+ }
1129
+ get time() {
1130
+ if (!this.#time.dataView || this.#time.dataView.buffer !== this.#memory.buffer) {
1131
+ this.#time.dataView = new DataView(this.#memory.buffer, this.wasm.get_time_ctx());
1132
+ }
1133
+ return this.#time;
1134
+ }
1135
+ get buffer() {
1136
+ return this.#memory.buffer;
1137
+ }
1138
+ get isRecording() {
1139
+ return this.wasm.is_recording();
1140
+ }
1141
+ get isReplaying() {
1142
+ return this.wasm.is_replaying();
1143
+ }
1144
+ get hasHistory() {
1145
+ return this.isRecording || this.isReplaying;
1146
+ }
1147
+ emit = {
1148
+ keydown: (key) => {
1149
+ this.wasm.emit_keydown(keyToKeyCode(key));
1150
+ },
1151
+ keyup: (key) => {
1152
+ this.wasm.emit_keyup(keyToKeyCode(key));
1153
+ },
1154
+ mousemove: (x, y) => {
1155
+ this.wasm.emit_mousemove(x, y);
1156
+ },
1157
+ mousedown: (button) => {
1158
+ this.wasm.emit_mousedown(mouseButtonToMouseButtonCode(button));
1159
+ },
1160
+ mouseup: (button) => {
1161
+ this.wasm.emit_mouseup(mouseButtonToMouseButtonCode(button));
1162
+ },
1163
+ mousewheel: (x, y) => {
1164
+ this.wasm.emit_mousewheel(x, y);
1165
+ }
964
1166
  };
965
- window.addEventListener("mousedown", handleMousedown);
966
- const handleMousewheel = (event) => {
967
- runtime.emit.mousewheel(event.deltaX, event.deltaY);
1167
+ }
1168
+ async function mount(opts) {
1169
+ const bytes = await fetch(opts.wasmUrl ?? DEFAULT_WASM_URL).then((res) => res.arrayBuffer()).catch((e) => {
1170
+ console.error(`Failed to fetch wasm at ${opts.wasmUrl ?? DEFAULT_WASM_URL}`, e);
1171
+ throw e;
1172
+ });
1173
+ const memory = new WebAssembly.Memory({ initial: 17, maximum: 1000 });
1174
+ const wasmInstantiatedSource = await WebAssembly.instantiate(bytes, {
1175
+ env: {
1176
+ memory,
1177
+ __cb: function(system_handle, ptr) {
1178
+ opts.hooks.setBuffer(memory.buffer);
1179
+ opts.hooks.systemsCallback(system_handle, ptr);
1180
+ },
1181
+ console_log: function(ptr, len) {
1182
+ const bytes2 = new Uint8Array(memory.buffer, ptr, len);
1183
+ const string = new TextDecoder("utf-8").decode(bytes2);
1184
+ console.log(string);
1185
+ },
1186
+ user_data_len: function() {
1187
+ const serializer = opts.hooks.serialize();
1188
+ return serializer ? serializer.size : 0;
1189
+ },
1190
+ user_data_serialize: function(ptr, len) {
1191
+ const serializer = opts.hooks.serialize();
1192
+ assert(len === serializer.size, `user_data_serialize length mismatch, expected=${serializer.size} got=${len}`);
1193
+ serializer.write(memory.buffer, ptr);
1194
+ },
1195
+ user_data_deserialize: function(ptr, len) {
1196
+ opts.hooks.deserialize(memory.buffer, ptr, len);
1197
+ }
1198
+ }
1199
+ });
1200
+ const wasm = wasmInstantiatedSource.instance.exports;
1201
+ const enginePointer = wasm.initialize();
1202
+ const sim = new Sim(wasm, memory, {
1203
+ serialize: opts.hooks.serialize
1204
+ });
1205
+ if (opts.startRecording ?? true) {
1206
+ sim.record();
1207
+ }
1208
+ opts.hooks.setBuffer(memory.buffer);
1209
+ opts.hooks.setContext(enginePointer);
1210
+ return {
1211
+ sim
968
1212
  };
969
- window.addEventListener("wheel", handleMousewheel);
970
- const playbarHotkeys = (event) => {
971
- const isPauseHotkey = event.key === "Enter" && (event.ctrlKey || event.metaKey);
972
- if (isPauseHotkey || event.key === "6") {
973
- isPaused = !isPaused;
1213
+ }
1214
+
1215
+ class Bloop {
1216
+ #systems = [];
1217
+ #context;
1218
+ #engineBuffer = new ArrayBuffer(0);
1219
+ static create(opts = {}) {
1220
+ return new Bloop(opts, "dontCallMeDirectly");
1221
+ }
1222
+ constructor(opts = {}, dontCallMeDirectly) {
1223
+ if (dontCallMeDirectly !== "dontCallMeDirectly") {
1224
+ throw new Error("Bloop constructor is private. Use Bloop.create() to create a new game instance.");
974
1225
  }
975
- if (isPaused) {
976
- switch (event.key) {
977
- case ",":
978
- case "5":
979
- runtime.stepBack();
980
- break;
981
- case ".":
982
- case "7":
983
- runtime.step();
984
- break;
1226
+ this.#context = {
1227
+ bag: opts.bag ?? {},
1228
+ time: new TimeContext,
1229
+ inputs: new InputContext,
1230
+ rawPointer: -1
1231
+ };
1232
+ }
1233
+ get bag() {
1234
+ return this.#context.bag;
1235
+ }
1236
+ get context() {
1237
+ return this.#context;
1238
+ }
1239
+ system(label, system) {
1240
+ system.label ??= label;
1241
+ this.#systems.push(system);
1242
+ return this.#systems.length;
1243
+ }
1244
+ hooks = {
1245
+ serialize: () => {
1246
+ const str = JSON.stringify(this.#context.bag);
1247
+ const encoder = new TextEncoder;
1248
+ const textBytes = encoder.encode(str);
1249
+ return {
1250
+ size: textBytes.byteLength,
1251
+ write: (buffer, ptr) => {
1252
+ const memoryView = new Uint8Array(buffer, ptr, textBytes.byteLength);
1253
+ memoryView.set(textBytes);
1254
+ }
1255
+ };
1256
+ },
1257
+ deserialize: (buffer, ptr, len) => {
1258
+ const bagBytes = new Uint8Array(buffer, ptr, len);
1259
+ const decoder = new TextDecoder;
1260
+ const str = decoder.decode(bagBytes);
1261
+ try {
1262
+ this.#context.bag = JSON.parse(str);
1263
+ } catch (e) {
1264
+ console.error("failed to deserialize bag", { json: str, error: e });
1265
+ }
1266
+ },
1267
+ setBuffer: (buffer) => {
1268
+ this.#engineBuffer = buffer;
1269
+ },
1270
+ setContext: (ptr) => {
1271
+ if (!this.#engineBuffer) {
1272
+ throw new Error("Tried to set context before engine buffer");
1273
+ }
1274
+ const dv = new DataView(this.#engineBuffer, ptr);
1275
+ const timeCtxPtr = dv.getUint32(TIME_CTX_OFFSET, true);
1276
+ const inputCtxPtr = dv.getUint32(INPUT_CTX_OFFSET, true);
1277
+ this.#context.rawPointer = ptr;
1278
+ this.#context.inputs.dataView = new DataView(this.#engineBuffer, inputCtxPtr);
1279
+ this.#context.time.dataView = new DataView(this.#engineBuffer, timeCtxPtr);
1280
+ },
1281
+ systemsCallback: (system_handle, ptr) => {
1282
+ this.hooks.setContext(ptr);
1283
+ const dv = new DataView(this.#engineBuffer, ptr);
1284
+ const eventsPtr = dv.getUint32(EVENTS_OFFSET, true);
1285
+ const eventsDataView = new DataView(this.#engineBuffer, eventsPtr);
1286
+ for (const system of this.#systems) {
1287
+ system.update?.(this.#context);
1288
+ const eventCount = eventsDataView.getUint32(0, true);
1289
+ let offset = Uint32Array.BYTES_PER_ELEMENT;
1290
+ for (let i = 0;i < eventCount; i++) {
1291
+ const eventType = eventsDataView.getUint8(offset);
1292
+ const payloadSize = EVENT_PAYLOAD_SIZE;
1293
+ const payloadByte = eventsDataView.getUint8(offset + EVENT_PAYLOAD_ALIGN);
1294
+ const payloadVec2 = {
1295
+ x: eventsDataView.getFloat32(offset + EVENT_PAYLOAD_ALIGN, true),
1296
+ y: eventsDataView.getFloat32(offset + EVENT_PAYLOAD_ALIGN + Float32Array.BYTES_PER_ELEMENT, true)
1297
+ };
1298
+ switch (eventType) {
1299
+ case exports_enums.EventType.KeyDown: {
1300
+ system.keydown?.(attachEvent(this.#context, {
1301
+ key: keyCodeToKey(payloadByte)
1302
+ }));
1303
+ break;
1304
+ }
1305
+ case exports_enums.EventType.KeyUp:
1306
+ system.keyup?.(attachEvent(this.#context, {
1307
+ key: keyCodeToKey(payloadByte)
1308
+ }));
1309
+ break;
1310
+ case exports_enums.EventType.MouseDown:
1311
+ system.mousedown?.(attachEvent(this.#context, {
1312
+ button: mouseButtonCodeToMouseButton(payloadByte)
1313
+ }));
1314
+ break;
1315
+ case exports_enums.EventType.MouseUp:
1316
+ system.mouseup?.(attachEvent(this.#context, {
1317
+ button: mouseButtonCodeToMouseButton(payloadByte)
1318
+ }));
1319
+ break;
1320
+ case exports_enums.EventType.MouseMove:
1321
+ system.mousemove?.(attachEvent(this.#context, {
1322
+ x: payloadVec2.x,
1323
+ y: payloadVec2.y
1324
+ }));
1325
+ break;
1326
+ case exports_enums.EventType.MouseWheel:
1327
+ system.mousewheel?.(attachEvent(this.#context, {
1328
+ x: payloadVec2.x,
1329
+ y: payloadVec2.y
1330
+ }));
1331
+ break;
1332
+ default:
1333
+ throw new Error(`Unknown event type: ${eventType}`);
1334
+ }
1335
+ offset += EVENT_PAYLOAD_ALIGN + EVENT_PAYLOAD_SIZE;
1336
+ }
1337
+ this.#context.event = undefined;
985
1338
  }
986
1339
  }
987
1340
  };
988
- window.addEventListener("keydown", playbarHotkeys);
989
- function frame() {
990
- if (!isPaused) {
991
- runtime.step(performance.now() - now);
1341
+ }
1342
+ function attachEvent(context, event) {
1343
+ context.event = event;
1344
+ return context;
1345
+ }
1346
+
1347
+ // ../engine/dist/engine.js
1348
+ var __defProp3 = Object.defineProperty;
1349
+ var __export3 = (target, all) => {
1350
+ for (var name in all)
1351
+ __defProp3(target, name, {
1352
+ get: all[name],
1353
+ enumerable: true,
1354
+ configurable: true,
1355
+ set: (newValue) => all[name] = () => newValue
1356
+ });
1357
+ };
1358
+ var exports_enums2 = {};
1359
+ __export3(exports_enums2, {
1360
+ MouseButton: () => MouseButton2,
1361
+ Key: () => Key2,
1362
+ EventType: () => EventType2
1363
+ });
1364
+ var EventType2;
1365
+ ((EventType22) => {
1366
+ EventType22[EventType22["None"] = 0] = "None";
1367
+ EventType22[EventType22["KeyDown"] = 1] = "KeyDown";
1368
+ EventType22[EventType22["KeyUp"] = 2] = "KeyUp";
1369
+ EventType22[EventType22["MouseMove"] = 3] = "MouseMove";
1370
+ EventType22[EventType22["MouseDown"] = 4] = "MouseDown";
1371
+ EventType22[EventType22["MouseUp"] = 5] = "MouseUp";
1372
+ EventType22[EventType22["MouseWheel"] = 6] = "MouseWheel";
1373
+ EventType22[EventType22["FrameStart"] = 7] = "FrameStart";
1374
+ })(EventType2 ||= {});
1375
+ var MouseButton2;
1376
+ ((MouseButton22) => {
1377
+ MouseButton22[MouseButton22["None"] = 0] = "None";
1378
+ MouseButton22[MouseButton22["Left"] = 1] = "Left";
1379
+ MouseButton22[MouseButton22["Middle"] = 2] = "Middle";
1380
+ MouseButton22[MouseButton22["Right"] = 3] = "Right";
1381
+ MouseButton22[MouseButton22["X1"] = 4] = "X1";
1382
+ MouseButton22[MouseButton22["X2"] = 5] = "X2";
1383
+ MouseButton22[MouseButton22["X3"] = 6] = "X3";
1384
+ MouseButton22[MouseButton22["X4"] = 7] = "X4";
1385
+ MouseButton22[MouseButton22["X5"] = 8] = "X5";
1386
+ })(MouseButton2 ||= {});
1387
+ var Key2;
1388
+ ((Key22) => {
1389
+ Key22[Key22["None"] = 0] = "None";
1390
+ Key22[Key22["Backquote"] = 1] = "Backquote";
1391
+ Key22[Key22["Backslash"] = 2] = "Backslash";
1392
+ Key22[Key22["BracketLeft"] = 3] = "BracketLeft";
1393
+ Key22[Key22["BracketRight"] = 4] = "BracketRight";
1394
+ Key22[Key22["Comma"] = 5] = "Comma";
1395
+ Key22[Key22["Digit0"] = 6] = "Digit0";
1396
+ Key22[Key22["Digit1"] = 7] = "Digit1";
1397
+ Key22[Key22["Digit2"] = 8] = "Digit2";
1398
+ Key22[Key22["Digit3"] = 9] = "Digit3";
1399
+ Key22[Key22["Digit4"] = 10] = "Digit4";
1400
+ Key22[Key22["Digit5"] = 11] = "Digit5";
1401
+ Key22[Key22["Digit6"] = 12] = "Digit6";
1402
+ Key22[Key22["Digit7"] = 13] = "Digit7";
1403
+ Key22[Key22["Digit8"] = 14] = "Digit8";
1404
+ Key22[Key22["Digit9"] = 15] = "Digit9";
1405
+ Key22[Key22["Equal"] = 16] = "Equal";
1406
+ Key22[Key22["IntlBackslash"] = 17] = "IntlBackslash";
1407
+ Key22[Key22["IntlRo"] = 18] = "IntlRo";
1408
+ Key22[Key22["IntlYen"] = 19] = "IntlYen";
1409
+ Key22[Key22["KeyA"] = 20] = "KeyA";
1410
+ Key22[Key22["KeyB"] = 21] = "KeyB";
1411
+ Key22[Key22["KeyC"] = 22] = "KeyC";
1412
+ Key22[Key22["KeyD"] = 23] = "KeyD";
1413
+ Key22[Key22["KeyE"] = 24] = "KeyE";
1414
+ Key22[Key22["KeyF"] = 25] = "KeyF";
1415
+ Key22[Key22["KeyG"] = 26] = "KeyG";
1416
+ Key22[Key22["KeyH"] = 27] = "KeyH";
1417
+ Key22[Key22["KeyI"] = 28] = "KeyI";
1418
+ Key22[Key22["KeyJ"] = 29] = "KeyJ";
1419
+ Key22[Key22["KeyK"] = 30] = "KeyK";
1420
+ Key22[Key22["KeyL"] = 31] = "KeyL";
1421
+ Key22[Key22["KeyM"] = 32] = "KeyM";
1422
+ Key22[Key22["KeyN"] = 33] = "KeyN";
1423
+ Key22[Key22["KeyO"] = 34] = "KeyO";
1424
+ Key22[Key22["KeyP"] = 35] = "KeyP";
1425
+ Key22[Key22["KeyQ"] = 36] = "KeyQ";
1426
+ Key22[Key22["KeyR"] = 37] = "KeyR";
1427
+ Key22[Key22["KeyS"] = 38] = "KeyS";
1428
+ Key22[Key22["KeyT"] = 39] = "KeyT";
1429
+ Key22[Key22["KeyU"] = 40] = "KeyU";
1430
+ Key22[Key22["KeyV"] = 41] = "KeyV";
1431
+ Key22[Key22["KeyW"] = 42] = "KeyW";
1432
+ Key22[Key22["KeyX"] = 43] = "KeyX";
1433
+ Key22[Key22["KeyY"] = 44] = "KeyY";
1434
+ Key22[Key22["KeyZ"] = 45] = "KeyZ";
1435
+ Key22[Key22["Minus"] = 46] = "Minus";
1436
+ Key22[Key22["Period"] = 47] = "Period";
1437
+ Key22[Key22["Quote"] = 48] = "Quote";
1438
+ Key22[Key22["Semicolon"] = 49] = "Semicolon";
1439
+ Key22[Key22["Slash"] = 50] = "Slash";
1440
+ Key22[Key22["AltLeft"] = 51] = "AltLeft";
1441
+ Key22[Key22["AltRight"] = 52] = "AltRight";
1442
+ Key22[Key22["Backspace"] = 53] = "Backspace";
1443
+ Key22[Key22["CapsLock"] = 54] = "CapsLock";
1444
+ Key22[Key22["ContextMenu"] = 55] = "ContextMenu";
1445
+ Key22[Key22["ControlLeft"] = 56] = "ControlLeft";
1446
+ Key22[Key22["ControlRight"] = 57] = "ControlRight";
1447
+ Key22[Key22["Enter"] = 58] = "Enter";
1448
+ Key22[Key22["MetaLeft"] = 59] = "MetaLeft";
1449
+ Key22[Key22["MetaRight"] = 60] = "MetaRight";
1450
+ Key22[Key22["ShiftLeft"] = 61] = "ShiftLeft";
1451
+ Key22[Key22["ShiftRight"] = 62] = "ShiftRight";
1452
+ Key22[Key22["Space"] = 63] = "Space";
1453
+ Key22[Key22["Tab"] = 64] = "Tab";
1454
+ Key22[Key22["Convert"] = 65] = "Convert";
1455
+ Key22[Key22["KanaMode"] = 66] = "KanaMode";
1456
+ Key22[Key22["Lang1"] = 67] = "Lang1";
1457
+ Key22[Key22["Lang2"] = 68] = "Lang2";
1458
+ Key22[Key22["Lang3"] = 69] = "Lang3";
1459
+ Key22[Key22["Lang4"] = 70] = "Lang4";
1460
+ Key22[Key22["Lang5"] = 71] = "Lang5";
1461
+ Key22[Key22["NonConvert"] = 72] = "NonConvert";
1462
+ Key22[Key22["Delete"] = 73] = "Delete";
1463
+ Key22[Key22["End"] = 74] = "End";
1464
+ Key22[Key22["Help"] = 75] = "Help";
1465
+ Key22[Key22["Home"] = 76] = "Home";
1466
+ Key22[Key22["Insert"] = 77] = "Insert";
1467
+ Key22[Key22["PageDown"] = 78] = "PageDown";
1468
+ Key22[Key22["PageUp"] = 79] = "PageUp";
1469
+ Key22[Key22["ArrowDown"] = 80] = "ArrowDown";
1470
+ Key22[Key22["ArrowLeft"] = 81] = "ArrowLeft";
1471
+ Key22[Key22["ArrowRight"] = 82] = "ArrowRight";
1472
+ Key22[Key22["ArrowUp"] = 83] = "ArrowUp";
1473
+ Key22[Key22["NumLock"] = 84] = "NumLock";
1474
+ Key22[Key22["Numpad0"] = 85] = "Numpad0";
1475
+ Key22[Key22["Numpad1"] = 86] = "Numpad1";
1476
+ Key22[Key22["Numpad2"] = 87] = "Numpad2";
1477
+ Key22[Key22["Numpad3"] = 88] = "Numpad3";
1478
+ Key22[Key22["Numpad4"] = 89] = "Numpad4";
1479
+ Key22[Key22["Numpad5"] = 90] = "Numpad5";
1480
+ Key22[Key22["Numpad6"] = 91] = "Numpad6";
1481
+ Key22[Key22["Numpad7"] = 92] = "Numpad7";
1482
+ Key22[Key22["Numpad8"] = 93] = "Numpad8";
1483
+ Key22[Key22["Numpad9"] = 94] = "Numpad9";
1484
+ Key22[Key22["NumpadAdd"] = 95] = "NumpadAdd";
1485
+ Key22[Key22["NumpadBackspace"] = 96] = "NumpadBackspace";
1486
+ Key22[Key22["NumpadClear"] = 97] = "NumpadClear";
1487
+ Key22[Key22["NumpadClearEntry"] = 98] = "NumpadClearEntry";
1488
+ Key22[Key22["NumpadComma"] = 99] = "NumpadComma";
1489
+ Key22[Key22["NumpadDecimal"] = 100] = "NumpadDecimal";
1490
+ Key22[Key22["NumpadDivide"] = 101] = "NumpadDivide";
1491
+ Key22[Key22["NumpadEnter"] = 102] = "NumpadEnter";
1492
+ Key22[Key22["NumpadEqual"] = 103] = "NumpadEqual";
1493
+ Key22[Key22["NumpadHash"] = 104] = "NumpadHash";
1494
+ Key22[Key22["NumpadMemoryAdd"] = 105] = "NumpadMemoryAdd";
1495
+ Key22[Key22["NumpadMemoryClear"] = 106] = "NumpadMemoryClear";
1496
+ Key22[Key22["NumpadMemoryRecall"] = 107] = "NumpadMemoryRecall";
1497
+ Key22[Key22["NumpadMemoryStore"] = 108] = "NumpadMemoryStore";
1498
+ Key22[Key22["NumpadMemorySubtract"] = 109] = "NumpadMemorySubtract";
1499
+ Key22[Key22["NumpadMultiply"] = 110] = "NumpadMultiply";
1500
+ Key22[Key22["NumpadParenLeft"] = 111] = "NumpadParenLeft";
1501
+ Key22[Key22["NumpadParenRight"] = 112] = "NumpadParenRight";
1502
+ Key22[Key22["NumpadStar"] = 113] = "NumpadStar";
1503
+ Key22[Key22["NumpadSubtract"] = 114] = "NumpadSubtract";
1504
+ Key22[Key22["Escape"] = 115] = "Escape";
1505
+ Key22[Key22["Fn"] = 116] = "Fn";
1506
+ Key22[Key22["FnLock"] = 117] = "FnLock";
1507
+ Key22[Key22["PrintScreen"] = 118] = "PrintScreen";
1508
+ Key22[Key22["ScrollLock"] = 119] = "ScrollLock";
1509
+ Key22[Key22["Pause"] = 120] = "Pause";
1510
+ Key22[Key22["BrowserBack"] = 121] = "BrowserBack";
1511
+ Key22[Key22["BrowserFavorites"] = 122] = "BrowserFavorites";
1512
+ Key22[Key22["BrowserForward"] = 123] = "BrowserForward";
1513
+ Key22[Key22["BrowserHome"] = 124] = "BrowserHome";
1514
+ Key22[Key22["BrowserRefresh"] = 125] = "BrowserRefresh";
1515
+ Key22[Key22["BrowserSearch"] = 126] = "BrowserSearch";
1516
+ Key22[Key22["BrowserStop"] = 127] = "BrowserStop";
1517
+ Key22[Key22["Eject"] = 128] = "Eject";
1518
+ Key22[Key22["LaunchApp1"] = 129] = "LaunchApp1";
1519
+ Key22[Key22["LaunchApp2"] = 130] = "LaunchApp2";
1520
+ Key22[Key22["LaunchMail"] = 131] = "LaunchMail";
1521
+ Key22[Key22["MediaPlayPause"] = 132] = "MediaPlayPause";
1522
+ Key22[Key22["MediaSelect"] = 133] = "MediaSelect";
1523
+ Key22[Key22["MediaStop"] = 134] = "MediaStop";
1524
+ Key22[Key22["MediaTrackNext"] = 135] = "MediaTrackNext";
1525
+ Key22[Key22["MediaTrackPrevious"] = 136] = "MediaTrackPrevious";
1526
+ Key22[Key22["Power"] = 137] = "Power";
1527
+ Key22[Key22["Sleep"] = 138] = "Sleep";
1528
+ Key22[Key22["AudioVolumeDown"] = 139] = "AudioVolumeDown";
1529
+ Key22[Key22["AudioVolumeMute"] = 140] = "AudioVolumeMute";
1530
+ Key22[Key22["AudioVolumeUp"] = 141] = "AudioVolumeUp";
1531
+ Key22[Key22["WakeUp"] = 142] = "WakeUp";
1532
+ Key22[Key22["Hyper"] = 143] = "Hyper";
1533
+ Key22[Key22["Super"] = 144] = "Super";
1534
+ Key22[Key22["Turbo"] = 145] = "Turbo";
1535
+ Key22[Key22["Abort"] = 146] = "Abort";
1536
+ Key22[Key22["Resume"] = 147] = "Resume";
1537
+ Key22[Key22["Suspend"] = 148] = "Suspend";
1538
+ Key22[Key22["Again"] = 149] = "Again";
1539
+ Key22[Key22["Copy"] = 150] = "Copy";
1540
+ Key22[Key22["Cut"] = 151] = "Cut";
1541
+ Key22[Key22["Find"] = 152] = "Find";
1542
+ Key22[Key22["Open"] = 153] = "Open";
1543
+ Key22[Key22["Paste"] = 154] = "Paste";
1544
+ Key22[Key22["Props"] = 155] = "Props";
1545
+ Key22[Key22["Select"] = 156] = "Select";
1546
+ Key22[Key22["Undo"] = 157] = "Undo";
1547
+ Key22[Key22["Hiragana"] = 158] = "Hiragana";
1548
+ Key22[Key22["Katakana"] = 159] = "Katakana";
1549
+ Key22[Key22["F1"] = 160] = "F1";
1550
+ Key22[Key22["F2"] = 161] = "F2";
1551
+ Key22[Key22["F3"] = 162] = "F3";
1552
+ Key22[Key22["F4"] = 163] = "F4";
1553
+ Key22[Key22["F5"] = 164] = "F5";
1554
+ Key22[Key22["F6"] = 165] = "F6";
1555
+ Key22[Key22["F7"] = 166] = "F7";
1556
+ Key22[Key22["F8"] = 167] = "F8";
1557
+ Key22[Key22["F9"] = 168] = "F9";
1558
+ Key22[Key22["F10"] = 169] = "F10";
1559
+ Key22[Key22["F11"] = 170] = "F11";
1560
+ Key22[Key22["F12"] = 171] = "F12";
1561
+ Key22[Key22["F13"] = 172] = "F13";
1562
+ Key22[Key22["F14"] = 173] = "F14";
1563
+ Key22[Key22["F15"] = 174] = "F15";
1564
+ Key22[Key22["F16"] = 175] = "F16";
1565
+ Key22[Key22["F17"] = 176] = "F17";
1566
+ Key22[Key22["F18"] = 177] = "F18";
1567
+ Key22[Key22["F19"] = 178] = "F19";
1568
+ Key22[Key22["F20"] = 179] = "F20";
1569
+ Key22[Key22["F21"] = 180] = "F21";
1570
+ Key22[Key22["F22"] = 181] = "F22";
1571
+ Key22[Key22["F23"] = 182] = "F23";
1572
+ Key22[Key22["F24"] = 183] = "F24";
1573
+ Key22[Key22["F25"] = 184] = "F25";
1574
+ Key22[Key22["F26"] = 185] = "F26";
1575
+ Key22[Key22["F27"] = 186] = "F27";
1576
+ Key22[Key22["F28"] = 187] = "F28";
1577
+ Key22[Key22["F29"] = 188] = "F29";
1578
+ Key22[Key22["F30"] = 189] = "F30";
1579
+ Key22[Key22["F31"] = 190] = "F31";
1580
+ Key22[Key22["F32"] = 191] = "F32";
1581
+ Key22[Key22["F33"] = 192] = "F33";
1582
+ Key22[Key22["F34"] = 193] = "F34";
1583
+ Key22[Key22["F35"] = 194] = "F35";
1584
+ })(Key2 ||= {});
1585
+ var MOUSE_OFFSET2 = 256;
1586
+ var MOUSE_BUTTONS_OFFSET2 = 16;
1587
+ var KEYBOARD_OFFSET2 = 0;
1588
+ function mouseButtonCodeToMouseButton2(code) {
1589
+ return MouseButton2[code];
1590
+ }
1591
+
1592
+ class InputContext2 {
1593
+ #dataView;
1594
+ #keys;
1595
+ #mouse;
1596
+ constructor(dataView) {
1597
+ if (dataView) {
1598
+ this.#dataView = dataView;
992
1599
  }
993
- now = performance.now();
994
- frameHandle = requestAnimationFrame(frame);
995
- }
996
- frame();
997
- return () => {
998
- window.removeEventListener("keydown", handleKeydown);
999
- window.removeEventListener("keyup", handleKeyup);
1000
- window.removeEventListener("mousemove", handleMousemove);
1001
- window.removeEventListener("mousedown", handleMousedown);
1002
- window.removeEventListener("wheel", handleMousewheel);
1003
- window.removeEventListener("keydown", playbarHotkeys);
1004
- cancelAnimationFrame(frameHandle);
1005
- };
1600
+ }
1601
+ get dataView() {
1602
+ if (!this.#dataView) {
1603
+ throw new Error("DataView is not initialized on InputContext");
1604
+ }
1605
+ return this.#dataView;
1606
+ }
1607
+ set dataView(dataView) {
1608
+ this.#dataView = dataView;
1609
+ this.#keys = undefined;
1610
+ this.#mouse = undefined;
1611
+ }
1612
+ get keys() {
1613
+ if (!this.#keys) {
1614
+ this.#keys = new KeyboardContext2(new DataView(this.dataView.buffer, this.dataView.byteOffset + KEYBOARD_OFFSET2));
1615
+ }
1616
+ return this.#keys;
1617
+ }
1618
+ get mouse() {
1619
+ if (!this.#mouse) {
1620
+ this.#mouse = new MouseContext2(new DataView(this.dataView.buffer, this.dataView.byteOffset + MOUSE_OFFSET2));
1621
+ }
1622
+ return this.#mouse;
1623
+ }
1624
+ }
1625
+
1626
+ class MouseContext2 {
1627
+ #dataView;
1628
+ #buttonStates = new Map;
1629
+ constructor(dataView) {
1630
+ this.#dataView = dataView;
1631
+ }
1632
+ get x() {
1633
+ return this.#dataView.getFloat32(0, true);
1634
+ }
1635
+ get y() {
1636
+ return this.#dataView.getFloat32(4, true);
1637
+ }
1638
+ get wheel() {
1639
+ return { x: this.wheelX, y: this.wheelY };
1640
+ }
1641
+ get wheelX() {
1642
+ return this.#dataView.getFloat32(8, true);
1643
+ }
1644
+ get wheelY() {
1645
+ return this.#dataView.getFloat32(12, true);
1646
+ }
1647
+ get left() {
1648
+ return this.#buttonState(1);
1649
+ }
1650
+ get right() {
1651
+ return this.#buttonState(3);
1652
+ }
1653
+ get middle() {
1654
+ return this.#buttonState(2);
1655
+ }
1656
+ #buttonState(code) {
1657
+ let state;
1658
+ if (this.#buttonStates.has(code)) {
1659
+ state = this.#buttonStates.get(code);
1660
+ } else {
1661
+ state = { down: false, held: false, up: false };
1662
+ this.#buttonStates.set(code, state);
1663
+ }
1664
+ const offset = MOUSE_BUTTONS_OFFSET2;
1665
+ state.held = !!(this.#dataView.getUint8(offset + code) & 1);
1666
+ state.down = state.held && !(this.#dataView.getUint8(offset + code) & 2);
1667
+ state.up = !state.held && !!(this.#dataView.getUint8(offset + code) & 2);
1668
+ return state;
1669
+ }
1670
+ }
1671
+
1672
+ class KeyboardContext2 {
1673
+ #dataView;
1674
+ #keystates = new Map;
1675
+ constructor(dataView) {
1676
+ this.#dataView = dataView;
1677
+ }
1678
+ get shift() {
1679
+ return this.shiftLeft || this.shiftRight;
1680
+ }
1681
+ get alt() {
1682
+ return this.altLeft || this.altRight;
1683
+ }
1684
+ get control() {
1685
+ return this.controlLeft || this.controlRight;
1686
+ }
1687
+ get meta() {
1688
+ return this.metaLeft || this.metaRight;
1689
+ }
1690
+ get backquote() {
1691
+ return this.#keystate(1);
1692
+ }
1693
+ get backslash() {
1694
+ return this.#keystate(2);
1695
+ }
1696
+ get bracketLeft() {
1697
+ return this.#keystate(3);
1698
+ }
1699
+ get bracketRight() {
1700
+ return this.#keystate(4);
1701
+ }
1702
+ get comma() {
1703
+ return this.#keystate(5);
1704
+ }
1705
+ get digit0() {
1706
+ return this.#keystate(6);
1707
+ }
1708
+ get digit1() {
1709
+ return this.#keystate(7);
1710
+ }
1711
+ get digit2() {
1712
+ return this.#keystate(8);
1713
+ }
1714
+ get digit3() {
1715
+ return this.#keystate(9);
1716
+ }
1717
+ get digit4() {
1718
+ return this.#keystate(10);
1719
+ }
1720
+ get digit5() {
1721
+ return this.#keystate(11);
1722
+ }
1723
+ get digit6() {
1724
+ return this.#keystate(12);
1725
+ }
1726
+ get digit7() {
1727
+ return this.#keystate(13);
1728
+ }
1729
+ get digit8() {
1730
+ return this.#keystate(14);
1731
+ }
1732
+ get digit9() {
1733
+ return this.#keystate(15);
1734
+ }
1735
+ get equal() {
1736
+ return this.#keystate(16);
1737
+ }
1738
+ get intlBackslash() {
1739
+ return this.#keystate(17);
1740
+ }
1741
+ get intlRo() {
1742
+ return this.#keystate(18);
1743
+ }
1744
+ get intlYen() {
1745
+ return this.#keystate(19);
1746
+ }
1747
+ get a() {
1748
+ return this.#keystate(20);
1749
+ }
1750
+ get b() {
1751
+ return this.#keystate(21);
1752
+ }
1753
+ get c() {
1754
+ return this.#keystate(22);
1755
+ }
1756
+ get d() {
1757
+ return this.#keystate(23);
1758
+ }
1759
+ get e() {
1760
+ return this.#keystate(24);
1761
+ }
1762
+ get f() {
1763
+ return this.#keystate(25);
1764
+ }
1765
+ get g() {
1766
+ return this.#keystate(26);
1767
+ }
1768
+ get h() {
1769
+ return this.#keystate(27);
1770
+ }
1771
+ get i() {
1772
+ return this.#keystate(28);
1773
+ }
1774
+ get j() {
1775
+ return this.#keystate(29);
1776
+ }
1777
+ get k() {
1778
+ return this.#keystate(30);
1779
+ }
1780
+ get l() {
1781
+ return this.#keystate(31);
1782
+ }
1783
+ get m() {
1784
+ return this.#keystate(32);
1785
+ }
1786
+ get n() {
1787
+ return this.#keystate(33);
1788
+ }
1789
+ get o() {
1790
+ return this.#keystate(34);
1791
+ }
1792
+ get p() {
1793
+ return this.#keystate(35);
1794
+ }
1795
+ get q() {
1796
+ return this.#keystate(36);
1797
+ }
1798
+ get r() {
1799
+ return this.#keystate(37);
1800
+ }
1801
+ get s() {
1802
+ return this.#keystate(38);
1803
+ }
1804
+ get t() {
1805
+ return this.#keystate(39);
1806
+ }
1807
+ get u() {
1808
+ return this.#keystate(40);
1809
+ }
1810
+ get v() {
1811
+ return this.#keystate(41);
1812
+ }
1813
+ get w() {
1814
+ return this.#keystate(42);
1815
+ }
1816
+ get x() {
1817
+ return this.#keystate(43);
1818
+ }
1819
+ get y() {
1820
+ return this.#keystate(44);
1821
+ }
1822
+ get z() {
1823
+ return this.#keystate(45);
1824
+ }
1825
+ get minus() {
1826
+ return this.#keystate(46);
1827
+ }
1828
+ get period() {
1829
+ return this.#keystate(47);
1830
+ }
1831
+ get quote() {
1832
+ return this.#keystate(48);
1833
+ }
1834
+ get semicolon() {
1835
+ return this.#keystate(49);
1836
+ }
1837
+ get slash() {
1838
+ return this.#keystate(50);
1839
+ }
1840
+ get altLeft() {
1841
+ return this.#keystate(51);
1842
+ }
1843
+ get altRight() {
1844
+ return this.#keystate(52);
1845
+ }
1846
+ get backspace() {
1847
+ return this.#keystate(53);
1848
+ }
1849
+ get capsLock() {
1850
+ return this.#keystate(54);
1851
+ }
1852
+ get contextMenu() {
1853
+ return this.#keystate(55);
1854
+ }
1855
+ get controlLeft() {
1856
+ return this.#keystate(56);
1857
+ }
1858
+ get controlRight() {
1859
+ return this.#keystate(57);
1860
+ }
1861
+ get enter() {
1862
+ return this.#keystate(58);
1863
+ }
1864
+ get metaLeft() {
1865
+ return this.#keystate(59);
1866
+ }
1867
+ get metaRight() {
1868
+ return this.#keystate(60);
1869
+ }
1870
+ get shiftLeft() {
1871
+ return this.#keystate(61);
1872
+ }
1873
+ get shiftRight() {
1874
+ return this.#keystate(62);
1875
+ }
1876
+ get space() {
1877
+ return this.#keystate(63);
1878
+ }
1879
+ get tab() {
1880
+ return this.#keystate(64);
1881
+ }
1882
+ get convert() {
1883
+ return this.#keystate(65);
1884
+ }
1885
+ get kanaMode() {
1886
+ return this.#keystate(66);
1887
+ }
1888
+ get lang1() {
1889
+ return this.#keystate(67);
1890
+ }
1891
+ get lang2() {
1892
+ return this.#keystate(68);
1893
+ }
1894
+ get lang3() {
1895
+ return this.#keystate(69);
1896
+ }
1897
+ get lang4() {
1898
+ return this.#keystate(70);
1899
+ }
1900
+ get lang5() {
1901
+ return this.#keystate(71);
1902
+ }
1903
+ get nonConvert() {
1904
+ return this.#keystate(72);
1905
+ }
1906
+ get delete() {
1907
+ return this.#keystate(73);
1908
+ }
1909
+ get end() {
1910
+ return this.#keystate(74);
1911
+ }
1912
+ get help() {
1913
+ return this.#keystate(75);
1914
+ }
1915
+ get home() {
1916
+ return this.#keystate(76);
1917
+ }
1918
+ get insert() {
1919
+ return this.#keystate(77);
1920
+ }
1921
+ get pageDown() {
1922
+ return this.#keystate(78);
1923
+ }
1924
+ get pageUp() {
1925
+ return this.#keystate(79);
1926
+ }
1927
+ get arrowDown() {
1928
+ return this.#keystate(80);
1929
+ }
1930
+ get arrowLeft() {
1931
+ return this.#keystate(81);
1932
+ }
1933
+ get arrowRight() {
1934
+ return this.#keystate(82);
1935
+ }
1936
+ get arrowUp() {
1937
+ return this.#keystate(83);
1938
+ }
1939
+ get numLock() {
1940
+ return this.#keystate(84);
1941
+ }
1942
+ get numpad0() {
1943
+ return this.#keystate(85);
1944
+ }
1945
+ get numpad1() {
1946
+ return this.#keystate(86);
1947
+ }
1948
+ get numpad2() {
1949
+ return this.#keystate(87);
1950
+ }
1951
+ get numpad3() {
1952
+ return this.#keystate(88);
1953
+ }
1954
+ get numpad4() {
1955
+ return this.#keystate(89);
1956
+ }
1957
+ get numpad5() {
1958
+ return this.#keystate(90);
1959
+ }
1960
+ get numpad6() {
1961
+ return this.#keystate(91);
1962
+ }
1963
+ get numpad7() {
1964
+ return this.#keystate(92);
1965
+ }
1966
+ get numpad8() {
1967
+ return this.#keystate(93);
1968
+ }
1969
+ get numpad9() {
1970
+ return this.#keystate(94);
1971
+ }
1972
+ get numpadAdd() {
1973
+ return this.#keystate(95);
1974
+ }
1975
+ get numpadBackspace() {
1976
+ return this.#keystate(96);
1977
+ }
1978
+ get numpadClear() {
1979
+ return this.#keystate(97);
1980
+ }
1981
+ get numpadClearEntry() {
1982
+ return this.#keystate(98);
1983
+ }
1984
+ get numpadComma() {
1985
+ return this.#keystate(99);
1986
+ }
1987
+ get numpadDecimal() {
1988
+ return this.#keystate(100);
1989
+ }
1990
+ get numpadDivide() {
1991
+ return this.#keystate(101);
1992
+ }
1993
+ get numpadEnter() {
1994
+ return this.#keystate(102);
1995
+ }
1996
+ get numpadEqual() {
1997
+ return this.#keystate(103);
1998
+ }
1999
+ get numpadHash() {
2000
+ return this.#keystate(104);
2001
+ }
2002
+ get numpadMemoryAdd() {
2003
+ return this.#keystate(105);
2004
+ }
2005
+ get numpadMemoryClear() {
2006
+ return this.#keystate(106);
2007
+ }
2008
+ get numpadMemoryRecall() {
2009
+ return this.#keystate(107);
2010
+ }
2011
+ get numpadMemoryStore() {
2012
+ return this.#keystate(108);
2013
+ }
2014
+ get numpadMemorySubtract() {
2015
+ return this.#keystate(109);
2016
+ }
2017
+ get numpadMultiply() {
2018
+ return this.#keystate(110);
2019
+ }
2020
+ get numpadParenLeft() {
2021
+ return this.#keystate(111);
2022
+ }
2023
+ get numpadParenRight() {
2024
+ return this.#keystate(112);
2025
+ }
2026
+ get numpadStar() {
2027
+ return this.#keystate(113);
2028
+ }
2029
+ get numpadSubtract() {
2030
+ return this.#keystate(114);
2031
+ }
2032
+ get escape() {
2033
+ return this.#keystate(115);
2034
+ }
2035
+ get fn() {
2036
+ return this.#keystate(116);
2037
+ }
2038
+ get fnLock() {
2039
+ return this.#keystate(117);
2040
+ }
2041
+ get printScreen() {
2042
+ return this.#keystate(118);
2043
+ }
2044
+ get scrollLock() {
2045
+ return this.#keystate(119);
2046
+ }
2047
+ get pause() {
2048
+ return this.#keystate(120);
2049
+ }
2050
+ get browserBack() {
2051
+ return this.#keystate(121);
2052
+ }
2053
+ get browserFavorites() {
2054
+ return this.#keystate(122);
2055
+ }
2056
+ get browserForward() {
2057
+ return this.#keystate(123);
2058
+ }
2059
+ get browserHome() {
2060
+ return this.#keystate(124);
2061
+ }
2062
+ get browserRefresh() {
2063
+ return this.#keystate(125);
2064
+ }
2065
+ get browserSearch() {
2066
+ return this.#keystate(126);
2067
+ }
2068
+ get browserStop() {
2069
+ return this.#keystate(127);
2070
+ }
2071
+ get eject() {
2072
+ return this.#keystate(128);
2073
+ }
2074
+ get launchApp1() {
2075
+ return this.#keystate(129);
2076
+ }
2077
+ get launchApp2() {
2078
+ return this.#keystate(130);
2079
+ }
2080
+ get launchMail() {
2081
+ return this.#keystate(131);
2082
+ }
2083
+ get mediaPlayPause() {
2084
+ return this.#keystate(132);
2085
+ }
2086
+ get mediaSelect() {
2087
+ return this.#keystate(133);
2088
+ }
2089
+ get mediaStop() {
2090
+ return this.#keystate(134);
2091
+ }
2092
+ get mediaTrackNext() {
2093
+ return this.#keystate(135);
2094
+ }
2095
+ get mediaTrackPrevious() {
2096
+ return this.#keystate(136);
2097
+ }
2098
+ get power() {
2099
+ return this.#keystate(137);
2100
+ }
2101
+ get sleep() {
2102
+ return this.#keystate(138);
2103
+ }
2104
+ get audioVolumeDown() {
2105
+ return this.#keystate(139);
2106
+ }
2107
+ get audioVolumeMute() {
2108
+ return this.#keystate(140);
2109
+ }
2110
+ get audioVolumeUp() {
2111
+ return this.#keystate(141);
2112
+ }
2113
+ get wakeUp() {
2114
+ return this.#keystate(142);
2115
+ }
2116
+ get hyper() {
2117
+ return this.#keystate(143);
2118
+ }
2119
+ get super() {
2120
+ return this.#keystate(144);
2121
+ }
2122
+ get turbo() {
2123
+ return this.#keystate(145);
2124
+ }
2125
+ get abort() {
2126
+ return this.#keystate(146);
2127
+ }
2128
+ get resume() {
2129
+ return this.#keystate(147);
2130
+ }
2131
+ get suspend() {
2132
+ return this.#keystate(148);
2133
+ }
2134
+ get again() {
2135
+ return this.#keystate(149);
2136
+ }
2137
+ get copy() {
2138
+ return this.#keystate(150);
2139
+ }
2140
+ get cut() {
2141
+ return this.#keystate(151);
2142
+ }
2143
+ get find() {
2144
+ return this.#keystate(152);
2145
+ }
2146
+ get open() {
2147
+ return this.#keystate(153);
2148
+ }
2149
+ get paste() {
2150
+ return this.#keystate(154);
2151
+ }
2152
+ get props() {
2153
+ return this.#keystate(155);
2154
+ }
2155
+ get select() {
2156
+ return this.#keystate(156);
2157
+ }
2158
+ get undo() {
2159
+ return this.#keystate(157);
2160
+ }
2161
+ get hiragana() {
2162
+ return this.#keystate(158);
2163
+ }
2164
+ get katakana() {
2165
+ return this.#keystate(159);
2166
+ }
2167
+ get f1() {
2168
+ return this.#keystate(160);
2169
+ }
2170
+ get f2() {
2171
+ return this.#keystate(161);
2172
+ }
2173
+ get f3() {
2174
+ return this.#keystate(162);
2175
+ }
2176
+ get f4() {
2177
+ return this.#keystate(163);
2178
+ }
2179
+ get f5() {
2180
+ return this.#keystate(164);
2181
+ }
2182
+ get f6() {
2183
+ return this.#keystate(165);
2184
+ }
2185
+ get f7() {
2186
+ return this.#keystate(166);
2187
+ }
2188
+ get f8() {
2189
+ return this.#keystate(167);
2190
+ }
2191
+ get f9() {
2192
+ return this.#keystate(168);
2193
+ }
2194
+ get f10() {
2195
+ return this.#keystate(169);
2196
+ }
2197
+ get f11() {
2198
+ return this.#keystate(170);
2199
+ }
2200
+ get f12() {
2201
+ return this.#keystate(171);
2202
+ }
2203
+ get f13() {
2204
+ return this.#keystate(172);
2205
+ }
2206
+ get f14() {
2207
+ return this.#keystate(173);
2208
+ }
2209
+ get f15() {
2210
+ return this.#keystate(174);
2211
+ }
2212
+ get f16() {
2213
+ return this.#keystate(175);
2214
+ }
2215
+ get f17() {
2216
+ return this.#keystate(176);
2217
+ }
2218
+ get f18() {
2219
+ return this.#keystate(177);
2220
+ }
2221
+ get f19() {
2222
+ return this.#keystate(178);
2223
+ }
2224
+ get f20() {
2225
+ return this.#keystate(179);
2226
+ }
2227
+ get f21() {
2228
+ return this.#keystate(180);
2229
+ }
2230
+ get f22() {
2231
+ return this.#keystate(181);
2232
+ }
2233
+ get f23() {
2234
+ return this.#keystate(182);
2235
+ }
2236
+ get f24() {
2237
+ return this.#keystate(183);
2238
+ }
2239
+ get f25() {
2240
+ return this.#keystate(184);
2241
+ }
2242
+ get f26() {
2243
+ return this.#keystate(185);
2244
+ }
2245
+ get f27() {
2246
+ return this.#keystate(186);
2247
+ }
2248
+ get f28() {
2249
+ return this.#keystate(187);
2250
+ }
2251
+ get f29() {
2252
+ return this.#keystate(188);
2253
+ }
2254
+ get f30() {
2255
+ return this.#keystate(189);
2256
+ }
2257
+ get f31() {
2258
+ return this.#keystate(190);
2259
+ }
2260
+ get f32() {
2261
+ return this.#keystate(191);
2262
+ }
2263
+ get f33() {
2264
+ return this.#keystate(192);
2265
+ }
2266
+ get f34() {
2267
+ return this.#keystate(193);
2268
+ }
2269
+ get f35() {
2270
+ return this.#keystate(194);
2271
+ }
2272
+ #keystate(code) {
2273
+ let state;
2274
+ if (this.#keystates.has(code)) {
2275
+ state = this.#keystates.get(code);
2276
+ } else {
2277
+ state = { down: false, held: false, up: false };
2278
+ this.#keystates.set(code, state);
2279
+ }
2280
+ state.held = !!(this.#dataView.getUint8(code) & 1);
2281
+ state.down = state.held && !(this.#dataView.getUint8(code) & 2);
2282
+ state.up = !state.held && !!(this.#dataView.getUint8(code) & 2);
2283
+ return state;
2284
+ }
2285
+ }
2286
+ var DEFAULT_WASM_URL2 = new URL("../wasm/bloop.wasm", import.meta.url);
2287
+ var TIME_CTX_OFFSET2 = 0;
2288
+ var INPUT_CTX_OFFSET2 = TIME_CTX_OFFSET2 + 4;
2289
+ var EVENTS_OFFSET2 = INPUT_CTX_OFFSET2 + 4;
2290
+
2291
+ // src/App.ts
2292
+ async function start(opts) {
2293
+ if (!opts.sim) {
2294
+ const { sim } = await mount({
2295
+ hooks: opts.game.hooks,
2296
+ startRecording: opts.startRecording ?? true,
2297
+ wasmUrl: opts.engineWasmUrl
2298
+ });
2299
+ opts.sim = sim;
2300
+ }
2301
+ const app = new App(opts.sim, opts.game);
2302
+ return app;
2303
+ }
2304
+
2305
+ class App {
2306
+ #sim;
2307
+ game;
2308
+ #rafHandle = null;
2309
+ #unsubscribe = null;
2310
+ #now = performance.now();
2311
+ constructor(sim, game) {
2312
+ this.#sim = sim;
2313
+ this.game = game;
2314
+ this.subscribe();
2315
+ }
2316
+ get sim() {
2317
+ return this.#sim;
2318
+ }
2319
+ set sim(sim) {
2320
+ this.#sim = sim;
2321
+ }
2322
+ subscribe() {
2323
+ const handleKeydown = (event) => {
2324
+ this.sim.emit.keydown(event.code);
2325
+ };
2326
+ window.addEventListener("keydown", handleKeydown);
2327
+ const handleKeyup = (event) => {
2328
+ this.sim.emit.keyup(event.code);
2329
+ };
2330
+ window.addEventListener("keyup", handleKeyup);
2331
+ const handleMousemove = (event) => {
2332
+ this.sim.emit.mousemove(event.clientX, event.clientY);
2333
+ };
2334
+ window.addEventListener("mousemove", handleMousemove);
2335
+ const handleMousedown = (event) => {
2336
+ this.sim.emit.mousedown(mouseButtonCodeToMouseButton2(event.button + 1));
2337
+ };
2338
+ window.addEventListener("mousedown", handleMousedown);
2339
+ const handleMousewheel = (event) => {
2340
+ this.sim.emit.mousewheel(event.deltaX, event.deltaY);
2341
+ };
2342
+ window.addEventListener("wheel", handleMousewheel);
2343
+ const playbarHotkeys = (event) => {
2344
+ const isPauseHotkey = event.key === "Enter" && (event.ctrlKey || event.metaKey);
2345
+ if (isPauseHotkey || event.key === "6") {
2346
+ this.sim.isPaused ? this.sim.unpause() : this.sim.pause();
2347
+ }
2348
+ if (this.sim.isPaused) {
2349
+ switch (event.key) {
2350
+ case ",":
2351
+ case "5":
2352
+ this.sim.stepBack();
2353
+ break;
2354
+ case ".":
2355
+ case "7":
2356
+ this.sim.step();
2357
+ break;
2358
+ }
2359
+ }
2360
+ };
2361
+ window.addEventListener("keydown", playbarHotkeys);
2362
+ const frame = () => {
2363
+ this.sim.step(performance.now() - this.#now);
2364
+ this.#now = performance.now();
2365
+ this.#rafHandle = requestAnimationFrame(frame);
2366
+ };
2367
+ frame();
2368
+ this.#unsubscribe = () => {
2369
+ window.removeEventListener("keydown", handleKeydown);
2370
+ window.removeEventListener("keyup", handleKeyup);
2371
+ window.removeEventListener("mousemove", handleMousemove);
2372
+ window.removeEventListener("mousedown", handleMousedown);
2373
+ window.removeEventListener("wheel", handleMousewheel);
2374
+ window.removeEventListener("keydown", playbarHotkeys);
2375
+ if (this.#rafHandle != null) {
2376
+ cancelAnimationFrame(this.#rafHandle);
2377
+ }
2378
+ };
2379
+ }
2380
+ cleanup() {
2381
+ this.#unsubscribe?.();
2382
+ this.sim.unmount();
2383
+ }
2384
+ async acceptHmr(module, opts) {
2385
+ const game = module.game ?? module;
2386
+ if (!game.hooks) {
2387
+ throw new Error(`HMR: missing game.hooks export on module: ${JSON.stringify(module)}`);
2388
+ }
2389
+ this.sim.pause();
2390
+ const { sim } = await mount({
2391
+ hooks: game.hooks,
2392
+ wasmUrl: new URL("/bloop-wasm/bloop.wasm", window.location.href),
2393
+ ...opts
2394
+ });
2395
+ sim.cloneSession(this.sim);
2396
+ this.sim.unmount();
2397
+ this.sim = sim;
2398
+ this.game = game;
2399
+ }
1006
2400
  }
1007
2401
  export {
1008
- connect
2402
+ start,
2403
+ App
1009
2404
  };
1010
2405
 
1011
- //# debugId=CED373C6D92686EE64756E2164756E21
2406
+ //# debugId=2D6099B6C20ED59864756E2164756E21
1012
2407
  //# sourceMappingURL=mod.js.map