fix(hotkey): sight and fire shortcuts

This commit is contained in:
AkiChase 2024-05-19 10:45:37 +08:00
parent d1b6292b8a
commit fd790b029f

View File

@ -654,7 +654,6 @@ function addSightShortcuts(
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 sightClientX = 70 + sightKeyMapping.posX; const sightClientX = 70 + sightKeyMapping.posX;
const sightClientY = 30 + sightKeyMapping.posY; const sightClientY = 30 + sightKeyMapping.posY;
const sightDeviceX = Math.round( const sightDeviceX = Math.round(
@ -664,6 +663,14 @@ function addSightShortcuts(
(sightKeyMapping.posY / relativeSize.h) * screenSizeH (sightKeyMapping.posY / relativeSize.h) * screenSizeH
); );
const fireDeviceX = fireKeyMapping
? Math.round((fireKeyMapping.posX / relativeSize.w) * screenSizeW)
: 0;
const fireDeviceY = fireKeyMapping
? Math.round((fireKeyMapping.posY / relativeSize.h) * screenSizeH)
: 0;
const removeShortcut = (key: string) => { const removeShortcut = (key: string) => {
loopDownKeyCBMap.delete(key); loopDownKeyCBMap.delete(key);
downKeyCBMap.delete(key); downKeyCBMap.delete(key);
@ -705,10 +712,10 @@ function addSightShortcuts(
await touchX( await touchX(
TouchAction.Move, TouchAction.Move,
fireKeyMapping.pointerId, fireKeyMapping.pointerId,
fireKeyMapping.posX + fireDeviceX +
accOffsetX + accOffsetX +
clientxToPosOffsetx(mouseX, sightDeviceX, fireKeyMapping.scaleX), clientxToPosOffsetx(mouseX, sightDeviceX, fireKeyMapping.scaleX),
fireKeyMapping.posX + fireDeviceY +
accOffsetY + accOffsetY +
clientyToPosOffsety(mouseY, sightDeviceY, fireKeyMapping.scaleY) clientyToPosOffsety(mouseY, sightDeviceY, fireKeyMapping.scaleY)
); );
@ -721,7 +728,7 @@ function addSightShortcuts(
if (fireKeyMapping && fireKeyMapping.drag && downKeyMap.get("M0")) { if (fireKeyMapping && fireKeyMapping.drag && downKeyMap.get("M0")) {
// fire drag mode // fire drag mode
// stop fireDragLoopCB // stop fireDragLoopCB
loopDownKeyCBMap.delete(key); loopDownKeyCBMap.delete(sightKeyMapping.key);
// cal accOffset // cal accOffset
accOffsetX += clientxToPosOffsetx( accOffsetX += clientxToPosOffsetx(
mouseX, mouseX,
@ -740,13 +747,13 @@ function addSightShortcuts(
mouseX = sightClientX; mouseX = sightClientX;
mouseY = sightClientY; mouseY = sightClientY;
// start fireDragLoopCB // start fireDragLoopCB
loopDownKeyCBMap.set(key, fireDragLoopCB!); loopDownKeyCBMap.set(sightKeyMapping.key, fireDragLoopCB!);
} else { } else {
// sight mode or fire without drag mode // sight mode or fire without drag mode
const fireFlag = const fireFlag =
fireKeyMapping && !fireKeyMapping.drag && downKeyMap.get("M0"); fireKeyMapping && !fireKeyMapping.drag && downKeyMap.get("M0");
// stop sightLoopCB or fireNoDragLoopCB // stop sightLoopCB or fireNoDragLoopCB
loopDownKeyCBMap.delete(key); loopDownKeyCBMap.delete(sightKeyMapping.key);
// touch up // touch up
if (fireFlag) { if (fireFlag) {
await touchX( await touchX(
@ -775,18 +782,22 @@ function addSightShortcuts(
sightDeviceY sightDeviceY
); );
// start sightLoopCB or fireNoDragLoopCB // start sightLoopCB or fireNoDragLoopCB
loopDownKeyCBMap.set(key, fireFlag ? fireNoDragLoopCB! : sightLoopCB); loopDownKeyCBMap.set(
sightKeyMapping.key,
fireFlag ? fireNoDragLoopCB! : sightLoopCB
);
} }
}; };
// add sight shortcut // add sight shortcut
addShortcut(key, async () => { addShortcut(sightKeyMapping.key, async () => {
if (mouseLock) { if (mouseLock) {
// stop sight mode // stop sight mode
loopDownKeyCBMap.delete(key); loopDownKeyCBMap.delete(sightKeyMapping.key);
await touchRelateToSight(TouchAction.Up); await touchRelateToSight(TouchAction.Up);
await appWindow.setCursorVisible(true); await appWindow.setCursorVisible(true);
maskElement.removeEventListener("mouseleave", moveLeaveHandler); maskElement.removeEventListener("mouseleave", moveLeaveHandler);
maskElement.style.cursor = "pointer";
mouseLock = false; mouseLock = false;
if (msgReactive) { if (msgReactive) {
msgReactive.destroy(); msgReactive.destroy();
@ -802,10 +813,14 @@ function addSightShortcuts(
// start sight mode // start sight mode
await appWindow.setCursorVisible(false); await appWindow.setCursorVisible(false);
maskElement.addEventListener("mouseleave", moveLeaveHandler); maskElement.addEventListener("mouseleave", moveLeaveHandler);
maskElement.style.cursor = "none";
mouseLock = true; mouseLock = true;
msgReactive = message.info(t("pages.Mask.sightMode", [key]), { msgReactive = message.info(
duration: 0, t("pages.Mask.sightMode", [sightKeyMapping.key]),
}); {
duration: 0,
}
);
await appWindow.setCursorPosition( await appWindow.setCursorPosition(
new LogicalPosition(sightClientX, sightClientY) new LogicalPosition(sightClientX, sightClientY)
@ -818,7 +833,7 @@ function addSightShortcuts(
sightDeviceX, sightDeviceX,
sightDeviceY sightDeviceY
); );
loopDownKeyCBMap.set(key, sightLoopCB); loopDownKeyCBMap.set(sightKeyMapping.key, sightLoopCB);
// remove click // remove click
removeShortcut("M0"); removeShortcut("M0");
// add fire key // add fire key
@ -828,7 +843,7 @@ function addSightShortcuts(
"M0", "M0",
async () => { async () => {
// stop sightLoopCB // stop sightLoopCB
loopDownKeyCBMap.delete(key); loopDownKeyCBMap.delete(sightKeyMapping.key);
// touch up sight // touch up sight
await touchRelateToSight(TouchAction.Up); await touchRelateToSight(TouchAction.Up);
if (!fireKeyMapping.drag) { if (!fireKeyMapping.drag) {
@ -854,31 +869,31 @@ function addSightShortcuts(
await touchX( await touchX(
TouchAction.Down, TouchAction.Down,
fireKeyMapping.pointerId, fireKeyMapping.pointerId,
fireKeyMapping.posX, fireDeviceX,
fireKeyMapping.posY fireDeviceY
); );
// start fireDragLoopCB or fireNoDragLoopCB // start fireDragLoopCB or fireNoDragLoopCB
loopDownKeyCBMap.set( loopDownKeyCBMap.set(
key, sightKeyMapping.key,
fireKeyMapping.drag ? fireDragLoopCB! : fireNoDragLoopCB! fireKeyMapping.drag ? fireDragLoopCB! : fireNoDragLoopCB!
); );
}, },
undefined, undefined,
async () => { async () => {
// stop fireDragLoopCB or fireNoDragLoopCB // stop fireDragLoopCB or fireNoDragLoopCB
loopDownKeyCBMap.delete(key); loopDownKeyCBMap.delete(sightKeyMapping.key);
// touch up fire // touch up fire
await touchX( await touchX(
TouchAction.Up, TouchAction.Up,
fireKeyMapping.pointerId, fireKeyMapping.pointerId,
fireKeyMapping.posX + fireDeviceX +
clientxToPosOffsetx( clientxToPosOffsetx(
mouseX, mouseX,
sightDeviceX, sightDeviceX,
fireKeyMapping.scaleX fireKeyMapping.scaleX
), ),
fireKeyMapping.posY + fireDeviceY +
clientyToPosOffsety(mouseY, sightDeviceY, fireKeyMapping.scaleY) clientyToPosOffsety(mouseY, sightDeviceY, fireKeyMapping.scaleY)
); );
// touch down sight // touch down sight
@ -895,7 +910,7 @@ function addSightShortcuts(
mouseX = sightClientX; mouseX = sightClientX;
mouseY = sightClientY; mouseY = sightClientY;
// start sightLoopCB // start sightLoopCB
loopDownKeyCBMap.set(key, sightLoopCB); loopDownKeyCBMap.set(sightKeyMapping.key, sightLoopCB);
} }
); );
} }