forked from Pedro-Pathing/Visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.svelte
More file actions
941 lines (822 loc) · 28.2 KB
/
App.svelte
File metadata and controls
941 lines (822 loc) · 28.2 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
<script lang="ts">
import * as d3 from "d3";
import { onMount, onDestroy } from "svelte";
import Two from "two.js";
import type { Path } from "two.js/src/path";
import type { Line as PathLine } from "two.js/src/shapes/line";
import ControlTab from "./lib/ControlTab.svelte";
import Navbar from "./lib/Navbar.svelte";
import MathTools from "./lib/MathTools.svelte";
import _ from "lodash";
import {
easeInOutQuad,
getCurvePoint,
getMousePos,
getRandomColor,
quadraticToCubic,
radiansToDegrees,
shortestRotation,
} from "./utils";
import hotkeys from 'hotkeys-js';
import { save, open } from '@tauri-apps/plugin-dialog';
import { writeTextFile, readTextFile, readFile } from '@tauri-apps/plugin-fs';
let two: Two;
let twoElement: HTMLDivElement;
let resizeTrigger = 0; // Used to trigger reactive updates on window resize
let pointRadius = 1.15;
let lineWidth = 0.57;
let robotWidth = 16;
let robotHeight = 16;
let settings: FPASettings = {
xVelocity: 60,
yVelocity: 60,
aVelocity: Math.PI,
kFriction: 0.05,
rWidth: robotWidth,
rHeight: robotHeight
};
let x
let percent: number = 0;
// Initialize default shapes - start with no obstacles
let shapes: Shape[] = [];
/**
* Converter for X axis from inches to pixels.
* Updates when twoElement dimensions or resizeTrigger changes.
*/
$: {
void resizeTrigger; // Force reactive update when resizeTrigger changes
x = d3
.scaleLinear()
.domain([0, 144])
.range([0, twoElement?.clientWidth ?? 144]);
}
/**
* Converter for Y axis from inches to pixels.
* Updates when twoElement dimensions or resizeTrigger changes.
*/
$: {
void resizeTrigger; // Force reactive update when resizeTrigger changes
y = d3
.scaleLinear()
.domain([0, 144])
.range([twoElement?.clientHeight ?? 144, 0]);
}
let lineGroup = new Two.Group();
lineGroup.id = "line-group";
let pointGroup = new Two.Group();
pointGroup.id = "point-group";
let shapeGroup = new Two.Group();
shapeGroup.id = "shape-group";
let startPoint: Point = {
x: 56,
y: 8,
heading: "linear",
startDeg: 90,
endDeg: 180
};
let lines: Line[] = [
{
name: "Path 1",
endPoint: { x: 56, y: 36, heading: "linear", startDeg: 90, endDeg: 180 },
controlPoints: [],
color: getRandomColor(),
},
];
$: points = (() => {
let _points = [];
let startPointElem = new Two.Circle(
x(startPoint.x),
y(startPoint.y),
x(pointRadius)
);
startPointElem.id = `point-0-0`;
startPointElem.fill = lines[0].color;
startPointElem.noStroke();
_points.push(startPointElem);
lines.forEach((line, idx) => {
[line.endPoint, ...line.controlPoints].forEach((point, idx1) => {
if (idx1 > 0) {
let pointGroup = new Two.Group();
pointGroup.id = `point-${idx + 1}-${idx1}`;
let pointElem = new Two.Circle(
x(point.x),
y(point.y),
x(pointRadius)
);
pointElem.id = `point-${idx + 1}-${idx1}-background`;
pointElem.fill = line.color;
pointElem.noStroke();
let pointText = new Two.Text(
`${idx1}`,
x(point.x),
y(point.y - 0.15),
x(pointRadius)
);
pointText.id = `point-${idx + 1}-${idx1}-text`;
pointText.size = x(1.55);
pointText.leading = 1;
pointText.family = "ui-sans-serif, system-ui, sans-serif";
pointText.alignment = "center";
pointText.baseline = "middle";
pointText.fill = "white";
pointText.noStroke();
pointGroup.add(pointElem, pointText);
_points.push(pointGroup);
} else {
let pointElem = new Two.Circle(
x(point.x),
y(point.y),
x(pointRadius)
);
pointElem.id = `point-${idx + 1}-${idx1}`;
pointElem.fill = line.color;
pointElem.noStroke();
_points.push(pointElem);
}
});
});
// Add obstacle vertices as draggable points
shapes.forEach((shape, shapeIdx) => {
shape.vertices.forEach((vertex, vertexIdx) => {
let pointGroup = new Two.Group();
pointGroup.id = `obstacle-${shapeIdx}-${vertexIdx}`;
let pointElem = new Two.Circle(
x(vertex.x),
y(vertex.y),
x(pointRadius)
);
pointElem.id = `obstacle-${shapeIdx}-${vertexIdx}-background`;
pointElem.fill = "#991b1b"; // Match obstacle color
pointElem.noStroke();
let pointText = new Two.Text(
`${vertexIdx + 1}`,
x(vertex.x),
y(vertex.y - 0.15),
x(pointRadius)
);
pointText.id = `obstacle-${shapeIdx}-${vertexIdx}-text`;
pointText.size = x(1.55);
pointText.leading = 1;
pointText.family = "ui-sans-serif, system-ui, sans-serif";
pointText.alignment = "center";
pointText.baseline = "middle";
pointText.fill = "white";
pointText.noStroke();
pointGroup.add(pointElem, pointText);
_points.push(pointGroup);
});
});
return _points;
})();
$: path = (() => {
let _path: (Path | PathLine)[] = [];
lines.forEach((line, idx) => {
let _startPoint = idx === 0 ? startPoint : lines[idx - 1].endPoint;
let lineElem: Path | PathLine;
if (line.controlPoints.length > 2) {
// Approximate an n-degree bezier curve by sampling it at 100 points
const samples = 100;
const cps = [_startPoint, ...line.controlPoints, line.endPoint];
let points = [new Two.Anchor(x(_startPoint.x), y(_startPoint.y), 0, 0, 0, 0, Two.Commands.move)];
for (let i = 1; i <= samples; ++i) {
const point = getCurvePoint(i / samples, cps);
points.push(new Two.Anchor(x(point.x), y(point.y), 0, 0, 0, 0, Two.Commands.line));
}
points.forEach((point) => (point.relative = false));
lineElem = new Two.Path(points);
lineElem.automatic = false;
} else if (line.controlPoints.length > 0) {
let cp1 = line.controlPoints[1]
? line.controlPoints[0]
: quadraticToCubic(_startPoint, line.controlPoints[0], line.endPoint)
.Q1;
let cp2 =
line.controlPoints[1] ??
quadraticToCubic(_startPoint, line.controlPoints[0], line.endPoint)
.Q2;
let points = [
new Two.Anchor(
x(_startPoint.x),
y(_startPoint.y),
x(_startPoint.x),
y(_startPoint.y),
x(cp1.x),
y(cp1.y),
Two.Commands.move
),
new Two.Anchor(
x(line.endPoint.x),
y(line.endPoint.y),
x(cp2.x),
y(cp2.y),
x(line.endPoint.x),
y(line.endPoint.y),
Two.Commands.curve
),
];
points.forEach((point) => (point.relative = false));
lineElem = new Two.Path(points);
lineElem.automatic = false;
} else {
lineElem = new Two.Line(
x(_startPoint.x),
y(_startPoint.y),
x(line.endPoint.x),
y(line.endPoint.y)
);
}
lineElem.id = `line-${idx + 1}`;
lineElem.stroke = line.color;
lineElem.linewidth = x(lineWidth);
lineElem.noFill();
_path.push(lineElem);
});
return _path;
})();
$: shapeElements = (() => {
let _shapes: Path[] = [];
shapes.forEach((shape, idx) => {
if (shape.vertices.length >= 3) {
// Create polygon from vertices - properly format for Two.js
let vertices = [];
// Start with move command for first vertex
vertices.push(new Two.Anchor(
x(shape.vertices[0].x),
y(shape.vertices[0].y),
0, 0, 0, 0,
Two.Commands.move
));
// Add line commands for remaining vertices
for (let i = 1; i < shape.vertices.length; i++) {
vertices.push(new Two.Anchor(
x(shape.vertices[i].x),
y(shape.vertices[i].y),
0, 0, 0, 0,
Two.Commands.line
));
}
// Close the shape
vertices.push(new Two.Anchor(
x(shape.vertices[0].x),
y(shape.vertices[0].y),
0, 0, 0, 0,
Two.Commands.close
));
vertices.forEach((point) => (point.relative = false));
let shapeElement = new Two.Path(vertices);
shapeElement.id = `shape-${idx}`;
shapeElement.stroke = "#991b1b"; // Deeper red border (red-800)
shapeElement.fill = "#ef4444"; // Deeper red fill (red-500)
shapeElement.opacity = 0.4; // Make it pretty transparent
shapeElement.linewidth = x(0.8); // Make border more visible
shapeElement.automatic = false;
_shapes.push(shapeElement);
}
});
return _shapes;
})();
let robotXY: BasePoint = { x: 0, y: 0 };
let robotHeading: number = 0;
$: {
let totalLineProgress = (lines.length * Math.min(percent, 99.999999999)) / 100;
let currentLineIdx = Math.min(Math.trunc(totalLineProgress), lines.length - 1);
let currentLine = lines[currentLineIdx];
let linePercent = easeInOutQuad(totalLineProgress - Math.floor(totalLineProgress));
let _startPoint = currentLineIdx === 0 ? startPoint : lines[currentLineIdx - 1].endPoint;
let robotInchesXY = getCurvePoint(linePercent, [_startPoint, ...currentLine.controlPoints, currentLine.endPoint]);
robotXY = { x: x(robotInchesXY.x), y: y(robotInchesXY.y) };
switch (currentLine.endPoint.heading) {
case "linear":
robotHeading = -shortestRotation(
currentLine.endPoint.startDeg,
currentLine.endPoint.endDeg,
linePercent
);
break;
case "constant":
robotHeading = -currentLine.endPoint.degrees;
break;
case "tangential":
const nextPointInches = getCurvePoint(
linePercent + (currentLine.endPoint.reverse ? -0.01 : 0.01),
[_startPoint, ...currentLine.controlPoints, currentLine.endPoint]
);
const nextPoint = { x: x(nextPointInches.x), y: y(nextPointInches.y) };
const dx = nextPoint.x - robotXY.x;
const dy = nextPoint.y - robotXY.y;
if (dx !== 0 || dy !== 0) {
const angle = Math.atan2(dy, dx);
robotHeading = radiansToDegrees(angle);
}
break;
}
}
$: (() => {
if (!two) {
return;
}
two.renderer.domElement.style["z-index"] = "30";
two.renderer.domElement.style["position"] = "absolute";
two.renderer.domElement.style["top"] = "0px";
two.renderer.domElement.style["left"] = "0px";
two.renderer.domElement.style["width"] = "100%";
two.renderer.domElement.style["height"] = "100%";
two.clear();
two.add(...shapeElements);
two.add(...path);
two.add(...points);
two.update();
})();
let playing = false;
let animationFrame: number;
let startTime: number | null = null;
let previousTime: number | null = null;
function animate(timestamp: number) {
if (!startTime) {
startTime = timestamp;
}
if (previousTime !== null) {
const deltaTime = timestamp - previousTime;
if (percent >= 100) {
percent = 0;
} else {
percent += (0.65 / lines.length) * (deltaTime * 0.1);
}
}
previousTime = timestamp;
if (playing) {
requestAnimationFrame(animate);
}
}
function play() {
if (!playing) {
playing = true;
startTime = null;
previousTime = null;
animationFrame = requestAnimationFrame(animate);
}
}
function pause() {
playing = false;
cancelAnimationFrame(animationFrame);
}
async function fpa(l: FPALine, s: FPASettings, o: Shape): Promise<Line> {
let status = 'Starting optimization...';
let result = null;
// Convert to arrays, not JSON strings - this was the main issue!
// If no obstacle vertices, create a small default obstacle outside the field
const obstacle = o.vertices.length >= 3 ?
o.vertices.map(p => [p.x, p.y]) :
[[-10, -10], [-10, -5], [-5, -5], [-5, -10]]; // Small rectangle outside field
const inputWaypoints = [l.startPoint, ...l.controlPoints, l.endPoint].map(p => [p.x, p.y]);
// Extract heading degrees based on Point type
let startHeadingDeg = 0;
let endHeadingDeg = 0;
if (l.startPoint.heading === "linear") {
startHeadingDeg = l.startPoint.startDeg ?? 0;
} else if (l.startPoint.heading === "constant") {
startHeadingDeg = (l.startPoint as any).degrees ?? 0;
}
if (l.endPoint.heading === "linear") {
endHeadingDeg = l.endPoint.endDeg ?? 0;
} else if (l.endPoint.heading === "constant") {
endHeadingDeg = (l.endPoint as any).degrees ?? 0;
}
console.log('FPA Optimization Parameters:');
console.log('Waypoints:', inputWaypoints);
console.log('Obstacle:', obstacle);
console.log('Start heading:', startHeadingDeg);
console.log('End heading:', endHeadingDeg);
console.log('Settings:', s);
const payload = {
waypoints: inputWaypoints,
start_heading_degrees: startHeadingDeg,
end_heading_degrees: endHeadingDeg,
x_velocity: s.xVelocity,
y_velocity: s.yVelocity,
angular_velocity: s.aVelocity,
friction_coefficient: s.kFriction,
obstacle: obstacle,
robot_width: s.rWidth,
robot_height: s.rHeight,
min_coord_field: 0,
max_coord_field: 144,
interpolation: l.interpolation === "tangential" ? "tangent" : l.interpolation
};
try {
result = await runOptimization(payload);
status = 'Optimization Complete!';
} catch (e: any) {
status = 'Error: ' + e.message;
throw e;
}
// result is already parsed JSON data, no need to call .json()
const resultData = result;
// Handle the new API format that returns optimized_waypoints
let optimizedWaypoints;
if (resultData.optimized_waypoints) {
optimizedWaypoints = resultData.optimized_waypoints;
} else if (Array.isArray(resultData)) {
// Legacy format support
optimizedWaypoints = resultData;
} else {
throw new Error('Unexpected result format from optimization API');
}
// Handle the different Point types based on heading
let endPoint: Point;
if (l.interpolation === "tangential") {
endPoint = {
x: optimizedWaypoints[optimizedWaypoints.length - 1][0],
y: optimizedWaypoints[optimizedWaypoints.length - 1][1],
heading: "tangential",
reverse: l.endPoint.reverse ?? false
};
} else if (l.interpolation === "constant") {
endPoint = {
x: optimizedWaypoints[optimizedWaypoints.length - 1][0],
y: optimizedWaypoints[optimizedWaypoints.length - 1][1],
heading: "constant",
degrees: (l.endPoint as any).degrees ?? 0
};
} else {
// linear
endPoint = {
x: optimizedWaypoints[optimizedWaypoints.length - 1][0],
y: optimizedWaypoints[optimizedWaypoints.length - 1][1],
heading: "linear",
startDeg: l.endPoint.startDeg ?? 0,
endDeg: l.endPoint.endDeg ?? 0
};
}
return {
name: l.name,
endPoint,
color: l.color,
controlPoints: optimizedWaypoints.slice(1, optimizedWaypoints.length - 1).map((p: number[]) => ({ x: p[0], y: p[1] }))
}
/*return {
endPoint: { x: 36, y: 80, heading: "linear", startDeg: 0, endDeg: 0 },
controlPoints: [],
color: getRandomColor(),
}*/
}
function sleep(ms: number) {
return new Promise(res => setTimeout(res, ms));
}
export async function createTask(payload: any) {
try {
console.log('Creating optimization task with payload:', payload);
const response = await fetch('https://fpa.pedropathing.com/optimize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
console.log('Response status:', response.status);
// Handle offline response from service worker
if (response.status === 503) {
const errorData = await response.json();
if (errorData.error === 'offline') {
throw new Error('OFFLINE: ' + errorData.message);
}
}
if (!response.ok) {
const errorText = await response.text();
console.error('Server error response:', errorText);
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
console.log('Job created with ID:', data.job_id);
return data.job_id;
} catch (error) {
console.error('Failed to create optimization task:', error);
throw error;
}
}
export async function pollForResult(jobId: string, pollInterval = 1000, maxTries = 60) {
for (let i = 0; i < maxTries; i++) {
try {
const response = await fetch(`https://fpa.pedropathing.com/job/${jobId}`);
// Handle offline response from service worker
if (response.status === 503) {
const errorData = await response.json();
if (errorData.error === 'offline') {
console.log('OFFLINE: ' + errorData.message)
throw new Error('OFFLINE: ' + errorData.message);
}
}
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
const data = await response.json();
if (data.status === 'completed' && data.result) {
return data.result;
} else if (data.status === 'error') {
throw new Error('Optimization failed with error.');
}
await sleep(pollInterval);
} catch (error) {
console.error(`Polling attempt ${i + 1} failed:`, error);
if (i === maxTries - 1) throw error; // Re-throw on last attempt
await sleep(pollInterval);
}
}
console.log('Polling timed out after', maxTries, 'attempts.')
throw new Error('Timeout waiting for job result.');
}
// 3. Run Optimization - creates task, then polls for result and returns it
export async function runOptimization(payload: any, pollInterval = 1000, maxTries = 60) {
const jobId = await createTask(payload);
const result = await pollForResult(jobId, pollInterval, maxTries);
return result;
}
// Handle window resize to update canvas dimensions
const handleResize = () => {
if (two) {
two.fit();
resizeTrigger++; // Trigger reactive updates
}
};
onMount(() => {
two = new Two({
fitted: true,
type: Two.Types.svg,
}).appendTo(twoElement);
updateRobotImage();
let currentElem: string | null = null;
let isDown = false;
two.renderer.domElement.addEventListener("mousemove", (evt: MouseEvent) => {
const elem = document.elementFromPoint(evt.clientX, evt.clientY);
if (isDown && currentElem) {
const { x: xPos, y: yPos } = getMousePos(evt, two.renderer.domElement);
if (currentElem.startsWith("obstacle-")) {
// Handle obstacle vertex dragging
const parts = currentElem.split("-");
const shapeIdx = Number(parts[1]);
const vertexIdx = Number(parts[2]);
shapes[shapeIdx].vertices[vertexIdx].x = x.invert(xPos);
shapes[shapeIdx].vertices[vertexIdx].y = y.invert(yPos);
} else {
// Handle path point dragging
const line = Number(currentElem.split("-")[1]) - 1;
const point = Number(currentElem.split("-")[2]);
if (line === -1) {
startPoint.x = x.invert(xPos);
startPoint.y = y.invert(yPos);
} else {
if (point === 0) {
lines[line].endPoint.x = x.invert(xPos);
lines[line].endPoint.y = y.invert(yPos);
} else {
lines[line].controlPoints[point - 1].x = x.invert(xPos);
lines[line].controlPoints[point - 1].y = y.invert(yPos);
}
}
}
} else {
if (elem?.id.startsWith("point") || elem?.id.startsWith("obstacle")) {
two.renderer.domElement.style.cursor = "pointer";
currentElem = elem.id;
} else {
two.renderer.domElement.style.cursor = "auto";
currentElem = null;
}
}
});
two.renderer.domElement.addEventListener("mousedown", () => {
isDown = true;
});
two.renderer.domElement.addEventListener("mouseup", () => {
isDown = false;
});
window.addEventListener("resize", handleResize);
});
onDestroy(() => {
window.removeEventListener("resize", handleResize);
});
document.addEventListener("keydown", function (evt) {
if (evt.code === "Space" && document.activeElement === document.body) {
if (playing) {
pause();
} else {
play();
}
}
});
async function saveFile() {
try {
const jsonString = JSON.stringify({ startPoint, lines, shapes }, null, 2);
// Use Tauri's save dialog
const filePath = await save({
filters: [{
name: 'Pedro Pathing',
extensions: ['pp']
}],
defaultPath: 'trajectory.pp'
});
if (filePath) {
await writeTextFile(filePath, jsonString);
console.log('File saved successfully to:', filePath);
}
} catch (error) {
console.error('Error saving file:', error);
// Fallback to browser download if Tauri APIs are not available
const jsonString = JSON.stringify({ startPoint, lines, shapes });
const blob = new Blob([jsonString], { type: "application/json" });
const linkObj = document.createElement("a");
const url = URL.createObjectURL(blob);
linkObj.href = url;
linkObj.download = "trajectory.pp";
document.body.appendChild(linkObj);
linkObj.click();
document.body.removeChild(linkObj);
URL.revokeObjectURL(url);
}
}
async function loadFile(evt?: Event) {
try {
// Use Tauri's open dialog
const selected = await open({
filters: [{
name: 'Pedro Pathing',
extensions: ['pp', 'json']
}],
multiple: false
});
if (selected && typeof selected === 'string') {
const contents = await readTextFile(selected);
const jsonObj: {
startPoint: Point;
lines: Line[];
shapes?: Shape[];
} = JSON.parse(contents);
startPoint = jsonObj.startPoint;
lines = jsonObj.lines;
if (jsonObj.shapes) {
shapes = jsonObj.shapes;
}
console.log('File loaded successfully from:', selected);
}
} catch (error) {
console.error('Error loading file:', error);
// Fallback to file input if Tauri APIs are not available or if called from input
if (evt) {
const elem = evt.target as HTMLInputElement;
const file = elem.files?.[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e: ProgressEvent<FileReader>) {
try {
const result = e.target?.result as string;
const jsonObj: {
startPoint: Point;
lines: Line[];
shapes?: Shape[];
} = JSON.parse(result);
startPoint = jsonObj.startPoint;
lines = jsonObj.lines;
if (jsonObj.shapes) {
shapes = jsonObj.shapes;
}
} catch (err) {
console.error(err);
}
};
reader.readAsText(file);
}
}
}
}
async function loadRobot(evt?: Event) {
try {
// Use Tauri's open dialog
const selected = await open({
filters: [{
name: 'PNG Image',
extensions: ['png']
}],
multiple: false
});
if (selected && typeof selected === 'string') {
const fileData = await readFile(selected);
// Convert to base64 data URL
const base64 = btoa(
new Uint8Array(fileData).reduce((data, byte) => data + String.fromCharCode(byte), '')
);
const dataUrl = `data:image/png;base64,${base64}`;
localStorage.setItem('robot.png', dataUrl);
updateRobotImage();
console.log('Robot image loaded successfully from:', selected);
}
} catch (error) {
console.error('Error loading robot image:', error);
// Fallback to file input if Tauri APIs are not available or if called from input
if (evt) {
const elem = evt.target as HTMLInputElement;
const file = elem.files?.[0];
if (file && file.type === "image/png") {
const reader = new FileReader();
reader.onload = function (e: ProgressEvent<FileReader>) {
const result = e.target?.result as string;
localStorage.setItem('robot.png', result);
updateRobotImage();
};
reader.readAsDataURL(file);
} else {
console.error("Invalid file type. Please upload a PNG file.");
}
}
}
}
function updateRobotImage() {
const robotImage = document.querySelector('img[alt="Robot"]') as HTMLImageElement;
const storedImage = localStorage.getItem('robot.png');
if (robotImage && storedImage) {
robotImage.src = storedImage;
}
}
function addNewLine() {
lines = [
...lines,
{
endPoint: {
x: _.random(36, 108),
y: _.random(36, 108),
heading: "tangential",
reverse: true,
// Remove startDeg/endDeg for tangential type if not needed by your Point type
} as Point,
controlPoints: [],
color: getRandomColor(),
},
];
}
function addControlPoint() {
if (lines.length > 0) {
const lastLine = lines[lines.length - 1];
lastLine.controlPoints.push({
x: _.random(36, 108),
y: _.random(36, 108),
});
}
}
function removeControlPoint() {
if (lines.length > 0) {
const lastLine = lines[lines.length - 1];
if (lastLine.controlPoints.length > 0) {
lastLine.controlPoints.pop();
}
}
}
hotkeys('w', function(event, handler){
event.preventDefault();
addNewLine();
});
hotkeys('a', function(event, handler){
event.preventDefault();
addControlPoint();
two.update();
});
hotkeys('s', function(event, handler){
event.preventDefault();
removeControlPoint();
two.update();
});
</script>
<Navbar bind:lines bind:startPoint bind:shapes bind:settings bind:robotWidth bind:robotHeight {saveFile} {loadFile} {loadRobot}/>
<div
class="w-screen h-screen pt-20 p-2 flex flex-row justify-center items-center gap-2"
>
<div class="flex h-full justify-center items-center">
<div
bind:this={twoElement}
class="h-full aspect-square rounded-lg shadow-md bg-neutral-50 dark:bg-neutral-900 relative overflow-clip"
>
<img
src="/fields/decode.webp"
alt="Field"
class="absolute top-0 left-0 w-full h-full rounded-lg z-10 pointer-events-none"
/>
<MathTools {x} {y} {twoElement} {robotXY} {robotHeading} />
<img
src={"/robot.png"}
alt="Robot"
style={`position: absolute; top: ${robotXY.y}px; left: ${robotXY.x}px; transform: translate(-50%, -50%) rotate(${robotHeading}deg); z-index: 20; width: ${x(robotWidth)}px; height: ${x(robotHeight)}px;`}
/>
</div>
</div>
<ControlTab
bind:playing
{play}
{pause}
bind:startPoint
bind:lines
bind:robotWidth
bind:robotHeight
bind:settings
bind:percent
bind:robotXY
bind:robotHeading
bind:shapes
{x}
{y}
{fpa}
/>
</div>