A game about forced loneliness, made by TACStudios
1using System.Collections.Generic; 2using UnityEditor.UIElements; 3using UnityEngine; 4using UnityEngine.UIElements; 5 6namespace UnityEditor.Rendering.LookDev 7{ 8 class ToolbarRadio : UIElements.Toolbar, INotifyValueChanged<int> 9 { 10 List<ToolbarToggle> radios = new List<ToolbarToggle>(); 11 12 public new static readonly string ussClassName = "unity-toolbar-radio"; 13 14 bool m_CanDeselectAll = false; 15 16 public int radioLength { get; private set; } = 0; 17 18 int m_Value; 19 public int value 20 { 21 get => m_Value; 22 set 23 { 24 if (value == m_Value) 25 return; 26 27 if (panel != null) 28 { 29 using (ChangeEvent<int> evt = ChangeEvent<int>.GetPooled(m_Value, value)) 30 { 31 evt.target = this; 32 SetValueWithoutNotify(value); 33 SendEvent(evt); 34 } 35 } 36 else 37 { 38 SetValueWithoutNotify(value); 39 } 40 } 41 } 42 43 public ToolbarRadio() : this(null, false) { } 44 45 public ToolbarRadio(string label = null, bool canDeselectAll = false) 46 { 47 RemoveFromClassList(UIElements.Toolbar.ussClassName); 48 AddToClassList(ussClassName); 49 50 m_CanDeselectAll = canDeselectAll; 51 if (m_CanDeselectAll) 52 m_Value = -1; 53 if (label != null) 54 Add(new Label() { text = label }); 55 } 56 57 public void AddRadio(string text = null, Texture2D icon = null, string tooltip = null) 58 { 59 var toggle = new ToolbarToggle(); 60 toggle.RegisterValueChangedCallback(InnerValueChanged(radioLength)); 61 toggle.SetValueWithoutNotify(radioLength == (m_CanDeselectAll ? -1 : 0)); 62 toggle.tooltip = tooltip; 63 radios.Add(toggle); 64 if (icon != null) 65 { 66 var childsContainer = toggle.Q(null, ToolbarToggle.inputUssClassName); 67 childsContainer.Add(new Image() { image = icon }); 68 if (text != null) 69 childsContainer.Add(new Label() { text = text }); 70 } 71 else 72 toggle.text = text; 73 Add(toggle); 74 if (radioLength == 0) 75 toggle.style.borderLeftWidth = 1; 76 radioLength++; 77 } 78 79 public void AddRadios(string[] labels) 80 { 81 foreach (var label in labels) 82 AddRadio(label); 83 } 84 85 public void AddRadios((string text, string tooltip)[] labels) 86 { 87 foreach (var label in labels) 88 AddRadio(label.text, null, label.tooltip); 89 } 90 91 public void AddRadios(Texture2D[] icons) 92 { 93 foreach (var icon in icons) 94 AddRadio(null, icon); 95 } 96 97 public void AddRadios((string text, Texture2D icon)[] labels) 98 { 99 foreach (var label in labels) 100 AddRadio(label.text, label.icon); 101 } 102 103 public void AddRadios((Texture2D icon, string tooltip)[] labels) 104 { 105 foreach (var label in labels) 106 AddRadio(null, label.icon, label.tooltip); 107 } 108 109 public void AddRadios((string text, Texture2D icon, string tooltip)[] labels) 110 { 111 foreach (var label in labels) 112 AddRadio(label.text, label.icon, label.tooltip); 113 } 114 115 EventCallback<ChangeEvent<bool>> InnerValueChanged(int radioIndex) 116 { 117 return (ChangeEvent<bool> evt) => 118 { 119 if (radioIndex == m_Value) 120 { 121 if (!evt.newValue && !m_CanDeselectAll) 122 radios[radioIndex].SetValueWithoutNotify(true); 123 else 124 value = -1; 125 } 126 else 127 value = radioIndex; 128 }; 129 } 130 131 public void SetValueWithoutNotify(int newValue) 132 { 133 if (m_Value != newValue) 134 { 135 if (newValue < (m_CanDeselectAll ? -1 : 0) || newValue >= radioLength) 136 throw new System.IndexOutOfRangeException(); 137 138 if (m_Value == newValue && m_CanDeselectAll) 139 { 140 if (m_Value > -1) 141 radios[m_Value].SetValueWithoutNotify(false); 142 m_Value = -1; 143 } 144 else 145 { 146 if (m_Value > -1) 147 radios[m_Value].SetValueWithoutNotify(false); 148 if (newValue > -1) 149 radios[newValue].SetValueWithoutNotify(true); 150 m_Value = newValue; 151 } 152 } 153 } 154 } 155}