RealWorldTerrainBuildingManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* INFINITY CODE */
  2. /* https://infinity-code.com */
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using InfinityCode.RealWorldTerrain.OSM;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace InfinityCode.RealWorldTerrain.Tools
  9. {
  10. public class RealWorldTerrainBuildingManager : EditorWindow
  11. {
  12. private RealWorldTerrainOSMMeta[] buildings;
  13. private Vector2 scrollPosition;
  14. private string filter;
  15. private FilterType filterType = FilterType.KeyAndValue;
  16. private RealWorldTerrainOSMMeta[] filteredBuildings;
  17. private int page = 0;
  18. private void FilterBuildings()
  19. {
  20. if (string.IsNullOrEmpty(filter)) filteredBuildings = buildings;
  21. else
  22. {
  23. string f = filter.ToUpperInvariant();
  24. bool searchInKey = filterType != FilterType.Value;
  25. bool searchInValue = filterType != FilterType.Key;
  26. filteredBuildings = buildings.Where(b => b.ContainKeyOrValue(f, searchInKey, searchInValue)).ToArray();
  27. }
  28. }
  29. private void OnEnable()
  30. {
  31. UpdateBuildings();
  32. }
  33. private void OnGUI()
  34. {
  35. EditorGUILayout.HelpBox("Filters buildings by tag (in whole or in part).", MessageType.Info);
  36. EditorGUI.BeginChangeCheck();
  37. filter = EditorGUILayout.TextField("Filter", filter);
  38. filterType = (FilterType)EditorGUILayout.EnumPopup("Search in", filterType);
  39. if (EditorGUI.EndChangeCheck()) FilterBuildings();
  40. int pageSize = 500;
  41. bool showPaginator = filteredBuildings.Length > pageSize;
  42. if (!showPaginator) page = 0;
  43. else if (page * pageSize >= filteredBuildings.Length) page = filteredBuildings.Length / pageSize;
  44. int start = page * pageSize;
  45. int end = Mathf.Min(start + pageSize, filteredBuildings.Length);
  46. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
  47. bool hasNullItems = false;
  48. for (int i = start; i < end; i++)
  49. {
  50. RealWorldTerrainOSMMeta building = filteredBuildings[i];
  51. GUILayout.BeginHorizontal();
  52. string buildingName = "";
  53. if (building == null)
  54. {
  55. buildingName = "Missed";
  56. hasNullItems = true;
  57. }
  58. else buildingName = building.name;
  59. GUILayout.Label(buildingName);
  60. if (GUILayout.Button(new GUIContent(">", "Select"), GUILayout.ExpandWidth(false)))
  61. {
  62. Selection.activeGameObject = building.gameObject;
  63. }
  64. GUILayout.EndHorizontal();
  65. }
  66. EditorGUILayout.EndScrollView();
  67. if (showPaginator)
  68. {
  69. EditorGUILayout.BeginHorizontal();
  70. EditorGUI.BeginDisabledGroup(page == 0);
  71. if (GUILayout.Button("<")) page--;
  72. EditorGUI.EndDisabledGroup();
  73. EditorGUILayout.LabelField((start + 1) + "-" + end + " from " + filteredBuildings.Length);
  74. EditorGUI.BeginDisabledGroup(page == filteredBuildings.Length / pageSize);
  75. if (GUILayout.Button(">")) page++;
  76. EditorGUI.EndDisabledGroup();
  77. EditorGUILayout.EndHorizontal();
  78. }
  79. if (GUILayout.Button("Select All"))
  80. {
  81. Selection.objects = filteredBuildings.Select(b => b.gameObject).ToArray();
  82. }
  83. if (hasNullItems)
  84. {
  85. UpdateBuildings();
  86. Repaint();
  87. }
  88. }
  89. [MenuItem("Window/Infinity Code/Real World Terrain/Tools/Building Manager")]
  90. public static void OpenWindow()
  91. {
  92. GetWindow<RealWorldTerrainBuildingManager>(true, "Building Manager");
  93. }
  94. private void UpdateBuildings()
  95. {
  96. buildings = FindObjectsOfType<RealWorldTerrainBuilding>().Select(b => b.GetComponent<RealWorldTerrainOSMMeta>()).OrderBy(b => b.name).ToArray();
  97. FilterBuildings();
  98. }
  99. public enum FilterType
  100. {
  101. KeyAndValue,
  102. Key,
  103. Value
  104. }
  105. }
  106. }