修改命名空间
This commit is contained in:
陈思海 2025-12-09 20:30:11 +08:00
parent 32c0fc13fd
commit d17eaaaa8b
11 changed files with 240 additions and 233 deletions

View File

@ -2,13 +2,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using AlicizaX.Editor; using AlicizaX.Editor;
using AlicizaX.UI.Extension;
using AlicizaX.UI.Extension.UXComponent.Hotkey;
using AlicizaX.UI.Runtime; using AlicizaX.UI.Runtime;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
namespace UnityEngine.UI namespace AlicizaX.UI
{ {
[CustomEditor(typeof(HotkeyBindComponent))] [CustomEditor(typeof(HotkeyBindComponent))]
public class HotkeyBindComponentInspector : GameFrameworkInspector public class HotkeyBindComponentInspector : GameFrameworkInspector

View File

@ -1,8 +1,9 @@
using AlicizaX.Editor; #if INPUTSYSTEM_SUPPORT
using AlicizaX.UI.Extension; using AlicizaX.UI;
using UnityEditor; using UnityEditor;
using UnityEngine;
namespace UnityEngine.UI namespace AlicizaX.UI
{ {
[CanEditMultipleObjects] [CanEditMultipleObjects]
[CustomEditor(typeof(UXHotkeyButton), true)] [CustomEditor(typeof(UXHotkeyButton), true)]
@ -29,3 +30,5 @@ namespace UnityEngine.UI
} }
} }
} }
#endif

View File

