-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathInstructionApplicationAnimation.tsx
143 lines (136 loc) · 5.31 KB
/
InstructionApplicationAnimation.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React from 'react';
import { Group } from 'react-konva';
import { ControlItemComponent } from '../components/ControlItemComponent';
import { StashItemComponent } from '../components/StashItemComponent';
import { Visible } from '../components/Visible';
import { ControlStashConfig } from '../CseMachineControlStashConfig';
import {
defaultActiveColor,
defaultDangerColor,
getTextWidth,
isStashItemInDanger,
reachedStrokeColor
} from '../CseMachineUtils';
import { Animatable, AnimationConfig } from './base/Animatable';
import { AnimatedGenericArrow } from './base/AnimatedGenericArrow';
import { AnimatedTextbox } from './base/AnimatedTextbox';
import { getNodePosition } from './base/AnimationUtils';
/**
* Animation for control instructions that combines the previous stash items into a new stash result.
*
* Instructions include `array lit=`, `call` (for built-in functions)
*/
export class InstructionApplicationAnimation extends Animatable {
private controlInstrAnimation: AnimatedTextbox; // the array literal control item
private stashItemAnimations: AnimatedTextbox[];
private resultAnimation: AnimatedTextbox;
private arrowAnimation?: AnimatedGenericArrow<StashItemComponent, Visible>;
private endX: number;
private resultItemIsFirst: boolean;
constructor(
private controlInstrItem: ControlItemComponent,
private stashItems: StashItemComponent[],
private resultItem: StashItemComponent
) {
super();
this.resultItemIsFirst = (resultItem?.index ?? stashItems[0].index) === 0;
this.endX = stashItems.at(-1)!.x() + stashItems.at(-1)!.width();
this.controlInstrAnimation = new AnimatedTextbox(
controlInstrItem.text,
getNodePosition(controlInstrItem),
{ rectProps: { stroke: defaultActiveColor() } }
);
this.stashItemAnimations = stashItems.map(item => {
return new AnimatedTextbox(item.text, getNodePosition(item), {
rectProps: {
stroke: isStashItemInDanger(item.index) ? defaultDangerColor() : reachedStrokeColor()
}
});
});
this.resultAnimation = new AnimatedTextbox(resultItem.text, {
...getNodePosition(resultItem),
opacity: 0
});
if (resultItem.arrow) {
this.arrowAnimation = new AnimatedGenericArrow(resultItem.arrow, { opacity: 0 });
}
}
draw(): React.ReactNode {
return (
<Group ref={this.ref} key={Animatable.key--}>
{this.controlInstrAnimation.draw()}
{this.stashItemAnimations.map(a => a.draw())}
{this.resultAnimation.draw()}
{this.arrowAnimation?.draw()}
</Group>
);
}
async animate(animationConfig?: AnimationConfig) {
this.resultItem?.ref.current?.hide();
this.resultItem?.arrow?.ref.current?.hide();
const minInstrWidth =
getTextWidth(this.controlInstrItem.text) + ControlStashConfig.ControlItemTextPadding * 2;
const resultX = this.resultItem?.x() ?? this.stashItems[0].x();
const resultY = this.resultItem?.y() ?? this.stashItems[0].y();
const startX = resultX - (this.resultItemIsFirst ? minInstrWidth : 0);
const fadeDuration = ((animationConfig?.duration ?? 1) * 3) / 4;
const fadeInDelay = (animationConfig?.delay ?? 0) + (animationConfig?.duration ?? 1) / 4;
// Move array literal instruction next to stash items
await Promise.all([
this.resultAnimation.animateTo(
{ x: startX + (this.endX - startX) / 2 - this.resultItem!.width() / 2 },
{ duration: 0 }
),
this.controlInstrAnimation.animateRectTo({ stroke: reachedStrokeColor() }, animationConfig),
this.controlInstrAnimation.animateTo(
{
x: startX,
y:
resultY +
(this.resultItemIsFirst
? 0
: (this.resultItem?.height() ?? this.stashItems[0].height())),
width: minInstrWidth
},
animationConfig
),
...this.stashItemAnimations.map(a =>
a.animateRectTo({ stroke: reachedStrokeColor() }, animationConfig)
)
]);
animationConfig = { ...animationConfig, delay: 0 };
// Merge all elements together to form the result
await Promise.all([
this.controlInstrAnimation.animateTo({ x: resultX, y: resultY }, animationConfig),
this.controlInstrAnimation.animateTo(
{ opacity: 0 },
{ ...animationConfig, duration: fadeDuration }
),
...this.stashItemAnimations.flatMap(a => [
a.animateTo({ x: resultX }, animationConfig),
a.animateTo({ opacity: 0 }, { ...animationConfig, duration: fadeDuration })
]),
this.resultAnimation?.animateTo({ x: resultX }, animationConfig),
isStashItemInDanger(this.resultItem.index) &&
this.resultAnimation?.animateRectTo({ stroke: defaultDangerColor() }, animationConfig),
this.resultAnimation?.animateTo(
{ opacity: 1 },
{ ...animationConfig, duration: fadeDuration, delay: fadeInDelay }
),
this.arrowAnimation?.animateTo(
{ opacity: 1 },
{ ...animationConfig, delay: animationConfig?.duration ?? 1 }
)
]);
this.destroy();
}
destroy() {
this.ref.current?.hide();
this.resultItem.ref.current?.show();
this.resultItem.arrow?.ref.current?.show();
this.controlInstrAnimation.destroy();
this.stashItemAnimations.map(a => a.destroy());
this.resultAnimation.destroy();
this.arrowAnimation?.destroy();
}
}