Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ButtonStripField.cs 4.4KB

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