No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ButtonStripField.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // This class is taken from UnityEngine.UIElements.ButtonStripField
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. using UnityEngine.U2D.Common;
  6. namespace UnityEditor.U2D.Animation.Upgrading
  7. {
  8. #if ENABLE_UXML_SERIALIZED_DATA
  9. [UxmlElement]
  10. #endif
  11. internal partial class ButtonStripField : BaseField<int>
  12. {
  13. #if ENABLE_UXML_TRAITS
  14. public new class UxmlFactory : UxmlFactory<ButtonStripField, UxmlTraits> {}
  15. public new class UxmlTraits : BaseField<int>.UxmlTraits {}
  16. #endif
  17. const string k_ClassName = "unity-button-strip-field";
  18. const string k_ButtonClass = k_ClassName + "__button";
  19. const string k_IconClass = k_ClassName + "__button-icon";
  20. const string k_ButtonLeftClass = k_ButtonClass + "--left";
  21. const string k_ButtonMiddleClass = k_ButtonClass + "--middle";
  22. const string k_ButtonRightClass = k_ButtonClass + "--right";
  23. const string k_ButtonAloneClass = k_ButtonClass + "--alone";
  24. List<Button> m_Buttons = new List<Button>();
  25. /// <summary>
  26. /// Add a button to the button strip.
  27. /// </summary>
  28. /// <param name="text">Text to be displayed.</param>
  29. /// <param name="name">Name of the element.</param>
  30. public void AddButton(string text, string name = "")
  31. {
  32. var button = CreateButton(name);
  33. button.text = text;
  34. Add(button);
  35. }
  36. /// <summary>
  37. /// Add a button to the button strip.
  38. /// </summary>
  39. /// <param name="icon">Icon used for the button.</param>
  40. /// <param name="name">Name of the element.</param>
  41. public void AddButton(Background icon, string name = "")
  42. {
  43. var button = CreateButton(name);
  44. var iconElement = new VisualElement();
  45. iconElement.AddToClassList(k_IconClass);
  46. iconElement.style.backgroundImage = icon;
  47. button.Add(iconElement);
  48. Add(button);
  49. }
  50. Button CreateButton(string name)
  51. {
  52. var button = new Button { name = name, };
  53. button.AddToClassList(k_ButtonClass);
  54. button.RegisterCallback<DetachFromPanelEvent>(OnButtonDetachFromPanel);
  55. button.clicked += () => { value = m_Buttons.IndexOf(button); };
  56. m_Buttons.Add(button);
  57. Add(button);
  58. RefreshButtonsStyling();
  59. return button;
  60. }
  61. static void OnButtonDetachFromPanel(DetachFromPanelEvent evt)
  62. {
  63. if (evt.currentTarget is VisualElement element
  64. && element.parent is ButtonStripField buttonStrip)
  65. {
  66. buttonStrip.RefreshButtonsStyling();
  67. buttonStrip.EnsureValueIsValid();
  68. }
  69. }
  70. void RefreshButtonsStyling()
  71. {
  72. for (var i = 0; i < m_Buttons.Count; ++i)
  73. {
  74. var button = m_Buttons[i];
  75. bool alone = m_Buttons.Count == 1;
  76. bool left = i == 0;
  77. bool right = i == m_Buttons.Count - 1;
  78. button.EnableInClassList(k_ButtonAloneClass, alone);
  79. button.EnableInClassList(k_ButtonLeftClass, !alone && left);
  80. button.EnableInClassList(k_ButtonRightClass, !alone && right);
  81. button.EnableInClassList(k_ButtonMiddleClass, !alone && !left && !right);
  82. }
  83. }
  84. /// <summary>
  85. /// Constructs a <see cref="ButtonStripField"/>, with all required properties provided.
  86. /// </summary>
  87. public ButtonStripField() : base("", null)
  88. {
  89. }
  90. /// <summary>
  91. /// Constructs a <see cref="ButtonStripField"/>, with all required properties provided.
  92. /// </summary>
  93. /// <param name="label">The list of items to use as a data source.</param>
  94. public ButtonStripField(string label) : base(label, null)
  95. {
  96. AddToClassList(k_ClassName);
  97. }
  98. public override void SetValueWithoutNotify(int newValue)
  99. {
  100. newValue = Mathf.Clamp(newValue, 0, m_Buttons.Count - 1);
  101. base.SetValueWithoutNotify(newValue);
  102. RefreshButtonsState();
  103. }
  104. void EnsureValueIsValid()
  105. {
  106. SetValueWithoutNotify(Mathf.Clamp(value, 0, m_Buttons.Count - 1));
  107. }
  108. void RefreshButtonsState()
  109. {
  110. for (var i = 0; i < m_Buttons.Count; ++i)
  111. m_Buttons[i].SetChecked(i == value);
  112. }
  113. }
  114. }