SpriteBoneInfluenceListWidget.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. namespace UnityEditor.U2D.Animation
  7. {
  8. internal class SelectListView : ListView
  9. {
  10. public class CustomUxmlFactory : UxmlFactory<SelectListView, UxmlTraits> {}
  11. public new void AddToSelection(int index)
  12. {
  13. base.AddToSelection(index);
  14. }
  15. public new void ClearSelection()
  16. {
  17. base.ClearSelection();
  18. }
  19. }
  20. internal class SpriteBoneInfluenceListWidget : VisualElement
  21. {
  22. public class CustomUxmlFactory : UxmlFactory<SpriteBoneInfluenceListWidget, CustomUxmlTraits> {}
  23. public class CustomUxmlTraits : UxmlTraits {}
  24. private List<BoneCache> m_BoneInfluences;
  25. private SelectListView m_ListView;
  26. bool m_IgnoreSelectionChange = false;
  27. private Button m_AddButton;
  28. private Button m_RemoveButton;
  29. public Action onAddBone = () => {};
  30. public Action onRemoveBone = () => {};
  31. public Action<IEnumerable<BoneCache>> onReordered = _ => {};
  32. public Action<IEnumerable<BoneCache>> onSelectionChanged = (s) => {};
  33. public Func<SpriteBoneInflueceToolController> GetController = () => null;
  34. public SpriteBoneInfluenceListWidget()
  35. {
  36. var visualTree = ResourceLoader.Load<VisualTreeAsset>("SkinningModule/SpriteBoneInfluenceListWidget.uxml");
  37. var ve = visualTree.CloneTree().Q("Container");
  38. ve.styleSheets.Add(ResourceLoader.Load<StyleSheet>("SkinningModule/SpriteBoneInfluenceListWidgetStyle.uss"));
  39. if (EditorGUIUtility.isProSkin)
  40. AddToClassList("Dark");
  41. this.Add(ve);
  42. BindElements();
  43. }
  44. private void BindElements()
  45. {
  46. m_ListView = this.Q<SelectListView>();
  47. m_ListView.selectionType = SelectionType.Multiple;
  48. m_ListView.itemsSource = m_BoneInfluences;
  49. m_ListView.makeItem = () =>
  50. {
  51. var label = new Label()
  52. {
  53. name = "ListRow"
  54. };
  55. return label;
  56. };
  57. m_ListView.bindItem = (e, index) =>
  58. {
  59. if (m_BoneInfluences[index] == null)
  60. return;
  61. (e as Label).text = m_BoneInfluences[index].name;
  62. if (index % 2 == 0)
  63. {
  64. e.RemoveFromClassList("ListRowOddColor");
  65. e.AddToClassList("ListRowEvenColor");
  66. }
  67. else
  68. {
  69. e.RemoveFromClassList("ListRowEvenColor");
  70. e.AddToClassList("ListRowOddColor");
  71. }
  72. };
  73. m_ListView.onSelectionChange += OnListViewSelectionChanged;
  74. m_AddButton = this.Q<Button>("AddButton");
  75. m_AddButton.clickable.clicked += OnAddButtonClick;
  76. m_RemoveButton = this.Q<Button>("RemoveButton");
  77. m_RemoveButton.clickable.clicked += OnRemoveButtonClick;
  78. this.RegisterCallback<DragPerformEvent>(x => onReordered(m_BoneInfluences) );
  79. }
  80. private void OnListViewSelectionChanged(IEnumerable<object> o)
  81. {
  82. if (m_IgnoreSelectionChange)
  83. return;
  84. var selectedBones = o.OfType<BoneCache>().ToArray();
  85. onSelectionChanged(selectedBones);
  86. }
  87. private void OnAddButtonClick()
  88. {
  89. onAddBone();
  90. }
  91. private void OnRemoveButtonClick()
  92. {
  93. onRemoveBone();
  94. }
  95. public void Update()
  96. {
  97. m_BoneInfluences = GetController().GetSelectedSpriteBoneInfluence().ToList();
  98. m_ListView.itemsSource = m_BoneInfluences;
  99. m_ListView.Refresh();
  100. }
  101. internal void OnBoneSelectionChanged()
  102. {
  103. var selectedBones = GetController().GetSelectedBoneForList(m_BoneInfluences);
  104. m_IgnoreSelectionChange = true;
  105. m_ListView.ClearSelection();
  106. foreach (var bone in selectedBones)
  107. {
  108. m_ListView.AddToSelection(bone);
  109. }
  110. m_IgnoreSelectionChange = false;
  111. m_AddButton.SetEnabled(GetController().ShouldEnableAddButton(m_BoneInfluences));
  112. m_RemoveButton.SetEnabled(GetController().InCharacterMode() && m_ListView.selectedIndex >= 0);
  113. }
  114. }
  115. }