178 lines
4.3 KiB
C#
178 lines
4.3 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) : 0;
|
|
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;
|
|
|
|
startIndex = Mathf.Clamp(startIndex, -1, m_Buttons.Count - 1);
|
|
|
|
int fallback = -1;
|
|
|
|
for (int i = 0; i < m_Buttons.Count; i++)
|
|
{
|
|
int index = (startIndex + direction + m_Buttons.Count + i * direction) % m_Buttons.Count;
|
|
|
|
index = direction < 0 ? (m_Buttons.Count - index - 1) : index;
|
|
|
|
var button = m_Buttons[index];
|
|
|
|
if (button == null || !button.isActiveAndEnabled || !button.Interactable)
|
|
continue;
|
|
|
|
if (fallback == -1) fallback = index;
|
|
|
|
if (_current == null)
|
|
return index;
|
|
}
|
|
|
|
return fallback;
|
|
}
|
|
}
|