| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- using UnityEngine;
- using UnityEngine.UIElements;
- /// <summary>
- /// Helper script to automatically set up the Map Location Names and Legend system.
- /// Add this to a GameObject in MapScene2 to automatically configure all components.
- /// </summary>
- public class MapLocationSystemSetup : MonoBehaviour
- {
- [Header("Auto Setup Configuration")]
- public bool setupOnStart = true;
- public bool findExistingComponents = true;
- [Header("Feature Generation Settings")]
- public bool enableAutoGeneration = true;
- public bool enableDebugMode = false;
- [Header("Legend Settings")]
- public bool legendStartVisible = true;
- public KeyCode legendToggleKey = KeyCode.L;
- [Header("Name Display Settings")]
- public bool showSettlements = true;
- public bool showForests = true;
- public bool showLakes = true;
- public bool showPlains = true;
- public bool showMountains = true;
- public bool showRivers = true;
- [Header("References (Optional - will auto-find if not set)")]
- public UIDocument mapUIDocument;
- private void Start()
- {
- if (setupOnStart)
- {
- SetupMapLocationSystem();
- }
- }
- [ContextMenu("Setup Map Location System")]
- public void SetupMapLocationSystem()
- {
- Debug.Log("🚀 Setting up Map Location Names and Legend System...");
- SetupGeographicFeatureManager();
- SetupMapLocationNameDisplay();
- SetupMapLegendUI();
- ConnectLegendToNameDisplay();
- SetupClickManager();
- Debug.Log("✅ Map Location System setup complete!");
- }
- [ContextMenu("Force Refresh All Components")]
- public void ForceRefreshAllComponents()
- {
- Debug.Log("🔄 Force refreshing all map location components...");
- // Force setup even for existing components
- bool originalFindExisting = findExistingComponents;
- findExistingComponents = false;
- SetupMapLocationSystem();
- findExistingComponents = originalFindExisting;
- // Force refresh names
- RefreshLocationNames();
- Debug.Log("✅ Force refresh complete!");
- }
- private void ConnectLegendToNameDisplay()
- {
- var legendUI = FindFirstObjectByType<MapSceneLegendUI>();
- if (legendUI != null)
- {
- legendUI.RefreshNameDisplayConnection();
- Debug.Log("🔗 Connected legend to name display");
- }
- }
- private void SetupGeographicFeatureManager()
- {
- var existing = FindFirstObjectByType<GeographicFeatureManager>();
- if (existing != null && findExistingComponents)
- {
- Debug.Log("📍 Found existing GeographicFeatureManager");
- ConfigureFeatureManager(existing);
- return;
- }
- // Create GeographicFeatureManager
- GameObject featureManagerGO = new GameObject("GeographicFeatureManager");
- var featureManager = featureManagerGO.AddComponent<GeographicFeatureManager>();
- ConfigureFeatureManager(featureManager);
- Debug.Log("📍 Created GeographicFeatureManager");
- }
- private void ConfigureFeatureManager(GeographicFeatureManager manager)
- {
- manager.autoGenerateFeatures = enableAutoGeneration;
- manager.debugMode = enableDebugMode;
- }
- private void SetupMapLocationNameDisplay()
- {
- var existing = FindFirstObjectByType<MapLocationNameDisplay>();
- if (existing != null && findExistingComponents)
- {
- Debug.Log("🏷️ Found existing MapLocationNameDisplay");
- ConfigureNameDisplay(existing);
- // Ensure existing component has proper UIDocument setup
- var existingUIDocument = existing.GetComponent<UIDocument>();
- if (existingUIDocument != null)
- {
- SetupUIDocumentForNameDisplay(existingUIDocument);
- }
- return;
- }
- // Create MapLocationNameDisplay
- GameObject nameDisplayGO = new GameObject("MapLocationNameDisplay");
- var nameDisplay = nameDisplayGO.AddComponent<MapLocationNameDisplay>();
- // Add UIDocument component for the name display
- var nameUIDocument = nameDisplayGO.AddComponent<UIDocument>();
- SetupUIDocumentForNameDisplay(nameUIDocument);
- ConfigureNameDisplay(nameDisplay);
- Debug.Log("🏷️ Created MapLocationNameDisplay");
- }
- private void SetupUIDocumentForNameDisplay(UIDocument uiDocument)
- {
- uiDocument.sortingOrder = 1; // Above map terrain, below UI panels
- // Set PanelSettings using fallback approach
- SetupPanelSettings(uiDocument, "Name Display");
- // Load the UXML template
- var nameDisplayUXML = Resources.Load<VisualTreeAsset>("UI/MapLocationNameDisplay");
- if (nameDisplayUXML != null)
- {
- uiDocument.visualTreeAsset = nameDisplayUXML;
- Debug.Log("✅ Loaded UXML template for Name Display");
- }
- else
- {
- Debug.LogWarning("Could not find MapLocationNameDisplay.uxml template in Resources/UI/");
- }
- }
- private void ConfigureNameDisplay(MapLocationNameDisplay display)
- {
- // Configure the display settings
- display.showSettlementNames = showSettlements;
- display.showForestNames = showForests;
- display.showLakeNames = showLakes;
- display.showPlainNames = showPlains;
- display.showMountainNames = showMountains;
- display.showRiverNames = showRivers;
- }
- private void SetupMapLegendUI()
- {
- var existing = FindFirstObjectByType<MapSceneLegendUI>();
- if (existing != null && findExistingComponents)
- {
- Debug.Log("🗺️ Found existing MapSceneLegendUI");
- ConfigureLegendUI(existing);
- // Ensure existing component has proper UIDocument setup
- var existingUIDocument = existing.GetComponent<UIDocument>();
- if (existingUIDocument != null)
- {
- SetupUIDocumentForLegend(existingUIDocument);
- }
- return;
- }
- // Create MapSceneLegendUI
- GameObject legendGO = new GameObject("MapSceneLegendUI");
- var uiDocument = legendGO.AddComponent<UIDocument>();
- var legendUI = legendGO.AddComponent<MapSceneLegendUI>();
- SetupUIDocumentForLegend(uiDocument);
- legendUI.uiDocument = uiDocument;
- ConfigureLegendUI(legendUI);
- Debug.Log("🗺️ Created MapSceneLegendUI");
- }
- private void SetupUIDocumentForLegend(UIDocument uiDocument)
- {
- // Set sorting order to be above name display but below modal UIs
- uiDocument.sortingOrder = 2; // Above name display (1), below other UI panels (3+)
- // Set PanelSettings using fallback approach
- SetupPanelSettings(uiDocument, "Legend UI");
- // Load the UXML template for the legend
- var legendUXML = Resources.Load<VisualTreeAsset>("UI/MapLegend");
- if (legendUXML != null)
- {
- uiDocument.visualTreeAsset = legendUXML;
- Debug.Log("✅ Loaded UXML template for Legend UI");
- }
- else
- {
- Debug.LogWarning("Could not find MapLegend.uxml template in Resources/UI/");
- }
- }
- private void ConfigureLegendUI(MapSceneLegendUI legendUI)
- {
- legendUI.startVisible = legendStartVisible;
- legendUI.toggleKey = legendToggleKey;
- }
- private void SetupClickManager()
- {
- // ClickManager is a singleton that creates itself, just ensure it exists
- var clickManager = ClickManager.Instance;
- if (clickManager != null)
- {
- Debug.Log("🖱️ ClickManager ready");
- }
- else
- {
- Debug.LogWarning("⚠️ ClickManager could not be initialized");
- }
- }
- private void SetupPanelSettings(UIDocument uiDocument, string componentName)
- {
- // Try multiple fallback approaches to find PanelSettings
- UnityEngine.UIElements.PanelSettings panelSettings = null;
- // 1. Try loading from Resources (original approach)
- panelSettings = Resources.Load<UnityEngine.UIElements.PanelSettings>("MainSettings");
- if (panelSettings != null)
- {
- uiDocument.panelSettings = panelSettings;
- Debug.Log($"✅ Set Panel Settings for {componentName} from Resources");
- return;
- }
- // 2. Try getting from TravelUI GameObject
- var travelUI = FindFirstObjectByType<TravelUI>();
- if (travelUI != null && travelUI.uiDocument != null && travelUI.uiDocument.panelSettings != null)
- {
- uiDocument.panelSettings = travelUI.uiDocument.panelSettings;
- Debug.Log($"✅ Set Panel Settings for {componentName} from TravelUI");
- return;
- }
- // 3. Try getting from any existing UIDocument with valid PanelSettings
- var allUIDocuments = FindObjectsByType<UIDocument>(FindObjectsSortMode.None);
- foreach (var existingUIDoc in allUIDocuments)
- {
- if (existingUIDoc.panelSettings != null)
- {
- uiDocument.panelSettings = existingUIDoc.panelSettings;
- Debug.Log($"✅ Set Panel Settings for {componentName} from existing UIDocument");
- return;
- }
- }
- Debug.LogWarning($"⚠️ Could not find any PanelSettings for {componentName}");
- }
- [ContextMenu("Find Map UI Document")]
- public void FindMapUIDocument()
- {
- var uiDocs = FindObjectsByType<UIDocument>(FindObjectsSortMode.None);
- foreach (var doc in uiDocs)
- {
- if (doc.rootVisualElement != null)
- {
- mapUIDocument = doc;
- Debug.Log($"📄 Found UIDocument on {doc.gameObject.name}");
- break;
- }
- }
- if (mapUIDocument == null)
- {
- Debug.LogWarning("⚠️ No suitable UIDocument found in scene");
- }
- }
- [ContextMenu("Regenerate All Features")]
- public void RegenerateAllFeatures()
- {
- var featureManager = FindFirstObjectByType<GeographicFeatureManager>();
- if (featureManager != null)
- {
- featureManager.RegenerateFeatures();
- Debug.Log("🔄 Regenerated geographic features");
- }
- else
- {
- Debug.LogWarning("⚠️ GeographicFeatureManager not found");
- }
- }
- [ContextMenu("Refresh Location Names")]
- public void RefreshLocationNames()
- {
- var nameDisplay = FindFirstObjectByType<MapLocationNameDisplay>();
- if (nameDisplay != null)
- {
- nameDisplay.RefreshLocationNames();
- Debug.Log("🔄 Refreshed location names");
- }
- else
- {
- Debug.LogWarning("⚠️ MapLocationNameDisplay not found");
- }
- }
- [ContextMenu("Test Click Blocking")]
- public void TestClickBlocking()
- {
- var clickManager = ClickManager.Instance;
- if (clickManager != null)
- {
- bool isBlocked = clickManager.IsClickBlocked(Input.mousePosition);
- Debug.Log($"🖱️ Click at mouse position {Input.mousePosition} is {(isBlocked ? "BLOCKED" : "ALLOWED")}");
- Debug.Log($"📊 Active blockers: {clickManager.GetRegisteredBlockersCount()}");
- }
- else
- {
- Debug.LogWarning("⚠️ ClickManager not available");
- }
- }
- }
|