-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAnimationControl.tsx
More file actions
272 lines (263 loc) · 11.6 KB
/
Copy pathAnimationControl.tsx
File metadata and controls
272 lines (263 loc) · 11.6 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import SkipPreviousIcon from '@mui/icons-material/SkipPrevious';
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
import PauseTwoToneIcon from '@mui/icons-material/PauseTwoTone';
import SkipNextIcon from '@mui/icons-material/SkipNext';
import Button from '@mui/material/Button';
import ButtonGroup from '@mui/material/ButtonGroup';
import Box from '@mui/material/Box';
import RestartAltIcon from '@mui/icons-material/RestartAlt';
import Slider from '@mui/material/Slider';
import RepeatIcon from '@mui/icons-material/Repeat';
import RepeatOnIcon from '@mui/icons-material/RepeatOn';
import Stack from '@mui/material/Stack';
import Tooltip from '@mui/material/Tooltip';
import LooksOneIcon from '@mui/icons-material/LooksOne';
import LooksOneOutlinedIcon from '@mui/icons-material/LooksOneOutlined';
import { useEffect } from 'react';
import DirectionsIcon from '@mui/icons-material/Directions';
import DirectionsOutlinedIcon from '@mui/icons-material/DirectionsOutlined';
import FilterCenterFocusOutlinedIcon from '@mui/icons-material/FilterCenterFocusOutlined';
import ScreenshotMonitorOutlinedIcon from '@mui/icons-material/ScreenshotMonitorOutlined';
import StartIcon from '@mui/icons-material/Start';
import SmartToyOutlinedIcon from '@mui/icons-material/SmartToyOutlined';
import SmartToyIcon from '@mui/icons-material/SmartToy';
import FlagIcon from '@mui/icons-material/Flag';
import OutlinedFlagIcon from '@mui/icons-material/OutlinedFlag';
import PolylineIcon from '@mui/icons-material/Polyline';
import PolylineOutlinedIcon from '@mui/icons-material/PolylineOutlined';
const STEP_SIZE_INCREMENT = 0.2;
const STEP_SIZE_MAX = 10;
const STEP_SIZE_MIN = 0.2;
const STEP_BACKWARD_KEY = 'ArrowLeft';
const PLAY_PAUSE_KEY = ' ';
const STEP_FORWARD_KEY = 'ArrowRight';
const RESTART_KEY = 'r';
const LOOP_KEY = 'l';
const FIT_VIEW_KEY = 'f';
const SHOW_AGENT_ID_KEY = 'a';
const STEP_SIZE_UP_KEY = 'ArrowUp';
const STEP_SIZE_DOWN_KEY = 'ArrowDown';
const TRACE_PATHS_KEY = 'p';
const SCREENSHOT_KEY = 's';
const SHOW_CELL_ID_KEY = 'c';
const SHOW_GOALS_KEY = 'g';
const SHOW_GOAL_VECTORS_KEY = 'v';
interface AnimationControlProps {
playAnimation: boolean;
onPlayAnimationChange: (playAnimation: boolean) => void;
onSkipBackward: () => void;
onSkipForward: () => void;
onRestart: () => void;
stepSize: number;
onStepSizeChange: (speed: number) => void;
loopAnimation: boolean,
onLoopAnimationChange: (loopAnimation: boolean) => void;
onFitView: () => void;
showAgentId: boolean;
onShowAgentIdChange: (showAgentId: boolean) => void;
tracePaths: boolean;
onTracePathsChange: (tracePaths: boolean) => void;
canScreenshot: boolean;
takeScreenshot: () => void;
showCellId: boolean;
setShowCellId: (showCellId: boolean) => void;
showGoals: boolean;
setShowGoals: (showGoals: boolean) => void;
showGoalVectors: boolean;
setShowGoalVectors: (showGoalVectors: boolean) => void;
}
function AnimationControl({
playAnimation,
onPlayAnimationChange,
onSkipBackward,
onSkipForward,
onRestart,
stepSize,
onStepSizeChange,
loopAnimation,
onLoopAnimationChange,
onFitView,
showAgentId,
onShowAgentIdChange,
tracePaths,
onTracePathsChange,
canScreenshot,
takeScreenshot,
showCellId,
setShowCellId,
showGoals,
setShowGoals,
showGoalVectors,
setShowGoalVectors,
}: AnimationControlProps) {
const roundAndSetStepSize = (value: number) => {
onStepSizeChange(Number(value.toFixed(1)));
}
const handleSliderChange = (event: Event, value: number | number[]) => {
event.preventDefault();
if (typeof value === 'number') roundAndSetStepSize(value);
};
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (!event.ctrlKey && !event.altKey && !event.metaKey) {
event.preventDefault();
}
if (event.key === STEP_BACKWARD_KEY) {
onSkipBackward();
} else if (event.key === PLAY_PAUSE_KEY) {
onPlayAnimationChange(!playAnimation);
} else if (event.key === STEP_FORWARD_KEY) {
onSkipForward();
} else if (event.key === RESTART_KEY) {
onRestart();
} else if (event.key === LOOP_KEY) {
onLoopAnimationChange(!loopAnimation);
} else if (event.key === FIT_VIEW_KEY) {
onFitView();
} else if (event.key === SHOW_AGENT_ID_KEY) {
onShowAgentIdChange(!showAgentId);
} else if (event.key === STEP_SIZE_UP_KEY && stepSize + STEP_SIZE_INCREMENT <= STEP_SIZE_MAX) {
roundAndSetStepSize(stepSize + STEP_SIZE_INCREMENT);
} else if (event.key === STEP_SIZE_DOWN_KEY && stepSize - STEP_SIZE_INCREMENT >= STEP_SIZE_MIN) {
roundAndSetStepSize(stepSize - STEP_SIZE_INCREMENT);
} else if (event.key === TRACE_PATHS_KEY) {
onTracePathsChange(!tracePaths);
} else if (event.key === SCREENSHOT_KEY) {
takeScreenshot();
} else if (event.key === SHOW_CELL_ID_KEY) {
setShowCellId(!showCellId);
} else if (event.key === SHOW_GOALS_KEY) {
setShowGoals(!showGoals);
} else if (event.key === SHOW_GOAL_VECTORS_KEY) {
setShowGoalVectors(!showGoalVectors);
}
};
window.addEventListener('keydown', handleKeyDown);
return () => {
window.removeEventListener('keydown', handleKeyDown);
};
}, [playAnimation, onPlayAnimationChange, loopAnimation, onFitView,
onLoopAnimationChange, onRestart, onShowAgentIdChange, onSkipBackward,
onSkipForward, onStepSizeChange, showAgentId, stepSize, onTracePathsChange, tracePaths,
takeScreenshot, showCellId, setShowCellId, showGoals, setShowGoals, showGoalVectors,
setShowGoalVectors]);
return (
<Stack direction="column" spacing={1}>
<Stack direction="row" spacing={2} justifyContent="center">
<Tooltip
title={
<div style={{ textAlign: 'center' }}>
Adjust animation step size
({STEP_SIZE_UP_KEY}/{STEP_SIZE_DOWN_KEY})
</div>
}
>
<Slider
value={stepSize}
step={STEP_SIZE_INCREMENT}
marks
min={STEP_SIZE_MIN}
max={STEP_SIZE_MAX}
valueLabelDisplay="auto"
onChange={handleSliderChange}
sx={{ width: '40%', height: "auto"}}
/>
</Tooltip>
<Tooltip title="Reset step size">
<Button onClick={() => roundAndSetStepSize(1)}>
<RestartAltIcon />
</Button>
</Tooltip>
</Stack>
<Box display="flex" justifyContent="center">
<ButtonGroup size="large" variant="outlined">
<Tooltip title={`Backward one step (${STEP_BACKWARD_KEY})`}>
<Button onClick={onSkipBackward}>
<SkipPreviousIcon />
</Button>
</Tooltip>
<Tooltip title={(playAnimation ? "Pause" : "Play") + ` (spacebar)`}>
<Button onClick={() => onPlayAnimationChange(!playAnimation)}>
{playAnimation ?
<PauseTwoToneIcon /> :
<PlayArrowIcon />}
</Button>
</Tooltip>
<Tooltip title={`Forward one step (${STEP_FORWARD_KEY})`}>
<Button onClick={onSkipForward}>
<SkipNextIcon />
</Button>
</Tooltip>
</ButtonGroup>
</Box>
<Box display="flex" justifyContent="center">
<ButtonGroup size="large" variant="outlined">
<Tooltip title={`Restart animation (${RESTART_KEY})`}>
<Button onClick={onRestart}>
<StartIcon />
</Button>
</Tooltip>
<Tooltip title={(loopAnimation ? "Disable loop" : "Enable loop") + ` (${LOOP_KEY})`}>
<Button onClick={() => onLoopAnimationChange(!loopAnimation)}>
{loopAnimation ?
<RepeatOnIcon /> :
<RepeatIcon />}
</Button>
</Tooltip>
<Tooltip title={`Reset view (${FIT_VIEW_KEY})`}>
<Button onClick={onFitView}>
<FilterCenterFocusOutlinedIcon />
</Button>
</Tooltip>
<Tooltip title={"Take screenshot" + ` (${SCREENSHOT_KEY})`}>
<span>
<Button disabled={!canScreenshot} onClick={takeScreenshot}>
<ScreenshotMonitorOutlinedIcon />
</Button>
</span>
</Tooltip>
</ButtonGroup>
</Box>
<Box display="flex" justifyContent="center">
<ButtonGroup size="large" variant="outlined">
<Tooltip title={(showAgentId ? "Hide agent ID" : "Show agent ID") + ` (${SHOW_AGENT_ID_KEY})`}>
<Button onClick={() => onShowAgentIdChange(!showAgentId)}>
{showAgentId ?
<SmartToyIcon />:
<SmartToyOutlinedIcon />}
</Button>
</Tooltip>
<Tooltip title={(showCellId ? "Hide cell ID" : "Show cell ID") + ` (${SHOW_CELL_ID_KEY})`}>
<Button onClick={() => setShowCellId(!showCellId)}>
{showCellId ?
<LooksOneIcon />:
<LooksOneOutlinedIcon />}
</Button>
</Tooltip>
<Tooltip title={(tracePaths ? "Hide paths" : "Show paths") + ` (${TRACE_PATHS_KEY})`}>
<Button onClick={() => onTracePathsChange(!tracePaths)}>
{tracePaths ?
<DirectionsIcon />:
<DirectionsOutlinedIcon />}
</Button>
</Tooltip>
<Tooltip title={(showGoals ? "Hide goals" : "Show goals") + ` (${SHOW_GOALS_KEY})`}>
<Button onClick={() => setShowGoals(!showGoals)}>
{showGoals ?
<FlagIcon />:
<OutlinedFlagIcon />}
</Button>
</Tooltip>
<Tooltip title={(showGoalVectors ? "Hide goal vectors" : "Show goal vectors") + ` (${SHOW_GOAL_VECTORS_KEY})`}>
<Button onClick={() => setShowGoalVectors(!showGoalVectors)}>
{showGoalVectors ?
<PolylineIcon />:
<PolylineOutlinedIcon />}
</Button>
</Tooltip>
</ButtonGroup>
</Box>
</Stack>
);
}
export default AnimationControl;