using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; namespace AxibugEmuOnline.Client.InputDevices { public class InputDevicesManager { InputResolver m_inputResolver = InputResolver.Create(); Dictionary m_devices = new Dictionary(); public InputDevicesManager() { m_inputResolver.OnDeviceConnected += OnDeviceConnected; m_inputResolver.OnDeviceLost += OnDeviceLost; foreach (var device in m_inputResolver.GetDevices()) AddDevice(device); } private void OnDeviceLost(InputDevice lostDevice) { } private void OnDeviceConnected(InputDevice connectDevice) { throw new System.NotImplementedException(); } public void AddDevice(InputDevice device) { m_devices[device.UniqueName] = device; } public void RemoveDevice(InputDevice device) { m_devices.Remove(device.UniqueName); } public InputDevice.KeyBase 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.FindKeyByKeyName(keyName); return key; } public InputDevice FindDeviceByName(string deviceName) { m_devices.TryGetValue(deviceName, out var device); return device; } /// /// 获得键盘设备 /// 键盘设备被设计为有且仅有一个,所以这里应该总是能获得键盘设备 /// public KeyBoard GetKeyboard() { foreach (var d in m_devices.Values) { if (d is KeyBoard kb) return kb; } return null; } /// 由外部驱动的逻辑更新入口 public void Update() { foreach (var device in m_devices.Values) device.Update(); } } public abstract class InputDevice { public abstract string UniqueName { get; } public delegate void OnKeyStateChangedHandle(KeyBase sender); public event OnKeyStateChangedHandle OnKeyStateChanged; /// 指示该设备是否在线 public abstract bool Online { get; } /// 指示该设备当前帧是否有任意键被按下 public bool AnyKeyDown { get; private set; } protected Dictionary m_keyMapper = new Dictionary(); protected InputResolver m_resolver; public InputDevice(InputResolver resolver) { m_resolver = resolver; foreach (var key in DefineKeys()) { m_keyMapper[key.KeyName] = key; } } public void Update() { AnyKeyDown = false; foreach (var key in m_keyMapper.Values) { if (key.GetButtonDown()) { AnyKeyDown = true; RaiseKeyEvent(key); } } } protected abstract IEnumerable DefineKeys(); protected void RaiseKeyEvent(KeyBase sender) { OnKeyStateChanged?.Invoke(sender); } public KeyBase FindKeyByKeyName(string keyName) { m_keyMapper.TryGetValue(keyName, out var key); return key; } /// /// 输入设备的键接口 /// public abstract class KeyBase { public InputDevice HostDevice { get; internal set; } /// 获取该键是否在当前调用帧被按下 public abstract bool GetButtonDown(); /// 获取该键是否在当前调用帧被抬起 public abstract bool GetButtonUp(); /// 获取该键是否在当前调用帧是否处于按下状态 public abstract bool IsPressing(); public virtual Vector2 GetVector2() { throw new System.NotImplementedException(); } public virtual float GetFlaot() { throw new System.NotImplementedException(); } /// 键名 public abstract string KeyName { get; } public string GetPath() { return $"{HostDevice.UniqueName}/{KeyName}"; } } } }