Нема описа
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.

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