| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- using UnityEngine;
- using UnityEngine.UIElements;
- /// <summary>
- /// Simple test script to help debug and fix map location positioning issues.
- /// Add this to a GameObject in MapScene2 to test the location naming system.
- /// </summary>
- public class MapLocationTestHelper : MonoBehaviour
- {
- [Header("Test Settings")]
- public bool runTestOnStart = false;
- public KeyCode testKey = KeyCode.T;
- [Header("Debug")]
- public bool verboseLogging = false;
- private void Start()
- {
- if (runTestOnStart)
- {
- RunDiagnostics();
- }
- }
- private void Update()
- {
- if (Input.GetKeyDown(testKey))
- {
- RunDiagnostics();
- }
- }
- [ContextMenu("Run Full Diagnostics")]
- public void RunDiagnostics()
- {
- Debug.Log("=== MAP LOCATION SYSTEM DIAGNOSTICS ===");
- TestMapMaker();
- TestGeographicFeatures();
- TestNameDisplay();
- TestLegend();
- TestUISetup();
- Debug.Log("=== DIAGNOSTICS COMPLETE ===");
- }
- private void TestMapMaker()
- {
- Debug.Log("--- Testing MapMaker2 ---");
- var mapMaker = FindFirstObjectByType<MapMaker2>();
- if (mapMaker != null)
- {
- var mapData = mapMaker.GetMapData();
- if (mapData != null)
- {
- Debug.Log($"✅ MapMaker2 found with MapData: {mapData.Width}x{mapData.Height}");
- var settlements = mapData.GetAllSettlements();
- Debug.Log($"✅ Settlements found: {settlements.Count}");
- if (verboseLogging && settlements.Count > 0)
- {
- foreach (var settlement in settlements)
- {
- Debug.Log($" - {settlement.name} ({settlement.Type}) at {settlement.position}");
- }
- }
- }
- else
- {
- Debug.LogError("❌ MapMaker2 found but MapData is null");
- }
- }
- else
- {
- Debug.LogError("❌ MapMaker2 not found in scene");
- }
- }
- private void TestGeographicFeatures()
- {
- Debug.Log("--- Testing Geographic Features ---");
- var featureManager = FindFirstObjectByType<GeographicFeatureManager>();
- if (featureManager != null)
- {
- var features = featureManager.GeographicFeatures;
- Debug.Log($"✅ GeographicFeatureManager found with {features.Count} features");
- if (verboseLogging && features.Count > 0)
- {
- foreach (var feature in features)
- {
- Debug.Log($" - {feature.name} ({feature.type}) at {feature.centerPosition} with {feature.tiles.Count} tiles");
- }
- }
- }
- else
- {
- Debug.LogError("❌ GeographicFeatureManager not found in scene");
- }
- }
- private void TestNameDisplay()
- {
- Debug.Log("--- Testing Name Display ---");
- var nameDisplay = FindFirstObjectByType<MapLocationNameDisplay>();
- if (nameDisplay != null)
- {
- Debug.Log($"✅ MapLocationNameDisplay found");
- Debug.Log($" - Show Settlements: {nameDisplay.showSettlementNames}");
- Debug.Log($" - Show Forests: {nameDisplay.showForestNames}");
- Debug.Log($" - Show Lakes: {nameDisplay.showLakeNames}");
- // Test if it has a UIDocument
- if (nameDisplay.mapUIDocument != null)
- {
- Debug.Log($"✅ UIDocument assigned");
- if (nameDisplay.mapUIDocument.rootVisualElement != null)
- {
- Debug.Log($"✅ Root visual element exists");
- var mapContainer = nameDisplay.mapUIDocument.rootVisualElement.Q<VisualElement>("map-container");
- if (mapContainer != null)
- {
- Debug.Log($"✅ Map container found: {mapContainer.worldBound}");
- }
- else
- {
- Debug.LogWarning("⚠️ Map container not found");
- }
- }
- else
- {
- Debug.LogError("❌ Root visual element is null");
- }
- }
- else
- {
- Debug.LogError("❌ UIDocument not assigned to MapLocationNameDisplay");
- }
- }
- else
- {
- Debug.LogError("❌ MapLocationNameDisplay not found in scene");
- }
- }
- private void TestLegend()
- {
- Debug.Log("--- Testing Legend ---");
- var legend = FindFirstObjectByType<MapSceneLegendUI>();
- if (legend != null)
- {
- Debug.Log($"✅ MapSceneLegendUI found");
- Debug.Log($" - Start Visible: {legend.startVisible}");
- Debug.Log($" - Toggle Key: {legend.toggleKey}");
- if (legend.uiDocument != null)
- {
- Debug.Log($"✅ UIDocument assigned");
- if (legend.uiDocument.rootVisualElement != null)
- {
- Debug.Log($"✅ Root visual element exists with {legend.uiDocument.rootVisualElement.childCount} children");
- }
- else
- {
- Debug.LogError("❌ Root visual element is null");
- }
- }
- else
- {
- Debug.LogError("❌ UIDocument not assigned to MapSceneLegendUI");
- }
- }
- else
- {
- Debug.LogError("❌ MapSceneLegendUI not found in scene");
- }
- }
- private void TestUISetup()
- {
- Debug.Log("--- Testing UI Setup ---");
- var uiDocuments = FindObjectsByType<UIDocument>(FindObjectsSortMode.None);
- Debug.Log($"Found {uiDocuments.Length} UIDocument components in scene");
- foreach (var doc in uiDocuments)
- {
- if (doc.rootVisualElement != null)
- {
- Debug.Log($" - {doc.gameObject.name}: Root element with {doc.rootVisualElement.childCount} children");
- }
- else
- {
- Debug.LogWarning($" - {doc.gameObject.name}: No root element");
- }
- }
- }
- [ContextMenu("Force Refresh All")]
- public void ForceRefreshAll()
- {
- Debug.Log("=== FORCING REFRESH OF ALL SYSTEMS ===");
- // Refresh geographic features
- var featureManager = FindFirstObjectByType<GeographicFeatureManager>();
- if (featureManager != null)
- {
- featureManager.RegenerateFeatures();
- Debug.Log("✅ Regenerated geographic features");
- }
- // Refresh name display
- var nameDisplay = FindFirstObjectByType<MapLocationNameDisplay>();
- if (nameDisplay != null)
- {
- nameDisplay.RefreshLocationNames();
- Debug.Log("✅ Refreshed location names");
- }
- // Refresh legend
- var legend = FindFirstObjectByType<MapSceneLegendUI>();
- if (legend != null)
- {
- legend.OnFeaturesGenerated();
- Debug.Log("✅ Refreshed legend");
- }
- Debug.Log("=== REFRESH COMPLETE ===");
- }
- [ContextMenu("Test Coordinate Conversion")]
- public void TestCoordinateConversion()
- {
- var nameDisplay = FindFirstObjectByType<MapLocationNameDisplay>();
- var mapMaker = FindFirstObjectByType<MapMaker2>();
- if (nameDisplay != null && mapMaker != null)
- {
- var mapData = mapMaker.GetMapData();
- if (mapData != null)
- {
- Debug.Log("=== TESTING COORDINATE CONVERSION ===");
- // Test corner coordinates
- var testPoints = new Vector2Int[]
- {
- new Vector2Int(0, 0),
- new Vector2Int(mapData.Width / 2, mapData.Height / 2),
- new Vector2Int(mapData.Width - 1, mapData.Height - 1)
- };
- foreach (var point in testPoints)
- {
- // We can't directly call WorldToUIPosition as it's private,
- // but we can test with settlements at those positions
- Debug.Log($"World point {point} would be at map bounds");
- }
- }
- }
- }
- }
|