154 lines
4.5 KiB
C#
154 lines
4.5 KiB
C#
/*
|
|
* @Author: 白哉
|
|
* @Description:
|
|
* @Date: 2025年09月05日 星期五 15:09:36
|
|
* @Modify:
|
|
*/
|
|
|
|
|
|
using System.Collections.Generic;
|
|
using MemoryPack;
|
|
using UnityEngine;
|
|
|
|
|
|
namespace Logic
|
|
{
|
|
public enum InputEnum
|
|
{
|
|
Check,
|
|
Cancel,
|
|
SwitchTech,
|
|
SwitchHero,
|
|
SwitchRanking,
|
|
NextTurn,
|
|
SwitchUnit,
|
|
Action1,
|
|
Action2,
|
|
Action3,
|
|
Action4,
|
|
Action5,
|
|
Action6,
|
|
Action7,
|
|
Action8,
|
|
Action9
|
|
}
|
|
|
|
|
|
[MemoryPackable]
|
|
public partial class InputConfig
|
|
{
|
|
public List<InputKeyConfig> InputKeyConfigs = new List<InputKeyConfig>();
|
|
private Dictionary<InputEnum, InputKeyConfig> _inputKeyConfigDict = new Dictionary<InputEnum, InputKeyConfig>();
|
|
|
|
public void Init()
|
|
{
|
|
_inputKeyConfigDict.Clear();
|
|
foreach (var inputKeyConfig in InputKeyConfigs)
|
|
_inputKeyConfigDict[inputKeyConfig.InputType] = inputKeyConfig;
|
|
// 遍历 InputEnum 填充InputKeyConfigs
|
|
foreach (InputEnum inputEnum in System.Enum.GetValues(typeof(InputEnum)))
|
|
{
|
|
if (_inputKeyConfigDict.ContainsKey(inputEnum)) continue;
|
|
var newConfig = new InputKeyConfig() { InputType = inputEnum };
|
|
InputKeyConfigs.Add(newConfig);
|
|
_inputKeyConfigDict[inputEnum] = newConfig;
|
|
}
|
|
CheckConfigConflict();
|
|
}
|
|
|
|
public void SetInputKeyConfig(InputEnum inputEnum, List<KeyCode> keyCodes)
|
|
{
|
|
if (!_inputKeyConfigDict.TryGetValue(inputEnum, out var config)) return;
|
|
|
|
config.KeyCodes.Clear();
|
|
foreach (var keyCode in keyCodes) config.KeyCodes.Add(keyCode);
|
|
config.SortModifierKeysFirst();
|
|
CheckConfigConflict();
|
|
}
|
|
|
|
public InputKeyConfig GetInputKeyConfig(InputEnum inputEnum)
|
|
{
|
|
_inputKeyConfigDict.TryGetValue(inputEnum, out var config);
|
|
return config;
|
|
}
|
|
|
|
private void CheckConfigConflict()
|
|
{
|
|
// 先重置所有冲突标记
|
|
foreach (var config in InputKeyConfigs)
|
|
{
|
|
config.IsConflict = false;
|
|
}
|
|
|
|
// 两两比较检测冲突
|
|
for (int i = 0; i < InputKeyConfigs.Count; i++)
|
|
{
|
|
for (int j = i + 1; j < InputKeyConfigs.Count; j++)
|
|
{
|
|
var configA = InputKeyConfigs[i];
|
|
var configB = InputKeyConfigs[j];
|
|
|
|
if (IsKeyCodesEqual(configA.KeyCodes, configB.KeyCodes))
|
|
{
|
|
configA.IsConflict = true;
|
|
configB.IsConflict = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 比较两个按键组合是否相同且不为空
|
|
private bool IsKeyCodesEqual(List<KeyCode> keysA, List<KeyCode> keysB)
|
|
{
|
|
// 任一为空或无按键则不算冲突
|
|
if (keysA == null || keysB == null) return false;
|
|
if (keysA.Count == 0 || keysB.Count == 0) return false;
|
|
if (keysA.Count != keysB.Count) return false;
|
|
|
|
// 由于已经排序过,直接按顺序比较
|
|
for (int i = 0; i < keysA.Count; i++)
|
|
{
|
|
if (keysA[i] != keysB[i])
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
[MemoryPackable]
|
|
public partial class InputKeyConfig
|
|
{
|
|
public InputEnum InputType;
|
|
public List<KeyCode> KeyCodes = new List<KeyCode>();
|
|
[MemoryPackIgnore]
|
|
public bool IsConflict = false;
|
|
[MemoryPackIgnore]
|
|
public bool IsActive;
|
|
|
|
|
|
public string GetKeyCombinationString()
|
|
{
|
|
if (KeyCodes == null || KeyCodes.Count == 0) return "未设置";
|
|
return string.Join(" + ", KeyCodes);
|
|
}
|
|
|
|
public void SortModifierKeysFirst()
|
|
{
|
|
KeyCodes.Sort((a, b) => GetKeyPriority(a).CompareTo(GetKeyPriority(b)));
|
|
}
|
|
|
|
private int GetKeyPriority(KeyCode key)
|
|
{
|
|
// 修饰键优先级更高(数值更小排在前面)
|
|
return key switch
|
|
{
|
|
KeyCode.LeftControl or KeyCode.RightControl => 0,
|
|
KeyCode.LeftShift or KeyCode.RightShift => 1,
|
|
KeyCode.LeftAlt or KeyCode.RightAlt => 2,
|
|
_ => 10
|
|
};
|
|
}
|
|
}
|
|
} |