暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TilemapEditorTool.cs 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  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. /// <summary>
  15. /// Context to determine if TilemapEditorTools can be triggered through shortcuts
  16. /// </summary>
  17. public class ShortcutContext : IShortcutToolContext
  18. {
  19. public bool active { get; set; }
  20. }
  21. private static Dictionary<Type, EditorTool> s_TilemapEditorToolsMap;
  22. private static EditorTool[] s_DefaultTilemapEditorTools;
  23. /// <summary>
  24. /// Types for all the Default Editor Tools
  25. /// </summary>
  26. internal static Type[] s_DefaultToolTypes = new[]
  27. {
  28. typeof(SelectTool),
  29. typeof(MoveTool),
  30. typeof(PaintTool),
  31. typeof(BoxTool),
  32. typeof(PickingTool),
  33. typeof(EraseTool),
  34. typeof(FillTool)
  35. };
  36. /// <summary>
  37. /// All currently active Editor Tools which work with the Tile Palette
  38. /// </summary>
  39. public static EditorTool[] tilemapEditorTools
  40. {
  41. get
  42. {
  43. if (IsCachedEditorToolsInvalid())
  44. InstantiateEditorTools();
  45. return GridPaintingState.activeBrushTools;
  46. }
  47. }
  48. /// <summary>
  49. /// The horizontal size of a Toolbar with all the TilemapEditorTools
  50. /// </summary>
  51. public static float tilemapEditorToolsToolbarSize
  52. {
  53. get
  54. {
  55. if (IsCachedEditorToolsInvalid())
  56. InstantiateEditorTools();
  57. return GridPaintingState.activeBrushToolbarSize;
  58. }
  59. }
  60. /// <summary>
  61. /// Tooltip String format which accepts a shortcut combination as the parameter
  62. /// </summary>
  63. protected abstract string tooltipStringFormat { get; }
  64. /// <summary>
  65. /// Shortcut Id for this tool
  66. /// </summary>
  67. protected abstract string shortcutId { get; }
  68. /// <summary>
  69. /// Gets the text for the tooltip given a tooltip string format
  70. /// and the shortcut combination for a tooltip
  71. /// </summary>
  72. /// <param name="tooltipStringFormat">String format which accepts a shortcut combination as the parameter</param>
  73. /// <param name="shortcutId">Shortcut Id for this tool</param>
  74. /// <returns>The final text for the tooltip</returns>
  75. protected static string GetTooltipText(string tooltipStringFormat, string shortcutId)
  76. {
  77. return String.Format(tooltipStringFormat, GetKeysFromToolName(shortcutId));
  78. }
  79. /// <summary>
  80. /// Gets the key combination for triggering the shortcut for this tool
  81. /// </summary>
  82. /// <param name="id">The shortcut id for this tool</param>
  83. /// <returns>The key combination for triggering the shortcut for this tool</returns>
  84. protected static string GetKeysFromToolName(string id)
  85. {
  86. return ShortcutIntegration.instance.GetKeyCombinationFor(id);
  87. }
  88. /// <summary>
  89. /// Updates the tooltip whenever there is a change in shortcut combinations
  90. /// </summary>
  91. protected void UpdateTooltip()
  92. {
  93. toolbarIcon.tooltip = GetTooltipText(tooltipStringFormat, shortcutId);
  94. }
  95. /// <summary>
  96. /// Method called when the Tool is being used.
  97. /// Override this to have custom behaviour when the Tool is used.
  98. /// </summary>
  99. /// <param name="isHotControl">Whether the tool is the hot control</param>
  100. /// <param name="gridLayout">GridLayout the tool is being used on</param>
  101. /// <param name="brushTarget">Target GameObject the tool is being used on</param>
  102. /// <param name="gridMousePosition">Grid Cell position of the Mouse on the GridLayout</param>
  103. /// <returns>Whether the tool has been used and modified the brushTarget</returns>
  104. public virtual bool HandleTool(bool isHotControl, GridLayout gridLayout, GameObject brushTarget, Vector3Int gridMousePosition)
  105. {
  106. return false;
  107. }
  108. /// <summary>
  109. /// Gets whether the tool is available for use
  110. /// </summary>
  111. /// <returns>Whether the tool is available for use</returns>
  112. public override bool IsAvailable()
  113. {
  114. return (GridPaintPaletteWindow.instances.Count > 0) && GridPaintingState.gridBrush;
  115. }
  116. internal static void UpdateTooltips()
  117. {
  118. if (IsCachedEditorToolsInvalid())
  119. InstantiateEditorTools();
  120. foreach (var editorTool in GridPaintingState.activeBrushTools)
  121. {
  122. var tilemapEditorTool = editorTool as TilemapEditorTool;
  123. if (tilemapEditorTool == null)
  124. return;
  125. tilemapEditorTool.UpdateTooltip();
  126. }
  127. }
  128. /// <summary>
  129. /// Toggles the state of active editor tool with the type passed in.
  130. /// </summary>
  131. /// <remarks>
  132. /// This will change the current active editor tool if the type passed in
  133. /// is not the same as the current active editor tool. Otherwise, it will
  134. /// set the View Mode tool as the current active editor tool.
  135. /// </remarks>
  136. /// <param name="type">
  137. /// The type of editor tool. This must be inherited from EditorTool.
  138. /// </param>
  139. public static void ToggleActiveEditorTool(Type type)
  140. {
  141. if (ToolManager.activeToolType != type)
  142. {
  143. SetActiveEditorTool(type);
  144. }
  145. else
  146. {
  147. ToolManager.RestorePreviousPersistentTool();
  148. }
  149. }
  150. /// <summary>
  151. /// Sets the current active editor tool to the type passed in
  152. /// </summary>
  153. /// <param name="type">The type of editor tool. This must be inherited from TilemapEditorTool</param>
  154. /// <exception cref="ArgumentException">Throws this if an invalid type parameter is set</exception>
  155. public static void SetActiveEditorTool(Type type)
  156. {
  157. if (type == null || !type.IsSubclassOf(typeof(TilemapEditorTool)))
  158. throw new ArgumentException("The tool to set must be valid and derive from TilemapEditorTool.");
  159. EditorTool selectedTool = null;
  160. foreach (var tool in tilemapEditorTools)
  161. {
  162. if (tool.GetType() == type)
  163. {
  164. selectedTool = tool;
  165. break;
  166. }
  167. }
  168. if (selectedTool != null)
  169. {
  170. ToolManager.SetActiveTool(selectedTool);
  171. }
  172. }
  173. internal static bool IsActive(Type toolType)
  174. {
  175. return ToolManager.activeToolType != null && ToolManager.activeToolType == toolType;
  176. }
  177. private static bool IsCachedEditorToolsInvalid()
  178. {
  179. return s_TilemapEditorToolsMap == null
  180. || s_DefaultTilemapEditorTools == null
  181. || s_DefaultTilemapEditorTools.Length == 0
  182. || s_DefaultTilemapEditorTools[0] == null;
  183. }
  184. private static void InstantiateEditorTools()
  185. {
  186. s_DefaultTilemapEditorTools = TilemapEditorToolPreferences.CreateDefaultTilePaletteEditorTools();
  187. s_TilemapEditorToolsMap = new Dictionary<Type, EditorTool>(s_DefaultTilemapEditorTools.Length);
  188. foreach (var editorTool in s_DefaultTilemapEditorTools)
  189. {
  190. s_TilemapEditorToolsMap.Add(editorTool.GetType(), editorTool);
  191. }
  192. GridPaintingState.UpdateBrushToolbar();
  193. }
  194. internal static void UpdateEditorTools(BrushToolsAttribute brushToolsAttribute)
  195. {
  196. if (IsCachedEditorToolsInvalid())
  197. InstantiateEditorTools();
  198. EditorTool[] editorTools;
  199. if (brushToolsAttribute?.toolList == null || brushToolsAttribute.toolList.Count == 0)
  200. {
  201. editorTools = s_DefaultTilemapEditorTools;
  202. }
  203. else
  204. {
  205. editorTools = new EditorTool[brushToolsAttribute.toolList.Count];
  206. for (int i = 0; i < brushToolsAttribute.toolList.Count; ++i)
  207. {
  208. var toolType = brushToolsAttribute.toolList[i];
  209. if (!s_TilemapEditorToolsMap.TryGetValue(toolType, out EditorTool editorTool))
  210. {
  211. editorTool = (EditorTool)ScriptableObject.CreateInstance(toolType);
  212. s_TilemapEditorToolsMap.Add(toolType, editorTool);
  213. }
  214. editorTools[i] = editorTool;
  215. }
  216. }
  217. GridPaintingState.SetBrushTools(editorTools);
  218. }
  219. internal static bool IsCustomTilemapEditorToolActive()
  220. {
  221. if (EditorToolManager.activeTool == null
  222. || !(EditorToolManager.activeTool is TilemapEditorTool))
  223. return false;
  224. if (s_DefaultTilemapEditorTools == null)
  225. return false;
  226. var activeToolType = EditorToolManager.activeTool.GetType();
  227. foreach (var toolType in s_DefaultToolTypes)
  228. {
  229. if (toolType == activeToolType)
  230. return false;
  231. }
  232. return true;
  233. }
  234. }
  235. }