-
Notifications
You must be signed in to change notification settings - Fork 30
/
hotkey.ts
1599 lines (1518 loc) · 42.3 KB
/
hotkey.ts
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
// https://github.com/jamiebuilds/tinykeys/pull/193/commits/2598ecb3db6b3948c7acbf0e7bd8b0674961ad61
import { MessageReactive, useMessage } from "naive-ui";
import {
SwipeAction,
TouchAction,
swipe,
touch,
} from "./frontcommand/scrcpyMaskCmd";
import {
KeyCancelSkill,
KeyDirectionalSkill,
KeyDirectionlessSkill,
KeyFire,
KeyMacro,
KeyMacroList,
KeyMappingConfig,
KeyObservation,
KeySight,
KeySteeringWheel,
KeySwipe,
KeyTap,
KeyTriggerWhenDoublePressedSkill,
KeyTriggerWhenPressedSkill,
} from "./keyMappingConfig";
import { useGlobalStore } from "./store/global";
import { LogicalPosition, getCurrent } from "@tauri-apps/api/window";
import { readText } from "@tauri-apps/plugin-clipboard-manager";
import { useI18n } from "vue-i18n";
import { KeyToCodeMap } from "./frontcommand/KeyToCodeMap";
import {
AndroidKeyEventAction,
AndroidMetastate,
} from "./frontcommand/android";
import { UIEventsCode } from "./frontcommand/UIEventsCode";
import { sendInjectKeycode, sendSetClipboard } from "./frontcommand/controlMsg";
function clientxToPosx(clientx: number) {
return clientx < 70
? 0
: Math.floor((clientx - 70) * (store.screenSizeW / store.maskSizeW));
}
function clientyToPosy(clienty: number) {
return clienty < 30
? 0
: Math.floor((clienty - 30) * (store.screenSizeH / store.maskSizeH));
}
function clientxToPosOffsetx(clientx: number, posx: number, scale = 1) {
let offsetX = clientxToPosx(clientx) - posx;
return Math.round(offsetX * scale);
}
function clientyToPosOffsety(clienty: number, posy: number, scale = 1) {
let offsetY = clientyToPosy(clienty) - posy;
return Math.round(offsetY * scale);
}
function clientPosToSkillOffset(
clientPos: { x: number; y: number },
range: number
): { offsetX: number; offsetY: number } {
const maskSizeH = store.maskSizeH;
const maskSizeW = store.maskSizeW;
const maxLength = (120 / maskSizeH) * store.screenSizeH;
const centerX = maskSizeW * 0.5;
const centerY = maskSizeH * 0.5;
// The center of the game display is higher than the center of the mask
clientPos.y -= maskSizeH * 0.066;
// w450 : h315 = 100 : 70, so the true offsetX is 0.7 * cOffsetX
const cOffsetX = (clientPos.x - 70 - centerX) * 0.7;
const cOffsetY = clientPos.y - 30 - centerY;
const offsetD = Math.sqrt(cOffsetX ** 2 + cOffsetY ** 2);
if (offsetD == 0) {
return {
offsetX: 0,
offsetY: 0,
};
}
const rangeD = (maskSizeH - centerY) * range * 0.01;
if (offsetD >= rangeD) {
// include the case of rangeD == 0
return {
offsetX: Math.round((maxLength / offsetD) * cOffsetX),
offsetY: Math.round((maxLength / offsetD) * cOffsetY),
};
} else {
const factor = offsetD / rangeD;
return {
offsetX: Math.round((cOffsetX / rangeD) * maxLength * factor),
offsetY: Math.round((cOffsetY / rangeD) * maxLength * factor),
};
}
}
async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function calculateMacroPosX(
// pos relative to the mask
posX: [string, number] | number,
relativeSizeW: number
): number {
if (typeof posX === "number") {
return Math.round(posX * (store.screenSizeW / relativeSizeW));
}
if (typeof posX === "string") {
return clientxToPosx(mouseX);
} else {
if (posX[0] === "mouse") {
return (
clientxToPosx(mouseX) +
Math.round(posX[1] * (store.screenSizeW / relativeSizeW))
);
} else {
throw new Error("Invalid pos");
}
}
}
function calculateMacroPosY(
// pos relative to the mask
posY: [string, number] | number,
relativeSizeH: number
): number {
if (typeof posY === "number") {
return Math.round(posY * (store.screenSizeH / relativeSizeH));
}
if (typeof posY === "string") {
return clientyToPosy(mouseY);
} else {
if (posY[0] === "mouse") {
return (
clientyToPosy(mouseY) +
Math.round(posY[1] * (store.screenSizeH / relativeSizeH))
);
} else {
throw new Error("Invalid pos");
}
}
}
function calculateMacroPosList(
posList: [[string, number] | number, [string, number] | number][],
relativeSize: { w: number; h: number }
): { x: number; y: number }[] {
return posList.map((posPair) => {
return {
x: calculateMacroPosX(posPair[0], relativeSize.w),
y: calculateMacroPosY(posPair[1], relativeSize.h),
};
});
}
// add shortcuts for observation
function addObservationShortcuts(
key: string,
relativeSize: { w: number; h: number },
// pos relative to the mask
posX: number,
posY: number,
scale: number,
pointerId: number
) {
let observationMouseX = 0;
let observationMouseY = 0;
posX = Math.round((posX / relativeSize.w) * store.screenSizeW);
posY = Math.round((posY / relativeSize.h) * store.screenSizeH);
addShortcut(
key,
async () => {
observationMouseX = clientxToPosx(mouseX);
observationMouseY = clientyToPosy(mouseY);
await touchX(TouchAction.Down, pointerId, posX, posY);
},
async () => {
await touchX(
TouchAction.Move,
pointerId,
posX + clientxToPosOffsetx(mouseX, observationMouseX, scale),
posY + clientyToPosOffsety(mouseY, observationMouseY, scale)
);
},
async () => {
await touchX(
TouchAction.Up,
pointerId,
posX + clientxToPosOffsetx(mouseX, observationMouseY, scale),
posY + clientyToPosOffsety(mouseY, observationMouseY, scale)
);
}
);
}
// add shortcuts for simple tap (touch when press down)
function addTapShortcuts(
key: string,
relativeSize: { w: number; h: number },
time: number,
// pos relative to the mask
posX: number,
posY: number,
pointerId: number
) {
posX = Math.round((posX / relativeSize.w) * store.screenSizeW);
posY = Math.round((posY / relativeSize.h) * store.screenSizeH);
addShortcut(
key,
async () => {
await touchX(TouchAction.Default, pointerId, posX, posY, time);
},
undefined,
undefined
);
}
// add shortcuts for cancel skill
function addCancelSkillShortcuts(
key: string,
relativeSize: { w: number; h: number },
// pos relative to the mask
posX: number,
posY: number,
pointerId: number
) {
posX = Math.round((posX / relativeSize.w) * store.screenSizeW);
posY = Math.round((posY / relativeSize.h) * store.screenSizeH);
addShortcut(
key,
async () => {
// delete the callback
for (const cancelAbleKey of cancelAbleKeyList) {
loopDownKeyCBMap.delete(cancelAbleKey);
upKeyCBMap.delete(cancelAbleKey);
}
// special case for double press skill
for (const [key, val] of doublePressedDownKey) {
if (val) {
loopDownKeyCBMap.delete(key);
doublePressedDownKey.set(key, false);
}
}
let distance = 0;
while (distance <= 20) {
await touchX(TouchAction.Move, pointerId, posX + distance, posY);
await sleep(5);
distance += 1;
}
await touchX(TouchAction.Up, pointerId, posX, posY);
},
undefined,
undefined
);
}
// add shortcuts for trigger when pressed skill
function addTriggerWhenPressedSkillShortcuts(
key: string,
relativeSize: { w: number; h: number },
// pos relative to the mask
posX: number,
posY: number,
directional: boolean,
// range: when directional is true
// time: when directional is false
rangeOrTime: number,
pointerId: number
) {
posX = Math.round((posX / relativeSize.w) * store.screenSizeW);
posY = Math.round((posY / relativeSize.h) * store.screenSizeH);
if (directional) {
addShortcut(
key,
// down
async () => {
// up doublepress skill
await upDoublePressedKey();
const skillOffset = clientPosToSkillOffset(
{ x: mouseX, y: mouseY },
rangeOrTime
);
await swipe({
action: SwipeAction.Default,
pointerId,
screen: {
w: store.screenSizeW,
h: store.screenSizeH,
},
pos: [
{ x: posX, y: posY },
{
x: posX + skillOffset.offsetX,
y: posY + skillOffset.offsetY,
},
],
intervalBetweenPos: 0,
});
},
undefined,
undefined
);
} else {
addShortcut(
key,
async () => {
await upDoublePressedKey();
await touchX(TouchAction.Default, pointerId, posX, posY, rangeOrTime);
},
undefined,
undefined
);
}
}
// add shortcuts for trigger when double pressed skill (cancelable, but in another way)
const doublePressedDownKey = new Map<string, boolean>();
function addTriggerWhenDoublePressedSkillShortcuts(
key: string,
relativeSize: { w: number; h: number },
// pos relative to the mask
posX: number,
posY: number,
range: number,
pointerId: number
) {
posX = Math.round((posX / relativeSize.w) * store.screenSizeW);
posY = Math.round((posY / relativeSize.h) * store.screenSizeH);
doublePressedDownKey.set(key, false);
addShortcut(
key,
// down
async () => {
if (doublePressedDownKey.get(key) === false) {
// first press: touch down
const skillOffset = clientPosToSkillOffset(
{ x: mouseX, y: mouseY },
range
);
await swipe({
action: SwipeAction.NoUp,
pointerId,
screen: {
w: store.screenSizeW,
h: store.screenSizeH,
},
pos: [
{ x: posX, y: posY },
{
x: posX + skillOffset.offsetX,
y: posY + skillOffset.offsetY,
},
],
intervalBetweenPos: 0,
});
// set the flag to true
doublePressedDownKey.set(key, true);
// add loop CB
loopDownKeyCBMap.set(key, async () => {
const loopSkillOffset = clientPosToSkillOffset(
{ x: mouseX, y: mouseY },
range
);
await touchX(
TouchAction.Move,
pointerId,
posX + loopSkillOffset.offsetX,
posY + loopSkillOffset.offsetY
);
});
} else {
// second press: touch up
// delete the loop CB
loopDownKeyCBMap.delete(key);
const skillOffset = clientPosToSkillOffset(
{ x: mouseX, y: mouseY },
range
);
await touchX(
TouchAction.Up,
pointerId,
posX + skillOffset.offsetX,
posY + skillOffset.offsetY
);
// set the flag to false
doublePressedDownKey.set(key, false);
}
},
undefined,
undefined
);
}
// add shortcuts for directionless skill (cancelable)
function addDirectionlessSkillShortcuts(
key: string,
relativeSize: { w: number; h: number },
// pos relative to the mask
posX: number,
posY: number,
pointerId: number
) {
posX = Math.round((posX / relativeSize.w) * store.screenSizeW);
posY = Math.round((posY / relativeSize.h) * store.screenSizeH);
addShortcut(
key,
// down
async () => {
// up doublepress skill
await upDoublePressedKey();
await touchX(TouchAction.Down, pointerId, posX, posY);
},
// loop
undefined,
// up
async () => {
await touch({
action: TouchAction.Up,
pointerId,
screen: {
w: store.screenSizeW,
h: store.screenSizeH,
},
pos: {
x: posX,
y: posY,
},
});
},
true
);
}
// up all double pressed key
async function upDoublePressedKey() {
for (const [key, val] of doublePressedDownKey) {
if (val) {
await downKeyCBMap.get(key)?.();
}
}
}
// add shortcuts for directional skill (cancelable)
function addDirectionalSkillShortcuts(
key: string,
relativeSize: { w: number; h: number },
// pos relative to the mask
posX: number,
posY: number,
range: number,
pointerId: number
) {
posX = Math.round((posX / relativeSize.w) * store.screenSizeW);
posY = Math.round((posY / relativeSize.h) * store.screenSizeH);
addShortcut(
key,
// down
async () => {
// up doublepress skill
await upDoublePressedKey();
const skillOffset = clientPosToSkillOffset(
{ x: mouseX, y: mouseY },
range
);
await swipe({
action: SwipeAction.NoUp,
pointerId,
screen: {
w: store.screenSizeW,
h: store.screenSizeH,
},
pos: [
{ x: posX, y: posY },
{
x: posX + skillOffset.offsetX,
y: posY + skillOffset.offsetY,
},
],
intervalBetweenPos: 0,
});
},
// loop
async () => {
const skillOffset = clientPosToSkillOffset(
{ x: mouseX, y: mouseY },
range
);
await touchX(
TouchAction.Move,
pointerId,
posX + skillOffset.offsetX,
posY + skillOffset.offsetY
);
},
// up
async () => {
const skillOffset = clientPosToSkillOffset(
{ x: mouseX, y: mouseY },
range
);
await touchX(
TouchAction.Up,
pointerId,
posX + skillOffset.offsetX,
posY + skillOffset.offsetY
);
},
true
);
}
// add shortcuts for steering wheel
function addSteeringWheelKeyboardShortcuts(
key: wheelKey,
relativeSize: { w: number; h: number },
// pos relative to the mask
posX: number,
posY: number,
offset: number,
pointerId: number
) {
let loopFlag = false;
let curPosX = 0;
let curPosY = 0;
posX = Math.round((posX / relativeSize.w) * store.screenSizeW);
posY = Math.round((posY / relativeSize.h) * store.screenSizeH);
// calculate the end coordinates of the eight directions of the direction wheel
let offsetHalf = Math.round(offset / 1.414);
const calculatedPosList = [
{ x: posX - offset, y: posY }, // left
{ x: posX + offset, y: posY }, // right
{ x: posX, y: posY - offset }, // up
{ x: posX, y: posY + offset }, // down
{ x: posX - offsetHalf, y: posY - offsetHalf }, // left up
{ x: posX + offsetHalf, y: posY - offsetHalf }, // right up
{ x: posX - offsetHalf, y: posY + offsetHalf }, // left down
{ x: posX + offsetHalf, y: posY + offsetHalf }, // right down
];
// united loop callback for all directions
const unitedloopCB = async () => {
let newPosIndex;
if (downKeyMap.get(key.left)) {
newPosIndex = downKeyMap.get(key.up)
? 4
: downKeyMap.get(key.down)
? 6
: 0;
} else if (downKeyMap.get(key.right)) {
newPosIndex = downKeyMap.get(key.up)
? 5
: downKeyMap.get(key.down)
? 7
: 1;
} else if (downKeyMap.get(key.up)) {
newPosIndex = 2;
} else if (downKeyMap.get(key.down)) {
newPosIndex = 3;
} else {
// all keys released
await unitedUpCB();
return;
}
// if newPos is the same as curPos, return
let newPos = calculatedPosList[newPosIndex];
if (newPos.x === curPosX && newPos.y === curPosY) return;
curPosX = newPos.x;
curPosY = newPos.y;
// move to the direction
await touchX(TouchAction.Move, pointerId, newPos.x, newPos.y);
};
const unitedUpCB = async () => {
if (
downKeyMap.get(key.left) === false &&
downKeyMap.get(key.right) === false &&
downKeyMap.get(key.up) === false &&
downKeyMap.get(key.down) === false
) {
// all wheel keys has been released
for (const k of ["left", "right", "up", "down"]) {
// only delete the valid key
loopDownKeyCBMap.delete(key[k]);
upKeyCBMap.delete(key[k]);
}
// touch up
await touchX(TouchAction.Up, pointerId, curPosX, curPosY);
// recover the status
curPosX = 0;
curPosY = 0;
loopFlag = false;
}
};
for (const k of ["left", "right", "up", "down"]) {
addShortcut(
key[k],
async () => {
if (loopFlag) return;
loopFlag = true;
// add upCB
upKeyCBMap.set(key[k], unitedUpCB);
// touch down on the center position
await touchX(TouchAction.Down, pointerId, posX, posY);
// add loopCB
loopDownKeyCBMap.set(key[k], unitedloopCB);
},
undefined,
undefined
);
}
}
interface wheelKey {
left: string;
right: string;
up: string;
down: string;
[key: string]: string;
}
// add baisc click shortcuts
function addClickShortcuts(key: string, pointerId: number) {
addShortcut(
key,
async () => {
await touchX(
TouchAction.Down,
pointerId,
clientxToPosx(mouseX),
clientyToPosy(mouseY)
);
},
async () => {
await touchX(
TouchAction.Move,
pointerId,
clientxToPosx(mouseX),
clientyToPosy(mouseY)
);
},
async () => {
await touchX(
TouchAction.Up,
pointerId,
clientxToPosx(mouseX),
clientyToPosy(mouseY)
);
}
);
}
// add shortcut for sight mode
function addSightShortcuts(
relativeSize: { w: number; h: number },
sightKeyMapping: KeySight,
fireKeyMapping?: KeyFire
) {
const appWindow = getCurrent();
let mouseLock = false;
let msgReactive: ReturnType<typeof message.info> | null = null;
const sightClientX = 70 + sightKeyMapping.posX;
const sightClientY = 30 + sightKeyMapping.posY;
const sightDeviceX = Math.round(
(sightKeyMapping.posX / relativeSize.w) * store.screenSizeW
);
const sightDeviceY = Math.round(
(sightKeyMapping.posY / relativeSize.h) * store.screenSizeH
);
const fireDeviceX = fireKeyMapping
? Math.round((fireKeyMapping.posX / relativeSize.w) * store.screenSizeW)
: 0;
const fireDeviceY = fireKeyMapping
? Math.round((fireKeyMapping.posY / relativeSize.h) * store.screenSizeH)
: 0;
const removeShortcut = (key: string) => {
loopDownKeyCBMap.delete(key);
downKeyCBMap.delete(key);
upKeyCBMap.delete(key);
downKeyMap.delete(key);
};
const touchRelateToSight = async (action: TouchAction) => {
await touchX(
action,
sightKeyMapping.pointerId,
sightDeviceX +
clientxToPosOffsetx(mouseX, sightDeviceX, sightKeyMapping.scaleX),
sightDeviceY +
clientyToPosOffsety(mouseY, sightDeviceY, sightKeyMapping.scaleY)
);
};
const sightLoopCB = async () => {
await touchRelateToSight(TouchAction.Move);
};
// only scaleX and scaleY are different from sightLoopCB
const fireNoDragLoopCB = fireKeyMapping
? async () => {
await touchX(
TouchAction.Move,
sightKeyMapping.pointerId,
sightDeviceX +
clientxToPosOffsetx(mouseX, sightDeviceX, fireKeyMapping.scaleX),
sightDeviceY +
clientyToPosOffsety(mouseY, sightDeviceY, fireKeyMapping.scaleY)
);
}
: undefined;
const fireDragLoopCB = fireKeyMapping
? async () => {
await touchX(
TouchAction.Move,
fireKeyMapping.pointerId,
fireDeviceX +
accOffsetX +
clientxToPosOffsetx(mouseX, sightDeviceX, fireKeyMapping.scaleX),
fireDeviceY +
accOffsetY +
clientyToPosOffsety(mouseY, sightDeviceY, fireKeyMapping.scaleY)
);
}
: undefined;
let accOffsetX = 0;
let accOffsetY = 0;
const moveLeaveHandler = async () => {
if (fireKeyMapping && fireKeyMapping.drag && downKeyMap.get("M0")) {
// fire drag mode
// stop fireDragLoopCB
loopDownKeyCBMap.delete(sightKeyMapping.key);
// cal accOffset
accOffsetX += clientxToPosOffsetx(
mouseX,
sightDeviceX,
fireKeyMapping.scaleX
);
accOffsetY += clientyToPosOffsety(
mouseY,
sightDeviceY,
fireKeyMapping.scaleY
);
// move mouse
await appWindow.setCursorPosition(
new LogicalPosition(sightClientX, sightClientY)
);
mouseX = sightClientX;
mouseY = sightClientY;
// start fireDragLoopCB
loopDownKeyCBMap.set(sightKeyMapping.key, fireDragLoopCB!);
} else {
// sight mode or fire without drag mode
const fireFlag =
fireKeyMapping && !fireKeyMapping.drag && downKeyMap.get("M0");
// stop sightLoopCB or fireNoDragLoopCB
loopDownKeyCBMap.delete(sightKeyMapping.key);
// touch up
if (fireFlag) {
await touchX(
TouchAction.Move,
sightKeyMapping.pointerId,
sightDeviceX +
clientxToPosOffsetx(mouseX, sightDeviceX, fireKeyMapping.scaleX),
sightDeviceY +
clientyToPosOffsety(mouseY, sightDeviceY, fireKeyMapping.scaleY)
);
} else {
await touchRelateToSight(TouchAction.Up);
}
// move mouse
await appWindow.setCursorPosition(
new LogicalPosition(sightClientX, sightClientY)
);
mouseX = sightClientX;
mouseY = sightClientY;
// touch down
await touchX(
TouchAction.Down,
sightKeyMapping.pointerId,
sightDeviceX,
sightDeviceY
);
// start sightLoopCB or fireNoDragLoopCB
loopDownKeyCBMap.set(
sightKeyMapping.key,
fireFlag ? fireNoDragLoopCB! : sightLoopCB
);
}
};
// add sight shortcut
addShortcut(sightKeyMapping.key, async () => {
if (mouseLock) {
// stop sight mode
// remove box element
const mouseRangeBoxElement = document.getElementById("mouseRangeBox");
if (mouseRangeBoxElement) {
mouseRangeBoxElement.removeEventListener(
"mouseleave",
moveLeaveHandler
);
mouseRangeBoxElement.remove();
}
maskElement.style.cursor = "pointer";
mouseLock = false;
loopDownKeyCBMap.delete(sightKeyMapping.key);
await touchRelateToSight(TouchAction.Up);
await appWindow.setCursorVisible(true);
if (msgReactive) {
msgReactive.destroy();
msgReactive = null;
}
// remove fire key
if (fireKeyMapping) {
removeShortcut("M0");
}
// add click
addClickShortcuts("M0", 0);
} else {
// start sight mode
// create box element
maskElement.style.cursor = "none";
const mouseRangeBoxElement = createMouseRangeBox();
mouseRangeBoxElement.addEventListener("contextmenu", (e) =>
e.preventDefault()
);
mouseRangeBoxElement.addEventListener("mouseleave", moveLeaveHandler);
document.body.appendChild(mouseRangeBoxElement);
mouseLock = true;
await appWindow.setCursorVisible(false);
msgReactive = message.info(
t("pages.Mask.sightMode", [sightKeyMapping.key]),
{
duration: 0,
}
);
await appWindow.setCursorPosition(
new LogicalPosition(sightClientX, sightClientY)
);
mouseX = sightClientX;
mouseY = sightClientY;
await touchX(
TouchAction.Down,
sightKeyMapping.pointerId,
sightDeviceX,
sightDeviceY
);
loopDownKeyCBMap.set(sightKeyMapping.key, sightLoopCB);
// remove click
removeShortcut("M0");
// add fire key
if (fireKeyMapping) {
// fire with drag
addShortcut(
"M0",
async () => {
// stop sightLoopCB
loopDownKeyCBMap.delete(sightKeyMapping.key);
// touch up sight
await touchRelateToSight(TouchAction.Up);
if (!fireKeyMapping.drag) {
// touch down sight
await touchX(
TouchAction.Down,
sightKeyMapping.pointerId,
sightDeviceX,
sightDeviceY
);
} else {
// clear accumulated offset
accOffsetX = 0;
accOffsetY = 0;
}
// move cursor
await appWindow.setCursorPosition(
new LogicalPosition(sightClientX, sightClientY)
);
mouseX = sightClientX;
mouseY = sightClientY;
// touch down fire
await touchX(
TouchAction.Down,
fireKeyMapping.pointerId,
fireDeviceX,
fireDeviceY
);
// start fireDragLoopCB or fireNoDragLoopCB
loopDownKeyCBMap.set(
sightKeyMapping.key,
fireKeyMapping.drag ? fireDragLoopCB! : fireNoDragLoopCB!
);
},
undefined,
async () => {
// stop fireDragLoopCB or fireNoDragLoopCB
loopDownKeyCBMap.delete(sightKeyMapping.key);
// touch up fire
await touchX(
TouchAction.Up,
fireKeyMapping.pointerId,
fireDeviceX +
clientxToPosOffsetx(
mouseX,
sightDeviceX,
fireKeyMapping.scaleX
),
fireDeviceY +
clientyToPosOffsety(mouseY, sightDeviceY, fireKeyMapping.scaleY)
);
// touch down sight
await touchX(
TouchAction.Down,
sightKeyMapping.pointerId,
sightDeviceX,
sightDeviceY
);
// move cursor
await appWindow.setCursorPosition(
new LogicalPosition(sightClientX, sightClientY)
);
mouseX = sightClientX;
mouseY = sightClientY;
// start sightLoopCB
loopDownKeyCBMap.set(sightKeyMapping.key, sightLoopCB);
}
);
}
}
});
}
function addSwipeShortcuts(
key: string,
relativeSize: { w: number; h: number },
// pos relative to the mask
pos: { x: number; y: number }[],
pointerId: number,
intervalBetweenPos: number
) {
const newPosList = pos.map((posObj) => {
return {
x: Math.round((posObj.x / relativeSize.w) * store.screenSizeW),
y: Math.round((posObj.y / relativeSize.h) * store.screenSizeH),
};
});
addShortcut(
key,
async () => {
await swipe({
action: SwipeAction.Default,
pointerId,
screen: {
w: store.screenSizeW,
h: store.screenSizeH,
},
pos: newPosList,
intervalBetweenPos,
});
},
undefined,
undefined
);
}
function createMouseRangeBox(): HTMLElement {
const box = document.createElement("div");
box.id = "mouseRangeBox";
box.style.position = "absolute";
box.style.top = "40px";
box.style.bottom = "40px";
box.style.left = "100px";
box.style.right = "100px";
box.style.zIndex = "9999";
box.style.backgroundColor = "transparent";
box.style.cursor = "none";
return box;
}
function handleKeydown(event: KeyboardEvent) {