// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created :
//
// Last Modified By : Kimch
// Last Modified On :
// ***********************************************************************
//
//
// ***********************************************************************
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
[AddComponentMenu("UI/Custom/Switch Group", 32)]
[DisallowMultipleComponent]
public class KUISwitchGroup : UIBehaviour
{
[SerializeField] private bool m_AllowSwitchOff = false;
public bool allowSwitchOff { get { return m_AllowSwitchOff; } set { m_AllowSwitchOff = value; } }
private List m_Switchs = new List();
protected KUISwitchGroup()
{ }
private void ValidateSwitchIsInGroup(KUISwitch uiSwitch)
{
if (uiSwitch == null || !m_Switchs.Contains(uiSwitch))
throw new ArgumentException(string.Format("Switch {0} is not part of SwitchGroup {1}", new object[] { uiSwitch, this }));
}
public void NotifySwitchOn(KUISwitch Switch)
{
ValidateSwitchIsInGroup(Switch);
// disable all Switchs in the group
for (var i = 0; i < m_Switchs.Count; i++)
{
if (m_Switchs[i] == Switch)
continue;
m_Switchs[i].isOn = false;
}
}
public void UnregisterSwitch(KUISwitch Switch)
{
if (m_Switchs.Contains(Switch))
{
m_Switchs.Remove(Switch);
Switch.onValueChanged.RemoveAllListeners();
}
}
public void RegisterSwitch(KUISwitch Switch)
{
if (!m_Switchs.Contains(Switch))
{
m_Switchs.Add(Switch);
Switch.onValueChanged.AddListener((value) =>
{
OnToggleValueChanged(Switch, value);
});
}
}
public bool AnySwitchsOn()
{
return m_Switchs.Find(x => x.isOn) != null;
}
public IEnumerable ActiveSwitchs()
{
return m_Switchs.FindAll(x => x.isOn);
}
public void SetAllSwitchsOff()
{
bool oldAllowSwitchOff = m_AllowSwitchOff;
m_AllowSwitchOff = true;
for (var i = 0; i < m_Switchs.Count; i++)
m_Switchs[i].isOn = false;
m_AllowSwitchOff = oldAllowSwitchOff;
}
public event System.Action onSwitchSelect;
private void OnToggleValueChanged(KUISwitch uiSwitch, bool value)
{
if (value && onSwitchSelect != null)
{
onSwitchSelect(uiSwitch.name);
}
}
}