From 3b4cb410dc0a20ef8bee31ecd94869c016a45fa6 Mon Sep 17 00:00:00 2001 From: AkiChase <1003019131@qq.com> Date: Sun, 28 Apr 2024 14:29:04 +0800 Subject: [PATCH] feat(KeyBoard): check key unique --- src/components/keyboard/KeyBoard.vue | 65 +++++++++++++++++++------- src/components/keyboard/KeySetting.vue | 7 ++- src/store/global.ts | 28 ++++++++++- 3 files changed, 80 insertions(+), 20 deletions(-) diff --git a/src/components/keyboard/KeyBoard.vue b/src/components/keyboard/KeyBoard.vue index 3ec01a5..b9a4d0a 100644 --- a/src/components/keyboard/KeyBoard.vue +++ b/src/components/keyboard/KeyBoard.vue @@ -4,8 +4,9 @@ import KeyInfo from "./KeyInfo.vue"; import KeySetting from "./KeySetting.vue"; import KeyCommon from "./KeyCommon.vue"; import KeySteeringWheel from "./KeySteeringWheel.vue"; +import { KeySteeringWheel as KeyMappingSteeringWheel } from "../../keyMappingConfig"; import { useGlobalStore } from "../../store/global"; -import { useDialog } from "naive-ui"; +import { useDialog, useMessage } from "naive-ui"; import { onBeforeRouteLeave } from "vue-router"; // TODO 普通按钮 KeyMacro,KeyCancelSkill,KeyTap @@ -25,12 +26,42 @@ const showKeyInfoFlag = ref(false); const showSettingFlag = ref(false); const store = useGlobalStore(); const dialog = useDialog(); +const message = useMessage(); const activeButtonIndex = ref(-1); const activeSteeringWheelButtonKeyIndex = ref(-1); let edited = ref(false); +function isKeyUnique(curKey: string): boolean { + const set = new Set(); + for (const keyMapping of store.editKeyMappingList) { + if (keyMapping.type === "SteeringWheel") { + const nameList: ["up", "down", "left", "right"] = [ + "up", + "down", + "left", + "right", + ]; + for (const name of nameList) { + if (set.has((keyMapping as KeyMappingSteeringWheel).key[name])) + return false; + set.add((keyMapping as KeyMappingSteeringWheel).key[name]); + } + } else { + if (set.has(keyMapping.key as string)) return false; + set.add(keyMapping.key as string); + } + } + if (set.has(curKey)) return false; + return true; +} + function setCurButtonKey(curKey: string) { + if (!isKeyUnique(curKey)) { + message.error("按键重复:" + curKey); + return; + } + const keyMapping = store.editKeyMappingList[activeButtonIndex.value]; if (keyMapping.type === "SteeringWheel") { const keyObject = keyMapping.key as { @@ -39,19 +70,18 @@ function setCurButtonKey(curKey: string) { up: string; down: string; }; - switch (activeSteeringWheelButtonKeyIndex.value) { - case 0: - keyObject.up = curKey; - break; - case 1: - keyObject.down = curKey; - break; - case 2: - keyObject.left = curKey; - break; - case 3: - keyObject.right = curKey; - break; + const nameList: ["up", "down", "left", "right"] = [ + "up", + "down", + "left", + "right", + ]; + if ( + activeSteeringWheelButtonKeyIndex.value >= 0 && + activeSteeringWheelButtonKeyIndex.value <= 3 + ) { + const curName = nameList[activeSteeringWheelButtonKeyIndex.value]; + keyObject[curName] = curKey; } } else { keyMapping.key = curKey; @@ -143,8 +173,11 @@ onBeforeRouteLeave(() => { positiveText: "保存", negativeText: "取消", onPositiveClick: () => { - store.applyEditKeyMappingList(); - edited.value = false; + if (store.applyEditKeyMappingList()) { + edited.value = false; + } else { + message.error("存在重复按键,无法保存"); + } }, onNegativeClick: () => { store.resetEditKeyMappingList(); diff --git a/src/components/keyboard/KeySetting.vue b/src/components/keyboard/KeySetting.vue index 5fd98d9..1b7c8a6 100644 --- a/src/components/keyboard/KeySetting.vue +++ b/src/components/keyboard/KeySetting.vue @@ -235,8 +235,11 @@ function exportKeyMappingConfig() { } function saveKeyMappingConfig() { - store.applyEditKeyMappingList(); - edited.value = false; + if (store.applyEditKeyMappingList()) { + edited.value = false; + } else { + message.error("存在重复按键,无法保存"); + } } function resetKeyMappingConfig() { diff --git a/src/store/global.ts b/src/store/global.ts index ab3660b..6d63c7e 100644 --- a/src/store/global.ts +++ b/src/store/global.ts @@ -1,7 +1,11 @@ import { defineStore } from "pinia"; import { Ref, ref } from "vue"; import { Device } from "../invoke"; -import { KeyMapping, KeyMappingConfig } from "../keyMappingConfig"; +import { + KeyMapping, + KeyMappingConfig, + KeySteeringWheel, +} from "../keyMappingConfig"; import { Store } from "@tauri-apps/plugin-store"; const localStore = new Store("store.bin"); @@ -30,10 +34,30 @@ export const useGlobalStore = defineStore("counter", () => { const curKeyMappingIndex = ref(0); const editKeyMappingList: Ref = ref([]); - function applyEditKeyMappingList() { + function applyEditKeyMappingList(): boolean { + const set = new Set(); + for (const keyMapping of editKeyMappingList.value) { + if (keyMapping.type === "SteeringWheel") { + const nameList: ["up", "down", "left", "right"] = [ + "up", + "down", + "left", + "right", + ]; + for (const name of nameList) { + if (set.has((keyMapping as KeySteeringWheel).key[name])) return false; + set.add((keyMapping as KeySteeringWheel).key[name]); + } + } else { + if (set.has(keyMapping.key as string)) return false; + set.add(keyMapping.key as string); + } + } + keyMappingConfigList.value[curKeyMappingIndex.value].list = editKeyMappingList.value; localStore.set("keyMappingConfigList", keyMappingConfigList.value); + return true; } function resetEditKeyMappingList() {