123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- namespace SC.XR.Unity
- {
- public class SCToggleGroup3D : MonoBehaviour
- {
- [SerializeField] private bool m_AllowSwitchOff = false;
- public bool allowSwitchOff { get { return m_AllowSwitchOff; } set { m_AllowSwitchOff = value; } }
- private List<SCToggleBase> m_Toggles = new List<SCToggleBase>();
- protected SCToggleGroup3D()
- { }
- private void ValidateToggleIsInGroup(SCToggleBase toggle)
- {
- if (toggle == null || !m_Toggles.Contains(toggle))
- throw new ArgumentException(string.Format("Toggle {0} is not part of ToggleGroup {1}", new object[] { toggle, this }));
- }
- public void NotifyToggleOn(SCToggleBase toggle)
- {
- ValidateToggleIsInGroup(toggle);
- // disable all toggles in the group
- for (var i = 0; i < m_Toggles.Count; i++)
- {
- if (m_Toggles[i] == toggle)
- continue;
- m_Toggles[i].isOn = false;
- }
- }
- public void UnregisterToggle(SCToggleBase toggle)
- {
- if (m_Toggles.Contains(toggle))
- m_Toggles.Remove(toggle);
- }
- public void RegisterToggle(SCToggleBase toggle)
- {
- if (!m_Toggles.Contains(toggle))
- m_Toggles.Add(toggle);
- }
- public bool AnyTogglesOn()
- {
- return m_Toggles.Find(x => x.isOn) != null;
- }
- public IEnumerable<SCToggleBase> ActiveToggles()
- {
- return m_Toggles.Where(x => x.isOn);
- }
- public void SetAllTogglesOff()
- {
- bool oldAllowSwitchOff = m_AllowSwitchOff;
- m_AllowSwitchOff = true;
- for (var i = 0; i < m_Toggles.Count; i++)
- m_Toggles[i].isOn = false;
- m_AllowSwitchOff = oldAllowSwitchOff;
- }
- }
- }
|