BoneReparentTool.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using UnityEditor.U2D.Layout;
  3. using UnityEditor.IMGUI.Controls;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. namespace UnityEditor.U2D.Animation
  7. {
  8. internal class BoneReparentToolController : BoneTreeWidgetController
  9. {
  10. public BoneReparentToolController(IBoneTreeViewModel model, SkinningEvents eventSystem) : base(model, eventSystem)
  11. {}
  12. public override bool CanDrag()
  13. {
  14. m_SkinningEvents.boneVisibility.Invoke("drag");
  15. return (m_Model.hasCharacter && m_Model.mode == SkinningMode.Character) ||
  16. (!m_Model.hasCharacter && m_Model.mode == SkinningMode.SpriteSheet);
  17. }
  18. public override bool CanRename()
  19. {
  20. m_SkinningEvents.boneVisibility.Invoke("rename");
  21. return true;
  22. }
  23. }
  24. internal class BoneReparentToolModel : BoneTreeWidgetModel
  25. {
  26. public BoneReparentToolModel(SkinningCache cache, IBoneVisibilityToolView view)
  27. {
  28. m_SkinningCache = cache;
  29. m_View = view;
  30. m_Data = skinningCache.CreateCache<BoneVisibilityToolData>();
  31. }
  32. }
  33. internal class BoneReparentTool : SkeletonToolWrapper
  34. {
  35. BoneReparentToolWindow m_View;
  36. BoneReparentToolModel m_Model;
  37. private BoneReparentToolController m_Controller;
  38. public override void Initialize(LayoutOverlay layout)
  39. {
  40. if (m_View == null)
  41. {
  42. m_View = BoneReparentToolWindow.CreateFromUXML();
  43. }
  44. m_Model = new BoneReparentToolModel(skinningCache, m_View);
  45. m_Controller = new BoneReparentToolController(m_Model, skinningCache.events);
  46. m_View.GetController = () => m_Controller;
  47. m_View.GetModel = () => m_Model;
  48. layout.rightOverlay.Add(m_View);
  49. m_View.SetHiddenFromLayout(true);
  50. }
  51. protected override void OnActivate()
  52. {
  53. m_View.SetHiddenFromLayout(false);
  54. m_Controller.Activate();
  55. skeletonTool.Activate();
  56. }
  57. protected override void OnDeactivate()
  58. {
  59. m_View.SetHiddenFromLayout(true);
  60. m_Controller.Deactivate();
  61. skeletonTool.Deactivate();
  62. }
  63. protected override void OnGUI()
  64. {
  65. skeletonTool.mode = mode;
  66. skeletonTool.editBindPose = editBindPose;
  67. skeletonTool.DoGUI();
  68. }
  69. }
  70. internal class BoneReparentToolWindow : VisualElement, IBoneVisibilityToolView
  71. {
  72. public class CustomUxmlFactory : UxmlFactory<BoneReparentToolWindow, UxmlTraits> {}
  73. BoneReparentToolView m_ToolView;
  74. public Func<IBoneTreeViewModel> GetModel = () => null;
  75. public Func<BoneTreeWidgetController> GetController = () => null;
  76. static internal BoneReparentToolWindow CreateFromUXML()
  77. {
  78. var visualTree = ResourceLoader.Load<VisualTreeAsset>("SkinningModule/BoneReparentWindow.uxml");
  79. var ve = visualTree.CloneTree().Q("BoneReparentToolWindow") as BoneReparentToolWindow;
  80. ve.BindElements();
  81. return ve;
  82. }
  83. internal void BindElements()
  84. {
  85. m_ToolView = this.Q<BoneReparentToolView>();
  86. m_ToolView.GetModel = InternalGetModel;
  87. m_ToolView.GetController = InternalGetController;
  88. this.styleSheets.Add(ResourceLoader.Load<StyleSheet>("SkinningModule/BoneReparentStyle.uss"));
  89. }
  90. IBoneTreeViewModel InternalGetModel()
  91. {
  92. return GetModel();
  93. }
  94. BoneTreeWidgetController InternalGetController()
  95. {
  96. return GetController();
  97. }
  98. public void OnBoneSelectionChange(SkeletonSelection skeleton)
  99. {
  100. ((IBoneVisibilityToolView)toolView).OnBoneSelectionChange(skeleton);
  101. }
  102. public void OnBoneExpandedChange(BoneCache[] bones)
  103. {
  104. ((IBoneVisibilityToolView)toolView).OnBoneExpandedChange(bones);
  105. }
  106. public void OnBoneNameChanged(BoneCache bone)
  107. {
  108. ((IBoneVisibilityToolView)toolView).OnBoneNameChanged(bone);
  109. }
  110. public void OnSelectionChange(SkeletonCache skeleton)
  111. {
  112. ((IBoneVisibilityToolView)toolView).OnSelectionChange(skeleton);
  113. }
  114. BoneReparentToolView toolView { get {return m_ToolView; } }
  115. public void Deactivate()
  116. {
  117. toolView.Deactivate();
  118. }
  119. }
  120. internal class BoneReparentToolView : BoneVisibilityToolView
  121. {
  122. public class CustomUxmlFactory : UxmlFactory<BoneReparentToolView, CustomUxmlTraits> {}
  123. public class CustomUxmlTraits : UxmlTraits {}
  124. protected override VisibilityToolColumnHeader SetupToolColumnHeader()
  125. {
  126. var columns = new MultiColumnHeaderState.Column[3];
  127. columns[0] = new MultiColumnHeaderState.Column
  128. {
  129. headerContent = VisibilityTreeViewBase.VisibilityIconStyle.visibilityOnIcon,
  130. headerTextAlignment = TextAlignment.Center,
  131. width = 32,
  132. minWidth = 32,
  133. maxWidth = 32,
  134. autoResize = false,
  135. allowToggleVisibility = true
  136. };
  137. columns[1] = new MultiColumnHeaderState.Column
  138. {
  139. headerContent = EditorGUIUtility.TrTextContent(TextContent.bone),
  140. headerTextAlignment = TextAlignment.Center,
  141. width = 130,
  142. minWidth = 130,
  143. autoResize = true,
  144. allowToggleVisibility = false
  145. };
  146. columns[2] = new MultiColumnHeaderState.Column
  147. {
  148. headerContent = EditorGUIUtility.TrTextContent(TextContent.depth),
  149. headerTextAlignment = TextAlignment.Center,
  150. width = 50,
  151. minWidth = 50,
  152. maxWidth = 50,
  153. autoResize = true,
  154. allowToggleVisibility = true
  155. };
  156. var multiColumnHeaderState = new MultiColumnHeaderState(columns);
  157. return new VisibilityToolColumnHeader(multiColumnHeaderState)
  158. {
  159. GetAllVisibility = GetAllVisibility,
  160. SetAllVisibility = SetAllVisibility,
  161. canSort = false,
  162. height = 20,
  163. visibilityColumn = 0
  164. };
  165. }
  166. }
  167. }