swf2js 0.1.1 → 0.1.2
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 +9 -9
- package/package.json +2 -1
- package/src/cli.js +15 -6
- package/src/emitter/bundle-emitter.js +56 -16
- package/src/runtime/avm1-runtime.js +329 -84
- package/src/runtime/avm2-runtime.js +56 -6
- package/src/runtime/embedded-movie-runtime.js +6 -1
- package/src/runtime/index.js +2 -6
- package/src/runtime/movie-runtime.js +2 -1
- package/src/runtime/swflib/assemble.js +34 -0
- package/src/runtime/swflib/bitmap-image.js +4 -0
- package/src/runtime/swflib/bitmap.js +1004 -0
- package/src/runtime/swflib/builtins.js +156 -0
- package/src/runtime/swflib/bytearray.js +949 -0
- package/src/runtime/swflib/compiled-swf.js +3 -0
- package/src/runtime/swflib/context-menu.js +245 -0
- package/src/runtime/swflib/cursor-flag.js +12 -0
- package/src/runtime/swflib/dictionary.js +121 -0
- package/src/runtime/swflib/display.js +823 -0
- package/src/runtime/swflib/events.js +754 -0
- package/src/runtime/swflib/filters.js +298 -0
- package/src/runtime/swflib/flash.js +130 -0
- package/src/runtime/swflib/geom.js +759 -0
- package/src/runtime/swflib/glyph-metrics.js +14 -0
- package/src/runtime/swflib/graphics.js +735 -0
- package/src/runtime/swflib/host.js +2 -0
- package/src/runtime/swflib/inherit.js +22 -0
- package/src/runtime/swflib/input.js +1085 -0
- package/src/runtime/swflib/loader.js +687 -0
- package/src/runtime/swflib/marks.js +16 -0
- package/src/runtime/swflib/matrix3d.js +144 -0
- package/src/runtime/swflib/pointer-bridge.js +8 -0
- package/src/runtime/swflib/projection.js +158 -0
- package/src/runtime/swflib/render.js +1240 -0
- package/src/runtime/swflib/sound.js +1166 -0
- package/src/runtime/swflib/sprite-drag.js +116 -0
- package/src/runtime/swflib/stage-ref.js +9 -0
- package/src/runtime/swflib/stage.js +484 -0
- package/src/runtime/swflib/system.js +987 -0
- package/src/runtime/swflib/text-field.js +519 -0
- package/src/runtime/swflib/text-paint.js +797 -0
- package/src/runtime/swflib/text.js +1387 -0
- package/src/runtime/swflib/transform.js +133 -0
- package/src/runtime/swflib/xml.js +268 -0
- package/src/transpiler/avm1/avm1-ir.js +2 -1
- package/src/transpiler/avm1/avm1-rewrite.js +23 -2
- package/src/transpiler/avm1/avm1-transpiler.js +4 -1
- package/src/transpiler/avm2/avm2-transpiler.js +38 -7
- package/src/transpiler/timeline/timeline-codegen.js +366 -123
- package/src/runtime/swflib.js +0 -14050
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sprite mouse/touch drag state.
|
|
3
|
+
* Kept out of graphics.js so Sprite methods can call here without importing input.
|
|
4
|
+
*/
|
|
5
|
+
import { isStageDescendant } from './display.js';
|
|
6
|
+
import { Point, Rectangle } from './geom.js';
|
|
7
|
+
import { stageRef } from './stage-ref.js';
|
|
8
|
+
|
|
9
|
+
/** Matches MultitouchInputMode.TOUCH_POINT without importing filters (cycle-safe). */
|
|
10
|
+
const TOUCH_POINT = 'touchPoint';
|
|
11
|
+
|
|
12
|
+
export let activeMouseDrag = null;
|
|
13
|
+
export let activeTouchDrag = null;
|
|
14
|
+
export const activeCanvasTouches = new Map();
|
|
15
|
+
|
|
16
|
+
export function setActiveMouseDrag(value) { activeMouseDrag = value; }
|
|
17
|
+
export function setActiveTouchDrag(value) { activeTouchDrag = value; }
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Topmost display object under the stage point, excluding `dragged` and its
|
|
21
|
+
* descendants — Flash Sprite.dropTarget / getObjectsUnderPoint style.
|
|
22
|
+
*/
|
|
23
|
+
export function spriteDropTargetAt(stageX, stageY, dragged) {
|
|
24
|
+
var stage = stageRef.current;
|
|
25
|
+
if (!stage) return null;
|
|
26
|
+
function walk(node) {
|
|
27
|
+
if (!node || node.visible === false) return null;
|
|
28
|
+
if (node === dragged) return null;
|
|
29
|
+
var kids = node._children;
|
|
30
|
+
if (kids) {
|
|
31
|
+
for (var i = kids.length - 1; i >= 0; i--) {
|
|
32
|
+
var found = walk(kids[i]);
|
|
33
|
+
if (found) return found;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (node === stage) return null;
|
|
37
|
+
try {
|
|
38
|
+
if (typeof node.hitTestPoint === 'function' && node.hitTestPoint(stageX, stageY, true)) return node;
|
|
39
|
+
} catch (_) {}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
return walk(stage);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function refreshDropTarget(drag, stageX, stageY) {
|
|
46
|
+
if (!drag?.sprite) return;
|
|
47
|
+
drag.sprite._dropTarget = spriteDropTargetAt(stageX, stageY, drag.sprite);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function createSpriteDrag(sprite, x, y, lockCenter, bounds) {
|
|
51
|
+
var parent = sprite.parent;
|
|
52
|
+
var stage = stageRef.current;
|
|
53
|
+
if (!parent || !stage || !isStageDescendant(stage, sprite)) return null;
|
|
54
|
+
var point = parent.globalToLocal(new Point(x, y));
|
|
55
|
+
if (!Number.isFinite(point.x) || !Number.isFinite(point.y)) return null;
|
|
56
|
+
return {
|
|
57
|
+
sprite: sprite, parent: parent,
|
|
58
|
+
offsetX: lockCenter ? 0 : sprite.x - point.x,
|
|
59
|
+
offsetY: lockCenter ? 0 : sprite.y - point.y,
|
|
60
|
+
bounds: bounds ? new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height) : null,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function advanceSpriteDrag(drag, stageX, stageY) {
|
|
65
|
+
var stage = stageRef.current;
|
|
66
|
+
if (drag.parent !== drag.sprite.parent || !stage || !isStageDescendant(stage, drag.sprite)) return false;
|
|
67
|
+
var local = drag.parent.globalToLocal(new Point(stageX, stageY));
|
|
68
|
+
if (!Number.isFinite(local.x) || !Number.isFinite(local.y)) return true;
|
|
69
|
+
var x = local.x + drag.offsetX, y = local.y + drag.offsetY;
|
|
70
|
+
var bounds = drag.bounds;
|
|
71
|
+
if (bounds) {
|
|
72
|
+
x = Math.max(bounds.x, Math.min(bounds.right, x));
|
|
73
|
+
y = Math.max(bounds.y, Math.min(bounds.bottom, y));
|
|
74
|
+
}
|
|
75
|
+
drag.sprite.x = x;
|
|
76
|
+
drag.sprite.y = y;
|
|
77
|
+
refreshDropTarget(drag, stageX, stageY);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function advanceTouchDrag(contact) {
|
|
82
|
+
var drag = activeTouchDrag;
|
|
83
|
+
if (drag?.contact === contact && !advanceSpriteDrag(drag, contact.x, contact.y)) activeTouchDrag = null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function startSpriteMouseDrag(sprite, lockCenter, bounds) {
|
|
87
|
+
var stage = stageRef.current;
|
|
88
|
+
if (!stage) return;
|
|
89
|
+
var drag = createSpriteDrag(sprite, stage.mouseX, stage.mouseY, lockCenter, bounds);
|
|
90
|
+
if (!drag) return;
|
|
91
|
+
activeMouseDrag = drag;
|
|
92
|
+
if (lockCenter) advanceSpriteDrag(drag, stage.mouseX, stage.mouseY);
|
|
93
|
+
else refreshDropTarget(drag, stage.mouseX, stage.mouseY);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function stopSpriteMouseDrag(sprite) {
|
|
97
|
+
if (activeMouseDrag?.sprite === sprite) activeMouseDrag = null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function startSpriteTouchDrag(sprite, id, lockCenter, bounds) {
|
|
101
|
+
var contact = activeCanvasTouches.get(id | 0);
|
|
102
|
+
if (!contact || contact.mode !== TOUCH_POINT) return;
|
|
103
|
+
var drag = createSpriteDrag(sprite, contact.x, contact.y, lockCenter, bounds);
|
|
104
|
+
if (!drag) return;
|
|
105
|
+
drag.contact = contact;
|
|
106
|
+
activeTouchDrag = drag;
|
|
107
|
+
if (lockCenter) advanceTouchDrag(contact);
|
|
108
|
+
else refreshDropTarget(drag, contact.x, contact.y);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function stopSpriteTouchDrag(sprite, id) {
|
|
112
|
+
if (activeTouchDrag?.sprite === sprite &&
|
|
113
|
+
activeTouchDrag.contact === activeCanvasTouches.get(id | 0)) {
|
|
114
|
+
activeTouchDrag = null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The installed stage, filled in by installStage after the display list exists. */
|
|
2
|
+
export const stageRef = {
|
|
3
|
+
current: null,
|
|
4
|
+
defaultPerspective() { return null; },
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export function isStage(node) {
|
|
8
|
+
return !!node && (node._swoofIsStage === true || node === stageRef.current);
|
|
9
|
+
}
|
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import { cursorFlag, setCursorFlagChange } from './cursor-flag.js';
|
|
2
|
+
export { cursorFlag };
|
|
3
|
+
import { BitmapData } from './bitmap.js';
|
|
4
|
+
import { DisplayObjectContainer, childrenBeingRemoved, displayBoundsInTarget, displayObjectMatrix, displayTreePixelBounds, isStageDescendant, mapBounds } from './display.js';
|
|
5
|
+
import { Event, FocusEvent, KeyboardEvent, MouseEvent, dispatchStageBroadcast } from './events.js';
|
|
6
|
+
import { MouseCursor } from './filters.js';
|
|
7
|
+
import { Point } from './geom.js';
|
|
8
|
+
import { isSprite } from './marks.js';
|
|
9
|
+
import { global } from './host.js';
|
|
10
|
+
import { stageRef } from './stage-ref.js';
|
|
11
|
+
import { _keyboardButtonCapture, dispatchButtonEvent, dispatchEmbeddedAvm1Key, setKeyboardButtonCapture } from './input.js';
|
|
12
|
+
import { LoaderInfo, MovieClip, SimpleButton } from './loader.js';
|
|
13
|
+
|
|
14
|
+
export var mouseCursorCanvas = null;
|
|
15
|
+
export var mouseCursorStageWrap = null;
|
|
16
|
+
export function setMouseCursorCanvas(value) { mouseCursorCanvas = value; }
|
|
17
|
+
export function setMouseCursorStageWrap(value) { mouseCursorStageWrap = value; }
|
|
18
|
+
export var mouseCursorTarget = null;
|
|
19
|
+
export var mouseCursorLegacyControl = false;
|
|
20
|
+
export var registeredMouseCursors = new Map();
|
|
21
|
+
export var mouseCursorAnimation = null;
|
|
22
|
+
export class MouseCursorData {
|
|
23
|
+
constructor() {
|
|
24
|
+
this.data = null;
|
|
25
|
+
this.frameRate = 0;
|
|
26
|
+
this.hotSpot = new Point();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export function mouseCursorFrameStyle(bitmap, hotSpot) {
|
|
30
|
+
if (!(bitmap instanceof BitmapData) || bitmap._disposed || !bitmap.width || !bitmap.height) {
|
|
31
|
+
throw new TypeError('A mouse cursor frame requires a live BitmapData');
|
|
32
|
+
}
|
|
33
|
+
if (bitmap.width > 32 || bitmap.height > 32) throw new RangeError('Mouse cursor frames are limited to 32 by 32 pixels');
|
|
34
|
+
var image = bitmap._getDisplayImage();
|
|
35
|
+
var url = image?.toDataURL?.('image/png') || image?.currentSrc || image?.src;
|
|
36
|
+
if (!url || url === 'data:,') throw new TypeError('Mouse cursor frame has no browser image');
|
|
37
|
+
var x = Math.max(0, Math.min(bitmap.width - 1, Math.trunc(Number(hotSpot?.x) || 0)));
|
|
38
|
+
var y = Math.max(0, Math.min(bitmap.height - 1, Math.trunc(Number(hotSpot?.y) || 0)));
|
|
39
|
+
return 'url(' + JSON.stringify(url) + ') ' + x + ' ' + y + ', auto';
|
|
40
|
+
}
|
|
41
|
+
export function syncMouseCursorAnimation(cursor) {
|
|
42
|
+
if (mouseCursorAnimation?.cursor === cursor) return;
|
|
43
|
+
if (mouseCursorAnimation) {
|
|
44
|
+
clearInterval(mouseCursorAnimation.timer);
|
|
45
|
+
mouseCursorAnimation = null;
|
|
46
|
+
}
|
|
47
|
+
if (!cursor || cursor.frames.length < 2 || !(cursor.frameRate > 0)) return;
|
|
48
|
+
var animation = { cursor: cursor, frame: 0 };
|
|
49
|
+
mouseCursorAnimation = animation;
|
|
50
|
+
animation.timer = setInterval(function () {
|
|
51
|
+
if (mouseCursorAnimation !== animation) return;
|
|
52
|
+
animation.frame = (animation.frame + 1) % cursor.frames.length;
|
|
53
|
+
updateMouseCursor();
|
|
54
|
+
}, Math.max(1, 1000 / cursor.frameRate));
|
|
55
|
+
}
|
|
56
|
+
setCursorFlagChange(function (target) {
|
|
57
|
+
if (target === mouseCursorTarget) updateMouseCursor();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export function cssMouseCursor() {
|
|
61
|
+
if (Mouse._hidden) return 'none';
|
|
62
|
+
var custom = registeredMouseCursors.get(Mouse._cursor);
|
|
63
|
+
if (custom) return custom.frames[mouseCursorAnimation?.frame ?? 0];
|
|
64
|
+
switch (Mouse._cursor) {
|
|
65
|
+
case MouseCursor.ARROW: return 'default';
|
|
66
|
+
case MouseCursor.BUTTON: return 'pointer';
|
|
67
|
+
case MouseCursor.HAND: return 'grab';
|
|
68
|
+
case MouseCursor.IBEAM: return 'text';
|
|
69
|
+
default: return Mouse._cursor;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export function updateMouseCursor() {
|
|
73
|
+
if (!mouseCursorCanvas?.style) return;
|
|
74
|
+
syncMouseCursorAnimation(Mouse._hidden ? null : registeredMouseCursors.get(Mouse._cursor));
|
|
75
|
+
var cursor = cssMouseCursor();
|
|
76
|
+
if (mouseCursorStageWrap?.style && mouseCursorStageWrap.style.cursor !== cursor) {
|
|
77
|
+
mouseCursorStageWrap.style.cursor = cursor;
|
|
78
|
+
}
|
|
79
|
+
if (cursor === 'auto') {
|
|
80
|
+
var usesHandCursor = mouseCursorLegacyControl
|
|
81
|
+
? (!mouseCursorTarget || mouseCursorTarget.useHandCursor) && (mouseCursorTarget?.enabled ?? true)
|
|
82
|
+
: mouseCursorTarget?.useHandCursor &&
|
|
83
|
+
(mouseCursorTarget instanceof SimpleButton ? mouseCursorTarget.enabled : mouseCursorTarget.buttonMode);
|
|
84
|
+
if (usesHandCursor) cursor = 'pointer';
|
|
85
|
+
}
|
|
86
|
+
if (mouseCursorCanvas.style.cursor !== cursor) mouseCursorCanvas.style.cursor = cursor;
|
|
87
|
+
}
|
|
88
|
+
export function setMouseCursorTarget(target, legacyControl = false) {
|
|
89
|
+
mouseCursorTarget = target;
|
|
90
|
+
mouseCursorLegacyControl = legacyControl;
|
|
91
|
+
updateMouseCursor();
|
|
92
|
+
}
|
|
93
|
+
export var Mouse = {
|
|
94
|
+
_cursor: 'auto',
|
|
95
|
+
_hidden: false,
|
|
96
|
+
_isDown: false,
|
|
97
|
+
get cursor() { return this._cursor; },
|
|
98
|
+
set cursor(v) {
|
|
99
|
+
this._cursor = v || 'auto';
|
|
100
|
+
updateMouseCursor();
|
|
101
|
+
},
|
|
102
|
+
get supportsCursor() {
|
|
103
|
+
return typeof document !== 'undefined' && (global.matchMedia?.('(any-hover: hover)').matches ?? true);
|
|
104
|
+
},
|
|
105
|
+
get supportsNativeCursor() {
|
|
106
|
+
return this.supportsCursor && !!global.CSS?.supports('cursor', 'url("data:image/png;base64,") 0 0, auto');
|
|
107
|
+
},
|
|
108
|
+
registerCursor: function (name, cursor) {
|
|
109
|
+
name = String(name ?? '');
|
|
110
|
+
if (!name || !(cursor instanceof MouseCursorData) || !Array.isArray(cursor.data) || !cursor.data.length) {
|
|
111
|
+
throw new TypeError('Expected a named MouseCursorData with at least one frame');
|
|
112
|
+
}
|
|
113
|
+
var frames = Array.from(cursor.data, function (bitmap) { return mouseCursorFrameStyle(bitmap, cursor.hotSpot); });
|
|
114
|
+
var frameRate = Number(cursor.frameRate);
|
|
115
|
+
registeredMouseCursors.set(name, { frames: frames, frameRate: Number.isFinite(frameRate) ? frameRate : 0 });
|
|
116
|
+
updateMouseCursor();
|
|
117
|
+
},
|
|
118
|
+
unregisterCursor: function (name) {
|
|
119
|
+
name = String(name ?? '');
|
|
120
|
+
if (!registeredMouseCursors.delete(name)) return;
|
|
121
|
+
if (this._cursor === name) this._cursor = MouseCursor.AUTO;
|
|
122
|
+
updateMouseCursor();
|
|
123
|
+
},
|
|
124
|
+
hide: function () {
|
|
125
|
+
this._hidden = true;
|
|
126
|
+
updateMouseCursor();
|
|
127
|
+
},
|
|
128
|
+
show: function () {
|
|
129
|
+
this._hidden = false;
|
|
130
|
+
updateMouseCursor();
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export var Keyboard = {
|
|
135
|
+
BACKSPACE: 8, TAB: 9, ENTER: 13, SHIFT: 16, CONTROL: 17, ALT: 18, ESCAPE: 27,
|
|
136
|
+
SPACE: 32, PAGE_UP: 33, PAGE_DOWN: 34, END: 35, HOME: 36,
|
|
137
|
+
LEFT: 37, UP: 38, RIGHT: 39, DOWN: 40, INSERT: 45, DELETE: 46,
|
|
138
|
+
NUMBER_0: 48, NUMBER_1: 49, NUMBER_2: 50, NUMBER_3: 51, NUMBER_4: 52,
|
|
139
|
+
NUMBER_5: 53, NUMBER_6: 54, NUMBER_7: 55, NUMBER_8: 56, NUMBER_9: 57,
|
|
140
|
+
A: 65, B: 66, C: 67, D: 68, E: 69, F: 70, G: 71, H: 72, I: 73, J: 74,
|
|
141
|
+
K: 75, L: 76, M: 77, N: 78, O: 79, P: 80, Q: 81, R: 82, S: 83, T: 84,
|
|
142
|
+
U: 85, V: 86, W: 87, X: 88, Y: 89, Z: 90,
|
|
143
|
+
F1: 112, F2: 113, F3: 114, F4: 115, F5: 116, F6: 117,
|
|
144
|
+
F7: 118, F8: 119, F9: 120, F10: 121, F11: 122, F12: 123,
|
|
145
|
+
NUMPAD_0: 96, NUMPAD_1: 97, NUMPAD_2: 98, NUMPAD_3: 99, NUMPAD_4: 100,
|
|
146
|
+
NUMPAD_5: 101, NUMPAD_6: 102, NUMPAD_7: 103, NUMPAD_8: 104, NUMPAD_9: 105,
|
|
147
|
+
NUMPAD_MULTIPLY: 106, NUMPAD_ADD: 107, NUMPAD_SUBTRACT: 109, NUMPAD_DECIMAL: 110, NUMPAD_DIVIDE: 111,
|
|
148
|
+
SEMICOLON: 186, EQUAL: 187, COMMA: 188, MINUS: 189, PERIOD: 190, SLASH: 191,
|
|
149
|
+
BACKQUOTE: 192, LEFTBRACKET: 219, BACKSLASH: 220, RIGHTBRACKET: 221, QUOTE: 222,
|
|
150
|
+
capsLock: false, numLock: false,
|
|
151
|
+
};
|
|
152
|
+
export var keyboardDown = Object.create(null);
|
|
153
|
+
export var keyboardLastCode = 0;
|
|
154
|
+
export var keyboardLastChar = 0;
|
|
155
|
+
export var Key = {
|
|
156
|
+
_down: keyboardDown,
|
|
157
|
+
_last: 0,
|
|
158
|
+
isDown: function (code) { return !!keyboardDown[code | 0]; },
|
|
159
|
+
isToggled: function (code) {
|
|
160
|
+
code = code | 0;
|
|
161
|
+
return !!((code === 20 && Keyboard.capsLock) || (code === 144 && Keyboard.numLock));
|
|
162
|
+
},
|
|
163
|
+
getCode: function () { return keyboardLastCode; },
|
|
164
|
+
getAscii: function () { return keyboardLastChar; },
|
|
165
|
+
BACKSPACE: Keyboard.BACKSPACE, END: Keyboard.END, HOME: Keyboard.HOME,
|
|
166
|
+
CAPSLOCK: 20, SPACE: 32, ENTER: 13, UP: 38, DOWN: 40, LEFT: 37, RIGHT: 39,
|
|
167
|
+
};
|
|
168
|
+
export var namedKeyboardCodes = Object.assign(Object.create(null), {
|
|
169
|
+
CapsLock: Key.CAPSLOCK, NumLock: 144,
|
|
170
|
+
Backspace: Keyboard.BACKSPACE, Tab: Keyboard.TAB, Enter: Keyboard.ENTER, NumpadEnter: Keyboard.ENTER,
|
|
171
|
+
Shift: Keyboard.SHIFT, ShiftLeft: Keyboard.SHIFT, ShiftRight: Keyboard.SHIFT,
|
|
172
|
+
Control: Keyboard.CONTROL, ControlLeft: Keyboard.CONTROL, ControlRight: Keyboard.CONTROL,
|
|
173
|
+
Alt: Keyboard.ALT, AltLeft: Keyboard.ALT, AltRight: Keyboard.ALT,
|
|
174
|
+
Escape: Keyboard.ESCAPE, Esc: Keyboard.ESCAPE,
|
|
175
|
+
' ': Keyboard.SPACE, Space: Keyboard.SPACE, Spacebar: Keyboard.SPACE,
|
|
176
|
+
PageUp: Keyboard.PAGE_UP, PageDown: Keyboard.PAGE_DOWN, End: Keyboard.END, Home: Keyboard.HOME,
|
|
177
|
+
ArrowLeft: Keyboard.LEFT, ArrowUp: Keyboard.UP, ArrowRight: Keyboard.RIGHT, ArrowDown: Keyboard.DOWN,
|
|
178
|
+
Insert: Keyboard.INSERT, Delete: Keyboard.DELETE,
|
|
179
|
+
F1: Keyboard.F1, F2: Keyboard.F2, F3: Keyboard.F3, F4: Keyboard.F4, F5: Keyboard.F5, F6: Keyboard.F6,
|
|
180
|
+
F7: Keyboard.F7, F8: Keyboard.F8, F9: Keyboard.F9, F10: Keyboard.F10, F11: Keyboard.F11, F12: Keyboard.F12,
|
|
181
|
+
NumpadMultiply: Keyboard.NUMPAD_MULTIPLY, NumpadAdd: Keyboard.NUMPAD_ADD,
|
|
182
|
+
NumpadSubtract: Keyboard.NUMPAD_SUBTRACT, NumpadDecimal: Keyboard.NUMPAD_DECIMAL,
|
|
183
|
+
NumpadDivide: Keyboard.NUMPAD_DIVIDE,
|
|
184
|
+
Semicolon: Keyboard.SEMICOLON, Equal: Keyboard.EQUAL, Comma: Keyboard.COMMA, Minus: Keyboard.MINUS,
|
|
185
|
+
Period: Keyboard.PERIOD, Slash: Keyboard.SLASH, Backquote: Keyboard.BACKQUOTE,
|
|
186
|
+
BracketLeft: Keyboard.LEFTBRACKET, Backslash: Keyboard.BACKSLASH,
|
|
187
|
+
BracketRight: Keyboard.RIGHTBRACKET, Quote: Keyboard.QUOTE,
|
|
188
|
+
});
|
|
189
|
+
export function keyboardCode(e) {
|
|
190
|
+
if (!e) return 0;
|
|
191
|
+
var legacy = e.keyCode | 0;
|
|
192
|
+
if (legacy <= 0) legacy = e.which | 0;
|
|
193
|
+
if (legacy > 0) return legacy;
|
|
194
|
+
var key = typeof e.key === 'string' ? e.key : '';
|
|
195
|
+
// This keeps the layout's letter for Key.getCode while allowing shifted
|
|
196
|
+
// punctuation to fall back to its physical key.
|
|
197
|
+
if (/^[a-z]$/i.test(key)) return key.toUpperCase().charCodeAt(0);
|
|
198
|
+
var code = typeof e.code === 'string' ? e.code : '';
|
|
199
|
+
var named = namedKeyboardCodes[key] || namedKeyboardCodes[code];
|
|
200
|
+
if (named) return named;
|
|
201
|
+
if (/^Digit[0-9]$/.test(code)) return code.charCodeAt(5);
|
|
202
|
+
if (/^Numpad[0-9]$/.test(code)) return Keyboard.NUMPAD_0 + Number(code[6]);
|
|
203
|
+
if (/^Key[A-Z]$/.test(code)) return code.charCodeAt(3);
|
|
204
|
+
return key.length === 1 ? key.toUpperCase().charCodeAt(0) : 0;
|
|
205
|
+
}
|
|
206
|
+
export function keyboardCharCode(e) {
|
|
207
|
+
if (!e) return 0;
|
|
208
|
+
if (e.charCode) return e.charCode | 0;
|
|
209
|
+
if (e.key === 'Space' || e.key === 'Spacebar') return Keyboard.SPACE;
|
|
210
|
+
return typeof e.key === 'string' && e.key.length === 1 ? e.key.charCodeAt(0) : 0;
|
|
211
|
+
}
|
|
212
|
+
export function updateKeyboardLocks(e) {
|
|
213
|
+
if (typeof e.getModifierState !== 'function') return;
|
|
214
|
+
Keyboard.capsLock = !!e.getModifierState('CapsLock');
|
|
215
|
+
Keyboard.numLock = !!e.getModifierState('NumLock');
|
|
216
|
+
}
|
|
217
|
+
export function clearKeyboardState() {
|
|
218
|
+
Object.keys(keyboardDown).forEach(function (code) { delete keyboardDown[code]; });
|
|
219
|
+
releaseKeyboardButton(false);
|
|
220
|
+
}
|
|
221
|
+
export function releaseKeyboardButton(activate, nativeEvent) {
|
|
222
|
+
var capture = _keyboardButtonCapture;
|
|
223
|
+
if (!capture) return;
|
|
224
|
+
setKeyboardButtonCapture(null);
|
|
225
|
+
var button = capture.button, x = stageSingleton.mouseX, y = stageSingleton.mouseY;
|
|
226
|
+
setMovieClipButtonState(button, mouseCursorTarget === button ? '_over' : '_up');
|
|
227
|
+
dispatchButtonEvent(button, MouseEvent.MOUSE_UP, x, y, nativeEvent);
|
|
228
|
+
if (activate && buttonIsKeyboardTarget(button)) dispatchButtonEvent(button, MouseEvent.CLICK, x, y, nativeEvent);
|
|
229
|
+
}
|
|
230
|
+
export function setMovieClipButtonState(clip, label) {
|
|
231
|
+
if (clip instanceof MovieClip && clip.buttonMode && clip.enabled !== false) {
|
|
232
|
+
clip._swoofLocalTimeline?.gotoButtonState?.(label);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
export function buttonIsKeyboardTarget(button) {
|
|
236
|
+
var buttonMode = button instanceof SimpleButton || isSprite(button) && button.buttonMode;
|
|
237
|
+
if (!buttonMode || button.enabled === false || keyboardFocusTarget() !== button) return false;
|
|
238
|
+
for (var node = button; node; node = node.parent) {
|
|
239
|
+
if (node.visible === false) return false;
|
|
240
|
+
}
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
// One capture listener feeds both AVM1 Key and AS3 stage keyboard events.
|
|
244
|
+
if (typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
|
|
245
|
+
Key._swoofBound = true;
|
|
246
|
+
window.addEventListener('keydown', function (e) {
|
|
247
|
+
var code = keyboardCode(e), charCode = keyboardCharCode(e);
|
|
248
|
+
updateKeyboardLocks(e);
|
|
249
|
+
keyboardDown[code] = true;
|
|
250
|
+
keyboardLastCode = Key._last = code;
|
|
251
|
+
keyboardLastChar = charCode;
|
|
252
|
+
var event = new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, false, charCode, code);
|
|
253
|
+
event.ctrlKey = !!e.ctrlKey; event.altKey = !!e.altKey; event.shiftKey = !!e.shiftKey;
|
|
254
|
+
event.keyLocation = e.location | 0;
|
|
255
|
+
var target = keyboardFocusTarget();
|
|
256
|
+
try { target.dispatchEvent(event); } catch (_) {}
|
|
257
|
+
if (code === 9) advanceStageFocus(!!e.shiftKey, e);
|
|
258
|
+
dispatchEmbeddedAvm1Key('keyDown', e);
|
|
259
|
+
if ((code === Keyboard.ENTER || code === Keyboard.SPACE) && buttonIsKeyboardTarget(target)) {
|
|
260
|
+
if (!_keyboardButtonCapture && !e.repeat && !e.defaultPrevented) {
|
|
261
|
+
setKeyboardButtonCapture({ button: target, code: code });
|
|
262
|
+
setMovieClipButtonState(target, '_down');
|
|
263
|
+
dispatchButtonEvent(target, MouseEvent.MOUSE_DOWN, stageSingleton.mouseX, stageSingleton.mouseY, e);
|
|
264
|
+
}
|
|
265
|
+
if (e.preventDefault) e.preventDefault();
|
|
266
|
+
}
|
|
267
|
+
}, true);
|
|
268
|
+
window.addEventListener('keyup', function (e) {
|
|
269
|
+
var code = keyboardCode(e), charCode = keyboardCharCode(e);
|
|
270
|
+
updateKeyboardLocks(e);
|
|
271
|
+
keyboardDown[code] = false;
|
|
272
|
+
keyboardLastCode = Key._last = code;
|
|
273
|
+
keyboardLastChar = charCode;
|
|
274
|
+
var event = new KeyboardEvent(KeyboardEvent.KEY_UP, true, false, charCode, code);
|
|
275
|
+
event.ctrlKey = !!e.ctrlKey; event.altKey = !!e.altKey; event.shiftKey = !!e.shiftKey;
|
|
276
|
+
event.keyLocation = e.location | 0;
|
|
277
|
+
var target = keyboardFocusTarget();
|
|
278
|
+
try { target.dispatchEvent(event); } catch (_) {}
|
|
279
|
+
dispatchEmbeddedAvm1Key('keyUp', e);
|
|
280
|
+
if (_keyboardButtonCapture && code === _keyboardButtonCapture.code) {
|
|
281
|
+
releaseKeyboardButton(!e.defaultPrevented, e);
|
|
282
|
+
if (e.preventDefault) e.preventDefault();
|
|
283
|
+
}
|
|
284
|
+
}, true);
|
|
285
|
+
window.addEventListener('blur', clearKeyboardState, true);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export class StageClass extends DisplayObjectContainer {
|
|
289
|
+
constructor() {
|
|
290
|
+
super();
|
|
291
|
+
this._swoofIsStage = true;
|
|
292
|
+
this.name = 'stage';
|
|
293
|
+
this.stage = this;
|
|
294
|
+
this.root = this;
|
|
295
|
+
this.frameRate = 30;
|
|
296
|
+
this.stageWidth = 0;
|
|
297
|
+
this.stageHeight = 0;
|
|
298
|
+
this._scaleMode = 'showAll';
|
|
299
|
+
this._align = '';
|
|
300
|
+
this.quality = 'high';
|
|
301
|
+
this.showMenu = false;
|
|
302
|
+
this.stageFocusRect = true;
|
|
303
|
+
this.loaderInfo = new LoaderInfo();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
invalidate() { stageRenderPending = true; }
|
|
307
|
+
}
|
|
308
|
+
Object.defineProperty(StageClass.prototype, 'focus', {
|
|
309
|
+
get: function () { return this._focus || null; },
|
|
310
|
+
set: function (target) {
|
|
311
|
+
if (target === this._focus) return;
|
|
312
|
+
if (target != null && !isStageDescendant(this, target)) throw new Error('Focus target must be on the stage.');
|
|
313
|
+
for (var ancestor = target; ancestor; ancestor = ancestor.parent) {
|
|
314
|
+
if (childrenBeingRemoved.has(ancestor)) return;
|
|
315
|
+
}
|
|
316
|
+
var previous = this._focus || null;
|
|
317
|
+
this._focus = target || null;
|
|
318
|
+
if (this === stageSingleton && _keyboardButtonCapture && _keyboardButtonCapture.button !== target) {
|
|
319
|
+
releaseKeyboardButton(false);
|
|
320
|
+
}
|
|
321
|
+
if (!this._focus && previous && previous._swoofOverlayElement &&
|
|
322
|
+
typeof previous._swoofOverlayElement.blur === 'function') {
|
|
323
|
+
try { previous._swoofOverlayElement.blur(); } catch (_) {}
|
|
324
|
+
}
|
|
325
|
+
var next = this._focus;
|
|
326
|
+
if (previous && typeof previous.dispatchEvent === 'function') previous.dispatchEvent(new FocusEvent(FocusEvent.FOCUS_OUT, true, false, next));
|
|
327
|
+
if (this._focus !== next) return;
|
|
328
|
+
if (this._focus && typeof this._focus.dispatchEvent === 'function') this._focus.dispatchEvent(new FocusEvent(FocusEvent.FOCUS_IN, true, false, previous));
|
|
329
|
+
if (this._focus && this._focus._swoofOverlayElement && typeof this._focus._swoofOverlayElement.focus === 'function') {
|
|
330
|
+
try { this._focus._swoofOverlayElement.focus(); } catch (_) {}
|
|
331
|
+
} else if (this._focus && typeof document !== 'undefined') {
|
|
332
|
+
var canvas = document.getElementById && document.getElementById('stage');
|
|
333
|
+
if (canvas && typeof canvas.focus === 'function') { try { canvas.focus(); } catch (_) {} }
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
export function stageTabOrder(stage) {
|
|
340
|
+
var order = [], sequence = 0;
|
|
341
|
+
function visit(node) {
|
|
342
|
+
if (node?.tabChildren === false) return;
|
|
343
|
+
var kids = node && node._children;
|
|
344
|
+
if (!kids) return;
|
|
345
|
+
for (var i = 0; i < kids.length; i++) {
|
|
346
|
+
var child = kids[i];
|
|
347
|
+
if (!child || child.visible === false || child.enabled === false) continue;
|
|
348
|
+
if (child.tabEnabled && child.mouseEnabled !== false) {
|
|
349
|
+
order.push({ target: child, tabIndex: child.tabIndex | 0, sequence: sequence++ });
|
|
350
|
+
}
|
|
351
|
+
visit(child);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
visit(stage);
|
|
355
|
+
if (order.some(function (entry) { return entry.tabIndex >= 0; })) {
|
|
356
|
+
order = order.filter(function (entry) { return entry.tabIndex >= 0; });
|
|
357
|
+
order.sort(function (a, b) { return a.tabIndex - b.tabIndex || a.sequence - b.sequence; });
|
|
358
|
+
} else {
|
|
359
|
+
var used = new Set();
|
|
360
|
+
for (var entry of order) {
|
|
361
|
+
var target = entry.target;
|
|
362
|
+
var bounds = displayTreePixelBounds(target);
|
|
363
|
+
var hitState = target instanceof SimpleButton && target.hitTestState;
|
|
364
|
+
if (hitState) {
|
|
365
|
+
var stateBounds = displayBoundsInTarget(hitState, hitState, true);
|
|
366
|
+
if (stateBounds.width || stateBounds.height) {
|
|
367
|
+
var matrix = displayObjectMatrix(hitState);
|
|
368
|
+
bounds = mapBounds(stateBounds, function (point) {
|
|
369
|
+
return target.localToGlobal(matrix.transformPoint(point));
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
// Flash ranks the top-left corner of the focus bounds in twips.
|
|
374
|
+
entry.position = bounds ? Math.round(bounds.x * 20) + 6 * Math.round(bounds.y * 20) : Infinity;
|
|
375
|
+
}
|
|
376
|
+
order.sort(function (a, b) { return a.position - b.position || a.sequence - b.sequence; });
|
|
377
|
+
order = order.filter(function (entry) {
|
|
378
|
+
if (!Number.isFinite(entry.position)) return true;
|
|
379
|
+
if (used.has(entry.position)) return false;
|
|
380
|
+
used.add(entry.position);
|
|
381
|
+
return true;
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
return order.map(function (entry) { return entry.target; });
|
|
385
|
+
}
|
|
386
|
+
Object.defineProperty(StageClass.prototype, 'width', {
|
|
387
|
+
get: function () { return this.stageWidth; },
|
|
388
|
+
set: function (v) { this.stageWidth = Math.max(0, +v || 0); },
|
|
389
|
+
});
|
|
390
|
+
Object.defineProperty(StageClass.prototype, 'height', {
|
|
391
|
+
get: function () { return this.stageHeight; },
|
|
392
|
+
set: function (v) { this.stageHeight = Math.max(0, +v || 0); },
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
export var stageRenderPending = false, dispatchingStageRender = false;
|
|
396
|
+
export function dispatchPendingStageRender() {
|
|
397
|
+
if (!stageRenderPending || dispatchingStageRender) return;
|
|
398
|
+
stageRenderPending = false;
|
|
399
|
+
dispatchingStageRender = true;
|
|
400
|
+
try { dispatchStageBroadcast(Event.RENDER); }
|
|
401
|
+
finally { dispatchingStageRender = false; }
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export var stageSingleton = null;
|
|
405
|
+
export function installStage() {
|
|
406
|
+
if (stageSingleton) return stageSingleton;
|
|
407
|
+
stageSingleton = new StageClass();
|
|
408
|
+
stageRef.current = stageSingleton;
|
|
409
|
+
stageSingleton._mouseX = 0;
|
|
410
|
+
stageSingleton._mouseY = 0;
|
|
411
|
+
Object.defineProperty(stageSingleton, 'mouseX', {
|
|
412
|
+
get: function () { return this._mouseX; },
|
|
413
|
+
configurable: true,
|
|
414
|
+
});
|
|
415
|
+
Object.defineProperty(stageSingleton, 'mouseY', {
|
|
416
|
+
get: function () { return this._mouseY; },
|
|
417
|
+
configurable: true,
|
|
418
|
+
});
|
|
419
|
+
try { global.stage = stageSingleton; } catch (_) {}
|
|
420
|
+
return stageSingleton;
|
|
421
|
+
}
|
|
422
|
+
if (typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
|
|
423
|
+
var stagePage = typeof document !== 'undefined' ? document : null;
|
|
424
|
+
var stageWindowFocused = stagePage?.hasFocus?.() ?? true;
|
|
425
|
+
var stageWindowActive = stageWindowFocused && !stagePage?.hidden;
|
|
426
|
+
function updateStageActivation() {
|
|
427
|
+
installStage();
|
|
428
|
+
var active = stageWindowFocused && !stagePage?.hidden;
|
|
429
|
+
if (active === stageWindowActive) return;
|
|
430
|
+
stageWindowActive = active;
|
|
431
|
+
dispatchStageBroadcast(active ? Event.ACTIVATE : Event.DEACTIVATE);
|
|
432
|
+
}
|
|
433
|
+
window.addEventListener('focus', function () {
|
|
434
|
+
stageWindowFocused = true;
|
|
435
|
+
updateStageActivation();
|
|
436
|
+
});
|
|
437
|
+
window.addEventListener('blur', function () {
|
|
438
|
+
stageWindowFocused = false;
|
|
439
|
+
updateStageActivation();
|
|
440
|
+
});
|
|
441
|
+
stagePage?.addEventListener?.('visibilitychange', updateStageActivation);
|
|
442
|
+
}
|
|
443
|
+
export function keyboardFocusTarget() {
|
|
444
|
+
var active = typeof document !== 'undefined' ? document.activeElement : null;
|
|
445
|
+
var overlayTarget = active && active._swoofTextField;
|
|
446
|
+
if (overlayTarget && typeof overlayTarget.dispatchEvent === 'function') {
|
|
447
|
+
return overlayTarget.mouseEnabled === false ? stageSingleton : overlayTarget;
|
|
448
|
+
}
|
|
449
|
+
var focused = stageSingleton.focus;
|
|
450
|
+
if (focused && isStageDescendant(stageSingleton, focused)) {
|
|
451
|
+
// Flash: mouseEnabled=false blocks mouse and keyboard delivery on that object.
|
|
452
|
+
return focused.mouseEnabled === false ? stageSingleton : focused;
|
|
453
|
+
}
|
|
454
|
+
if (focused) stageSingleton.focus = null;
|
|
455
|
+
return stageSingleton;
|
|
456
|
+
}
|
|
457
|
+
export function advanceStageFocus(reverse, nativeEvent) {
|
|
458
|
+
var order = stageTabOrder(stageSingleton);
|
|
459
|
+
if (!order.length) return;
|
|
460
|
+
var previous = stageSingleton.focus;
|
|
461
|
+
var index = order.indexOf(previous);
|
|
462
|
+
var nextIndex = index < 0 ? (reverse ? order.length - 1 : 0)
|
|
463
|
+
: (index + (reverse ? -1 : 1) + order.length) % order.length;
|
|
464
|
+
var next = order[nextIndex];
|
|
465
|
+
var event = new FocusEvent(FocusEvent.KEY_FOCUS_CHANGE, true, true, next, reverse, 9);
|
|
466
|
+
try { (previous || stageSingleton).dispatchEvent(event); } catch (_) {}
|
|
467
|
+
if (!event.defaultPrevented) stageSingleton.focus = next;
|
|
468
|
+
if (nativeEvent && nativeEvent.preventDefault) nativeEvent.preventDefault();
|
|
469
|
+
}
|
|
470
|
+
export function allowStageMouseFocusChange(target) {
|
|
471
|
+
var previous = stageSingleton.focus;
|
|
472
|
+
if (previous === target) return true;
|
|
473
|
+
var focusEvent = new FocusEvent(FocusEvent.MOUSE_FOCUS_CHANGE, true, true, target);
|
|
474
|
+
try { (previous || stageSingleton).dispatchEvent(focusEvent); } catch (_) {}
|
|
475
|
+
return !focusEvent.defaultPrevented;
|
|
476
|
+
}
|
|
477
|
+
export function focusCanvasPointerTarget(target) {
|
|
478
|
+
var previous = stageSingleton.focus;
|
|
479
|
+
target = target && target !== stageSingleton && isStageDescendant(stageSingleton, target) ? target : null;
|
|
480
|
+
if (previous === target || (!target && !previous?.tabEnabled)) return;
|
|
481
|
+
if (allowStageMouseFocusChange(target)) {
|
|
482
|
+
stageSingleton.focus = target?.tabEnabled && isStageDescendant(stageSingleton, target) ? target : null;
|
|
483
|
+
}
|
|
484
|
+
}
|