@ -1,9 +1,7 @@
// Assets/Scripts/AlicizaX/UI/IControllerState.cs
using System; using System;
using UnityEngine; using UnityEngine;
namespace AlicizaX.UI.Runtime namespace AlicizaX.UI
{ {
public abstract class ControllerStateBase public abstract class ControllerStateBase
{ {

View File

@ -3,7 +3,7 @@ using UnityEngine;
using UnityEngine.Serialization; using UnityEngine.Serialization;
using UnityEngine.UI; using UnityEngine.UI;
namespace AlicizaX.UI.Runtime namespace AlicizaX.UI
{ {
[Serializable] [Serializable]
[ControlerStateName("GameObject/Visiblity")] [ControlerStateName("GameObject/Visiblity")]

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
namespace AlicizaX.UI.Runtime namespace AlicizaX.UI
{ {
[DisallowMultipleComponent] [DisallowMultipleComponent]
[AddComponentMenu("Controller/控制器")] [AddComponentMenu("Controller/控制器")]

View File

@ -1,11 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Serialization;
using Random = UnityEngine.Random;
namespace AlicizaX.UI
namespace AlicizaX.UI.Runtime
{ {
[DisallowMultipleComponent] [DisallowMultipleComponent]
[AddComponentMenu("Controller/控制器状态")] [AddComponentMenu("Controller/控制器状态")]

View File

@ -4,136 +4,140 @@ using UnityEngine.Events;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
[DisallowMultipleComponent] namespace UnityEngine.UI
public class UXGroup : UIBehaviour
{ {
[SerializeField] private bool m_AllowSwitchOff; [DisallowMultipleComponent]
[SerializeField] private List<UXButton> m_Buttons = new(); [ExecuteAlways]
public class UXGroup : UIBehaviour
private UXButton _current;
public UnityEvent<UXButton> onSelectedChanged = new();
public bool allowSwitchOff
{ {
get => m_AllowSwitchOff; [SerializeField] private bool m_AllowSwitchOff;
set [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(); ValidateGroup();
} }
}
protected override void Start() => ValidateGroup(); public void UnregisterButton(UXButton button)
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) if (!button) return;
_current.InternalTogSelected = false; m_Buttons.Remove(button);
_current = 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.InternalTogSelected)
{ {
if (!button) return; if (m_AllowSwitchOff) SetSelected(null);
m_Buttons.Remove(button); }
if (_current == button) else
{
SetSelected(button);
}
}
private void SetSelected(UXButton target)
{
if (_current == target) return;
var previous = _current;
_current = null; _current = null;
button.InternalTogSelected = false;
}
internal void NotifyButtonClicked(UXButton button) foreach (var btn in m_Buttons)
{ {
if (!button) return; bool select = (btn == target);
if (btn.InternalTogSelected != select)
btn.InternalTogSelected = select;
if (select) _current = btn;
}
if (button.InternalTogSelected) if (_current) _current.Focus();
{ if (previous != _current)
if (m_AllowSwitchOff) SetSelected(null); onSelectedChanged?.Invoke(_current);
}
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(); private void ValidateGroup()
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++)
{ {
var btn = m_Buttons[index]; if (_current != null && _current.InternalTogSelected) return;
if (btn && btn.isActiveAndEnabled && btn.Interactable)
return index; if (!m_AllowSwitchOff && m_Buttons.Count > 0)
index = (index + dir + count) % count; 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;
}
} }
} }

View File

@ -2,7 +2,7 @@
using AlicizaX.UI.Runtime; using AlicizaX.UI.Runtime;
using UnityEngine; using UnityEngine;
namespace AlicizaX.UI.Extension.UXComponent.Hotkey namespace AlicizaX.UI
{ {
[DisallowMultipleComponent] [DisallowMultipleComponent]
public class HotkeyBindComponent : MonoBehaviour public class HotkeyBindComponent : MonoBehaviour

View File

@ -1,11 +1,13 @@
#if INPUTSYSTEM_SUPPORT
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem;
namespace AlicizaX.UI.Extension namespace AlicizaX.UI
{ {
[DisallowMultipleComponent] [DisallowMultipleComponent]
public class UXHotkeyButton : UXButton public class UXHotkeyButton : UXButton
{ {
[SerializeField] internal UnityEngine.InputSystem.InputActionReference _hotKeyRefrence; [SerializeField] internal InputActionReference _hotKeyRefrence;
[SerializeField] internal EHotkeyPressType _hotkeyPressType; [SerializeField] internal EHotkeyPressType _hotkeyPressType;
internal void HotkeyActionTrigger() internal void HotkeyActionTrigger()
@ -17,3 +19,5 @@ namespace AlicizaX.UI.Extension
} }
} }
} }
#endif

View File

@ -1,150 +1,153 @@
#if INPUTSYSTEM_SUPPORT #if INPUTSYSTEM_SUPPORT
using System;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections; using System.Collections;
using AlicizaX.UI.Extension; using AlicizaX.UI;
using UnityEditor.Callbacks;
internal enum EHotkeyPressType namespace AlicizaX.UI
{ {
Started, internal enum EHotkeyPressType
Performed
}
internal static class UXHotkeyRegisterManager
{
private readonly struct HotkeyRegistration
{ {
public readonly EHotkeyPressType pressType; Started,
public readonly UXHotkeyButton button; Performed
public HotkeyRegistration(UXHotkeyButton btn, EHotkeyPressType pressType)
{
button = btn;
this.pressType = pressType;
}
} }
private static readonly Dictionary<string, List<HotkeyRegistration>> _hotkeyRegistry = internal static class UXHotkeyRegisterManager
new Dictionary<string, List<HotkeyRegistration>>(32); {
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 = private static readonly Dictionary<string, (Action<InputAction.CallbackContext> handler, InputActionReference action)> _sharedHandlers =
new Dictionary<string, (System.Action<InputAction.CallbackContext>, InputActionReference)>(32); new(32);
private static readonly Dictionary<UXHotkeyButton, string> _buttonRegistrations = private static readonly Dictionary<UXHotkeyButton, string> _buttonRegistrations = new(64);
new Dictionary<UXHotkeyButton, string>(64);
#if UNITY_EDITOR #if UNITY_EDITOR
[UnityEditor.Callbacks.DidReloadScripts] [DidReloadScripts]
internal static void ClearHotkeyRegistry() internal static void ClearHotkeyRegistry()
{
foreach (var key in _buttonRegistrations.Keys)
{ {
UnregisterHotkey(key); foreach (var key in _buttonRegistrations.Keys)
} {
UnregisterHotkey(key);
}
_sharedHandlers.Clear(); _sharedHandlers.Clear();
_hotkeyRegistry.Clear(); _hotkeyRegistry.Clear();
_buttonRegistrations.Clear(); _buttonRegistrations.Clear();
} }
#endif #endif
internal static void RegisterHotkey(UXHotkeyButton button, InputActionReference action, EHotkeyPressType pressType) 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))
{ {
registrations = new List<HotkeyRegistration>(4); if (action == null || action.action == null || button == null)
_hotkeyRegistry[actionId] = registrations; 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: registrations = new List<HotkeyRegistration>(4);
action.action.started += handler; _hotkeyRegistry[actionId] = registrations;
break;
case EHotkeyPressType.Performed:
action.action.performed += handler;
break;
} }
action.action.Enable(); registrations.Add(registration);
}
}
public static void UnregisterHotkey(UXHotkeyButton button)
{
if (button == null || !_buttonRegistrations.TryGetValue(button, out var actionId))
return;
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations)) _buttonRegistrations[button] = actionId;
{
HotkeyRegistration hotkeyInfo; if (!_sharedHandlers.ContainsKey(actionId))
for (int i = registrations.Count - 1; i >= 0; i--)
{ {
if (registrations[i].button == button) Action<InputAction.CallbackContext> handler = ctx => OnHotkeyTriggered(actionId);
_sharedHandlers[actionId] = (handler, action);
switch (pressType)
{ {
hotkeyInfo = registrations[i]; case EHotkeyPressType.Started:
registrations.RemoveAt(i); 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; hotkeyInfo = registrations[i];
if (actionRef != null && actionRef.action != null) registrations.RemoveAt(i);
{
actionRef.action.Disable();
_sharedHandlers.Remove(actionId);
_hotkeyRegistry.Remove(actionId);
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.Disable();
actionRef.action.started -= handler; _sharedHandlers.Remove(actionId);
break; _hotkeyRegistry.Remove(actionId);
case EHotkeyPressType.Performed:
actionRef.action.performed -= handler; switch (hotkeyInfo.pressType)
break; {
case EHotkeyPressType.Started:
actionRef.action.started -= handler;
break;
case EHotkeyPressType.Performed:
actionRef.action.performed -= handler;
break;
}
} }
} }
}
break; break;
}
} }
} }
_buttonRegistrations.Remove(button);
} }
private static void OnHotkeyTriggered(string actionId)
_buttonRegistrations.Remove(button);
}
private static void OnHotkeyTriggered(string actionId)
{
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations) && registrations.Count > 0)
{ {
var registration = registrations[^1]; if (_hotkeyRegistry.TryGetValue(actionId, out var registrations) && registrations.Count > 0)
registration.button.HotkeyActionTrigger(); {
var registration = registrations[^1];
registration.button.HotkeyActionTrigger();
}
} }
} }
} }

View File

@ -2,7 +2,7 @@ using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
namespace AlicizaX.UI.Extension namespace AlicizaX.UI
{ {
public class UXDraggable:MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler public class UXDraggable:MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{ {