@overmvp/kumi 0.0.0-stage → 0.1.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.
Files changed (205) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +14 -2
  3. package/animation/color-transition.ts +196 -0
  4. package/animation/cycle.ts +151 -0
  5. package/animation/ease.ts +29 -0
  6. package/animation/index.ts +13 -0
  7. package/animation/package.json +12 -0
  8. package/animation/presets.ts +376 -0
  9. package/animation/spec.ts +197 -0
  10. package/builder/Cargo.lock +192 -0
  11. package/builder/Cargo.toml +7 -0
  12. package/builder/css-v1.mjs +334 -0
  13. package/builder/css-v1.test.mjs +321 -0
  14. package/builder/generated-output.mjs +61 -0
  15. package/builder/generated-output.test.mjs +132 -0
  16. package/builder/jsx-form.mjs +93 -0
  17. package/builder/jsx-form.test.mjs +48 -0
  18. package/builder/jsx-lists.mjs +209 -0
  19. package/builder/link-addon-package.mjs +86 -0
  20. package/builder/lua-assets.mjs +74 -0
  21. package/builder/lua-builtin-values.mjs +57 -0
  22. package/builder/lua-builtin-values.test.mjs +155 -0
  23. package/builder/lua-loop-closures.mjs +183 -0
  24. package/builder/lua-loop-closures.test.mjs +89 -0
  25. package/builder/lua-truthiness.mjs +61 -0
  26. package/builder/lua-truthiness.test.mjs +113 -0
  27. package/builder/module-declarations.mjs +594 -0
  28. package/builder/module-declarations.test.mjs +444 -0
  29. package/builder/native-kernels.mjs +60 -0
  30. package/builder/native-lua.d.ts +57 -0
  31. package/builder/native-routes.mjs +51 -0
  32. package/builder/no-static-ref-read.mjs +93 -0
  33. package/builder/no-static-ref-read.test.mjs +181 -0
  34. package/builder/secure-snippet.mjs +317 -0
  35. package/builder/secure-snippet.test.mjs +101 -0
  36. package/builder/shared-lua-runtime.mjs +150 -0
  37. package/builder/src/addon-link.lua +29 -0
  38. package/builder/src/arrays.lua +260 -0
  39. package/builder/src/assets.rs +177 -0
  40. package/builder/src/classes.lua +75 -0
  41. package/builder/src/collections.lua +190 -0
  42. package/builder/src/compiler.rs +2164 -0
  43. package/builder/src/config.rs +42 -0
  44. package/builder/src/entries.rs +166 -0
  45. package/builder/src/errors.lua +15 -0
  46. package/builder/src/lua_data.rs +231 -0
  47. package/builder/src/main.rs +367 -0
  48. package/builder/src/native_ir.rs +65 -0
  49. package/builder/src/native_signal.rs +47 -0
  50. package/builder/src/numbers.lua +62 -0
  51. package/builder/src/project-front.mjs +3331 -0
  52. package/builder/src/records.lua +75 -0
  53. package/builder/src/routes.rs +127 -0
  54. package/builder/src/signal-kernel.fingerprint +1 -0
  55. package/builder/src/strings.lua +103 -0
  56. package/builder/src/ts-lower.mjs +68 -0
  57. package/builder/src/wow_client.rs +253 -0
  58. package/builder/src/wow_types.rs +564 -0
  59. package/builder/style-states.mjs +100 -0
  60. package/builder/style-states.test.mjs +103 -0
  61. package/builder/vite-assets.mjs +53 -0
  62. package/builder/vite-project.mjs +10 -0
  63. package/config.ts +17 -0
  64. package/form/index.ts +522 -0
  65. package/form/package.json +13 -0
  66. package/motion/index.ts +2 -0
  67. package/motion/package.json +10 -0
  68. package/package.json +22 -4
  69. package/router/index.ts +43 -0
  70. package/router/package.json +12 -0
  71. package/router/routes.ts +64 -0
  72. package/scripts/kumi-cli.mjs +70 -0
  73. package/scripts/link-addons.mjs +51 -0
  74. package/src/bindings.ts +35 -0
  75. package/src/context.ts +15 -0
  76. package/src/css-tokens.ts +234 -0
  77. package/src/css-v1.ts +58 -0
  78. package/src/cvars.ts +91 -0
  79. package/src/element-node.ts +32 -0
  80. package/src/elements.ts +479 -0
  81. package/src/feature-scope.ts +82 -0
  82. package/src/flex-layout.ts +807 -0
  83. package/src/frame-scheduler.ts +334 -0
  84. package/src/grid-tracks.ts +165 -0
  85. package/src/image-imports.d.ts +24 -0
  86. package/src/images.ts +79 -0
  87. package/src/index.ts +28 -0
  88. package/src/jsx-dev-runtime.ts +2 -0
  89. package/src/jsx-runtime.ts +22 -0
  90. package/src/jsx.d.ts +12 -0
  91. package/src/keyed-list.ts +363 -0
  92. package/src/media-query.ts +144 -0
  93. package/src/module.ts +277 -0
  94. package/src/node-selectors.ts +114 -0
  95. package/src/owned-collections.ts +44 -0
  96. package/src/providers.ts +235 -0
  97. package/src/reactive-scope.ts +34 -0
  98. package/src/reactivity.ts +254 -0
  99. package/src/result.ts +58 -0
  100. package/src/runtime.ts +52 -0
  101. package/src/scheduler.ts +185 -0
  102. package/src/signal-kernel.ts +30 -0
  103. package/src/style-classes.ts +51 -0
  104. package/src/style-states.ts +134 -0
  105. package/src/style-variables.ts +53 -0
  106. package/src/text-children.ts +42 -0
  107. package/src/theme.ts +50 -0
  108. package/src/transition.ts +18 -0
  109. package/state/index.ts +3 -0
  110. package/state/package.json +12 -0
  111. package/state/store-bindings.ts +25 -0
  112. package/state/store.ts +82 -0
  113. package/styles/_layers.scss +14 -0
  114. package/templates/addon/README.md +14 -0
  115. package/templates/addon/__ADDON_NAME__.toc +4 -0
  116. package/templates/addon/eslint.config.mjs +12 -0
  117. package/templates/addon/gitignore +8 -0
  118. package/templates/addon/package.json +21 -0
  119. package/templates/addon/scripts/build.mjs +25 -0
  120. package/templates/addon/src/app/main.tsx +13 -0
  121. package/templates/addon/tsconfig.json +24 -0
  122. package/translate/index.ts +14 -0
  123. package/translate/package.json +9 -0
  124. package/utils/colors.ts +76 -0
  125. package/utils/format.ts +37 -0
  126. package/utils/index.ts +4 -0
  127. package/utils/number.ts +17 -0
  128. package/utils/package.json +7 -0
  129. package/utils/text.ts +6 -0
  130. package/validate/index.ts +119 -0
  131. package/validate/package.json +9 -0
  132. package/wow/absolute-position.ts +142 -0
  133. package/wow/animation.ts +428 -0
  134. package/wow/aura-button.ts +76 -0
  135. package/wow/aura-container.ts +591 -0
  136. package/wow/aura-countdown.ts +201 -0
  137. package/wow/aura-slot.ts +153 -0
  138. package/wow/bar-shade.ts +93 -0
  139. package/wow/borrowed-slot.ts +216 -0
  140. package/wow/button-drag.ts +48 -0
  141. package/wow/cast-clock.ts +153 -0
  142. package/wow/cast-text.ts +83 -0
  143. package/wow/class-colors.ts +126 -0
  144. package/wow/clock.ts +7 -0
  145. package/wow/color-picker.ts +128 -0
  146. package/wow/commands.ts +17 -0
  147. package/wow/contained-frame.ts +128 -0
  148. package/wow/contract.ts +42 -0
  149. package/wow/css-box.ts +110 -0
  150. package/wow/css-color.ts +12 -0
  151. package/wow/css-style.ts +109 -0
  152. package/wow/css-surfaces.ts +398 -0
  153. package/wow/css-text.ts +399 -0
  154. package/wow/cvar-schema.ts +32 -0
  155. package/wow/cvars.ts +98 -0
  156. package/wow/dropdown-art.ts +39 -0
  157. package/wow/element-click.ts +20 -0
  158. package/wow/element-event-coordinator.ts +219 -0
  159. package/wow/element-events.ts +317 -0
  160. package/wow/event-dispatcher.ts +151 -0
  161. package/wow/events.ts +50 -0
  162. package/wow/font-cache.ts +92 -0
  163. package/wow/frame-access.ts +31 -0
  164. package/wow/frame-layer.ts +5 -0
  165. package/wow/frames.ts +236 -0
  166. package/wow/heal-prediction.ts +305 -0
  167. package/wow/image-layers.ts +55 -0
  168. package/wow/index.ts +97 -0
  169. package/wow/layout-view.ts +651 -0
  170. package/wow/lua-string.ts +18 -0
  171. package/wow/lua-table.ts +18 -0
  172. package/wow/macro-book.ts +84 -0
  173. package/wow/map-position.ts +27 -0
  174. package/wow/media-query.ts +65 -0
  175. package/wow/module-events.ts +11 -0
  176. package/wow/native-hooks.ts +168 -0
  177. package/wow/native-placement.ts +88 -0
  178. package/wow/native.ts +135 -0
  179. package/wow/number-format.ts +8 -0
  180. package/wow/number-input.ts +10 -0
  181. package/wow/protect.ts +24 -0
  182. package/wow/protected-layout.ts +133 -0
  183. package/wow/range-art.ts +74 -0
  184. package/wow/reparent-frame.ts +99 -0
  185. package/wow/rounded-texture.ts +156 -0
  186. package/wow/saved-variables.ts +60 -0
  187. package/wow/scroll-view.ts +64 -0
  188. package/wow/secret-ref.ts +14 -0
  189. package/wow/secure-bindings.ts +89 -0
  190. package/wow/secure-clicks.ts +432 -0
  191. package/wow/secure-snippet.ts +235 -0
  192. package/wow/secure-state.ts +130 -0
  193. package/wow/secure-target-visibility.ts +89 -0
  194. package/wow/secure-transaction.ts +331 -0
  195. package/wow/special-window.ts +269 -0
  196. package/wow/start-addon.ts +99 -0
  197. package/wow/state-paint.ts +256 -0
  198. package/wow/transition.ts +86 -0
  199. package/wow/unit-button.ts +39 -0
  200. package/wow/unit-cast.ts +92 -0
  201. package/wow/unit-health-text.ts +47 -0
  202. package/wow/view-nodes.ts +304 -0
  203. package/wow/view.ts +1746 -0
  204. package/wow/visible-frame-job.ts +34 -0
  205. package/wow/width-reserve.ts +108 -0
