PaintableSceneViewGrid.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. using System;
  2. using System.Reflection;
  3. using UnityEditorInternal;
  4. using UnityEngine;
  5. using UnityEngine.Tilemaps;
  6. using Object = UnityEngine.Object;
  7. namespace UnityEditor.Tilemaps
  8. {
  9. internal class PaintableSceneViewGrid : PaintableGrid
  10. {
  11. private Transform gridTransform { get { return grid != null ? grid.transform : null; } }
  12. private Grid grid { get { return brushTarget != null ? brushTarget.GetComponentInParent<Grid>() : (Selection.activeGameObject != null ? Selection.activeGameObject.GetComponentInParent<Grid>() : null); } }
  13. private GridBrushBase gridBrush { get { return GridPaintingState.gridBrush; } }
  14. private SceneView activeSceneView;
  15. private int sceneViewTransformHash;
  16. private GameObject brushTarget
  17. {
  18. get { return GridPaintingState.scenePaintTarget; }
  19. }
  20. public Tilemap tilemap
  21. {
  22. get
  23. {
  24. if (brushTarget != null)
  25. {
  26. return brushTarget.GetComponent<Tilemap>();
  27. }
  28. return null;
  29. }
  30. }
  31. protected override void OnEnable()
  32. {
  33. base.OnEnable();
  34. SceneView.duringSceneGui += OnSceneGUI;
  35. Undo.undoRedoPerformed += UndoRedoPerformed;
  36. GridSelection.gridSelectionChanged += OnGridSelectionChanged;
  37. }
  38. protected override void OnDisable()
  39. {
  40. SceneView.duringSceneGui -= OnSceneGUI;
  41. Undo.undoRedoPerformed -= UndoRedoPerformed;
  42. GridSelection.gridSelectionChanged -= OnGridSelectionChanged;
  43. base.OnDisable();
  44. }
  45. private void OnGridSelectionChanged()
  46. {
  47. SceneView.RepaintAll();
  48. }
  49. private Rect GetSceneViewPositionRect(SceneView sceneView)
  50. {
  51. return new Rect(0, 0, sceneView.position.width, sceneView.position.height);
  52. }
  53. public void OnSceneGUI(SceneView sceneView)
  54. {
  55. HandleMouseEnterLeave(sceneView);
  56. CallOnSceneGUI();
  57. // Case 1093801: Handle only the currently active scene view
  58. if (sceneView != activeSceneView)
  59. return;
  60. // Case 1077400: SceneView camera transform changes may update the mouse grid position even though the mouse position has not changed
  61. var currentSceneViewTransformHash = sceneView.camera.transform.localToWorldMatrix.GetHashCode();
  62. UpdateMouseGridPosition(currentSceneViewTransformHash == sceneViewTransformHash);
  63. sceneViewTransformHash = currentSceneViewTransformHash;
  64. var dot = 1.0f;
  65. var gridView = GetGridView();
  66. if (gridView != null)
  67. {
  68. dot = Math.Abs(Vector3.Dot(sceneView.camera.transform.forward, Grid.Swizzle(gridView.cellSwizzle, gridView.transform.forward)));
  69. }
  70. // Case 1021655: Validate that grid is not totally parallel to view (+-5 degrees), otherwise tiles could be accidentally painted on large positions
  71. if (dot > 0.1f)
  72. {
  73. base.OnGUI();
  74. if (InGridEditMode())
  75. {
  76. if ((grid != null) && (GridPaintingState.activeGrid == this || GridSelection.active))
  77. {
  78. CallOnPaintSceneGUI();
  79. }
  80. if (Event.current.type == EventType.Repaint)
  81. EditorGUIUtility.AddCursorRect(GetSceneViewPositionRect(sceneView), MouseCursor.CustomCursor);
  82. }
  83. }
  84. }
  85. private void HandleMouseEnterLeave(SceneView sceneView)
  86. {
  87. if (inEditMode)
  88. {
  89. if (Event.current.type == EventType.MouseEnterWindow)
  90. {
  91. OnMouseEnter(sceneView);
  92. }
  93. else if (Event.current.type == EventType.MouseLeaveWindow)
  94. {
  95. OnMouseLeave(sceneView);
  96. }
  97. // Case 1043365: When docked, the docking area is considered part of the window and MouseEnter/LeaveWindow events are not considered when entering the docking area
  98. else if (sceneView.docked)
  99. {
  100. var guiPoint = Event.current.mousePosition;
  101. var sceneViewPosition = GetSceneViewPositionRect(sceneView);
  102. if (sceneViewPosition.Contains(guiPoint))
  103. {
  104. if (GridPaintingState.activeGrid != this)
  105. {
  106. OnMouseEnter(sceneView);
  107. }
  108. }
  109. else if (activeSceneView == sceneView)
  110. {
  111. if (GridPaintingState.activeGrid == this)
  112. {
  113. OnMouseLeave(sceneView);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. private void OnMouseEnter(SceneView sceneView)
  120. {
  121. if (GridPaintingState.activeBrushEditor != null)
  122. GridPaintingState.activeBrushEditor.OnMouseEnter();
  123. GridPaintingState.activeGrid = this;
  124. activeSceneView = sceneView;
  125. ResetPreviousMousePositionToCurrentPosition();
  126. }
  127. private void OnMouseLeave(SceneView sceneView)
  128. {
  129. if (GridPaintingState.activeBrushEditor != null)
  130. GridPaintingState.activeBrushEditor.OnMouseLeave();
  131. GridPaintingState.activeGrid = null;
  132. activeSceneView = null;
  133. }
  134. private void UndoRedoPerformed()
  135. {
  136. RefreshAllTiles();
  137. }
  138. private void RefreshAllTiles()
  139. {
  140. if (tilemap != null)
  141. tilemap.RefreshAllTiles();
  142. }
  143. protected override void RegisterUndo()
  144. {
  145. if (GridPaintingState.activeBrushEditor != null)
  146. {
  147. GridPaintingState.activeBrushEditor.RegisterUndo(brushTarget, EditTypeToBrushTool(UnityEditor.EditorTools.ToolManager.activeToolType));
  148. }
  149. }
  150. protected override void Paint(Vector3Int position)
  151. {
  152. if (grid != null)
  153. gridBrush.Paint(grid, brushTarget, position);
  154. }
  155. protected override void Erase(Vector3Int position)
  156. {
  157. if (grid != null)
  158. gridBrush.Erase(grid, brushTarget, position);
  159. }
  160. protected override void BoxFill(BoundsInt position)
  161. {
  162. if (grid != null)
  163. gridBrush.BoxFill(grid, brushTarget, position);
  164. }
  165. protected override void BoxErase(BoundsInt position)
  166. {
  167. if (grid != null)
  168. gridBrush.BoxErase(grid, brushTarget, position);
  169. }
  170. protected override void FloodFill(Vector3Int position)
  171. {
  172. if (grid != null)
  173. gridBrush.FloodFill(grid, brushTarget, position);
  174. }
  175. protected override void PickBrush(BoundsInt position, Vector3Int pickStart)
  176. {
  177. if (grid != null)
  178. gridBrush.Pick(grid, brushTarget, position, pickStart);
  179. }
  180. protected override void Select(BoundsInt position)
  181. {
  182. if (grid != null)
  183. {
  184. GridSelection.Select(brushTarget, position);
  185. gridBrush.Select(grid, brushTarget, position);
  186. }
  187. }
  188. protected override void Move(BoundsInt from, BoundsInt to)
  189. {
  190. if (grid != null)
  191. gridBrush.Move(grid, brushTarget, from, to);
  192. }
  193. protected override void MoveStart(BoundsInt position)
  194. {
  195. if (grid != null)
  196. gridBrush.MoveStart(grid, brushTarget, position);
  197. }
  198. protected override void MoveEnd(BoundsInt position)
  199. {
  200. if (grid != null)
  201. gridBrush.MoveEnd(grid, brushTarget, position);
  202. }
  203. protected override void OnEditStart()
  204. {
  205. if (GridPaintingState.activeBrushEditor != null && grid != null)
  206. GridPaintingState.activeBrushEditor.OnEditStart(grid, brushTarget);
  207. }
  208. protected override void OnEditEnd()
  209. {
  210. if (GridPaintingState.activeBrushEditor != null && grid != null)
  211. GridPaintingState.activeBrushEditor.OnEditEnd(grid, brushTarget);
  212. }
  213. protected override void ClearGridSelection()
  214. {
  215. GridSelection.Clear();
  216. }
  217. public override void Repaint()
  218. {
  219. SceneView.RepaintAll();
  220. }
  221. protected override bool ValidateFloodFillPosition(Vector3Int position)
  222. {
  223. return true;
  224. }
  225. protected override Vector2Int ScreenToGrid(Vector2 screenPosition)
  226. {
  227. if (tilemap != null)
  228. {
  229. var transform = tilemap.transform;
  230. Plane plane = new Plane(GetGridForward(tilemap), transform.position);
  231. Vector3Int cell = LocalToGrid(tilemap, GridEditorUtility.ScreenToLocal(transform, screenPosition, plane));
  232. return new Vector2Int(cell.x, cell.y);
  233. }
  234. if (grid != null)
  235. {
  236. Vector3Int cell = LocalToGrid(grid, GridEditorUtility.ScreenToLocal(gridTransform, screenPosition, GetGridPlane(grid)));
  237. return new Vector2Int(cell.x, cell.y);
  238. }
  239. return Vector2Int.zero;
  240. }
  241. protected override bool PickingIsDefaultTool()
  242. {
  243. return false;
  244. }
  245. protected override bool CanPickOutsideEditMode()
  246. {
  247. return false;
  248. }
  249. protected override GridLayout.CellLayout CellLayout()
  250. {
  251. return grid.cellLayout;
  252. }
  253. Vector3Int LocalToGrid(GridLayout gridLayout, Vector3 local)
  254. {
  255. return gridLayout.LocalToCell(local);
  256. }
  257. private Vector3 GetGridForward(GridLayout gridLayout)
  258. {
  259. switch (gridLayout.cellSwizzle)
  260. {
  261. case GridLayout.CellSwizzle.XYZ:
  262. return gridLayout.transform.forward * -1f;
  263. case GridLayout.CellSwizzle.XZY:
  264. return gridLayout.transform.up * -1f;
  265. case GridLayout.CellSwizzle.YXZ:
  266. return gridLayout.transform.forward;
  267. case GridLayout.CellSwizzle.YZX:
  268. return gridLayout.transform.up;
  269. case GridLayout.CellSwizzle.ZXY:
  270. return gridLayout.transform.right;
  271. case GridLayout.CellSwizzle.ZYX:
  272. return gridLayout.transform.right * -1f;
  273. }
  274. return gridLayout.transform.forward * -1f;
  275. }
  276. private Plane GetGridPlane(Grid grid)
  277. {
  278. return new Plane(GetGridForward(grid), grid.transform.position);
  279. }
  280. private GridLayout GetGridView()
  281. {
  282. if (tilemap != null)
  283. return tilemap;
  284. if (grid != null)
  285. return grid;
  286. return null;
  287. }
  288. void CallOnPaintSceneGUI()
  289. {
  290. bool hasSelection = GridSelection.active && GridSelection.target == brushTarget;
  291. if (!hasSelection && GridPaintingState.activeGrid != this)
  292. return;
  293. RectInt rect = new RectInt(mouseGridPosition, new Vector2Int(1, 1));
  294. if (m_MarqueeStart.HasValue)
  295. rect = GridEditorUtility.GetMarqueeRect(mouseGridPosition, m_MarqueeStart.Value);
  296. else if (hasSelection)
  297. rect = new RectInt(GridSelection.position.xMin, GridSelection.position.yMin, GridSelection.position.size.x, GridSelection.position.size.y);
  298. var layoutGrid = tilemap != null ? tilemap.layoutGrid : grid as GridLayout;
  299. BoundsInt brushBounds = new BoundsInt(new Vector3Int(rect.x, rect.y, zPosition), new Vector3Int(rect.width, rect.height, 1));
  300. if (GridPaintingState.activeBrushEditor != null)
  301. {
  302. GridPaintingState.activeBrushEditor.OnPaintSceneGUI(layoutGrid, brushTarget, brushBounds
  303. , EditTypeToBrushTool(UnityEditor.EditorTools.ToolManager.activeToolType), m_MarqueeStart.HasValue || executing);
  304. }
  305. else // Fallback when user hasn't defined custom editor
  306. {
  307. GridBrushEditorBase.OnPaintSceneGUIInternal(layoutGrid, brushTarget, brushBounds
  308. , EditTypeToBrushTool(UnityEditor.EditorTools.ToolManager.activeToolType), m_MarqueeStart.HasValue || executing);
  309. }
  310. }
  311. void CallOnSceneGUI()
  312. {
  313. var gridLayout = tilemap != null ? tilemap : grid as GridLayout;
  314. bool hasSelection = GridSelection.active && GridSelection.target == brushTarget;
  315. if (GridPaintingState.activeBrushEditor != null)
  316. {
  317. GridPaintingState.activeBrushEditor.OnSceneGUI(gridLayout, brushTarget);
  318. if (hasSelection)
  319. {
  320. GridPaintingState.activeBrushEditor.OnSelectionSceneGUI(gridLayout, brushTarget);
  321. }
  322. }
  323. if (hasSelection)
  324. {
  325. RectInt rect = new RectInt(GridSelection.position.xMin, GridSelection.position.yMin, GridSelection.position.size.x, GridSelection.position.size.y);
  326. BoundsInt brushBounds = new BoundsInt(new Vector3Int(rect.x, rect.y, zPosition), new Vector3Int(rect.width, rect.height, 1));
  327. GridBrushEditorBase.OnSceneGUIInternal(gridLayout, brushTarget, brushBounds
  328. , EditTypeToBrushTool(UnityEditor.EditorTools.ToolManager.activeToolType), m_MarqueeStart.HasValue || executing);
  329. }
  330. }
  331. }
  332. }