AgentInfoPanel.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. /// <summary>
  4. /// Displays agent information in a popup panel using UI Toolkit
  5. /// Singleton pattern to ensure only one panel is shown at a time
  6. /// </summary>
  7. public class AgentInfoPanel : MonoBehaviour
  8. {
  9. private static AgentInfoPanel instance;
  10. private static UIDocument uiDocument;
  11. private static VisualElement panelRoot;
  12. private static AIAgent currentAgent;
  13. void Awake()
  14. {
  15. if (instance != null && instance != this)
  16. {
  17. Destroy(gameObject);
  18. return;
  19. }
  20. instance = this;
  21. uiDocument = GetComponent<UIDocument>();
  22. if (uiDocument == null)
  23. {
  24. Debug.LogError("AgentInfoPanel: UIDocument component not found!");
  25. return;
  26. }
  27. }
  28. void OnEnable()
  29. {
  30. if (uiDocument == null) return;
  31. panelRoot = uiDocument.rootVisualElement.Q<VisualElement>("AgentInfoPanelRoot");
  32. if (panelRoot == null)
  33. {
  34. Debug.LogError("AgentInfoPanel: AgentInfoPanelRoot not found in UXML!");
  35. return;
  36. }
  37. // Hide panel initially
  38. panelRoot.style.display = DisplayStyle.None;
  39. // Setup close button
  40. var closeButton = panelRoot.Q<Button>("CloseButton");
  41. if (closeButton != null)
  42. {
  43. closeButton.clicked += HidePanel;
  44. }
  45. }
  46. /// <summary>
  47. /// Shows agent information in the info panel
  48. /// </summary>
  49. public static void ShowAgentInfo(AIAgent agent)
  50. {
  51. if (instance == null)
  52. {
  53. Debug.LogWarning("AgentInfoPanel: Instance not found!");
  54. return;
  55. }
  56. if (agent == null) return;
  57. currentAgent = agent;
  58. instance.DisplayAgentInfo(agent);
  59. }
  60. /// <summary>
  61. /// Displays the agent info in the UI
  62. /// </summary>
  63. private void DisplayAgentInfo(AIAgent agent)
  64. {
  65. if (panelRoot == null) return;
  66. // Show panel
  67. panelRoot.style.display = DisplayStyle.Flex;
  68. // Set agent name
  69. var nameLabel = panelRoot.Q<Label>("AgentNameLabel");
  70. if (nameLabel != null)
  71. {
  72. nameLabel.text = agent.AgentName;
  73. }
  74. // Set stats
  75. var stats = agent.Stats;
  76. if (stats != null)
  77. {
  78. UpdateStatDisplay("StrengthBar", "StrengthValue", stats.Strength);
  79. UpdateStatDisplay("SpeedBar", "SpeedValue", stats.Speed);
  80. UpdateStatDisplay("MagicBar", "MagicValue", stats.Magic);
  81. UpdateStatDisplay("DexterityBar", "DexterityValue", stats.Dexterity);
  82. var totalLabel = panelRoot.Q<Label>("TotalStatsValue");
  83. if (totalLabel != null)
  84. {
  85. totalLabel.text = stats.GetTotalStats().ToString();
  86. }
  87. }
  88. // Set status
  89. var statusLabel = panelRoot.Q<Label>("StatusLabel");
  90. if (statusLabel != null)
  91. {
  92. if (agent.HasReachedGoal)
  93. {
  94. statusLabel.text = "✓ Reached Exit";
  95. }
  96. else
  97. {
  98. statusLabel.text = "Exploring maze...";
  99. }
  100. }
  101. Debug.Log($"Displaying info for: {agent.AgentName}");
  102. }
  103. /// <summary>
  104. /// Updates a stat display (progress bar and label)
  105. /// </summary>
  106. private void UpdateStatDisplay(string barName, string labelName, int value)
  107. {
  108. var bar = panelRoot.Q<ProgressBar>(barName);
  109. if (bar != null)
  110. {
  111. bar.value = value;
  112. bar.highValue = 100;
  113. }
  114. var label = panelRoot.Q<Label>(labelName);
  115. if (label != null)
  116. {
  117. label.text = value.ToString();
  118. }
  119. }
  120. /// <summary>
  121. /// Hides the info panel
  122. /// </summary>
  123. private static void HidePanel()
  124. {
  125. if (panelRoot != null)
  126. {
  127. panelRoot.style.display = DisplayStyle.None;
  128. currentAgent = null;
  129. }
  130. }
  131. /// <summary>
  132. /// Hides panel when clicking outside of it
  133. /// </summary>
  134. void Update()
  135. {
  136. if (panelRoot == null || panelRoot.style.display == DisplayStyle.None) return;
  137. if (Input.GetMouseButtonDown(0))
  138. {
  139. // Check if click is outside the panel
  140. var clickPos = Input.mousePosition;
  141. var panelWorldRect = panelRoot.worldBound;
  142. if (!panelWorldRect.Contains(new Vector2(clickPos.x, Screen.height - clickPos.y)))
  143. {
  144. HidePanel();
  145. }
  146. }
  147. }
  148. }