TilemapEditorTool.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Linq;
  3. using UnityEditor.EditorTools;
  4. using UnityEditor.ShortcutManagement;
  5. using UnityEngine;
  6. namespace UnityEditor.Tilemaps
  7. {
  8. /// <summary>
  9. /// A base class for Editor Tools which work with the Tile Palette
  10. /// and GridBrushes
  11. /// </summary>
  12. public abstract class TilemapEditorTool : EditorTool
  13. {
  14. private static EditorTool[] s_TilemapEditorTools = null;
  15. private static float s_TilemapEditorToolsToolbarSize = 0.0f;
  16. /// <summary>
  17. /// All currently active Editor Tools which work with the Tile Palette
  18. /// </summary>
  19. public static EditorTool[] tilemapEditorTools
  20. {
  21. get
  22. {
  23. if (IsCachedEditorToolsInvalid())
  24. InstantiateEditorTools();
  25. return s_TilemapEditorTools;
  26. }
  27. }
  28. /// <summary>
  29. /// The horizontal size of a Toolbar with all the TilemapEditorTools
  30. /// </summary>
  31. public static float tilemapEditorToolsToolbarSize
  32. {
  33. get
  34. {
  35. if (IsCachedEditorToolsInvalid())
  36. InstantiateEditorTools();
  37. return s_TilemapEditorToolsToolbarSize;
  38. }
  39. }
  40. /// <summary>
  41. /// Tooltip String format which accepts a shortcut combination as the parameter
  42. /// </summary>
  43. protected abstract string tooltipStringFormat { get; }
  44. /// <summary>
  45. /// Shortcut Id for this tool
  46. /// </summary>
  47. protected abstract string shortcutId { get; }
  48. /// <summary>
  49. /// Gets the text for the tooltip given a tooltip string format
  50. /// and the shortcut combination for a tooltip
  51. /// </summary>
  52. /// <param name="tooltipStringFormat">String format which accepts a shortcut combination as the parameter</param>
  53. /// <param name="shortcutId">Shortcut Id for this tool</param>
  54. /// <returns>The final text for the tooltip</returns>
  55. protected static string GetTooltipText(string tooltipStringFormat, string shortcutId)
  56. {
  57. return String.Format(tooltipStringFormat, GetKeysFromToolName(shortcutId));
  58. }
  59. /// <summary>
  60. /// Gets the key combination for triggering the shortcut for this tool
  61. /// </summary>
  62. /// <param name="id">The shortcut id for this tool</param>
  63. /// <returns>The key combination for triggering the shortcut for this tool</returns>
  64. protected static string GetKeysFromToolName(string id)
  65. {
  66. return ShortcutIntegration.instance.GetKeyCombinationFor(id);
  67. }
  68. /// <summary>
  69. /// Updates the tooltip whenever there is a change in shortcut combinations
  70. /// </summary>
  71. protected void UpdateTooltip()
  72. {
  73. toolbarIcon.tooltip = GetTooltipText(tooltipStringFormat, shortcutId);
  74. }
  75. /// <summary>
  76. /// Gets whether the tool is available for use
  77. /// </summary>
  78. /// <returns>Whether the tool is available for use</returns>
  79. public override bool IsAvailable()
  80. {
  81. return (GridPaintPaletteWindow.instances.Count > 0) && GridPaintingState.gridBrush;
  82. }
  83. internal static void UpdateTooltips()
  84. {
  85. if (s_TilemapEditorTools == null)
  86. InstantiateEditorTools();
  87. foreach (var editorTool in s_TilemapEditorTools)
  88. {
  89. var tilemapEditorTool = editorTool as TilemapEditorTool;
  90. if (tilemapEditorTool == null)
  91. return;
  92. tilemapEditorTool.UpdateTooltip();
  93. }
  94. }
  95. /// <summary>
  96. /// Toggles the state of active editor tool with the type passed in.
  97. /// </summary>
  98. /// <remarks>
  99. /// This will change the current active editor tool if the type passed in
  100. /// is not the same as the current active editor tool. Otherwise, it will
  101. /// set the View Mode tool as the current active editor tool.
  102. /// </remarks>
  103. /// <param name="type">
  104. /// The type of editor tool. This must be inherited from EditorTool.
  105. /// </param>
  106. public static void ToggleActiveEditorTool(Type type)
  107. {
  108. if (ToolManager.activeToolType != type)
  109. {
  110. SetActiveEditorTool(type);
  111. }
  112. else
  113. {
  114. // Switch out of TilemapEditorTool if possible
  115. var lastTool = EditorToolManager.GetLastTool(x => !(x is TilemapEditorTool));
  116. if (lastTool != null)
  117. ToolManager.SetActiveTool(lastTool);
  118. else
  119. ToolManager.SetActiveTool(typeof(ViewModeTool));
  120. }
  121. }
  122. /// <summary>
  123. /// Sets the current active editor tool to the type passed in
  124. /// </summary>
  125. /// <param name="type">The type of editor tool. This must be inherited from TilemapEditorTool</param>
  126. /// <exception cref="ArgumentException">Throws this if an invalid type parameter is set</exception>
  127. public static void SetActiveEditorTool(Type type)
  128. {
  129. if (type == null || !type.IsSubclassOf(typeof(TilemapEditorTool)))
  130. throw new ArgumentException("The tool to set must be valid and derive from TilemapEditorTool.");
  131. EditorTool selectedTool = null;
  132. foreach (var tool in tilemapEditorTools)
  133. {
  134. if (tool.GetType() == type)
  135. {
  136. selectedTool = tool;
  137. break;
  138. }
  139. }
  140. if (selectedTool != null)
  141. {
  142. ToolManager.SetActiveTool(selectedTool);
  143. }
  144. }
  145. internal static bool IsActive(Type toolType)
  146. {
  147. return ToolManager.activeToolType != null && ToolManager.activeToolType == toolType;
  148. }
  149. private static bool IsCachedEditorToolsInvalid()
  150. {
  151. return s_TilemapEditorTools == null || s_TilemapEditorTools.Length == 0 || s_TilemapEditorTools[0] == null;
  152. }
  153. private static void InstantiateEditorTools()
  154. {
  155. s_TilemapEditorTools = new EditorTool[]
  156. {
  157. CreateInstance<SelectTool>(),
  158. CreateInstance<MoveTool>(),
  159. CreateInstance<PaintTool>(),
  160. CreateInstance<BoxTool>(),
  161. CreateInstance<PickingTool>(),
  162. CreateInstance<EraseTool>(),
  163. CreateInstance<FillTool>()
  164. };
  165. GUIStyle toolbarStyle = "Command";
  166. s_TilemapEditorToolsToolbarSize = s_TilemapEditorTools.Sum(x => toolbarStyle.CalcSize(x.toolbarIcon).x);
  167. }
  168. }
  169. }