HorizontalToggleTools.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. internal class HorizontalToggleTools
  6. {
  7. private static class Styles
  8. {
  9. public static GUIContent visibilityCollapseIcon = new GUIContent(IconUtility.LoadIconResource("Visibility_Tool", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr(TextContent.visibilityIconTooltip));
  10. public static GUIContent visibilityIcon = new GUIContent(L10n.Tr(TextContent.visibilityIconText), IconUtility.LoadIconResource("Visibility_Tool", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr(TextContent.visibilityIconTooltip));
  11. public static GUIContent characterCollapseIcon = new GUIContent(IconUtility.LoadIconResource("character_Mode", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr(TextContent.restorePose));
  12. public static GUIContent characterIcon = new GUIContent(L10n.Tr(TextContent.characterIconText), IconUtility.LoadIconResource("character_Mode", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr(TextContent.restorePose));
  13. public static GUIContent spriteSheetIcon = new GUIContent(L10n.Tr(TextContent.spriteSheetIconText), IconUtility.LoadIconResource("Sprite_Mode", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr(TextContent.spriteSheetIconTooltip));
  14. public static GUIContent spriteSheetCollapseIcon = new GUIContent(IconUtility.LoadIconResource("Sprite_Mode", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr(TextContent.spriteSheetIconTooltip));
  15. public static GUIContent copyIcon = new GUIContent(L10n.Tr(TextContent.copyText), IconUtility.LoadIconResource("Copy", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr(TextContent.copyTooltip));
  16. public static GUIContent copyCollapseIcon = new GUIContent(IconUtility.LoadIconResource("Copy", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr(TextContent.copyTooltip));
  17. public static GUIContent pasteIcon = new GUIContent(L10n.Tr(TextContent.pasteText), IconUtility.LoadIconResource("Paste", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr(TextContent.pasteTooltip));
  18. public static GUIContent pasteCollapseIcon = new GUIContent(IconUtility.LoadIconResource("Paste", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr(TextContent.pasteTooltip));
  19. }
  20. private SkinningCache skinningCache { get; set; }
  21. private CopyTool copyTool
  22. {
  23. get { return skinningCache.GetTool(Tools.CopyPaste) as CopyTool; }
  24. }
  25. private VisibilityTool visibilityTool
  26. {
  27. get { return skinningCache.GetTool(Tools.Visibility) as VisibilityTool; }
  28. }
  29. private SwitchModeTool switchmodeTool
  30. {
  31. get { return skinningCache.GetTool(Tools.SwitchMode) as SwitchModeTool; }
  32. }
  33. private GUIContent spriteSheetIcon
  34. {
  35. get { return collapseToolbar ? Styles.spriteSheetCollapseIcon : Styles.spriteSheetIcon; }
  36. }
  37. private GUIContent copyIcon
  38. {
  39. get { return collapseToolbar ? Styles.copyCollapseIcon : Styles.copyIcon; }
  40. }
  41. private GUIContent pasteIcon
  42. {
  43. get { return collapseToolbar ? Styles.pasteCollapseIcon : Styles.pasteIcon; }
  44. }
  45. internal Action<BaseTool> onActivateTool = (b) => {};
  46. private BaseTool m_PreviousTool;
  47. public bool collapseToolbar { get; set; }
  48. internal HorizontalToggleTools(SkinningCache s)
  49. {
  50. skinningCache = s;
  51. }
  52. internal void DoGUI(Rect drawArea, BaseTool currentTool, bool isDisabled)
  53. {
  54. using (new EditorGUI.DisabledScope(isDisabled))
  55. {
  56. GUILayout.BeginArea(drawArea);
  57. EditorGUILayout.BeginHorizontal();
  58. DoPreviewToggle();
  59. DoModeToggle();
  60. DoCopyToggle(currentTool);
  61. GUILayout.FlexibleSpace();
  62. DoVisibilityToggle(currentTool);
  63. EditorGUILayout.EndHorizontal();
  64. GUILayout.EndArea();
  65. }
  66. }
  67. private void StorePreviousTool(BaseTool currentTool)
  68. {
  69. if(currentTool != copyTool && currentTool != visibilityTool)
  70. m_PreviousTool = currentTool;
  71. }
  72. private void DoModeToggle()
  73. {
  74. if (skinningCache.hasCharacter)
  75. {
  76. EditorGUI.BeginChangeCheck();
  77. var isActive = GUILayout.Toggle(switchmodeTool.isActive , spriteSheetIcon, EditorStyles.toolbarButton);
  78. if (EditorGUI.EndChangeCheck())
  79. {
  80. using (skinningCache.UndoScope(TextContent.setMode))
  81. {
  82. if (isActive)
  83. switchmodeTool.Activate();
  84. else
  85. switchmodeTool.Deactivate();
  86. }
  87. }
  88. }
  89. }
  90. private void DoCopyToggle(BaseTool currentTool)
  91. {
  92. if (GUILayout.Button(copyIcon, EditorStyles.toolbarButton))
  93. {
  94. copyTool.OnCopyActivated();
  95. }
  96. EditorGUI.BeginChangeCheck();
  97. GUILayout.Toggle(copyTool.isActive, pasteIcon, EditorStyles.toolbarButton);
  98. if (EditorGUI.EndChangeCheck())
  99. TogglePasteTool(currentTool);
  100. }
  101. internal void TogglePasteTool(BaseTool currentTool)
  102. {
  103. if (!copyTool.isActive)
  104. {
  105. onActivateTool(copyTool);
  106. StorePreviousTool(currentTool);
  107. }
  108. else if (m_PreviousTool != null)
  109. {
  110. onActivateTool(m_PreviousTool);
  111. }
  112. }
  113. void DoVisibilityToggle(BaseTool currentTool)
  114. {
  115. EditorGUI.BeginChangeCheck();
  116. GUILayout.Toggle(visibilityTool.isActive, visbilityIcon, EditorStyles.toolbarButton);
  117. if (EditorGUI.EndChangeCheck())
  118. ToggleVisibilityTool(currentTool);
  119. }
  120. GUIContent visbilityIcon { get { return collapseToolbar ? Styles.visibilityCollapseIcon : Styles.visibilityIcon; } }
  121. internal void ToggleVisibilityTool(BaseTool currentTool)
  122. {
  123. onActivateTool(visibilityTool);
  124. }
  125. private void DoPreviewToggle()
  126. {
  127. var skeleton = skinningCache.GetEffectiveSkeleton(skinningCache.selectedSprite);
  128. EditorGUI.BeginDisabledGroup(skeleton == null || skeleton.isPosePreview == false);
  129. EditorGUI.BeginChangeCheck();
  130. GUILayout.Button(characterIcon, EditorStyles.toolbarButton);
  131. if (EditorGUI.EndChangeCheck())
  132. {
  133. using (skinningCache.UndoScope("Restore Pose"))
  134. {
  135. skinningCache.RestoreBindPose();
  136. skinningCache.events.restoreBindPose.Invoke();
  137. }
  138. }
  139. EditorGUI.EndDisabledGroup();
  140. }
  141. GUIContent characterIcon { get { return collapseToolbar ? Styles.characterCollapseIcon : Styles.characterIcon; } }
  142. }
  143. }