@@ -0,0 +1,428 @@
1
+ import { ownedArray, type OwnedArray, type OwnedReadonlyArray, type OwnedRecord } from "../src/owned-collections";
2
+ import type { RuntimeScope } from "../src/runtime";
3
+ import { fail, ok, type Result } from "../src/result";
4
+ import { currentScope } from "../src/reactive-scope";
5
+ import { nativeSmoothing } from "../animation/ease";
6
+ import { finalAlpha, planAnimation, type AlphaStep, type AnimationPlan, type AnimationSpec, type AnimationStep, type FlipBookStep,
7
+ type MoveStep, type PlannedStep, type RegionAnimation, type RegionStep, type RotateStep, type ScaleStep } from "../animation/spec";
8
+ import { scaleFactors } from "../animation/cycle";
9
+ import type { AnimationGroup, Frame, NativeAnimationOf, Texture } from "./frames";
10
+ import { reveal } from "./native";
11
+ import { protect } from "./protect";
12
+
13
+ type NativeAnimation = ReturnType<AnimationGroup["CreateAnimation"]>;
14
+
15
+ /**
16
+ * A region that owns native animation groups: a frame, a texture or a font string.
17
+ * Animating a frame moves, scales and fades everything inside it.
18
+ */
19
+ export interface AnimationTarget {
20
+ CreateAnimationGroup(this: object, name?: string, template?: string): AnimationGroup;
21
+ SetAlpha(this: object, alpha: number): void;
22
+ }
23
+
24
+ /** The region type a spec needs: rotation and flipbooks work on textures only. */
25
+ export type TargetOf<Step extends AnimationStep> = [Exclude<Step, RegionStep>] extends [never] ? AnimationTarget : Texture;
26
+
27
+ /** "reduced" freezes loops on their first pose and finishes one-shots at once. */
28
+ export type MotionMode = "full" | "reduced";
29
+
30
+ export interface AnimationDefaults {
31
+ /** Read on every play of an animation that has no `motion` option of its own. */
32
+ motion?: (this: void) => MotionMode;
33
+ }
34
+
35
+ export interface NativeAnimationOptions {
36
+ /**
37
+ * Stops the group and clears its script when the scope is disposed. Inside a component's setup the
38
+ * component's scope is used when this is left out.
39
+ */
40
+ scope?: RuntimeScope;
41
+ /** Multiplies every move offset, for views that scale their art. */
42
+ distance?: number;
43
+ /** Native speed multiplier. */
44
+ speed?: number;
45
+ /** Overrides the default motion policy for this animation. Read on every play. */
46
+ motion?: (this: void) => MotionMode;
47
+ /**
48
+ * Called once a one-shot animation ends, and at once when reduced motion skips it.
49
+ * Leave it out for regions whose visibility the client controls with restricted values, such as aura buttons.
50
+ */
51
+ onFinish?: (this: void) => void;
52
+ }
53
+
54
+ export interface PlayOptions {
55
+ reverse?: boolean;
56
+ /** Seconds into the animation. */
57
+ offset?: number;
58
+ }
59
+
60
+ /** Fields a named step can change between plays. */
61
+ export type StepPatch = Partial<Pick<AnimationStep, "duration" | "delay" | "endDelay" | "ease">> & {
62
+ alpha?: AlphaStep["alpha"];
63
+ scale?: ScaleStep["scale"];
64
+ rotate?: number;
65
+ move?: MoveStep["move"];
66
+ };
67
+
68
+ /** Playback of one native animation group. */
69
+ export interface AnimationPlayback {
70
+ /** Start from the beginning, restarting a running animation. */
71
+ play(this: void, options?: PlayOptions): void;
72
+ /** Play a stopped or paused animation without restarting a running one. Use it to keep loops alive. */
73
+ resume(this: void): void;
74
+ pause(this: void): void;
75
+ stop(this: void): void;
76
+ isPlaying(this: void): boolean;
77
+ /** Change a named step. The timeline is planned again, so later steps follow a changed delay or duration. */
78
+ set(this: void, id: string, patch: StepPatch): Result<void>;
79
+ setSpeed(this: void, multiplier: number): void;
80
+ /** Seconds of one pass, after the latest `set`. */
81
+ length(this: void): number;
82
+ }
83
+
84
+ export interface AnimationControl extends AnimationPlayback {
85
+ /** Stop for good and clear the group's script. The native group itself cannot be destroyed. */
86
+ dispose(this: void): void;
87
+ /** The native group, for integrations the controller does not cover. */
88
+ readonly group: AnimationGroup;
89
+ }
90
+
91
+ const looping = { repeat: "REPEAT", bounce: "BOUNCE" } as const;
92
+ let defaults: AnimationDefaults = {};
93
+
94
+ /** Set application-wide defaults, such as the reduced motion setting of the player's profile. */
95
+ export function configureAnimations(this: void, options: AnimationDefaults): void {
96
+ defaults = { ...options as OwnedRecord<AnimationDefaults> };
97
+ }
98
+
99
+ function applyTiming(animation: NativeAnimation, step: PlannedStep) {
100
+ animation.SetOrder(1);
101
+ animation.SetDuration(step.duration);
102
+ animation.SetStartDelay(step.start);
103
+ animation.SetEndDelay(step.endDelay);
104
+ animation.SetSmoothing(nativeSmoothing(step.ease));
105
+ }
106
+
107
+ function applyValues(animation: NativeAnimation, step: AnimationStep, distance: number) {
108
+ if (step.alpha !== undefined) {
109
+ const alpha = animation as NativeAnimationOf<"Alpha">;
110
+ alpha.SetFromAlpha(step.alpha[0]);
111
+ alpha.SetToAlpha(step.alpha[1]);
112
+ } else if (step.scale !== undefined) {
113
+ const scale = animation as NativeAnimationOf<"Scale">;
114
+ const [fromX, fromY] = scaleFactors(step.scale[0]);
115
+ const [toX, toY] = scaleFactors(step.scale[1]);
116
+ scale.SetScaleFrom(fromX, fromY);
117
+ scale.SetScaleTo(toX, toY);
118
+ if (step.origin) scale.SetOrigin(step.origin.point, step.origin.x ?? 0, step.origin.y ?? 0);
119
+ } else if (step.rotate !== undefined) {
120
+ const rotation = animation as NativeAnimationOf<"Rotation">;
121
+ rotation.SetDegrees(step.rotate);
122
+ const origin = (step as RotateStep).origin;
123
+ if (origin) rotation.SetOrigin(origin.point, origin.x ?? 0, origin.y ?? 0);
124
+ } else if (step.move !== undefined) {
125
+ const move = animation as NativeAnimationOf<"Translation">;
126
+ move.SetOffset(step.move[0] * distance, step.move[1] * distance);
127
+ } else {
128
+ const book = animation as NativeAnimationOf<"FlipBook">;
129
+ const { rows, columns, frames, frameWidth, frameHeight } = (step as FlipBookStep).flipbook;
130
+ book.SetFlipBookRows(rows);
131
+ book.SetFlipBookColumns(columns);
132
+ book.SetFlipBookFrames(frames);
133
+ if (frameWidth !== undefined) book.SetFlipBookFrameWidth(frameWidth);
134
+ if (frameHeight !== undefined) book.SetFlipBookFrameHeight(frameHeight);
135
+ }
136
+ }
137
+
138
+ function createStep(group: AnimationGroup, step: PlannedStep): NativeAnimation {
139
+ switch (step.kind) {
140
+ case "alpha": return group.CreateAnimation("Alpha");
141
+ case "scale": return group.CreateAnimation("Scale");
142
+ case "rotate": return group.CreateAnimation("Rotation");
143
+ case "move": return group.CreateAnimation("Translation");
144
+ case "flipbook": return group.CreateAnimation("FlipBook");
145
+ }
146
+ }
147
+
148
+ function patchSteps(supplied: readonly AnimationStep[], id: string, patch: StepPatch): Result<{ steps: OwnedArray<AnimationStep>; index: number }> {
149
+ const steps = supplied as OwnedReadonlyArray<AnimationStep>;
150
+ const index = steps.findIndex(step => step.id === id);
151
+ if (index < 0) return fail("Unknown animation step " + id);
152
+ const step = steps[index];
153
+ // A patch keeps the step's kind. Changing it would need a new native animation.
154
+ if ((patch.alpha !== undefined && step.alpha === undefined) || (patch.scale !== undefined && step.scale === undefined)
155
+ || (patch.rotate !== undefined && step.rotate === undefined) || (patch.move !== undefined && step.move === undefined)) {
156
+ return fail("A step patch cannot change the kind of step");
157
+ }
158
+ const next = steps.slice() as OwnedArray<AnimationStep>;
159
+ next[index] = { ...step as OwnedRecord<AnimationStep>, ...patch as OwnedRecord<StepPatch> } as AnimationStep;
160
+ return ok({ steps: next, index });
161
+ }
162
+
163
+ /**
164
+ * Build one native animation group from a spec. The group is created once; play it as often as needed.
165
+ * Steps share order 1 and start through their delays, the layout of Blizzard's alert animations.
166
+ * Rotation and flipbook steps need a texture; the overloads check that at compile time.
167
+ */
168
+ export function createAnimation(this: void, target: AnimationTarget, spec: RegionAnimation, options?: NativeAnimationOptions): Result<AnimationControl>;
169
+ export function createAnimation(this: void, target: Texture, spec: AnimationSpec, options?: NativeAnimationOptions): Result<AnimationControl>;
170
+ export function createAnimation(this: void, target: AnimationTarget, spec: AnimationSpec,
171
+ options: NativeAnimationOptions = {}): Result<AnimationControl> {
172
+ return build(target, spec, options, options.scope ?? currentScope());
173
+ }
174
+
175
+ function build(target: AnimationTarget, spec: AnimationSpec, options: Omit<NativeAnimationOptions, "scope">,
176
+ scope: RuntimeScope | undefined): Result<AnimationControl> {
177
+ const planned = planAnimation(spec);
178
+ if (!planned.ok) return planned;
179
+ if (scope && !scope.isActive()) return fail("Cannot animate after the scope has been disposed");
180
+ const distance = options.distance ?? 1;
181
+ if (!Number.isFinite(distance)) return fail("Animation distance must be a number");
182
+
183
+ const created = protect(() => {
184
+ const group = target.CreateAnimationGroup();
185
+ if (planned.value.loop) group.SetLooping(looping[planned.value.loop]);
186
+ if (planned.value.keepFinalAlpha) group.SetToFinalAlpha(true);
187
+ const animations = ownedArray<NativeAnimation>();
188
+ for (const step of planned.value.steps as OwnedReadonlyArray<PlannedStep>) {
189
+ const animation = createStep(group, step);
190
+ applyTiming(animation, step);
191
+ applyValues(animation, step.step, distance);
192
+ animations.push(animation);
193
+ }
194
+ return { group, animations };
195
+ });
196
+ if (!created.ok) return created;
197
+ const { group, animations } = created.value;
198
+ let plan: AnimationPlan = planned.value;
199
+ let steps: readonly AnimationStep[] = spec.steps;
200
+ let released = false;
201
+ const onFinish = options.onFinish;
202
+ const scripted = onFinish !== undefined && !plan.loop;
203
+
204
+ if (scripted) group.SetScript("OnFinished", () => { if (!released) onFinish(); });
205
+ if (options.speed !== undefined) group.SetAnimationSpeedMultiplier(options.speed);
206
+
207
+ function reduced() {
208
+ return (options.motion ?? defaults.motion)?.() === "reduced";
209
+ }
210
+
211
+ function skipToEnd() {
212
+ group.Stop();
213
+ const alpha = plan.keepFinalAlpha ? finalAlpha(plan) : undefined;
214
+ if (alpha !== undefined) target.SetAlpha(alpha);
215
+ onFinish?.();
216
+ }
217
+
218
+ function freeze() {
219
+ // Pause on the first pose. The group's transforms stay applied while it is paused.
220
+ group.Stop();
221
+ group.Play();
222
+ group.Pause();
223
+ }
224
+
225
+ function dispose() {
226
+ if (released) return;
227
+ released = true;
228
+ group.Stop();
229
+ if (scripted) group.SetScript("OnFinished", undefined);
230
+ }
231
+
232
+ const control: AnimationControl = {
233
+ group,
234
+ play: playOptions => {
235
+ if (released) return;
236
+ if (reduced()) {
237
+ if (plan.loop) freeze();
238
+ else skipToEnd();
239
+ return;
240
+ }
241
+ group.Stop();
242
+ // Without options this is the plain Play() the addon already uses.
243
+ if (playOptions) group.Play(playOptions.reverse === true, playOptions.offset ?? 0);
244
+ else group.Play();
245
+ },
246
+ resume: () => {
247
+ if (released) return;
248
+ if (reduced()) {
249
+ if (plan.loop) freeze();
250
+ return;
251
+ }
252
+ if (reveal(group.IsPlaying()) !== true || reveal(group.IsPaused()) === true) group.Play();
253
+ },
254
+ pause: () => { group.Pause(); },
255
+ stop: () => { group.Stop(); },
256
+ isPlaying: () => reveal(group.IsPlaying()) === true && reveal(group.IsPaused()) !== true,
257
+ set: (id, patch) => {
258
+ const patched = patchSteps(steps, id, patch);
259
+ if (!patched.ok) return patched;
260
+ const replanned = planAnimation({ ...spec as OwnedRecord<AnimationSpec>, steps: patched.value.steps });
261
+ if (!replanned.ok) return replanned;
262
+ const applied = protect(() => {
263
+ for (const step of replanned.value.steps as OwnedReadonlyArray<PlannedStep>) applyTiming(animations[step.index], step);
264
+ applyValues(animations[patched.value.index], patched.value.steps[patched.value.index], distance);
265
+ });
266
+ if (!applied.ok) return applied;
267
+ plan = replanned.value;
268
+ steps = patched.value.steps;
269
+ return ok(undefined);
270
+ },
271
+ setSpeed: multiplier => {
272
+ if (Number.isFinite(multiplier) && multiplier > 0) group.SetAnimationSpeedMultiplier(multiplier);
273
+ },
274
+ length: () => plan.length,
275
+ dispose,
276
+ };
277
+
278
+ scope?.onDispose(dispose);
279
+ return ok(control);
280
+ }
281
+
282
+ export interface AnimationRefOptions extends Omit<NativeAnimationOptions, "scope"> {
283
+ /** Play when the element mounts. */
284
+ autoplay?: boolean;
285
+ }
286
+
287
+ /**
288
+ * An animation attached through a JSX `ref`. It is built when the element mounts and disposed with it, so it
289
+ * needs no scope. Playback calls before the mount are kept: `play` plays on mount and `set` changes the spec.
290
+ */
291
+ export interface AnimationRef<Target> extends AnimationPlayback {
292
+ readonly ref: (this: void, target?: Target) => void;
293
+ /** The mounted animation, or a failure explaining why there is none. */
294
+ control(this: void): Result<AnimationControl>;
295
+ }
296
+
297
+ const notMounted = fail("The animated element is not mounted");
298
+
299
+ /**
300
+ * Animate a JSX element through its `ref`. On a frame the animation moves, scales and fades everything inside it.
301
+ *
302
+ * ```tsx
303
+ * const pop = useAnimation(presets.popIn(), { autoplay: true });
304
+ * return <div ref={pop.ref} width={120} height={40}><span>Ready</span></div>;
305
+ * ```
306
+ */
307
+ export function useAnimation<const Step extends AnimationStep>(this: void, spec: AnimationSpec<Step>,
308
+ options: AnimationRefOptions = {}): AnimationRef<TargetOf<Step>> {
309
+ let current: AnimationSpec = spec;
310
+ let control: Result<AnimationControl> = notMounted;
311
+ let pending: PlayOptions | true | undefined = options.autoplay ? true : undefined;
312
+ let speed = options.speed;
313
+
314
+ function attach(target: AnimationTarget) {
315
+ if (control.ok) control.value.dispose();
316
+ // The element's mount owns the animation: the renderer calls the ref again with nothing on release.
317
+ control = build(target, current, { ...options as OwnedRecord<NativeAnimationOptions>, speed }, undefined);
318
+ if (!control.ok) return;
319
+ if (pending !== undefined) control.value.play(pending === true ? undefined : pending);
320
+ pending = options.autoplay ? true : undefined;
321
+ }
322
+
323
+ return {
324
+ ref: target => {
325
+ if (target === undefined) {
326
+ if (control.ok) control.value.dispose();
327
+ control = notMounted;
328
+ pending = options.autoplay ? true : undefined;
329
+ } else {
330
+ attach(target as AnimationTarget);
331
+ }
332
+ },
333
+ control: () => control,
334
+ play: playOptions => {
335
+ if (control.ok) control.value.play(playOptions);
336
+ else pending = playOptions ?? true;
337
+ },
338
+ resume: () => {
339
+ if (control.ok) control.value.resume();
340
+ else pending ??= true;
341
+ },
342
+ pause: () => { if (control.ok) control.value.pause(); },
343
+ stop: () => {
344
+ if (control.ok) control.value.stop();
345
+ else pending = undefined;
346
+ },
347
+ isPlaying: () => control.ok && control.value.isPlaying(),
348
+ set: (id, patch) => {
349
+ if (control.ok) {
350
+ const changed = control.value.set(id, patch);
351
+ if (!changed.ok) return changed;
352
+ }
353
+ const patched = patchSteps(current.steps, id, patch);
354
+ if (!patched.ok) return patched;
355
+ const next = { ...current as OwnedRecord<AnimationSpec>, steps: patched.value.steps };
356
+ const valid = planAnimation(next);
357
+ if (!valid.ok) return valid;
358
+ current = next;
359
+ return ok(undefined);
360
+ },
361
+ setSpeed: multiplier => {
362
+ if (!Number.isFinite(multiplier) || multiplier <= 0) return;
363
+ speed = multiplier;
364
+ if (control.ok) control.value.setSpeed(multiplier);
365
+ },
366
+ length: () => control.ok ? control.value.length() : lengthOf(current),
367
+ };
368
+ }
369
+
370
+ function lengthOf(spec: AnimationSpec) {
371
+ const plan = planAnimation(spec);
372
+ return plan.ok ? plan.value.length : 0;
373
+ }
374
+
375
+ /**
376
+ * Keep animations running only while a frame is visible. Hooks cannot be removed, so the scope only disables them.
377
+ * Do not use it on aura buttons or other frames the client shows and hides with restricted values.
378
+ */
379
+ export function playWhileVisible(this: void, frame: Frame, scope: RuntimeScope, ...controls: AnimationPlayback[]): void {
380
+ let active = scope.isActive();
381
+ scope.onDispose(() => { active = false; });
382
+ frame.HookScript("OnShow", () => {
383
+ if (active) for (const control of controls) control.resume();
384
+ });
385
+ frame.HookScript("OnHide", () => {
386
+ if (active) for (const control of controls) control.stop();
387
+ });
388
+ if (active && reveal(frame.IsVisible()) === true) for (const control of controls) control.resume();
389
+ }
390
+
391
+ export interface Ticker {
392
+ start(this: void): void;
393
+ stop(this: void): void;
394
+ isRunning(this: void): boolean;
395
+ }
396
+
397
+ /**
398
+ * Call `tick` every `interval` seconds from a looping native animation. The client keeps the time, so no
399
+ * Lua runs between ticks, unlike an OnUpdate counter. Return false from `tick` to stop.
400
+ * Inside a component's setup the ticker stops with the component when `scope` is left out.
401
+ */
402
+ export function createTicker(this: void, target: Pick<AnimationTarget, "CreateAnimationGroup">, interval: number,
403
+ tick: (this: void) => boolean | void, scope: RuntimeScope | undefined = currentScope()): Result<Ticker> {
404
+ if (!Number.isFinite(interval) || interval <= 0) return fail("A ticker needs a positive interval");
405
+ const created = protect(() => {
406
+ const group = target.CreateAnimationGroup();
407
+ group.SetLooping("REPEAT");
408
+ group.CreateAnimation("Animation").SetDuration(interval);
409
+ return group;
410
+ });
411
+ if (!created.ok) return created;
412
+ const group = created.value;
413
+ let released = false;
414
+ group.SetScript("OnLoop", () => {
415
+ if (released) return;
416
+ if (tick() === false) group.Stop();
417
+ });
418
+ scope?.onDispose(() => {
419
+ released = true;
420
+ group.Stop();
421
+ group.SetScript("OnLoop", undefined);
422
+ });
423
+ return ok({
424
+ start: () => { if (!released && reveal(group.IsPlaying()) !== true) group.Play(); },
425
+ stop: () => { group.Stop(); },
426
+ isRunning: () => reveal(group.IsPlaying()) === true,
427
+ });
428
+ }
@@ -0,0 +1,76 @@
1
+ import { fail, type Result } from "../src/result";
2
+ import type { Frame, Texture } from "./frames";
3
+ import { api, type WoWGlobals } from "./native";
4
+ import { protect } from "./protect";
5
+
6
+ type NativeEnums = WoWGlobals["Enum"];
7
+ type DispelTextureStyle = keyof NativeEnums["CustomAuraButtonDispelTypeTextureStyle"];
8
+ type StealableFilter = keyof NativeEnums["CustomAuraButtonDispelTypeStealableFilter"];
9
+
10
+ export type AuraDispelColorMap = Readonly<Partial<Record<string, object>>>;
11
+
12
+ interface NativeDispelTextureOptions {
13
+ style: number;
14
+ showWhenHarmful: boolean;
15
+ showWhenHelpful: boolean;
16
+ showWithoutDispelType: boolean;
17
+ stealableFilter?: number;
18
+ customDispelColorMap?: AuraDispelColorMap;
19
+ }
20
+
21
+ export interface AuraDispelTarget {
22
+ AddDispelTypeTexture?(this: object, texture: Texture, options: NativeDispelTextureOptions): void;
23
+ }
24
+
25
+ export interface AuraDispelTextureOptions {
26
+ style?: DispelTextureStyle;
27
+ showWhenHarmful?: boolean;
28
+ showWhenHelpful?: boolean;
29
+ showWithoutDispelType?: boolean;
30
+ stealableFilter?: StealableFilter;
31
+ colors?: AuraDispelColorMap;
32
+ fallbackToDefaultColors?: boolean;
33
+ }
34
+
35
+ /** Register a native tint or icon. The client selects and colors the region from the aura it displays. */
36
+ export function bindAuraDispelTexture(button: Frame & AuraDispelTarget, texture: Texture, options: AuraDispelTextureOptions = {}): Result<void> {
37
+ const add = button.AddDispelTypeTexture;
38
+ const style = api.Enum?.CustomAuraButtonDispelTypeTextureStyle?.[options.style ?? "PreserveAsset"];
39
+ if (add === undefined || style === undefined) return fail("Aura dispel textures are unavailable");
40
+ const filter = options.stealableFilter;
41
+ const stealableFilter = filter === undefined ? undefined : api.Enum?.CustomAuraButtonDispelTypeStealableFilter?.[filter];
42
+ if (filter !== undefined && stealableFilter === undefined) return fail("Aura stealable filter is unavailable");
43
+ const settings: NativeDispelTextureOptions = {
44
+ style,
45
+ showWhenHarmful: options.showWhenHarmful ?? true,
46
+ showWhenHelpful: options.showWhenHelpful ?? false,
47
+ showWithoutDispelType: options.showWithoutDispelType ?? false,
48
+ stealableFilter,
49
+ };
50
+ const result = protect(() => {
51
+ texture.SetVertexColor(1, 1, 1, 1);
52
+ const nativeOptions = options.colors === undefined ? settings : { ...settings, customDispelColorMap: options.colors };
53
+ add.call(button, texture, nativeOptions);
54
+ });
55
+ if (result.ok || options.colors === undefined || options.fallbackToDefaultColors === false) return result;
56
+ return protect(() => add.call(button, texture, settings));
57
+ }
58
+
59
+ interface AuraTooltipTarget {
60
+ SetHideTooltipInCombat?(this: object, hidden: boolean): void;
61
+ }
62
+
63
+ export type AuraTooltipMode = "shown" | "combat" | "hidden";
64
+
65
+ /** Configure hover while letting clicks pass through to the unit frame below the aura. */
66
+ export function configureAuraMouse(button: Frame & AuraTooltipTarget, mode: AuraTooltipMode): Result<void> {
67
+ const setTooltip = button.SetHideTooltipInCombat;
68
+ const tooltip = mode !== "hidden" && setTooltip !== undefined
69
+ ? protect(() => setTooltip.call(button, mode === "combat"))
70
+ : undefined;
71
+ const hover = protect(() => button.SetMouseMotionEnabled?.(mode !== "hidden"));
72
+ // Configuring tooltips may re-enable clicks. Attempt this last even when a tooltip call fails.
73
+ const clicks = protect(() => button.SetMouseClickEnabled?.(false));
74
+ if (tooltip !== undefined && !tooltip.ok) return tooltip;
75
+ return !hover.ok ? hover : clicks;
76
+ }