完成按键输入按键显示
This commit is contained in:
@@ -79,13 +79,14 @@ namespace NBF
|
||||
{
|
||||
public string Name;
|
||||
public string Key;
|
||||
public bool ShowLeft;
|
||||
public UIInputButtonShowMode Mode;
|
||||
|
||||
public InputInvokeAttribute(string name, string key = "", bool showLeft = true)
|
||||
public InputInvokeAttribute(string name, UIInputButtonShowMode mode = UIInputButtonShowMode.None,
|
||||
string key = "")
|
||||
{
|
||||
Name = name;
|
||||
Key = key;
|
||||
ShowLeft = showLeft;
|
||||
Mode = mode;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using FairyGUI;
|
||||
using NBC;
|
||||
// using Rewired;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.InputSystem.Utilities;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
@@ -14,10 +17,22 @@ namespace NBF
|
||||
GamePad = 1
|
||||
}
|
||||
|
||||
public struct UIInputInvoke
|
||||
public enum UIInputButtonShowMode
|
||||
{
|
||||
None,
|
||||
MenuLeft,
|
||||
MenuRight,
|
||||
SubMenuLeft,
|
||||
SubMenuRight,
|
||||
BottomLeft,
|
||||
BottomRight,
|
||||
}
|
||||
|
||||
public class UIInputInvoke
|
||||
{
|
||||
public object UIObject;
|
||||
public InputInvokeAttribute InputInvoke;
|
||||
public MethodInfo MethodInfo;
|
||||
}
|
||||
|
||||
public class InputManager : MonoService<InputManager>
|
||||
@@ -25,6 +40,7 @@ namespace NBF
|
||||
public static bool IsOp1;
|
||||
public static bool IsOp2;
|
||||
|
||||
|
||||
public static event Action<bool> OnOp1Action;
|
||||
public static event Action<bool> OnOp2Action;
|
||||
|
||||
@@ -63,6 +79,9 @@ namespace NBF
|
||||
/// </summary>
|
||||
public static event Action<InteractiveObject> OnInteractiveObjectAction;
|
||||
|
||||
public static event Action OnUIInvokeChange;
|
||||
|
||||
|
||||
public static PlayerInputControl PlayerInputControl { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
@@ -77,6 +96,9 @@ namespace NBF
|
||||
|
||||
public static ControllerType ControllerType = ControllerType.KeyboardMouse;
|
||||
|
||||
|
||||
private InputActionMap _uiInputActionMap;
|
||||
|
||||
protected override void OnAwake()
|
||||
{
|
||||
InputCursorExtension.InputInit();
|
||||
@@ -140,6 +162,8 @@ namespace NBF
|
||||
actionMap.Enable();
|
||||
if (actionMap.name == "UI")
|
||||
{
|
||||
_uiInputActionMap = actionMap;
|
||||
CacheInputActionIcons(actionMap.actions);
|
||||
foreach (var action in actionMap.actions)
|
||||
{
|
||||
if (action.type == InputActionType.Button)
|
||||
@@ -147,6 +171,8 @@ namespace NBF
|
||||
action.performed += OnUIButtonPerformed;
|
||||
action.canceled += OnUIButtonCanceled;
|
||||
}
|
||||
|
||||
//var fileName = binding.effectivePath.Replace("<", "").Replace(">", "").Replace("/", "_");
|
||||
}
|
||||
}
|
||||
else if (actionMap.name == "Player")
|
||||
@@ -184,6 +210,7 @@ namespace NBF
|
||||
private void OnUIButtonCanceled(InputAction.CallbackContext context)
|
||||
{
|
||||
OnUICanceled?.Invoke(context.action.name);
|
||||
InvokeUIInput(context.action.name);
|
||||
}
|
||||
|
||||
private void OnPlayerButtonPerformed(InputAction.CallbackContext context)
|
||||
@@ -243,6 +270,7 @@ namespace NBF
|
||||
{
|
||||
OnUIPerformed?.Invoke(actionName);
|
||||
OnUICanceled?.Invoke(actionName);
|
||||
InvokeUIInput(actionName);
|
||||
}
|
||||
|
||||
#region UI界面按键管理
|
||||
@@ -250,12 +278,107 @@ namespace NBF
|
||||
private readonly Dictionary<object, List<UIInputInvoke>> _panelActions =
|
||||
new Dictionary<object, List<UIInputInvoke>>();
|
||||
|
||||
private Dictionary<string, string> _keyboardIcons = new Dictionary<string, string>();
|
||||
|
||||
public void On(object obj)
|
||||
{
|
||||
var ms = Reflection.GetMethodsAttribute<InputInvokeAttribute>(obj.GetType());
|
||||
List<UIInputInvoke> inputInvokes = new List<UIInputInvoke>();
|
||||
foreach (var kv in ms)
|
||||
{
|
||||
var invokeData = new UIInputInvoke()
|
||||
{
|
||||
MethodInfo = kv.Key,
|
||||
UIObject = obj,
|
||||
InputInvoke = kv.Value
|
||||
};
|
||||
inputInvokes.Add(invokeData);
|
||||
}
|
||||
|
||||
if (inputInvokes.Count > 0)
|
||||
{
|
||||
_panelActions[obj] = inputInvokes;
|
||||
}
|
||||
|
||||
OnUIInvokeChange?.Invoke();
|
||||
}
|
||||
|
||||
public void Off(object obj)
|
||||
{
|
||||
_panelActions.Remove(obj, out List<UIInputInvoke> inputInvokes);
|
||||
OnUIInvokeChange?.Invoke();
|
||||
}
|
||||
|
||||
|
||||
public void InvokeUIInput(string actionName)
|
||||
{
|
||||
foreach (var obj in _panelActions.Keys)
|
||||
{
|
||||
var invokes = _panelActions[obj];
|
||||
foreach (var uiInputInvoke in invokes)
|
||||
{
|
||||
if (uiInputInvoke.InputInvoke.Name != actionName) continue;
|
||||
if (uiInputInvoke.UIObject is IUIPanel panel)
|
||||
{
|
||||
if (!panel.IsShowing) continue;
|
||||
if (!panel.IsTop) continue;
|
||||
}
|
||||
else if (uiInputInvoke.UIObject is GObject gObject)
|
||||
{
|
||||
if (!gObject.visible) continue;
|
||||
}
|
||||
|
||||
uiInputInvoke.MethodInfo.Invoke(uiInputInvoke.UIObject, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<UIInputInvoke> GetUsableInvokes()
|
||||
{
|
||||
List<UIInputInvoke> ret = new List<UIInputInvoke>();
|
||||
|
||||
foreach (var keyValuePair in _panelActions.Values)
|
||||
{
|
||||
foreach (var uiInputInvoke in keyValuePair)
|
||||
{
|
||||
if (uiInputInvoke.UIObject is IUIPanel { IsTop: true })
|
||||
{
|
||||
ret.Add(uiInputInvoke);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public string GetUIKeyCode(string actionName)
|
||||
{
|
||||
if (_keyboardIcons.TryGetValue(actionName, out var keyboardIcon))
|
||||
{
|
||||
return keyboardIcon;
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
|
||||
private void CacheInputActionIcons(ReadOnlyArray<InputAction> actions)
|
||||
{
|
||||
_keyboardIcons.Clear();
|
||||
foreach (var inputAction in actions)
|
||||
{
|
||||
foreach (var binding in inputAction.bindings)
|
||||
{
|
||||
var path = binding.effectivePath.Replace("<", "").Replace(">", "").Replace("/", "_");
|
||||
if (path.Contains("Keyboard") || path.Contains("keyboard"))
|
||||
{
|
||||
path = path.Replace("Keyboard", "keyboard");
|
||||
_keyboardIcons.TryAdd(inputAction.name, path);
|
||||
|
||||
Log.Info($"ActionIcons {inputAction.name}={path}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace NBF.Setting
|
||||
public override string Group => SettingsDef.Group.Graphic;
|
||||
protected override int DefaultValue => _defaultResolution;
|
||||
public override string Tab => SettingsDef.Tab.Graphic;
|
||||
public override bool IsDropdown => true;
|
||||
// public override bool IsDropdown => true;
|
||||
|
||||
|
||||
static readonly (int w, int h)[] Classic16_9 =
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
using System;
|
||||
using FairyGUI;
|
||||
using NBC;
|
||||
using UIPanel = NBC.UIPanel;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class UIInputListenAttribute : BaseAttribute
|
||||
{
|
||||
}
|
||||
|
||||
public static class UIInputExtensions
|
||||
{
|
||||
public static void AutoListenInput(this GComponent panel)
|
||||
{
|
||||
var ms = Reflection.GetMethodsWithUIInputAttribute(panel.GetType(), typeof(UIInputListenAttribute));
|
||||
foreach (var method in ms)
|
||||
{
|
||||
Log.Error(method.Name);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AutoListenInput(this UIPanel panel)
|
||||
{
|
||||
var ms = Reflection.GetMethodsWithUIInputAttribute(panel.GetType(), typeof(UIInputListenAttribute));
|
||||
foreach (var method in ms)
|
||||
{
|
||||
Log.Error(method.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9bfb9f042814fb787428551c801aada
|
||||
timeCreated: 1748944711
|
||||
@@ -54,7 +54,8 @@ namespace NBF
|
||||
}
|
||||
|
||||
// 获取所有方法,包括公共、非公共、实例和静态方法
|
||||
MethodInfo[] allMethods = classType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic);
|
||||
MethodInfo[] allMethods =
|
||||
classType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
|
||||
foreach (MethodInfo method in allMethods)
|
||||
{
|
||||
@@ -66,5 +67,35 @@ namespace NBF
|
||||
|
||||
return methodsWithAttribute;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有标记指定特性的方法
|
||||
/// </summary>
|
||||
/// <param name="classType">查找类</param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<MethodInfo, T> GetMethodsAttribute<T>(Type classType) where T : Attribute
|
||||
{
|
||||
Dictionary<MethodInfo, T> methodsWithAttribute = new Dictionary<MethodInfo, T>();
|
||||
|
||||
if (classType == null)
|
||||
{
|
||||
return methodsWithAttribute;
|
||||
}
|
||||
|
||||
// 获取所有方法,包括公共、非公共、实例和静态方法
|
||||
MethodInfo[] allMethods =
|
||||
classType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
|
||||
foreach (MethodInfo method in allMethods)
|
||||
{
|
||||
var attribute = method.GetCustomAttribute<T>();
|
||||
if (attribute != null)
|
||||
{
|
||||
methodsWithAttribute[method] = attribute;
|
||||
}
|
||||
}
|
||||
|
||||
return methodsWithAttribute;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user