TilePoolPreloaderInspector.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using DunGen.Graph;
  2. using DunGen.Pooling;
  3. using UnityEditor;
  4. using UnityEditorInternal;
  5. using UnityEngine;
  6. namespace DunGen.Editor.Inspectors
  7. {
  8. [CustomEditor(typeof(TilePoolPreloader))]
  9. [CanEditMultipleObjects]
  10. public class TilePoolPreloaderInspector : UnityEditor.Editor
  11. {
  12. #region Constants
  13. public static class Label
  14. {
  15. public static readonly GUIContent ListHeader = new GUIContent("Tile Pool Entries", "How many of each tile prefab we want to pre-load into the pool");
  16. public static readonly GUIContent NoneElement = new GUIContent("Drag a Tile, TileSet, Archetype, or DungeonFlow here");
  17. public static readonly GUIContent ClearList = new GUIContent("Clear List", "Clears all entries from the list");
  18. public static readonly GUIContent SetAllCounts = new GUIContent("Set All Counts", "Sets the count value of every tile prefab in the list to the specified value");
  19. public static readonly GUIContent ApplyButton = new GUIContent("Apply");
  20. public static readonly GUIContent ListControls = new GUIContent("List Controls");
  21. public static readonly GUIContent ClearInstancesButton = new GUIContent("Clear Instances");
  22. public static readonly GUIContent RefreshInstancesButton = new GUIContent("Refresh Instances");
  23. }
  24. #endregion
  25. private static int setAllCountValue = 1;
  26. private static bool showListControls = false;
  27. private SerializedProperty entries;
  28. private ReorderableList reorderableList;
  29. private void OnEnable()
  30. {
  31. entries = serializedObject.FindProperty(nameof(TilePoolPreloader.Entries));
  32. reorderableList = new ReorderableList(serializedObject, entries, true, true, true, true);
  33. reorderableList.drawHeaderCallback = rect =>
  34. {
  35. EditorGUI.LabelField(rect, Label.ListHeader);
  36. };
  37. reorderableList.onAddCallback = list =>
  38. {
  39. var newElement = entries.GetArrayElementAtIndex(entries.arraySize++);
  40. var prefabProperty = newElement.FindPropertyRelative(nameof(TilePoolPreloaderEntry.TilePrefab));
  41. var countProperty = newElement.FindPropertyRelative(nameof(TilePoolPreloaderEntry.Count));
  42. prefabProperty.objectReferenceValue = null;
  43. countProperty.intValue = setAllCountValue;
  44. };
  45. reorderableList.drawElementCallback = (rect, index, isActive, isFocused) =>
  46. {
  47. var element = entries.GetArrayElementAtIndex(index);
  48. rect.y += 2;
  49. float countWidth = 100;
  50. float prefabWidth = rect.width - countWidth;
  51. // Draw tile prefab field
  52. EditorGUI.PropertyField(
  53. new Rect(rect.x, rect.y, prefabWidth, EditorGUIUtility.singleLineHeight),
  54. element.FindPropertyRelative(nameof(TilePoolPreloaderEntry.TilePrefab)),
  55. GUIContent.none);
  56. // Draw count field
  57. var countProperty = element.FindPropertyRelative(nameof(TilePoolPreloaderEntry.Count));
  58. countProperty.intValue = Mathf.Max(1, EditorGUI.IntField(
  59. new Rect(rect.x + prefabWidth, rect.y, countWidth, EditorGUIUtility.singleLineHeight),
  60. countProperty.intValue));
  61. };
  62. reorderableList.drawNoneElementCallback = rect =>
  63. {
  64. EditorGUI.LabelField(rect, Label.NoneElement, EditorStyles.centeredGreyMiniLabel);
  65. };
  66. }
  67. public override void OnInspectorGUI()
  68. {
  69. EditorGUI.BeginDisabledGroup(EditorApplication.isPlayingOrWillChangePlaymode);
  70. {
  71. serializedObject.Update();
  72. DrawListControls();
  73. EditorGUILayout.Space();
  74. DrawPrefabList();
  75. EditorGUILayout.Space();
  76. DrawSpawnControls();
  77. serializedObject.ApplyModifiedProperties();
  78. }
  79. EditorGUI.EndDisabledGroup();
  80. }
  81. private void DrawListControls()
  82. {
  83. showListControls = EditorGUILayout.Foldout(showListControls, Label.ListControls, true);
  84. if (showListControls)
  85. {
  86. EditorGUI.indentLevel++;
  87. // Clear List
  88. if (GUILayout.Button(Label.ClearList))
  89. {
  90. entries.ClearArray();
  91. GUI.changed = true;
  92. }
  93. // Set all counts controls
  94. EditorGUILayout.BeginHorizontal();
  95. {
  96. setAllCountValue = Mathf.Max(1, EditorGUILayout.IntField(Label.SetAllCounts, setAllCountValue));
  97. if (GUILayout.Button(Label.ApplyButton, GUILayout.Width(60)))
  98. {
  99. for (int i = 0; i < entries.arraySize; i++)
  100. {
  101. var element = entries.GetArrayElementAtIndex(i);
  102. var countProperty = element.FindPropertyRelative(nameof(TilePoolPreloaderEntry.Count));
  103. countProperty.intValue = setAllCountValue;
  104. }
  105. GUI.changed = true;
  106. }
  107. }
  108. EditorGUILayout.EndHorizontal();
  109. EditorGUI.indentLevel--;
  110. EditorGUILayout.Space();
  111. }
  112. }
  113. private void DrawPrefabList()
  114. {
  115. // Handle drag and drop
  116. Rect dropRect = GUILayoutUtility.GetRect(0, reorderableList.GetHeight());
  117. reorderableList.DoList(dropRect);
  118. Event evt = Event.current;
  119. switch (evt.type)
  120. {
  121. case EventType.DragUpdated:
  122. case EventType.DragPerform:
  123. if (!dropRect.Contains(evt.mousePosition))
  124. break;
  125. DragAndDrop.visualMode = HasValidDragObject(DragAndDrop.objectReferences) ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Rejected;
  126. if (evt.type == EventType.DragPerform)
  127. {
  128. DragAndDrop.AcceptDrag();
  129. foreach (Object draggedObject in DragAndDrop.objectReferences)
  130. TryAddDraggedObject(draggedObject);
  131. GUI.changed = true;
  132. }
  133. evt.Use();
  134. break;
  135. }
  136. }
  137. private void DrawSpawnControls()
  138. {
  139. var preloader = target as TilePoolPreloader;
  140. EditorGUI.BeginDisabledGroup(!preloader.HasSpawnedInstances());
  141. {
  142. if (GUILayout.Button(Label.ClearInstancesButton))
  143. preloader.ClearSpawnedInstances();
  144. }
  145. EditorGUI.EndDisabledGroup();
  146. if (GUILayout.Button(Label.RefreshInstancesButton))
  147. {
  148. int totalInstanceCount = 0;
  149. foreach(var entry in preloader.Entries)
  150. {
  151. if(entry.TilePrefab != null)
  152. totalInstanceCount += entry.Count;
  153. }
  154. if (totalInstanceCount > 0)
  155. {
  156. bool generateInstances;
  157. // Get confirmation from the user before spawning the tile instances
  158. generateInstances = EditorUtility.DisplayDialog(
  159. "Generating Tiles",
  160. $"{totalInstanceCount} tile instances will be created. Are you sure you want to continue?",
  161. "Continue",
  162. "Cancel");
  163. if (generateInstances)
  164. preloader.RefreshTileInstances();
  165. }
  166. }
  167. }
  168. private bool HasValidDragObject(Object[] draggedObjects)
  169. {
  170. foreach (var draggedObject in draggedObjects)
  171. {
  172. if (draggedObject is GameObject gameObject)
  173. {
  174. var prefabType = PrefabUtility.GetPrefabAssetType(gameObject);
  175. bool isPrefab = prefabType == PrefabAssetType.Regular || prefabType == PrefabAssetType.Variant;
  176. if (isPrefab && gameObject.TryGetComponent<Tile>(out var tile))
  177. return true;
  178. }
  179. else if (draggedObject is TileSet || draggedObject is DungeonArchetype || draggedObject is DungeonFlow)
  180. return true;
  181. }
  182. return false;
  183. }
  184. private void AddTile(Tile tilePrefab)
  185. {
  186. if(tilePrefab == null)
  187. return;
  188. // Check if this tile already exists in the list
  189. for (int i = 0; i < entries.arraySize; i++)
  190. {
  191. var element = entries.GetArrayElementAtIndex(i);
  192. var prefabProperty = element.FindPropertyRelative(nameof(TilePoolPreloaderEntry.TilePrefab));
  193. var existingTilePrefab = prefabProperty.objectReferenceValue as Tile;
  194. if (tilePrefab == existingTilePrefab)
  195. return; // Tile already exists, skip it
  196. }
  197. entries.InsertArrayElementAtIndex(entries.arraySize);
  198. var newElement = entries.GetArrayElementAtIndex(entries.arraySize - 1);
  199. newElement.FindPropertyRelative(nameof(TilePoolPreloaderEntry.TilePrefab)).objectReferenceValue = tilePrefab;
  200. newElement.FindPropertyRelative(nameof(TilePoolPreloaderEntry.Count)).intValue = 1;
  201. }
  202. private void AddTileSet(TileSet tileSet)
  203. {
  204. foreach (var entry in tileSet.TileWeights.Weights)
  205. {
  206. if (entry == null || entry.Value == null)
  207. continue;
  208. if (entry.Value.TryGetComponent(out Tile tile))
  209. AddTile(tile);
  210. }
  211. }
  212. private void AddArchetype(DungeonArchetype archetype)
  213. {
  214. // Add regular tile sets
  215. foreach (var tileSet in archetype.TileSets)
  216. if (tileSet != null)
  217. AddTileSet(tileSet);
  218. // Add branch start tile sets
  219. foreach (var tileSet in archetype.BranchStartTileSets)
  220. if (tileSet != null)
  221. AddTileSet(tileSet);
  222. // Add branch end tile sets
  223. foreach (var tileSet in archetype.BranchCapTileSets)
  224. if (tileSet != null)
  225. AddTileSet(tileSet);
  226. }
  227. private void AddDungeonFlow(DungeonFlow flow)
  228. {
  229. // Add tile sets from nodes
  230. foreach(var node in flow.Nodes)
  231. {
  232. foreach(var tileSet in node.TileSets)
  233. if(tileSet != null)
  234. AddTileSet(tileSet);
  235. }
  236. // Add archetypes from lines
  237. foreach(var line in flow.Lines)
  238. {
  239. foreach(var archetype in line.DungeonArchetypes)
  240. if (archetype != null)
  241. AddArchetype(archetype);
  242. }
  243. // Add tile sets from injection rules
  244. foreach (var injectionRule in flow.TileInjectionRules)
  245. {
  246. if(injectionRule.TileSet != null)
  247. AddTileSet(injectionRule.TileSet);
  248. }
  249. }
  250. private void TryAddDraggedObject(Object draggedObject)
  251. {
  252. // Tile Prefab
  253. if (draggedObject is GameObject gameObject)
  254. {
  255. var prefabType = PrefabUtility.GetPrefabAssetType(gameObject);
  256. bool isPrefab = prefabType == PrefabAssetType.Regular || prefabType == PrefabAssetType.Variant;
  257. if (isPrefab && gameObject.TryGetComponent<Tile>(out var tile))
  258. AddTile(tile);
  259. }
  260. // TileSet
  261. else if (draggedObject is TileSet tileSet)
  262. AddTileSet(tileSet);
  263. // Archetype
  264. else if (draggedObject is DungeonArchetype archetype)
  265. AddArchetype(archetype);
  266. else if (draggedObject is DungeonFlow flow)
  267. AddDungeonFlow(flow);
  268. }
  269. }
  270. }