No Description
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 10KB

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