完成按键输入按键显示
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 =
|
||||
|
||||
Reference in New Issue
Block a user