暫無描述
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.

InfluenceWindow.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.UIElements;
  4. using UnityEditor.U2D.Common;
  5. namespace UnityEditor.U2D.Animation
  6. {
  7. internal interface IInfluenceWindow
  8. {
  9. void UpdateList(List<TransformCache> transformsList);
  10. void UpdateSelection(IEnumerable<TransformCache> newSelection);
  11. void ToggleAddButton(bool enabled);
  12. void ToggleRemoveButton(bool enabled);
  13. string headerText { get; set; }
  14. string listLabelText { set; }
  15. void SetHiddenFromLayout(bool hide);
  16. void SetTooltips(string addButtonTooltip, string removeButtonTooltip);
  17. bool visible { get; }
  18. event Action onAddElement;
  19. event Action onRemoveElement;
  20. event Action<IEnumerable<TransformCache>> onReordered;
  21. event Action<IEnumerable<object>> onSelectionChanged;
  22. }
  23. #if ENABLE_UXML_SERIALIZED_DATA
  24. [UxmlElement]
  25. #endif
  26. internal partial class InfluenceWindow : VisualElement, IInfluenceWindow
  27. {
  28. #if ENABLE_UXML_TRAITS
  29. public class CustomUxmlFactory : UxmlFactory<InfluenceWindow, UxmlTraits> { }
  30. #endif
  31. public event Action onAddElement = () => {};
  32. public event Action onRemoveElement = () => {};
  33. public event Action<IEnumerable<TransformCache>> onReordered = _ => {};
  34. public event Action<IEnumerable<object>> onSelectionChanged = _ => {};
  35. UnityEngine.UIElements.PopupWindow m_HeaderLabel;
  36. Label m_ListLabel;
  37. IEnumerable<TransformCache> m_Selection;
  38. ListView m_ListView;
  39. List<TransformCache> m_Influences = new List<TransformCache>();
  40. Button m_AddButton;
  41. Button m_RemoveButton;
  42. bool m_IgnoreSelectionChange = false;
  43. public string headerText
  44. {
  45. get => m_HeaderLabel.text;
  46. set => m_HeaderLabel.text = value;
  47. }
  48. public string listLabelText
  49. {
  50. get => m_ListLabel.text;
  51. set => m_ListLabel.text = value;
  52. }
  53. public void SetTooltips(string addButtonTooltip, string removeButtonTooltip)
  54. {
  55. m_AddButton.tooltip = addButtonTooltip;
  56. m_RemoveButton.tooltip = removeButtonTooltip;
  57. }
  58. internal static InfluenceWindow CreateFromUxml()
  59. {
  60. var visualTree = ResourceLoader.Load<VisualTreeAsset>("SkinningModule/InfluenceWindow.uxml");
  61. var ve = (InfluenceWindow)visualTree.CloneTree().Q("InfluenceWindow");
  62. ve.styleSheets.Add(ResourceLoader.Load<StyleSheet>("SkinningModule/InfluenceWindowStyle.uss"));
  63. if (EditorGUIUtility.isProSkin)
  64. ve.AddToClassList("Dark");
  65. ve.LocalizeTextInChildren();
  66. ve.BindElements();
  67. return ve;
  68. }
  69. public void SetListReorderable(bool reorderable)
  70. {
  71. m_ListView.reorderable = reorderable;
  72. }
  73. public void SetAllowMultiselect(bool allowMultiselect)
  74. {
  75. m_ListView.selectionType = allowMultiselect ? SelectionType.Multiple : SelectionType.Single;
  76. }
  77. private void BindElements()
  78. {
  79. m_HeaderLabel = this.Q<UnityEngine.UIElements.PopupWindow>();
  80. m_ListLabel = this.Q<Label>();
  81. m_ListView = this.Q<ListView>();
  82. m_ListView.selectionType = SelectionType.Multiple;
  83. m_ListView.itemsSource = m_Influences;
  84. m_ListView.makeItem = () =>
  85. {
  86. var label = new Label()
  87. {
  88. name = "ListRow"
  89. };
  90. return label;
  91. };
  92. m_ListView.bindItem = (e, index) =>
  93. {
  94. if (m_Influences[index] == null)
  95. return;
  96. ((Label)e).text = m_Influences[index].name;
  97. if (index % 2 == 0)
  98. {
  99. e.RemoveFromClassList("ListRowOddColor");
  100. e.AddToClassList("ListRowEvenColor");
  101. }
  102. else
  103. {
  104. e.RemoveFromClassList("ListRowEvenColor");
  105. e.AddToClassList("ListRowOddColor");
  106. }
  107. };
  108. m_ListView.selectionChanged += OnListViewSelectionChanged;
  109. m_AddButton = this.Q<Button>("AddButton");
  110. m_AddButton.clickable.clicked += () =>
  111. {
  112. onAddElement.Invoke();
  113. };
  114. m_RemoveButton = this.Q<Button>("RemoveButton");
  115. m_RemoveButton.clickable.clicked += () =>
  116. {
  117. onRemoveElement.Invoke();
  118. };
  119. RegisterCallback<DragPerformEvent>(x => onReordered(m_Influences) );
  120. }
  121. public void UpdateList(List<TransformCache> transformsList)
  122. {
  123. m_Influences = transformsList;
  124. m_ListView.itemsSource = m_Influences;
  125. m_ListView.Rebuild();
  126. }
  127. public void UpdateSelection(IEnumerable<TransformCache> newSelection)
  128. {
  129. m_Selection = newSelection;
  130. m_IgnoreSelectionChange = true;
  131. m_ListView.ClearSelection();
  132. foreach (var selection in newSelection)
  133. {
  134. var index = m_Influences.IndexOf(selection);
  135. if(index >= 0)
  136. m_ListView.AddToSelection(index);
  137. }
  138. m_IgnoreSelectionChange = false;
  139. }
  140. private void OnListViewSelectionChanged(IEnumerable<object> newSelection)
  141. {
  142. if (m_IgnoreSelectionChange)
  143. return;
  144. onSelectionChanged(newSelection);
  145. }
  146. public void ToggleAddButton(bool enabled)
  147. {
  148. m_AddButton.SetEnabled(enabled);
  149. }
  150. public void ToggleRemoveButton(bool enabled)
  151. {
  152. m_RemoveButton.SetEnabled(enabled && m_ListView.selectedIndex >= 0);
  153. }
  154. void IInfluenceWindow.SetHiddenFromLayout(bool hide)
  155. {
  156. this.SetHiddenFromLayout(hide);
  157. }
  158. }
  159. }