2024-04-13 09:53:41 +08:00
|
|
|
<script setup lang="ts">
|
2024-04-25 22:48:05 +08:00
|
|
|
import { Ref, onActivated, ref } from "vue";
|
2024-04-26 23:07:26 +08:00
|
|
|
import { NDialog, useMessage } from "naive-ui";
|
2024-04-13 09:53:41 +08:00
|
|
|
import { useGlobalStore } from "../store/global";
|
2024-04-14 17:15:39 +08:00
|
|
|
import { onBeforeRouteLeave, useRouter } from "vue-router";
|
2024-04-16 22:38:07 +08:00
|
|
|
import {
|
2024-04-18 11:03:19 +08:00
|
|
|
applyShortcuts,
|
|
|
|
clearShortcuts,
|
2024-04-16 22:38:07 +08:00
|
|
|
listenToKeyEvent,
|
|
|
|
unlistenToKeyEvent,
|
|
|
|
updateScreenSizeAndMaskArea,
|
|
|
|
} from "../hotkey";
|
|
|
|
import { getCurrent } from "@tauri-apps/api/window";
|
2024-04-13 09:53:41 +08:00
|
|
|
|
|
|
|
const store = useGlobalStore();
|
|
|
|
const router = useRouter();
|
2024-04-26 23:07:26 +08:00
|
|
|
const message = useMessage();
|
2024-04-13 09:53:41 +08:00
|
|
|
|
2024-04-25 22:48:05 +08:00
|
|
|
const renderedButtons: Ref<any[]> = ref([]);
|
|
|
|
|
2024-04-14 17:15:39 +08:00
|
|
|
onBeforeRouteLeave(() => {
|
2024-04-27 17:43:20 +08:00
|
|
|
const maskElement = document.getElementById("maskElement") as HTMLElement;
|
2024-04-27 09:26:14 +08:00
|
|
|
|
|
|
|
if (store.controledDevice) {
|
2024-04-18 11:03:19 +08:00
|
|
|
unlistenToKeyEvent();
|
2024-04-27 09:26:14 +08:00
|
|
|
clearShortcuts(maskElement);
|
2024-04-14 17:15:39 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-13 09:53:41 +08:00
|
|
|
onActivated(async () => {
|
2024-04-27 17:43:20 +08:00
|
|
|
const maskElement = document.getElementById("maskElement") as HTMLElement;
|
2024-04-27 09:26:14 +08:00
|
|
|
|
|
|
|
if (store.controledDevice) {
|
2024-04-18 11:03:19 +08:00
|
|
|
const mt = 30;
|
|
|
|
const ml = 70;
|
|
|
|
const appWindow = getCurrent();
|
|
|
|
const size = (await appWindow.outerSize()).toLogical(
|
|
|
|
await appWindow.scaleFactor()
|
|
|
|
);
|
|
|
|
updateScreenSizeAndMaskArea(
|
|
|
|
[store.screenSizeW, store.screenSizeH],
|
|
|
|
[size.width - ml, size.height - mt]
|
|
|
|
);
|
|
|
|
|
2024-04-26 23:07:26 +08:00
|
|
|
if (
|
|
|
|
applyShortcuts(
|
2024-04-27 09:26:14 +08:00
|
|
|
maskElement,
|
2024-04-26 23:07:26 +08:00
|
|
|
store.keyMappingConfigList[store.curKeyMappingIndex]
|
|
|
|
)
|
|
|
|
) {
|
2024-04-27 17:43:20 +08:00
|
|
|
refreshKeyMappingButton();
|
2024-04-26 23:07:26 +08:00
|
|
|
listenToKeyEvent();
|
2024-04-27 09:26:14 +08:00
|
|
|
} else {
|
|
|
|
message.error("按键方案异常,请删除此方案");
|
2024-04-26 23:07:26 +08:00
|
|
|
}
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function toStartServer() {
|
|
|
|
router.replace({ name: "device" });
|
|
|
|
}
|
|
|
|
|
2024-04-25 22:48:05 +08:00
|
|
|
function refreshKeyMappingButton() {
|
2024-04-27 17:43:20 +08:00
|
|
|
const maskElement = document.getElementById("maskElement") as HTMLElement;
|
2024-04-27 09:26:14 +08:00
|
|
|
|
2024-04-26 23:07:26 +08:00
|
|
|
const curKeyMappingConfig =
|
|
|
|
store.keyMappingConfigList[store.curKeyMappingIndex];
|
2024-04-25 22:48:05 +08:00
|
|
|
const relativeSize = curKeyMappingConfig.relativeSize;
|
2024-04-27 09:26:14 +08:00
|
|
|
const maskSizeW = maskElement.clientWidth;
|
|
|
|
const maskSizeH = maskElement.clientHeight;
|
2024-04-27 17:43:20 +08:00
|
|
|
const relativePosToMaskPos = (x: number, y: number) => {
|
|
|
|
return {
|
|
|
|
x: Math.round((x / relativeSize.w) * maskSizeW),
|
|
|
|
y: Math.round((y / relativeSize.h) * maskSizeH),
|
2024-04-25 22:48:05 +08:00
|
|
|
};
|
2024-04-27 17:43:20 +08:00
|
|
|
};
|
|
|
|
const buttons = [];
|
|
|
|
for (let keyObject of curKeyMappingConfig.list) {
|
|
|
|
const { x, y } = relativePosToMaskPos(keyObject.posX, keyObject.posY);
|
|
|
|
buttons.push({
|
|
|
|
...keyObject,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
});
|
2024-04-25 22:48:05 +08:00
|
|
|
}
|
2024-04-27 17:43:20 +08:00
|
|
|
renderedButtons.value = buttons;
|
2024-04-25 22:48:05 +08:00
|
|
|
}
|
2024-04-13 09:53:41 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div v-show="!store.controledDevice" class="notice">
|
|
|
|
<div class="content">
|
|
|
|
<NDialog
|
|
|
|
:closable="false"
|
|
|
|
title="未找到受控设备"
|
|
|
|
content="请启动服务端并控制任意设备"
|
|
|
|
positive-text="去启动"
|
|
|
|
type="warning"
|
|
|
|
@positive-click="toStartServer"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
v-show="store.controledDevice"
|
2024-04-14 17:15:39 +08:00
|
|
|
@contextmenu.prevent
|
2024-04-13 09:53:41 +08:00
|
|
|
class="mask"
|
2024-04-27 17:43:20 +08:00
|
|
|
id="maskElement"
|
2024-04-25 22:48:05 +08:00
|
|
|
>
|
|
|
|
<template v-for="button in renderedButtons">
|
|
|
|
<div
|
|
|
|
v-if="button.type === 'SteeringWheel'"
|
|
|
|
class="mask-steering-wheel"
|
|
|
|
:style="{
|
|
|
|
left: button.x - 75 + 'px',
|
|
|
|
top: button.y - 75 + 'px',
|
|
|
|
}"
|
|
|
|
>
|
|
|
|
<div class="wheel-container">
|
2024-04-28 14:05:52 +08:00
|
|
|
<i />
|
2024-04-26 17:24:07 +08:00
|
|
|
<span>{{ button.key.up }}</span>
|
2024-04-28 14:05:52 +08:00
|
|
|
<i />
|
2024-04-26 17:24:07 +08:00
|
|
|
<span>{{ button.key.left }}</span>
|
2024-04-28 14:05:52 +08:00
|
|
|
<i />
|
2024-04-26 17:24:07 +08:00
|
|
|
<span>{{ button.key.right }}</span>
|
2024-04-28 14:05:52 +08:00
|
|
|
<i />
|
2024-04-26 17:24:07 +08:00
|
|
|
<span>{{ button.key.down }}</span>
|
2024-04-28 14:05:52 +08:00
|
|
|
<i />
|
2024-04-25 22:48:05 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
v-else
|
|
|
|
class="mask-button"
|
|
|
|
:style="{
|
|
|
|
left: button.x + 'px',
|
|
|
|
top: button.y - 14 + 'px',
|
|
|
|
}"
|
|
|
|
>
|
|
|
|
{{ button.key }}
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
</div>
|
2024-04-13 09:53:41 +08:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<style scoped lang="scss">
|
|
|
|
.mask {
|
2024-04-16 22:50:21 +08:00
|
|
|
background-color: transparent;
|
2024-04-13 09:53:41 +08:00
|
|
|
overflow: hidden;
|
2024-04-14 17:15:39 +08:00
|
|
|
cursor: pointer;
|
2024-04-25 22:48:05 +08:00
|
|
|
position: relative;
|
|
|
|
border-right: 1px solid var(--bg-color);
|
|
|
|
border-bottom: 1px solid var(--bg-color);
|
|
|
|
border-radius: 0 0 5px 0;
|
|
|
|
|
|
|
|
& > .mask-button {
|
|
|
|
position: absolute;
|
|
|
|
background-color: rgba(0, 0, 0, 0.2);
|
|
|
|
color: rgba(255, 255, 255, 0.6);
|
|
|
|
border-radius: 5px;
|
|
|
|
padding: 5px;
|
|
|
|
font-size: 12px;
|
|
|
|
}
|
|
|
|
|
|
|
|
& > .mask-steering-wheel {
|
|
|
|
position: absolute;
|
|
|
|
background-color: rgba(0, 0, 0, 0.2);
|
|
|
|
color: rgba(255, 255, 255, 0.6);
|
|
|
|
border-radius: 50%;
|
|
|
|
width: 150px;
|
|
|
|
height: 150px;
|
|
|
|
font-size: 12px;
|
|
|
|
|
|
|
|
.wheel-container {
|
|
|
|
display: grid;
|
|
|
|
grid-template-columns: repeat(3, 50px);
|
|
|
|
grid-template-rows: repeat(3, 50px);
|
|
|
|
justify-items: center;
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
}
|
2024-04-13 09:53:41 +08:00
|
|
|
}
|
|
|
|
.notice {
|
2024-04-16 22:50:21 +08:00
|
|
|
background-color: rgba(0, 0, 0, 0.5);
|
2024-04-13 09:53:41 +08:00
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
.content {
|
|
|
|
width: 80%;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|