MapLocationNameDisplay.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. using System.Collections.Generic;
  4. using System.Collections;
  5. public class MapLocationNameDisplay : MonoBehaviour, IClickBlocker
  6. {
  7. [Header("System Control")]
  8. public bool systemEnabled = false; // Disabled - moving to 3D world space approach
  9. [Header("UI References")]
  10. public UIDocument mapUIDocument;
  11. [Header("Display Settings")]
  12. public bool showSettlementNames = true;
  13. public bool showForestNames = true;
  14. public bool showLakeNames = true;
  15. public bool showPlainNames = true;
  16. public bool showMountainNames = true;
  17. public bool showRiverNames = true;
  18. [Header("Visual Settings")]
  19. public Color settlementNameColor = Color.white;
  20. public Color forestNameColor = Color.green;
  21. public Color lakeNameColor = Color.cyan;
  22. public Color plainNameColor = Color.yellow;
  23. public Color mountainNameColor = Color.gray;
  24. public Color riverNameColor = Color.blue;
  25. [Header("World Positioning")]
  26. public bool debugMode = false;
  27. public float updateFrequency = 0.33f; // Update positions 3 times per second for good performance
  28. private VisualElement mapContainer;
  29. private List<Label> nameLabels = new List<Label>();
  30. private List<Vector3> worldPositions = new List<Vector3>(); // Store world positions for each label
  31. private GeographicFeatureManager featureManager;
  32. private MapData mapData;
  33. private MapMaker2 mapMaker;
  34. private Camera mainCamera;
  35. private Coroutine positionUpdateCoroutine;
  36. private Vector3 lastCameraPosition;
  37. private float lastCameraSize;
  38. private void Start()
  39. {
  40. if (!systemEnabled)
  41. {
  42. if (debugMode) Debug.Log("MapLocationNameDisplay: System disabled, not initializing");
  43. return;
  44. }
  45. InitializeReferences();
  46. SetupUI();
  47. // Find the main camera
  48. mainCamera = Camera.main;
  49. if (mainCamera == null)
  50. {
  51. mainCamera = FindFirstObjectByType<Camera>();
  52. }
  53. // Initialize camera tracking
  54. if (mainCamera != null)
  55. {
  56. lastCameraPosition = mainCamera.transform.position;
  57. lastCameraSize = mainCamera.orthographicSize;
  58. }
  59. if (debugMode)
  60. {
  61. Debug.Log($"MapLocationNameDisplay: Started. MapData: {(mapData != null ? $"{mapData.Width}x{mapData.Height}" : "NULL")}");
  62. Debug.Log($"MapLocationNameDisplay: FeatureManager: {(featureManager != null ? "Found" : "NULL")}");
  63. Debug.Log($"MapLocationNameDisplay: MapContainer: {(mapContainer != null ? "Created" : "NULL")}");
  64. Debug.Log($"MapLocationNameDisplay: Camera: {(mainCamera != null ? "Found" : "NULL")}");
  65. }
  66. // Initial refresh of location names
  67. StartCoroutine(InitialRefreshDelayed());
  68. }
  69. private void InitializeReferences()
  70. {
  71. if (mapUIDocument == null)
  72. mapUIDocument = GetComponent<UIDocument>();
  73. featureManager = FindFirstObjectByType<GeographicFeatureManager>();
  74. mapMaker = FindFirstObjectByType<MapMaker2>();
  75. if (mapMaker != null)
  76. {
  77. mapData = mapMaker.GetMapData();
  78. }
  79. if (debugMode)
  80. {
  81. Debug.Log($"MapLocationNameDisplay.InitializeReferences:");
  82. Debug.Log($" - mapUIDocument: {(mapUIDocument != null ? "Found" : "NULL")}");
  83. Debug.Log($" - featureManager: {(featureManager != null ? $"Found ({featureManager.GeographicFeatures.Count} features)" : "NULL")}");
  84. Debug.Log($" - mapMaker: {(mapMaker != null ? "Found" : "NULL")}");
  85. Debug.Log($" - mapData: {(mapData != null ? $"Found ({mapData.Width}x{mapData.Height})" : "NULL")}");
  86. }
  87. }
  88. private void SetupUI()
  89. {
  90. if (mapUIDocument?.rootVisualElement != null)
  91. {
  92. // Load the CSS for proper layering
  93. var layerStyleSheet = Resources.Load<StyleSheet>("UI/MapLocationNamesLayer");
  94. if (layerStyleSheet != null)
  95. {
  96. mapUIDocument.rootVisualElement.styleSheets.Add(layerStyleSheet);
  97. if (debugMode) Debug.Log("MapLocationNameDisplay: Loaded MapLocationNamesLayer stylesheet");
  98. }
  99. else if (debugMode)
  100. {
  101. Debug.LogWarning("MapLocationNameDisplay: Could not load MapLocationNamesLayer stylesheet from Resources/UI/");
  102. }
  103. // Try to find existing map container
  104. mapContainer = mapUIDocument.rootVisualElement.Q<VisualElement>("map-container");
  105. if (mapContainer == null)
  106. {
  107. // Create map container if it doesn't exist
  108. mapContainer = new VisualElement();
  109. mapContainer.name = "map-container";
  110. mapContainer.style.position = Position.Absolute;
  111. mapContainer.style.width = Length.Percent(100);
  112. mapContainer.style.height = Length.Percent(100);
  113. mapContainer.style.left = 0;
  114. mapContainer.style.top = 0;
  115. mapContainer.pickingMode = PickingMode.Ignore; // Allow clicks to pass through
  116. // Ensure the container is visible and can contain children
  117. mapContainer.style.overflow = Overflow.Visible;
  118. mapContainer.style.visibility = Visibility.Visible;
  119. mapContainer.style.display = DisplayStyle.Flex;
  120. // Use CSS to control layering instead of insertion position
  121. mapContainer.AddToClassList("map-location-names-layer");
  122. // Add to the end of the hierarchy, but use CSS z-index to control layering
  123. mapUIDocument.rootVisualElement.Add(mapContainer);
  124. if (debugMode) Debug.Log("MapLocationNameDisplay: Created new map-container with click-through enabled");
  125. }
  126. else
  127. {
  128. // Ensure existing container also has proper picking mode and visibility
  129. mapContainer.pickingMode = PickingMode.Ignore;
  130. mapContainer.AddToClassList("map-location-names-layer");
  131. mapContainer.style.overflow = Overflow.Visible;
  132. mapContainer.style.visibility = Visibility.Visible;
  133. mapContainer.style.display = DisplayStyle.Flex;
  134. if (debugMode) Debug.Log($"MapLocationNameDisplay: Found existing map-container with {mapContainer.childCount} children, ensured click-through");
  135. }
  136. }
  137. else if (debugMode)
  138. {
  139. Debug.LogWarning("MapLocationNameDisplay: mapUIDocument or rootVisualElement is null");
  140. }
  141. }
  142. public void OnFeaturesGenerated()
  143. {
  144. // Wait a frame to ensure everything is set up
  145. StartCoroutine(RefreshNamesDelayed());
  146. }
  147. private System.Collections.IEnumerator InitialRefreshDelayed()
  148. {
  149. yield return null; // Wait one frame for setup
  150. yield return null; // Wait another frame to be sure
  151. if (debugMode) Debug.Log("MapLocationNameDisplay: Initial refresh triggered");
  152. RefreshLocationNames();
  153. }
  154. private System.Collections.IEnumerator RefreshNamesDelayed()
  155. {
  156. yield return null; // Wait one frame
  157. yield return null; // Wait another frame to be sure
  158. if (debugMode) Debug.Log("MapLocationNameDisplay: Delayed refresh triggered");
  159. RefreshLocationNames();
  160. }
  161. public void RefreshLocationNames()
  162. {
  163. if (!systemEnabled)
  164. {
  165. if (debugMode) Debug.Log("MapLocationNameDisplay: System disabled, skipping refresh");
  166. return;
  167. }
  168. ClearAllLabels();
  169. if (mapContainer == null || mapData == null)
  170. {
  171. if (debugMode) Debug.LogWarning($"MapLocationNameDisplay: Cannot refresh - mapContainer: {(mapContainer != null ? "OK" : "NULL")}, mapData: {(mapData != null ? "OK" : "NULL")}");
  172. return;
  173. }
  174. if (debugMode) Debug.Log($"MapLocationNameDisplay: Refreshing location names... Container children before: {mapContainer.childCount}");
  175. // Display settlement names
  176. if (showSettlementNames)
  177. {
  178. if (debugMode) Debug.Log("MapLocationNameDisplay: Attempting to display settlement names...");
  179. DisplaySettlementNames();
  180. }
  181. // Display geographic feature names
  182. if (featureManager != null)
  183. {
  184. if (debugMode) Debug.Log($"MapLocationNameDisplay: Found {featureManager.GeographicFeatures.Count} geographic features");
  185. var features = featureManager.GeographicFeatures;
  186. if (debugMode) Debug.Log($"MapLocationNameDisplay: Found {features.Count} geographic features");
  187. foreach (var feature in features)
  188. {
  189. if (ShouldDisplayFeature(feature))
  190. {
  191. CreateFeatureLabel(feature);
  192. }
  193. }
  194. }
  195. else if (debugMode)
  196. {
  197. Debug.LogWarning("MapLocationNameDisplay: FeatureManager is null");
  198. }
  199. if (debugMode) Debug.Log($"MapLocationNameDisplay: Created {nameLabels.Count} name labels total");
  200. // Debug final container state
  201. if (debugMode && mapContainer != null)
  202. {
  203. Debug.Log($"MapLocationNameDisplay: Final container state - Children: {mapContainer.childCount}, PickingMode: {mapContainer.pickingMode}, Parent: {(mapContainer.parent != null ? mapContainer.parent.name : "NULL")}");
  204. Debug.Log($"MapLocationNameDisplay: Container bounds: {mapContainer.localBound}, World bounds: {mapContainer.worldBound}");
  205. Debug.Log($"MapLocationNameDisplay: Container visible: {mapContainer.style.visibility}, Display: {mapContainer.style.display}, Overflow: {mapContainer.style.overflow}");
  206. Debug.Log($"MapLocationNameDisplay: Container size: width={mapContainer.style.width}, height={mapContainer.style.height}");
  207. Debug.Log($"MapLocationNameDisplay: Container position: left={mapContainer.style.left}, top={mapContainer.style.top}, position={mapContainer.style.position}");
  208. // Double-check that labels are actually in the container
  209. if (mapContainer.childCount != nameLabels.Count)
  210. {
  211. Debug.LogError($"MapLocationNameDisplay: MISMATCH! Container has {mapContainer.childCount} children but nameLabels has {nameLabels.Count} items!");
  212. // Try to re-add labels if they're missing
  213. for (int i = 0; i < nameLabels.Count; i++)
  214. {
  215. if (nameLabels[i].parent == null)
  216. {
  217. Debug.Log($"MapLocationNameDisplay: Re-adding orphaned label: {nameLabels[i].text}");
  218. mapContainer.Add(nameLabels[i]);
  219. }
  220. }
  221. Debug.Log($"MapLocationNameDisplay: After re-adding, container has {mapContainer.childCount} children");
  222. }
  223. else
  224. {
  225. Debug.Log($"MapLocationNameDisplay: SUCCESS! Container and nameLabels count match: {mapContainer.childCount}");
  226. // Check first few labels for debugging
  227. for (int i = 0; i < Mathf.Min(3, nameLabels.Count); i++)
  228. {
  229. var label = nameLabels[i];
  230. Debug.Log($"MapLocationNameDisplay: Label {i}: '{label.text}' - Visible: {label.style.visibility}, Display: {label.style.display}, Position: ({label.style.left.value.value:F1}, {label.style.top.value.value:F1}), PickingMode: {label.pickingMode}, Parent: {(label.parent != null ? label.parent.name : "NULL")}");
  231. }
  232. }
  233. }
  234. // Start position updates if we have labels and camera
  235. if (nameLabels.Count > 0 && mainCamera != null)
  236. {
  237. StartPositionUpdates();
  238. if (debugMode) Debug.Log("MapLocationNameDisplay: Started continuous position updates");
  239. }
  240. }
  241. private void OnDestroy()
  242. {
  243. StopPositionUpdates();
  244. }
  245. private void DisplaySettlementNames()
  246. {
  247. var settlements = mapData.GetAllSettlements();
  248. if (debugMode) Debug.Log($"MapLocationNameDisplay: Displaying {settlements.Count} settlement names");
  249. foreach (var settlement in settlements)
  250. {
  251. CreateSettlementLabel(settlement);
  252. }
  253. }
  254. private bool ShouldDisplayFeature(GeographicFeature feature)
  255. {
  256. switch (feature.type)
  257. {
  258. case GeographicFeatureType.Forest:
  259. return showForestNames;
  260. case GeographicFeatureType.Lake:
  261. return showLakeNames;
  262. case GeographicFeatureType.Plain:
  263. return showPlainNames;
  264. case GeographicFeatureType.Mountain:
  265. return showMountainNames;
  266. case GeographicFeatureType.River:
  267. return showRiverNames;
  268. default:
  269. return false;
  270. }
  271. }
  272. private void CreateSettlementLabel(Settlement settlement)
  273. {
  274. var label = new Label(settlement.name);
  275. label.AddToClassList("location-name-label");
  276. label.AddToClassList("settlement-name");
  277. // Store the world position for this label
  278. Vector3 worldPos = new Vector3(settlement.position.x, 0, settlement.position.y);
  279. worldPositions.Add(worldPos);
  280. // Position the label (will be updated continuously)
  281. Vector2 uiPosition = WorldToUIPosition(settlement.position);
  282. PositionLabel(label, uiPosition);
  283. // Manual styling to avoid CSS conflicts
  284. label.style.color = Color.white;
  285. label.style.fontSize = settlement.Type == SettlementType.Town ? 14 : 12;
  286. label.style.unityFontStyleAndWeight = settlement.Type == SettlementType.Town ? FontStyle.Bold : FontStyle.Normal;
  287. // Background for visibility
  288. label.style.backgroundColor = new Color(0, 0, 0, 0.7f);
  289. label.style.paddingLeft = 3;
  290. label.style.paddingRight = 3;
  291. label.style.paddingTop = 1;
  292. label.style.paddingBottom = 1;
  293. label.style.borderTopLeftRadius = 3;
  294. label.style.borderTopRightRadius = 3;
  295. label.style.borderBottomLeftRadius = 3;
  296. label.style.borderBottomRightRadius = 3;
  297. // CRITICAL: Allow clicks to pass through labels to the map beneath
  298. label.pickingMode = PickingMode.Ignore;
  299. // Ensure label is visible
  300. label.style.visibility = Visibility.Visible;
  301. label.style.display = DisplayStyle.Flex;
  302. mapContainer.Add(label);
  303. nameLabels.Add(label);
  304. // Debug verification
  305. if (debugMode)
  306. {
  307. Debug.Log($"MapLocationNameDisplay: Added settlement label '{label.text}' at world {worldPos} -> UI {uiPosition}. Container children: {mapContainer.childCount}. PickingMode: {label.pickingMode}");
  308. }
  309. }
  310. private void CreateFeatureLabel(GeographicFeature feature)
  311. {
  312. var label = new Label(feature.name);
  313. label.AddToClassList("location-name-label");
  314. label.AddToClassList($"{feature.type.ToString().ToLower()}-name");
  315. // Store the world position for this label
  316. Vector2Int featureCenter = feature.GetCenterPosition();
  317. Vector3 worldPos = new Vector3(featureCenter.x, 0, featureCenter.y);
  318. worldPositions.Add(worldPos);
  319. // Position the label at feature center (will be updated continuously)
  320. Vector2 uiPosition = WorldToUIPosition(featureCenter);
  321. PositionLabel(label, uiPosition);
  322. // Manual styling based on feature type to avoid CSS conflicts
  323. Color featureColor = GetFeatureColor(feature.type);
  324. label.style.color = featureColor;
  325. label.style.fontSize = GetFeatureFontSize(feature.type);
  326. label.style.unityFontStyleAndWeight = FontStyle.Normal;
  327. // Background for visibility
  328. label.style.backgroundColor = new Color(0, 0, 0, 0.6f);
  329. label.style.paddingLeft = 2;
  330. label.style.paddingRight = 2;
  331. label.style.paddingTop = 1;
  332. label.style.paddingBottom = 1;
  333. label.style.borderTopLeftRadius = 3;
  334. label.style.borderTopRightRadius = 3;
  335. label.style.borderBottomLeftRadius = 3;
  336. label.style.borderBottomRightRadius = 3;
  337. // CRITICAL: Allow clicks to pass through labels to the map beneath
  338. label.pickingMode = PickingMode.Ignore;
  339. // Ensure label is visible
  340. label.style.visibility = Visibility.Visible;
  341. label.style.display = DisplayStyle.Flex;
  342. // Add opacity for undiscovered features
  343. if (!feature.isDiscovered)
  344. {
  345. label.style.opacity = 0.6f;
  346. }
  347. mapContainer.Add(label);
  348. nameLabels.Add(label);
  349. // Debug verification
  350. if (debugMode)
  351. {
  352. Debug.Log($"MapLocationNameDisplay: Added feature label '{label.text}' to container. Container children: {mapContainer.childCount}. PickingMode: {label.pickingMode}");
  353. }
  354. }
  355. private Color GetFeatureColor(GeographicFeatureType type)
  356. {
  357. switch (type)
  358. {
  359. case GeographicFeatureType.Forest:
  360. return forestNameColor;
  361. case GeographicFeatureType.Lake:
  362. return lakeNameColor;
  363. case GeographicFeatureType.Plain:
  364. return plainNameColor;
  365. case GeographicFeatureType.Mountain:
  366. return mountainNameColor;
  367. case GeographicFeatureType.River:
  368. return riverNameColor;
  369. default:
  370. return Color.white;
  371. }
  372. }
  373. private int GetFeatureFontSize(GeographicFeatureType type)
  374. {
  375. switch (type)
  376. {
  377. case GeographicFeatureType.Mountain:
  378. return 13;
  379. case GeographicFeatureType.Forest:
  380. case GeographicFeatureType.Lake:
  381. return 12;
  382. case GeographicFeatureType.Plain:
  383. case GeographicFeatureType.River:
  384. return 11;
  385. default:
  386. return 12;
  387. }
  388. }
  389. private Vector2 WorldToUIPosition(Vector2Int worldPosition)
  390. {
  391. if (mainCamera == null || mapData == null)
  392. {
  393. if (debugMode) Debug.LogWarning("MapLocationNameDisplay: Camera or MapData is null in WorldToUIPosition");
  394. return Vector2.zero;
  395. }
  396. // Convert map grid coordinates to world position
  397. // Each tile is 1 unit in world space
  398. Vector3 worldPos = new Vector3(worldPosition.x, 0, worldPosition.y);
  399. // Convert world position to screen position
  400. Vector3 screenPos = mainCamera.WorldToScreenPoint(worldPos);
  401. // Convert screen position to UI coordinates
  402. if (mapContainer == null)
  403. {
  404. if (debugMode) Debug.LogWarning("MapLocationNameDisplay: MapContainer is null in WorldToUIPosition");
  405. return Vector2.zero;
  406. }
  407. // Get the container's bounds in screen space
  408. var containerBounds = mapContainer.worldBound;
  409. // Convert screen coordinates to UI element coordinates
  410. Vector2 uiPosition = new Vector2(
  411. screenPos.x - containerBounds.x,
  412. (Screen.height - screenPos.y) - containerBounds.y // Flip Y coordinate for UI space
  413. );
  414. if (debugMode && Random.Range(0f, 1f) < 0.05f) // Log occasionally to avoid spam
  415. {
  416. Debug.Log($"MapLocationNameDisplay: World {worldPosition} -> WorldPos {worldPos} -> Screen {screenPos} -> UI {uiPosition}");
  417. }
  418. return uiPosition;
  419. }
  420. private void PositionLabel(Label label, Vector2 uiPosition)
  421. {
  422. label.style.position = Position.Absolute;
  423. // The uiPosition is already calculated relative to the container
  424. label.style.left = uiPosition.x;
  425. label.style.top = uiPosition.y;
  426. if (debugMode && Random.Range(0f, 1f) < 0.05f) // Log occasionally to avoid spam
  427. {
  428. Debug.Log($"MapLocationNameDisplay: Positioned label '{label.text}' at ({uiPosition.x:F1}, {uiPosition.y:F1})");
  429. }
  430. }
  431. private void ClearAllLabels()
  432. {
  433. if (debugMode) Debug.Log($"MapLocationNameDisplay: Clearing {nameLabels.Count} existing labels");
  434. foreach (var label in nameLabels)
  435. {
  436. if (label.parent != null)
  437. {
  438. label.parent.Remove(label);
  439. }
  440. }
  441. nameLabels.Clear();
  442. worldPositions.Clear(); // Clear corresponding world positions
  443. if (debugMode && mapContainer != null)
  444. {
  445. Debug.Log($"MapLocationNameDisplay: After clearing, container has {mapContainer.childCount} children");
  446. }
  447. }
  448. private void StartPositionUpdates()
  449. {
  450. if (positionUpdateCoroutine != null)
  451. {
  452. StopCoroutine(positionUpdateCoroutine);
  453. }
  454. positionUpdateCoroutine = StartCoroutine(UpdateLabelPositions());
  455. }
  456. private void StopPositionUpdates()
  457. {
  458. if (positionUpdateCoroutine != null)
  459. {
  460. StopCoroutine(positionUpdateCoroutine);
  461. positionUpdateCoroutine = null;
  462. }
  463. }
  464. private IEnumerator UpdateLabelPositions()
  465. {
  466. while (true)
  467. {
  468. if (mainCamera != null && nameLabels.Count > 0 && worldPositions.Count == nameLabels.Count)
  469. {
  470. // Only update if camera has moved or zoom changed (more sensitive for smoother updates)
  471. bool cameraChanged = false;
  472. Vector3 currentCameraPos = mainCamera.transform.position;
  473. float currentCameraSize = mainCamera.orthographicSize;
  474. if (Vector3.Distance(currentCameraPos, lastCameraPosition) > 0.05f ||
  475. Mathf.Abs(currentCameraSize - lastCameraSize) > 0.05f)
  476. {
  477. cameraChanged = true;
  478. lastCameraPosition = currentCameraPos;
  479. lastCameraSize = currentCameraSize;
  480. }
  481. if (cameraChanged)
  482. {
  483. for (int i = 0; i < nameLabels.Count; i++)
  484. {
  485. if (nameLabels[i] != null && i < worldPositions.Count)
  486. {
  487. Vector3 worldPos = worldPositions[i];
  488. Vector3 screenPos = mainCamera.WorldToScreenPoint(worldPos);
  489. // Check if the position is in front of the camera
  490. if (screenPos.z > 0)
  491. {
  492. Vector2 uiPos = ScreenToUIPosition(screenPos);
  493. PositionLabel(nameLabels[i], uiPos);
  494. nameLabels[i].style.visibility = Visibility.Visible;
  495. }
  496. else
  497. {
  498. // Hide labels that are behind the camera
  499. nameLabels[i].style.visibility = Visibility.Hidden;
  500. }
  501. }
  502. }
  503. }
  504. }
  505. yield return new WaitForSeconds(updateFrequency);
  506. }
  507. }
  508. private Vector2 ScreenToUIPosition(Vector3 screenPos)
  509. {
  510. if (mapContainer == null) return Vector2.zero;
  511. var containerBounds = mapContainer.worldBound;
  512. // Convert screen coordinates to UI element coordinates
  513. Vector2 uiPosition = new Vector2(
  514. screenPos.x - containerBounds.x,
  515. (Screen.height - screenPos.y) - containerBounds.y // Flip Y coordinate for UI space
  516. );
  517. return uiPosition;
  518. }
  519. // Public methods for toggling different name types
  520. public void ToggleSettlementNames(bool show)
  521. {
  522. showSettlementNames = show;
  523. RefreshLocationNames();
  524. }
  525. public void ToggleForestNames(bool show)
  526. {
  527. showForestNames = show;
  528. RefreshLocationNames();
  529. }
  530. public void ToggleLakeNames(bool show)
  531. {
  532. showLakeNames = show;
  533. RefreshLocationNames();
  534. }
  535. public void TogglePlainNames(bool show)
  536. {
  537. showPlainNames = show;
  538. RefreshLocationNames();
  539. }
  540. public void ToggleMountainNames(bool show)
  541. {
  542. showMountainNames = show;
  543. RefreshLocationNames();
  544. }
  545. public void ToggleRiverNames(bool show)
  546. {
  547. showRiverNames = show;
  548. RefreshLocationNames();
  549. }
  550. [ContextMenu("Refresh Names")]
  551. public void RefreshNamesManual()
  552. {
  553. RefreshLocationNames();
  554. }
  555. [ContextMenu("Debug Info")]
  556. public void DebugInfo()
  557. {
  558. Debug.Log($"=== MapLocationNameDisplay Debug Info ===");
  559. Debug.Log($"MapData: {(mapData != null ? $"{mapData.Width}x{mapData.Height}" : "NULL")}");
  560. Debug.Log($"FeatureManager: {(featureManager != null ? "Found" : "NULL")}");
  561. Debug.Log($"MapContainer: {(mapContainer != null ? "Found" : "NULL")}");
  562. Debug.Log($"MapUIDocument: {(mapUIDocument != null ? "Found" : "NULL")}");
  563. Debug.Log($"Name Labels: {nameLabels.Count}");
  564. if (featureManager != null)
  565. {
  566. Debug.Log($"Geographic Features: {featureManager.GeographicFeatures.Count}");
  567. }
  568. if (mapData != null)
  569. {
  570. var settlements = mapData.GetAllSettlements();
  571. Debug.Log($"Settlements: {settlements.Count}");
  572. }
  573. }
  574. // IClickBlocker implementation - system disabled, never blocks clicks
  575. public bool IsBlockingClick(Vector2 screenPosition)
  576. {
  577. // System disabled - UI toolkit approach caused too many issues
  578. // Moving to 3D world space TextMeshPro approach instead
  579. return false;
  580. }
  581. }