embedded-react 0.19.0 → 0.20.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/README.md +7 -3
- package/aot/compile.mjs +178 -58
- package/aot/compiler/animations.mjs +290 -281
- package/aot/compiler/boot.mjs +70 -33
- package/aot/compiler/branch-merge.mjs +4 -3
- package/aot/compiler/check.mjs +9 -9
- package/aot/compiler/collect.mjs +324 -205
- package/aot/compiler/control-flow.mjs +209 -197
- package/aot/compiler/diagnostics.mjs +22 -25
- package/aot/compiler/elements/activity-indicator.mjs +25 -24
- package/aot/compiler/elements/dial.mjs +91 -77
- package/aot/compiler/elements/flat-list.mjs +24 -22
- package/aot/compiler/elements/image.mjs +51 -50
- package/aot/compiler/elements/modal.mjs +31 -30
- package/aot/compiler/elements/props.mjs +23 -23
- package/aot/compiler/elements/switch.mjs +42 -34
- package/aot/compiler/elements/text-input.mjs +34 -32
- package/aot/compiler/expressions.mjs +372 -333
- package/aot/compiler/handlers.mjs +473 -387
- package/aot/compiler/ir/functions.mjs +11 -1
- package/aot/compiler/ir/nodes.mjs +4 -4
- package/aot/compiler/ir/reads.mjs +19 -19
- package/aot/compiler/ir/types.mjs +14 -14
- package/aot/compiler/ir/vectors.mjs +5 -1
- package/aot/compiler/keyboard.mjs +17 -7
- package/aot/compiler/lower.mjs +77 -80
- package/aot/compiler/nodes.mjs +169 -152
- package/aot/compiler/options.mjs +35 -30
- package/aot/compiler/out.mjs +18 -22
- package/aot/compiler/package/animations.mjs +669 -0
- package/aot/compiler/package/arena.mjs +252 -0
- package/aot/compiler/package/assemble.mjs +177 -0
- package/aot/compiler/package/bytecode.mjs +286 -0
- package/aot/compiler/package/checks.mjs +1022 -0
- package/aot/compiler/package/codegen.mjs +1329 -0
- package/aot/compiler/package/enums.mjs +138 -0
- package/aot/compiler/package/format.mjs +760 -0
- package/aot/compiler/package/layouts.mjs +136 -0
- package/aot/compiler/package/lower.mjs +1606 -0
- package/aot/compiler/package/props.mjs +118 -0
- package/aot/compiler/package/tables.mjs +103 -0
- package/aot/compiler/package/vectors.mjs +502 -0
- package/aot/compiler/package/writer.mjs +664 -0
- package/aot/compiler/pan-responder.mjs +69 -61
- package/aot/compiler/parse.mjs +21 -21
- package/aot/compiler/static-eval.mjs +25 -23
- package/aot/compiler/style-text.mjs +161 -172
- package/aot/compiler/svg.mjs +477 -251
- package/aot/screenshot-smoke.mjs +54 -40
- package/aot/style-map.mjs +102 -90
- package/assets/bake-font.mjs +123 -77
- package/assets/bake-image.mjs +39 -22
- package/assets/bake-svg.mjs +361 -262
- package/assets/build-builtin-font.mjs +11 -7
- package/assets/emit-c.mjs +77 -58
- package/assets/emit-container.mjs +123 -37
- package/assets/emit-pack.mjs +77 -67
- package/assets/font-config.mjs +9 -6
- package/assets/font-sizes.mjs +163 -123
- package/assets/glyph-coverage.mjs +74 -54
- package/assets/index.mjs +31 -9
- package/assets/rasterize.mjs +126 -99
- package/assets/svg-loader.mjs +8 -8
- package/build.mjs +14 -12
- package/cli.mjs +359 -172
- package/compile-bin.mjs +3 -3
- package/flash.mjs +537 -0
- package/hotreload/dev-device.mjs +90 -33
- package/hotreload/pack-app.mjs +55 -43
- package/hotreload/pack-split.mjs +46 -37
- package/hotreload/package-transfer.mjs +175 -0
- package/hotreload/protocol.mjs +50 -37
- package/hotreload/uploader.mjs +38 -27
- package/hotreload/watch-tree.mjs +19 -19
- package/pack-container.mjs +28 -20
- package/package.json +7 -8
- package/persist-transform.mjs +34 -28
- package/qjsc-wasm.mjs +25 -14
- package/sim/embedded-react.cjs +1 -1
- package/sim/embedded-react.js +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +94 -85
- package/sim-server.mjs +101 -82
- package/src/embedded-react/Animated.js +24 -24
- package/src/embedded-react/Easing.js +1 -1
- package/src/embedded-react/FlatList.js +3 -3
- package/src/embedded-react/LayoutAnimation.js +8 -5
- package/src/embedded-react/PanResponder.js +75 -71
- package/src/embedded-react/Pressable.js +2 -8
- package/src/embedded-react/SectionList.js +6 -6
- package/src/embedded-react/StyleSheet.js +4 -3
- package/src/embedded-react/TouchableOpacity.js +13 -8
- package/src/embedded-react/index.d.ts +5 -5
- package/src/embedded-react/layout-anim-config.js +10 -4
- package/src/embedded-react/list-child.js +5 -5
- package/src/embedded-react/named-colors.js +1 -1
- package/src/embedded-react/split-style.js +11 -11
- package/src/embedded-react/svg-ops.js +760 -593
- package/src/embedded-react/useHostValue.js +3 -3
- package/src/embedded-react/usePersistentState.js +6 -6
- package/src/embedded-react/warn-props.js +1 -1
- package/src/host-config.js +60 -53
- package/src/props.js +49 -41
- package/src/renderer.js +7 -4
- package/aot/compiler/assemble.mjs +0 -944
- package/aot/compiler/c/animations.mjs +0 -436
- package/aot/compiler/c/boot.mjs +0 -127
- package/aot/compiler/c/expressions.mjs +0 -270
- package/aot/compiler/c/functions.mjs +0 -268
- package/aot/compiler/c/keyboard.mjs +0 -96
- package/aot/compiler/c/nodes.mjs +0 -303
- package/aot/compiler/c/pan-responder.mjs +0 -136
- package/aot/compiler/c/statements.mjs +0 -319
- package/aot/compiler/c/text.mjs +0 -83
- package/aot/compiler/c/vectors.mjs +0 -381
- package/aot/compiler/c-syntax.mjs +0 -127
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 Cory Lamming
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/*
|
|
17
|
+
* Animations in a package.
|
|
18
|
+
*
|
|
19
|
+
* An animated value is a slot. Each animation the IR starts is a leaf descriptor, started by ASTART with its
|
|
20
|
+
* toValue in a register; its delay is the engine's own delay_ms. Each on_complete callback is an
|
|
21
|
+
* animation-done function: a chain's steps start one another from them, and a completion wait is a delay leaf
|
|
22
|
+
* started from one. A completion is a helper that takes `finished`, entered through a done function of its own
|
|
23
|
+
* when an animation ends with it, or called directly. A run joined at run time (a spring beside a
|
|
24
|
+
* longer delay) is a parallel group of a delay leaf and the animation. A composition with waits keeps the handle
|
|
25
|
+
* of its last wait in a slot, so a stop can end it.
|
|
26
|
+
*/
|
|
27
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
28
|
+
- Imports
|
|
29
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
30
|
+
import { ANIMATION_PROP_FIELDS, AnimationFlag, AnimationKind, BindingKind, ENGINE_VALUES, EventField, FunctionKind, InterpolationFlag, NONE, ValueType, } from './format.mjs';
|
|
31
|
+
import { FunctionBuilder } from './bytecode.mjs';
|
|
32
|
+
import { abiProp, floatBits } from './props.mjs';
|
|
33
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
34
|
+
- Constants
|
|
35
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
36
|
+
/** Waits that run at once. */
|
|
37
|
+
const MAX_WAITS = 8;
|
|
38
|
+
/** Running-animation entries a wait takes: its delay leaf, and a joined run's group. */
|
|
39
|
+
const WAIT_ENTRIES = 2;
|
|
40
|
+
/** A register operand that names no register (ASTART with no toValue in one). */
|
|
41
|
+
const NO_REGISTER = 0xff;
|
|
42
|
+
/** A <TouchableOpacity> fade's easing. */
|
|
43
|
+
const PRESS_FADE_EASING = 'ER_EASE_QUAD_IN_OUT';
|
|
44
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
45
|
+
- Classes
|
|
46
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
47
|
+
/**
|
|
48
|
+
* The program's animations: the slots they keep, their descriptors and interpolations, and the done functions
|
|
49
|
+
* and completion helpers code names. Functions are numbered in place until the program's are, then given their
|
|
50
|
+
* final indices.
|
|
51
|
+
*
|
|
52
|
+
* @class AnimationPlan
|
|
53
|
+
*/
|
|
54
|
+
export class AnimationPlan {
|
|
55
|
+
/** Each animated value's slot, by its handle. */
|
|
56
|
+
values = new Map();
|
|
57
|
+
/** The slot each started composition with waits keeps its last wait's handle in, by owner. */
|
|
58
|
+
owners = new Map();
|
|
59
|
+
loops = new Map();
|
|
60
|
+
animations = [];
|
|
61
|
+
interpolations = [];
|
|
62
|
+
/** The done functions, in function order, by name. */
|
|
63
|
+
doneFunctions = new Map();
|
|
64
|
+
/** The completions, in function order (callers first), by name. */
|
|
65
|
+
completions = new Map();
|
|
66
|
+
descriptors = new Map();
|
|
67
|
+
waits = new Map();
|
|
68
|
+
interpolationIndex = new Map();
|
|
69
|
+
/** The running-animation table's entries. */
|
|
70
|
+
entries = 0;
|
|
71
|
+
doneBase = 0;
|
|
72
|
+
helperBase = 0;
|
|
73
|
+
/**
|
|
74
|
+
* Plans the slots the app's animations keep, and the running-animation table.
|
|
75
|
+
*
|
|
76
|
+
* @param app The app.
|
|
77
|
+
* @param arena The arena being planned.
|
|
78
|
+
*/
|
|
79
|
+
constructor(app, arena) {
|
|
80
|
+
const { output } = app;
|
|
81
|
+
for (const value of app.animations) {
|
|
82
|
+
this.values.set(value.slotName, arena.slot(ValueType.animatedValue, floatBits(value.initial)));
|
|
83
|
+
}
|
|
84
|
+
for (const [, record] of output.animationWaits) {
|
|
85
|
+
if (record.isStarted) {
|
|
86
|
+
this.owners.set(record.owner, arena.slot(ValueType.animation));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// Each callback, in the order the app declares them; a completion's own, first, is called from the rest.
|
|
90
|
+
const completions = [];
|
|
91
|
+
for (const callback of output.animationCallbacks) {
|
|
92
|
+
if (callback.kind === 'completion') {
|
|
93
|
+
completions.push(callback.completion);
|
|
94
|
+
this.doneFunctions.set(callback.completion.name, {
|
|
95
|
+
kind: 'completion',
|
|
96
|
+
completion: callback.completion,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
else if (callback.kind === 'completionWait') {
|
|
100
|
+
this.doneFunctions.set(callback.wait.callback, {
|
|
101
|
+
kind: 'wait',
|
|
102
|
+
completion: callback.completion,
|
|
103
|
+
delayMs: callback.wait.delayMs,
|
|
104
|
+
owner: callback.wait.owner,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
const { chain } = callback;
|
|
109
|
+
if (chain.completion && chain.completionWait) {
|
|
110
|
+
this.doneFunctions.set(chain.completionWait.callback, {
|
|
111
|
+
kind: 'wait',
|
|
112
|
+
completion: chain.completion,
|
|
113
|
+
delayMs: chain.completionWait.delayMs,
|
|
114
|
+
owner: chain.completionWait.owner,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
for (let index = chain.steps.length - 1; index >= 1; index--) {
|
|
118
|
+
this.doneFunctions.set(chainCallback(chain, index), {
|
|
119
|
+
kind: 'step',
|
|
120
|
+
chain,
|
|
121
|
+
index,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
if (chain.loop) {
|
|
125
|
+
this.doneFunctions.set(chainCallback(chain, 0), {
|
|
126
|
+
kind: 'repeat',
|
|
127
|
+
chain,
|
|
128
|
+
});
|
|
129
|
+
this.loops.set(chain.id, {
|
|
130
|
+
passes: chain.loop.iterations >= 0 ? arena.slot(ValueType.int) : null,
|
|
131
|
+
from: chain.loop.resetHandle ? arena.slot(ValueType.float) : null,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// A completion that a completion calls was lowered inside it, so before it: callers go first, as CALL needs.
|
|
137
|
+
for (const completion of completions.reverse()) {
|
|
138
|
+
this.completions.set(completion.name, completion);
|
|
139
|
+
}
|
|
140
|
+
// Each value runs one animation at a time, and one more while a start cancels it; each wait takes its own,
|
|
141
|
+
// even in an app that only waits.
|
|
142
|
+
if (this.values.size || this.owners.size) {
|
|
143
|
+
const waits = this.owners.size ? MAX_WAITS * WAIT_ENTRIES : 0;
|
|
144
|
+
this.entries = this.values.size + 1 + waits;
|
|
145
|
+
arena.animTable(this.entries);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Numbers the done functions and the completion helpers.
|
|
150
|
+
*
|
|
151
|
+
* @param doneBase The first done function's index.
|
|
152
|
+
* @param helperBase The first helper's index.
|
|
153
|
+
*/
|
|
154
|
+
number(doneBase, helperBase) {
|
|
155
|
+
this.doneBase = doneBase;
|
|
156
|
+
this.helperBase = helperBase;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* The done function a callback is.
|
|
160
|
+
*
|
|
161
|
+
* @param name The callback's name.
|
|
162
|
+
*
|
|
163
|
+
* @returns Its index.
|
|
164
|
+
*/
|
|
165
|
+
done(name) {
|
|
166
|
+
const index = [...this.doneFunctions.keys()].indexOf(name);
|
|
167
|
+
if (index < 0) {
|
|
168
|
+
throw new Error(`AOT internal: no animation callback "${name}"`);
|
|
169
|
+
}
|
|
170
|
+
return this.doneBase + index;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The helper a completion is.
|
|
174
|
+
*
|
|
175
|
+
* @param completion The completion.
|
|
176
|
+
*
|
|
177
|
+
* @returns Its index.
|
|
178
|
+
*/
|
|
179
|
+
helper(completion) {
|
|
180
|
+
return (this.helperBase + [...this.completions.keys()].indexOf(completion.name));
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* An animated value's slot.
|
|
184
|
+
*
|
|
185
|
+
* @param handle Its handle.
|
|
186
|
+
*
|
|
187
|
+
* @returns The slot.
|
|
188
|
+
*/
|
|
189
|
+
value(handle) {
|
|
190
|
+
const slot = this.values.get(handle);
|
|
191
|
+
if (!slot) {
|
|
192
|
+
throw new Error(`AOT internal: no animated value "${handle}"`);
|
|
193
|
+
}
|
|
194
|
+
return slot;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* An animation's leaf descriptor, made the first time.
|
|
198
|
+
*
|
|
199
|
+
* @param animation The animation.
|
|
200
|
+
*
|
|
201
|
+
* @returns Its index.
|
|
202
|
+
*/
|
|
203
|
+
leaf(animation) {
|
|
204
|
+
let index = this.descriptors.get(animation);
|
|
205
|
+
if (index === undefined) {
|
|
206
|
+
index = this.animations.push(this.leafRecord(animation)) - 1;
|
|
207
|
+
this.descriptors.set(animation, index);
|
|
208
|
+
}
|
|
209
|
+
return index;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* A press fade's descriptor: a timing of the node's opacity value to a constant.
|
|
213
|
+
*
|
|
214
|
+
* @param fade The fade.
|
|
215
|
+
*
|
|
216
|
+
* @returns Its index.
|
|
217
|
+
*/
|
|
218
|
+
fade(fade) {
|
|
219
|
+
let index = this.descriptors.get(fade);
|
|
220
|
+
if (index === undefined) {
|
|
221
|
+
index =
|
|
222
|
+
this.animations.push({
|
|
223
|
+
...leafDefaults(),
|
|
224
|
+
kind: AnimationKind.timing,
|
|
225
|
+
easing: ENGINE_VALUES.get(PRESS_FADE_EASING),
|
|
226
|
+
target: this.value(fade.value).index,
|
|
227
|
+
amount: fade.durationMs,
|
|
228
|
+
to: floatBits(fade.opacity),
|
|
229
|
+
}) - 1;
|
|
230
|
+
this.descriptors.set(fade, index);
|
|
231
|
+
}
|
|
232
|
+
return index;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* A completion wait's delay leaf.
|
|
236
|
+
*
|
|
237
|
+
* @param delayMs The wait.
|
|
238
|
+
*
|
|
239
|
+
* @returns Its index.
|
|
240
|
+
*/
|
|
241
|
+
wait(delayMs) {
|
|
242
|
+
let index = this.waits.get(delayMs);
|
|
243
|
+
if (index === undefined) {
|
|
244
|
+
index = this.animations.push({ ...leafDefaults(), amount: delayMs }) - 1;
|
|
245
|
+
this.waits.set(delayMs, index);
|
|
246
|
+
}
|
|
247
|
+
return index;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* A joined run: a parallel group of its delay and its one animation, done when both are and not finished as
|
|
251
|
+
* soon as the animation stops.
|
|
252
|
+
*
|
|
253
|
+
* @param start The start.
|
|
254
|
+
*
|
|
255
|
+
* @returns The group's index.
|
|
256
|
+
*/
|
|
257
|
+
join(start) {
|
|
258
|
+
let index = this.descriptors.get(start);
|
|
259
|
+
if (index === undefined) {
|
|
260
|
+
index = this.animations.length;
|
|
261
|
+
this.animations.push({
|
|
262
|
+
...leafDefaults(),
|
|
263
|
+
kind: AnimationKind.parallel,
|
|
264
|
+
first: index + 1,
|
|
265
|
+
count: 2,
|
|
266
|
+
}, { ...leafDefaults(), amount: start.completionJoin.delayMs }, this.leafRecord(start.animations[0]));
|
|
267
|
+
this.descriptors.set(start, index);
|
|
268
|
+
}
|
|
269
|
+
return index;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* An animated value's bind as a binding: straight onto the prop, or through an interpolation.
|
|
273
|
+
*
|
|
274
|
+
* @param bind The bind.
|
|
275
|
+
*
|
|
276
|
+
* @returns The binding.
|
|
277
|
+
*/
|
|
278
|
+
binding(bind) {
|
|
279
|
+
const field = ANIMATION_PROP_FIELDS.get(bind.prop);
|
|
280
|
+
if (!field) {
|
|
281
|
+
throw new Error(`AOT internal: no ABI prop for ${bind.prop}`);
|
|
282
|
+
}
|
|
283
|
+
const prop = abiProp(field).id;
|
|
284
|
+
const slot = this.value(bind.slotName);
|
|
285
|
+
const { interpolation } = bind;
|
|
286
|
+
if (!interpolation) {
|
|
287
|
+
return { kind: BindingKind.animated, event: prop, target: slot.index };
|
|
288
|
+
}
|
|
289
|
+
// Interpolations that are the same are stored once, so list rows that bind one can share a template.
|
|
290
|
+
const record = {
|
|
291
|
+
slot: slot.index,
|
|
292
|
+
flags: interpolation.colors ? InterpolationFlag.color : 0,
|
|
293
|
+
extrapolateLeft: ENGINE_VALUES.get(interpolation.extrapolateLeft),
|
|
294
|
+
extrapolateRight: ENGINE_VALUES.get(interpolation.extrapolateRight),
|
|
295
|
+
input: interpolation.input.map(floatBits),
|
|
296
|
+
output: interpolation.colors
|
|
297
|
+
? interpolation.colors.map(color => color >>> 0)
|
|
298
|
+
: interpolation.output.map(floatBits),
|
|
299
|
+
};
|
|
300
|
+
const key = JSON.stringify(record);
|
|
301
|
+
let index = this.interpolationIndex.get(key);
|
|
302
|
+
if (index === undefined) {
|
|
303
|
+
index = this.interpolations.push(record) - 1;
|
|
304
|
+
this.interpolationIndex.set(key, index);
|
|
305
|
+
}
|
|
306
|
+
return { kind: BindingKind.interpolated, event: prop, target: index };
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* The slot a composition keeps its last wait's handle in.
|
|
310
|
+
*
|
|
311
|
+
* @param owner The composition.
|
|
312
|
+
*
|
|
313
|
+
* @returns The slot.
|
|
314
|
+
*/
|
|
315
|
+
owner(owner) {
|
|
316
|
+
const slot = this.owners.get(owner);
|
|
317
|
+
if (!slot) {
|
|
318
|
+
throw new Error(`AOT internal: no slot for composition ${owner}`);
|
|
319
|
+
}
|
|
320
|
+
return slot;
|
|
321
|
+
}
|
|
322
|
+
leafRecord(animation) {
|
|
323
|
+
const { curve } = animation;
|
|
324
|
+
const record = {
|
|
325
|
+
...leafDefaults(),
|
|
326
|
+
target: this.value(animation.handle).index,
|
|
327
|
+
delayMs: animation.delayMs,
|
|
328
|
+
flags: (animation.isLooping ? AnimationFlag.loop : 0) |
|
|
329
|
+
(animation.to?.kind === 'number' ? 0 : AnimationFlag.toRegister),
|
|
330
|
+
to: animation.to?.kind === 'number' ? floatBits(animation.to.value) : 0,
|
|
331
|
+
};
|
|
332
|
+
switch (curve.kind) {
|
|
333
|
+
case 'spring':
|
|
334
|
+
return {
|
|
335
|
+
...record,
|
|
336
|
+
kind: AnimationKind.spring,
|
|
337
|
+
parameters: [
|
|
338
|
+
floatBits(curve.stiffness),
|
|
339
|
+
floatBits(curve.damping),
|
|
340
|
+
floatBits(curve.mass),
|
|
341
|
+
0,
|
|
342
|
+
],
|
|
343
|
+
};
|
|
344
|
+
case 'decay':
|
|
345
|
+
return {
|
|
346
|
+
...record,
|
|
347
|
+
kind: AnimationKind.decay,
|
|
348
|
+
parameters: [
|
|
349
|
+
floatBits(curve.velocity),
|
|
350
|
+
floatBits(curve.deceleration),
|
|
351
|
+
0,
|
|
352
|
+
0,
|
|
353
|
+
],
|
|
354
|
+
};
|
|
355
|
+
case 'timing':
|
|
356
|
+
return {
|
|
357
|
+
...record,
|
|
358
|
+
kind: AnimationKind.timing,
|
|
359
|
+
easing: ENGINE_VALUES.get(curve.easing),
|
|
360
|
+
amount: curve.durationMs,
|
|
361
|
+
parameters: curve.bezier
|
|
362
|
+
? curve.bezier.map(floatBits)
|
|
363
|
+
: [0, 0, 0, 0],
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
369
|
+
- Implementation
|
|
370
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
371
|
+
/**
|
|
372
|
+
* A descriptor with nothing set: a delay leaf of 0 ms, which a record overrides.
|
|
373
|
+
*
|
|
374
|
+
* @returns The record.
|
|
375
|
+
*/
|
|
376
|
+
const leafDefaults = () => ({
|
|
377
|
+
kind: AnimationKind.timing,
|
|
378
|
+
easing: 0,
|
|
379
|
+
flags: 0,
|
|
380
|
+
target: NONE,
|
|
381
|
+
prop: NONE,
|
|
382
|
+
first: 0,
|
|
383
|
+
count: 0,
|
|
384
|
+
amount: 0,
|
|
385
|
+
delayMs: 0,
|
|
386
|
+
to: 0,
|
|
387
|
+
parameters: [0, 0, 0, 0],
|
|
388
|
+
});
|
|
389
|
+
/**
|
|
390
|
+
* The name of a chain's callback that starts step `index` (step 0's starts a repeat).
|
|
391
|
+
*
|
|
392
|
+
* @param chain The chain.
|
|
393
|
+
* @param index The step.
|
|
394
|
+
*
|
|
395
|
+
* @returns The name.
|
|
396
|
+
*/
|
|
397
|
+
const chainCallback = (chain, index) => `chain_${chain.id}_${index}`;
|
|
398
|
+
/**
|
|
399
|
+
* The done function the step after `index` is started from, or what runs after the last step: the repeat for a
|
|
400
|
+
* loop, else the completion's wait or the completion.
|
|
401
|
+
*
|
|
402
|
+
* @param chain The chain.
|
|
403
|
+
* @param index The step.
|
|
404
|
+
* @param plan The program's animations.
|
|
405
|
+
*
|
|
406
|
+
* @returns The function, or NONE.
|
|
407
|
+
*/
|
|
408
|
+
function afterStep(chain, index, plan) {
|
|
409
|
+
if (index < chain.steps.length - 1) {
|
|
410
|
+
return plan.done(chainCallback(chain, index + 1));
|
|
411
|
+
}
|
|
412
|
+
if (chain.loop) {
|
|
413
|
+
return plan.done(chainCallback(chain, 0));
|
|
414
|
+
}
|
|
415
|
+
const name = chain.completionWait?.callback ?? chain.completion?.name;
|
|
416
|
+
return name ? plan.done(name) : NONE;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Starts a descriptor, as er_animation_value_animate starts an animation.
|
|
420
|
+
*
|
|
421
|
+
* @param codegen The function's codegen.
|
|
422
|
+
* @param descriptor The descriptor.
|
|
423
|
+
* @param done The done function, or NONE.
|
|
424
|
+
* @param to The register its toValue is in, or null when the descriptor holds it.
|
|
425
|
+
*
|
|
426
|
+
* @returns The running animation's handle.
|
|
427
|
+
*/
|
|
428
|
+
function start(codegen, descriptor, done, to) {
|
|
429
|
+
const handle = codegen.builder.register('animation');
|
|
430
|
+
codegen.builder.abx('ASTART', handle.index, descriptor, (done & 0xffff) | ((to?.index ?? NO_REGISTER) << 16));
|
|
431
|
+
return handle;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* An animation's toValue in a float register, or null when its descriptor holds it. A decay with
|
|
435
|
+
* none is given the value it holds now.
|
|
436
|
+
*
|
|
437
|
+
* @param codegen The function's codegen.
|
|
438
|
+
* @param animation The animation.
|
|
439
|
+
* @param plan The program's animations.
|
|
440
|
+
*
|
|
441
|
+
* @returns The register.
|
|
442
|
+
*/
|
|
443
|
+
function toValue(codegen, animation, plan) {
|
|
444
|
+
if (animation.to?.kind === 'number') {
|
|
445
|
+
return null;
|
|
446
|
+
}
|
|
447
|
+
if (animation.to) {
|
|
448
|
+
return codegen.valueAs(animation.to, 'float');
|
|
449
|
+
}
|
|
450
|
+
const register = codegen.builder.register('float');
|
|
451
|
+
codegen.builder.abx('AVGET', register.index, plan.value(animation.handle).index);
|
|
452
|
+
return register;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Starts one animation.
|
|
456
|
+
*
|
|
457
|
+
* @param codegen The function's codegen.
|
|
458
|
+
* @param animation The animation.
|
|
459
|
+
* @param done The done function, or NONE.
|
|
460
|
+
* @param plan The program's animations.
|
|
461
|
+
*/
|
|
462
|
+
function startAnimation(codegen, animation, done, plan) {
|
|
463
|
+
codegen.scoped(() => start(codegen, plan.leaf(animation), done, toValue(codegen, animation, plan)));
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Starts a completion's wait, keeping its handle where a stop of its composition finds it.
|
|
467
|
+
*
|
|
468
|
+
* @param codegen The function's codegen.
|
|
469
|
+
* @param completion The completion it runs.
|
|
470
|
+
* @param delayMs The wait.
|
|
471
|
+
* @param owner The composition.
|
|
472
|
+
* @param plan The program's animations.
|
|
473
|
+
*/
|
|
474
|
+
function startWait(codegen, completion, delayMs, owner, plan) {
|
|
475
|
+
codegen.scoped(() => {
|
|
476
|
+
const handle = start(codegen, plan.wait(delayMs), plan.done(completion.name), null);
|
|
477
|
+
codegen.builder.abx('SET32', handle.index, plan.owner(owner).index);
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Calls a completion directly.
|
|
482
|
+
*
|
|
483
|
+
* @param codegen The function's codegen.
|
|
484
|
+
* @param completion The completion.
|
|
485
|
+
* @param finished A bool register holding what it is passed.
|
|
486
|
+
* @param plan The program's animations.
|
|
487
|
+
*/
|
|
488
|
+
function callCompletion(codegen, completion, finished, plan) {
|
|
489
|
+
codegen.builder.abx('CALL', finished.index, plan.helper(completion));
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Calls a completion with a constant.
|
|
493
|
+
*
|
|
494
|
+
* @param codegen The function's codegen.
|
|
495
|
+
* @param completion The completion.
|
|
496
|
+
* @param finished What it is passed.
|
|
497
|
+
* @param plan The program's animations.
|
|
498
|
+
*/
|
|
499
|
+
function callCompletionWith(codegen, completion, finished, plan) {
|
|
500
|
+
codegen.scoped(() => callCompletion(codegen, completion, codegen.load('bool', finished ? 1 : 0), plan));
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Lowers an animation statement that starts or stops one.
|
|
504
|
+
*
|
|
505
|
+
* @param codegen The function's codegen.
|
|
506
|
+
* @param statement The statement.
|
|
507
|
+
* @param plan The program's animations.
|
|
508
|
+
*/
|
|
509
|
+
export function animationStatement(codegen, statement, plan) {
|
|
510
|
+
const { builder } = codegen;
|
|
511
|
+
switch (statement.kind) {
|
|
512
|
+
case 'startAnimations': {
|
|
513
|
+
const { animations, completion, completionJoin } = statement;
|
|
514
|
+
if (!animations.length) {
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
// A joined run is one tree, kept where a stop of its composition finds it.
|
|
518
|
+
if (completionJoin && completion) {
|
|
519
|
+
codegen.scoped(() => {
|
|
520
|
+
const handle = start(codegen, plan.join(statement), plan.done(completion.name), toValue(codegen, animations[0], plan));
|
|
521
|
+
builder.abx('SET32', handle.index, plan.owner(completionJoin.owner).index);
|
|
522
|
+
});
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
// The completion (or its wait) goes on the last animation, the only one when there is a completion.
|
|
526
|
+
const name = statement.completionWait?.callback ?? completion?.name;
|
|
527
|
+
animations.forEach((animation, index) => startAnimation(codegen, animation, index === animations.length - 1 && name ? plan.done(name) : NONE, plan));
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
case 'completeAfter':
|
|
531
|
+
if (statement.owner !== null) {
|
|
532
|
+
startWait(codegen, statement.completion, statement.delayMs, statement.owner, plan);
|
|
533
|
+
}
|
|
534
|
+
else {
|
|
535
|
+
callCompletionWith(codegen, statement.completion, true, plan);
|
|
536
|
+
}
|
|
537
|
+
return;
|
|
538
|
+
case 'startChain': {
|
|
539
|
+
// A loop first records the pass and where the value starts, then step 0 starts here.
|
|
540
|
+
const loop = plan.loops.get(statement.id);
|
|
541
|
+
if (loop?.passes) {
|
|
542
|
+
codegen.scoped(() => builder.abx('SET32', codegen.load('int', 1).index, loop.passes.index));
|
|
543
|
+
}
|
|
544
|
+
if (loop?.from) {
|
|
545
|
+
codegen.scoped(() => {
|
|
546
|
+
const from = builder.register('float');
|
|
547
|
+
builder.abx('AVGET', from.index, plan.value(statement.loop.resetHandle).index);
|
|
548
|
+
builder.abx('SET32', from.index, loop.from.index);
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
startAnimation(codegen, statement.steps[0], afterStep(statement, 0, plan), plan);
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
case 'stopAnimations':
|
|
555
|
+
// Each value freezes where it is, which ends its animation as not finished; then the composition's wait.
|
|
556
|
+
for (const handle of statement.handles) {
|
|
557
|
+
codegen.scoped(() => {
|
|
558
|
+
const value = builder.register('float');
|
|
559
|
+
const slot = plan.value(handle).index;
|
|
560
|
+
builder.abx('AVGET', value.index, slot);
|
|
561
|
+
builder.abx('AVSET', value.index, slot);
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
if (statement.waitOwner !== null &&
|
|
565
|
+
plan.owners.has(statement.waitOwner)) {
|
|
566
|
+
codegen.scoped(() => {
|
|
567
|
+
const handle = builder.register('animation');
|
|
568
|
+
builder.abx('GET32', handle.index, plan.owner(statement.waitOwner).index);
|
|
569
|
+
builder.abc('ASTOP', handle.index);
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Starts a <TouchableOpacity>'s fade, before the app's own handler for the event runs.
|
|
576
|
+
*
|
|
577
|
+
* @param codegen The handler's codegen.
|
|
578
|
+
* @param fade The fade.
|
|
579
|
+
* @param plan The program's animations.
|
|
580
|
+
*/
|
|
581
|
+
export function startPressFade(codegen, fade, plan) {
|
|
582
|
+
codegen.scoped(() => start(codegen, plan.fade(fade), NONE, null));
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* The done functions, in order: each one ends the chain or composition when the animation before it was
|
|
586
|
+
* interrupted, calling the completion as not finished.
|
|
587
|
+
*
|
|
588
|
+
* @param plan The program's animations.
|
|
589
|
+
* @param codegenFor Makes a function's codegen.
|
|
590
|
+
*
|
|
591
|
+
* @returns The functions.
|
|
592
|
+
*/
|
|
593
|
+
export function doneFunctions(plan, codegenFor) {
|
|
594
|
+
return [...plan.doneFunctions].map(([name, done]) => {
|
|
595
|
+
const builder = new FunctionBuilder(FunctionKind.animationDone, `animation ${name}`);
|
|
596
|
+
const codegen = codegenFor(builder);
|
|
597
|
+
const finished = builder.register('bool');
|
|
598
|
+
builder.abc('EVT', finished.index, EventField.finished);
|
|
599
|
+
if (done.kind === 'completion') {
|
|
600
|
+
callCompletion(codegen, done.completion, finished, plan);
|
|
601
|
+
return builder.finish();
|
|
602
|
+
}
|
|
603
|
+
const completion = done.kind === 'wait' ? done.completion : done.chain.completion;
|
|
604
|
+
const afterCompletion = builder.label();
|
|
605
|
+
const end = builder.label();
|
|
606
|
+
builder.branch('JT', afterCompletion, finished);
|
|
607
|
+
if (completion) {
|
|
608
|
+
callCompletion(codegen, completion, finished, plan);
|
|
609
|
+
}
|
|
610
|
+
builder.branch('JMP', end);
|
|
611
|
+
builder.place(afterCompletion);
|
|
612
|
+
if (done.kind === 'wait') {
|
|
613
|
+
startWait(codegen, done.completion, done.delayMs, done.owner, plan);
|
|
614
|
+
}
|
|
615
|
+
else if (done.kind === 'step') {
|
|
616
|
+
startAnimation(codegen, done.chain.steps[done.index], afterStep(done.chain, done.index, plan), plan);
|
|
617
|
+
}
|
|
618
|
+
else {
|
|
619
|
+
repeat(codegen, done.chain, end, plan);
|
|
620
|
+
}
|
|
621
|
+
builder.place(end);
|
|
622
|
+
return builder.finish();
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* A loop's repeat, when its last step finished: it counts the pass, puts the value back, and starts step 0 again.
|
|
627
|
+
*
|
|
628
|
+
* @param codegen The done function's codegen.
|
|
629
|
+
* @param chain The chain.
|
|
630
|
+
* @param end Where the function returns.
|
|
631
|
+
* @param plan The program's animations.
|
|
632
|
+
*/
|
|
633
|
+
function repeat(codegen, chain, end, plan) {
|
|
634
|
+
const { builder } = codegen;
|
|
635
|
+
const loop = chain.loop;
|
|
636
|
+
const slots = plan.loops.get(chain.id);
|
|
637
|
+
if (slots.passes) {
|
|
638
|
+
const passes = slots.passes;
|
|
639
|
+
codegen.scoped(() => {
|
|
640
|
+
const count = builder.register('int');
|
|
641
|
+
const more = builder.register('bool');
|
|
642
|
+
const next = builder.label();
|
|
643
|
+
builder.abx('GET32', count.index, passes.index);
|
|
644
|
+
builder.abc('LT', more.index, count.index, codegen.load('int', loop.iterations).index);
|
|
645
|
+
builder.branch('JT', next, more);
|
|
646
|
+
if (chain.completion) {
|
|
647
|
+
if (loop.finalWait) {
|
|
648
|
+
startWait(codegen, chain.completion, loop.finalWait.delayMs, loop.finalWait.owner, plan);
|
|
649
|
+
}
|
|
650
|
+
else {
|
|
651
|
+
callCompletionWith(codegen, chain.completion, true, plan);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
builder.branch('JMP', end);
|
|
655
|
+
builder.place(next);
|
|
656
|
+
builder.abc('ADD', count.index, count.index, codegen.load('int', 1).index);
|
|
657
|
+
builder.abx('SET32', count.index, passes.index);
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
if (slots.from) {
|
|
661
|
+
const from = slots.from;
|
|
662
|
+
codegen.scoped(() => {
|
|
663
|
+
const value = builder.register('float');
|
|
664
|
+
builder.abx('GET32', value.index, from.index);
|
|
665
|
+
builder.abx('AVSET', value.index, plan.value(loop.resetHandle).index);
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
startAnimation(codegen, loop.restart, afterStep(chain, 0, plan), plan);
|
|
669
|
+
}
|