-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathArraySpreadAnimation.tsx
149 lines (135 loc) · 5.23 KB
/
ArraySpreadAnimation.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
144
145
146
147
148
149
//import { Easings } from 'konva/lib/Tween';
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,
defaultStrokeColor,
getTextWidth
} from '../CseMachineUtils';
import { Animatable, AnimationConfig } from './base/Animatable';
import { AnimatedGenericArrow } from './base/AnimatedGenericArrow';
import { AnimatedTextbox } from './base/AnimatedTextbox';
import { getNodePosition } from './base/AnimationUtils';
/**
* Adapted from InstructionApplicationAnimation, but changed resultAnimation to [], among others
*/
export class ArraySpreadAnimation extends Animatable {
private controlInstrAnimation: AnimatedTextbox; // the array literal control item
private stashItemAnimation: AnimatedTextbox;
private resultAnimations: AnimatedTextbox[];
private arrowAnimation?: AnimatedGenericArrow<StashItemComponent, Visible>;
private currCallInstrAnimation: AnimatedTextbox;
private endX: number;
constructor(
private controlInstrItem: ControlItemComponent,
private stashItem: StashItemComponent,
private resultItems: StashItemComponent[],
private currCallInstrItem: ControlItemComponent
) {
super();
this.endX = stashItem!.x() + stashItem!.width();
this.controlInstrAnimation = new AnimatedTextbox(
controlInstrItem.text,
getNodePosition(controlInstrItem),
{ rectProps: { stroke: defaultActiveColor() } }
);
this.stashItemAnimation = new AnimatedTextbox(stashItem.text, getNodePosition(stashItem), {
rectProps: {
stroke: defaultDangerColor()
}
});
// call instr above
this.currCallInstrAnimation = new AnimatedTextbox(
this.currCallInstrItem.text,
getNodePosition(this.currCallInstrItem),
{ rectProps: { stroke: defaultActiveColor() } }
);
this.resultAnimations = resultItems.map(item => {
return new AnimatedTextbox(item.text, {
...getNodePosition(item),
opacity: 0
});
});
if (stashItem.arrow) {
this.arrowAnimation = new AnimatedGenericArrow(stashItem.arrow, { opacity: 0 });
}
}
draw(): React.ReactNode {
return (
<Group ref={this.ref} key={Animatable.key--}>
{this.controlInstrAnimation.draw()}
{this.stashItemAnimation.draw()}
{this.currCallInstrAnimation.draw()}
{this.resultAnimations.map(a => a.draw())}
{this.arrowAnimation?.draw()}
</Group>
);
}
async animate(animationConfig?: AnimationConfig) {
this.resultItems?.map(a => a.ref.current?.hide());
this.resultItems?.map(a => a.arrow?.ref.current?.hide());
const minInstrWidth =
getTextWidth(this.controlInstrItem.text) + ControlStashConfig.ControlItemTextPadding * 2;
const resultX = (idx: number) => this.resultItems[idx]?.x() ?? this.stashItem.x();
const resultY = this.resultItems[0]?.y() ?? this.stashItem.y();
const startX = resultX(0);
const fadeDuration = ((animationConfig?.duration ?? 1) * 3) / 4;
const fadeInDelay = (animationConfig?.delay ?? 0) + (animationConfig?.duration ?? 1) / 4;
// Move spread instruction next to stash item (array pointer)
await Promise.all([
...this.resultAnimations.flatMap(a => [
a.animateTo(
{ x: startX + (this.endX - startX) / 2 - this.resultItems[0]?.width() / 2 },
{ duration: 0 }
)
]),
this.controlInstrAnimation.animateRectTo({ stroke: defaultStrokeColor() }, animationConfig),
this.controlInstrAnimation.animateTo(
{
x: startX,
y: resultY + (this.resultItems[0]?.height() ?? this.stashItem.height()),
width: minInstrWidth
},
animationConfig
),
this.stashItemAnimation.animateRectTo({ stroke: defaultDangerColor() }, animationConfig)
]);
animationConfig = { ...animationConfig, delay: 0 };
// Merge all elements together to form the result
await Promise.all([
this.controlInstrAnimation.animateTo({ x: resultX(0), y: resultY }, animationConfig),
this.controlInstrAnimation.animateTo(
{ opacity: 0 },
{ ...animationConfig, duration: fadeDuration }
),
this.stashItemAnimation.animateTo({ x: resultX(0) }, animationConfig),
this.stashItemAnimation.animateTo(
{ opacity: 0 },
{ ...animationConfig, duration: fadeDuration }
),
...this.resultAnimations.flatMap((a, idx) => [
a.animateTo({ x: resultX(idx) }, animationConfig),
a.animateRectTo({ stroke: defaultDangerColor() }, animationConfig),
a.animateTo(
{ opacity: 1 },
{ ...animationConfig, duration: fadeDuration, delay: fadeInDelay }
)
])
]);
this.destroy();
}
destroy() {
this.ref.current?.hide();
this.resultItems.map(a => a.ref.current?.show());
this.resultItems.map(a => a.arrow?.ref.current?.show());
this.controlInstrAnimation.destroy();
this.stashItemAnimation.destroy();
this.resultAnimations.map(a => a.destroy());
this.arrowAnimation?.destroy();
}
}