diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/KeyMapperSetting.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/KeyMapperSetting.cs index c18b21fa..c6d6a001 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/KeyMapperSetting.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/KeyMapperSetting.cs @@ -55,6 +55,7 @@ namespace AxibugEmuOnline.Client where T : Enum { List<BindingPage> m_bindingPages = new List<BindingPage>(); + KeyBoard m_currentKeyboard; public EmuCoreControllerKeyBinding() { @@ -62,7 +63,31 @@ namespace AxibugEmuOnline.Client { 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() @@ -71,19 +96,21 @@ namespace AxibugEmuOnline.Client } /// <summary> - /// 加载默认键盘映射 + /// 加载键盘映射配置 /// </summary> - public void LoadDefaultKeyboardMapper() + void LoadKeyboardMapper() { foreach (var binding in m_bindingPages) { binding.ClearKeyboardBinding(); - var keyboard = App.inputDevicesMgr.GetKeyboard(); - if (keyboard != null) OnLoadDefaultKeyboardMapper(keyboard, binding); + if (m_currentKeyboard != null) OnLoadKeyboardMapper(m_currentKeyboard, 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) { @@ -203,7 +230,7 @@ namespace AxibugEmuOnline.Client /// <summary> /// 移除与键盘设备建立的绑定设置 /// </summary> - public void ClearKeyboardBinding() + internal void ClearKeyboardBinding() { foreach (var list in m_mapSetting.Values) { diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/NesKeyBinding.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/NesKeyBinding.cs index 8258e202..b0632ffe 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/NesKeyBinding.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/NesKeyBinding.cs @@ -10,7 +10,7 @@ namespace AxibugEmuOnline.Client public override RomPlatformType Platform => RomPlatformType.Nes; public override int ControllerCount => 4; - protected override void OnLoadDefaultKeyboardMapper(KeyBoard keyboard, BindingPage binding) + protected override void OnLoadKeyboardMapper(KeyBoard keyboard, BindingPage binding) { switch (binding.ControllerIndex) { diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/XMBKeyBinding.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/XMBKeyBinding.cs index a5525e65..bd31c72e 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/XMBKeyBinding.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppSettings/KeyMapperSetting/XMBKeyBinding.cs @@ -9,7 +9,7 @@ namespace AxibugEmuOnline.Client public override RomPlatformType Platform => RomPlatformType.Invalid; public override int ControllerCount => 2; - protected override void OnLoadDefaultKeyboardMapper(KeyBoard keyboard, BindingPage binding) + protected override void OnLoadKeyboardMapper(KeyBoard keyboard, BindingPage binding) { switch (binding.ControllerIndex) { diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/InputDevicesManager/InputDevicesManager.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/InputDevicesManager/InputDevicesManager.cs index 45183e11..ad1a73f6 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/InputDevicesManager/InputDevicesManager.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/InputDevicesManager/InputDevicesManager.cs @@ -8,53 +8,39 @@ namespace AxibugEmuOnline.Client.InputDevices InputResolver m_inputResolver = InputResolver.Create(); 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() { - m_inputResolver.OnDeviceConnected += OnDeviceConnected; - m_inputResolver.OnDeviceLost += OnDeviceLost; + m_inputResolver.OnDeviceConnected += Resolver_OnDeviceConnected; + m_inputResolver.OnDeviceLost += Resolver_OnDeviceLost; foreach (var device in m_inputResolver.GetDevices()) AddDevice(device); } - private void OnDeviceLost(InputDevice lostDevice) + private void Resolver_OnDeviceLost(InputDevice lostDevice) { RemoveDevice(lostDevice); } - private void OnDeviceConnected(InputDevice connectDevice) + private void Resolver_OnDeviceConnected(InputDevice connectDevice) { AddDevice(connectDevice); } - public void AddDevice(InputDevice device) + void AddDevice(InputDevice device) { m_devices[device.UniqueName] = device; + OnDeviceConnected?.Invoke(device); } - public void RemoveDevice(InputDevice device) + void RemoveDevice(InputDevice device) { m_devices.Remove(device.UniqueName); - } - - 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; + OnDeviceLost?.Invoke(device); } /// <summary>