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 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 KeyMacroKeyCancelSkillKeyTap
@ -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<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) {
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();
if (store.applyEditKeyMappingList()) {
edited.value = false;
} else {
message.error("存在重复按键,无法保存");
}
},
onNegativeClick: () => {
store.resetEditKeyMappingList();

View File

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

View File

@ -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<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 =
editKeyMappingList.value;
localStore.set("keyMappingConfigList", keyMappingConfigList.value);
return true;
}
function resetEditKeyMappingList() {