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