暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PaintableGrid.cs 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. using System;
  2. using UnityEditor.EditorTools;
  3. using UnityEngine;
  4. namespace UnityEditor.Tilemaps
  5. {
  6. internal abstract class PaintableGrid : ScriptableObject
  7. {
  8. private const int k_MaxMouseCellDelta = 500;
  9. public enum MarqueeType { None = 0, Pick, Box, Select }
  10. private int m_PermanentControlID;
  11. public abstract void Repaint();
  12. protected abstract void RegisterUndo();
  13. protected abstract void Paint(Vector3Int position);
  14. protected abstract void Erase(Vector3Int position);
  15. protected abstract void BoxFill(BoundsInt position);
  16. protected abstract void BoxErase(BoundsInt position);
  17. protected abstract void FloodFill(Vector3Int position);
  18. protected abstract void PickBrush(BoundsInt position, Vector3Int pickStart);
  19. protected abstract void Select(BoundsInt position);
  20. protected abstract void Move(BoundsInt from, BoundsInt to);
  21. protected abstract void MoveStart(BoundsInt position);
  22. protected abstract void MoveEnd(BoundsInt position);
  23. protected abstract bool CustomTool(bool isToolHotControl, TilemapEditorTool tool, Vector3Int position);
  24. protected abstract bool ValidateFloodFillPosition(Vector3Int position);
  25. protected abstract Vector2Int ScreenToGrid(Vector2 screenPosition);
  26. protected abstract bool PickingIsDefaultTool();
  27. protected abstract bool CanPickOutsideEditMode();
  28. protected abstract GridLayout.CellLayout CellLayout();
  29. protected abstract void ClearGridSelection();
  30. public abstract bool isActive { get; }
  31. protected virtual void OnBrushPickStarted() {}
  32. protected virtual void OnBrushPickDragged(BoundsInt position) {}
  33. protected virtual void OnBrushPickCancelled() {}
  34. protected virtual void OnEditStart() {}
  35. protected virtual void OnEditEnd() {}
  36. // UIToolkit does not set type to Ignore if mouse position is not in window
  37. protected virtual bool IsMouseUpInWindow() { return Event.current.type == EventType.MouseUp; }
  38. internal static PaintableGrid s_LastActivePaintableGrid;
  39. private Vector2 m_MousePosition;
  40. private Vector2Int m_PreviousMouseGridPosition;
  41. private Vector2Int m_MouseGridPosition;
  42. private bool m_MouseGridPositionChanged;
  43. private bool m_PositionChangeRepaintDone;
  44. protected Vector2Int? m_PreviousMove;
  45. protected Vector2Int? m_MarqueeStart;
  46. private MarqueeType m_MarqueeType = MarqueeType.None;
  47. private bool m_IsExecuting;
  48. private Type m_TypeBeforeExecution;
  49. private int m_ZPosition;
  50. public Vector2 mousePosition { get { return m_MousePosition; } }
  51. public Vector2Int mouseGridPosition { get { return m_MouseGridPosition; } }
  52. public bool isPicking { get { return m_MarqueeType == MarqueeType.Pick; } }
  53. public bool isBoxing { get { return m_MarqueeType == MarqueeType.Box; } }
  54. public GridLayout.CellLayout cellLayout { get { return CellLayout(); } }
  55. public int zPosition { get { return m_ZPosition; } set { m_ZPosition = value; } }
  56. protected bool executing
  57. {
  58. get { return m_IsExecuting; }
  59. set
  60. {
  61. var isExecuting = value && isHotControl;
  62. if (isExecuting != m_IsExecuting)
  63. {
  64. if (isExecuting)
  65. OnEditStart();
  66. else
  67. OnEditEnd();
  68. }
  69. m_IsExecuting = isExecuting;
  70. }
  71. }
  72. protected bool isNearestControl { get { return HandleUtility.nearestControl == m_PermanentControlID; } }
  73. protected bool isHotControl { get { return GUIUtility.hotControl == m_PermanentControlID; } }
  74. protected bool mouseGridPositionChanged { get { return m_MouseGridPositionChanged; } }
  75. protected bool inEditMode { get { return InGridEditMode(); } }
  76. protected virtual void OnEnable()
  77. {
  78. m_PermanentControlID = GUIUtility.GetPermanentControlID();
  79. }
  80. protected virtual void OnDisable()
  81. {
  82. }
  83. public virtual void OnGUI()
  84. {
  85. var evt = Event.current;
  86. if (CanPickOutsideEditMode() || inEditMode)
  87. {
  88. if (evt.type == EventType.Layout)
  89. HandleUtility.AddDefaultControl(m_PermanentControlID);
  90. HandleBrushPicking();
  91. }
  92. if (inEditMode)
  93. {
  94. HandleBrushPaintAndErase();
  95. HandleSelectTool();
  96. HandleMoveTool();
  97. HandleEditModeChange();
  98. HandleFloodFill();
  99. HandleBoxTool();
  100. HandleCustomTool();
  101. }
  102. else if (isHotControl && !IsPickingEvent(evt))
  103. {
  104. // Release hot control if it still has it while not in picking or grid edit mode
  105. GUIUtility.hotControl = 0;
  106. }
  107. if (mouseGridPositionChanged && !m_PositionChangeRepaintDone)
  108. {
  109. Repaint();
  110. m_PositionChangeRepaintDone = true;
  111. }
  112. }
  113. protected void ResetPreviousMousePositionToCurrentPosition()
  114. {
  115. m_PreviousMouseGridPosition = m_MouseGridPosition;
  116. }
  117. protected void UpdateMouseGridPosition(bool forceUpdate = false)
  118. {
  119. if (Event.current.type == EventType.MouseDrag
  120. || Event.current.type == EventType.MouseMove
  121. // Case 1075857: Mouse Down when window is not in focus needs to update mouse grid position
  122. || Event.current.type == EventType.MouseDown
  123. || Event.current.type == EventType.DragUpdated
  124. || forceUpdate)
  125. {
  126. m_MousePosition = Event.current.mousePosition;
  127. Vector2Int newGridPosition = ScreenToGrid(m_MousePosition);
  128. if (newGridPosition != m_MouseGridPosition)
  129. {
  130. var delta = newGridPosition - m_MouseGridPosition;
  131. // Case 1024422: Limit mouse cell delta changes for Grid/Tilemap input handling due to camera changes when switching modes/axis views
  132. if (Mathf.Abs(delta.x) > k_MaxMouseCellDelta)
  133. newGridPosition.x = m_MouseGridPosition.x + Math.Sign(delta.x) * k_MaxMouseCellDelta;
  134. if (Mathf.Abs(delta.y) > k_MaxMouseCellDelta)
  135. newGridPosition.y = m_MouseGridPosition.y + Math.Sign(delta.y) * k_MaxMouseCellDelta;
  136. ResetPreviousMousePositionToCurrentPosition();
  137. m_MouseGridPosition = newGridPosition;
  138. MouseGridPositionChanged();
  139. }
  140. else if (!forceUpdate || Event.current.type == EventType.MouseMove)
  141. {
  142. m_MouseGridPositionChanged = false;
  143. }
  144. }
  145. }
  146. private void MouseGridPositionChanged()
  147. {
  148. m_MouseGridPositionChanged = true;
  149. m_PositionChangeRepaintDone = false;
  150. }
  151. private void HandleEditModeChange()
  152. {
  153. // Handles changes in EditMode while tool is expected to be in the same mode
  154. if (isPicking && !TilemapEditorTool.IsActive(typeof(PickingTool)))
  155. {
  156. m_MarqueeStart = null;
  157. m_MarqueeType = MarqueeType.None;
  158. if (isHotControl)
  159. {
  160. GUI.changed = true;
  161. GUIUtility.hotControl = 0;
  162. }
  163. }
  164. if (isBoxing && !TilemapEditorTool.IsActive(typeof(BoxTool)))
  165. {
  166. m_MarqueeStart = null;
  167. m_MarqueeType = MarqueeType.None;
  168. if (isHotControl)
  169. {
  170. GUI.changed = true;
  171. GUIUtility.hotControl = 0;
  172. }
  173. }
  174. if (!TilemapEditorTool.IsActive(typeof(SelectTool))
  175. && !TilemapEditorTool.IsActive(typeof(MoveTool))
  176. && !GridSelectionTool.IsActive())
  177. {
  178. ClearGridSelection();
  179. }
  180. }
  181. private void HandleBrushPicking()
  182. {
  183. Event evt = Event.current;
  184. if (isNearestControl && evt.type == EventType.MouseDown && IsPickingEvent(evt) && !isHotControl)
  185. {
  186. m_TypeBeforeExecution = typeof(PaintTool);
  187. if (inEditMode && !TilemapEditorTool.IsActive(typeof(PickingTool)))
  188. {
  189. m_TypeBeforeExecution = ToolManager.activeToolType;
  190. TilemapEditorTool.SetActiveEditorTool(typeof(PickingTool));
  191. }
  192. m_MarqueeStart = mouseGridPosition;
  193. m_MarqueeType = MarqueeType.Pick;
  194. Event.current.Use();
  195. GUI.changed = true;
  196. GUIUtility.hotControl = m_PermanentControlID;
  197. OnBrushPickStarted();
  198. }
  199. if (evt.type == EventType.MouseDrag && isHotControl && m_MarqueeStart.HasValue && m_MarqueeType == MarqueeType.Pick && IsPickingEvent(evt))
  200. {
  201. RectInt rect = GridEditorUtility.GetMarqueeRect(m_MarqueeStart.Value, mouseGridPosition);
  202. OnBrushPickDragged(new BoundsInt(new Vector3Int(rect.xMin, rect.yMin, zPosition), new Vector3Int(rect.size.x, rect.size.y, 1)));
  203. Event.current.Use();
  204. GUI.changed = true;
  205. }
  206. if (evt.rawType == EventType.MouseUp && isHotControl && m_MarqueeStart.HasValue && m_MarqueeType == MarqueeType.Pick && IsPickingEvent(evt))
  207. {
  208. // Check if event only occurred in the PaintableGrid window as evt.type will filter for this
  209. if (IsMouseUpInWindow() && m_MarqueeType == MarqueeType.Pick)
  210. {
  211. RectInt rect = GridEditorUtility.GetMarqueeRect(m_MarqueeStart.Value, mouseGridPosition);
  212. Vector2Int pivot = GetMarqueePivot(m_MarqueeStart.Value, mouseGridPosition);
  213. PickBrush(new BoundsInt(new Vector3Int(rect.xMin, rect.yMin, zPosition), new Vector3Int(rect.size.x, rect.size.y, 1)), new Vector3Int(pivot.x, pivot.y, 0));
  214. if (inEditMode && ToolManager.activeToolType != m_TypeBeforeExecution)
  215. {
  216. if (PickingIsDefaultTool()
  217. && (m_TypeBeforeExecution == typeof(EraseTool)
  218. || m_TypeBeforeExecution == typeof(MoveTool)))
  219. {
  220. // If Picking is default, change to a Paint Tool to facilitate editing if previous tool does not allow for painting
  221. TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
  222. }
  223. else
  224. {
  225. TilemapEditorTool.SetActiveEditorTool(m_TypeBeforeExecution);
  226. }
  227. }
  228. GridPaletteBrushes.ActiveGridBrushAssetChanged();
  229. Event.current.Use();
  230. GUI.changed = true;
  231. }
  232. else
  233. // Event occurred outside of PaintableGrid window, cancel the pick event
  234. {
  235. OnBrushPickCancelled();
  236. }
  237. m_MarqueeType = MarqueeType.None;
  238. m_MarqueeStart = null;
  239. GUIUtility.hotControl = 0;
  240. InspectorWindow.RepaintAllInspectors();
  241. }
  242. }
  243. private bool IsPickingEvent(Event evt)
  244. {
  245. return ((evt.control && !TilemapEditorTool.IsActive(typeof(MoveTool)))
  246. || TilemapEditorTool.IsActive(typeof(PickingTool))
  247. || !TilemapEditorTool.IsActive(typeof(SelectTool)) && PickingIsDefaultTool())
  248. && evt.button == 0 && !evt.alt;
  249. }
  250. private void HandleSelectTool()
  251. {
  252. Event evt = Event.current;
  253. if (isNearestControl && evt.type == EventType.MouseDown && evt.button == 0 && !evt.alt && (TilemapEditorTool.IsActive(typeof(SelectTool)) || (TilemapEditorTool.IsActive(typeof(MoveTool)) && evt.control)))
  254. {
  255. if (TilemapEditorTool.IsActive(typeof(MoveTool)) && evt.control)
  256. TilemapEditorTool.SetActiveEditorTool(typeof(SelectTool));
  257. m_PreviousMove = null;
  258. m_MarqueeStart = mouseGridPosition;
  259. m_MarqueeType = MarqueeType.Select;
  260. GUIUtility.hotControl = m_PermanentControlID;
  261. Event.current.Use();
  262. }
  263. if (evt.rawType == EventType.MouseUp && evt.button == 0 && !evt.alt && m_MarqueeStart.HasValue && isHotControl && TilemapEditorTool.IsActive(typeof(SelectTool)))
  264. {
  265. // Check if event only occurred in the PaintableGrid window as evt.type will filter for this
  266. if (IsMouseUpInWindow() && m_MarqueeType == MarqueeType.Select)
  267. {
  268. RectInt rect = GridEditorUtility.GetMarqueeRect(m_MarqueeStart.Value, mouseGridPosition);
  269. Select(new BoundsInt(new Vector3Int(rect.xMin, rect.yMin, zPosition), new Vector3Int(rect.size.x, rect.size.y, 1)));
  270. Event.current.Use();
  271. }
  272. if (evt.control)
  273. TilemapEditorTool.SetActiveEditorTool(typeof(MoveTool));
  274. m_MarqueeStart = null;
  275. m_MarqueeType = MarqueeType.None;
  276. InspectorWindow.RepaintAllInspectors();
  277. GUIUtility.hotControl = 0;
  278. }
  279. if (evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Escape && !m_MarqueeStart.HasValue && !m_PreviousMove.HasValue)
  280. {
  281. ClearGridSelection();
  282. Event.current.Use();
  283. }
  284. }
  285. private void HandleMoveTool()
  286. {
  287. Event evt = Event.current;
  288. if (isNearestControl && evt.type == EventType.MouseDown && evt.button == 0 && !evt.alt && TilemapEditorTool.IsActive(typeof(MoveTool)))
  289. {
  290. RegisterUndo();
  291. Vector3Int mouse3D = new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, GridSelection.position.zMin);
  292. if (GridSelection.active && GridSelection.position.Contains(mouse3D))
  293. {
  294. GUIUtility.hotControl = m_PermanentControlID;
  295. executing = true;
  296. m_MarqueeStart = null;
  297. m_MarqueeType = MarqueeType.None;
  298. m_PreviousMove = mouseGridPosition;
  299. MoveStart(GridSelection.position);
  300. }
  301. Event.current.Use();
  302. }
  303. if (evt.type == EventType.MouseDrag && evt.button == 0 && TilemapEditorTool.IsActive(typeof(MoveTool)) && isHotControl)
  304. {
  305. if (m_MouseGridPositionChanged && m_PreviousMove.HasValue)
  306. {
  307. executing = true;
  308. BoundsInt previousRect = GridSelection.position;
  309. BoundsInt previousBounds = new BoundsInt(new Vector3Int(previousRect.xMin, previousRect.yMin, GridSelection.position.zMin), new Vector3Int(previousRect.size.x, previousRect.size.y, 1));
  310. Vector2Int direction = mouseGridPosition - m_PreviousMove.Value;
  311. BoundsInt pos = GridSelection.position;
  312. pos.position = new Vector3Int(pos.x + direction.x, pos.y + direction.y, pos.z);
  313. GridSelection.position = pos;
  314. Move(previousBounds, pos);
  315. m_PreviousMove = mouseGridPosition;
  316. Event.current.Use();
  317. }
  318. }
  319. if (IsMouseUpInWindow() && evt.button == 0 && m_PreviousMove.HasValue && TilemapEditorTool.IsActive(typeof(MoveTool)) && isHotControl)
  320. {
  321. m_PreviousMove = null;
  322. MoveEnd(GridSelection.position);
  323. executing = false;
  324. GUIUtility.hotControl = 0;
  325. Event.current.Use();
  326. }
  327. }
  328. private void HandleBrushPaintAndErase()
  329. {
  330. Event evt = Event.current;
  331. if (!IsPaintingEvent(evt) && !IsErasingEvent(evt))
  332. return;
  333. switch (evt.type)
  334. {
  335. case EventType.MouseDown:
  336. if (isNearestControl)
  337. {
  338. RegisterUndo();
  339. GUIUtility.hotControl = m_PermanentControlID;
  340. executing = true;
  341. if (ToolManager.activeToolType != null && ToolManager.activeToolType.IsSubclassOf(typeof(TilemapEditorTool)))
  342. m_TypeBeforeExecution = ToolManager.activeToolType;
  343. var position = new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, zPosition);
  344. if (IsErasingEvent(evt))
  345. {
  346. if (!TilemapEditorTool.IsActive(typeof(EraseTool)))
  347. TilemapEditorTool.SetActiveEditorTool(typeof(EraseTool));
  348. Erase(position);
  349. }
  350. else
  351. {
  352. if (!TilemapEditorTool.IsActive(typeof(PaintTool)))
  353. TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
  354. Paint(position);
  355. }
  356. ResetPreviousMousePositionToCurrentPosition();
  357. Event.current.Use();
  358. GUI.changed = true;
  359. }
  360. break;
  361. case EventType.MouseDrag:
  362. executing = true;
  363. if (isHotControl && mouseGridPositionChanged)
  364. {
  365. var points = GridEditorUtility.GetPointsOnLine(m_PreviousMouseGridPosition, mouseGridPosition);
  366. if (!evt.shift && !TilemapEditorTool.IsActive(typeof(PaintTool)) && m_TypeBeforeExecution == typeof(PaintTool))
  367. TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
  368. else if (evt.shift && TilemapEditorTool.IsActive(typeof(PaintTool)))
  369. TilemapEditorTool.SetActiveEditorTool(typeof(EraseTool));
  370. foreach (var point in points)
  371. {
  372. var position = new Vector3Int(point.x, point.y, zPosition);
  373. if (IsErasingEvent(evt))
  374. Erase(position);
  375. else
  376. Paint(position);
  377. }
  378. ResetPreviousMousePositionToCurrentPosition();
  379. Event.current.Use();
  380. GUI.changed = true;
  381. }
  382. break;
  383. case EventType.MouseUp:
  384. executing = false;
  385. if (isHotControl)
  386. {
  387. if (!TilemapEditorTool.IsActive(typeof(PaintTool)) && m_TypeBeforeExecution == typeof(PaintTool))
  388. {
  389. TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
  390. }
  391. Event.current.Use();
  392. GUI.changed = true;
  393. GUIUtility.hotControl = 0;
  394. }
  395. break;
  396. }
  397. }
  398. private bool IsPaintingEvent(Event evt)
  399. {
  400. return (evt.button == 0 && !evt.control && !evt.alt && TilemapEditorTool.IsActive(typeof(PaintTool)));
  401. }
  402. private bool IsErasingEvent(Event evt)
  403. {
  404. return (evt.button == 0 && !evt.control && !evt.alt
  405. && ((evt.shift && !TilemapEditorTool.IsActive(typeof(BoxTool))
  406. && !TilemapEditorTool.IsActive(typeof(FillTool))
  407. && !TilemapEditorTool.IsActive(typeof(SelectTool))
  408. && !TilemapEditorTool.IsActive(typeof(MoveTool)))
  409. || TilemapEditorTool.IsActive(typeof(EraseTool))));
  410. }
  411. private void HandleFloodFill()
  412. {
  413. if (TilemapEditorTool.IsActive(typeof(FillTool)) && GridPaintingState.gridBrush != null && ValidateFloodFillPosition(new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, 0)))
  414. {
  415. Event evt = Event.current;
  416. if (isNearestControl && evt.type == EventType.MouseDown && evt.button == 0 && !evt.alt)
  417. {
  418. GUIUtility.hotControl = m_PermanentControlID;
  419. GUI.changed = true;
  420. executing = true;
  421. Event.current.Use();
  422. }
  423. if (IsMouseUpInWindow() && evt.button == 0 && isHotControl)
  424. {
  425. RegisterUndo();
  426. FloodFill(new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, zPosition));
  427. executing = false;
  428. GUI.changed = true;
  429. Event.current.Use();
  430. GUIUtility.hotControl = 0;
  431. }
  432. }
  433. }
  434. private void HandleBoxTool()
  435. {
  436. Event evt = Event.current;
  437. if (isNearestControl && evt.type == EventType.MouseDown && evt.button == 0 && !evt.alt && TilemapEditorTool.IsActive(typeof(BoxTool)))
  438. {
  439. m_MarqueeStart = mouseGridPosition;
  440. m_MarqueeType = MarqueeType.Box;
  441. Event.current.Use();
  442. GUI.changed = true;
  443. executing = true;
  444. GUIUtility.hotControl = m_PermanentControlID;
  445. }
  446. if (evt.type == EventType.MouseDrag && evt.button == 0 && TilemapEditorTool.IsActive(typeof(BoxTool)))
  447. {
  448. if (isHotControl && m_MarqueeStart.HasValue)
  449. {
  450. Event.current.Use();
  451. executing = true;
  452. GUI.changed = true;
  453. }
  454. }
  455. if (IsMouseUpInWindow() && evt.button == 0 && TilemapEditorTool.IsActive(typeof(BoxTool)))
  456. {
  457. if (isHotControl && m_MarqueeStart.HasValue)
  458. {
  459. RegisterUndo();
  460. RectInt rect = GridEditorUtility.GetMarqueeRect(m_MarqueeStart.Value, mouseGridPosition);
  461. if (evt.shift)
  462. BoxErase(new BoundsInt(rect.x, rect.y, zPosition, rect.size.x, rect.size.y, 1));
  463. else
  464. BoxFill(new BoundsInt(rect.x, rect.y, zPosition, rect.size.x, rect.size.y, 1));
  465. Event.current.Use();
  466. executing = false;
  467. GUI.changed = true;
  468. GUIUtility.hotControl = 0;
  469. }
  470. m_MarqueeStart = null;
  471. m_MarqueeType = MarqueeType.None;
  472. }
  473. }
  474. private void HandleCustomTool()
  475. {
  476. Event evt = Event.current;
  477. if (evt.type == EventType.Layout || evt.type == EventType.Repaint)
  478. return;
  479. if (!TilemapEditorTool.IsCustomTilemapEditorToolActive())
  480. return;
  481. TilemapEditorTool activeTool = EditorToolManager.activeTool as TilemapEditorTool;
  482. var executed = CustomTool(isHotControl, activeTool, new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, zPosition));
  483. if (executed != executing)
  484. {
  485. GUIUtility.hotControl = executed ? m_PermanentControlID : 0;
  486. executing = executed;
  487. GUI.changed = true;
  488. Event.current.Use();
  489. }
  490. else if (executing)
  491. {
  492. GUI.changed = true;
  493. Event.current.Use();
  494. }
  495. }
  496. private Vector2Int GetMarqueePivot(Vector2Int start, Vector2Int end)
  497. {
  498. Vector2Int pivot = new Vector2Int(
  499. Math.Max(end.x - start.x, 0),
  500. Math.Max(end.y - start.y, 0)
  501. );
  502. return pivot;
  503. }
  504. public void ChangeZPosition(int change)
  505. {
  506. m_ZPosition += change;
  507. MouseGridPositionChanged();
  508. Repaint();
  509. }
  510. public void ResetZPosition()
  511. {
  512. if (m_ZPosition == 0)
  513. return;
  514. m_ZPosition = 0;
  515. MouseGridPositionChanged();
  516. Repaint();
  517. }
  518. public static bool InGridEditMode()
  519. {
  520. return ToolManager.activeToolType != null
  521. && (ToolManager.activeToolType.IsSubclassOf(typeof(TilemapEditorTool)));
  522. }
  523. // TODO: Someday EditMode or its future incarnation will be public and we can get rid of this
  524. // TODO: Temporarily use ActiveTool's type to determine brush tool
  525. public static GridBrushBase.Tool EditTypeToBrushTool(Type activeToolType)
  526. {
  527. if (activeToolType == typeof(BoxTool))
  528. return GridBrushBase.Tool.Box;
  529. if (activeToolType == typeof(EraseTool))
  530. return GridBrushBase.Tool.Erase;
  531. if (activeToolType == typeof(FillTool))
  532. return GridBrushBase.Tool.FloodFill;
  533. if (activeToolType == typeof(PaintTool))
  534. return GridBrushBase.Tool.Paint;
  535. if (activeToolType == typeof(PickingTool))
  536. return GridBrushBase.Tool.Pick;
  537. if (activeToolType == typeof(SelectTool))
  538. return GridBrushBase.Tool.Select;
  539. if (activeToolType == typeof(MoveTool))
  540. return GridBrushBase.Tool.Move;
  541. return GridBrushBase.Tool.Other;
  542. }
  543. }
  544. }