feat(KeyBoard): check key unique

This commit is contained in:
AkiChase 2024-04-28 14:29:04 +08:00
parent 716f3f508b
commit 3b4cb410dc
3 changed files with 80 additions and 20 deletions

View File

@ -4,8 +4,9 @@ import KeyInfo from "./KeyInfo.vue";
import KeySetting from "./KeySetting.vue"; import KeySetting from "./KeySetting.vue";
import KeyCommon from "./KeyCommon.vue"; import KeyCommon from "./KeyCommon.vue";
import KeySteeringWheel from "./KeySteeringWheel.vue"; import KeySteeringWheel from "./KeySteeringWheel.vue";
import { KeySteeringWheel as KeyMappingSteeringWheel } from "../../keyMappingConfig";
import { useGlobalStore } from "../../store/global"; import { useGlobalStore } from "../../store/global";
import { useDialog } from "naive-ui"; import { useDialog, useMessage } from "naive-ui";
import { onBeforeRouteLeave } from "vue-router"; import { onBeforeRouteLeave } from "vue-router";
// TODO KeyMacroKeyCancelSkillKeyTap // TODO KeyMacroKeyCancelSkillKeyTap
@ -25,12 +26,42 @@ const showKeyInfoFlag = ref(false);
const showSettingFlag = ref(false); const showSettingFlag = ref(false);
const store = useGlobalStore(); const store = useGlobalStore();
const dialog = useDialog(); const dialog = useDialog();
const message = useMessage();
const activeButtonIndex = ref(-1); const activeButtonIndex = ref(-1);
const activeSteeringWheelButtonKeyIndex = ref(-1); const activeSteeringWheelButtonKeyIndex = ref(-1);
let edited = ref(false); let edited = ref(false);
function isKeyUnique(curKey: string): boolean {
const set = new Set<string>();
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) { function setCurButtonKey(curKey: string) {
if (!isKeyUnique(curKey)) {
message.error("按键重复:" + curKey);
return;
}
const keyMapping = store.editKeyMappingList[activeButtonIndex.value]; const keyMapping = store.editKeyMappingList[activeButtonIndex.value];
if (keyMapping.type === "SteeringWheel") { if (keyMapping.type === "SteeringWheel") {
const keyObject = keyMapping.key as { const keyObject = keyMapping.key as {
@ -39,19 +70,18 @@ function setCurButtonKey(curKey: string) {
up: string; up: string;
down: string; down: string;
}; };
switch (activeSteeringWheelButtonKeyIndex.value) { const nameList: ["up", "down", "left", "right"] = [
case 0: "up",
keyObject.up = curKey; "down",
break; "left",
case 1: "right",
keyObject.down = curKey; ];
break; if (
case 2: activeSteeringWheelButtonKeyIndex.value >= 0 &&
keyObject.left = curKey; activeSteeringWheelButtonKeyIndex.value <= 3
break; ) {
case 3: const curName = nameList[activeSteeringWheelButtonKeyIndex.value];
keyObject.right = curKey; keyObject[curName] = curKey;
break;
} }
} else { } else {
keyMapping.key = curKey; keyMapping.key = curKey;
@ -143,8 +173,11 @@ onBeforeRouteLeave(() => {
positiveText: "保存", positiveText: "保存",
negativeText: "取消", negativeText: "取消",
onPositiveClick: () => { onPositiveClick: () => {
store.applyEditKeyMappingList(); if (store.applyEditKeyMappingList()) {
edited.value = false; edited.value = false;
} else {
message.error("存在重复按键,无法保存");
}
}, },
onNegativeClick: () => { onNegativeClick: () => {
store.resetEditKeyMappingList(); store.resetEditKeyMappingList();

View File

@ -235,8 +235,11 @@ function exportKeyMappingConfig() {
} }
function saveKeyMappingConfig() { function saveKeyMappingConfig() {
store.applyEditKeyMappingList(); if (store.applyEditKeyMappingList()) {
edited.value = false; edited.value = false;
} else {
message.error("存在重复按键,无法保存");
}
} }
function resetKeyMappingConfig() { function resetKeyMappingConfig() {

View File

@ -1,7 +1,11 @@
import { defineStore } from "pinia"; import { defineStore } from "pinia";
import { Ref, ref } from "vue"; import { Ref, ref } from "vue";
import { Device } from "../invoke"; import { Device } from "../invoke";
import { KeyMapping, KeyMappingConfig } from "../keyMappingConfig"; import {
KeyMapping,
KeyMappingConfig,
KeySteeringWheel,
} from "../keyMappingConfig";
import { Store } from "@tauri-apps/plugin-store"; import { Store } from "@tauri-apps/plugin-store";
const localStore = new Store("store.bin"); const localStore = new Store("store.bin");
@ -30,10 +34,30 @@ export const useGlobalStore = defineStore("counter", () => {
const curKeyMappingIndex = ref(0); const curKeyMappingIndex = ref(0);
const editKeyMappingList: Ref<KeyMapping[]> = ref([]); const editKeyMappingList: Ref<KeyMapping[]> = ref([]);
function applyEditKeyMappingList() { function applyEditKeyMappingList(): boolean {
const set = new Set<string>();
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 = keyMappingConfigList.value[curKeyMappingIndex.value].list =
editKeyMappingList.value; editKeyMappingList.value;
localStore.set("keyMappingConfigList", keyMappingConfigList.value); localStore.set("keyMappingConfigList", keyMappingConfigList.value);
return true;
} }
function resetEditKeyMappingList() { function resetEditKeyMappingList() {