-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1342 lines (1192 loc) · 47 KB
/
Copy pathscript.js
File metadata and controls
1342 lines (1192 loc) · 47 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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const ip = '192.168.31.248:'
const port = '9090'; // 替换为你的Rosbridge服务器端口
async function loadConfig() {
try {
const response = await fetch('config.json');
if (!response.ok) throw new Error('Failed to load config file');
const config_data = await response.json();
console.log('Config file loaded successfully:', config_data);
if (!config_data.rosbridge_ip || !config_data.rosbridge_port || !config_data.http_port) {
throw new Error('Necessary configuration fields are missing');
}
return config_data;
} catch (error) {
console.error('Error while loading config:', error);
// 提供默认配置作为回退
return {
rosbridge_ip: "192.168.31.248",
rosbridge_port: "9090",
http_port: 8080,
publish_interval: 10,
dead_zone: 0.2,
_isFallBack: true
};
}
}
// 全局变量
// 摇杆输出值
let leftOutput = { x: 0, y: 0 };
let rightOutput = { x: 0 };
let window_width = document.documentElement.clientWidth;
let window_height = document.documentElement.clientHeight;
let size_multiplier = 1.0;
// 活动触摸点记录
let activeTouches = {
left: null,
vertical: null,
right: null,
pitch: null,
gaitPeriod: null,
gaitLength: null,
ZSwing: null
};
// 高度滑块控制元素
const verticalSliderKnob = document.getElementById('vertical-slider-knob');
const verticalSliderContainer = document.querySelector('.vertical-slider-container');
let verticalValue = 0.5;
let verticalActive = false;
let verticalStartY = 0;
let verticalSliderHeight = 200;
function initVerticalSlider() {
updateVerticalSliderPostion(verticalValue);
}
const pitchSliderKnob = document.getElementById('pitch-slider-knob');
const pitchSliderContainer = document.querySelector('.pitch-slider-container');
let pitchValue = 0.5;
let pitchActive = false;
let pitchStartY = 0;
let pitchSliderHeight = 200;
function initPitchSlider() {
updatePitchSliderPosition(pitchValue);
}
// 贝塞尔曲线变换函数
function applyVerticalCurveTransform(progress) {
return getPointOnBezierCurve(
progress,
verticalBezierCurvePoints.p0*size_multiplier,
verticalBezierCurvePoints.p1*size_multiplier,
verticalBezierCurvePoints.p2*size_multiplier,
verticalBezierCurvePoints.p3*size_multiplier
);
}
function applyPitchCurveTransform(progress) {
return getPointOnBezierCurve(
progress,
pitchBezierCurvePoints.p0*size_multiplier,
pitchBezierCurvePoints.p1*size_multiplier,
pitchBezierCurvePoints.p2*size_multiplier,
pitchBezierCurvePoints.p3*size_multiplier
);
}
const verticalBezierCurvePoints = {
p0: {x: 200, y: 200},
p1: {x: 180, y: 100},
p2: {x: 75, y: 35},
p3: {x: 25, y: 25}
};
const pitchBezierCurvePoints = {
p0: {x: 55, y: 200},
p1: {x: 75, y: 100},
p2: {x: 180, y: 35},
p3: {x: 230, y: 25}
}
function getPointOnBezierCurve(percent, p0, p1, p2, p3) {
// 确保百分比在0-1之间
const t = Math.max(0, Math.min(1, percent));
// 三次贝塞尔曲线公式
const x = Math.pow(1 - t, 3) * p0.x +
3 * Math.pow(1 - t, 2) * t * p1.x +
3 * (1 - t) * Math.pow(t, 2) * p2.x +
Math.pow(t, 3) * p3.x;
const y = Math.pow(1 - t, 3) * p0.y +
3 * Math.pow(1 - t, 2) * t * p1.y +
3 * (1 - t) * Math.pow(t, 2) * p2.y +
Math.pow(t, 3) * p3.y;
return { x, y };
}
function updateVerticalSliderPostion(progress) {
verticalValue = progress;
document.getElementById('vertical-value').textContent = verticalValue.toFixed(2);
console.info(`Updating vertical slider position: ${verticalValue}`);
const knobPosition = applyVerticalCurveTransform(progress);
verticalSliderKnob.style.left = `${knobPosition.x*size_multiplier}px`;
verticalSliderKnob.style.top = `${knobPosition.y*size_multiplier}px`;
updateVerticalFillPath(progress);
}
function updatePitchSliderPosition(progress) {
pitchValue = progress;
document.getElementById('pitch-value').textContent = pitchValue.toFixed(2);
console.info(`Updating pitch slider position: ${pitchValue}`);
const knobPosition = applyPitchCurveTransform(progress);
pitchSliderKnob.style.left = `${knobPosition.x*size_multiplier}px`;
pitchSliderKnob.style.top = `${knobPosition.y*size_multiplier}px`;
updatePitchFillPath(progress);
}
function updateVerticalFillPath(progress) {
const path = document.getElementById('vertical-fill-path');
const segments = 20;
let d = `M${200 * size_multiplier}, ${200 * size_multiplier}`;
for (let i = 0; i <= segments; i++) {
const p = Math.min(i / segments, progress);
const point = applyVerticalCurveTransform(p);
d += ` L${point.x},${point.y}`;
}
path.setAttribute('d', d);
path.setAttribute('stroke-width', 30 * size_multiplier);
}
function updatePitchFillPath(progress) {
const path = document.getElementById('pitch-fill-path');
const segments = 20;
let d = `M${55 * size_multiplier}, ${200 * size_multiplier}`;
for (let i = 0; i <= segments; i++) {
const p = Math.min(i / segments, progress);
const point = applyPitchCurveTransform(p);
d += ` L${point.x},${point.y}`;
}
path.setAttribute('d', d);
path.setAttribute('stroke-width', 30 * size_multiplier);
}
verticalSliderContainer.addEventListener('touchstart', (event) => {
if (activeTouches.vertical !== null) return;
event.preventDefault();
const touch = event.changedTouches[0];
verticalActive = true;
activeTouches.vertical = touch.identifier;
updateVerticalSliderFromEvent(event);
updateDebugInfo("height-info", "Height slider touch start");
});
verticalSliderKnob.addEventListener('touchstart', startDragVertical);
verticalSliderContainer.addEventListener('mousedown', startDragVertical);
function startDragVertical(event) {
if (activeTouches.vertical !== null) return;
event.preventDefault();
verticalActive = true;
if (event.touches) {
for (let i = 0; i < event.touches.length; i++) {
if (event.touches[i].target === verticalSliderKnob || event.touches[i].target === verticalSliderContainer) {
activeTouches.vertical = event.touches[i].identifier;
break;
}
}
} else {
activeTouches.vertical = 'mouse';
}
updateVerticalSliderFromEvent(event);
updateDebugInfo("height-info", "Height slider touch start");
}
pitchSliderContainer.addEventListener('touchstart', (event) => {
if (activeTouches.pitch !== null) return;
event.preventDefault();
const touch = event.changedTouches[0];
pitchActive = true;
activeTouches.pitch = touch.identifier;
updatePitchSliderFromEvent(event);
updateDebugInfo("pitch-info", "Pitch slider touch start");
});
pitchSliderKnob.addEventListener('touchstart', startDragPitch);
pitchSliderContainer.addEventListener('mousedown', startDragPitch);
function startDragPitch(event) {
if (activeTouches.pitch !== null) return;
event.preventDefault();
pitchActive = true;
if (event.touches) {
for (let i = 0; i < event.touches.length; i++) {
if (event.touches[i].target === pitchSliderKnob || event.touches[i].target === pitchSliderContainer) {
activeTouches.pitch = event.touches[i].identifier;
break;
}
}
}
else {
activeTouches.pitch = 'mouse';
}
updatePitchSliderFromEvent(event);
updateDebugInfo("pitch-info", "Pitch slider touch start");
}
function updateVerticalSliderFromEvent(event) {
const rect = verticalSliderContainer.getBoundingClientRect();
let mouseX, mouseY;
if (event.type == 'touchmove'){
for (let i = 0; i < event.touches.length; i++) {
if (event.touches[i].identifier === activeTouches.vertical) {
mouseX = event.touches[i].clientX;
mouseY = event.touches[i].clientY;
break;
}
}
} else {
mouseX = event.clientX;
mouseY = event.clientY;
}
if (!mouseX || !mouseY) {
console.warn("Mouse coordinates not found, aborting update.");
return;
}
const x = mouseX - rect.left;
const y = mouseY - rect.top;
let closestProgress = 0;
let minDistance = Infinity;
for (let p = 0; p < 1.01; p+=0.01){
const point = applyVerticalCurveTransform(p);
const distance = Math.sqrt(Math.pow(point.x - x, 2) + Math.pow(point.y - y, 2));
if (distance < minDistance) {
minDistance = distance;
closestProgress = p;
}
}
verticalValue = closestProgress;
onParamsChanged();
updateVerticalSliderPostion(verticalValue);
updateDebugInfo("height-info", `Height slider updated: ${verticalValue.toFixed(2)}`);
}
function updatePitchSliderFromEvent(event) {
const rect = pitchSliderContainer.getBoundingClientRect();
let mouseX, mouseY;
if (event.type == 'touchmove'){
for (let i = 0; i < event.touches.length; i++) {
if (event.touches[i].identifier === activeTouches.pitch) {
mouseX = event.touches[i].clientX;
mouseY = event.touches[i].clientY;
break;
}
}
} else {
mouseX = event.clientX;
mouseY = event.clientY;
}
if (!mouseX || !mouseY) {
console.warn("Mouse coordinates not found, aborting update.");
return;
}
const x = mouseX - rect.left
const y = mouseY - rect.top;
let closestProgress = 0;
let minDistance = Infinity;
for (let p = 0; p < 1.01; p+=0.01){
const point = applyPitchCurveTransform(p);
const distance = Math.sqrt(Math.pow(point.x - x, 2) + Math.pow(point.y - y, 2));
if (distance < minDistance) {
minDistance = distance;
closestProgress = p;
}
}
pitchValue = closestProgress;
onParamsChanged();
updatePitchSliderPosition(pitchValue);
updateDebugInfo("pitch-info", `Pitch slider updated: ${pitchValue.toFixed(2)}`);
}
const gaitPeriodContainer = document.querySelector('.gait-period-container');
const gaitPeriodSVG = document.getElementById('gait-period-svg');
const gaitPeriodTrackPath = document.getElementById('gait-period-track-path');
const gaitPeriodFillPath = document.getElementById('gait-period-fill-path');
const gaitPeriodKnob = document.getElementById('gait-period-knob');
let gaitPeriodValue = 0;
let gaitPeriodActive = false;
const gaitPeriodStartY = 180;
let gaitPeriodSliderHeight = 160;
function initGaitPeriodSlider() {
updateGaitPeriodSliderPosition(gaitPeriodValue);
}
function updateGaitPeriodSliderPosition(progress) {
gaitPeriodValue = progress;
document.getElementById('gait-period-value').textContent = gaitPeriodValue.toFixed(2);
console.info(`Updating gait period slider position: ${gaitPeriodValue}`);
const knobPosition = gaitPeriodStartY - progress * gaitPeriodSliderHeight;
gaitPeriodKnob.style.top = `${knobPosition}px`;
updateGaitPeriodFillPath(progress);
}
function updateGaitPeriodFillPath(progress) {
const path = document.getElementById('gait-period-fill-path');
const segments = 20;
let d = `M 50, ${gaitPeriodStartY}`;
for (let i = 0; i <= segments; i++) {
const p = Math.min(i / segments, progress);
const y = gaitPeriodStartY - p * gaitPeriodSliderHeight;
d += ` L${50},${y}`;
}
path.setAttribute('d', d);
}
gaitPeriodContainer.addEventListener('touchstart', startDragGaitPeriod);
gaitPeriodContainer.addEventListener('mousedown', startDragGaitPeriod);
function startDragGaitPeriod(event) {
if (activeTouches.gaitPeriod != null) return;
event.preventDefault();
if (event.touches) {
for (let i = 0; i < event.touches.length; i++) {
const target = event.touches[i].target;
if (target === gaitPeriodKnob || target === gaitPeriodContainer || target === gaitPeriodSVG || target === gaitPeriodTrackPath || target === gaitPeriodFillPath) {
activeTouches.gaitPeriod = event.touches[i].identifier;
break;
}
}
} else {
gaitPeriodActive = true;
}
updateGaitPeriodSliderFromEvent(event);
}
function updateGaitPeriodSliderFromEvent(event) {
const rect = gaitPeriodContainer.getBoundingClientRect();
let mouseY;
if (event.type == 'touchmove') {
for (let i = 0; i < event.touches.length; i++) {
if (event.touches[i].identifier === activeTouches.gaitPeriod) {
mouseY = event.touches[i].clientY - 25;
break;
}
}
}
else {
mouseY = event.clientY - 25;
}
if (!mouseY) {
console.warn("Mouse Y coordinate not found, aborting update.");
return;
}
const y = mouseY - rect.top;
const progress = Math.max(0, Math.min(1, (gaitPeriodStartY - y) / gaitPeriodSliderHeight));
gaitPeriodValue = progress;
onParamsChanged();
updateGaitPeriodSliderPosition(gaitPeriodValue);
updateDebugInfo("gait-period-info", `Gait period slider updated: ${gaitPeriodValue.toFixed(2)}`);
}
const gaitLengthContainer = document.querySelector('.gait-length-container');
const gaitLengthSVG = document.getElementById('gait-length-svg');
const gaitLengthTrackPath = document.getElementById('gait-length-track-path');
const gaitLengthFillPath = document.getElementById('gait-length-fill-path');
const gaitLengthKnob = document.getElementById('gait-length-knob');
let gaitLengthValue = 0;
let gaitLengthActive = false;
const gaitLengthStartY = 180;
let gaitLengthSliderHeight = 160;
function initGaitLengthSlider() {
updateGaitLengthSliderPosition(gaitLengthValue);
}
function updateGaitLengthSliderPosition(progress) {
gaitLengthValue = progress;
document.getElementById('gait-length-value').textContent = gaitLengthValue.toFixed(2);
console.info(`Updating gait length slider position: ${gaitLengthValue}`);
const knobPosition = gaitLengthStartY - progress * gaitLengthSliderHeight;
gaitLengthKnob.style.top = `${knobPosition}px`;
updateGaitLengthFillPath(progress);
}
function updateGaitLengthFillPath(progress) {
const path = document.getElementById('gait-length-fill-path');
const segments = 20;
let d = `M 60, ${gaitLengthStartY}`;
for (let i = 0; i <= segments; i++) {
const p = Math.min(i / segments, progress);
const y = gaitLengthStartY - p * gaitLengthSliderHeight;
d += ` L${60},${y}`;
}
path.setAttribute('d', d);
const track = document.getElementById('gait-length-track-path');
d = `M ${60 * size_multiplier}, ${gaitLengthStartY * size_multiplier} L ${60 * size_multiplier}, ${(gaitLengthStartY - gaitLengthSliderHeight) * size_multiplier}`;
}
gaitLengthContainer.addEventListener('touchstart', startDragGaitLength);
gaitLengthContainer.addEventListener('mousedown', startDragGaitLength);
function startDragGaitLength(event) {
if (activeTouches.gaitLength != null) return;
event.preventDefault();
if (event.touches) {
for (let i = 0; i < event.touches.length; i++) {
const target = event.touches[i].target;
if (target === gaitLengthKnob || target === gaitLengthContainer || target === gaitLengthSVG || target === gaitLengthTrackPath || target === gaitLengthFillPath) {
activeTouches.gaitLength = event.touches[i].identifier;
break;
}
}
} else {
gaitLengthActive = true;
}
updateGaitLengthSliderFromEvent(event);
}
function updateGaitLengthSliderFromEvent(event) {
const rect = gaitLengthContainer.getBoundingClientRect();
let mouseY;
if (event.type == 'touchmove') {
for (let i = 0; i < event.touches.length; i++) {
if (event.touches[i].identifier === activeTouches.gaitLength) {
mouseY = event.touches[i].clientY - 25;
break;
}
}
}
else {
mouseY = event.clientY - 25;
}
if (!mouseY) {
console.warn("Mouse Y coordinate not found, aborting update.");
return;
}
const y = mouseY - rect.top;
const progress = Math.max(0, Math.min(1, (gaitLengthStartY - y) / gaitLengthSliderHeight));
gaitLengthValue = progress;
onParamsChanged();
updateGaitLengthSliderPosition(gaitLengthValue);
updateDebugInfo("gait-length-info", `Gait length slider updated: ${gaitLengthValue.toFixed(2)}`);
}
const zSwingContainer = document.querySelector('.z-swing-container');
const zSwingSVG = document.getElementById('z-swing-svg');
const zSwingTrackPath = document.getElementById('z-swing-track-path');
const zSwingFillPath = document.getElementById('z-swing-fill-path');
const zSwingKnob = document.getElementById('z-swing-knob');
let zSwingValue = 0;
let zSwingActive = false;
const zSwingStartX = 20;
let zSwingSliderWidth = 300;
function initZSwingSlider() {
updateZSwingSliderPosition(zSwingValue);
}
function updateZSwingSliderPosition(progress) {
zSwingValue = progress;
document.getElementById('z-swing-value').textContent = zSwingValue.toFixed(2);
console.info(`Updating Z-Swing slider position: ${zSwingValue}`);
const knobPosition = progress * zSwingSliderWidth;
zSwingKnob.style.left = `${knobPosition}px`;
updateZSwingFillPath(progress);
}
function updateZSwingFillPath(progress) {
const path = document.getElementById('z-swing-fill-path');
const segments = 40;
let d = `M ${zSwingStartX}, 50`;
for (let i = 0; i <= segments; i++) {
const p = Math.min(i / segments, progress);
const x = zSwingStartX + p * zSwingSliderWidth;
d += ` L${x},50`;
}
path.setAttribute('d', d);
}
zSwingContainer.addEventListener('touchstart', startDragZSwing);
zSwingContainer.addEventListener('mousedown', startDragZSwing);
function startDragZSwing(event) {
if (activeTouches.ZSwing != null) return;
event.preventDefault();
if (event.touches) {
for (let i = 0; i < event.touches.length; i++) {
const target = event.touches[i].target;
if (target === zSwingKnob || target === zSwingContainer || target === zSwingSVG || target === zSwingTrackPath || target === zSwingFillPath) {
activeTouches.ZSwing = event.touches[i].identifier;
break;
}
}
} else {
zSwingActive = true;
}
updateZSwingSliderFromEvent(event);
}
function updateZSwingSliderFromEvent(event) {
const rect = zSwingContainer.getBoundingClientRect();
let mouseX;
if (event.type == 'touchmove') {
for (let i = 0; i < event.touches.length; i++) {
if (event.touches[i].identifier === activeTouches.ZSwing) {
mouseX = event.touches[i].clientX;
break;
}
}
} else {
mouseX = event.clientX;
}
if (!mouseX) {
console.warn("Mouse X coordinate not found, aborting update.");
return;
}
const x = mouseX - rect.left;
const progress = Math.max(0, Math.min(1, (x - zSwingStartX) / zSwingSliderWidth));
zSwingValue = progress;
onParamsChanged();
updateZSwingSliderPosition(zSwingValue);
updateDebugInfo("z-swing-info", `Z-Swing slider updated: ${zSwingValue.toFixed(2)}`);
}
// 初始化
window.addEventListener('load', () => {
initVerticalSlider();
initPitchSlider();
initGaitPeriodSlider();
initGaitLengthSlider();
initZSwingSlider();
})
// 摇杆元素
const leftJoystickOuter = document.getElementById('left-outer');
const leftJoystickInner = document.getElementById('left-inner');
const rightJoystickOuter = document.getElementById('right-outer');
const rightJoystickInner = document.getElementById('right-inner');
// 按钮元素
const resetButton = document.getElementById('reset-all');
const jumpButton = document.getElementById('jump-btn');
const flipButton = document.getElementById('flip-btn');
const navigationButton = document.getElementById('navigation-btn');
const crawButton = document.getElementById('craw-btn');
const sandpitButton = document.getElementById('sandpit-btn');
const straightupButton = document.getElementById('straightup-btn');
const stairsButton = document.getElementById('stairs-btn');
const slopeButton = document.getElementById('slope-btn');
const sideslopeButton = document.getElementById('sideslope-btn');
// 新版更新调试信息函数
// 在全局作用域定义消息管理对象
const messageRegistry = new Map();
// 重写消息显示函数
function updateDebugInfo(id, message) {
const debugList = document.getElementById('debugList');
const timestamp = `[${new Date().toLocaleTimeString()}]`;
let msgElement = document.querySelector(`[data-id="${id}"]`); // 通过ID查找元素
// 情况1:元素存在且已隐藏 → 重激活
if (msgElement && msgElement.style.display === 'none') {
msgElement.textContent = `${timestamp} ${message}`;
msgElement.style.opacity = '1';
msgElement.style.display = 'block'; // 重新显示
clearTimeout(msgElement.timer); // 清除旧计时器
msgElement.timer = startFadeTimer(msgElement, id); // 重启淡出计时
messageRegistry.set(id, msgElement); // 重新注册
return;
}
// 情况2:元素存在且显示中 → 更新内容并重置计时器
if (msgElement) {
msgElement.textContent = `${timestamp} ${message}`;
clearTimeout(msgElement.timer);
msgElement.timer = startFadeTimer(msgElement, id);
return;
}
// 情况3:元素不存在 → 新建消息
msgElement = document.createElement('div');
msgElement.className = 'debug-item';
msgElement.dataset.id = id; // 标记ID以便查找
msgElement.textContent = `${timestamp} ${message}`;
debugList.prepend(msgElement);
msgElement.timer = startFadeTimer(msgElement, id); // 启动淡出计时
messageRegistry.set(id, msgElement); // 注册新消息
manageMessageQueue(); // 管理队列长度
debugList.scrollTop = 0;
}
// 启动淡出计时器
function startFadeTimer(element, id) {
return setTimeout(() => {
element.style.transition = 'opacity 0.5s ease';
element.style.opacity = '0';
// 淡出后隐藏而非移除元素
setTimeout(() => {
element.style.display = 'none';
messageRegistry.delete(id); // 清理注册表记录
}, 500);
}, 3000);
}
// 管理消息队列
function manageMessageQueue() {
const messages = debugList.querySelectorAll('.debug-item');
if (messages.length > 20) {
// 移除最旧且未显示的消息(非激活状态)
const oldestMsg = Array.from(messages).find(msg =>
msg.style.display !== 'none'
);
oldestMsg?.remove();
}
}
// 摇杆功能 - 支持多点触控
function setupJoystickEvents() {
// 左侧摇杆触摸开始
leftJoystickOuter.addEventListener('touchstart', function(e) {
// 检查是否已有一个活动触摸点
if (activeTouches.left !== null) return;
const touch = e.changedTouches[0];
activeTouches.left = touch.identifier;
const rect = leftJoystickOuter.getBoundingClientRect();
const centerX = rect.left + rect.width/2;
const centerY = rect.top + rect.height/2;
// 更新摇杆位置
updateJoystickPosition('left', touch.clientX - centerX, touch.clientY - centerY);
e.preventDefault();
updateDebugInfo("left-joystick","Left joystick touch start");
}, { passive: false });
// 右侧摇杆触摸开始
rightJoystickOuter.addEventListener('touchstart', function(e) {
// 检查是否已有一个活动触摸点
if (activeTouches.right !== null) return;
const touch = e.changedTouches[0];
activeTouches.right = touch.identifier;
const rect = rightJoystickOuter.getBoundingClientRect();
const centerX = rect.left + rect.width/2;
const centerY = rect.top + rect.height/2;
// 更新摇杆位置
updateJoystickPosition('right', touch.clientX - centerX, touch.clientY - centerY);
e.preventDefault();
updateDebugInfo("right-joystick","Right joystick touch start");
}, { passive: false });
// 左侧摇杆鼠标按下
leftJoystickOuter.addEventListener('mousedown', function(e) {
activeTouches.left = 'mouse';
const rect = leftJoystickOuter.getBoundingClientRect();
const centerX = rect.left + rect.width/2;
const centerY = rect.top + rect.height/2;
updateJoystickPosition('left', e.clientX - centerX, e.clientY - centerY);
e.preventDefault();
});
// 右侧摇杆鼠标按下
rightJoystickOuter.addEventListener('mousedown', function(e) {
activeTouches.right = 'mouse';
const rect = rightJoystickOuter.getBoundingClientRect();
const centerX = rect.left + rect.width/2;
const centerY = rect.top + rect.height/2;
updateJoystickPosition('right', e.clientX - centerX, e.clientY - centerY);
e.preventDefault();
});
// 新增鼠标移动事件绑定
leftJoystickOuter.addEventListener('mousemove', function(e) {
if (activeTouches.left === 'mouse') {
const rect = leftJoystickOuter.getBoundingClientRect();
const centerX = rect.left + rect.width/2;
const centerY = rect.top + rect.height/2;
updateJoystickPosition('left', e.clientX - centerX, e.clientY - centerY);
}
});
rightJoystickOuter.addEventListener('mousemove', function(e) {
if (activeTouches.right === 'mouse') {
const rect = rightJoystickOuter.getBoundingClientRect();
const centerX = rect.left + rect.width/2;
const centerY = rect.top + rect.height/2;
updateJoystickPosition('right', e.clientX - centerX, e.clientY - centerY);
}
});
}
// 更新摇杆位置
function updateJoystickPosition(type, dx, dy) {
const rect = (type === 'left' ?
leftJoystickOuter :
rightJoystickOuter).getBoundingClientRect();
// 最大移动距离
const maxDistance = Math.min(rect.width, rect.height) * 0.35;
const distance = Math.sqrt(dx * dx + dy * dy);
let newDx = dx;
let newDy = dy;
// 限制在最大距离内
if (distance > maxDistance) {
const scale = maxDistance / distance;
newDx = dx * scale;
newDy = dy * scale;
}
// 更新摇杆位置
const element = (type === 'left' ?
leftJoystickInner :
rightJoystickInner);
element.style.transform = `translate(calc(-50% + ${newDx}px), calc(-50% + ${newDy}px))`;
// 存储输出值
if (type === 'left') {
leftOutput.x = -newDy / maxDistance;
leftOutput.y = newDx / maxDistance;
if (leftOutput.y * leftOutput.y + leftOutput.x * leftOutput.x < config.dead_zone * config.dead_zone) {
leftOutput = { x: 0, y: 0 }; // 重置为0
}
else {
// 应用死区处理
const alpha = Math.atan2(leftOutput.y, leftOutput.x);
leftOutput.y = (leftOutput.y - config.dead_zone * Math.sin(alpha)) / (1 - config.dead_zone) * Math.abs(Math.sin(alpha));
leftOutput.x = (leftOutput.x - config.dead_zone * Math.cos(alpha)) / (1 - config.dead_zone) * Math.abs(Math.cos(alpha));
}
leftOutput.x = leftOutput.x > 0 ? Math.sqrt(leftOutput.x) : -Math.sqrt(-leftOutput.x);
leftOutput.y = leftOutput.y > 0 ? Math.sqrt(leftOutput.y) : -Math.sqrt(-leftOutput.y);
document.getElementById('left-x-output').textContent = leftOutput.x.toFixed(2);
document.getElementById('left-y-output').textContent = leftOutput.y.toFixed(2);
updateDebugInfo("left-info",`Left joystick: X:${leftOutput.x.toFixed(2)}, Y:${leftOutput.y.toFixed(2)}`);
} else {
rightOutput.x = newDx / maxDistance;
if (rightOutput.x * rightOutput.x < config.dead_zone * config.dead_zone) {
rightOutput = { x: 0 }; // 重置为0
}
else {
rightOutput.x = (rightOutput.x - (rightOutput.x > 0 ? config.dead_zone : -config.dead_zone)) / (1 - config.dead_zone);
}
document.getElementById('right-x-output').textContent = rightOutput.x.toFixed(2);
updateDebugInfo("right-info",`Right joystick: X:${rightOutput.x.toFixed(2)}`);
}
}
function inQuad(progress){
return progress * progress;
}
document.addEventListener('touchstart', function(e) {
for (let i = 0; i < e.touches.length; i++) {
const touch = e.touches[i];
if (touch.target === resetButton) {
resetButton.dispatchEvent(new TouchEvent('click'));
e.preventDefault();
updateDebugInfo("reset-button", "Reset button touched");
}
if (touch.target === jumpButton) {
jumpButton.dispatchEvent(new TouchEvent('click'));
e.preventDefault();
updateDebugInfo("jump-button", "Jump button touched");
}
if (touch.target === flipButton) {
flipButton.dispatchEvent(new TouchEvent('click'));
e.preventDefault();
updateDebugInfo("flip-button", "Flip button touched");
}
if (touch.target === stairsButton) {
stairsButton.dispatchEvent(new TouchEvent('click'));
e.preventDefault();
updateDebugInfo("stairs-button", "Stairs button touched");
}
if (touch.target === slopeButton) {
slopeButton.dispatchEvent(new TouchEvent('click'));
e.preventDefault();
updateDebugInfo("slope-button", "Slope button touched");
}
if (touch.target === sideslopeButton) {
sideslopeButton.dispatchEvent(new TouchEvent('click'));
e.preventDefault();
updateDebugInfo("sideslope-button", "Side Slope button touched");
}
if (touch.target === crawButton) {
crawButton.dispatchEvent(new TouchEvent('click'));
e.preventDefault();
updateDebugInfo("craw-button", "Craw button touched");
}
if (touch.target === sandpitButton) {
sandpitButton.dispatchEvent(new TouchEvent('click'));
e.preventDefault();
updateDebugInfo("sandpit-button", "Sandpit button touched");
}
if (touch.target === straightupButton) {
straightupButton.dispatchEvent(new TouchEvent('click'));
e.preventDefault();
updateDebugInfo("straightup-button", "Straight Up button touched");
}
if (touch.target === navigationButton) {
navigationButton.dispatchEvent(new TouchEvent('click'));
e.preventDefault();
updateDebugInfo("navigation-button", "Navigation button touched");
}
}
})
// 触摸移动处理 - 多点触控支持
document.addEventListener('touchmove', function(e) {
e.preventDefault();
for (let i = 0; i < e.touches.length; i++) {
const touch = e.touches[i];
// 更新左侧摇杆
if (activeTouches.left === touch.identifier) {
const rect = leftJoystickOuter.getBoundingClientRect();
const centerX = rect.left + rect.width/2;
const centerY = rect.top + rect.height/2;
updateJoystickPosition('left', touch.clientX - centerX, touch.clientY - centerY);
}
// 更新右侧摇杆
if (activeTouches.right === touch.identifier) {
const rect = rightJoystickOuter.getBoundingClientRect();
const centerX = rect.left + rect.width/2;
const centerY = rect.top + rect.height/2;
updateJoystickPosition('right', touch.clientX - centerX, touch.clientY - centerY);
}
// 更新高度条
if (activeTouches.vertical === touch.identifier) {
updateVerticalSliderFromEvent(e);
}
// 更新倾角条
if (activeTouches.pitch === touch.identifier) {
updatePitchSliderFromEvent(e);
}
if (activeTouches.gaitLength === touch.identifier) {
updateGaitLengthSliderFromEvent(e);
}
if (activeTouches.gaitPeriod === touch.identifier) {
updateGaitPeriodSliderFromEvent(e);
}
// 更新Z-Swing条
if (activeTouches.ZSwing === touch.identifier) {
updateZSwingSliderFromEvent(e);
}
}
}, { passive: false });
// 触摸结束处理
document.addEventListener('touchend', function(e) {
for (let i = 0; i < e.changedTouches.length; i++) {
const touch = e.changedTouches[i];
// 重置左侧摇杆
if (activeTouches.left === touch.identifier) {
resetJoystick('left');
activeTouches.left = null;
updateDebugInfo("left-joystick","Left joystick touch end");
}
// 重置右侧摇杆
if (activeTouches.right === touch.identifier) {
resetJoystick('right');
activeTouches.right = null;
updateDebugInfo("right-joystick","Right joystick touch end");
}
// 重置高度条
if (activeTouches.vertical === touch.identifier) {
verticalActive = false;
activeTouches.vertical = null;
updateDebugInfo("height-info","Height slider touch end");
}
// 重置倾角条
if (activeTouches.pitch === touch.identifier) {
pitchActive = false;
activeTouches.pitch = null;
updateDebugInfo("pitch-info", "Pitch slider touch end");
}
// 重置步态周期条
if (activeTouches.gaitPeriod === touch.identifier) {
gaitPeriodActive = false;
activeTouches.gaitPeriod = null;
updateDebugInfo("gait-period-info", "Gait period slider touch end");
}
// 重置步态长度条
if (activeTouches.gaitLength === touch.identifier) {
gaitLengthActive = false;
activeTouches.gaitLength = null;
updateDebugInfo("gait-length-info", "Gait length slider touch end");
}
// 重置Z-Swing条
if (activeTouches.ZSwing === touch.identifier) {
zSwingActive = false;
activeTouches.ZSwing = null;
updateDebugInfo("z-swing-info", "Z-Swing slider touch end");
}
}
});
// 鼠标移动处理
document.addEventListener('mousemove', function(e) {
if (verticalActive) {
updateVerticalSliderFromEvent(e);
}
if (activeTouches.left === 'mouse') {
const rect = leftJoystickOuter.getBoundingClientRect();
const centerX = rect.left + rect.width/2;
const centerY = rect.top + rect.height/2;
updateJoystickPosition('left', e.clientX - centerX, e.clientY - centerY);
e.preventDefault();
}
if (activeTouches.right === 'mouse') {
const rect = rightJoystickOuter.getBoundingClientRect();
const centerX = rect.left + rect.width/2;
const centerY = rect.top + rect.height/2;
updateJoystickPosition('right', e.clientX - centerX, e.clientY - centerY);
e.preventDefault();
}
if (pitchActive) {
updatePitchSliderFromEvent(e);
}
if (gaitPeriodActive) {
updateGaitPeriodSliderFromEvent(e);
}
if (gaitLengthActive) {
updateGaitLengthSliderFromEvent(e);
}
if (zSwingActive) {
updateZSwingSliderFromEvent(e);
}
});
// 鼠标抬起处理
document.addEventListener('mouseup', function() {
if (verticalActive) {