2024-04-13 09:53:41 +08:00
|
|
|
// https://github.com/jamiebuilds/tinykeys/pull/193/commits/2598ecb3db6b3948c7acbf0e7bd8b0674961ad61
|
2024-05-17 22:30:58 +08:00
|
|
|
import { useMessage } from "naive-ui";
|
2024-04-13 09:53:41 +08:00
|
|
|
import {
|
|
|
|
SwipeAction,
|
|
|
|
TouchAction,
|
|
|
|
swipe,
|
|
|
|
touch,
|
|
|
|
} from "./frontcommand/scrcpyMaskCmd";
|
2024-04-26 23:07:26 +08:00
|
|
|
import {
|
|
|
|
KeyCancelSkill,
|
|
|
|
KeyDirectionalSkill,
|
|
|
|
KeyDirectionlessSkill,
|
|
|
|
KeyMacro,
|
|
|
|
KeyMacroList,
|
|
|
|
KeyMappingConfig,
|
|
|
|
KeyObservation,
|
|
|
|
KeySteeringWheel,
|
|
|
|
KeyTap,
|
2024-05-05 19:24:50 +08:00
|
|
|
KeyTriggerWhenDoublePressedSkill,
|
2024-04-26 23:07:26 +08:00
|
|
|
KeyTriggerWhenPressedSkill,
|
|
|
|
} from "./keyMappingConfig";
|
2024-05-07 10:20:44 +08:00
|
|
|
import { useGlobalStore } from "./store/global";
|
2024-05-17 22:30:58 +08:00
|
|
|
import { LogicalPosition, getCurrent } from "@tauri-apps/api/window";
|
2024-04-13 09:53:41 +08:00
|
|
|
|
|
|
|
function clientxToPosx(clientx: number) {
|
2024-04-16 22:38:07 +08:00
|
|
|
return clientx < 70
|
|
|
|
? 0
|
|
|
|
: Math.floor((clientx - 70) * (screenSizeW / maskSizeW));
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function clientyToPosy(clienty: number) {
|
2024-04-16 22:38:07 +08:00
|
|
|
return clienty < 30
|
|
|
|
? 0
|
|
|
|
: Math.floor((clienty - 30) * (screenSizeH / maskSizeH));
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|
|
|
|
|
2024-04-30 13:41:04 +08:00
|
|
|
function clientxToPosOffsetx(clientx: number, posx: number, scale = 1) {
|
2024-04-14 17:19:45 +08:00
|
|
|
let offsetX = clientxToPosx(clientx) - posx;
|
|
|
|
return Math.round(offsetX * scale);
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|
|
|
|
|
2024-04-30 13:41:04 +08:00
|
|
|
function clientyToPosOffsety(clienty: number, posy: number, scale = 1) {
|
2024-04-14 17:19:45 +08:00
|
|
|
let offsetY = clientyToPosy(clienty) - posy;
|
|
|
|
return Math.round(offsetY * scale);
|
|
|
|
}
|
|
|
|
|
2024-04-30 13:41:04 +08:00
|
|
|
function clientPosToSkillOffset(
|
|
|
|
clientPos: { x: number; y: number },
|
|
|
|
range: number
|
|
|
|
): { offsetX: number; offsetY: number } {
|
2024-05-05 16:49:10 +08:00
|
|
|
const maxLength = (120 / maskSizeH) * screenSizeH;
|
2024-04-30 13:41:04 +08:00
|
|
|
const centerX = maskSizeW * 0.5;
|
2024-05-02 16:10:41 +08:00
|
|
|
const centerY = maskSizeH * 0.5;
|
|
|
|
|
|
|
|
// The center of the game display is higher than the center of the mask
|
|
|
|
clientPos.y -= maskSizeH * 0.066;
|
|
|
|
|
2024-05-03 18:43:25 +08:00
|
|
|
// w450 : h315 = 100 : 70, so the true offsetX is 0.7 * cOffsetX
|
|
|
|
const cOffsetX = (clientPos.x - 70 - centerX) * 0.7;
|
2024-04-30 13:41:04 +08:00
|
|
|
const cOffsetY = clientPos.y - 30 - centerY;
|
|
|
|
const offsetD = Math.sqrt(cOffsetX ** 2 + cOffsetY ** 2);
|
|
|
|
if (offsetD == 0) {
|
|
|
|
return {
|
|
|
|
offsetX: 0,
|
|
|
|
offsetY: 0,
|
|
|
|
};
|
|
|
|
}
|
2024-04-14 17:19:45 +08:00
|
|
|
|
2024-04-30 13:41:04 +08:00
|
|
|
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),
|
|
|
|
};
|
|
|
|
}
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function sleep(ms: number) {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
}
|
|
|
|
|
2024-04-17 10:17:39 +08:00
|
|
|
function calculateMacroPosX(
|
2024-05-02 13:15:11 +08:00
|
|
|
// pos relative to the mask
|
2024-04-17 10:17:39 +08:00
|
|
|
posX: [string, number] | number,
|
|
|
|
relativeSizeW: number
|
|
|
|
): number {
|
|
|
|
if (typeof posX === "number") {
|
|
|
|
return Math.round(posX * (screenSizeW / relativeSizeW));
|
2024-04-14 17:19:45 +08:00
|
|
|
}
|
2024-04-17 10:17:39 +08:00
|
|
|
if (typeof posX === "string") {
|
2024-04-14 17:19:45 +08:00
|
|
|
return clientxToPosx(mouseX);
|
|
|
|
} else {
|
2024-04-17 10:17:39 +08:00
|
|
|
if (posX[0] === "mouse") {
|
|
|
|
return (
|
|
|
|
clientxToPosx(mouseX) +
|
|
|
|
Math.round(posX[1] * (screenSizeW / relativeSizeW))
|
|
|
|
);
|
2024-04-14 17:19:45 +08:00
|
|
|
} else {
|
|
|
|
throw new Error("Invalid pos");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 10:17:39 +08:00
|
|
|
function calculateMacroPosY(
|
2024-05-02 13:15:11 +08:00
|
|
|
// pos relative to the mask
|
2024-04-17 10:17:39 +08:00
|
|
|
posY: [string, number] | number,
|
|
|
|
relativeSizeH: number
|
|
|
|
): number {
|
|
|
|
if (typeof posY === "number") {
|
|
|
|
return Math.round(posY * (screenSizeH / relativeSizeH));
|
2024-04-14 17:19:45 +08:00
|
|
|
}
|
2024-04-17 10:17:39 +08:00
|
|
|
if (typeof posY === "string") {
|
2024-04-14 17:19:45 +08:00
|
|
|
return clientyToPosy(mouseY);
|
|
|
|
} else {
|
2024-04-17 10:17:39 +08:00
|
|
|
if (posY[0] === "mouse") {
|
|
|
|
return (
|
|
|
|
clientyToPosy(mouseY) +
|
|
|
|
Math.round(posY[1] * (screenSizeH / relativeSizeH))
|
|
|
|
);
|
2024-04-14 17:19:45 +08:00
|
|
|
} else {
|
|
|
|
throw new Error("Invalid pos");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function calculateMacroPosList(
|
2024-04-17 10:17:39 +08:00
|
|
|
posList: [[string, number] | number, [string, number] | number][],
|
|
|
|
relativeSize: { w: number; h: number }
|
2024-04-14 17:19:45 +08:00
|
|
|
): { x: number; y: number }[] {
|
|
|
|
return posList.map((posPair) => {
|
|
|
|
return {
|
2024-04-17 10:17:39 +08:00
|
|
|
x: calculateMacroPosX(posPair[0], relativeSize.w),
|
|
|
|
y: calculateMacroPosY(posPair[1], relativeSize.h),
|
2024-04-14 17:19:45 +08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// add shortcuts for observation
|
|
|
|
function addObservationShortcuts(
|
|
|
|
key: string,
|
2024-04-17 10:17:39 +08:00
|
|
|
relativeSize: { w: number; h: number },
|
2024-04-14 17:19:45 +08:00
|
|
|
posX: number,
|
|
|
|
posY: number,
|
|
|
|
scale: number,
|
|
|
|
pointerId: number
|
|
|
|
) {
|
|
|
|
let observationMouseX = 0;
|
|
|
|
let observationMouseY = 0;
|
2024-04-17 10:17:39 +08:00
|
|
|
posX = Math.round((posX / relativeSize.w) * screenSizeW);
|
|
|
|
posY = Math.round((posY / relativeSize.h) * screenSizeH);
|
2024-04-14 17:19:45 +08:00
|
|
|
addShortcut(
|
|
|
|
key,
|
|
|
|
async () => {
|
|
|
|
observationMouseX = clientxToPosx(mouseX);
|
|
|
|
observationMouseY = clientyToPosy(mouseY);
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(TouchAction.Down, pointerId, posX, posY);
|
2024-04-14 17:19:45 +08:00
|
|
|
},
|
|
|
|
async () => {
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(
|
|
|
|
TouchAction.Move,
|
2024-04-14 17:19:45 +08:00
|
|
|
pointerId,
|
2024-05-17 22:30:58 +08:00
|
|
|
posX + clientxToPosOffsetx(mouseX, observationMouseX, scale),
|
|
|
|
posY + clientyToPosOffsety(mouseY, observationMouseY, scale)
|
|
|
|
);
|
2024-04-14 17:19:45 +08:00
|
|
|
},
|
|
|
|
async () => {
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(
|
|
|
|
TouchAction.Up,
|
2024-04-14 17:19:45 +08:00
|
|
|
pointerId,
|
2024-05-17 22:30:58 +08:00
|
|
|
posX + clientxToPosOffsetx(mouseX, observationMouseY, scale),
|
|
|
|
posY + clientyToPosOffsety(mouseY, observationMouseY, scale)
|
|
|
|
);
|
2024-04-14 17:19:45 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-27 21:45:33 +08:00
|
|
|
// add shortcuts for simple tap (touch when press down)
|
2024-04-14 17:19:45 +08:00
|
|
|
function addTapShortcuts(
|
|
|
|
key: string,
|
2024-04-17 10:17:39 +08:00
|
|
|
relativeSize: { w: number; h: number },
|
2024-04-19 16:30:29 +08:00
|
|
|
time: number,
|
2024-04-14 17:19:45 +08:00
|
|
|
posX: number,
|
|
|
|
posY: number,
|
|
|
|
pointerId: number
|
|
|
|
) {
|
2024-04-17 10:17:39 +08:00
|
|
|
posX = Math.round((posX / relativeSize.w) * screenSizeW);
|
|
|
|
posY = Math.round((posY / relativeSize.h) * screenSizeH);
|
2024-04-14 17:19:45 +08:00
|
|
|
addShortcut(
|
|
|
|
key,
|
|
|
|
async () => {
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(TouchAction.Default, pointerId, posX, posY, time);
|
2024-04-14 17:19:45 +08:00
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add shortcuts for cancel skill
|
|
|
|
function addCancelSkillShortcuts(
|
|
|
|
key: string,
|
2024-04-17 10:17:39 +08:00
|
|
|
relativeSize: { w: number; h: number },
|
2024-04-14 17:19:45 +08:00
|
|
|
posX: number,
|
|
|
|
posY: number,
|
|
|
|
pointerId: number
|
|
|
|
) {
|
2024-04-17 10:17:39 +08:00
|
|
|
posX = Math.round((posX / relativeSize.w) * screenSizeW);
|
|
|
|
posY = Math.round((posY / relativeSize.h) * screenSizeH);
|
2024-04-14 17:19:45 +08:00
|
|
|
addShortcut(
|
|
|
|
key,
|
|
|
|
async () => {
|
|
|
|
// delete the callback
|
|
|
|
for (const cancelAbleKey of cancelAbleKeyList) {
|
|
|
|
loopDownKeyCBMap.delete(cancelAbleKey);
|
|
|
|
upKeyCBMap.delete(cancelAbleKey);
|
|
|
|
}
|
2024-05-06 09:12:43 +08:00
|
|
|
// special case for double press skill
|
|
|
|
for (const [key, val] of doublePressedDownKey) {
|
|
|
|
if (val) {
|
|
|
|
loopDownKeyCBMap.delete(key);
|
|
|
|
doublePressedDownKey.set(key, false);
|
|
|
|
}
|
|
|
|
}
|
2024-04-14 17:19:45 +08:00
|
|
|
let distance = 0;
|
|
|
|
while (distance <= 20) {
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(TouchAction.Move, pointerId, posX + distance, posY);
|
2024-04-14 17:19:45 +08:00
|
|
|
await sleep(5);
|
|
|
|
distance += 1;
|
|
|
|
}
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(TouchAction.Up, pointerId, posX, posY);
|
2024-04-14 17:19:45 +08:00
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
}
|
2024-04-13 09:53:41 +08:00
|
|
|
|
|
|
|
// add shortcuts for trigger when pressed skill
|
|
|
|
function addTriggerWhenPressedSkillShortcuts(
|
|
|
|
key: string,
|
2024-04-17 10:17:39 +08:00
|
|
|
relativeSize: { w: number; h: number },
|
2024-04-13 09:53:41 +08:00
|
|
|
// pos relative to the device
|
|
|
|
posX: number,
|
|
|
|
posY: number,
|
|
|
|
directional: boolean,
|
2024-04-19 18:32:42 +08:00
|
|
|
// range: when directional is true
|
|
|
|
// time: when directional is false
|
|
|
|
rangeOrTime: number,
|
2024-04-13 09:53:41 +08:00
|
|
|
pointerId: number
|
|
|
|
) {
|
2024-05-12 21:30:45 +08:00
|
|
|
posX = Math.round((posX / relativeSize.w) * screenSizeW);
|
|
|
|
posY = Math.round((posY / relativeSize.h) * screenSizeH);
|
2024-04-13 09:53:41 +08:00
|
|
|
if (directional) {
|
|
|
|
addShortcut(
|
|
|
|
key,
|
|
|
|
// down
|
|
|
|
async () => {
|
2024-05-06 09:12:43 +08:00
|
|
|
// up doublepress skill
|
2024-05-12 21:30:45 +08:00
|
|
|
await upDoublePressedKey();
|
2024-04-30 13:41:04 +08:00
|
|
|
const skillOffset = clientPosToSkillOffset(
|
|
|
|
{ x: mouseX, y: mouseY },
|
|
|
|
rangeOrTime
|
|
|
|
);
|
2024-04-13 09:53:41 +08:00
|
|
|
await swipe({
|
|
|
|
action: SwipeAction.Default,
|
|
|
|
pointerId,
|
|
|
|
screen: {
|
|
|
|
w: screenSizeW,
|
|
|
|
h: screenSizeH,
|
|
|
|
},
|
|
|
|
pos: [
|
|
|
|
{ x: posX, y: posY },
|
|
|
|
{
|
2024-04-30 13:41:04 +08:00
|
|
|
x: posX + skillOffset.offsetX,
|
|
|
|
y: posY + skillOffset.offsetY,
|
2024-04-13 09:53:41 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
intervalBetweenPos: 0,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
} else {
|
2024-05-12 21:30:45 +08:00
|
|
|
addShortcut(
|
|
|
|
key,
|
|
|
|
async () => {
|
|
|
|
await upDoublePressedKey();
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(TouchAction.Default, pointerId, posX, posY, rangeOrTime);
|
2024-05-12 21:30:45 +08:00
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
undefined
|
|
|
|
);
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 09:12:43 +08:00
|
|
|
// add shortcuts for trigger when double pressed skill (cancelable, but in another way)
|
|
|
|
const doublePressedDownKey = new Map<string, boolean>();
|
2024-05-05 19:24:50 +08:00
|
|
|
function addTriggerWhenDoublePressedSkillShortcuts(
|
|
|
|
key: string,
|
|
|
|
relativeSize: { w: number; h: number },
|
|
|
|
// pos relative to the device
|
|
|
|
posX: number,
|
|
|
|
posY: number,
|
|
|
|
range: number,
|
|
|
|
pointerId: number
|
|
|
|
) {
|
|
|
|
posX = Math.round((posX / relativeSize.w) * screenSizeW);
|
|
|
|
posY = Math.round((posY / relativeSize.h) * screenSizeH);
|
2024-05-06 09:12:43 +08:00
|
|
|
doublePressedDownKey.set(key, false);
|
2024-05-05 19:24:50 +08:00
|
|
|
addShortcut(
|
|
|
|
key,
|
|
|
|
// down
|
|
|
|
async () => {
|
2024-05-06 09:12:43 +08:00
|
|
|
if (doublePressedDownKey.get(key) === false) {
|
2024-05-05 19:24:50 +08:00
|
|
|
// first press: touch down
|
|
|
|
const skillOffset = clientPosToSkillOffset(
|
|
|
|
{ x: mouseX, y: mouseY },
|
|
|
|
range
|
|
|
|
);
|
|
|
|
await swipe({
|
|
|
|
action: SwipeAction.NoUp,
|
|
|
|
pointerId,
|
|
|
|
screen: {
|
|
|
|
w: screenSizeW,
|
|
|
|
h: screenSizeH,
|
|
|
|
},
|
|
|
|
pos: [
|
|
|
|
{ x: posX, y: posY },
|
|
|
|
{
|
|
|
|
x: posX + skillOffset.offsetX,
|
|
|
|
y: posY + skillOffset.offsetY,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
intervalBetweenPos: 0,
|
|
|
|
});
|
|
|
|
// set the flag to true
|
2024-05-06 09:12:43 +08:00
|
|
|
doublePressedDownKey.set(key, true);
|
2024-05-05 19:24:50 +08:00
|
|
|
// add loop CB
|
|
|
|
loopDownKeyCBMap.set(key, async () => {
|
|
|
|
const loopSkillOffset = clientPosToSkillOffset(
|
|
|
|
{ x: mouseX, y: mouseY },
|
|
|
|
range
|
|
|
|
);
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(
|
|
|
|
TouchAction.Move,
|
2024-05-05 19:24:50 +08:00
|
|
|
pointerId,
|
2024-05-17 22:30:58 +08:00
|
|
|
posX + loopSkillOffset.offsetX,
|
|
|
|
posY + loopSkillOffset.offsetY
|
|
|
|
);
|
2024-05-05 19:24:50 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// second press: touch up
|
|
|
|
// delete the loop CB
|
|
|
|
loopDownKeyCBMap.delete(key);
|
|
|
|
const skillOffset = clientPosToSkillOffset(
|
|
|
|
{ x: mouseX, y: mouseY },
|
|
|
|
range
|
|
|
|
);
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(
|
|
|
|
TouchAction.Up,
|
2024-05-05 19:24:50 +08:00
|
|
|
pointerId,
|
2024-05-17 22:30:58 +08:00
|
|
|
posX + skillOffset.offsetX,
|
|
|
|
posY + skillOffset.offsetY
|
|
|
|
);
|
2024-05-05 19:24:50 +08:00
|
|
|
// set the flag to false
|
2024-05-06 09:12:43 +08:00
|
|
|
doublePressedDownKey.set(key, false);
|
2024-05-05 19:24:50 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-14 17:19:45 +08:00
|
|
|
// add shortcuts for directionless skill (cancelable)
|
2024-04-13 09:53:41 +08:00
|
|
|
function addDirectionlessSkillShortcuts(
|
|
|
|
key: string,
|
2024-04-17 10:17:39 +08:00
|
|
|
relativeSize: { w: number; h: number },
|
2024-04-13 09:53:41 +08:00
|
|
|
// pos relative to the device
|
|
|
|
posX: number,
|
|
|
|
posY: number,
|
|
|
|
pointerId: number
|
|
|
|
) {
|
2024-04-17 10:17:39 +08:00
|
|
|
posX = Math.round((posX / relativeSize.w) * screenSizeW);
|
|
|
|
posY = Math.round((posY / relativeSize.h) * screenSizeH);
|
2024-04-13 09:53:41 +08:00
|
|
|
addShortcut(
|
|
|
|
key,
|
|
|
|
// down
|
|
|
|
async () => {
|
2024-05-06 09:12:43 +08:00
|
|
|
// up doublepress skill
|
2024-05-12 21:30:45 +08:00
|
|
|
await upDoublePressedKey();
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(TouchAction.Down, pointerId, posX, posY);
|
2024-04-13 09:53:41 +08:00
|
|
|
},
|
|
|
|
// loop
|
|
|
|
undefined,
|
|
|
|
// up
|
|
|
|
async () => {
|
|
|
|
await touch({
|
|
|
|
action: TouchAction.Up,
|
|
|
|
pointerId,
|
|
|
|
screen: {
|
|
|
|
w: screenSizeW,
|
|
|
|
h: screenSizeH,
|
|
|
|
},
|
|
|
|
pos: {
|
|
|
|
x: posX,
|
|
|
|
y: posY,
|
|
|
|
},
|
|
|
|
});
|
2024-04-14 17:19:45 +08:00
|
|
|
},
|
|
|
|
true
|
2024-04-13 09:53:41 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-12 21:30:45 +08:00
|
|
|
// up all double pressed key
|
|
|
|
async function upDoublePressedKey() {
|
|
|
|
for (const [key, val] of doublePressedDownKey) {
|
|
|
|
if (val) {
|
|
|
|
await downKeyCBMap.get(key)?.();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-14 17:19:45 +08:00
|
|
|
// add shortcuts for directional skill (cancelable)
|
2024-04-13 09:53:41 +08:00
|
|
|
function addDirectionalSkillShortcuts(
|
|
|
|
key: string,
|
2024-04-17 10:17:39 +08:00
|
|
|
relativeSize: { w: number; h: number },
|
2024-04-13 09:53:41 +08:00
|
|
|
// pos relative to the device
|
|
|
|
posX: number,
|
|
|
|
posY: number,
|
|
|
|
range: number,
|
|
|
|
pointerId: number
|
|
|
|
) {
|
2024-04-17 10:17:39 +08:00
|
|
|
posX = Math.round((posX / relativeSize.w) * screenSizeW);
|
|
|
|
posY = Math.round((posY / relativeSize.h) * screenSizeH);
|
2024-04-13 09:53:41 +08:00
|
|
|
addShortcut(
|
|
|
|
key,
|
|
|
|
// down
|
|
|
|
async () => {
|
2024-05-06 09:12:43 +08:00
|
|
|
// up doublepress skill
|
2024-05-12 21:30:45 +08:00
|
|
|
await upDoublePressedKey();
|
2024-04-30 13:41:04 +08:00
|
|
|
const skillOffset = clientPosToSkillOffset(
|
|
|
|
{ x: mouseX, y: mouseY },
|
|
|
|
range
|
|
|
|
);
|
2024-04-13 09:53:41 +08:00
|
|
|
await swipe({
|
|
|
|
action: SwipeAction.NoUp,
|
|
|
|
pointerId,
|
|
|
|
screen: {
|
|
|
|
w: screenSizeW,
|
|
|
|
h: screenSizeH,
|
|
|
|
},
|
|
|
|
pos: [
|
|
|
|
{ x: posX, y: posY },
|
|
|
|
{
|
2024-04-30 13:41:04 +08:00
|
|
|
x: posX + skillOffset.offsetX,
|
|
|
|
y: posY + skillOffset.offsetY,
|
2024-04-13 09:53:41 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
intervalBetweenPos: 0,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
// loop
|
|
|
|
async () => {
|
2024-04-30 13:41:04 +08:00
|
|
|
const skillOffset = clientPosToSkillOffset(
|
|
|
|
{ x: mouseX, y: mouseY },
|
|
|
|
range
|
|
|
|
);
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(
|
|
|
|
TouchAction.Move,
|
2024-04-13 09:53:41 +08:00
|
|
|
pointerId,
|
2024-05-17 22:30:58 +08:00
|
|
|
posX + skillOffset.offsetX,
|
|
|
|
posY + skillOffset.offsetY
|
|
|
|
);
|
2024-04-13 09:53:41 +08:00
|
|
|
},
|
|
|
|
// up
|
|
|
|
async () => {
|
2024-04-30 13:41:04 +08:00
|
|
|
const skillOffset = clientPosToSkillOffset(
|
|
|
|
{ x: mouseX, y: mouseY },
|
|
|
|
range
|
|
|
|
);
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(
|
|
|
|
TouchAction.Up,
|
2024-04-13 09:53:41 +08:00
|
|
|
pointerId,
|
2024-05-17 22:30:58 +08:00
|
|
|
posX + skillOffset.offsetX,
|
|
|
|
posY + skillOffset.offsetY
|
|
|
|
);
|
2024-04-14 17:19:45 +08:00
|
|
|
},
|
|
|
|
true
|
2024-04-13 09:53:41 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add shortcuts for steering wheel
|
|
|
|
function addSteeringWheelKeyboardShortcuts(
|
|
|
|
key: wheelKey,
|
2024-04-17 10:17:39 +08:00
|
|
|
relativeSize: { w: number; h: number },
|
2024-04-13 09:53:41 +08:00
|
|
|
// pos relative to the device
|
|
|
|
posX: number,
|
|
|
|
posY: number,
|
|
|
|
offset: number,
|
|
|
|
pointerId: number
|
|
|
|
) {
|
2024-04-14 21:29:28 +08:00
|
|
|
let loopFlag = false;
|
|
|
|
let curPosX = 0;
|
|
|
|
let curPosY = 0;
|
2024-04-17 10:17:39 +08:00
|
|
|
posX = Math.round((posX / relativeSize.w) * screenSizeW);
|
|
|
|
posY = Math.round((posY / relativeSize.h) * screenSizeH);
|
2024-04-13 09:53:41 +08:00
|
|
|
|
|
|
|
// calculate the end coordinates of the eight directions of the direction wheel
|
|
|
|
let offsetHalf = Math.round(offset / 1.414);
|
2024-04-14 21:29:28 +08:00
|
|
|
const calculatedPosList = [
|
2024-04-13 09:53:41 +08:00
|
|
|
{ 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
|
|
|
|
];
|
|
|
|
|
2024-04-14 21:29:28 +08:00
|
|
|
// 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;
|
2024-04-13 09:53:41 +08:00
|
|
|
} else {
|
2024-04-14 21:29:28 +08:00
|
|
|
// all keys released
|
|
|
|
await unitedUpCB();
|
|
|
|
return;
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|
2024-04-14 21:29:28 +08:00
|
|
|
// 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
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(TouchAction.Move, pointerId, newPos.x, newPos.y);
|
2024-04-14 21:29:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(TouchAction.Up, pointerId, curPosX, curPosY);
|
2024-04-14 21:29:28 +08:00
|
|
|
// 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
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(TouchAction.Down, pointerId, posX, posY);
|
2024-04-14 21:29:28 +08:00
|
|
|
// add loopCB
|
|
|
|
loopDownKeyCBMap.set(key[k], unitedloopCB);
|
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
undefined
|
|
|
|
);
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|
|
|
|
}
|
2024-04-14 21:29:28 +08:00
|
|
|
|
2024-04-13 09:53:41 +08:00
|
|
|
interface wheelKey {
|
|
|
|
left: string;
|
|
|
|
right: string;
|
|
|
|
up: string;
|
|
|
|
down: string;
|
|
|
|
[key: string]: string;
|
|
|
|
}
|
|
|
|
|
2024-04-14 17:19:45 +08:00
|
|
|
// add baisc click shortcuts
|
|
|
|
function addClickShortcuts(key: string, pointerId: number) {
|
2024-04-13 09:53:41 +08:00
|
|
|
addShortcut(
|
2024-04-14 17:19:45 +08:00
|
|
|
key,
|
2024-04-13 09:53:41 +08:00
|
|
|
async () => {
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(
|
|
|
|
TouchAction.Down,
|
2024-04-13 09:53:41 +08:00
|
|
|
pointerId,
|
2024-05-17 22:30:58 +08:00
|
|
|
clientxToPosx(mouseX),
|
|
|
|
clientyToPosy(mouseY)
|
|
|
|
);
|
2024-04-13 09:53:41 +08:00
|
|
|
},
|
|
|
|
async () => {
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(
|
|
|
|
TouchAction.Move,
|
2024-04-13 09:53:41 +08:00
|
|
|
pointerId,
|
2024-05-17 22:30:58 +08:00
|
|
|
clientxToPosx(mouseX),
|
|
|
|
clientyToPosy(mouseY)
|
|
|
|
);
|
2024-04-13 09:53:41 +08:00
|
|
|
},
|
|
|
|
async () => {
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(
|
|
|
|
TouchAction.Up,
|
2024-04-13 09:53:41 +08:00
|
|
|
pointerId,
|
2024-05-17 22:30:58 +08:00
|
|
|
clientxToPosx(mouseX),
|
|
|
|
clientyToPosy(mouseY)
|
|
|
|
);
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-17 22:30:58 +08:00
|
|
|
function addFrontSightShortcuts() {
|
|
|
|
// TODO 1. 补上开火键 2. i18n 3. 单独函数,同时配合可视化组件 4. 组件配置中唯一
|
|
|
|
const appWindow = getCurrent();
|
|
|
|
|
|
|
|
// TODO cal it
|
|
|
|
const hasFireKey = true;
|
|
|
|
|
|
|
|
const fireKeyMapping = {
|
|
|
|
type: "Fire",
|
|
|
|
pointerId: 2,
|
|
|
|
note: "开火键",
|
|
|
|
posX: 300,
|
|
|
|
posY: 300,
|
|
|
|
drag: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mouseLock = false;
|
|
|
|
let msgReactive: ReturnType<typeof message.info> | null = null;
|
|
|
|
const key = "KeyH";
|
|
|
|
const centerX = 70 + maskSizeW / 2;
|
|
|
|
const centerY = 30 + maskSizeH / 2;
|
|
|
|
const centerPosX = screenSizeW / 2;
|
|
|
|
const centerPosY = screenSizeH / 2;
|
|
|
|
const scaleX = 0.5;
|
|
|
|
const scaleY = 0.5;
|
|
|
|
|
|
|
|
const removeShortcut = (key: string) => {
|
|
|
|
loopDownKeyCBMap.delete(key);
|
|
|
|
downKeyCBMap.delete(key);
|
|
|
|
upKeyCBMap.delete(key);
|
|
|
|
downKeyMap.delete(key);
|
|
|
|
};
|
|
|
|
|
|
|
|
const touchRelateToCenter = async (action: TouchAction) => {
|
|
|
|
await touchX(
|
|
|
|
action,
|
|
|
|
0,
|
|
|
|
centerPosX + clientxToPosOffsetx(mouseX, centerPosX, scaleX),
|
|
|
|
centerPosY + clientyToPosOffsety(mouseY, centerPosY, scaleY)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const sightLoopCB = async () => {
|
|
|
|
await touchRelateToCenter(TouchAction.Move);
|
|
|
|
};
|
|
|
|
|
|
|
|
const moveLeaveHandler = async () => {
|
|
|
|
// stop loop touch move
|
|
|
|
loopDownKeyCBMap.delete(key);
|
|
|
|
// touch up
|
|
|
|
await touchRelateToCenter(TouchAction.Up);
|
|
|
|
await sleep(150);
|
|
|
|
// move mouse
|
|
|
|
appWindow.setCursorPosition(new LogicalPosition(centerX, centerY));
|
|
|
|
mouseX = centerX;
|
|
|
|
mouseY = centerY;
|
|
|
|
// touch down
|
|
|
|
touchX(TouchAction.Down, 0, centerPosX, centerPosY);
|
|
|
|
// start loop touch move
|
|
|
|
loopDownKeyCBMap.set(key, sightLoopCB);
|
|
|
|
};
|
|
|
|
|
|
|
|
// add sight shortcut
|
|
|
|
addShortcut(key, async () => {
|
|
|
|
if (mouseLock) {
|
|
|
|
// stop sight mode
|
|
|
|
loopDownKeyCBMap.delete(key);
|
|
|
|
await touchRelateToCenter(TouchAction.Up);
|
|
|
|
await appWindow.setCursorVisible(true);
|
|
|
|
maskElement.removeEventListener("mouseleave", moveLeaveHandler);
|
|
|
|
mouseLock = false;
|
|
|
|
if (msgReactive) {
|
|
|
|
msgReactive.destroy();
|
|
|
|
msgReactive = null;
|
|
|
|
}
|
|
|
|
// remove fire key
|
|
|
|
if (hasFireKey) {
|
|
|
|
removeShortcut("M0");
|
|
|
|
}
|
|
|
|
// add click
|
|
|
|
addClickShortcuts("M0", 0);
|
|
|
|
} else {
|
|
|
|
// start sight mode
|
|
|
|
await appWindow.setCursorVisible(false);
|
|
|
|
maskElement.addEventListener("mouseleave", moveLeaveHandler);
|
|
|
|
mouseLock = true;
|
|
|
|
msgReactive = message.info(`鼠标已锁定, 按 ${key} 键解锁`, {
|
|
|
|
duration: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
await touchX(TouchAction.Down, 0, centerPosX, centerPosY);
|
|
|
|
await appWindow.setCursorPosition(new LogicalPosition(centerX, centerY));
|
|
|
|
mouseX = centerX;
|
|
|
|
mouseY = centerY;
|
|
|
|
loopDownKeyCBMap.set(key, sightLoopCB);
|
|
|
|
// remove click
|
|
|
|
removeShortcut("M0");
|
|
|
|
// add fire key
|
|
|
|
if (hasFireKey) {
|
|
|
|
if (fireKeyMapping.drag) {
|
|
|
|
addShortcut(
|
|
|
|
"M0",
|
|
|
|
async () => {
|
|
|
|
// stop sight loop
|
|
|
|
loopDownKeyCBMap.delete(key);
|
|
|
|
await touchRelateToCenter(TouchAction.Up);
|
|
|
|
// move cursor to center
|
|
|
|
await appWindow.setCursorPosition(
|
|
|
|
new LogicalPosition(centerX, centerY)
|
|
|
|
);
|
|
|
|
mouseX = centerX;
|
|
|
|
mouseY = centerY;
|
|
|
|
// touch down fire
|
|
|
|
await touchX(
|
|
|
|
TouchAction.Down,
|
|
|
|
fireKeyMapping.pointerId,
|
|
|
|
fireKeyMapping.posX,
|
|
|
|
fireKeyMapping.posY
|
|
|
|
);
|
|
|
|
},
|
|
|
|
async () => {
|
|
|
|
await touchX(
|
|
|
|
TouchAction.Move,
|
|
|
|
fireKeyMapping.pointerId,
|
|
|
|
fireKeyMapping.posX +
|
|
|
|
clientxToPosOffsetx(mouseX, centerPosX, scaleX),
|
|
|
|
fireKeyMapping.posY +
|
|
|
|
clientyToPosOffsety(mouseY, centerPosY, scaleY)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
async () => {
|
|
|
|
// touch up fire
|
|
|
|
await touchX(
|
|
|
|
TouchAction.Up,
|
|
|
|
fireKeyMapping.pointerId,
|
|
|
|
fireKeyMapping.posX +
|
|
|
|
clientxToPosOffsetx(mouseX, centerPosX, scaleX),
|
|
|
|
fireKeyMapping.posY +
|
|
|
|
clientyToPosOffsety(mouseY, centerPosY, scaleY)
|
|
|
|
);
|
|
|
|
// start sight loop
|
|
|
|
await touchX(TouchAction.Down, 0, centerPosX, centerPosY);
|
|
|
|
await appWindow.setCursorPosition(
|
|
|
|
new LogicalPosition(centerX, centerY)
|
|
|
|
);
|
|
|
|
mouseX = centerX;
|
|
|
|
mouseY = centerY;
|
|
|
|
loopDownKeyCBMap.set(key, sightLoopCB);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
addShortcut(
|
|
|
|
"M0",
|
|
|
|
async () => {
|
|
|
|
await touchX(
|
|
|
|
TouchAction.Down,
|
|
|
|
fireKeyMapping.pointerId,
|
|
|
|
fireKeyMapping.posX,
|
|
|
|
fireKeyMapping.posY
|
|
|
|
);
|
|
|
|
},
|
|
|
|
async () => {
|
|
|
|
await touchX(
|
|
|
|
TouchAction.Move,
|
|
|
|
fireKeyMapping.pointerId,
|
|
|
|
fireKeyMapping.posX,
|
|
|
|
fireKeyMapping.posY
|
|
|
|
);
|
|
|
|
},
|
|
|
|
async () => {
|
|
|
|
await touchX(
|
|
|
|
TouchAction.Up,
|
|
|
|
fireKeyMapping.pointerId,
|
|
|
|
fireKeyMapping.posX,
|
|
|
|
fireKeyMapping.posY
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-13 09:53:41 +08:00
|
|
|
let screenSizeW: number;
|
|
|
|
let screenSizeH: number;
|
2024-04-16 22:38:07 +08:00
|
|
|
let maskSizeW: number;
|
|
|
|
let maskSizeH: number;
|
2024-04-13 09:53:41 +08:00
|
|
|
let mouseX = 0;
|
|
|
|
let mouseY = 0;
|
2024-04-30 13:41:04 +08:00
|
|
|
let maskElement: HTMLElement;
|
2024-05-17 22:30:58 +08:00
|
|
|
let message: ReturnType<typeof useMessage>;
|
2024-04-13 09:53:41 +08:00
|
|
|
|
|
|
|
const downKeyMap: Map<string, boolean> = new Map();
|
|
|
|
const downKeyCBMap: Map<string, () => Promise<void>> = new Map();
|
|
|
|
const loopDownKeyCBMap: Map<string, () => Promise<void>> = new Map();
|
|
|
|
const upKeyCBMap: Map<string, () => Promise<void>> = new Map();
|
2024-04-14 17:19:45 +08:00
|
|
|
const cancelAbleKeyList: string[] = [];
|
2024-04-13 09:53:41 +08:00
|
|
|
|
2024-05-16 19:45:08 +08:00
|
|
|
function handleKeydown(event: KeyboardEvent) {
|
2024-04-14 17:19:45 +08:00
|
|
|
event.preventDefault();
|
2024-05-12 21:30:45 +08:00
|
|
|
if (event.repeat) return;
|
2024-04-14 17:19:45 +08:00
|
|
|
if (downKeyMap.has(event.code)) {
|
2024-04-14 21:29:28 +08:00
|
|
|
downKeyMap.set(event.code, true);
|
2024-04-13 09:53:41 +08:00
|
|
|
// execute the down callback (if there is) asyncily
|
2024-04-14 17:19:45 +08:00
|
|
|
let cb = downKeyCBMap.get(event.code);
|
2024-04-13 09:53:41 +08:00
|
|
|
if (cb) cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-16 19:45:08 +08:00
|
|
|
function handleKeyup(event: KeyboardEvent) {
|
2024-04-14 17:19:45 +08:00
|
|
|
event.preventDefault();
|
|
|
|
if (downKeyMap.has(event.code)) {
|
2024-04-14 21:29:28 +08:00
|
|
|
downKeyMap.set(event.code, false);
|
2024-04-13 09:53:41 +08:00
|
|
|
// execute the up callback (if there is) asyncily
|
2024-04-14 17:19:45 +08:00
|
|
|
let cb = upKeyCBMap.get(event.code);
|
2024-04-13 09:53:41 +08:00
|
|
|
if (cb) cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleMouseDown(event: MouseEvent) {
|
2024-04-30 13:41:04 +08:00
|
|
|
if (event.target !== maskElement) return;
|
2024-04-14 17:19:45 +08:00
|
|
|
mouseX = event.clientX;
|
|
|
|
mouseY = event.clientY;
|
|
|
|
event.preventDefault();
|
2024-04-13 09:53:41 +08:00
|
|
|
let key = "M" + event.button.toString();
|
|
|
|
if (downKeyMap.has(key)) {
|
2024-04-14 21:29:28 +08:00
|
|
|
downKeyMap.set(key, true);
|
2024-04-13 09:53:41 +08:00
|
|
|
// execute the down callback asyncily
|
|
|
|
let cb = downKeyCBMap.get(key);
|
|
|
|
if (cb) cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleMouseUp(event: MouseEvent) {
|
2024-04-14 17:19:45 +08:00
|
|
|
mouseX = event.clientX;
|
|
|
|
mouseY = event.clientY;
|
|
|
|
event.preventDefault();
|
2024-04-13 09:53:41 +08:00
|
|
|
let key = "M" + event.button.toString();
|
2024-04-30 13:41:04 +08:00
|
|
|
if (downKeyMap.has(key) && downKeyMap.get(key)) {
|
2024-04-14 21:29:28 +08:00
|
|
|
downKeyMap.set(key, false);
|
2024-04-13 09:53:41 +08:00
|
|
|
// execute the up callback asyncily
|
|
|
|
let cb = upKeyCBMap.get(key);
|
|
|
|
if (cb) cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleMouseMove(event: MouseEvent) {
|
|
|
|
mouseX = event.clientX;
|
|
|
|
mouseY = event.clientY;
|
|
|
|
}
|
|
|
|
|
2024-04-18 17:07:41 +08:00
|
|
|
let lastWheelDownTime: number = 0;
|
|
|
|
let lastWheelUpTime: number = 0;
|
|
|
|
function handleMouseWheel(event: WheelEvent) {
|
|
|
|
event.preventDefault();
|
2024-04-19 16:30:29 +08:00
|
|
|
// trigger interval is 50ms
|
2024-04-18 17:07:41 +08:00
|
|
|
if (event.deltaY > 0 && event.timeStamp - lastWheelDownTime > 50) {
|
|
|
|
lastWheelDownTime = event.timeStamp;
|
|
|
|
// WheelDown
|
2024-05-06 11:24:04 +08:00
|
|
|
downKeyCBMap.get("WheelDown")?.();
|
|
|
|
upKeyCBMap.get("WheelDown")?.();
|
2024-04-18 17:07:41 +08:00
|
|
|
} else if (event.deltaY < 0 && event.timeStamp - lastWheelUpTime > 50) {
|
|
|
|
lastWheelUpTime = event.timeStamp;
|
|
|
|
// WheelUp
|
2024-05-06 11:24:04 +08:00
|
|
|
downKeyCBMap.get("WheelUp")?.();
|
|
|
|
upKeyCBMap.get("WheelUp")?.();
|
2024-04-18 17:07:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-13 09:53:41 +08:00
|
|
|
function addShortcut(
|
|
|
|
key: string,
|
2024-04-19 18:32:42 +08:00
|
|
|
downCB?: () => Promise<void>,
|
2024-04-13 09:53:41 +08:00
|
|
|
loopCB?: () => Promise<void>,
|
2024-04-14 17:19:45 +08:00
|
|
|
upCB?: () => Promise<void>,
|
|
|
|
cancelAble = false // only work with downCB && upCB
|
2024-04-13 09:53:41 +08:00
|
|
|
) {
|
|
|
|
downKeyMap.set(key, false);
|
|
|
|
|
2024-04-14 17:19:45 +08:00
|
|
|
if (cancelAble && downCB && upCB) {
|
|
|
|
cancelAbleKeyList.push(key);
|
|
|
|
const cancelAbleUpCB = async () => {
|
|
|
|
loopDownKeyCBMap.delete(key);
|
|
|
|
upKeyCBMap.delete(key);
|
|
|
|
await upCB();
|
|
|
|
};
|
2024-04-13 09:53:41 +08:00
|
|
|
if (loopCB)
|
2024-04-14 17:19:45 +08:00
|
|
|
downKeyCBMap.set(key, async () => {
|
|
|
|
loopDownKeyCBMap.set(key, loopCB);
|
|
|
|
upKeyCBMap.set(key, cancelAbleUpCB);
|
|
|
|
await downCB();
|
|
|
|
});
|
|
|
|
else {
|
|
|
|
// no loopCB
|
|
|
|
downKeyCBMap.set(key, async () => {
|
|
|
|
upKeyCBMap.set(key, cancelAbleUpCB);
|
|
|
|
await downCB();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (downCB && loopCB)
|
|
|
|
downKeyCBMap.set(key, async () => {
|
|
|
|
downCB();
|
|
|
|
loopDownKeyCBMap.set(key, loopCB);
|
|
|
|
});
|
|
|
|
else if (downCB) {
|
|
|
|
// no loopCB
|
|
|
|
downKeyCBMap.set(key, downCB);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (upCB) {
|
2024-04-13 09:53:41 +08:00
|
|
|
upKeyCBMap.set(key, async () => {
|
|
|
|
loopDownKeyCBMap.delete(key);
|
|
|
|
upCB();
|
|
|
|
});
|
2024-04-14 17:19:45 +08:00
|
|
|
}
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-14 17:19:45 +08:00
|
|
|
/**
|
|
|
|
* execute the json object macro
|
|
|
|
* @param macro
|
|
|
|
* @example
|
|
|
|
* await execMacro([
|
2024-04-19 16:30:29 +08:00
|
|
|
* // touch down
|
2024-04-14 17:19:45 +08:00
|
|
|
* {
|
|
|
|
* type: "touch",
|
|
|
|
* // op, pointerId, posX, posY
|
|
|
|
* args: ["down", 5, ["mouse", -10], 600],
|
|
|
|
* },
|
2024-04-19 16:30:29 +08:00
|
|
|
* // sleep 1000ms
|
2024-04-14 17:19:45 +08:00
|
|
|
* {
|
|
|
|
* type: "sleep",
|
2024-04-19 16:30:29 +08:00
|
|
|
* // time(ms)
|
2024-04-14 17:19:45 +08:00
|
|
|
* args: [1000],
|
|
|
|
* },
|
2024-04-19 16:30:29 +08:00
|
|
|
* // touch up
|
2024-04-14 17:19:45 +08:00
|
|
|
* {
|
|
|
|
* type: "touch",
|
|
|
|
* args: ["up", 5, ["mouse", 10], 600],
|
|
|
|
* },
|
2024-04-19 16:30:29 +08:00
|
|
|
* // touch 1000ms
|
|
|
|
* {
|
|
|
|
* type: "touch",
|
|
|
|
* args: ["default", 5, ["mouse", 10], 600, 1000],
|
|
|
|
* },
|
|
|
|
* // swipe
|
2024-04-14 17:19:45 +08:00
|
|
|
* {
|
|
|
|
* type: "swipe",
|
|
|
|
* // op, pointerId, posList, intervalBetweenPos
|
|
|
|
* args: [
|
|
|
|
* "default", 5,
|
|
|
|
* [
|
|
|
|
* [
|
|
|
|
* ["mouse", 100],
|
|
|
|
* ["mouse", -100],
|
|
|
|
* ],
|
|
|
|
* ["mouse", "mouse"],
|
|
|
|
* ],
|
|
|
|
* 1000,
|
|
|
|
* ],
|
|
|
|
* },
|
2024-05-07 10:20:44 +08:00
|
|
|
* // input-text
|
|
|
|
* {
|
|
|
|
* type: "input-text",
|
|
|
|
* // 1:on, 2:off
|
|
|
|
* args: [1]
|
|
|
|
* }
|
2024-04-14 17:19:45 +08:00
|
|
|
* ]);
|
|
|
|
*/
|
2024-04-26 23:07:26 +08:00
|
|
|
async function execMacro(
|
|
|
|
relativeSize: { w: number; h: number },
|
|
|
|
macro: KeyMacroList
|
|
|
|
) {
|
|
|
|
if (macro === null) return;
|
2024-04-14 17:19:45 +08:00
|
|
|
for (const cmd of macro) {
|
|
|
|
if (!cmd.hasOwnProperty("type") || !cmd.hasOwnProperty("args")) {
|
|
|
|
console.error("Invalid command: ", cmd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
switch (cmd.type) {
|
|
|
|
case "sleep":
|
|
|
|
await sleep(cmd.args[0]);
|
|
|
|
break;
|
|
|
|
case "touch":
|
|
|
|
let touchAction;
|
|
|
|
switch (cmd.args[0]) {
|
|
|
|
case "default":
|
|
|
|
touchAction = TouchAction.Default;
|
|
|
|
break;
|
|
|
|
case "down":
|
|
|
|
touchAction = TouchAction.Down;
|
|
|
|
break;
|
|
|
|
case "up":
|
|
|
|
touchAction = TouchAction.Up;
|
|
|
|
break;
|
|
|
|
case "move":
|
|
|
|
touchAction = TouchAction.Move;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.error("Invalid touch action: ", cmd.args[0]);
|
|
|
|
return;
|
|
|
|
}
|
2024-05-17 22:30:58 +08:00
|
|
|
await touchX(
|
|
|
|
touchAction,
|
|
|
|
cmd.args[1],
|
|
|
|
calculateMacroPosX(cmd.args[2], relativeSize.w),
|
|
|
|
calculateMacroPosY(cmd.args[3], relativeSize.h),
|
|
|
|
cmd.args.length > 4 ? cmd.args[4] : undefined
|
|
|
|
);
|
2024-04-14 17:19:45 +08:00
|
|
|
break;
|
|
|
|
case "swipe":
|
|
|
|
let swipeAction;
|
|
|
|
switch (cmd.args[0]) {
|
|
|
|
case "default":
|
|
|
|
swipeAction = SwipeAction.Default;
|
|
|
|
break;
|
|
|
|
case "noUp":
|
|
|
|
swipeAction = SwipeAction.NoUp;
|
|
|
|
break;
|
|
|
|
case "noDown":
|
|
|
|
swipeAction = SwipeAction.NoDown;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.error("Invalid swipe action: ", cmd.args[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await swipe({
|
|
|
|
action: swipeAction,
|
|
|
|
pointerId: cmd.args[1],
|
|
|
|
screen: {
|
|
|
|
w: screenSizeW,
|
|
|
|
h: screenSizeH,
|
|
|
|
},
|
2024-04-17 10:17:39 +08:00
|
|
|
pos: calculateMacroPosList(cmd.args[2], relativeSize),
|
2024-04-14 17:19:45 +08:00
|
|
|
intervalBetweenPos: cmd.args[3],
|
|
|
|
});
|
|
|
|
break;
|
2024-05-07 10:20:44 +08:00
|
|
|
case "input-text":
|
|
|
|
if (cmd.args[0] === 1) {
|
|
|
|
// on
|
|
|
|
useGlobalStore().showInputBox(true);
|
|
|
|
} else {
|
|
|
|
// off
|
|
|
|
useGlobalStore().showInputBox(false);
|
|
|
|
}
|
|
|
|
break;
|
2024-04-14 17:19:45 +08:00
|
|
|
default:
|
|
|
|
console.error("Invalid command: ", cmd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Invalid command: ", cmd, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 11:52:28 +08:00
|
|
|
let loopFlag = false;
|
|
|
|
function execLoopCB() {
|
|
|
|
loopDownKeyCBMap.forEach((cb) => {
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
if (loopFlag) requestAnimationFrame(execLoopCB);
|
|
|
|
}
|
|
|
|
|
2024-04-26 23:07:26 +08:00
|
|
|
// change ts type
|
|
|
|
function asType<T>(_val: any): asserts _val is T {}
|
|
|
|
|
|
|
|
function applyKeyMappingConfigShortcuts(
|
2024-04-30 13:41:04 +08:00
|
|
|
keyMappingConfig: KeyMappingConfig
|
2024-04-26 23:07:26 +08:00
|
|
|
): boolean {
|
2024-04-19 18:32:42 +08:00
|
|
|
try {
|
|
|
|
const relativeSize = keyMappingConfig.relativeSize;
|
|
|
|
for (const item of keyMappingConfig.list) {
|
|
|
|
switch (item.type) {
|
|
|
|
case "SteeringWheel":
|
2024-04-26 23:07:26 +08:00
|
|
|
asType<KeySteeringWheel>(item);
|
2024-04-19 18:32:42 +08:00
|
|
|
addSteeringWheelKeyboardShortcuts(
|
|
|
|
item.key,
|
|
|
|
relativeSize,
|
|
|
|
item.posX,
|
|
|
|
item.posY,
|
|
|
|
item.offset,
|
|
|
|
item.pointerId
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "DirectionalSkill":
|
2024-04-26 23:07:26 +08:00
|
|
|
asType<KeyDirectionalSkill>(item);
|
2024-04-19 18:32:42 +08:00
|
|
|
addDirectionalSkillShortcuts(
|
|
|
|
item.key,
|
|
|
|
relativeSize,
|
|
|
|
item.posX,
|
|
|
|
item.posY,
|
|
|
|
item.range,
|
|
|
|
item.pointerId
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "DirectionlessSkill":
|
2024-04-26 23:07:26 +08:00
|
|
|
asType<KeyDirectionlessSkill>(item);
|
2024-04-19 18:32:42 +08:00
|
|
|
addDirectionlessSkillShortcuts(
|
|
|
|
item.key,
|
|
|
|
relativeSize,
|
|
|
|
item.posX,
|
|
|
|
item.posY,
|
|
|
|
item.pointerId
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "CancelSkill":
|
2024-04-26 23:07:26 +08:00
|
|
|
asType<KeyCancelSkill>(item);
|
2024-04-19 18:32:42 +08:00
|
|
|
addCancelSkillShortcuts(
|
|
|
|
item.key,
|
|
|
|
relativeSize,
|
|
|
|
item.posX,
|
|
|
|
item.posY,
|
|
|
|
item.pointerId
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "Tap":
|
2024-04-26 23:07:26 +08:00
|
|
|
asType<KeyTap>(item);
|
2024-04-19 18:32:42 +08:00
|
|
|
addTapShortcuts(
|
|
|
|
item.key,
|
|
|
|
relativeSize,
|
|
|
|
item.time,
|
|
|
|
item.posX,
|
|
|
|
item.posY,
|
|
|
|
item.pointerId
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "TriggerWhenPressedSkill":
|
2024-04-26 23:07:26 +08:00
|
|
|
asType<KeyTriggerWhenPressedSkill>(item);
|
2024-04-19 18:32:42 +08:00
|
|
|
addTriggerWhenPressedSkillShortcuts(
|
|
|
|
item.key,
|
|
|
|
relativeSize,
|
|
|
|
item.posX,
|
|
|
|
item.posY,
|
|
|
|
item.directional,
|
|
|
|
item.rangeOrTime,
|
|
|
|
item.pointerId
|
|
|
|
);
|
|
|
|
break;
|
2024-05-05 19:24:50 +08:00
|
|
|
case "TriggerWhenDoublePressedSkill":
|
|
|
|
asType<KeyTriggerWhenDoublePressedSkill>(item);
|
|
|
|
addTriggerWhenDoublePressedSkillShortcuts(
|
|
|
|
item.key,
|
|
|
|
relativeSize,
|
|
|
|
item.posX,
|
|
|
|
item.posY,
|
|
|
|
item.range,
|
|
|
|
item.pointerId
|
|
|
|
);
|
|
|
|
break;
|
2024-04-19 18:32:42 +08:00
|
|
|
case "Observation":
|
2024-04-26 23:07:26 +08:00
|
|
|
asType<KeyObservation>(item);
|
2024-04-19 18:32:42 +08:00
|
|
|
addObservationShortcuts(
|
|
|
|
item.key,
|
|
|
|
relativeSize,
|
|
|
|
item.posX,
|
|
|
|
item.posY,
|
|
|
|
item.scale,
|
|
|
|
item.pointerId
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "Macro":
|
2024-04-26 23:07:26 +08:00
|
|
|
asType<KeyMacro>(item);
|
2024-04-19 18:32:42 +08:00
|
|
|
addShortcut(
|
|
|
|
item.key,
|
|
|
|
item.macro.down === null
|
|
|
|
? undefined
|
|
|
|
: async () => {
|
|
|
|
await execMacro(relativeSize, item.macro.down);
|
|
|
|
},
|
|
|
|
item.macro.loop === null
|
|
|
|
? undefined
|
|
|
|
: async () => {
|
|
|
|
await execMacro(relativeSize, item.macro.loop);
|
|
|
|
},
|
|
|
|
item.macro.up === null
|
|
|
|
? undefined
|
|
|
|
: async () => {
|
|
|
|
await execMacro(relativeSize, item.macro.up);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
2024-05-07 11:06:41 +08:00
|
|
|
console.error("Invalid item type: ", item);
|
2024-04-19 18:32:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Invalid keyMappingConfig: ", keyMappingConfig, e);
|
2024-04-30 13:41:04 +08:00
|
|
|
clearShortcuts();
|
2024-04-19 18:32:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 22:30:58 +08:00
|
|
|
async function touchX(
|
|
|
|
action: TouchAction,
|
|
|
|
pointerId: number,
|
|
|
|
posX: number,
|
|
|
|
posY: number,
|
|
|
|
time?: number
|
|
|
|
) {
|
|
|
|
await touch({
|
|
|
|
action,
|
|
|
|
pointerId,
|
|
|
|
screen: {
|
|
|
|
w: screenSizeW,
|
|
|
|
h: screenSizeH,
|
|
|
|
},
|
|
|
|
pos: {
|
|
|
|
x: posX,
|
|
|
|
y: posY,
|
|
|
|
},
|
|
|
|
time,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-07 10:20:44 +08:00
|
|
|
export function listenToEvent() {
|
2024-05-16 19:45:08 +08:00
|
|
|
window.addEventListener("keydown", handleKeydown);
|
|
|
|
window.addEventListener("keyup", handleKeyup);
|
2024-05-07 10:20:44 +08:00
|
|
|
window.addEventListener("mousedown", handleMouseDown);
|
|
|
|
window.addEventListener("mousemove", handleMouseMove);
|
|
|
|
window.addEventListener("mouseup", handleMouseUp);
|
|
|
|
window.addEventListener("wheel", handleMouseWheel);
|
2024-04-15 11:52:28 +08:00
|
|
|
loopFlag = true;
|
|
|
|
execLoopCB();
|
2024-04-14 17:19:45 +08:00
|
|
|
}
|
|
|
|
|
2024-05-07 10:20:44 +08:00
|
|
|
export function unlistenToEvent() {
|
2024-05-16 19:45:08 +08:00
|
|
|
window.removeEventListener("keydown", handleKeydown);
|
|
|
|
window.removeEventListener("keyup", handleKeyup);
|
2024-04-30 13:41:04 +08:00
|
|
|
window.removeEventListener("mousedown", handleMouseDown);
|
|
|
|
window.removeEventListener("mousemove", handleMouseMove);
|
|
|
|
window.removeEventListener("mouseup", handleMouseUp);
|
|
|
|
window.removeEventListener("wheel", handleMouseWheel);
|
2024-04-26 23:07:26 +08:00
|
|
|
|
2024-05-07 10:20:44 +08:00
|
|
|
loopFlag = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clearShortcuts() {
|
2024-04-18 11:03:19 +08:00
|
|
|
downKeyMap.clear();
|
|
|
|
downKeyCBMap.clear();
|
|
|
|
loopDownKeyCBMap.clear();
|
|
|
|
upKeyCBMap.clear();
|
|
|
|
cancelAbleKeyList.length = 0;
|
|
|
|
}
|
|
|
|
|
2024-04-16 22:38:07 +08:00
|
|
|
export function updateScreenSizeAndMaskArea(
|
2024-04-14 17:19:45 +08:00
|
|
|
screenSize: [number, number],
|
2024-04-16 22:38:07 +08:00
|
|
|
maskArea: [number, number]
|
2024-04-13 09:53:41 +08:00
|
|
|
) {
|
|
|
|
screenSizeW = screenSize[0];
|
|
|
|
screenSizeH = screenSize[1];
|
2024-04-16 22:38:07 +08:00
|
|
|
maskSizeW = maskArea[0];
|
|
|
|
maskSizeH = maskArea[1];
|
|
|
|
}
|
2024-04-14 17:19:45 +08:00
|
|
|
|
2024-04-26 23:07:26 +08:00
|
|
|
export function applyShortcuts(
|
|
|
|
element: HTMLElement,
|
2024-05-17 22:30:58 +08:00
|
|
|
keyMappingConfig: KeyMappingConfig,
|
|
|
|
messageAPI: ReturnType<typeof useMessage>
|
2024-04-26 23:07:26 +08:00
|
|
|
) {
|
2024-04-30 13:41:04 +08:00
|
|
|
maskElement = element;
|
2024-05-17 22:30:58 +08:00
|
|
|
message = messageAPI;
|
2024-04-14 17:19:45 +08:00
|
|
|
addClickShortcuts("M0", 0);
|
2024-05-17 22:30:58 +08:00
|
|
|
|
|
|
|
addFrontSightShortcuts();
|
|
|
|
|
2024-04-30 13:41:04 +08:00
|
|
|
return applyKeyMappingConfigShortcuts(keyMappingConfig);
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|