@leafer-game/worker 1.0.10 → 1.1.1

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.
@@ -1,654 +1,5 @@
1
1
  export * from '@leafer-ui/worker';
2
2
  export * from '@leafer-in/robot';
3
- import { decorateLeafAttr, attr, State, PathCommandMap, MatrixHelper, PointHelper, UnitConvert, BezierHelper, isNull as isNull$1, Transition, UI as UI$1, LeafHelper, BranchHelper } from '@leafer-ui/draw';
4
- import { MathHelper, State as State$1, isNull, PointerEvent, UI, dataType } from '@leafer-ui/core';
3
+ export * from '@leafer-in/state';
5
4
  export * from '@leafer-in/animate';
6
-
7
- function stateType(defaultValue, styleName) {
8
- return decorateLeafAttr(defaultValue, (key) => attr({
9
- set(value) {
10
- this.__setAttr(key, value);
11
- this.waitLeafer(() => styleName ? State.setStyleName(this, styleName, value) : State.set(this, value));
12
- }
13
- }));
14
- }
15
- function stateStyleType(defaultValue) {
16
- return decorateLeafAttr(defaultValue, (key) => attr({
17
- set(value) {
18
- this.__setAttr(key, value);
19
- this.__layout.stateStyleChanged = true;
20
- }
21
- }));
22
- }
23
-
24
- function findParentButton(leaf, button) {
25
- if (button && button !== true)
26
- return button;
27
- if (!leaf.button) {
28
- let { parent } = leaf;
29
- for (let i = 0; i < 2; i++) {
30
- if (parent) {
31
- if (parent.button)
32
- return parent;
33
- parent = parent.parent;
34
- }
35
- }
36
- }
37
- return null;
38
- }
39
-
40
- function setStyle(leaf, style) {
41
- if (typeof style !== 'object')
42
- style = undefined;
43
- updateStyle(leaf, style, 'in');
44
- }
45
- function unsetStyle(leaf, style) {
46
- const { normalStyle } = leaf;
47
- if (typeof style !== 'object')
48
- style = undefined;
49
- if (normalStyle) {
50
- if (!style)
51
- style = normalStyle;
52
- updateStyle(leaf, style, 'out');
53
- }
54
- }
55
- const emprtyStyle = {};
56
- function updateStyle(leaf, style, type) {
57
- const { normalStyle } = leaf;
58
- if (!style)
59
- style = emprtyStyle;
60
- if (style.scale) {
61
- MathHelper.assignScale(style, style.scale);
62
- delete style.scale;
63
- }
64
- if (style === emprtyStyle || !State$1.canAnimate)
65
- type = null;
66
- let transition = type ? getTransition(type, style, leaf) : false;
67
- const fromStyle = transition ? getFromStyle(leaf, style) : undefined;
68
- leaf.killAnimate('transition');
69
- if (normalStyle)
70
- leaf.set(normalStyle, true);
71
- const statesStyle = getStyle(leaf);
72
- if (statesStyle) {
73
- const { animation } = statesStyle;
74
- if (animation) {
75
- const animate = leaf.animate(animation, undefined, 'animation', true);
76
- Object.assign(statesStyle, animate.endingStyle);
77
- if (type !== 'in' || style.animation !== animation)
78
- animate.kill();
79
- else
80
- transition = false;
81
- delete statesStyle.animation;
82
- }
83
- leaf.normalStyle = filterStyle(statesStyle, leaf);
84
- leaf.set(statesStyle, true);
85
- }
86
- else {
87
- leaf.normalStyle = undefined;
88
- }
89
- if (transition) {
90
- const toStyle = filterStyle(fromStyle, leaf);
91
- leaf.set(fromStyle, true);
92
- leaf.animate([fromStyle, toStyle], transition, 'transition', true);
93
- }
94
- leaf.__layout.stateStyleChanged = false;
95
- }
96
- function getStyle(leaf) {
97
- let exist;
98
- const style = {}, { state } = leaf, button = findParentButton(leaf);
99
- const stateStyle = state && leaf.states[state];
100
- if (stateStyle && State$1.isState(state, leaf, button))
101
- exist = assign(style, stateStyle);
102
- const selectedStyle = style.selectedStyle || leaf.selectedStyle;
103
- if (selectedStyle && State$1.isSelected(leaf, button))
104
- exist = assign(style, selectedStyle);
105
- if (State$1.isDisabled(leaf, button)) {
106
- const disabledStyle = style.disabledStyle || leaf.disabledStyle;
107
- if (disabledStyle)
108
- exist = assign(style, disabledStyle);
109
- }
110
- else {
111
- const focusStyle = style.focusStyle || leaf.focusStyle;
112
- if (focusStyle && State$1.isFocus(leaf, button))
113
- exist = assign(style, focusStyle);
114
- const hoverStyle = style.hoverStyle || leaf.hoverStyle;
115
- if (hoverStyle && State$1.isHover(leaf, button))
116
- exist = assign(style, hoverStyle);
117
- const pressStyle = style.pressStyle || leaf.pressStyle;
118
- if (pressStyle && State$1.isPress(leaf, button))
119
- exist = assign(style, pressStyle);
120
- }
121
- return exist ? style : undefined;
122
- }
123
- function filterStyle(style, data, addStyle, useAnimateExcludes) {
124
- const to = addStyle ? style : {}, forStyle = addStyle || style;
125
- for (let key in forStyle) {
126
- if (useAnimateExcludes) {
127
- if (!State$1.animateExcludes[key])
128
- to[key] = data[key];
129
- }
130
- else
131
- to[key] = data[key];
132
- }
133
- return to;
134
- }
135
- function filterAnimateStyle(style, data, addStyle) {
136
- return filterStyle(style, data, addStyle, true);
137
- }
138
- function getFromStyle(leaf, style) {
139
- const fromStyle = filterAnimateStyle(style, leaf), animate = leaf.animate();
140
- if (animate)
141
- filterAnimateStyle(fromStyle, leaf, animate.fromStyle);
142
- return fromStyle;
143
- }
144
- function getTransition(type, style, data) {
145
- let name = type === 'in' ? 'transition' : 'transitionOut';
146
- if (type === 'out' && isNull(data[name]) && isNull(style[name]))
147
- name = 'transition';
148
- return isNull(style[name]) ? data[name] : style[name];
149
- }
150
- function assign(style, stateStyle) {
151
- Object.assign(style, stateStyle);
152
- return true;
153
- }
154
-
155
- function setPointerState(leaf, stateName) {
156
- const style = leaf[stateName];
157
- if (style)
158
- setStyle(leaf, style);
159
- if (leaf.button)
160
- setChildrenState(leaf.children, stateName);
161
- }
162
- function setState(leaf, stateName, stateStyle) {
163
- if (!stateStyle)
164
- stateStyle = leaf.states[stateName];
165
- setStyle(leaf, stateStyle);
166
- if (leaf.button)
167
- setChildrenState(leaf.children, null, stateName);
168
- }
169
- function setChildrenState(children, stateType, state) {
170
- if (!children)
171
- return;
172
- let leaf, update;
173
- for (let i = 0, len = children.length; i < len; i++) {
174
- leaf = children[i];
175
- if (stateType) {
176
- update = true;
177
- switch (stateType) {
178
- case 'hoverStyle':
179
- if (State$1.isHover(leaf))
180
- update = false;
181
- break;
182
- case 'pressStyle':
183
- if (State$1.isPress(leaf))
184
- update = false;
185
- break;
186
- case 'focusStyle':
187
- if (State$1.isFocus(leaf))
188
- update = false;
189
- }
190
- if (update)
191
- setPointerState(leaf, stateType);
192
- }
193
- else if (state)
194
- setState(leaf, state);
195
- if (leaf.isBranch)
196
- setChildrenState(leaf.children, stateType, state);
197
- }
198
- }
199
-
200
- function unsetPointerState(leaf, stateName) {
201
- const style = leaf[stateName];
202
- if (style)
203
- unsetStyle(leaf, style);
204
- if (leaf.button)
205
- unsetChildrenState(leaf.children, stateName);
206
- }
207
- function unsetState(leaf, stateName, stateStyle) {
208
- unsetStyle(leaf, stateStyle);
209
- if (leaf.button)
210
- unsetChildrenState(leaf.children, null, stateName);
211
- }
212
- function unsetChildrenState(children, stateType, state) {
213
- if (!children)
214
- return;
215
- let leaf;
216
- for (let i = 0, len = children.length; i < len; i++) {
217
- leaf = children[i];
218
- if (stateType)
219
- unsetPointerState(leaf, stateType);
220
- else if (state)
221
- unsetState(leaf, state);
222
- if (leaf.isBranch)
223
- unsetChildrenState(leaf.children, stateType, state);
224
- }
225
- }
226
-
227
- function updateEventStyle(leaf, eventType) {
228
- switch (eventType) {
229
- case PointerEvent.ENTER:
230
- setPointerState(leaf, 'hoverStyle');
231
- break;
232
- case PointerEvent.LEAVE:
233
- unsetPointerState(leaf, 'hoverStyle');
234
- break;
235
- case PointerEvent.DOWN:
236
- setPointerState(leaf, 'pressStyle');
237
- break;
238
- case PointerEvent.UP:
239
- unsetPointerState(leaf, 'pressStyle');
240
- break;
241
- }
242
- }
243
-
244
- function checkPointerState(fnName, leaf, button) {
245
- let find;
246
- const interaction = leaf.leafer ? leaf.leafer.interaction : null;
247
- if (interaction) {
248
- find = interaction[fnName](leaf);
249
- if (!find && button) {
250
- const parentButton = findParentButton(leaf, button);
251
- if (parentButton)
252
- find = interaction[fnName](parentButton);
253
- }
254
- }
255
- return find;
256
- }
257
- function checkFixedState(attrName, leaf, button) {
258
- let find = leaf[attrName];
259
- if (!find && button) {
260
- const parentButton = findParentButton(leaf, button);
261
- if (parentButton)
262
- find = parentButton[attrName];
263
- }
264
- return find;
265
- }
266
- function checkState(stateName, leaf, button) {
267
- let find = leaf.states[stateName];
268
- if (!find && button) {
269
- const parentButton = findParentButton(leaf, button);
270
- if (parentButton)
271
- find = parentButton.states[stateName];
272
- }
273
- return !!find;
274
- }
275
-
276
- State$1.animateExcludes = {
277
- animation: 1,
278
- animationOut: 1,
279
- transition: 1,
280
- transitionOut: 1,
281
- states: 1,
282
- state: 1,
283
- normalStyle: 1,
284
- hoverStyle: 1,
285
- pressStyle: 1,
286
- focusStyle: 1,
287
- selectedStyle: 1,
288
- disabledStyle: 1
289
- };
290
- State$1.isState = function (state, leaf, button) { return checkState(state, leaf, button); };
291
- State$1.isSelected = function (leaf, button) { return checkFixedState('selected', leaf, button); };
292
- State$1.isDisabled = function (leaf, button) { return checkFixedState('disabled', leaf, button); };
293
- State$1.isFocus = function (leaf, button) { return checkPointerState('isFocus', leaf, button); };
294
- State$1.isHover = function (leaf, button) { return checkPointerState('isHover', leaf, button); };
295
- State$1.isPress = function (leaf, button) { return checkPointerState('isPress', leaf, button); };
296
- State$1.isDrag = function (leaf, button) { return checkPointerState('isDrag', leaf, button); };
297
- State$1.setStyleName = function (leaf, stateType, value) { value ? setState(leaf, stateType, leaf[stateType]) : unsetState(leaf, stateType, leaf[stateType]); };
298
- State$1.set = function (leaf, stateName) { const style = leaf.states[stateName]; style ? setState(leaf, stateName, style) : unsetState(leaf, stateName, style); };
299
- State$1.getStyle = getStyle;
300
- State$1.updateStyle = updateStyle;
301
- State$1.updateEventStyle = updateEventStyle;
302
- const ui$1 = UI.prototype;
303
- stateType(false, 'selectedStyle')(ui$1, 'selected');
304
- stateType(false, 'disabledStyle')(ui$1, 'disabled');
305
- stateStyleType({})(ui$1, 'states');
306
- stateType('')(ui$1, 'state');
307
- dataType()(ui$1, 'normalStyle');
308
- stateStyleType()(ui$1, 'hoverStyle');
309
- stateStyleType()(ui$1, 'pressStyle');
310
- stateStyleType()(ui$1, 'focusStyle');
311
- stateStyleType()(ui$1, 'selectedStyle');
312
- stateStyleType()(ui$1, 'disabledStyle');
313
- dataType(false)(ui$1, 'button');
314
- ui$1.focus = function (value = true) {
315
- this.waitLeafer(() => {
316
- let { focusData } = this.app.interaction;
317
- if (value) {
318
- if (focusData)
319
- focusData.focus(false);
320
- focusData = this;
321
- }
322
- else
323
- focusData = null;
324
- this.app.interaction.focusData = focusData;
325
- value ? setPointerState(this, 'focusStyle') : unsetPointerState(this, 'focusStyle');
326
- });
327
- };
328
- ui$1.updateState = function () {
329
- State$1.updateStyle(this, undefined, 'in');
330
- };
331
-
332
- const gaussNodes = [0.1488743389, 0.4333953941, 0.6794095682, 0.8650633666, 0.9739065285];
333
- const gaussWeights = [0.2955242247, 0.2692667193, 0.2190863625, 0.1494513491, 0.0666713443];
334
- const { sqrt } = Math;
335
- const HighBezierHelper = {
336
- getDistance(fromX, fromY, x1, y1, x2, y2, toX, toY) {
337
- let distance = 0, t1, t2, d1X, d1Y, d2X, d2Y;
338
- for (let i = 0; i < gaussNodes.length; i++) {
339
- t1 = 0.5 * (1 + gaussNodes[i]);
340
- t2 = 0.5 * (1 - gaussNodes[i]);
341
- d1X = getDerivative(t1, fromX, x1, x2, toX);
342
- d1Y = getDerivative(t1, fromY, y1, y2, toY);
343
- d2X = getDerivative(t2, fromX, x1, x2, toX);
344
- d2Y = getDerivative(t2, fromY, y1, y2, toY);
345
- distance += gaussWeights[i] * (sqrt(d1X * d1X + d1Y * d1Y) + sqrt(d2X * d2X + d2Y * d2Y));
346
- }
347
- return distance * 0.5;
348
- },
349
- getDerivative(t, fromV, v1, v2, toV) {
350
- const o = 1 - t;
351
- return 3 * o * o * (v1 - fromV) + 6 * o * t * (v2 - v1) + 3 * t * t * (toV - v2);
352
- },
353
- cut(data, t, fromX, fromY, x1, y1, x2, y2, toX, toY) {
354
- const o = 1 - t;
355
- const ax = o * fromX + t * x1, ay = o * fromY + t * y1;
356
- const mbx = o * x1 + t * x2, mby = o * y1 + t * y2;
357
- const mcx = o * x2 + t * toX, mcy = o * y2 + t * toY;
358
- const bx = o * ax + t * mbx, by = o * ay + t * mby;
359
- const mbcx = o * mbx + t * mcx, mbcy = o * mby + t * mcy;
360
- const cx = o * bx + t * mbcx, cy = o * by + t * mbcy;
361
- data.push(PathCommandMap.C, ax, ay, bx, by, cx, cy);
362
- }
363
- };
364
- const { getDerivative } = HighBezierHelper;
365
-
366
- const { M, L, C, Z } = PathCommandMap;
367
- const tempPoint = {}, tempFrom = {};
368
- const HighCurveHelper = {
369
- transform(data, matrix) {
370
- let i = 0, command;
371
- const len = data.length;
372
- while (i < len) {
373
- command = data[i];
374
- switch (command) {
375
- case M:
376
- case L:
377
- HighCurveHelper.transformPoints(data, matrix, i, 1);
378
- i += 3;
379
- break;
380
- case C:
381
- HighCurveHelper.transformPoints(data, matrix, i, 3);
382
- i += 7;
383
- break;
384
- case Z:
385
- i += 1;
386
- }
387
- }
388
- },
389
- transformPoints(data, matrix, start, pointCount) {
390
- for (let i = start + 1, end = i + pointCount * 2; i < end; i += 2) {
391
- tempPoint.x = data[i];
392
- tempPoint.y = data[i + 1];
393
- MatrixHelper.toOuterPoint(matrix, tempPoint);
394
- data[i] = tempPoint.x;
395
- data[i + 1] = tempPoint.y;
396
- }
397
- },
398
- getMotionPathData(data) {
399
- let total = 0, distance, segments = [];
400
- let i = 0, x = 0, y = 0, toX, toY, command;
401
- const len = data.length;
402
- while (i < len) {
403
- command = data[i];
404
- switch (command) {
405
- case M:
406
- case L:
407
- toX = data[i + 1];
408
- toY = data[i + 2];
409
- distance = (command === L && i > 0) ? PointHelper.getDistanceFrom(x, y, toX, toY) : 0;
410
- x = toX;
411
- y = toY;
412
- i += 3;
413
- break;
414
- case C:
415
- toX = data[i + 5];
416
- toY = data[i + 6];
417
- distance = HighBezierHelper.getDistance(x, y, data[i + 1], data[i + 2], data[i + 3], data[i + 4], toX, toY);
418
- x = toX;
419
- y = toY;
420
- i += 7;
421
- break;
422
- case Z:
423
- i += 1;
424
- default:
425
- distance = 0;
426
- }
427
- segments.push(distance);
428
- total += distance;
429
- }
430
- return { total, segments, data };
431
- },
432
- getDistancePoint(distanceData, motionDistance) {
433
- const { segments, data } = distanceData;
434
- motionDistance = UnitConvert.number(motionDistance, distanceData.total);
435
- let total = 0, distance, to = {};
436
- let i = 0, index = 0, x = 0, y = 0, toX, toY, command;
437
- const len = data.length;
438
- while (i < len) {
439
- command = data[i];
440
- switch (command) {
441
- case M:
442
- case L:
443
- toX = data[i + 1];
444
- toY = data[i + 2];
445
- distance = segments[index];
446
- if (total + distance > motionDistance || !distanceData.total) {
447
- if (!i)
448
- x = toX, y = toY;
449
- tempFrom.x = x;
450
- tempFrom.y = y;
451
- to.x = toX;
452
- to.y = toY;
453
- PointHelper.getDistancePoint(tempFrom, to, motionDistance - total, true);
454
- to.rotation = PointHelper.getAngle(tempFrom, to);
455
- return to;
456
- }
457
- x = toX;
458
- y = toY;
459
- i += 3;
460
- break;
461
- case C:
462
- toX = data[i + 5];
463
- toY = data[i + 6];
464
- distance = segments[index];
465
- if (total + distance > motionDistance) {
466
- const x1 = data[i + 1], y1 = data[i + 2], x2 = data[i + 3], y2 = data[i + 4];
467
- motionDistance -= total;
468
- BezierHelper.getPointAndSet(motionDistance / distance, x, y, x1, y1, x2, y2, toX, toY, to);
469
- BezierHelper.getPointAndSet(Math.max(0, motionDistance - 0.1) / distance, x, y, x1, y1, x2, y2, toX, toY, tempFrom);
470
- to.rotation = PointHelper.getAngle(tempFrom, to);
471
- return to;
472
- }
473
- x = toX;
474
- y = toY;
475
- i += 7;
476
- break;
477
- case Z:
478
- i += 1;
479
- default:
480
- distance = 0;
481
- }
482
- index++;
483
- total += distance;
484
- }
485
- return to;
486
- },
487
- getDistancePath(distanceData, motionDistance) {
488
- const { segments, data } = distanceData, path = [];
489
- motionDistance = UnitConvert.number(motionDistance, distanceData.total);
490
- let total = 0, distance, to = {};
491
- let i = 0, index = 0, x = 0, y = 0, toX, toY, command;
492
- const len = data.length;
493
- while (i < len) {
494
- command = data[i];
495
- switch (command) {
496
- case M:
497
- case L:
498
- toX = data[i + 1];
499
- toY = data[i + 2];
500
- distance = segments[index];
501
- if (total + distance > motionDistance || !distanceData.total) {
502
- if (!i)
503
- x = toX, y = toY;
504
- tempFrom.x = x;
505
- tempFrom.y = y;
506
- to.x = toX;
507
- to.y = toY;
508
- PointHelper.getDistancePoint(tempFrom, to, motionDistance - total, true);
509
- path.push(command, to.x, to.y);
510
- return path;
511
- }
512
- x = toX;
513
- y = toY;
514
- i += 3;
515
- path.push(command, x, y);
516
- break;
517
- case C:
518
- const x1 = data[i + 1], y1 = data[i + 2], x2 = data[i + 3], y2 = data[i + 4];
519
- toX = data[i + 5];
520
- toY = data[i + 6];
521
- distance = segments[index];
522
- if (total + distance > motionDistance) {
523
- HighBezierHelper.cut(path, (motionDistance - total) / distance, x, y, x1, y1, x2, y2, toX, toY);
524
- return path;
525
- }
526
- x = toX;
527
- y = toY;
528
- i += 7;
529
- path.push(command, x1, y1, x2, y2, toX, toY);
530
- break;
531
- case Z:
532
- i += 1;
533
- path.push(command);
534
- default:
535
- distance = 0;
536
- }
537
- index++;
538
- total += distance;
539
- }
540
- return path;
541
- }
542
- };
543
-
544
- function motionPathType(defaultValue) {
545
- return decorateLeafAttr(defaultValue, (key) => attr({
546
- set(value) {
547
- this.__setAttr(key, value);
548
- this.__hasMotionPath = this.motionPath || !isNull$1(this.motion);
549
- this.__layout.matrixChanged || this.__layout.matrixChange();
550
- }
551
- }));
552
- }
553
-
554
- Transition.register('motion', function (from, to, t, target) {
555
- if (!from)
556
- from = 0;
557
- else if (typeof from === 'object')
558
- from = UnitConvert.number(from, target.getMotionTotal());
559
- if (!to)
560
- to = 0;
561
- else if (typeof to === 'object')
562
- to = UnitConvert.number(to, target.getMotionTotal());
563
- return Transition.number(from, to, t);
564
- });
565
- Transition.register('motionRotation', function (from, to, t) {
566
- return Transition.number(from, to, t);
567
- });
568
- const ui = UI$1.prototype;
569
- const { updateMatrix, updateAllMatrix } = LeafHelper;
570
- const { updateBounds } = BranchHelper;
571
- motionPathType()(ui, 'motionPath');
572
- motionPathType()(ui, 'motion');
573
- motionPathType(true)(ui, 'motionRotation');
574
- ui.getMotionPathData = function () {
575
- return getMotionPathData(getMotionPath(this));
576
- };
577
- ui.getMotionPoint = function (motionDistance) {
578
- const path = getMotionPath(this);
579
- const data = getMotionPathData(path);
580
- if (!data.total)
581
- return {};
582
- const point = HighCurveHelper.getDistancePoint(data, motionDistance);
583
- MatrixHelper.toOuterPoint(path.localTransform, point);
584
- const { motionRotation } = this;
585
- if (motionRotation === false)
586
- delete point.rotation;
587
- else if (typeof motionRotation === 'number')
588
- point.rotation += motionRotation;
589
- return point;
590
- };
591
- ui.getMotionTotal = function () {
592
- return this.getMotionPathData().total;
593
- };
594
- ui.__updateMotionPath = function () {
595
- const data = this.__;
596
- if (this.__layout.resized && data.__pathForMotion)
597
- data.__pathForMotion = undefined;
598
- if (this.motionPath) {
599
- let child;
600
- const { children } = this.parent, { leaferIsReady } = this;
601
- for (let i = 0; i < children.length; i++) {
602
- child = children[i];
603
- if (!isNull$1(child.motion) && !child.__layout.matrixChanged) {
604
- if (leaferIsReady && child !== this)
605
- this.leafer.layouter.addExtra(child);
606
- updateMotion(child);
607
- }
608
- }
609
- }
610
- else
611
- updateMotion(this);
612
- };
613
- function updateMotion(leaf) {
614
- const { motion, leaferIsCreated } = leaf;
615
- if (isNull$1(motion))
616
- return;
617
- if (leaferIsCreated)
618
- leaf.leafer.created = false;
619
- if (leaf.motionPath) {
620
- const data = getMotionPathData(leaf);
621
- if (data.total)
622
- leaf.__.__pathForRender = HighCurveHelper.getDistancePath(data, motion);
623
- }
624
- else {
625
- leaf.set(leaf.getMotionPoint(motion));
626
- if (!leaf.__hasAutoLayout) {
627
- if (leaf.isBranch)
628
- updateAllMatrix(leaf), updateBounds(leaf, leaf);
629
- else
630
- updateMatrix(leaf);
631
- }
632
- }
633
- if (leaferIsCreated)
634
- leaf.leafer.created = true;
635
- }
636
- function getMotionPath(leaf) {
637
- const { parent } = leaf;
638
- if (!leaf.motionPath && parent) {
639
- const { children } = parent;
640
- for (let i = 0; i < children.length; i++) {
641
- if (children[i].motionPath)
642
- return children[i];
643
- }
644
- }
645
- return leaf;
646
- }
647
- function getMotionPathData(leaf) {
648
- const data = leaf.__;
649
- if (data.__pathForMotion)
650
- return data.__pathForMotion;
651
- return data.__pathForMotion = HighCurveHelper.getMotionPathData(leaf.getPath(true, true));
652
- }
653
-
654
- export { HighBezierHelper, HighCurveHelper, motionPathType, stateStyleType, stateType };
5
+ export * from '@leafer-in/motion-path';
@@ -1 +1 @@
1
- export*from"@leafer-ui/worker";export*from"@leafer-in/robot";import{decorateLeafAttr as t,attr as e,State as n,PathCommandMap as o,MatrixHelper as i,PointHelper as s,UnitConvert as a,BezierHelper as r,isNull as c,Transition as l,UI as u,LeafHelper as f,BranchHelper as h}from"@leafer-ui/draw";import{MathHelper as d,State as y,isNull as m,PointerEvent as S,UI as g,dataType as p}from"@leafer-ui/core";export*from"@leafer-in/animate";function b(o,i){return t(o,(t=>e({set(e){this.__setAttr(t,e),this.waitLeafer((()=>i?n.setStyleName(this,i,e):n.set(this,e)))}})))}function _(n){return t(n,(t=>e({set(e){this.__setAttr(t,e),this.__layout.stateStyleChanged=!0}})))}function P(t,e){if(e&&!0!==e)return e;if(!t.button){let{parent:e}=t;for(let t=0;t<2;t++)if(e){if(e.button)return e;e=e.parent}}return null}function v(t,e){"object"!=typeof e&&(e=void 0),x(t,e,"in")}function D(t,e){const{normalStyle:n}=t;"object"!=typeof e&&(e=void 0),n&&(e||(e=n),x(t,e,"out"))}const M={};function x(t,e,n){const{normalStyle:o}=t;e||(e=M),e.scale&&(d.assignScale(e,e.scale),delete e.scale),e!==M&&y.canAnimate||(n=null);let i=!!n&&function(t,e,n){let o="in"===t?"transition":"transitionOut";"out"===t&&m(n[o])&&m(e[o])&&(o="transition");return m(e[o])?n[o]:e[o]}(n,e,t);const s=i?function(t,e){const n=F(e,t),o=t.animate();o&&F(n,t,o.fromStyle);return n}(t,e):void 0;t.killAnimate("transition"),o&&t.set(o,!0);const a=k(t);if(a){const{animation:o}=a;if(o){const s=t.animate(o,void 0,"animation",!0);Object.assign(a,s.endingStyle),"in"!==n||e.animation!==o?s.kill():i=!1,delete a.animation}t.normalStyle=A(a,t),t.set(a,!0)}else t.normalStyle=void 0;if(i){const e=A(s,t);t.set(s,!0),t.animate([s,e],i,"transition",!0)}t.__layout.stateStyleChanged=!1}function k(t){let e;const n={},{state:o}=t,i=P(t),s=o&&t.states[o];s&&y.isState(o,t,i)&&(e=w(n,s));const a=n.selectedStyle||t.selectedStyle;if(a&&y.isSelected(t,i)&&(e=w(n,a)),y.isDisabled(t,i)){const o=n.disabledStyle||t.disabledStyle;o&&(e=w(n,o))}else{const o=n.focusStyle||t.focusStyle;o&&y.isFocus(t,i)&&(e=w(n,o));const s=n.hoverStyle||t.hoverStyle;s&&y.isHover(t,i)&&(e=w(n,s));const a=n.pressStyle||t.pressStyle;a&&y.isPress(t,i)&&(e=w(n,a))}return e?n:void 0}function A(t,e,n,o){const i=n?t:{},s=n||t;for(let t in s)o&&y.animateExcludes[t]||(i[t]=e[t]);return i}function F(t,e,n){return A(t,e,n,!0)}function w(t,e){return Object.assign(t,e),!0}function C(t,e){const n=t[e];n&&v(t,n),t.button&&O(t.children,e)}function E(t,e,n){n||(n=t.states[e]),v(t,n),t.button&&O(t.children,null,e)}function O(t,e,n){if(!t)return;let o,i;for(let s=0,a=t.length;s<a;s++){if(o=t[s],e){switch(i=!0,e){case"hoverStyle":y.isHover(o)&&(i=!1);break;case"pressStyle":y.isPress(o)&&(i=!1);break;case"focusStyle":y.isFocus(o)&&(i=!1)}i&&C(o,e)}else n&&E(o,n);o.isBranch&&O(o.children,e,n)}}function j(t,e){const n=t[e];n&&D(t,n),t.button&&L(t.children,e)}function R(t,e,n){D(t,n),t.button&&L(t.children,null,e)}function L(t,e,n){if(!t)return;let o;for(let i=0,s=t.length;i<s;i++)o=t[i],e?j(o,e):n&&R(o,n),o.isBranch&&L(o.children,e,n)}function N(t,e,n){let o;const i=e.leafer?e.leafer.interaction:null;if(i&&(o=i[t](e),!o&&n)){const s=P(e,n);s&&(o=i[t](s))}return o}function T(t,e,n){let o=e[t];if(!o&&n){const i=P(e,n);i&&(o=i[t])}return o}y.animateExcludes={animation:1,animationOut:1,transition:1,transitionOut:1,states:1,state:1,normalStyle:1,hoverStyle:1,pressStyle:1,focusStyle:1,selectedStyle:1,disabledStyle:1},y.isState=function(t,e,n){return function(t,e,n){let o=e.states[t];if(!o&&n){const i=P(e,n);i&&(o=i.states[t])}return!!o}(t,e,n)},y.isSelected=function(t,e){return T("selected",t,e)},y.isDisabled=function(t,e){return T("disabled",t,e)},y.isFocus=function(t,e){return N("isFocus",t,e)},y.isHover=function(t,e){return N("isHover",t,e)},y.isPress=function(t,e){return N("isPress",t,e)},y.isDrag=function(t,e){return N("isDrag",t,e)},y.setStyleName=function(t,e,n){n?E(t,e,t[e]):R(t,e,t[e])},y.set=function(t,e){const n=t.states[e];n?E(t,e,n):R(t,e,n)},y.getStyle=k,y.updateStyle=x,y.updateEventStyle=function(t,e){switch(e){case S.ENTER:C(t,"hoverStyle");break;case S.LEAVE:j(t,"hoverStyle");break;case S.DOWN:C(t,"pressStyle");break;case S.UP:j(t,"pressStyle")}};const B=g.prototype;b(!1,"selectedStyle")(B,"selected"),b(!1,"disabledStyle")(B,"disabled"),_({})(B,"states"),b("")(B,"state"),p()(B,"normalStyle"),_()(B,"hoverStyle"),_()(B,"pressStyle"),_()(B,"focusStyle"),_()(B,"selectedStyle"),_()(B,"disabledStyle"),p(!1)(B,"button"),B.focus=function(t=!0){this.waitLeafer((()=>{let{focusData:e}=this.app.interaction;t?(e&&e.focus(!1),e=this):e=null,this.app.interaction.focusData=e,t?C(this,"focusStyle"):j(this,"focusStyle")}))},B.updateState=function(){y.updateStyle(this,void 0,"in")};const H=[.1488743389,.4333953941,.6794095682,.8650633666,.9739065285],I=[.2955242247,.2692667193,.2190863625,.1494513491,.0666713443],{sqrt:U}=Math,q={getDistance(t,e,n,o,i,s,a,r){let c,l,u,f,h,d,y=0;for(let m=0;m<H.length;m++)c=.5*(1+H[m]),l=.5*(1-H[m]),u=z(c,t,n,i,a),f=z(c,e,o,s,r),h=z(l,t,n,i,a),d=z(l,e,o,s,r),y+=I[m]*(U(u*u+f*f)+U(h*h+d*d));return.5*y},getDerivative(t,e,n,o,i){const s=1-t;return 3*s*s*(n-e)+6*s*t*(o-n)+3*t*t*(i-o)},cut(t,e,n,i,s,a,r,c,l,u){const f=1-e,h=f*n+e*s,d=f*i+e*a,y=f*s+e*r,m=f*a+e*c,S=f*h+e*y,g=f*d+e*m,p=f*S+e*(f*y+e*(f*r+e*l)),b=f*g+e*(f*m+e*(f*c+e*u));t.push(o.C,h,d,S,g,p,b)}},{getDerivative:z}=q,{M:V,L:W,C:Z,Z:G}=o,J={},K={},Q={transform(t,e){let n,o=0;const i=t.length;for(;o<i;)switch(n=t[o],n){case V:case W:Q.transformPoints(t,e,o,1),o+=3;break;case Z:Q.transformPoints(t,e,o,3),o+=7;break;case G:o+=1}},transformPoints(t,e,n,o){for(let s=n+1,a=s+2*o;s<a;s+=2)J.x=t[s],J.y=t[s+1],i.toOuterPoint(e,J),t[s]=J.x,t[s+1]=J.y},getMotionPathData(t){let e,n,o,i,a=0,r=[],c=0,l=0,u=0;const f=t.length;for(;c<f;){switch(i=t[c],i){case V:case W:n=t[c+1],o=t[c+2],e=i===W&&c>0?s.getDistanceFrom(l,u,n,o):0,l=n,u=o,c+=3;break;case Z:n=t[c+5],o=t[c+6],e=q.getDistance(l,u,t[c+1],t[c+2],t[c+3],t[c+4],n,o),l=n,u=o,c+=7;break;case G:c+=1;default:e=0}r.push(e),a+=e}return{total:a,segments:r,data:t}},getDistancePoint(t,e){const{segments:n,data:o}=t;e=a.number(e,t.total);let i,c,l,u,f=0,h={},d=0,y=0,m=0,S=0;const g=o.length;for(;d<g;){switch(u=o[d],u){case V:case W:if(c=o[d+1],l=o[d+2],i=n[y],f+i>e||!t.total)return d||(m=c,S=l),K.x=m,K.y=S,h.x=c,h.y=l,s.getDistancePoint(K,h,e-f,!0),h.rotation=s.getAngle(K,h),h;m=c,S=l,d+=3;break;case Z:if(c=o[d+5],l=o[d+6],i=n[y],f+i>e){const t=o[d+1],n=o[d+2],a=o[d+3],u=o[d+4];return e-=f,r.getPointAndSet(e/i,m,S,t,n,a,u,c,l,h),r.getPointAndSet(Math.max(0,e-.1)/i,m,S,t,n,a,u,c,l,K),h.rotation=s.getAngle(K,h),h}m=c,S=l,d+=7;break;case G:d+=1;default:i=0}y++,f+=i}return h},getDistancePath(t,e){const{segments:n,data:o}=t,i=[];e=a.number(e,t.total);let r,c,l,u,f=0,h={},d=0,y=0,m=0,S=0;const g=o.length;for(;d<g;){switch(u=o[d],u){case V:case W:if(c=o[d+1],l=o[d+2],r=n[y],f+r>e||!t.total)return d||(m=c,S=l),K.x=m,K.y=S,h.x=c,h.y=l,s.getDistancePoint(K,h,e-f,!0),i.push(u,h.x,h.y),i;m=c,S=l,d+=3,i.push(u,m,S);break;case Z:const a=o[d+1],g=o[d+2],p=o[d+3],b=o[d+4];if(c=o[d+5],l=o[d+6],r=n[y],f+r>e)return q.cut(i,(e-f)/r,m,S,a,g,p,b,c,l),i;m=c,S=l,d+=7,i.push(u,a,g,p,b,c,l);break;case G:d+=1,i.push(u);default:r=0}y++,f+=r}return i}};function X(n){return t(n,(t=>e({set(e){this.__setAttr(t,e),this.__hasMotionPath=this.motionPath||!c(this.motion),this.__layout.matrixChanged||this.__layout.matrixChange()}})))}l.register("motion",(function(t,e,n,o){return t?"object"==typeof t&&(t=a.number(t,o.getMotionTotal())):t=0,e?"object"==typeof e&&(e=a.number(e,o.getMotionTotal())):e=0,l.number(t,e,n)})),l.register("motionRotation",(function(t,e,n){return l.number(t,e,n)}));const Y=u.prototype,{updateMatrix:$,updateAllMatrix:tt}=f,{updateBounds:et}=h;function nt(t){const{motion:e,leaferIsCreated:n}=t;if(!c(e)){if(n&&(t.leafer.created=!1),t.motionPath){const n=it(t);n.total&&(t.__.__pathForRender=Q.getDistancePath(n,e))}else t.set(t.getMotionPoint(e)),t.__hasAutoLayout||(t.isBranch?(tt(t),et(t,t)):$(t));n&&(t.leafer.created=!0)}}function ot(t){const{parent:e}=t;if(!t.motionPath&&e){const{children:t}=e;for(let e=0;e<t.length;e++)if(t[e].motionPath)return t[e]}return t}function it(t){const e=t.__;return e.__pathForMotion?e.__pathForMotion:e.__pathForMotion=Q.getMotionPathData(t.getPath(!0,!0))}X()(Y,"motionPath"),X()(Y,"motion"),X(!0)(Y,"motionRotation"),Y.getMotionPathData=function(){return it(ot(this))},Y.getMotionPoint=function(t){const e=ot(this),n=it(e);if(!n.total)return{};const o=Q.getDistancePoint(n,t);i.toOuterPoint(e.localTransform,o);const{motionRotation:s}=this;return!1===s?delete o.rotation:"number"==typeof s&&(o.rotation+=s),o},Y.getMotionTotal=function(){return this.getMotionPathData().total},Y.__updateMotionPath=function(){const t=this.__;if(this.__layout.resized&&t.__pathForMotion&&(t.__pathForMotion=void 0),this.motionPath){let t;const{children:e}=this.parent,{leaferIsReady:n}=this;for(let o=0;o<e.length;o++)t=e[o],c(t.motion)||t.__layout.matrixChanged||(n&&t!==this&&this.leafer.layouter.addExtra(t),nt(t))}else nt(this)};export{q as HighBezierHelper,Q as HighCurveHelper,X as motionPathType,_ as stateStyleType,b as stateType};
1
+ export*from"@leafer-ui/worker";export*from"@leafer-in/robot";export*from"@leafer-in/state";export*from"@leafer-in/animate";export*from"@leafer-in/motion-path";