Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PaintableGrid.cs 26KB

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