feat(hotkey): addFrontSightShortcuts with scale

This commit is contained in:
AkiChase 2024-05-18 17:25:29 +08:00
parent 7cc32d79c6
commit 8b848e5a72
2 changed files with 246 additions and 127 deletions

View File

@ -10,10 +10,12 @@ import {
KeyCancelSkill, KeyCancelSkill,
KeyDirectionalSkill, KeyDirectionalSkill,
KeyDirectionlessSkill, KeyDirectionlessSkill,
KeyFire,
KeyMacro, KeyMacro,
KeyMacroList, KeyMacroList,
KeyMappingConfig, KeyMappingConfig,
KeyObservation, KeyObservation,
KeySight,
KeySteeringWheel, KeySteeringWheel,
KeyTap, KeyTap,
KeyTriggerWhenDoublePressedSkill, KeyTriggerWhenDoublePressedSkill,
@ -146,6 +148,7 @@ function calculateMacroPosList(
function addObservationShortcuts( function addObservationShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number }, relativeSize: { w: number; h: number },
// pos relative to the mask
posX: number, posX: number,
posY: number, posY: number,
scale: number, scale: number,
@ -186,6 +189,7 @@ function addTapShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number }, relativeSize: { w: number; h: number },
time: number, time: number,
// pos relative to the mask
posX: number, posX: number,
posY: number, posY: number,
pointerId: number pointerId: number
@ -206,6 +210,7 @@ function addTapShortcuts(
function addCancelSkillShortcuts( function addCancelSkillShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number }, relativeSize: { w: number; h: number },
// pos relative to the mask
posX: number, posX: number,
posY: number, posY: number,
pointerId: number pointerId: number
@ -244,7 +249,7 @@ function addCancelSkillShortcuts(
function addTriggerWhenPressedSkillShortcuts( function addTriggerWhenPressedSkillShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number }, relativeSize: { w: number; h: number },
// pos relative to the device // pos relative to the mask
posX: number, posX: number,
posY: number, posY: number,
directional: boolean, directional: boolean,
@ -304,7 +309,7 @@ const doublePressedDownKey = new Map<string, boolean>();
function addTriggerWhenDoublePressedSkillShortcuts( function addTriggerWhenDoublePressedSkillShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number }, relativeSize: { w: number; h: number },
// pos relative to the device // pos relative to the mask
posX: number, posX: number,
posY: number, posY: number,
range: number, range: number,
@ -381,7 +386,7 @@ function addTriggerWhenDoublePressedSkillShortcuts(
function addDirectionlessSkillShortcuts( function addDirectionlessSkillShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number }, relativeSize: { w: number; h: number },
// pos relative to the device // pos relative to the mask
posX: number, posX: number,
posY: number, posY: number,
pointerId: number pointerId: number
@ -430,7 +435,7 @@ async function upDoublePressedKey() {
function addDirectionalSkillShortcuts( function addDirectionalSkillShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number }, relativeSize: { w: number; h: number },
// pos relative to the device // pos relative to the mask
posX: number, posX: number,
posY: number, posY: number,
range: number, range: number,
@ -499,7 +504,7 @@ function addDirectionalSkillShortcuts(
function addSteeringWheelKeyboardShortcuts( function addSteeringWheelKeyboardShortcuts(
key: wheelKey, key: wheelKey,
relativeSize: { w: number; h: number }, relativeSize: { w: number; h: number },
// pos relative to the device // pos relative to the mask
posX: number, posX: number,
posY: number, posY: number,
offset: number, offset: number,
@ -639,31 +644,25 @@ function addClickShortcuts(key: string, pointerId: number) {
); );
} }
function addFrontSightShortcuts() { function addSightShortcuts(
// TODO 1. 补上开火键 2. i18n 3. 单独函数,同时配合可视化组件 4. 组件配置中唯一 relativeSize: { w: number; h: number },
sightKeyMapping: KeySight,
fireKeyMapping?: KeyFire
) {
// TODO 2. i18n 3. 单独函数,同时配合可视化组件 4. 组件配置中唯一
const appWindow = getCurrent(); 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 mouseLock = false;
let msgReactive: ReturnType<typeof message.info> | null = null; let msgReactive: ReturnType<typeof message.info> | null = null;
const key = "KeyH"; const key = "KeyH";
const centerX = 70 + maskSizeW / 2; const sightClientX = 70 + sightKeyMapping.posX;
const centerY = 30 + maskSizeH / 2; const sightClientY = 30 + sightKeyMapping.posY;
const centerPosX = screenSizeW / 2; const sightDeviceX = Math.round(
const centerPosY = screenSizeH / 2; (sightKeyMapping.posX / relativeSize.w) * screenSizeW
const scaleX = 0.5; );
const scaleY = 0.5; const sightDeviceY = Math.round(
(sightKeyMapping.posY / relativeSize.h) * screenSizeH
);
const removeShortcut = (key: string) => { const removeShortcut = (key: string) => {
loopDownKeyCBMap.delete(key); loopDownKeyCBMap.delete(key);
@ -672,33 +671,112 @@ function addFrontSightShortcuts() {
downKeyMap.delete(key); downKeyMap.delete(key);
}; };
const touchRelateToCenter = async (action: TouchAction) => { const touchRelateToSight = async (action: TouchAction) => {
await touchX( await touchX(
action, action,
0, sightKeyMapping.pointerId,
centerPosX + clientxToPosOffsetx(mouseX, centerPosX, scaleX), sightDeviceX +
centerPosY + clientyToPosOffsety(mouseY, centerPosY, scaleY) clientxToPosOffsetx(mouseX, sightDeviceX, sightKeyMapping.scaleX),
sightDeviceY +
clientyToPosOffsety(mouseY, sightDeviceY, sightKeyMapping.scaleY)
); );
}; };
const sightLoopCB = async () => { const sightLoopCB = async () => {
await touchRelateToCenter(TouchAction.Move); 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,
fireKeyMapping.posX +
accOffsetX +
clientxToPosOffsetx(mouseX, sightDeviceX, fireKeyMapping.scaleX),
fireKeyMapping.posX +
accOffsetY +
clientyToPosOffsety(mouseY, sightDeviceY, fireKeyMapping.scaleY)
);
}
: undefined;
let accOffsetX = 0;
let accOffsetY = 0;
const moveLeaveHandler = async () => { const moveLeaveHandler = async () => {
// stop loop touch move if (fireKeyMapping && fireKeyMapping.drag && downKeyMap.get("M0")) {
loopDownKeyCBMap.delete(key); // fire drag mode
// touch up // stop fireDragLoopCB
await touchRelateToCenter(TouchAction.Up); loopDownKeyCBMap.delete(key);
await sleep(150); // cal accOffset
// move mouse accOffsetX += clientxToPosOffsetx(
appWindow.setCursorPosition(new LogicalPosition(centerX, centerY)); mouseX,
mouseX = centerX; sightDeviceX,
mouseY = centerY; fireKeyMapping.scaleX
// touch down );
touchX(TouchAction.Down, 0, centerPosX, centerPosY); accOffsetY += clientyToPosOffsety(
// start loop touch move mouseY,
loopDownKeyCBMap.set(key, sightLoopCB); sightDeviceY,
fireKeyMapping.scaleY
);
// move mouse
await appWindow.setCursorPosition(
new LogicalPosition(sightClientX, sightClientY)
);
mouseX = sightClientX;
mouseY = sightClientY;
// start fireDragLoopCB
loopDownKeyCBMap.set(key, fireDragLoopCB!);
} else {
// sight mode or fire without drag mode
const fireFlag =
fireKeyMapping && !fireKeyMapping.drag && downKeyMap.get("M0");
// stop sightLoopCB or fireNoDragLoopCB
loopDownKeyCBMap.delete(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);
}
await sleep(150);
// 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(key, fireFlag ? fireNoDragLoopCB! : sightLoopCB);
}
}; };
// add sight shortcut // add sight shortcut
@ -706,7 +784,7 @@ function addFrontSightShortcuts() {
if (mouseLock) { if (mouseLock) {
// stop sight mode // stop sight mode
loopDownKeyCBMap.delete(key); loopDownKeyCBMap.delete(key);
await touchRelateToCenter(TouchAction.Up); await touchRelateToSight(TouchAction.Up);
await appWindow.setCursorVisible(true); await appWindow.setCursorVisible(true);
maskElement.removeEventListener("mouseleave", moveLeaveHandler); maskElement.removeEventListener("mouseleave", moveLeaveHandler);
mouseLock = false; mouseLock = false;
@ -715,7 +793,7 @@ function addFrontSightShortcuts() {
msgReactive = null; msgReactive = null;
} }
// remove fire key // remove fire key
if (hasFireKey) { if (fireKeyMapping) {
removeShortcut("M0"); removeShortcut("M0");
} }
// add click // add click
@ -729,95 +807,97 @@ function addFrontSightShortcuts() {
duration: 0, duration: 0,
}); });
await touchX(TouchAction.Down, 0, centerPosX, centerPosY); await appWindow.setCursorPosition(
await appWindow.setCursorPosition(new LogicalPosition(centerX, centerY)); new LogicalPosition(sightClientX, sightClientY)
mouseX = centerX; );
mouseY = centerY; mouseX = sightClientX;
mouseY = sightClientY;
await touchX(
TouchAction.Down,
sightKeyMapping.pointerId,
sightDeviceX,
sightDeviceY
);
loopDownKeyCBMap.set(key, sightLoopCB); loopDownKeyCBMap.set(key, sightLoopCB);
// remove click // remove click
removeShortcut("M0"); removeShortcut("M0");
// add fire key // add fire key
if (hasFireKey) { if (fireKeyMapping) {
if (fireKeyMapping.drag) { // fire with drag
addShortcut( addShortcut(
"M0", "M0",
async () => { async () => {
// stop sight loop // stop sightLoopCB
loopDownKeyCBMap.delete(key); loopDownKeyCBMap.delete(key);
await touchRelateToCenter(TouchAction.Up); // touch up sight
// move cursor to center await touchRelateToSight(TouchAction.Up);
await appWindow.setCursorPosition( if (!fireKeyMapping.drag) {
new LogicalPosition(centerX, centerY) // touch down sight
);
mouseX = centerX;
mouseY = centerY;
// touch down fire
await touchX( await touchX(
TouchAction.Down, TouchAction.Down,
fireKeyMapping.pointerId, sightKeyMapping.pointerId,
fireKeyMapping.posX, sightDeviceX,
fireKeyMapping.posY sightDeviceY
); );
}, } else {
async () => { // clear accumulated offset
await touchX( accOffsetX = 0;
TouchAction.Move, accOffsetY = 0;
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);
} }
); // move cursor
} else { await appWindow.setCursorPosition(
addShortcut( new LogicalPosition(sightClientX, sightClientY)
"M0", );
async () => { mouseX = sightClientX;
await touchX( mouseY = sightClientY;
TouchAction.Down, // touch down fire
fireKeyMapping.pointerId, await touchX(
fireKeyMapping.posX, TouchAction.Down,
fireKeyMapping.posY fireKeyMapping.pointerId,
); fireKeyMapping.posX,
}, fireKeyMapping.posY
async () => { );
await touchX(
TouchAction.Move, // start fireDragLoopCB or fireNoDragLoopCB
fireKeyMapping.pointerId, loopDownKeyCBMap.set(
fireKeyMapping.posX, key,
fireKeyMapping.posY fireKeyMapping.drag ? fireDragLoopCB! : fireNoDragLoopCB!
); );
}, },
async () => { undefined,
await touchX( async () => {
TouchAction.Up, // stop fireDragLoopCB or fireNoDragLoopCB
fireKeyMapping.pointerId, loopDownKeyCBMap.delete(key);
fireKeyMapping.posX, // touch up fire
fireKeyMapping.posY await touchX(
); TouchAction.Up,
} fireKeyMapping.pointerId,
); fireKeyMapping.posX +
} clientxToPosOffsetx(
mouseX,
sightDeviceX,
fireKeyMapping.scaleX
),
fireKeyMapping.posY +
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(key, sightLoopCB);
}
);
} }
} }
}); });
@ -1308,7 +1388,29 @@ export function applyShortcuts(
message = messageAPI; message = messageAPI;
addClickShortcuts("M0", 0); addClickShortcuts("M0", 0);
addFrontSightShortcuts(); const relativeSize = { w: 1280, h: 720 };
const sightKeyMapping = {
type: "Sight" as "Sight",
key: "KeyH",
pointerId: 0,
note: "准星键",
posX: 640,
posY: 360,
scaleX: 1,
scaleY: 1,
};
const fireKeyMapping = {
type: "Fire" as "Fire",
pointerId: 2,
note: "开火键",
posX: 300,
posY: 300,
drag: true,
scaleX: 0.5,
scaleY: 0.2,
};
addSightShortcuts(relativeSize, sightKeyMapping, fireKeyMapping);
return applyKeyMappingConfigShortcuts(keyMappingConfig); return applyKeyMappingConfigShortcuts(keyMappingConfig);
} }

View File

@ -1,5 +1,6 @@
interface KeyBase { interface KeyBase {
note: string; note: string;
// pos relative to the mask
posX: number; posX: number;
posY: number; posY: number;
} }
@ -79,6 +80,22 @@ export interface KeyMacro extends KeyBase {
}; };
} }
export interface KeySight extends KeyBase {
type: "Sight";
key: string;
pointerId: number;
scaleX: number;
scaleY: number;
}
export interface KeyFire extends KeyBase {
type: "Fire";
drag: boolean;
pointerId: number;
scaleX: number;
scaleY: number;
}
export type KeyMapping = export type KeyMapping =
| KeySteeringWheel | KeySteeringWheel
| KeyDirectionalSkill | KeyDirectionalSkill