ToolbarRadio.cs 5.0 KB

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