Ei kuvausta
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.

GridPaintPaletteWindow.cs 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. using System.Collections.Generic;
  2. using UnityEditor.EditorTools;
  3. using UnityEditor.ShortcutManagement;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. using Event = UnityEngine.Event;
  7. namespace UnityEditor.Tilemaps
  8. {
  9. internal class GridPaintPaletteWindow : EditorWindow
  10. {
  11. private static class Styles
  12. {
  13. public static readonly GUIContent selectPaintTarget = EditorGUIUtility.TrTextContent("Select Paint Target");
  14. public static readonly GUIContent selectPalettePrefab = EditorGUIUtility.TrTextContent("Select Palette Prefab");
  15. public static readonly GUIContent selectTileAsset = EditorGUIUtility.TrTextContent("Select Tile Asset");
  16. public static readonly GUIContent unlockPaletteEditing = EditorGUIUtility.TrTextContent("Unlock Palette Editing");
  17. public static readonly GUIContent lockPaletteEditing = EditorGUIUtility.TrTextContent("Lock Palette Editing");
  18. public static readonly GUIContent verticalBrushSplit = EditorGUIUtility.TrTextContent("Vertical Split for Brush Inspector");
  19. public static readonly GUIContent horizontalBrushSplit = EditorGUIUtility.TrTextContent("Horizontal Split for Brush Inspector");
  20. public static readonly GUIContent openTilePalettePreferences = EditorGUIUtility.TrTextContent("Open Tile Palette Preferences");
  21. public static readonly GUIContent openAsFloatingWindow = EditorGUIUtility.TrTextContent("Open Window as/Floating");
  22. public static readonly GUIContent openAsDockableWindow = EditorGUIUtility.TrTextContent("Open Window as/Dockable");
  23. public static readonly GUIContent tilePalette = EditorGUIUtility.TrTextContent("Tile Palette");
  24. }
  25. private static class UIStyles
  26. {
  27. public static readonly string styleSheetPath = "Packages/com.unity.2d.tilemap/Editor/UI/GridPaintPaletteWindow.uss";
  28. public static readonly string ussClassName = "unity-grid-paint-palette-window";
  29. }
  30. private static readonly string k_TilePaletteVerticalBrushSplitPref = "TilePaletteVerticalBrushSplit";
  31. internal static bool tilePaletteVerticalBrushSplit
  32. {
  33. get
  34. {
  35. return EditorPrefs.GetBool(k_TilePaletteVerticalBrushSplitPref, true);
  36. }
  37. set
  38. {
  39. EditorPrefs.SetBool(k_TilePaletteVerticalBrushSplitPref, value);
  40. }
  41. }
  42. private const float k_ActiveTargetLabelWidth = 90f;
  43. private const float k_ActiveTargetDropdownWidth = 130f;
  44. private const float k_ActiveTargetWarningSize = 20f;
  45. private const float k_MinClipboardHeight = 200f;
  46. private static readonly Vector2 k_MinWindowSize = new Vector2(k_ActiveTargetLabelWidth + k_ActiveTargetDropdownWidth + k_ActiveTargetWarningSize, k_MinClipboardHeight);
  47. [FormerlyPrefKeyAs("Grid Painting/Select", "s")]
  48. [Shortcut("Grid Painting/Select", typeof(TilemapEditorTool.ShortcutContext), KeyCode.S)]
  49. static void GridSelectKey()
  50. {
  51. TilemapEditorTool.ToggleActiveEditorTool(typeof(SelectTool));
  52. }
  53. [FormerlyPrefKeyAs("Grid Painting/Move", "m")]
  54. [Shortcut("Grid Painting/Move", typeof(TilemapEditorTool.ShortcutContext), KeyCode.M)]
  55. static void GridMoveKey()
  56. {
  57. TilemapEditorTool.ToggleActiveEditorTool(typeof(MoveTool));
  58. }
  59. [FormerlyPrefKeyAs("Grid Painting/Brush", "b")]
  60. [Shortcut("Grid Painting/Brush", typeof(TilemapEditorTool.ShortcutContext), KeyCode.B)]
  61. static void GridBrushKey()
  62. {
  63. TilemapEditorTool.ToggleActiveEditorTool(typeof(PaintTool));
  64. }
  65. [FormerlyPrefKeyAs("Grid Painting/Rectangle", "u")]
  66. [Shortcut("Grid Painting/Rectangle", typeof(TilemapEditorTool.ShortcutContext), KeyCode.U)]
  67. static void GridRectangleKey()
  68. {
  69. TilemapEditorTool.ToggleActiveEditorTool(typeof(BoxTool));
  70. }
  71. [FormerlyPrefKeyAs("Grid Painting/Picker", "i")]
  72. [Shortcut("Grid Painting/Picker", typeof(TilemapEditorTool.ShortcutContext), KeyCode.I)]
  73. static void GridPickerKey()
  74. {
  75. TilemapEditorTool.ToggleActiveEditorTool(typeof(PickingTool));
  76. }
  77. [FormerlyPrefKeyAs("Grid Painting/Erase", "d")]
  78. [Shortcut("Grid Painting/Erase", typeof(TilemapEditorTool.ShortcutContext), KeyCode.D)]
  79. static void GridEraseKey()
  80. {
  81. TilemapEditorTool.ToggleActiveEditorTool(typeof(EraseTool));
  82. }
  83. [FormerlyPrefKeyAs("Grid Painting/Fill", "g")]
  84. [Shortcut("Grid Painting/Fill", typeof(TilemapEditorTool.ShortcutContext), KeyCode.G)]
  85. static void GridFillKey()
  86. {
  87. TilemapEditorTool.ToggleActiveEditorTool(typeof(FillTool));
  88. }
  89. static void RotateBrush(GridBrushBase.RotationDirection direction)
  90. {
  91. GridPaintingState.gridBrush.Rotate(direction, GridPaintingState.activeGrid.cellLayout);
  92. GridPaintingState.activeGrid.Repaint();
  93. }
  94. [FormerlyPrefKeyAs("Grid Painting/Rotate Clockwise", "]")]
  95. [Shortcut("Grid Painting/Rotate Clockwise", typeof(TilemapEditorTool.ShortcutContext), KeyCode.RightBracket)]
  96. static void RotateBrushClockwise()
  97. {
  98. if (GridPaintingState.gridBrush != null && GridPaintingState.activeGrid != null)
  99. RotateBrush(GridBrushBase.RotationDirection.Clockwise);
  100. }
  101. [FormerlyPrefKeyAs("Grid Painting/Rotate Anti-Clockwise", "[")]
  102. [Shortcut("Grid Painting/Rotate Anti-Clockwise", typeof(TilemapEditorTool.ShortcutContext), KeyCode.LeftBracket)]
  103. static void RotateBrushAntiClockwise()
  104. {
  105. if (GridPaintingState.gridBrush != null && GridPaintingState.activeGrid != null)
  106. RotateBrush(GridBrushBase.RotationDirection.CounterClockwise);
  107. }
  108. static void FlipBrush(GridBrushBase.FlipAxis axis)
  109. {
  110. GridPaintingState.gridBrush.Flip(axis, GridPaintingState.activeGrid.cellLayout);
  111. GridPaintingState.activeGrid.Repaint();
  112. }
  113. [FormerlyPrefKeyAs("Grid Painting/Flip X", "#[")]
  114. [Shortcut("Grid Painting/Flip X", typeof(TilemapEditorTool.ShortcutContext), KeyCode.LeftBracket, ShortcutModifiers.Shift)]
  115. static void FlipBrushX()
  116. {
  117. if (GridPaintingState.gridBrush != null && GridPaintingState.activeGrid != null)
  118. FlipBrush(GridBrushBase.FlipAxis.X);
  119. }
  120. [FormerlyPrefKeyAs("Grid Painting/Flip Y", "#]")]
  121. [Shortcut("Grid Painting/Flip Y", typeof(TilemapEditorTool.ShortcutContext), KeyCode.RightBracket, ShortcutModifiers.Shift)]
  122. static void FlipBrushY()
  123. {
  124. if (GridPaintingState.gridBrush != null && GridPaintingState.activeGrid != null)
  125. FlipBrush(GridBrushBase.FlipAxis.Y);
  126. }
  127. static void ChangeBrushZ(int change)
  128. {
  129. GridPaintingState.gridBrush.ChangeZPosition(change);
  130. GridPaintingState.activeGrid.ChangeZPosition(change);
  131. GridPaintingState.activeGrid.Repaint();
  132. foreach (var window in instances)
  133. {
  134. window.Repaint();
  135. }
  136. }
  137. [Shortcut("Grid Painting/Increase Z", typeof(TilemapEditorTool.ShortcutContext), KeyCode.Minus)]
  138. static void IncreaseBrushZ()
  139. {
  140. if (GridPaintingState.gridBrush != null
  141. && GridPaintingState.activeGrid != null
  142. && GridPaintingState.activeBrushEditor != null
  143. && GridPaintingState.activeBrushEditor.canChangeZPosition)
  144. ChangeBrushZ(1);
  145. }
  146. [Shortcut("Grid Painting/Decrease Z", typeof(TilemapEditorTool.ShortcutContext), KeyCode.Equals)]
  147. static void DecreaseBrushZ()
  148. {
  149. if (GridPaintingState.gridBrush != null
  150. && GridPaintingState.activeGrid != null
  151. && GridPaintingState.activeBrushEditor != null
  152. && GridPaintingState.activeBrushEditor.canChangeZPosition)
  153. ChangeBrushZ(-1);
  154. }
  155. [Shortcut("Grid Painting/Switch To Next Brush", typeof(TilemapEditorTool.ShortcutContext), KeyCode.B, ShortcutModifiers.Shift)]
  156. static void SwitchToNextBrush()
  157. {
  158. SwitchBrush(1);
  159. }
  160. [Shortcut("Grid Painting/Switch To Previous Brush", typeof(TilemapEditorTool.ShortcutContext), KeyCode.B, ShortcutModifiers.Shift | ShortcutModifiers.Alt)]
  161. static void SwitchToPreviousBrush()
  162. {
  163. SwitchBrush(-1);
  164. }
  165. static void SwitchBrush(int change)
  166. {
  167. var count = GridPaintingState.brushes.Count;
  168. var index = GridPaintingState.brushes.IndexOf(GridPaintingState.gridBrush);
  169. var newIndex = (index + change + count) % count;
  170. if (index != newIndex)
  171. GridPaintingState.gridBrush = GridPaintingState.brushes[newIndex];
  172. }
  173. internal static void PreferencesGUI()
  174. {
  175. using (new SettingsWindow.GUIScope())
  176. {
  177. EditorGUI.BeginChangeCheck();
  178. var val = (TilePaletteActiveTargetsProperties.PrefabEditModeSettings)EditorGUILayout.EnumPopup(TilePaletteActiveTargetsProperties.targetEditModeDialogLabel
  179. , (TilePaletteActiveTargetsProperties.PrefabEditModeSettings)EditorPrefs.GetInt(TilePaletteActiveTargetsProperties.targetEditModeEditorPref
  180. , 0));
  181. if (EditorGUI.EndChangeCheck())
  182. {
  183. EditorPrefs.SetInt(TilePaletteActiveTargetsProperties.targetEditModeEditorPref, (int)val);
  184. }
  185. }
  186. }
  187. private static List<GridPaintPaletteWindow> s_Instances;
  188. public static List<GridPaintPaletteWindow> instances
  189. {
  190. get
  191. {
  192. if (s_Instances == null)
  193. s_Instances = new List<GridPaintPaletteWindow>();
  194. return s_Instances;
  195. }
  196. }
  197. public static bool isActive
  198. {
  199. get
  200. {
  201. return s_Instances != null && s_Instances.Count > 0;
  202. }
  203. }
  204. public GameObject palette
  205. {
  206. get => GridPaintingState.palette;
  207. set => GridPaintingState.palette = value;
  208. }
  209. public GameObject paletteInstance => clipboardView.paletteInstance;
  210. public GridPaintPaletteClipboard clipboardView
  211. {
  212. get => m_ClipboardSplitView.paletteElement.clipboardView;
  213. }
  214. private Vector2 m_BrushScroll;
  215. private bool m_IsUtilityWindow;
  216. private VisualElement m_ToolbarVisualElement;
  217. private VisualElement m_ActiveTargetsVisualElement;
  218. private GridPaintPaletteWindowSplitView m_ClipboardSplitView;
  219. private void CreateGUI()
  220. {
  221. var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(UIStyles.styleSheetPath);
  222. if (styleSheet != null)
  223. {
  224. m_ToolbarVisualElement = new GridPaintingToolbar(this);
  225. m_ActiveTargetsVisualElement = new GridPaintPaletteWindowActiveTargets()
  226. {
  227. name = "activeTargetsDropdown",
  228. };
  229. m_ClipboardSplitView = new GridPaintPaletteWindowSplitView(this, tilePaletteVerticalBrushSplit);
  230. var root = rootVisualElement;
  231. root.Add(m_ToolbarVisualElement);
  232. root.Add(m_ActiveTargetsVisualElement);
  233. root.Add(m_ClipboardSplitView);
  234. root.styleSheetList.Add(styleSheet);
  235. root.AddToClassList(UIStyles.ussClassName);
  236. root.style.minHeight = k_MinClipboardHeight;
  237. root.AddManipulator(new TilePaletteContextMenuHandler(DoContextMenu));
  238. m_ToolbarVisualElement.AddManipulator(new TilePaletteContextMenuHandler(DoContextMenu));
  239. m_ActiveTargetsVisualElement.AddManipulator(new TilePaletteContextMenuHandler(DoContextMenu));
  240. m_ClipboardSplitView.AddManipulator(new TilePaletteDragHandler(DragUpdatedForConvertGridPrefabToPalette, DragPerformedForConvertGridPrefabToPalette));
  241. }
  242. }
  243. private void OnGUI()
  244. {
  245. // Keep repainting until all previews are loaded
  246. if (AssetPreview.IsLoadingAssetPreviews(GetInstanceID()))
  247. Repaint();
  248. // Release keyboard focus on click to empty space
  249. if (Event.current.type == EventType.MouseDown)
  250. GUIUtility.keyboardControl = 0;
  251. }
  252. private void DoContextMenu()
  253. {
  254. GenericMenu pm = new GenericMenu();
  255. if (GridPaintingState.scenePaintTarget != null)
  256. pm.AddItem(Styles.selectPaintTarget, false, SelectPaintTarget);
  257. else
  258. pm.AddDisabledItem(Styles.selectPaintTarget);
  259. if (palette != null)
  260. pm.AddItem(Styles.selectPalettePrefab, false, SelectPaletteAsset);
  261. else
  262. pm.AddDisabledItem(Styles.selectPalettePrefab);
  263. if (clipboardView.activeTile != null)
  264. pm.AddItem(Styles.selectTileAsset, false, SelectTileAsset);
  265. else
  266. pm.AddDisabledItem(Styles.selectTileAsset);
  267. pm.AddSeparator("");
  268. if (clipboardView.unlocked)
  269. pm.AddItem(Styles.lockPaletteEditing, false, FlipLocked);
  270. else
  271. pm.AddItem(Styles.unlockPaletteEditing, false, FlipLocked);
  272. if (tilePaletteVerticalBrushSplit)
  273. pm.AddItem(Styles.horizontalBrushSplit, false, FlipShowToolbarInSceneView);
  274. else
  275. pm.AddItem(Styles.verticalBrushSplit, false, FlipShowToolbarInSceneView);
  276. pm.AddItem(Styles.openTilePalettePreferences, false, OpenTilePalettePreferences);
  277. pm.AddItem(Styles.openAsDockableWindow, !m_IsUtilityWindow, () => OpenWindow(false));
  278. pm.AddItem(Styles.openAsFloatingWindow, m_IsUtilityWindow, () => OpenWindow(true));
  279. pm.ShowAsContext();
  280. }
  281. private void OpenWindow(bool utility)
  282. {
  283. Close();
  284. GridPaintPaletteWindow w = GetWindow<GridPaintPaletteWindow>(utility, Styles.tilePalette.text, true);
  285. w.m_IsUtilityWindow = utility;
  286. }
  287. private void OpenTilePalettePreferences()
  288. {
  289. var settingsWindow = SettingsWindow.Show(SettingsScope.User);
  290. settingsWindow.FilterProviders(TilePaletteActiveTargetsProperties.tilePalettePreferencesLookup);
  291. }
  292. private void FlipLocked()
  293. {
  294. m_ClipboardSplitView.paletteElement.clipboardUnlocked = !m_ClipboardSplitView.paletteElement.clipboardUnlocked;
  295. }
  296. private void FlipShowToolbarInSceneView()
  297. {
  298. var state = !m_ClipboardSplitView.isVerticalOrientation;
  299. tilePaletteVerticalBrushSplit = state;
  300. m_ClipboardSplitView.isVerticalOrientation = state;
  301. SceneView.RepaintAll();
  302. }
  303. private void SelectPaintTarget()
  304. {
  305. Selection.activeObject = GridPaintingState.scenePaintTarget;
  306. }
  307. private void SelectPaletteAsset()
  308. {
  309. Selection.activeObject = palette;
  310. }
  311. private void SelectTileAsset()
  312. {
  313. Selection.activeObject = clipboardView.activeTile;
  314. }
  315. public void OnEnable()
  316. {
  317. instances.Add(this);
  318. GridSelection.gridSelectionChanged += OnGridSelectionChanged;
  319. EditorApplication.projectWasLoaded += OnProjectLoaded;
  320. ToolManager.activeToolChanged += ActiveToolChanged;
  321. wantsMouseMove = true;
  322. wantsMouseEnterLeaveWindow = true;
  323. minSize = k_MinWindowSize;
  324. GridPaintingState.RegisterPainterInterest(this);
  325. }
  326. private void OnProjectLoaded()
  327. {
  328. GridPaintingState.RegisterShortcutContext();
  329. }
  330. private void OnGridSelectionChanged()
  331. {
  332. Repaint();
  333. }
  334. public void OnDisable()
  335. {
  336. GridPaintingState.UnregisterPainterInterest(this);
  337. ToolManager.activeToolChanged -= ActiveToolChanged;
  338. GridSelection.gridSelectionChanged -= OnGridSelectionChanged;
  339. EditorApplication.projectWasLoaded -= OnProjectLoaded;
  340. instances.Remove(this);
  341. }
  342. private void ActiveToolChanged()
  343. {
  344. Repaint();
  345. }
  346. private bool ValidateDragAndDrop()
  347. {
  348. if (DragAndDrop.objectReferences.Length != 1)
  349. return false;
  350. var draggedObject = DragAndDrop.objectReferences[0];
  351. if (!PrefabUtility.IsPartOfRegularPrefab(draggedObject))
  352. return false;
  353. return true;
  354. }
  355. private void DragUpdatedForConvertGridPrefabToPalette()
  356. {
  357. if (!ValidateDragAndDrop())
  358. return;
  359. DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
  360. }
  361. private void DragPerformedForConvertGridPrefabToPalette()
  362. {
  363. if (!ValidateDragAndDrop())
  364. return;
  365. var draggedObject = DragAndDrop.objectReferences[0];
  366. var path = AssetDatabase.GetAssetPath(draggedObject);
  367. var assets = AssetDatabase.LoadAllAssetsAtPath(path);
  368. bool hasNewPaletteAsset = false;
  369. Grid gridPrefab = null;
  370. foreach (var asset in assets)
  371. {
  372. var gridPalette = asset as GridPalette;
  373. hasNewPaletteAsset |= gridPalette != null;
  374. GameObject go = asset as GameObject;
  375. if (go != null)
  376. {
  377. var grid = go.GetComponent<Grid>();
  378. if (grid != null)
  379. gridPrefab = grid;
  380. }
  381. }
  382. if (!hasNewPaletteAsset && gridPrefab != null)
  383. {
  384. var cellLayout = gridPrefab.cellLayout;
  385. var cellSizing = (cellLayout == GridLayout.CellLayout.Rectangle
  386. || cellLayout == GridLayout.CellLayout.Hexagon)
  387. ? GridPalette.CellSizing.Automatic
  388. : GridPalette.CellSizing.Manual;
  389. var newPalette = GridPaletteUtility.CreateGridPalette(cellSizing);
  390. AssetDatabase.AddObjectToAsset(newPalette, path);
  391. AssetDatabase.ForceReserializeAssets(new[] {path});
  392. AssetDatabase.SaveAssets();
  393. Event.current.Use();
  394. GUIUtility.ExitGUI();
  395. }
  396. }
  397. internal void ResetZPosition()
  398. {
  399. GridPaintingState.gridBrush.ResetZPosition();
  400. GridPaintingState.lastActiveGrid.ResetZPosition();
  401. }
  402. [MenuItem("Window/2D/Tile Palette", false, 2)]
  403. public static void OpenTilemapPalette()
  404. {
  405. GridPaintPaletteWindow w = GetWindow<GridPaintPaletteWindow>();
  406. w.titleContent = Styles.tilePalette;
  407. w.m_IsUtilityWindow = false;
  408. }
  409. }
  410. }