ToolbarRadio.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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)
  50. {
  51. var toggle = new ToolbarToggle();
  52. toggle.RegisterValueChangedCallback(InnerValueChanged(radioLength));
  53. toggle.SetValueWithoutNotify(radioLength == (m_CanDeselectAll ? -1 : 0));
  54. radios.Add(toggle);
  55. if (icon != null)
  56. {
  57. var childsContainer = toggle.Q(null, ToolbarToggle.inputUssClassName);
  58. childsContainer.Add(new Image() { image = icon });
  59. if (text != null)
  60. childsContainer.Add(new Label() { text = text });
  61. }
  62. else
  63. toggle.text = text;
  64. Add(toggle);
  65. if (radioLength == 0)
  66. toggle.style.borderLeftWidth = 1;
  67. radioLength++;
  68. }
  69. public void AddRadios(string[] labels)
  70. {
  71. foreach (var label in labels)
  72. AddRadio(label);
  73. }
  74. public void AddRadios(Texture2D[] icons)
  75. {
  76. foreach (var icon in icons)
  77. AddRadio(null, icon);
  78. }
  79. public void AddRadios((string text, Texture2D icon)[] labels)
  80. {
  81. foreach (var label in labels)
  82. AddRadio(label.text, label.icon);
  83. }
  84. EventCallback<ChangeEvent<bool>> InnerValueChanged(int radioIndex)
  85. {
  86. return (ChangeEvent<bool> evt) =>
  87. {
  88. if (radioIndex == m_Value)
  89. {
  90. if (!evt.newValue && !m_CanDeselectAll)
  91. radios[radioIndex].SetValueWithoutNotify(true);
  92. else
  93. value = -1;
  94. }
  95. else
  96. value = radioIndex;
  97. };
  98. }
  99. public void SetValueWithoutNotify(int newValue)
  100. {
  101. if (m_Value != newValue)
  102. {
  103. if (newValue < (m_CanDeselectAll ? -1 : 0) || newValue >= radioLength)
  104. throw new System.IndexOutOfRangeException();
  105. if (m_Value == newValue && m_CanDeselectAll)
  106. {
  107. if (m_Value > -1)
  108. radios[m_Value].SetValueWithoutNotify(false);
  109. m_Value = -1;
  110. }
  111. else
  112. {
  113. if (m_Value > -1)
  114. radios[m_Value].SetValueWithoutNotify(false);
  115. if (newValue > -1)
  116. radios[newValue].SetValueWithoutNotify(true);
  117. m_Value = newValue;
  118. }
  119. }
  120. }
  121. }
  122. }