Няма описание
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.

PaintableSceneViewGrid.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. UpdateMouseGridPosition(true);
  126. ResetPreviousMousePositionToCurrentPosition();
  127. }
  128. private void OnMouseLeave(SceneView sceneView)
  129. {
  130. if (GridPaintingState.activeBrushEditor != null)
  131. GridPaintingState.activeBrushEditor.OnMouseLeave();
  132. GridPaintingState.activeGrid = null;
  133. activeSceneView = null;
  134. }
  135. private void UndoRedoPerformed()
  136. {
  137. RefreshAllTiles();
  138. }
  139. private void RefreshAllTiles()
  140. {
  141. if (tilemap != null)
  142. tilemap.RefreshAllTiles();
  143. }
  144. protected override void RegisterUndo()
  145. {
  146. if (GridPaintingState.activeBrushEditor != null)
  147. {
  148. GridPaintingState.activeBrushEditor.RegisterUndo(brushTarget, EditTypeToBrushTool(UnityEditor.EditorTools.ToolManager.activeToolType));
  149. }
  150. }
  151. protected override void Paint(Vector3Int position)
  152. {
  153. if (grid != null)
  154. gridBrush.Paint(grid, brushTarget, position);
  155. }
  156. protected override void Erase(Vector3Int position)
  157. {
  158. if (grid != null)
  159. gridBrush.Erase(grid, brushTarget, position);
  160. }
  161. protected override void BoxFill(BoundsInt position)
  162. {
  163. if (grid != null)
  164. gridBrush.BoxFill(grid, brushTarget, position);
  165. }
  166. protected override void BoxErase(BoundsInt position)
  167. {
  168. if (grid != null)
  169. gridBrush.BoxErase(grid, brushTarget, position);
  170. }
  171. protected override void FloodFill(Vector3Int position)
  172. {
  173. if (grid != null)
  174. gridBrush.FloodFill(grid, brushTarget, position);
  175. }
  176. protected override void PickBrush(BoundsInt position, Vector3Int pickStart)
  177. {
  178. if (grid != null)
  179. gridBrush.Pick(grid, brushTarget, position, pickStart);
  180. }
  181. protected override void Select(BoundsInt position)
  182. {
  183. if (grid != null)
  184. {
  185. GridSelection.Select(brushTarget, position);
  186. gridBrush.Select(grid, brushTarget, position);
  187. }
  188. }
  189. protected override void Move(BoundsInt from, BoundsInt to)
  190. {
  191. if (grid != null)
  192. gridBrush.Move(grid, brushTarget, from, to);
  193. }
  194. protected override void MoveStart(BoundsInt position)
  195. {
  196. if (grid != null)
  197. gridBrush.MoveStart(grid, brushTarget, position);
  198. }
  199. protected override void MoveEnd(BoundsInt position)
  200. {
  201. if (grid != null)
  202. gridBrush.MoveEnd(grid, brushTarget, position);
  203. }
  204. protected override bool CustomTool(bool isHotControl, TilemapEditorTool tool, Vector3Int position)
  205. {
  206. var executed = false;
  207. if (grid != null)
  208. {
  209. executed = tool.HandleTool(isHotControl, grid, brushTarget, position);
  210. }
  211. return executed;
  212. }
  213. protected override void OnEditStart()
  214. {
  215. if (GridPaintingState.activeBrushEditor != null && grid != null)
  216. GridPaintingState.activeBrushEditor.OnEditStart(grid, brushTarget);
  217. }
  218. protected override void OnEditEnd()
  219. {
  220. if (GridPaintingState.activeBrushEditor != null && grid != null)
  221. GridPaintingState.activeBrushEditor.OnEditEnd(grid, brushTarget);
  222. }
  223. protected override void ClearGridSelection()
  224. {
  225. GridSelection.Clear();
  226. }
  227. public override bool isActive => grid != null;
  228. public override void Repaint()
  229. {
  230. SceneView.RepaintAll();
  231. }
  232. protected override bool ValidateFloodFillPosition(Vector3Int position)
  233. {
  234. return true;
  235. }
  236. protected override Vector2Int ScreenToGrid(Vector2 screenPosition)
  237. {
  238. if (tilemap != null)
  239. {
  240. var transform = tilemap.transform;
  241. Plane plane = new Plane(GetGridForward(tilemap), transform.position);
  242. Vector3Int cell = LocalToGrid(tilemap, GridEditorUtility.ScreenToLocal(transform, screenPosition, plane));
  243. return new Vector2Int(cell.x, cell.y);
  244. }
  245. if (grid != null)
  246. {
  247. Vector3Int cell = LocalToGrid(grid, GridEditorUtility.ScreenToLocal(gridTransform, screenPosition, GetGridPlane(grid)));
  248. return new Vector2Int(cell.x, cell.y);
  249. }
  250. return Vector2Int.zero;
  251. }
  252. protected override bool PickingIsDefaultTool()
  253. {
  254. return false;
  255. }
  256. protected override bool CanPickOutsideEditMode()
  257. {
  258. return false;
  259. }
  260. protected override GridLayout.CellLayout CellLayout()
  261. {
  262. return grid.cellLayout;
  263. }
  264. Vector3Int LocalToGrid(GridLayout gridLayout, Vector3 local)
  265. {
  266. return gridLayout.LocalToCell(local);
  267. }
  268. private Vector3 GetGridForward(GridLayout gridLayout)
  269. {
  270. switch (gridLayout.cellSwizzle)
  271. {
  272. case GridLayout.CellSwizzle.XYZ:
  273. return gridLayout.transform.forward * -1f;
  274. case GridLayout.CellSwizzle.XZY:
  275. return gridLayout.transform.up * -1f;
  276. case GridLayout.CellSwizzle.YXZ:
  277. return gridLayout.transform.forward;
  278. case GridLayout.CellSwizzle.YZX:
  279. return gridLayout.transform.up;
  280. case GridLayout.CellSwizzle.ZXY:
  281. return gridLayout.transform.right;
  282. case GridLayout.CellSwizzle.ZYX:
  283. return gridLayout.transform.right * -1f;
  284. }
  285. return gridLayout.transform.forward * -1f;
  286. }
  287. private Plane GetGridPlane(Grid grid)
  288. {
  289. return new Plane(GetGridForward(grid), grid.transform.position);
  290. }
  291. private GridLayout GetGridView()
  292. {
  293. if (tilemap != null)
  294. return tilemap;
  295. if (grid != null)
  296. return grid;
  297. return null;
  298. }
  299. void CallOnPaintSceneGUI()
  300. {
  301. bool hasSelection = GridSelection.active && GridSelection.target == brushTarget;
  302. if (!hasSelection && GridPaintingState.activeGrid != this)
  303. return;
  304. RectInt rect = new RectInt(mouseGridPosition, new Vector2Int(1, 1));
  305. if (m_MarqueeStart.HasValue)
  306. rect = GridEditorUtility.GetMarqueeRect(mouseGridPosition, m_MarqueeStart.Value);
  307. else if (hasSelection)
  308. rect = new RectInt(GridSelection.position.xMin, GridSelection.position.yMin, GridSelection.position.size.x, GridSelection.position.size.y);
  309. var layoutGrid = tilemap != null ? tilemap.layoutGrid : grid as GridLayout;
  310. BoundsInt brushBounds = new BoundsInt(new Vector3Int(rect.x, rect.y, zPosition), new Vector3Int(rect.width, rect.height, 1));
  311. if (GridPaintingState.activeBrushEditor != null)
  312. {
  313. GridPaintingState.activeBrushEditor.OnPaintSceneGUI(layoutGrid, brushTarget, brushBounds
  314. , EditTypeToBrushTool(UnityEditor.EditorTools.ToolManager.activeToolType), m_MarqueeStart.HasValue || executing);
  315. }
  316. else // Fallback when user hasn't defined custom editor
  317. {
  318. GridBrushEditorBase.OnPaintSceneGUIInternal(layoutGrid, brushTarget, brushBounds
  319. , EditTypeToBrushTool(UnityEditor.EditorTools.ToolManager.activeToolType), m_MarqueeStart.HasValue || executing);
  320. }
  321. }
  322. void CallOnSceneGUI()
  323. {
  324. var gridLayout = tilemap != null ? tilemap : grid as GridLayout;
  325. bool hasSelection = GridSelection.active && GridSelection.target == brushTarget;
  326. if (GridPaintingState.activeBrushEditor != null)
  327. {
  328. GridPaintingState.activeBrushEditor.OnSceneGUI(gridLayout, brushTarget);
  329. if (hasSelection)
  330. {
  331. GridPaintingState.activeBrushEditor.OnSelectionSceneGUI(gridLayout, brushTarget);
  332. }
  333. }
  334. if (hasSelection)
  335. {
  336. RectInt rect = new RectInt(GridSelection.position.xMin, GridSelection.position.yMin, GridSelection.position.size.x, GridSelection.position.size.y);
  337. BoundsInt brushBounds = new BoundsInt(new Vector3Int(rect.x, rect.y, zPosition), new Vector3Int(rect.width, rect.height, 1));
  338. GridBrushEditorBase.OnSceneGUIInternal(gridLayout, brushTarget, brushBounds
  339. , EditTypeToBrushTool(UnityEditor.EditorTools.ToolManager.activeToolType), m_MarqueeStart.HasValue || executing);
  340. }
  341. }
  342. }
  343. }