SkinningModuleView.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using System;
  2. using UnityEditor.U2D.Common;
  3. using UnityEditor.U2D.Layout;
  4. using UnityEngine;
  5. using UnityEditor.ShortcutManagement;
  6. using UnityEngine.UIElements;
  7. namespace UnityEditor.U2D.Animation
  8. {
  9. internal partial class SkinningModule
  10. {
  11. private LayoutOverlay m_LayoutOverlay;
  12. private BoneToolbar m_BoneToolbar;
  13. private MeshToolbar m_MeshToolbar;
  14. private WeightToolbar m_WeightToolbar;
  15. private InternalEditorBridge.ShortcutContext m_ShortcutContext;
  16. private static SkinningModule GetModuleFromContext(ShortcutArguments args)
  17. {
  18. var sc = args.context as InternalEditorBridge.ShortcutContext;
  19. if (sc == null)
  20. return null;
  21. return sc.context as SkinningModule;
  22. }
  23. [Shortcut("2D/Animation/Toggle Tool Text", typeof(InternalEditorBridge.ShortcutContext), KeyCode.BackQuote, ShortcutModifiers.Shift)]
  24. private static void CollapseToolbar(ShortcutArguments args)
  25. {
  26. var sm = GetModuleFromContext(args);
  27. if (sm != null)
  28. {
  29. SkinningModuleSettings.compactToolBar = !SkinningModuleSettings.compactToolBar;
  30. }
  31. }
  32. [Shortcut("2D/Animation/Restore Bind Pose", typeof(InternalEditorBridge.ShortcutContext), KeyCode.Alpha1, ShortcutModifiers.Shift)]
  33. private static void DisablePoseModeKey(ShortcutArguments args)
  34. {
  35. var sm = GetModuleFromContext(args);
  36. if (sm != null && !sm.spriteEditor.editingDisabled)
  37. {
  38. var effectiveSkeleton = sm.skinningCache.GetEffectiveSkeleton(sm.skinningCache.selectedSprite);
  39. if (effectiveSkeleton != null && effectiveSkeleton.isPosePreview)
  40. {
  41. using (sm.skinningCache.UndoScope(TextContent.restorePose))
  42. {
  43. sm.skinningCache.RestoreBindPose();
  44. sm.skinningCache.events.shortcut.Invoke("#1");
  45. }
  46. }
  47. }
  48. }
  49. [Shortcut("2D/Animation/Toggle Character Mode", typeof(InternalEditorBridge.ShortcutContext), KeyCode.Alpha2, ShortcutModifiers.Shift)]
  50. private static void ToggleCharacterModeKey(ShortcutArguments args)
  51. {
  52. var sm = GetModuleFromContext(args);
  53. if (sm != null && !sm.spriteEditor.editingDisabled && sm.skinningCache.hasCharacter)
  54. {
  55. var tool = sm.skinningCache.GetTool(Tools.SwitchMode);
  56. using (sm.skinningCache.UndoScope(TextContent.setMode))
  57. {
  58. if (tool.isActive)
  59. tool.Deactivate();
  60. else
  61. tool.Activate();
  62. }
  63. sm.skinningCache.events.shortcut.Invoke("#2");
  64. }
  65. }
  66. [Shortcut("2D/Animation/Preview Pose", typeof(InternalEditorBridge.ShortcutContext), KeyCode.Q, ShortcutModifiers.Shift)]
  67. private static void EditPoseKey(ShortcutArguments args)
  68. {
  69. var sm = GetModuleFromContext(args);
  70. if (sm != null && !sm.spriteEditor.editingDisabled)
  71. {
  72. sm.SetSkeletonTool(Tools.EditPose);
  73. sm.skinningCache.events.shortcut.Invoke("#q");
  74. }
  75. }
  76. [Shortcut("2D/Animation/Edit Bone", typeof(InternalEditorBridge.ShortcutContext), KeyCode.W, ShortcutModifiers.Shift)]
  77. private static void EditJointsKey(ShortcutArguments args)
  78. {
  79. var sm = GetModuleFromContext(args);
  80. if (sm != null && !sm.spriteEditor.editingDisabled)
  81. {
  82. sm.SetSkeletonTool(Tools.EditJoints);
  83. sm.skinningCache.events.shortcut.Invoke("#w");
  84. }
  85. }
  86. [Shortcut("2D/Animation/Create Bone", typeof(InternalEditorBridge.ShortcutContext), KeyCode.E, ShortcutModifiers.Shift)]
  87. private static void CreateBoneKey(ShortcutArguments args)
  88. {
  89. var sm = GetModuleFromContext(args);
  90. if (sm != null && !sm.spriteEditor.editingDisabled)
  91. {
  92. sm.SetSkeletonTool(Tools.CreateBone);
  93. sm.skinningCache.events.shortcut.Invoke("#e");
  94. }
  95. }
  96. [Shortcut("2D/Animation/Split Bone", typeof(InternalEditorBridge.ShortcutContext), KeyCode.R, ShortcutModifiers.Shift)]
  97. private static void SplitBoneKey(ShortcutArguments args)
  98. {
  99. var sm = GetModuleFromContext(args);
  100. if (sm != null && !sm.spriteEditor.editingDisabled)
  101. {
  102. sm.SetSkeletonTool(Tools.SplitBone);
  103. sm.skinningCache.events.shortcut.Invoke("#r");
  104. }
  105. }
  106. [Shortcut("2D/Animation/Auto Geometry", typeof(InternalEditorBridge.ShortcutContext), KeyCode.A, ShortcutModifiers.Shift)]
  107. private static void GenerateGeometryKey(ShortcutArguments args)
  108. {
  109. var sm = GetModuleFromContext(args);
  110. if (sm != null && !sm.spriteEditor.editingDisabled)
  111. {
  112. sm.SetMeshTool(Tools.GenerateGeometry);
  113. sm.skinningCache.events.shortcut.Invoke("#a");
  114. }
  115. }
  116. [Shortcut("2D/Animation/Edit Geometry", typeof(InternalEditorBridge.ShortcutContext), KeyCode.S, ShortcutModifiers.Shift)]
  117. private static void MeshSelectionKey(ShortcutArguments args)
  118. {
  119. var sm = GetModuleFromContext(args);
  120. if (sm != null && !sm.spriteEditor.editingDisabled)
  121. {
  122. sm.SetMeshTool(Tools.EditGeometry);
  123. sm.skinningCache.events.shortcut.Invoke("#s");
  124. }
  125. }
  126. [Shortcut("2D/Animation/Create Vertex", typeof(InternalEditorBridge.ShortcutContext), KeyCode.J, ShortcutModifiers.Shift)]
  127. private static void CreateVertex(ShortcutArguments args)
  128. {
  129. var sm = GetModuleFromContext(args);
  130. if (sm != null && !sm.spriteEditor.editingDisabled)
  131. {
  132. sm.SetMeshTool(Tools.CreateVertex);
  133. sm.skinningCache.events.shortcut.Invoke("#d");
  134. }
  135. }
  136. [Shortcut("2D/Animation/Create Edge", typeof(InternalEditorBridge.ShortcutContext), KeyCode.G, ShortcutModifiers.Shift)]
  137. private static void CreateEdgeKey(ShortcutArguments args)
  138. {
  139. var sm = GetModuleFromContext(args);
  140. if (sm != null && !sm.spriteEditor.editingDisabled)
  141. {
  142. sm.SetMeshTool(Tools.CreateEdge);
  143. sm.skinningCache.events.shortcut.Invoke("#g");
  144. }
  145. }
  146. [Shortcut("2D/Animation/Split Edge", typeof(InternalEditorBridge.ShortcutContext), KeyCode.H, ShortcutModifiers.Shift)]
  147. private static void SplitEdge(ShortcutArguments args)
  148. {
  149. var sm = GetModuleFromContext(args);
  150. if (sm != null && !sm.spriteEditor.editingDisabled)
  151. {
  152. sm.SetMeshTool(Tools.SplitEdge);
  153. sm.skinningCache.events.shortcut.Invoke("#h");
  154. }
  155. }
  156. [Shortcut("2D/Animation/Auto Weights", typeof(InternalEditorBridge.ShortcutContext), KeyCode.Z, ShortcutModifiers.Shift)]
  157. private static void GenerateWeightsKey(ShortcutArguments args)
  158. {
  159. var sm = GetModuleFromContext(args);
  160. if (sm != null && !sm.spriteEditor.editingDisabled)
  161. {
  162. sm.SetWeightTool(Tools.GenerateWeights);
  163. sm.skinningCache.events.shortcut.Invoke("#z");
  164. }
  165. }
  166. [Shortcut("2D/Animation/Weight Slider", typeof(InternalEditorBridge.ShortcutContext), KeyCode.X, ShortcutModifiers.Shift)]
  167. private static void WeightSliderKey(ShortcutArguments args)
  168. {
  169. var sm = GetModuleFromContext(args);
  170. if (sm != null && !sm.spriteEditor.editingDisabled)
  171. {
  172. sm.SetWeightTool(Tools.WeightSlider);
  173. sm.skinningCache.events.shortcut.Invoke("#x");
  174. }
  175. }
  176. [Shortcut("2D/Animation/Weight Brush", typeof(InternalEditorBridge.ShortcutContext), KeyCode.N, ShortcutModifiers.Shift)]
  177. private static void WeightBrushKey(ShortcutArguments args)
  178. {
  179. var sm = GetModuleFromContext(args);
  180. if (sm != null && !sm.spriteEditor.editingDisabled)
  181. {
  182. sm.SetWeightTool(Tools.WeightBrush);
  183. sm.skinningCache.events.shortcut.Invoke("#c");
  184. }
  185. }
  186. [Shortcut("2D/Animation/Bone Influence", typeof(InternalEditorBridge.ShortcutContext), KeyCode.V, ShortcutModifiers.Shift)]
  187. private static void BoneInfluenceKey(ShortcutArguments args)
  188. {
  189. var sm = GetModuleFromContext(args);
  190. if (sm != null && !sm.spriteEditor.editingDisabled && sm.skinningCache.mode == SkinningMode.Character)
  191. {
  192. sm.SetWeightTool(Tools.BoneInfluence);
  193. sm.skinningCache.events.shortcut.Invoke("#v");
  194. }
  195. }
  196. [Shortcut("2D/Animation/Paste Panel Weights", typeof(InternalEditorBridge.ShortcutContext), KeyCode.B, ShortcutModifiers.Shift)]
  197. private static void PastePanelKey(ShortcutArguments args)
  198. {
  199. var sm = GetModuleFromContext(args);
  200. if (sm != null && !sm.spriteEditor.editingDisabled)
  201. {
  202. sm.m_HorizontalToggleTools.TogglePasteTool(sm.currentTool);
  203. sm.skinningCache.events.shortcut.Invoke("#b");
  204. }
  205. }
  206. [Shortcut("2D/Animation/Visibility Panel", typeof(InternalEditorBridge.ShortcutContext), KeyCode.P, ShortcutModifiers.Shift)]
  207. private static void VisibilityPanelKey(ShortcutArguments args)
  208. {
  209. var sm = GetModuleFromContext(args);
  210. if (sm != null && !sm.spriteEditor.editingDisabled)
  211. {
  212. sm.m_HorizontalToggleTools.ToggleVisibilityTool(sm.currentTool);
  213. sm.skinningCache.events.shortcut.Invoke("#p");
  214. }
  215. }
  216. private void AddMainUI(VisualElement mainView)
  217. {
  218. var visualTree = ResourceLoader.Load<VisualTreeAsset>("LayoutOverlay/LayoutOverlay.uxml");
  219. VisualElement clone = visualTree.CloneTree();
  220. m_LayoutOverlay = clone.Q<LayoutOverlay>("LayoutOverlay");
  221. mainView.Add(m_LayoutOverlay);
  222. m_LayoutOverlay.hasScrollbar = true;
  223. m_LayoutOverlay.StretchToParentSize();
  224. CreateBoneToolbar();
  225. CreateMeshToolbar();
  226. CreateWeightToolbar();
  227. m_ShortcutContext = new InternalEditorBridge.ShortcutContext()
  228. {
  229. isActive = isFocused,
  230. context = this
  231. };
  232. InternalEditorBridge.RegisterShortcutContext(m_ShortcutContext);
  233. InternalEditorBridge.AddEditorApplicationProjectLoadedCallback(OnProjectLoaded);
  234. }
  235. private void OnProjectLoaded()
  236. {
  237. if (m_ShortcutContext != null)
  238. InternalEditorBridge.RegisterShortcutContext(m_ShortcutContext);
  239. }
  240. private void DoViewGUI()
  241. {
  242. if (spriteEditor.editingDisabled == m_BoneToolbar.enabledSelf)
  243. {
  244. m_BoneToolbar.SetEnabled(!spriteEditor.editingDisabled);
  245. m_MeshToolbar.SetEnabled(!spriteEditor.editingDisabled);
  246. m_WeightToolbar.SetEnabled(!spriteEditor.editingDisabled);
  247. }
  248. if (spriteEditor.editingDisabled == m_LayoutOverlay.rightOverlay.enabledSelf)
  249. {
  250. m_LayoutOverlay.rightOverlay.SetEnabled(!spriteEditor.editingDisabled);
  251. m_LayoutOverlay.rightOverlay.visible = !spriteEditor.editingDisabled;
  252. }
  253. }
  254. private bool isFocused()
  255. {
  256. return spriteEditor != null && (EditorWindow.focusedWindow == spriteEditor as EditorWindow);
  257. }
  258. private void CreateBoneToolbar()
  259. {
  260. m_BoneToolbar = BoneToolbar.GenerateFromUXML();
  261. m_BoneToolbar.Setup(skinningCache);
  262. m_LayoutOverlay.verticalToolbar.AddToContainer(m_BoneToolbar);
  263. m_BoneToolbar.SetSkeletonTool += SetSkeletonTool;
  264. m_BoneToolbar.SetEnabled(!spriteEditor.editingDisabled);
  265. }
  266. private void CreateMeshToolbar()
  267. {
  268. m_MeshToolbar = MeshToolbar.GenerateFromUXML();
  269. m_MeshToolbar.skinningCache = skinningCache;
  270. m_LayoutOverlay.verticalToolbar.AddToContainer(m_MeshToolbar);
  271. m_MeshToolbar.SetMeshTool += SetMeshTool;
  272. m_MeshToolbar.SetEnabled(!spriteEditor.editingDisabled);
  273. }
  274. private void CreateWeightToolbar()
  275. {
  276. m_WeightToolbar = WeightToolbar.GenerateFromUXML();
  277. m_WeightToolbar.skinningCache = skinningCache;
  278. m_LayoutOverlay.verticalToolbar.AddToContainer(m_WeightToolbar);
  279. m_WeightToolbar.SetWeightTool += SetWeightTool;
  280. m_WeightToolbar.SetEnabled(!spriteEditor.editingDisabled);
  281. }
  282. private void SetSkeletonTool(Tools toolType)
  283. {
  284. var tool = skinningCache.GetTool(toolType) as SkeletonToolWrapper;
  285. if (currentTool == tool)
  286. return;
  287. using (skinningCache.UndoScope(TextContent.setTool))
  288. {
  289. ActivateTool(tool);
  290. if (tool.editBindPose)
  291. skinningCache.RestoreBindPose();
  292. }
  293. }
  294. private void SetMeshTool(Tools toolType)
  295. {
  296. var tool = skinningCache.GetTool(toolType);
  297. if (currentTool == tool)
  298. return;
  299. using (skinningCache.UndoScope(TextContent.setTool))
  300. {
  301. ActivateTool(tool);
  302. skinningCache.RestoreBindPose();
  303. UnselectBones();
  304. }
  305. }
  306. private void SetWeightTool(Tools toolType)
  307. {
  308. var tool = skinningCache.GetTool(toolType);
  309. if (currentTool == tool)
  310. return;
  311. using (skinningCache.UndoScope(TextContent.setTool))
  312. {
  313. ActivateTool(tool);
  314. }
  315. }
  316. private void ActivateTool(BaseTool tool)
  317. {
  318. m_ModuleToolGroup.ActivateTool(tool);
  319. UpdateToggleState();
  320. skinningCache.events.toolChanged.Invoke(tool);
  321. }
  322. private void UnselectBones()
  323. {
  324. skinningCache.skeletonSelection.Clear();
  325. skinningCache.events.boneSelectionChanged.Invoke();
  326. }
  327. private void UpdateToggleState()
  328. {
  329. Debug.Assert(m_BoneToolbar != null);
  330. Debug.Assert(m_MeshToolbar != null);
  331. Debug.Assert(m_WeightToolbar != null);
  332. m_BoneToolbar.UpdateToggleState();
  333. m_MeshToolbar.UpdateToggleState();
  334. m_WeightToolbar.UpdateToggleState();
  335. }
  336. private void RemoveMainUI(VisualElement mainView)
  337. {
  338. InternalEditorBridge.RemoveEditorApplicationProjectLoadedCallback(OnProjectLoaded);
  339. InternalEditorBridge.UnregisterShortcutContext(m_ShortcutContext);
  340. }
  341. }
  342. }