暫無描述
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

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