Ingen beskrivning
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.

PaintableGrid.cs 27KB

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