opt
修改命名空间
This commit is contained in:
parent
32c0fc13fd
commit
d17eaaaa8b
@ -2,13 +2,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using AlicizaX.Editor;
|
||||
using AlicizaX.UI.Extension;
|
||||
using AlicizaX.UI.Extension.UXComponent.Hotkey;
|
||||
using AlicizaX.UI.Runtime;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEngine.UI
|
||||
namespace AlicizaX.UI
|
||||
{
|
||||
[CustomEditor(typeof(HotkeyBindComponent))]
|
||||
public class HotkeyBindComponentInspector : GameFrameworkInspector
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
using AlicizaX.Editor;
|
||||
using AlicizaX.UI.Extension;
|
||||
#if INPUTSYSTEM_SUPPORT
|
||||
using AlicizaX.UI;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEngine.UI
|
||||
namespace AlicizaX.UI
|
||||
{
|
||||
[CanEditMultipleObjects]
|
||||
[CustomEditor(typeof(UXHotkeyButton), true)]
|
||||
@ -29,3 +30,5 @@ namespace UnityEngine.UI
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
// Assets/Scripts/AlicizaX/UI/IControllerState.cs
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AlicizaX.UI.Runtime
|
||||
namespace AlicizaX.UI
|
||||
{
|
||||
public abstract class ControllerStateBase
|
||||
{
|
||||
|
||||
@ -3,7 +3,7 @@ using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace AlicizaX.UI.Runtime
|
||||
namespace AlicizaX.UI
|
||||
{
|
||||
[Serializable]
|
||||
[ControlerStateName("GameObject/Visiblity")]
|
||||
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AlicizaX.UI.Runtime
|
||||
namespace AlicizaX.UI
|
||||
{
|
||||
[DisallowMultipleComponent]
|
||||
[AddComponentMenu("Controller/控制器")]
|
||||
|
||||
@ -1,11 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
|
||||
namespace AlicizaX.UI.Runtime
|
||||
namespace AlicizaX.UI
|
||||
{
|
||||
[DisallowMultipleComponent]
|
||||
[AddComponentMenu("Controller/控制器状态")]
|
||||
|
||||
@ -4,136 +4,140 @@ using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
public class UXGroup : UIBehaviour
|
||||
namespace UnityEngine.UI
|
||||
{
|
||||
[SerializeField] private bool m_AllowSwitchOff;
|
||||
[SerializeField] private List<UXButton> m_Buttons = new();
|
||||
|
||||
private UXButton _current;
|
||||
|
||||
public UnityEvent<UXButton> onSelectedChanged = new();
|
||||
|
||||
public bool allowSwitchOff
|
||||
[DisallowMultipleComponent]
|
||||
[ExecuteAlways]
|
||||
public class UXGroup : UIBehaviour
|
||||
{
|
||||
get => m_AllowSwitchOff;
|
||||
set
|
||||
[SerializeField] private bool m_AllowSwitchOff;
|
||||
[SerializeField] private List<UXButton> m_Buttons = new();
|
||||
|
||||
private UXButton _current;
|
||||
|
||||
public UnityEvent<UXButton> onSelectedChanged = new();
|
||||
|
||||
public bool allowSwitchOff
|
||||
{
|
||||
m_AllowSwitchOff = value;
|
||||
get => m_AllowSwitchOff;
|
||||
set
|
||||
{
|
||||
m_AllowSwitchOff = value;
|
||||
ValidateGroup();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Start() => ValidateGroup();
|
||||
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
foreach (var btn in m_Buttons)
|
||||
if (btn)
|
||||
btn.InternalTogSelected = false;
|
||||
m_Buttons.Clear();
|
||||
base.OnDestroy();
|
||||
}
|
||||
|
||||
public void RegisterButton(UXButton button)
|
||||
{
|
||||
if (!button) return;
|
||||
if (m_Buttons.Contains(button)) return;
|
||||
m_Buttons.Add(button);
|
||||
|
||||
if (button.InternalTogSelected)
|
||||
{
|
||||
if (_current && _current != button)
|
||||
_current.InternalTogSelected = false;
|
||||
_current = button;
|
||||
}
|
||||
|
||||
ValidateGroup();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Start() => ValidateGroup();
|
||||
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
foreach (var btn in m_Buttons)
|
||||
if (btn)
|
||||
btn.InternalTogSelected = false;
|
||||
m_Buttons.Clear();
|
||||
base.OnDestroy();
|
||||
}
|
||||
|
||||
public void RegisterButton(UXButton button)
|
||||
{
|
||||
if (!button) return;
|
||||
if (m_Buttons.Contains(button)) return;
|
||||
m_Buttons.Add(button);
|
||||
|
||||
if (button.InternalTogSelected)
|
||||
public void UnregisterButton(UXButton button)
|
||||
{
|
||||
if (_current && _current != button)
|
||||
_current.InternalTogSelected = false;
|
||||
_current = button;
|
||||
if (!button) return;
|
||||
m_Buttons.Remove(button);
|
||||
if (_current == button)
|
||||
_current = null;
|
||||
button.InternalTogSelected = false;
|
||||
}
|
||||
|
||||
ValidateGroup();
|
||||
}
|
||||
internal void NotifyButtonClicked(UXButton button)
|
||||
{
|
||||
if (!button) return;
|
||||
|
||||
public void UnregisterButton(UXButton button)
|
||||
{
|
||||
if (!button) return;
|
||||
m_Buttons.Remove(button);
|
||||
if (_current == button)
|
||||
if (button.InternalTogSelected)
|
||||
{
|
||||
if (m_AllowSwitchOff) SetSelected(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetSelected(button);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetSelected(UXButton target)
|
||||
{
|
||||
if (_current == target) return;
|
||||
var previous = _current;
|
||||
_current = null;
|
||||
button.InternalTogSelected = false;
|
||||
}
|
||||
|
||||
internal void NotifyButtonClicked(UXButton button)
|
||||
{
|
||||
if (!button) return;
|
||||
foreach (var btn in m_Buttons)
|
||||
{
|
||||
bool select = (btn == target);
|
||||
if (btn.InternalTogSelected != select)
|
||||
btn.InternalTogSelected = select;
|
||||
if (select) _current = btn;
|
||||
}
|
||||
|
||||
if (button.InternalTogSelected)
|
||||
{
|
||||
if (m_AllowSwitchOff) SetSelected(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetSelected(button);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetSelected(UXButton target)
|
||||
{
|
||||
if (_current == target) return;
|
||||
var previous = _current;
|
||||
_current = null;
|
||||
|
||||
foreach (var btn in m_Buttons)
|
||||
{
|
||||
bool select = (btn == target);
|
||||
if (btn.InternalTogSelected != select)
|
||||
btn.InternalTogSelected = select;
|
||||
if (select) _current = btn;
|
||||
if (_current) _current.Focus();
|
||||
if (previous != _current)
|
||||
onSelectedChanged?.Invoke(_current);
|
||||
}
|
||||
|
||||
if (_current) _current.Focus();
|
||||
if (previous != _current)
|
||||
onSelectedChanged?.Invoke(_current);
|
||||
}
|
||||
|
||||
private void ValidateGroup()
|
||||
{
|
||||
if (_current != null && _current.InternalTogSelected) return;
|
||||
|
||||
if (!m_AllowSwitchOff && m_Buttons.Count > 0)
|
||||
SetSelected(m_Buttons[0]);
|
||||
}
|
||||
|
||||
public bool AnyOtherSelected(UXButton exclude)
|
||||
{
|
||||
foreach (var btn in m_Buttons)
|
||||
if (btn != exclude && btn.InternalTogSelected)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SelectNext() => SelectRelative(1);
|
||||
public void SelectPrevious() => SelectRelative(-1);
|
||||
|
||||
private void SelectRelative(int dir)
|
||||
{
|
||||
if (m_Buttons.Count == 0) return;
|
||||
int start = _current ? m_Buttons.IndexOf(_current) : -1;
|
||||
int next = FindNextSelectable(start, dir);
|
||||
if (next >= 0) SetSelected(m_Buttons[next]);
|
||||
}
|
||||
|
||||
private int FindNextSelectable(int startIndex, int dir)
|
||||
{
|
||||
if (m_Buttons.Count == 0) return -1;
|
||||
int count = m_Buttons.Count;
|
||||
int index = (startIndex + dir + count) % count;
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
private void ValidateGroup()
|
||||
{
|
||||
var btn = m_Buttons[index];
|
||||
if (btn && btn.isActiveAndEnabled && btn.Interactable)
|
||||
return index;
|
||||
index = (index + dir + count) % count;
|
||||
if (_current != null && _current.InternalTogSelected) return;
|
||||
|
||||
if (!m_AllowSwitchOff && m_Buttons.Count > 0)
|
||||
SetSelected(m_Buttons[0]);
|
||||
}
|
||||
|
||||
return -1;
|
||||
public bool AnyOtherSelected(UXButton exclude)
|
||||
{
|
||||
foreach (var btn in m_Buttons)
|
||||
if (btn != exclude && btn.InternalTogSelected)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SelectNext() => SelectRelative(1);
|
||||
public void SelectPrevious() => SelectRelative(-1);
|
||||
|
||||
private void SelectRelative(int dir)
|
||||
{
|
||||
if (m_Buttons.Count == 0) return;
|
||||
int start = _current ? m_Buttons.IndexOf(_current) : -1;
|
||||
int next = FindNextSelectable(start, dir);
|
||||
if (next >= 0) SetSelected(m_Buttons[next]);
|
||||
}
|
||||
|
||||
private int FindNextSelectable(int startIndex, int dir)
|
||||
{
|
||||
if (m_Buttons.Count == 0) return -1;
|
||||
int count = m_Buttons.Count;
|
||||
int index = (startIndex + dir + count) % count;
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var btn = m_Buttons[index];
|
||||
if (btn && btn.isActiveAndEnabled && btn.Interactable)
|
||||
return index;
|
||||
index = (index + dir + count) % count;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
using AlicizaX.UI.Runtime;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AlicizaX.UI.Extension.UXComponent.Hotkey
|
||||
namespace AlicizaX.UI
|
||||
{
|
||||
[DisallowMultipleComponent]
|
||||
public class HotkeyBindComponent : MonoBehaviour
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
#if INPUTSYSTEM_SUPPORT
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace AlicizaX.UI.Extension
|
||||
namespace AlicizaX.UI
|
||||
{
|
||||
[DisallowMultipleComponent]
|
||||
public class UXHotkeyButton : UXButton
|
||||
{
|
||||
[SerializeField] internal UnityEngine.InputSystem.InputActionReference _hotKeyRefrence;
|
||||
[SerializeField] internal InputActionReference _hotKeyRefrence;
|
||||
[SerializeField] internal EHotkeyPressType _hotkeyPressType;
|
||||
|
||||
internal void HotkeyActionTrigger()
|
||||
@ -17,3 +19,5 @@ namespace AlicizaX.UI.Extension
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -1,150 +1,153 @@
|
||||
#if INPUTSYSTEM_SUPPORT
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections;
|
||||
using AlicizaX.UI.Extension;
|
||||
using AlicizaX.UI;
|
||||
using UnityEditor.Callbacks;
|
||||
|
||||
internal enum EHotkeyPressType
|
||||
namespace AlicizaX.UI
|
||||
{
|
||||
Started,
|
||||
Performed
|
||||
}
|
||||
|
||||
internal static class UXHotkeyRegisterManager
|
||||
{
|
||||
private readonly struct HotkeyRegistration
|
||||
internal enum EHotkeyPressType
|
||||
{
|
||||
public readonly EHotkeyPressType pressType;
|
||||
public readonly UXHotkeyButton button;
|
||||
|
||||
public HotkeyRegistration(UXHotkeyButton btn, EHotkeyPressType pressType)
|
||||
{
|
||||
button = btn;
|
||||
this.pressType = pressType;
|
||||
}
|
||||
Started,
|
||||
Performed
|
||||
}
|
||||
|
||||
private static readonly Dictionary<string, List<HotkeyRegistration>> _hotkeyRegistry =
|
||||
new Dictionary<string, List<HotkeyRegistration>>(32);
|
||||
internal static class UXHotkeyRegisterManager
|
||||
{
|
||||
private readonly struct HotkeyRegistration
|
||||
{
|
||||
public readonly EHotkeyPressType pressType;
|
||||
public readonly UXHotkeyButton button;
|
||||
|
||||
public HotkeyRegistration(UXHotkeyButton btn, EHotkeyPressType pressType)
|
||||
{
|
||||
button = btn;
|
||||
this.pressType = pressType;
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Dictionary<string, List<HotkeyRegistration>> _hotkeyRegistry = new(32);
|
||||
|
||||
|
||||
private static readonly Dictionary<string, (System.Action<InputAction.CallbackContext> handler, InputActionReference action)> _sharedHandlers =
|
||||
new Dictionary<string, (System.Action<InputAction.CallbackContext>, InputActionReference)>(32);
|
||||
private static readonly Dictionary<string, (Action<InputAction.CallbackContext> handler, InputActionReference action)> _sharedHandlers =
|
||||
new(32);
|
||||
|
||||
|
||||
private static readonly Dictionary<UXHotkeyButton, string> _buttonRegistrations =
|
||||
new Dictionary<UXHotkeyButton, string>(64);
|
||||
private static readonly Dictionary<UXHotkeyButton, string> _buttonRegistrations = new(64);
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.Callbacks.DidReloadScripts]
|
||||
internal static void ClearHotkeyRegistry()
|
||||
{
|
||||
foreach (var key in _buttonRegistrations.Keys)
|
||||
[DidReloadScripts]
|
||||
internal static void ClearHotkeyRegistry()
|
||||
{
|
||||
UnregisterHotkey(key);
|
||||
}
|
||||
foreach (var key in _buttonRegistrations.Keys)
|
||||
{
|
||||
UnregisterHotkey(key);
|
||||
}
|
||||
|
||||
_sharedHandlers.Clear();
|
||||
_hotkeyRegistry.Clear();
|
||||
_buttonRegistrations.Clear();
|
||||
}
|
||||
_sharedHandlers.Clear();
|
||||
_hotkeyRegistry.Clear();
|
||||
_buttonRegistrations.Clear();
|
||||
}
|
||||
#endif
|
||||
|
||||
internal static void RegisterHotkey(UXHotkeyButton button, InputActionReference action, EHotkeyPressType pressType)
|
||||
{
|
||||
if (action == null || action.action == null || button == null)
|
||||
return;
|
||||
|
||||
string actionId = action.action.id.ToString();
|
||||
|
||||
|
||||
HotkeyRegistration registration = new HotkeyRegistration(button, pressType);
|
||||
|
||||
|
||||
if (!_hotkeyRegistry.TryGetValue(actionId, out var registrations))
|
||||
internal static void RegisterHotkey(UXHotkeyButton button, InputActionReference action, EHotkeyPressType pressType)
|
||||
{
|
||||
registrations = new List<HotkeyRegistration>(4);
|
||||
_hotkeyRegistry[actionId] = registrations;
|
||||
}
|
||||
if (action == null || action.action == null || button == null)
|
||||
return;
|
||||
|
||||
registrations.Add(registration);
|
||||
string actionId = action.action.id.ToString();
|
||||
|
||||
|
||||
_buttonRegistrations[button] = actionId;
|
||||
HotkeyRegistration registration = new HotkeyRegistration(button, pressType);
|
||||
|
||||
if (!_sharedHandlers.ContainsKey(actionId))
|
||||
{
|
||||
System.Action<InputAction.CallbackContext> handler = ctx => OnHotkeyTriggered(actionId);
|
||||
_sharedHandlers[actionId] = (handler, action);
|
||||
|
||||
switch (pressType)
|
||||
if (!_hotkeyRegistry.TryGetValue(actionId, out var registrations))
|
||||
{
|
||||
case EHotkeyPressType.Started:
|
||||
action.action.started += handler;
|
||||
break;
|
||||
case EHotkeyPressType.Performed:
|
||||
action.action.performed += handler;
|
||||
break;
|
||||
registrations = new List<HotkeyRegistration>(4);
|
||||
_hotkeyRegistry[actionId] = registrations;
|
||||
}
|
||||
|
||||
action.action.Enable();
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnregisterHotkey(UXHotkeyButton button)
|
||||
{
|
||||
if (button == null || !_buttonRegistrations.TryGetValue(button, out var actionId))
|
||||
return;
|
||||
registrations.Add(registration);
|
||||
|
||||
|
||||
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations))
|
||||
{
|
||||
HotkeyRegistration hotkeyInfo;
|
||||
for (int i = registrations.Count - 1; i >= 0; i--)
|
||||
_buttonRegistrations[button] = actionId;
|
||||
|
||||
if (!_sharedHandlers.ContainsKey(actionId))
|
||||
{
|
||||
if (registrations[i].button == button)
|
||||
Action<InputAction.CallbackContext> handler = ctx => OnHotkeyTriggered(actionId);
|
||||
_sharedHandlers[actionId] = (handler, action);
|
||||
|
||||
switch (pressType)
|
||||
{
|
||||
hotkeyInfo = registrations[i];
|
||||
registrations.RemoveAt(i);
|
||||
case EHotkeyPressType.Started:
|
||||
action.action.started += handler;
|
||||
break;
|
||||
case EHotkeyPressType.Performed:
|
||||
action.action.performed += handler;
|
||||
break;
|
||||
}
|
||||
|
||||
if (registrations.Count == 0 && _sharedHandlers.TryGetValue(actionId, out var handlerInfo))
|
||||
action.action.Enable();
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnregisterHotkey(UXHotkeyButton button)
|
||||
{
|
||||
if (button == null || !_buttonRegistrations.TryGetValue(button, out var actionId))
|
||||
return;
|
||||
|
||||
|
||||
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations))
|
||||
{
|
||||
HotkeyRegistration hotkeyInfo;
|
||||
for (int i = registrations.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (registrations[i].button == button)
|
||||
{
|
||||
var (handler, actionRef) = handlerInfo;
|
||||
if (actionRef != null && actionRef.action != null)
|
||||
{
|
||||
actionRef.action.Disable();
|
||||
_sharedHandlers.Remove(actionId);
|
||||
_hotkeyRegistry.Remove(actionId);
|
||||
hotkeyInfo = registrations[i];
|
||||
registrations.RemoveAt(i);
|
||||
|
||||
switch (hotkeyInfo.pressType)
|
||||
if (registrations.Count == 0 && _sharedHandlers.TryGetValue(actionId, out var handlerInfo))
|
||||
{
|
||||
var (handler, actionRef) = handlerInfo;
|
||||
if (actionRef != null && actionRef.action != null)
|
||||
{
|
||||
case EHotkeyPressType.Started:
|
||||
actionRef.action.started -= handler;
|
||||
break;
|
||||
case EHotkeyPressType.Performed:
|
||||
actionRef.action.performed -= handler;
|
||||
break;
|
||||
actionRef.action.Disable();
|
||||
_sharedHandlers.Remove(actionId);
|
||||
_hotkeyRegistry.Remove(actionId);
|
||||
|
||||
switch (hotkeyInfo.pressType)
|
||||
{
|
||||
case EHotkeyPressType.Started:
|
||||
actionRef.action.started -= handler;
|
||||
break;
|
||||
case EHotkeyPressType.Performed:
|
||||
actionRef.action.performed -= handler;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_buttonRegistrations.Remove(button);
|
||||
}
|
||||
|
||||
|
||||
_buttonRegistrations.Remove(button);
|
||||
}
|
||||
|
||||
private static void OnHotkeyTriggered(string actionId)
|
||||
{
|
||||
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations) && registrations.Count > 0)
|
||||
private static void OnHotkeyTriggered(string actionId)
|
||||
{
|
||||
var registration = registrations[^1];
|
||||
registration.button.HotkeyActionTrigger();
|
||||
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations) && registrations.Count > 0)
|
||||
{
|
||||
var registration = registrations[^1];
|
||||
registration.button.HotkeyActionTrigger();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace AlicizaX.UI.Extension
|
||||
namespace AlicizaX.UI
|
||||
{
|
||||
public class UXDraggable:MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user