GridPaintingState.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6. namespace UnityEditor.Tilemaps
  7. {
  8. /// <summary>
  9. /// GridPaintingState controls the state of objects for painting with a Tile Palette.
  10. /// </summary>
  11. /// <remarks>
  12. /// Utilize this class to get and set the current painting target and brush for painting
  13. /// with the Tile Palette.
  14. /// </remarks>
  15. public class GridPaintingState : ScriptableSingleton<GridPaintingState>
  16. {
  17. [SerializeField] private GameObject m_EditModeScenePaintTarget; // Which GameObject in scene was the last painting target in EditMode
  18. [SerializeField] private GameObject m_ScenePaintTarget; // Which GameObject in scene is considered as painting target
  19. [SerializeField] private GridBrushBase m_Brush; // Which brush will handle painting callbacks
  20. [SerializeField] private PaintableGrid m_ActiveGrid; // Grid that has painting focus (can be palette, too)
  21. [SerializeField] private PaintableGrid m_LastActiveGrid; // Grid that last had painting focus (can be palette, too)
  22. [SerializeField] private HashSet<Object> m_InterestedPainters = new HashSet<Object>(); // A list of objects that can paint using the GridPaintingState
  23. private GameObject[] m_CachedPaintTargets;
  24. private bool m_FlushPaintTargetCache;
  25. private Editor m_CachedEditor;
  26. private bool m_SavingPalette;
  27. /// <summary>
  28. /// Callback when the Tile Palette's active target has changed
  29. /// </summary>
  30. public static event Action<GameObject> scenePaintTargetChanged;
  31. /// <summary>
  32. /// Callback when the Tile Palette's active brush has changed.
  33. /// </summary>
  34. public static event Action<GridBrushBase> brushChanged;
  35. /// <summary>
  36. /// Callback when the Tile Palette's active palette GameObject has changed.
  37. /// </summary>
  38. public static event Action<GameObject> paletteChanged;
  39. private void OnEnable()
  40. {
  41. EditorApplication.hierarchyChanged += HierarchyChanged;
  42. EditorApplication.playModeStateChanged += PlayModeStateChanged;
  43. Selection.selectionChanged += OnSelectionChange;
  44. m_FlushPaintTargetCache = true;
  45. }
  46. private void OnDisable()
  47. {
  48. m_InterestedPainters.Clear();
  49. EditorApplication.hierarchyChanged -= HierarchyChanged;
  50. EditorApplication.playModeStateChanged -= PlayModeStateChanged;
  51. Selection.selectionChanged -= OnSelectionChange;
  52. FlushCache();
  53. }
  54. private void OnSelectionChange()
  55. {
  56. if (hasInterestedPainters && ValidatePaintTarget(Selection.activeGameObject))
  57. {
  58. scenePaintTarget = Selection.activeGameObject;
  59. }
  60. }
  61. private void PlayModeStateChanged(PlayModeStateChange state)
  62. {
  63. if (state == PlayModeStateChange.ExitingEditMode)
  64. {
  65. m_EditModeScenePaintTarget = scenePaintTarget;
  66. }
  67. else if (state == PlayModeStateChange.EnteredEditMode)
  68. {
  69. if (GridPaintActiveTargetsPreferences.restoreEditModeSelection && m_EditModeScenePaintTarget != null)
  70. {
  71. scenePaintTarget = m_EditModeScenePaintTarget;
  72. }
  73. }
  74. }
  75. private void HierarchyChanged()
  76. {
  77. if (hasInterestedPainters)
  78. {
  79. m_FlushPaintTargetCache = true;
  80. if (validTargets == null || validTargets.Length == 0 || !validTargets.Contains(scenePaintTarget))
  81. {
  82. // case 1102618: Try to use current Selection as scene paint target if possible
  83. if (Selection.activeGameObject != null && hasInterestedPainters && ValidatePaintTarget(Selection.activeGameObject))
  84. {
  85. scenePaintTarget = Selection.activeGameObject;
  86. }
  87. else
  88. {
  89. AutoSelectPaintTarget();
  90. }
  91. }
  92. }
  93. }
  94. private GameObject[] GetValidTargets()
  95. {
  96. if (m_FlushPaintTargetCache)
  97. {
  98. m_CachedPaintTargets = null;
  99. if (activeBrushEditor != null)
  100. m_CachedPaintTargets = activeBrushEditor.validTargets;
  101. if (m_CachedPaintTargets == null || m_CachedPaintTargets.Length == 0)
  102. scenePaintTarget = null;
  103. else
  104. {
  105. var comparer = GridPaintActiveTargetsPreferences.GetTargetComparer();
  106. if (comparer != null)
  107. Array.Sort(m_CachedPaintTargets, comparer);
  108. }
  109. m_FlushPaintTargetCache = false;
  110. }
  111. return m_CachedPaintTargets;
  112. }
  113. internal static void AutoSelectPaintTarget()
  114. {
  115. if (activeBrushEditor != null)
  116. {
  117. if (validTargets != null && validTargets.Length > 0)
  118. {
  119. scenePaintTarget = validTargets[0];
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// The currently active target for the Tile Palette
  125. /// </summary>
  126. public static GameObject scenePaintTarget
  127. {
  128. get { return instance.m_ScenePaintTarget; }
  129. set
  130. {
  131. if (value != instance.m_ScenePaintTarget)
  132. {
  133. instance.m_ScenePaintTarget = value;
  134. if (scenePaintTargetChanged != null)
  135. scenePaintTargetChanged(instance.m_ScenePaintTarget);
  136. RepaintGridPaintPaletteWindow();
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// The currently active brush for the Tile Palette
  142. /// </summary>
  143. public static GridBrushBase gridBrush
  144. {
  145. get
  146. {
  147. if (instance.m_Brush == null)
  148. instance.m_Brush = GridPaletteBrushes.instance.GetLastUsedBrush();
  149. return instance.m_Brush;
  150. }
  151. set
  152. {
  153. if (instance.m_Brush != value)
  154. {
  155. instance.m_Brush = value;
  156. instance.m_FlushPaintTargetCache = true;
  157. if (value != null)
  158. GridPaletteBrushes.instance.StoreLastUsedBrush(value);
  159. // Ensure that current scenePaintTarget is still a valid target after a brush change
  160. if (scenePaintTarget != null && !ValidatePaintTarget(scenePaintTarget))
  161. scenePaintTarget = null;
  162. // Use Selection if previous scenePaintTarget was not valid
  163. if (scenePaintTarget == null)
  164. scenePaintTarget = ValidatePaintTarget(Selection.activeGameObject) ? Selection.activeGameObject : null;
  165. // Auto select a valid target if there is still no scenePaintTarget
  166. if (scenePaintTarget == null)
  167. AutoSelectPaintTarget();
  168. if (null != brushChanged)
  169. brushChanged(value);
  170. RepaintGridPaintPaletteWindow();
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// Returns all available brushes for the Tile Palette
  176. /// </summary>
  177. public static IList<GridBrushBase> brushes
  178. {
  179. get { return GridPaletteBrushes.brushes; }
  180. }
  181. internal static GridBrush defaultBrush
  182. {
  183. get { return gridBrush as GridBrush; }
  184. set { gridBrush = value; }
  185. }
  186. /// <summary>
  187. /// The currently active palette GameObject for the Tile Palette
  188. /// </summary>
  189. public static GameObject palette
  190. {
  191. get
  192. {
  193. if (GridPaintPaletteWindow.instances.Count > 0)
  194. return GridPaintPaletteWindow.instances[0].palette;
  195. return null;
  196. }
  197. set
  198. {
  199. if (value == null || !GridPalettes.palettes.Contains(value))
  200. throw new ArgumentException(L10n.Tr("Unable to set invalid palette"));
  201. if (GridPaintPaletteWindow.instances.Count > 0 && GridPaintPaletteWindow.instances[0].palette != value)
  202. {
  203. GridPaintPaletteWindow.instances[0].palette = value;
  204. }
  205. }
  206. }
  207. /// <summary>
  208. /// Checks if target GameObject is part of the active Palette.
  209. /// </summary>
  210. /// <param name="target">GameObject to check.</param>
  211. /// <returns>True if the target GameObject is part of the active palette. False if not.</returns>
  212. public static bool IsPartOfActivePalette(GameObject target)
  213. {
  214. if (GridPaintPaletteWindow.instances.Count > 0 && target == GridPaintPaletteWindow.instances[0].paletteInstance)
  215. return true;
  216. if (target == palette)
  217. return true;
  218. var parent = target.transform.parent;
  219. return parent != null && IsPartOfActivePalette(parent.gameObject);
  220. }
  221. /// <summary>
  222. /// Returns all available Palette GameObjects for the Tile Palette
  223. /// </summary>
  224. public static IList<GameObject> palettes
  225. {
  226. get { return GridPalettes.palettes; }
  227. }
  228. /// <summary>
  229. /// The currently active editor for the active brush for the Tile Palette
  230. /// </summary>
  231. public static GridBrushEditorBase activeBrushEditor
  232. {
  233. get
  234. {
  235. Editor.CreateCachedEditor(gridBrush, null, ref instance.m_CachedEditor);
  236. GridBrushEditorBase baseEditor = instance.m_CachedEditor as GridBrushEditorBase;
  237. return baseEditor;
  238. }
  239. }
  240. internal static Editor fallbackEditor
  241. {
  242. get
  243. {
  244. Editor.CreateCachedEditor(gridBrush, null, ref instance.m_CachedEditor);
  245. return instance.m_CachedEditor;
  246. }
  247. }
  248. internal static PaintableGrid activeGrid
  249. {
  250. get { return instance.m_ActiveGrid; }
  251. set
  252. {
  253. instance.m_ActiveGrid = value;
  254. if (instance.m_ActiveGrid != null)
  255. instance.m_LastActiveGrid = value;
  256. }
  257. }
  258. internal static PaintableGrid lastActiveGrid
  259. {
  260. get { return instance.m_LastActiveGrid; }
  261. }
  262. private static bool ValidatePaintTarget(GameObject candidate)
  263. {
  264. if (candidate == null || candidate.GetComponentInParent<Grid>() == null && candidate.GetComponent<Grid>() == null)
  265. return false;
  266. if (validTargets != null && validTargets.Length > 0 && !validTargets.Contains(candidate))
  267. return false;
  268. return true;
  269. }
  270. internal static void FlushCache()
  271. {
  272. if (instance.m_CachedEditor != null)
  273. {
  274. DestroyImmediate(instance.m_CachedEditor);
  275. instance.m_CachedEditor = null;
  276. }
  277. instance.m_FlushPaintTargetCache = true;
  278. }
  279. /// <summary>
  280. /// A list of all valid targets that can be set as an active target for the Tile Palette
  281. /// </summary>
  282. public static GameObject[] validTargets
  283. {
  284. get { return instance.GetValidTargets(); }
  285. }
  286. internal static bool savingPalette
  287. {
  288. get { return instance.m_SavingPalette; }
  289. set { instance.m_SavingPalette = value; }
  290. }
  291. internal static void OnPaletteChanged(GameObject palette)
  292. {
  293. if (null != paletteChanged)
  294. paletteChanged(palette);
  295. }
  296. internal static void UpdateActiveGridPalette()
  297. {
  298. if (GridPaintPaletteWindow.instances.Count > 0)
  299. GridPaintPaletteWindow.instances[0].DelayedResetPreviewInstance();
  300. }
  301. internal static void RepaintGridPaintPaletteWindow()
  302. {
  303. if (GridPaintPaletteWindow.instances.Count > 0)
  304. GridPaintPaletteWindow.instances[0].Repaint();
  305. }
  306. internal static void UnlockGridPaintPaletteClipboardForEditing()
  307. {
  308. if (GridPaintPaletteWindow.instances.Count > 0)
  309. GridPaintPaletteWindow.instances[0].clipboardView.UnlockAndEdit();
  310. }
  311. internal static void RegisterPainterInterest(Object painter)
  312. {
  313. instance.m_InterestedPainters.Add(painter);
  314. }
  315. internal static void UnregisterPainterInterest(Object painter)
  316. {
  317. instance.m_InterestedPainters.Remove(painter);
  318. }
  319. private bool hasInterestedPainters
  320. {
  321. get { return m_InterestedPainters.Count > 0; }
  322. }
  323. }
  324. }