123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using System;
- using System.Collections.Generic;
- using UnityEngine.UIElements;
- using UnityEditor.U2D.Common;
-
- namespace UnityEditor.U2D.Animation
- {
- internal interface IInfluenceWindow
- {
- void UpdateList(List<TransformCache> transformsList);
- void UpdateSelection(IEnumerable<TransformCache> newSelection);
- void ToggleAddButton(bool enabled);
- void ToggleRemoveButton(bool enabled);
- string headerText { get; set; }
- string listLabelText { set; }
- void SetHiddenFromLayout(bool hide);
- void SetTooltips(string addButtonTooltip, string removeButtonTooltip);
- bool visible { get; }
-
- event Action onAddElement;
- event Action onRemoveElement;
- event Action<IEnumerable<TransformCache>> onReordered;
- event Action<IEnumerable<object>> onSelectionChanged;
- }
-
- #if ENABLE_UXML_SERIALIZED_DATA
- [UxmlElement]
- #endif
- internal partial class InfluenceWindow : VisualElement, IInfluenceWindow
- {
- #if ENABLE_UXML_TRAITS
- public class CustomUxmlFactory : UxmlFactory<InfluenceWindow, UxmlTraits> { }
- #endif
-
- public event Action onAddElement = () => {};
- public event Action onRemoveElement = () => {};
- public event Action<IEnumerable<TransformCache>> onReordered = _ => {};
- public event Action<IEnumerable<object>> onSelectionChanged = _ => {};
-
- UnityEngine.UIElements.PopupWindow m_HeaderLabel;
-
- Label m_ListLabel;
-
- IEnumerable<TransformCache> m_Selection;
-
- ListView m_ListView;
- List<TransformCache> m_Influences = new List<TransformCache>();
-
- Button m_AddButton;
- Button m_RemoveButton;
- bool m_IgnoreSelectionChange = false;
-
- public string headerText
- {
- get => m_HeaderLabel.text;
- set => m_HeaderLabel.text = value;
- }
-
- public string listLabelText
- {
- get => m_ListLabel.text;
- set => m_ListLabel.text = value;
- }
-
- public void SetTooltips(string addButtonTooltip, string removeButtonTooltip)
- {
- m_AddButton.tooltip = addButtonTooltip;
- m_RemoveButton.tooltip = removeButtonTooltip;
- }
-
- internal static InfluenceWindow CreateFromUxml()
- {
- var visualTree = ResourceLoader.Load<VisualTreeAsset>("SkinningModule/InfluenceWindow.uxml");
- var ve = (InfluenceWindow)visualTree.CloneTree().Q("InfluenceWindow");
- ve.styleSheets.Add(ResourceLoader.Load<StyleSheet>("SkinningModule/InfluenceWindowStyle.uss"));
- if (EditorGUIUtility.isProSkin)
- ve.AddToClassList("Dark");
- ve.LocalizeTextInChildren();
- ve.BindElements();
- return ve;
- }
-
- public void SetListReorderable(bool reorderable)
- {
- m_ListView.reorderable = reorderable;
- }
-
- public void SetAllowMultiselect(bool allowMultiselect)
- {
- m_ListView.selectionType = allowMultiselect ? SelectionType.Multiple : SelectionType.Single;
- }
-
- private void BindElements()
- {
- m_HeaderLabel = this.Q<UnityEngine.UIElements.PopupWindow>();
- m_ListLabel = this.Q<Label>();
-
- m_ListView = this.Q<ListView>();
- m_ListView.selectionType = SelectionType.Multiple;
- m_ListView.itemsSource = m_Influences;
- m_ListView.makeItem = () =>
- {
- var label = new Label()
- {
- name = "ListRow"
- };
- return label;
- };
- m_ListView.bindItem = (e, index) =>
- {
- if (m_Influences[index] == null)
- return;
-
- ((Label)e).text = m_Influences[index].name;
- if (index % 2 == 0)
- {
- e.RemoveFromClassList("ListRowOddColor");
- e.AddToClassList("ListRowEvenColor");
- }
- else
- {
- e.RemoveFromClassList("ListRowEvenColor");
- e.AddToClassList("ListRowOddColor");
- }
- };
-
- m_ListView.selectionChanged += OnListViewSelectionChanged;
- m_AddButton = this.Q<Button>("AddButton");
- m_AddButton.clickable.clicked += () =>
- {
- onAddElement.Invoke();
- };
- m_RemoveButton = this.Q<Button>("RemoveButton");
- m_RemoveButton.clickable.clicked += () =>
- {
- onRemoveElement.Invoke();
- };
-
- RegisterCallback<DragPerformEvent>(x => onReordered(m_Influences) );
- }
-
- public void UpdateList(List<TransformCache> transformsList)
- {
- m_Influences = transformsList;
- m_ListView.itemsSource = m_Influences;
- m_ListView.Rebuild();
- }
-
- public void UpdateSelection(IEnumerable<TransformCache> newSelection)
- {
- m_Selection = newSelection;
-
- m_IgnoreSelectionChange = true;
- m_ListView.ClearSelection();
- foreach (var selection in newSelection)
- {
- var index = m_Influences.IndexOf(selection);
- if(index >= 0)
- m_ListView.AddToSelection(index);
- }
-
- m_IgnoreSelectionChange = false;
- }
-
- private void OnListViewSelectionChanged(IEnumerable<object> newSelection)
- {
- if (m_IgnoreSelectionChange)
- return;
-
- onSelectionChanged(newSelection);
- }
-
- public void ToggleAddButton(bool enabled)
- {
- m_AddButton.SetEnabled(enabled);
- }
-
- public void ToggleRemoveButton(bool enabled)
- {
- m_RemoveButton.SetEnabled(enabled && m_ListView.selectedIndex >= 0);
- }
-
- void IInfluenceWindow.SetHiddenFromLayout(bool hide)
- {
- this.SetHiddenFromLayout(hide);
- }
- }
- }
|