键盘映射配置加载机制修改,现在在初始化以及任何键盘设备断开和连接时,会检查可用键盘设备并且调用配置加载方法

This commit is contained in:
ALIENJACK\alien 2025-03-17 10:33:23 +08:00
parent 1c20707751
commit 3257e15dda
4 changed files with 49 additions and 36 deletions
AxibugEmuOnline.Client/Assets/Script/AppMain/Manager

View File

@ -55,6 +55,7 @@ namespace AxibugEmuOnline.Client
where T : Enum where T : Enum
{ {
List<BindingPage> m_bindingPages = new List<BindingPage>(); List<BindingPage> m_bindingPages = new List<BindingPage>();
KeyBoard m_currentKeyboard;
public EmuCoreControllerKeyBinding() public EmuCoreControllerKeyBinding()
{ {
@ -62,7 +63,31 @@ namespace AxibugEmuOnline.Client
{ {
m_bindingPages.Add(new BindingPage(i, this)); m_bindingPages.Add(new BindingPage(i, this));
} }
LoadDefaultKeyboardMapper(); m_currentKeyboard = App.inputDevicesMgr.GetKeyboard();
if (m_currentKeyboard != null)
LoadKeyboardMapper();
App.inputDevicesMgr.OnDeviceLost += InputDevicesMgr_OnDeviceLost;
App.inputDevicesMgr.OnDeviceConnected += InputDevicesMgr_OnDeviceConnected;
}
private void InputDevicesMgr_OnDeviceConnected(InputDevice connectDevice)
{
if (m_currentKeyboard == null && connectDevice is KeyBoard) //未建立键盘按键映射设置时,并且有新的键盘连接时,建立键盘映射设置
{
m_currentKeyboard = connectDevice as KeyBoard;
LoadKeyboardMapper();
}
}
private void InputDevicesMgr_OnDeviceLost(InputDevice lostDevice)
{
if (lostDevice == m_currentKeyboard) //当前键盘设备丢失,与其他键盘重新建立连接
{
m_currentKeyboard = App.inputDevicesMgr.GetKeyboard();
LoadKeyboardMapper();
}
} }
IEnumerable<T> DefineKeys() IEnumerable<T> DefineKeys()
@ -71,19 +96,21 @@ namespace AxibugEmuOnline.Client
} }
/// <summary> /// <summary>
/// 加载默认键盘映射 /// 加载键盘映射配置
/// </summary> /// </summary>
public void LoadDefaultKeyboardMapper() void LoadKeyboardMapper()
{ {
foreach (var binding in m_bindingPages) foreach (var binding in m_bindingPages)
{ {
binding.ClearKeyboardBinding(); binding.ClearKeyboardBinding();
var keyboard = App.inputDevicesMgr.GetKeyboard(); if (m_currentKeyboard != null) OnLoadKeyboardMapper(m_currentKeyboard, binding);
if (keyboard != null) OnLoadDefaultKeyboardMapper(keyboard, binding);
} }
} }
protected abstract void OnLoadDefaultKeyboardMapper(KeyBoard keyboard, BindingPage binding); /// <summary> 当加载键盘映射设置时触发 </summary>
/// <param name="keyboard"></param>
/// <param name="binding"></param>
protected abstract void OnLoadKeyboardMapper(KeyBoard keyboard, BindingPage binding);
public bool Start(T emuControl, int controllerIndex) public bool Start(T emuControl, int controllerIndex)
{ {
@ -203,7 +230,7 @@ namespace AxibugEmuOnline.Client
/// <summary> /// <summary>
/// 移除与键盘设备建立的绑定设置 /// 移除与键盘设备建立的绑定设置
/// </summary> /// </summary>
public void ClearKeyboardBinding() internal void ClearKeyboardBinding()
{ {
foreach (var list in m_mapSetting.Values) foreach (var list in m_mapSetting.Values)
{ {

View File

@ -10,7 +10,7 @@ namespace AxibugEmuOnline.Client
public override RomPlatformType Platform => RomPlatformType.Nes; public override RomPlatformType Platform => RomPlatformType.Nes;
public override int ControllerCount => 4; public override int ControllerCount => 4;
protected override void OnLoadDefaultKeyboardMapper(KeyBoard keyboard, BindingPage binding) protected override void OnLoadKeyboardMapper(KeyBoard keyboard, BindingPage binding)
{ {
switch (binding.ControllerIndex) switch (binding.ControllerIndex)
{ {

View File

@ -9,7 +9,7 @@ namespace AxibugEmuOnline.Client
public override RomPlatformType Platform => RomPlatformType.Invalid; public override RomPlatformType Platform => RomPlatformType.Invalid;
public override int ControllerCount => 2; public override int ControllerCount => 2;
protected override void OnLoadDefaultKeyboardMapper(KeyBoard keyboard, BindingPage binding) protected override void OnLoadKeyboardMapper(KeyBoard keyboard, BindingPage binding)
{ {
switch (binding.ControllerIndex) switch (binding.ControllerIndex)
{ {

View File

@ -8,53 +8,39 @@ namespace AxibugEmuOnline.Client.InputDevices
InputResolver m_inputResolver = InputResolver.Create(); InputResolver m_inputResolver = InputResolver.Create();
Dictionary<string, InputDevice> m_devices = new Dictionary<string, InputDevice>(); Dictionary<string, InputDevice> m_devices = new Dictionary<string, InputDevice>();
public delegate void OnDeviceConnectedHandle(InputDevice connectDevice);
public event OnDeviceConnectedHandle OnDeviceConnected;
public delegate void OnDeviceLostHandle(InputDevice lostDevice);
public event OnDeviceLostHandle OnDeviceLost;
public InputDevicesManager() public InputDevicesManager()
{ {
m_inputResolver.OnDeviceConnected += OnDeviceConnected; m_inputResolver.OnDeviceConnected += Resolver_OnDeviceConnected;
m_inputResolver.OnDeviceLost += OnDeviceLost; m_inputResolver.OnDeviceLost += Resolver_OnDeviceLost;
foreach (var device in m_inputResolver.GetDevices()) foreach (var device in m_inputResolver.GetDevices())
AddDevice(device); AddDevice(device);
} }
private void OnDeviceLost(InputDevice lostDevice) private void Resolver_OnDeviceLost(InputDevice lostDevice)
{ {
RemoveDevice(lostDevice); RemoveDevice(lostDevice);
} }
private void OnDeviceConnected(InputDevice connectDevice) private void Resolver_OnDeviceConnected(InputDevice connectDevice)
{ {
AddDevice(connectDevice); AddDevice(connectDevice);
} }
public void AddDevice(InputDevice device) void AddDevice(InputDevice device)
{ {
m_devices[device.UniqueName] = device; m_devices[device.UniqueName] = device;
OnDeviceConnected?.Invoke(device);
} }
public void RemoveDevice(InputDevice device) void RemoveDevice(InputDevice device)
{ {
m_devices.Remove(device.UniqueName); m_devices.Remove(device.UniqueName);
} OnDeviceLost?.Invoke(device);
public InputDevice.InputControl GetKeyByPath(string path)
{
var temp = path.Split("/");
Debug.Assert(temp.Length == 2, "Invalid Path Format");
var deviceName = temp[0];
var keyName = temp[1];
var targetDevice = FindDeviceByName(deviceName);
if (targetDevice == null) return null;
var key = targetDevice.FindControlByName(keyName);
return key;
}
public InputDevice FindDeviceByName(string deviceName)
{
m_devices.TryGetValue(deviceName, out var device);
return device;
} }
/// <summary> /// <summary>