MapLocationSystemSetup.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. /// <summary>
  4. /// Helper script to automatically set up the Map Location Names and Legend system.
  5. /// Add this to a GameObject in MapScene2 to automatically configure all components.
  6. /// </summary>
  7. public class MapLocationSystemSetup : MonoBehaviour
  8. {
  9. [Header("Auto Setup Configuration")]
  10. public bool setupOnStart = true;
  11. public bool findExistingComponents = true;
  12. [Header("Feature Generation Settings")]
  13. public bool enableAutoGeneration = true;
  14. public bool enableDebugMode = false;
  15. [Header("Legend Settings")]
  16. public bool legendStartVisible = true;
  17. public KeyCode legendToggleKey = KeyCode.L;
  18. [Header("Name Display Settings")]
  19. public bool showSettlements = true;
  20. public bool showForests = true;
  21. public bool showLakes = true;
  22. public bool showPlains = true;
  23. public bool showMountains = true;
  24. public bool showRivers = true;
  25. [Header("References (Optional - will auto-find if not set)")]
  26. public UIDocument mapUIDocument;
  27. private void Start()
  28. {
  29. if (setupOnStart)
  30. {
  31. SetupMapLocationSystem();
  32. }
  33. }
  34. [ContextMenu("Setup Map Location System")]
  35. public void SetupMapLocationSystem()
  36. {
  37. Debug.Log("🚀 Setting up Map Location Names and Legend System...");
  38. SetupGeographicFeatureManager();
  39. SetupMapLocationNameDisplay();
  40. SetupMapLegendUI();
  41. ConnectLegendToNameDisplay();
  42. SetupClickManager();
  43. Debug.Log("✅ Map Location System setup complete!");
  44. }
  45. [ContextMenu("Force Refresh All Components")]
  46. public void ForceRefreshAllComponents()
  47. {
  48. Debug.Log("🔄 Force refreshing all map location components...");
  49. // Force setup even for existing components
  50. bool originalFindExisting = findExistingComponents;
  51. findExistingComponents = false;
  52. SetupMapLocationSystem();
  53. findExistingComponents = originalFindExisting;
  54. // Force refresh names
  55. RefreshLocationNames();
  56. Debug.Log("✅ Force refresh complete!");
  57. }
  58. private void ConnectLegendToNameDisplay()
  59. {
  60. var legendUI = FindFirstObjectByType<MapSceneLegendUI>();
  61. if (legendUI != null)
  62. {
  63. legendUI.RefreshNameDisplayConnection();
  64. Debug.Log("🔗 Connected legend to name display");
  65. }
  66. }
  67. private void SetupGeographicFeatureManager()
  68. {
  69. var existing = FindFirstObjectByType<GeographicFeatureManager>();
  70. if (existing != null && findExistingComponents)
  71. {
  72. Debug.Log("📍 Found existing GeographicFeatureManager");
  73. ConfigureFeatureManager(existing);
  74. return;
  75. }
  76. // Create GeographicFeatureManager
  77. GameObject featureManagerGO = new GameObject("GeographicFeatureManager");
  78. var featureManager = featureManagerGO.AddComponent<GeographicFeatureManager>();
  79. ConfigureFeatureManager(featureManager);
  80. Debug.Log("📍 Created GeographicFeatureManager");
  81. }
  82. private void ConfigureFeatureManager(GeographicFeatureManager manager)
  83. {
  84. manager.autoGenerateFeatures = enableAutoGeneration;
  85. manager.debugMode = enableDebugMode;
  86. }
  87. private void SetupMapLocationNameDisplay()
  88. {
  89. var existing = FindFirstObjectByType<MapLocationNameDisplay>();
  90. if (existing != null && findExistingComponents)
  91. {
  92. Debug.Log("🏷️ Found existing MapLocationNameDisplay");
  93. ConfigureNameDisplay(existing);
  94. // Ensure existing component has proper UIDocument setup
  95. var existingUIDocument = existing.GetComponent<UIDocument>();
  96. if (existingUIDocument != null)
  97. {
  98. SetupUIDocumentForNameDisplay(existingUIDocument);
  99. }
  100. return;
  101. }
  102. // Create MapLocationNameDisplay
  103. GameObject nameDisplayGO = new GameObject("MapLocationNameDisplay");
  104. var nameDisplay = nameDisplayGO.AddComponent<MapLocationNameDisplay>();
  105. // Add UIDocument component for the name display
  106. var nameUIDocument = nameDisplayGO.AddComponent<UIDocument>();
  107. SetupUIDocumentForNameDisplay(nameUIDocument);
  108. ConfigureNameDisplay(nameDisplay);
  109. Debug.Log("🏷️ Created MapLocationNameDisplay");
  110. }
  111. private void SetupUIDocumentForNameDisplay(UIDocument uiDocument)
  112. {
  113. uiDocument.sortingOrder = 1; // Above map terrain, below UI panels
  114. // Set PanelSettings using fallback approach
  115. SetupPanelSettings(uiDocument, "Name Display");
  116. // Load the UXML template
  117. var nameDisplayUXML = Resources.Load<VisualTreeAsset>("UI/MapLocationNameDisplay");
  118. if (nameDisplayUXML != null)
  119. {
  120. uiDocument.visualTreeAsset = nameDisplayUXML;
  121. Debug.Log("✅ Loaded UXML template for Name Display");
  122. }
  123. else
  124. {
  125. Debug.LogWarning("Could not find MapLocationNameDisplay.uxml template in Resources/UI/");
  126. }
  127. }
  128. private void ConfigureNameDisplay(MapLocationNameDisplay display)
  129. {
  130. // Configure the display settings
  131. display.showSettlementNames = showSettlements;
  132. display.showForestNames = showForests;
  133. display.showLakeNames = showLakes;
  134. display.showPlainNames = showPlains;
  135. display.showMountainNames = showMountains;
  136. display.showRiverNames = showRivers;
  137. }
  138. private void SetupMapLegendUI()
  139. {
  140. var existing = FindFirstObjectByType<MapSceneLegendUI>();
  141. if (existing != null && findExistingComponents)
  142. {
  143. Debug.Log("🗺️ Found existing MapSceneLegendUI");
  144. ConfigureLegendUI(existing);
  145. // Ensure existing component has proper UIDocument setup
  146. var existingUIDocument = existing.GetComponent<UIDocument>();
  147. if (existingUIDocument != null)
  148. {
  149. SetupUIDocumentForLegend(existingUIDocument);
  150. }
  151. return;
  152. }
  153. // Create MapSceneLegendUI
  154. GameObject legendGO = new GameObject("MapSceneLegendUI");
  155. var uiDocument = legendGO.AddComponent<UIDocument>();
  156. var legendUI = legendGO.AddComponent<MapSceneLegendUI>();
  157. SetupUIDocumentForLegend(uiDocument);
  158. legendUI.uiDocument = uiDocument;
  159. ConfigureLegendUI(legendUI);
  160. Debug.Log("🗺️ Created MapSceneLegendUI");
  161. }
  162. private void SetupUIDocumentForLegend(UIDocument uiDocument)
  163. {
  164. // Set sorting order to be above name display but below modal UIs
  165. uiDocument.sortingOrder = 2; // Above name display (1), below other UI panels (3+)
  166. // Set PanelSettings using fallback approach
  167. SetupPanelSettings(uiDocument, "Legend UI");
  168. // Load the UXML template for the legend
  169. var legendUXML = Resources.Load<VisualTreeAsset>("UI/MapLegend");
  170. if (legendUXML != null)
  171. {
  172. uiDocument.visualTreeAsset = legendUXML;
  173. Debug.Log("✅ Loaded UXML template for Legend UI");
  174. }
  175. else
  176. {
  177. Debug.LogWarning("Could not find MapLegend.uxml template in Resources/UI/");
  178. }
  179. }
  180. private void ConfigureLegendUI(MapSceneLegendUI legendUI)
  181. {
  182. legendUI.startVisible = legendStartVisible;
  183. legendUI.toggleKey = legendToggleKey;
  184. }
  185. private void SetupClickManager()
  186. {
  187. // ClickManager is a singleton that creates itself, just ensure it exists
  188. var clickManager = ClickManager.Instance;
  189. if (clickManager != null)
  190. {
  191. Debug.Log("🖱️ ClickManager ready");
  192. }
  193. else
  194. {
  195. Debug.LogWarning("⚠️ ClickManager could not be initialized");
  196. }
  197. }
  198. private void SetupPanelSettings(UIDocument uiDocument, string componentName)
  199. {
  200. // Try multiple fallback approaches to find PanelSettings
  201. UnityEngine.UIElements.PanelSettings panelSettings = null;
  202. // 1. Try loading from Resources (original approach)
  203. panelSettings = Resources.Load<UnityEngine.UIElements.PanelSettings>("MainSettings");
  204. if (panelSettings != null)
  205. {
  206. uiDocument.panelSettings = panelSettings;
  207. Debug.Log($"✅ Set Panel Settings for {componentName} from Resources");
  208. return;
  209. }
  210. // 2. Try getting from TravelUI GameObject
  211. var travelUI = FindFirstObjectByType<TravelUI>();
  212. if (travelUI != null && travelUI.uiDocument != null && travelUI.uiDocument.panelSettings != null)
  213. {
  214. uiDocument.panelSettings = travelUI.uiDocument.panelSettings;
  215. Debug.Log($"✅ Set Panel Settings for {componentName} from TravelUI");
  216. return;
  217. }
  218. // 3. Try getting from any existing UIDocument with valid PanelSettings
  219. var allUIDocuments = FindObjectsByType<UIDocument>(FindObjectsSortMode.None);
  220. foreach (var existingUIDoc in allUIDocuments)
  221. {
  222. if (existingUIDoc.panelSettings != null)
  223. {
  224. uiDocument.panelSettings = existingUIDoc.panelSettings;
  225. Debug.Log($"✅ Set Panel Settings for {componentName} from existing UIDocument");
  226. return;
  227. }
  228. }
  229. Debug.LogWarning($"⚠️ Could not find any PanelSettings for {componentName}");
  230. }
  231. [ContextMenu("Find Map UI Document")]
  232. public void FindMapUIDocument()
  233. {
  234. var uiDocs = FindObjectsByType<UIDocument>(FindObjectsSortMode.None);
  235. foreach (var doc in uiDocs)
  236. {
  237. if (doc.rootVisualElement != null)
  238. {
  239. mapUIDocument = doc;
  240. Debug.Log($"📄 Found UIDocument on {doc.gameObject.name}");
  241. break;
  242. }
  243. }
  244. if (mapUIDocument == null)
  245. {
  246. Debug.LogWarning("⚠️ No suitable UIDocument found in scene");
  247. }
  248. }
  249. [ContextMenu("Regenerate All Features")]
  250. public void RegenerateAllFeatures()
  251. {
  252. var featureManager = FindFirstObjectByType<GeographicFeatureManager>();
  253. if (featureManager != null)
  254. {
  255. featureManager.RegenerateFeatures();
  256. Debug.Log("🔄 Regenerated geographic features");
  257. }
  258. else
  259. {
  260. Debug.LogWarning("⚠️ GeographicFeatureManager not found");
  261. }
  262. }
  263. [ContextMenu("Refresh Location Names")]
  264. public void RefreshLocationNames()
  265. {
  266. var nameDisplay = FindFirstObjectByType<MapLocationNameDisplay>();
  267. if (nameDisplay != null)
  268. {
  269. nameDisplay.RefreshLocationNames();
  270. Debug.Log("🔄 Refreshed location names");
  271. }
  272. else
  273. {
  274. Debug.LogWarning("⚠️ MapLocationNameDisplay not found");
  275. }
  276. }
  277. [ContextMenu("Test Click Blocking")]
  278. public void TestClickBlocking()
  279. {
  280. var clickManager = ClickManager.Instance;
  281. if (clickManager != null)
  282. {
  283. bool isBlocked = clickManager.IsClickBlocked(Input.mousePosition);
  284. Debug.Log($"🖱️ Click at mouse position {Input.mousePosition} is {(isBlocked ? "BLOCKED" : "ALLOWED")}");
  285. Debug.Log($"📊 Active blockers: {clickManager.GetRegisteredBlockersCount()}");
  286. }
  287. else
  288. {
  289. Debug.LogWarning("⚠️ ClickManager not available");
  290. }
  291. }
  292. }