171 lines
4.2 KiB
C#
171 lines
4.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using System.Collections.Generic;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine.EventSystems;
|
|
|
|
[DisallowMultipleComponent]
|
|
public class UXGroup : UIBehaviour
|
|
{
|
|
[SerializeField] private bool m_AllowSwitchOff;
|
|
[ReadOnly, SerializeField] private List<UXButton> m_Buttons = new();
|
|
|
|
private UXButton _current;
|
|
private readonly HashSet<UXButton> _registeredButtons = new();
|
|
|
|
public UnityEvent<UXButton> onSelectedChanged = new();
|
|
|
|
public bool allowSwitchOff
|
|
{
|
|
get => m_AllowSwitchOff;
|
|
set
|
|
{
|
|
m_AllowSwitchOff = value;
|
|
ValidateGroupState();
|
|
}
|
|
}
|
|
|
|
protected override void OnDestroy()
|
|
{
|
|
foreach (var button in _registeredButtons)
|
|
{
|
|
if (button) button.IsSelected = false;
|
|
}
|
|
|
|
m_Buttons.Clear();
|
|
_registeredButtons.Clear();
|
|
base.OnDestroy();
|
|
}
|
|
|
|
protected override void Awake() => ValidateGroupState();
|
|
|
|
public void RegisterButton(UXButton button)
|
|
{
|
|
if (!button || _registeredButtons.Contains(button)) return;
|
|
|
|
m_Buttons.Add(button);
|
|
_registeredButtons.Add(button);
|
|
|
|
if (button.IsSelected)
|
|
{
|
|
if (_current && _current != button)
|
|
_current.IsSelected = false;
|
|
_current = button;
|
|
}
|
|
|
|
ValidateGroupState();
|
|
}
|
|
|
|
public void UnregisterButton(UXButton button)
|
|
{
|
|
if (!button || !_registeredButtons.Contains(button)) return;
|
|
|
|
m_Buttons.Remove(button);
|
|
_registeredButtons.Remove(button);
|
|
button.IsSelected = false;
|
|
|
|
if (_current == button)
|
|
_current = null;
|
|
}
|
|
|
|
internal void NotifyButtonClicked(UXButton clickedButton)
|
|
{
|
|
if (clickedButton.IsSelected)
|
|
{
|
|
if (m_AllowSwitchOff)
|
|
SetSelectedButton(null);
|
|
}
|
|
else
|
|
{
|
|
SetSelectedButton(clickedButton);
|
|
}
|
|
}
|
|
|
|
private void SetSelectedButton(UXButton target)
|
|
{
|
|
var previous = _current;
|
|
_current = null;
|
|
|
|
foreach (var button in m_Buttons)
|
|
{
|
|
bool shouldSelect = (button == target);
|
|
if (button.IsSelected != shouldSelect)
|
|
button.IsSelected = shouldSelect;
|
|
|
|
if (shouldSelect)
|
|
_current = button;
|
|
}
|
|
|
|
if (previous != _current)
|
|
onSelectedChanged?.Invoke(_current);
|
|
}
|
|
|
|
private void ValidateGroupState()
|
|
{
|
|
bool hasSelected = _current != null && _current.IsSelected;
|
|
if (!hasSelected && m_Buttons.Count > 0 && !m_AllowSwitchOff)
|
|
SetSelectedButton(m_Buttons[0]);
|
|
}
|
|
|
|
public bool AnyOtherSelected(UXButton exclusion)
|
|
{
|
|
foreach (var button in m_Buttons)
|
|
{
|
|
if (button != exclusion && button.IsSelected)
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
public void SelectPrevious()
|
|
{
|
|
if (m_Buttons.Count == 0) return;
|
|
|
|
int startIndex = _current ? m_Buttons.IndexOf(_current) : -1;
|
|
int newIndex = FindSelectableIndex(startIndex, -1);
|
|
|
|
if (newIndex != -1)
|
|
{
|
|
SetSelectedButton(m_Buttons[newIndex]);
|
|
}
|
|
}
|
|
|
|
|
|
public void SelectNext()
|
|
{
|
|
if (m_Buttons.Count == 0) return;
|
|
|
|
int startIndex = _current ? m_Buttons.IndexOf(_current) : -1;
|
|
int newIndex = FindSelectableIndex(startIndex, 1);
|
|
|
|
if (newIndex != -1)
|
|
{
|
|
SetSelectedButton(m_Buttons[newIndex]);
|
|
}
|
|
}
|
|
|
|
|
|
private int FindSelectableIndex(int startIndex, int direction) {
|
|
if (m_Buttons.Count == 0) return -1;
|
|
|
|
// 计算当前有效起点
|
|
int current = (startIndex == -1)
|
|
? (direction > 0 ? 0 : m_Buttons.Count - 1)
|
|
: startIndex;
|
|
|
|
// 单次简单循环查找
|
|
for (int i = 0; i < m_Buttons.Count; i++) {
|
|
int index = (current + direction * i + m_Buttons.Count) % m_Buttons.Count;
|
|
|
|
if (m_Buttons[index] != null &&
|
|
m_Buttons[index].isActiveAndEnabled &&
|
|
m_Buttons[index].Interactable) {
|
|
return index;
|
|
}
|
|
}
|
|
return -1; // 没有可用按钮
|
|
}
|
|
}
|