-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigBar.tsx
243 lines (232 loc) · 7.68 KB
/
ConfigBar.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
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
import AnimationControl from './AnimationControl';
import { Graph } from './Graph';
import { parseSolution, Solution } from './Solution';
import { Divider, Stack, Button } from '@mui/material';
import { MuiFileInput } from "mui-file-input";
import React, { useEffect } from 'react';
import ClearIcon from '@mui/icons-material/Clear';
import FileDownloadOutlinedIcon from '@mui/icons-material/FileDownloadOutlined';
import Tooltip from '@mui/material/Tooltip';
interface ConfigBarProps {
graph: Graph | null;
onGraphChange: (graph: Graph | null) => void;
onSolutionChange: (solution: Solution | null) => void;
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 ConfigBar({
graph,
onGraphChange,
onSolutionChange,
playAnimation,
onPlayAnimationChange,
onSkipBackward,
onSkipForward,
onRestart,
stepSize,
onStepSizeChange,
loopAnimation,
onLoopAnimationChange,
onFitView,
showAgentId,
onShowAgentIdChange,
tracePaths,
onTracePathsChange,
canScreenshot,
takeScreenshot,
showCellId,
setShowCellId,
showGoals,
setShowGoals,
showGoalVectors,
setShowGoalVectors,
}: ConfigBarProps) {
const repoName = "JustinShetty/mapf-visualizer";
const [mapFile, setMapFile] = React.useState<File | null>(null);
const [mapError, setMapError] = React.useState<string | null>(null);
const [solutionFile, setSolutionFile] = React.useState<File | null>(null);
const [solutionError, setSolutionError] = React.useState<string | null>(null);
const blurActiveElement = () => {
// Blur (remove focus from) the file input
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
}
const handleLoadDemo = (mapName: string) => {
fetch(`${import.meta.env.BASE_URL}/${mapName}.map`)
.then((response) => response.text())
.then((text) => {
handleMapChange(new File([text], `${mapName}.map`));
return fetch(`${import.meta.env.BASE_URL}/demo_${mapName}.txt`);
})
.then((response) => response.text())
.then((text) => {
handleSolutionChange(new File([text], `demo_${mapName}.txt`));
});
};
useEffect(() => {
if (mapFile === null) {
onGraphChange(null);
return;
}
mapFile.text().then((text) => {
try {
onGraphChange(new Graph(text));
setMapError(null);
} catch (e) {
setMapFile(null);
onGraphChange(null);
setMapError(e instanceof Error ? e.message : "An unexpected error occurred");
}
});
}, [mapFile, onGraphChange]);
useEffect(() => {
if (solutionFile === null) {
onSolutionChange(null);
return
}
solutionFile.text().then((text) => {
try {
if (graph === null) throw new Error("Map must be loaded before solution");
const soln = parseSolution(text);
soln.forEach((config) => {
config.forEach((pose) => {
if (pose.position.x > graph.width || pose.position.y > graph.height) {
throw new Error(`Invalid solution: position ${pose.position} is out of bounds`);
}
});
});
onSolutionChange(soln);
setSolutionError(null);
} catch (e) {
setSolutionFile(null);
setSolutionError(e instanceof Error ? e.message : "An unexpected error occurred");
}
});
}, [graph, solutionFile, onSolutionChange]);
const handleMapChange = (newValue: File | null) => {
setMapFile(newValue);
setSolutionFile(null);
blurActiveElement();
};
const handleSolutionChange = (newValue: File | null) => {
setSolutionFile(newValue);
blurActiveElement();
};
const downloadFile = (file: File) => {
const url = URL.createObjectURL(file);
const a = document.createElement('a');
a.href = url;
a.download = file.name;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
return (
<Stack direction="column" spacing={1} sx={{padding: 2}} >
<Stack direction="column" spacing={1}>
<Button variant="outlined" onClick={() => handleLoadDemo("2x2")}>Load 2x2 demo</Button>
<Button variant="outlined" onClick={() => handleLoadDemo("random-32-32-20")}>Load 32x32 demo</Button>
</Stack>
<Divider />
<Stack direction="column" spacing={1}>
<h1>Map</h1>
<Stack direction="row" spacing={1}>
<MuiFileInput
value={mapFile}
onChange={handleMapChange}
placeholder="Select a map file"
sx={{width: '100%'}}
clearIconButtonProps={{
title: "Clear",
children: <ClearIcon fontSize="small" />
}}
/>
{mapFile &&
<Tooltip title={`Download ${mapFile?.name}`}>
<Button onClick={() => {downloadFile(mapFile as File)}}>
<FileDownloadOutlinedIcon />
</Button>
</Tooltip>
}
</Stack>
{mapError && <p style={{color: 'red'}}>{mapError}</p>}
</Stack>
<Divider />
<Stack direction="column" spacing={2}>
<h1>Solution</h1>
<Stack direction="row" spacing={1}>
<MuiFileInput
value={solutionFile}
onChange={handleSolutionChange}
placeholder="Select a solution file"
sx={{width: '100%'}}
clearIconButtonProps={{
title: "Clear",
children: <ClearIcon fontSize="small" />
}}
/>
{solutionFile &&
<Tooltip title={`Download ${solutionFile?.name}`}>
<Button onClick={() => {downloadFile(solutionFile as File)}}>
<FileDownloadOutlinedIcon />
</Button>
</Tooltip>
}
</Stack>
{solutionError && <p style={{color: 'red'}}>{solutionError}</p>}
</Stack>
<Divider />
<AnimationControl
playAnimation={playAnimation}
onPlayAnimationChange={onPlayAnimationChange}
onSkipBackward={onSkipBackward}
onSkipForward={onSkipForward}
onRestart={onRestart}
stepSize={stepSize}
onStepSizeChange={onStepSizeChange}
loopAnimation={loopAnimation}
onLoopAnimationChange={onLoopAnimationChange}
onFitView={onFitView}
showAgentId={showAgentId}
onShowAgentIdChange={onShowAgentIdChange}
tracePaths={tracePaths}
onTracePathsChange={onTracePathsChange}
canScreenshot={canScreenshot}
takeScreenshot={takeScreenshot}
showCellId={showCellId}
setShowCellId={setShowCellId}
showGoals={showGoals}
setShowGoals={setShowGoals}
showGoalVectors={showGoalVectors}
setShowGoalVectors={setShowGoalVectors}
/>
<Divider />
<a target="_blank" href={`https://github.com/${repoName}`} style={{ color: 'white', width: 'fit-content' }}>
{repoName}
</a>
</Stack>
);
}
export default ConfigBar;