feat(hotkey): add shortcut relative size args

This commit is contained in:
AkiChase 2024-04-17 10:17:39 +08:00
parent b332c60eea
commit 0f5f6a44bb
5 changed files with 112 additions and 52 deletions

View File

@ -121,12 +121,13 @@ async fn main() {
// restore window position and size // restore window position and size
match store.get("maskArea") { match store.get("maskArea") {
Some(value) => { Some(value) => {
let pos_x = value["posX"].as_i64().unwrap(); let pos_x = value["posX"].as_i64().unwrap_or(100);
let pos_y = value["posY"].as_i64().unwrap(); let pos_y = value["posY"].as_i64().unwrap_or(100);
let size_w = value["sizeW"].as_i64().unwrap(); let size_w = value["sizeW"].as_i64().unwrap_or(800);
let size_h = value["sizeH"].as_i64().unwrap(); let size_h = value["sizeH"].as_i64().unwrap_or(600);
let main_window: tauri::WebviewWindow = let main_window: tauri::WebviewWindow =
app.get_webview_window("main").unwrap(); app.get_webview_window("main").unwrap();
main_window.set_zoom(1.).unwrap();
main_window main_window
.set_position(tauri::Position::Logical(tauri::LogicalPosition { .set_position(tauri::Position::Logical(tauri::LogicalPosition {
x: (pos_x - 70) as f64, x: (pos_x - 70) as f64,

View File

@ -37,6 +37,10 @@ import {
"sidebar content"; "sidebar content";
} }
.n-scrollbar-container{
background-color: var(--bg-color);
}
.n-scrollbar-content { .n-scrollbar-content {
height: 100%; height: 100%;
} }

View File

@ -84,12 +84,12 @@ async function refreshAreaModel(size?: PhysicalSize, pos?: PhysicalPosition) {
// use logical position and size // use logical position and size
if (lSize !== undefined) { if (lSize !== undefined) {
areaModel.value.sizeW = lSize.width - ml; areaModel.value.sizeW = Math.round(lSize.width) - ml;
areaModel.value.sizeH = lSize.height - mt; areaModel.value.sizeH = Math.round(lSize.height) - mt;
} }
if (lPos !== undefined) { if (lPos !== undefined) {
areaModel.value.posX = lPos.x + ml; areaModel.value.posX = Math.round(lPos.x) + ml;
areaModel.value.posY = lPos.y + mt; areaModel.value.posY = Math.round(lPos.y) + mt;
} }
} }

View File

@ -35,7 +35,7 @@ import { NTabs, NTabPane, NScrollbar } from "naive-ui";
overflow-y: auto; overflow-y: auto;
display: flex; display: flex;
.NTabPane { .n-tab-pane {
padding: 0; padding: 0;
} }

View File

@ -46,30 +46,42 @@ async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms)); return new Promise((resolve) => setTimeout(resolve, ms));
} }
function calculateMacroPosX(pos: [string, number] | number): number { function calculateMacroPosX(
if (typeof pos === "number") { posX: [string, number] | number,
return pos; relativeSizeW: number
): number {
if (typeof posX === "number") {
return Math.round(posX * (screenSizeW / relativeSizeW));
} }
if (typeof pos === "string") { if (typeof posX === "string") {
return clientxToPosx(mouseX); return clientxToPosx(mouseX);
} else { } else {
if (pos[0] === "mouse") { if (posX[0] === "mouse") {
return clientxToPosx(mouseX) + pos[1]; return (
clientxToPosx(mouseX) +
Math.round(posX[1] * (screenSizeW / relativeSizeW))
);
} else { } else {
throw new Error("Invalid pos"); throw new Error("Invalid pos");
} }
} }
} }
function calculateMacroPosY(pos: [string, number] | number): number { function calculateMacroPosY(
if (typeof pos === "number") { posY: [string, number] | number,
return pos; relativeSizeH: number
): number {
if (typeof posY === "number") {
return Math.round(posY * (screenSizeH / relativeSizeH));
} }
if (typeof pos === "string") { if (typeof posY === "string") {
return clientyToPosy(mouseY); return clientyToPosy(mouseY);
} else { } else {
if (pos[0] === "mouse") { if (posY[0] === "mouse") {
return clientyToPosy(mouseY) + pos[1]; return (
clientyToPosy(mouseY) +
Math.round(posY[1] * (screenSizeH / relativeSizeH))
);
} else { } else {
throw new Error("Invalid pos"); throw new Error("Invalid pos");
} }
@ -77,12 +89,13 @@ function calculateMacroPosY(pos: [string, number] | number): number {
} }
function calculateMacroPosList( function calculateMacroPosList(
posList: [[string, number] | number, [string, number] | number][] posList: [[string, number] | number, [string, number] | number][],
relativeSize: { w: number; h: number }
): { x: number; y: number }[] { ): { x: number; y: number }[] {
return posList.map((posPair) => { return posList.map((posPair) => {
return { return {
x: calculateMacroPosX(posPair[0]), x: calculateMacroPosX(posPair[0], relativeSize.w),
y: calculateMacroPosY(posPair[1]), y: calculateMacroPosY(posPair[1], relativeSize.h),
}; };
}); });
} }
@ -92,6 +105,7 @@ function calculateMacroPosList(
// add shortcuts for observation // add shortcuts for observation
function addObservationShortcuts( function addObservationShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number },
posX: number, posX: number,
posY: number, posY: number,
scale: number, scale: number,
@ -99,6 +113,8 @@ function addObservationShortcuts(
) { ) {
let observationMouseX = 0; let observationMouseX = 0;
let observationMouseY = 0; let observationMouseY = 0;
posX = Math.round((posX / relativeSize.w) * screenSizeW);
posY = Math.round((posY / relativeSize.h) * screenSizeH);
addShortcut( addShortcut(
key, key,
async () => { async () => {
@ -151,10 +167,13 @@ function addObservationShortcuts(
// add shortcuts for simple tap (touch for 100 ms when pressed) // add shortcuts for simple tap (touch for 100 ms when pressed)
function addTapShortcuts( function addTapShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number },
posX: number, posX: number,
posY: number, posY: number,
pointerId: number pointerId: number
) { ) {
posX = Math.round((posX / relativeSize.w) * screenSizeW);
posY = Math.round((posY / relativeSize.h) * screenSizeH);
addShortcut( addShortcut(
key, key,
async () => { async () => {
@ -179,10 +198,13 @@ function addTapShortcuts(
// add shortcuts for cancel skill // add shortcuts for cancel skill
function addCancelSkillShortcuts( function addCancelSkillShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number },
posX: number, posX: number,
posY: number, posY: number,
pointerId: number pointerId: number
) { ) {
posX = Math.round((posX / relativeSize.w) * screenSizeW);
posY = Math.round((posY / relativeSize.h) * screenSizeH);
addShortcut( addShortcut(
key, key,
async () => { async () => {
@ -230,6 +252,7 @@ function addCancelSkillShortcuts(
// add shortcuts for trigger when pressed skill // add shortcuts for trigger when pressed skill
function addTriggerWhenPressedSkillShortcuts( function addTriggerWhenPressedSkillShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number },
// pos relative to the device // pos relative to the device
posX: number, posX: number,
posY: number, posY: number,
@ -239,6 +262,8 @@ function addTriggerWhenPressedSkillShortcuts(
pointerId: number pointerId: number
) { ) {
if (directional) { if (directional) {
posX = Math.round((posX / relativeSize.w) * screenSizeW);
posY = Math.round((posY / relativeSize.h) * screenSizeH);
addShortcut( addShortcut(
key, key,
// down // down
@ -264,18 +289,21 @@ function addTriggerWhenPressedSkillShortcuts(
undefined undefined
); );
} else { } else {
addTapShortcuts(key, posX, posY, pointerId); addTapShortcuts(key, relativeSize, posX, posY, pointerId);
} }
} }
// add shortcuts for directionless skill (cancelable) // add shortcuts for directionless skill (cancelable)
function addDirectionlessSkillShortcuts( function addDirectionlessSkillShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number },
// pos relative to the device // pos relative to the device
posX: number, posX: number,
posY: number, posY: number,
pointerId: number pointerId: number
) { ) {
posX = Math.round((posX / relativeSize.w) * screenSizeW);
posY = Math.round((posY / relativeSize.h) * screenSizeH);
addShortcut( addShortcut(
key, key,
// down // down
@ -317,12 +345,15 @@ function addDirectionlessSkillShortcuts(
// add shortcuts for directional skill (cancelable) // add shortcuts for directional skill (cancelable)
function addDirectionalSkillShortcuts( function addDirectionalSkillShortcuts(
key: string, key: string,
relativeSize: { w: number; h: number },
// pos relative to the device // pos relative to the device
posX: number, posX: number,
posY: number, posY: number,
range: number, range: number,
pointerId: number pointerId: number
) { ) {
posX = Math.round((posX / relativeSize.w) * screenSizeW);
posY = Math.round((posY / relativeSize.h) * screenSizeH);
addShortcut( addShortcut(
key, key,
// down // down
@ -381,6 +412,7 @@ function addDirectionalSkillShortcuts(
// add shortcuts for steering wheel // add shortcuts for steering wheel
function addSteeringWheelKeyboardShortcuts( function addSteeringWheelKeyboardShortcuts(
key: wheelKey, key: wheelKey,
relativeSize: { w: number; h: number },
// pos relative to the device // pos relative to the device
posX: number, posX: number,
posY: number, posY: number,
@ -390,6 +422,8 @@ function addSteeringWheelKeyboardShortcuts(
let loopFlag = false; let loopFlag = false;
let curPosX = 0; let curPosX = 0;
let curPosY = 0; let curPosY = 0;
posX = Math.round((posX / relativeSize.w) * screenSizeW);
posY = Math.round((posY / relativeSize.h) * screenSizeH);
// calculate the end coordinates of the eight directions of the direction wheel // calculate the end coordinates of the eight directions of the direction wheel
let offsetHalf = Math.round(offset / 1.414); let offsetHalf = Math.round(offset / 1.414);
@ -717,7 +751,8 @@ function addShortcut(
* }, * },
* ]); * ]);
*/ */
async function execMacro(macro: any[]) {
async function execMacro(relativeSize: { w: number; h: number }, macro: any[]) {
for (const cmd of macro) { for (const cmd of macro) {
if (!cmd.hasOwnProperty("type") || !cmd.hasOwnProperty("args")) { if (!cmd.hasOwnProperty("type") || !cmd.hasOwnProperty("args")) {
console.error("Invalid command: ", cmd); console.error("Invalid command: ", cmd);
@ -755,8 +790,8 @@ async function execMacro(macro: any[]) {
h: screenSizeH, h: screenSizeH,
}, },
pos: { pos: {
x: calculateMacroPosX(cmd.args[2]), x: calculateMacroPosX(cmd.args[2], relativeSize.w),
y: calculateMacroPosY(cmd.args[3]), y: calculateMacroPosY(cmd.args[3], relativeSize.h),
}, },
}); });
break; break;
@ -783,7 +818,7 @@ async function execMacro(macro: any[]) {
w: screenSizeW, w: screenSizeW,
h: screenSizeH, h: screenSizeH,
}, },
pos: calculateMacroPosList(cmd.args[2]), pos: calculateMacroPosList(cmd.args[2], relativeSize),
intervalBetweenPos: cmd.args[3], intervalBetweenPos: cmd.args[3],
}); });
break; break;
@ -836,6 +871,9 @@ export function initShortcuts(element: HTMLElement) {
element.addEventListener("mouseup", handleMouseUp); element.addEventListener("mouseup", handleMouseUp);
element.addEventListener("mouseout", handleMouseUp); // mouse out of the element as mouse up element.addEventListener("mouseout", handleMouseUp); // mouse out of the element as mouse up
// 读取按键配置文件时获取
const relativeSize = { w: 1280, h: 720 };
addClickShortcuts("M0", 0); addClickShortcuts("M0", 0);
addSteeringWheelKeyboardShortcuts( addSteeringWheelKeyboardShortcuts(
{ {
@ -844,36 +882,53 @@ export function initShortcuts(element: HTMLElement) {
up: "KeyW", up: "KeyW",
down: "KeyS", down: "KeyS",
}, },
relativeSize,
180, 180,
560, 560,
100, 100,
1 1
); );
addDirectionalSkillShortcuts("KeyQ", 950, 610, 200, 2); // skill 1 addDirectionalSkillShortcuts("KeyQ", relativeSize, 950, 610, 200, 2); // skill 1
addDirectionalSkillShortcuts("AltLeft", 1025, 500, 200, 2); // skill 2 addDirectionalSkillShortcuts("AltLeft", relativeSize, 1025, 500, 200, 2); // skill 2
addDirectionalSkillShortcuts("KeyE", 1160, 420, 200, 2); // skill 3 addDirectionalSkillShortcuts("KeyE", relativeSize, 1160, 420, 200, 2); // skill 3
addTriggerWhenPressedSkillShortcuts("M4", 1160, 420, false, 0, 2); // skill 3 (no direction and trigger when pressed) addTriggerWhenPressedSkillShortcuts(
addDirectionlessSkillShortcuts("M1", 1150, 280, 2); // equipment skill (middle mouse click) "M4",
addCancelSkillShortcuts("Space", 1160, 140, 2); // cancel skill relativeSize,
1160,
420,
false,
0,
2
); // skill 3 (no direction and trigger when pressed)
addDirectionlessSkillShortcuts("M1", relativeSize, 1150, 280, 2); // equipment skill (middle mouse click)
addCancelSkillShortcuts("Space", relativeSize, 1160, 140, 2); // cancel skill
addTapShortcuts("KeyB", 650, 650, 3); // home addTapShortcuts("KeyB", relativeSize, 650, 650, 3); // home
addTapShortcuts("KeyC", 740, 650, 3); // recover addTapShortcuts("KeyC", relativeSize, 740, 650, 3); // recover
addDirectionalSkillShortcuts("KeyF", 840, 650, 200, 2); // summoner skills addDirectionalSkillShortcuts("KeyF", relativeSize, 840, 650, 200, 2); // summoner skills
addTriggerWhenPressedSkillShortcuts("ControlLeft", 840, 650, false, 0, 3); // summoner skills (no direction and trigger when pressed) addTriggerWhenPressedSkillShortcuts(
addTapShortcuts("M2", 1165, 620, 3); // attack (right click) "ControlLeft",
addTapShortcuts("Digit1", 880, 560, 3); // skill 1 upgrade relativeSize,
addTapShortcuts("Digit2", 960, 430, 3); // skill 2 upgrade 840,
addTapShortcuts("Digit3", 1090, 350, 3); // skill 3 upgrade 650,
addTapShortcuts("Digit5", 130, 300, 3); // quick buy 1 false,
addTapShortcuts("Digit6", 130, 370, 3); // quick buy 2 0,
3
); // summoner skills (no direction and trigger when pressed)
addTapShortcuts("M2", relativeSize, 1165, 620, 3); // attack (right click)
addTapShortcuts("Digit1", relativeSize, 880, 560, 3); // skill 1 upgrade
addTapShortcuts("Digit2", relativeSize, 960, 430, 3); // skill 2 upgrade
addTapShortcuts("Digit3", relativeSize, 1090, 350, 3); // skill 3 upgrade
addTapShortcuts("Digit5", relativeSize, 130, 300, 3); // quick buy 1
addTapShortcuts("Digit6", relativeSize, 130, 370, 3); // quick buy 2
addObservationShortcuts("M3", 1000, 200, 0.5, 4); // observation addObservationShortcuts("M3", relativeSize, 1000, 200, 0.5, 4); // observation
// panel // panel
addShortcut( addShortcut(
"Tab", "Tab",
async () => { async () => {
await execMacro([ await execMacro(relativeSize, [
{ {
type: "touch", type: "touch",
args: ["default", 5, 1185, 40], args: ["default", 5, 1185, 40],
@ -882,7 +937,7 @@ export function initShortcuts(element: HTMLElement) {
}, },
undefined, undefined,
async () => { async () => {
await execMacro([ await execMacro(relativeSize, [
{ {
type: "touch", type: "touch",
args: ["default", 5, 1220, 100], args: ["default", 5, 1220, 100],
@ -895,7 +950,7 @@ export function initShortcuts(element: HTMLElement) {
addShortcut( addShortcut(
"ShiftLeft", "ShiftLeft",
async () => { async () => {
await execMacro([ await execMacro(relativeSize, [
{ {
type: "touch", type: "touch",
args: ["default", 5, 40, 300], args: ["default", 5, 40, 300],
@ -904,7 +959,7 @@ export function initShortcuts(element: HTMLElement) {
}, },
undefined, undefined,
async () => { async () => {
await execMacro([ await execMacro(relativeSize, [
{ {
type: "touch", type: "touch",
args: ["default", 5, 1200, 60], args: ["default", 5, 1200, 60],
@ -917,7 +972,7 @@ export function initShortcuts(element: HTMLElement) {
addShortcut( addShortcut(
"KeyZ", "KeyZ",
async () => { async () => {
await execMacro([ await execMacro(relativeSize, [
{ {
type: "touch", type: "touch",
args: ["default", 5, 250, 230], args: ["default", 5, 250, 230],
@ -926,7 +981,7 @@ export function initShortcuts(element: HTMLElement) {
}, },
undefined, undefined,
async () => { async () => {
await execMacro([ await execMacro(relativeSize, [
{ {
type: "touch", type: "touch",
args: ["default", 5, 640, 150], args: ["default", 5, 640, 150],