com.alicizax.unity.ui.exten.../Runtime/UXComponent/Group/UXGroup.cs

144 lines
3.9 KiB
C#
Raw Normal View History

2025-10-13 20:20:01 +08:00
using System.Collections.Generic;
2025-04-11 17:26:28 +08:00
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
2025-10-13 20:20:01 +08:00
2025-12-09 20:30:11 +08:00
namespace UnityEngine.UI
2025-04-11 17:26:28 +08:00
{
2025-12-09 20:30:11 +08:00
[DisallowMultipleComponent]
[ExecuteAlways]
public class UXGroup : UIBehaviour
{
[SerializeField] private bool m_AllowSwitchOff;
[SerializeField] private List<UXButton> m_Buttons = new();
2025-04-11 17:26:28 +08:00
2025-12-09 20:30:11 +08:00
private UXButton _current;
2025-04-11 17:26:28 +08:00
2025-12-09 20:30:11 +08:00
public UnityEvent<UXButton> onSelectedChanged = new();
2025-04-11 17:26:28 +08:00
2025-12-09 20:30:11 +08:00
public bool allowSwitchOff
2025-04-11 17:26:28 +08:00
{
2025-12-09 20:30:11 +08:00
get => m_AllowSwitchOff;
set
{
m_AllowSwitchOff = value;
ValidateGroup();
}
2025-04-11 17:26:28 +08:00
}
2025-10-13 20:20:01 +08:00
2025-12-09 20:30:11 +08:00
protected override void Start() => ValidateGroup();
2025-04-11 17:26:28 +08:00
2025-12-09 20:30:11 +08:00
protected override void OnDestroy()
2025-04-11 17:26:28 +08:00
{
2025-12-09 20:30:11 +08:00
foreach (var btn in m_Buttons)
if (btn)
btn.InternalTogSelected = false;
m_Buttons.Clear();
base.OnDestroy();
2025-04-11 17:26:28 +08:00
}
2025-07-28 13:04:49 +08:00
2025-12-09 20:30:11 +08:00
public void RegisterButton(UXButton button)
{
if (!button) return;
if (m_Buttons.Contains(button)) return;
m_Buttons.Add(button);
2025-04-11 17:26:28 +08:00
2025-12-09 20:30:11 +08:00
if (button.InternalTogSelected)
{
if (_current && _current != button)
_current.InternalTogSelected = false;
_current = button;
}
2025-10-13 20:20:01 +08:00
2025-12-09 20:30:11 +08:00
ValidateGroup();
2025-04-11 17:26:28 +08:00
}
2025-12-09 20:30:11 +08:00
public void UnregisterButton(UXButton button)
2025-04-11 17:26:28 +08:00
{
2025-12-09 20:30:11 +08:00
if (!button) return;
m_Buttons.Remove(button);
if (_current == button)
_current = null;
button.InternalTogSelected = false;
2025-04-11 17:26:28 +08:00
}
2025-12-09 20:30:11 +08:00
internal void NotifyButtonClicked(UXButton button)
2025-04-11 17:26:28 +08:00
{
2025-12-09 20:30:11 +08:00
if (!button) return;
if (button.InternalTogSelected)
{
if (m_AllowSwitchOff) SetSelected(null);
}
else
{
SetSelected(button);
}
2025-04-11 17:26:28 +08:00
}
2025-07-28 13:04:49 +08:00
2025-12-09 20:30:11 +08:00
private void SetSelected(UXButton target)
{
if (_current == target) return;
var previous = _current;
_current = null;
2025-10-13 20:20:01 +08:00
2025-12-09 20:30:11 +08:00
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);
}
2025-04-11 17:26:28 +08:00
2025-12-09 20:30:11 +08:00
private void ValidateGroup()
{
if (_current != null && _current.InternalTogSelected) return;
2025-08-08 20:56:31 +08:00
2025-12-09 20:30:11 +08:00
if (!m_AllowSwitchOff && m_Buttons.Count > 0)
SetSelected(m_Buttons[0]);
}
2025-08-08 20:56:31 +08:00
2025-12-09 20:30:11 +08:00
public bool AnyOtherSelected(UXButton exclude)
{
foreach (var btn in m_Buttons)
if (btn != exclude && btn.InternalTogSelected)
return true;
return false;
}
2025-08-08 20:56:31 +08:00
2025-12-09 20:30:11 +08:00
public void SelectNext() => SelectRelative(1);
public void SelectPrevious() => SelectRelative(-1);
2025-08-08 20:56:31 +08:00
2025-12-09 20:30:11 +08:00
private void SelectRelative(int dir)
2025-08-11 14:45:27 +08:00
{
2025-12-09 20:30:11 +08:00
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]);
2025-08-08 20:56:31 +08:00
}
2025-08-11 14:45:27 +08:00
2025-12-09 20:30:11 +08:00
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;
}
2025-08-08 20:56:31 +08:00
}
2025-04-11 17:26:28 +08:00
}