< Summary

Class:UIManager
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/UI/UIManager.cs
Covered lines:0
Uncovered lines:158
Coverable lines:158
Total lines:237
Line coverage:0% (0 of 158)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:29
Method coverage:0% (0 of 29)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UIManager()0%2100%
Awake()0%6200%
Start()0%2100%
LogAction(...)0%2100%
LogActionMessage(...)0%2100%
LogActionWarning(...)0%2100%
LogActionError(...)0%2100%
ToggleConfigSelectorPanel()0%2100%
SetupConfigSelectorPanel()0%2100%
PopulateConfigDropdown()0%6200%
LoadSelectedConfig()0%2100%
SetUIMode(...)0%2100%
GetUIMode()0%2100%
SetAgentPanelText(...)0%2100%
GetSwarmPanelText()0%2100%
UpdateSwarmPanel()0%6200%
UpdateSimTimeText()0%6200%
UpdateTotalCostText()0%6200%
FormatCost(...)0%20400%
RegisterSimulationEnded()0%2100%
UpdateSummaryText()0%2100%
RegisterNewInterceptor(...)0%2100%
RegisterNewThreat(...)0%2100%
RegisterAgentTerminated(...)0%12300%
RegisterInterceptorHit(...)0%2100%
RegisterInterceptorMiss(...)0%2100%
Update()0%2100%

File(s)

/github/workspace/Assets/Scripts/UI/UIManager.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using System.IO;
 4using UnityEngine;
 5using UnityEngine.UI;
 6using TMPro;
 7using System;
 8
 9public class UIManager : MonoBehaviour {
 010  public static UIManager Instance { get; private set; }
 11
 12  [SerializeField]
 13  private GameObject _agentStatusPanel;
 14  [SerializeField]
 15  private GameObject _configSelectorPanel;
 16  private TMP_Dropdown _configDropdown;
 17  public TextMeshProUGUI agentPanelText;
 18  public TextMeshProUGUI simTimeText;
 19  public TextMeshProUGUI interceptorCostText;
 20  public TextMeshProUGUI threatCostText;
 21  public TextMeshProUGUI netCostText;
 22
 23  public TextMeshProUGUI intrHitTextHandle;
 24  public TextMeshProUGUI intrMissTextHandle;
 25  public TextMeshProUGUI intrRemainTextHandle;
 26  public TextMeshProUGUI thrtRemainTextHandle;
 27
 28  public TextMeshProUGUI actionMessageTextHandle;
 29  public TextMeshProUGUI pActionMessageTextHandle;
 30  public TextMeshProUGUI ppActionMessageTextHandle;
 31
 032  private int _intrHitCount = 0;
 033  private int _intrMissCount = 0;
 034  private int _intrRemainCount = 0;
 035  private int _thrtRemainCount = 0;
 36  public TMP_FontAsset Font;
 37
 038  private UIMode curMode = UIMode.NONE;
 39
 40  // Start is called before the first frame update
 041  void Awake() {
 42    // singleton
 043    if (Instance == null)
 044      Instance = this;
 45    else
 046      Destroy(gameObject);
 047  }
 48
 049  void Start() {
 050    _configSelectorPanel.SetActive(false);
 051    SetupConfigSelectorPanel();
 52    // inputManager = InputManager.Instance;
 53    // worldManager = WorldManager.Instance;
 054    SimManager.Instance.OnNewInterceptor += RegisterNewInterceptor;
 055    SimManager.Instance.OnNewThreat += RegisterNewThreat;
 056    SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded;
 057    actionMessageTextHandle.text = "";
 058    pActionMessageTextHandle.text = "";
 059    ppActionMessageTextHandle.text = "";
 060  }
 61
 062  public void LogAction(string message, Color color) {
 63    // Shift existing messages to older slots with faded colors
 064    ppActionMessageTextHandle.text = pActionMessageTextHandle.text;
 065    ppActionMessageTextHandle.color = pActionMessageTextHandle.color * 0.5f;  // Fade color by 50%
 66
 067    pActionMessageTextHandle.text = actionMessageTextHandle.text;
 068    pActionMessageTextHandle.color = actionMessageTextHandle.color * 0.75f;  // Fade color by 25%
 69
 70    // Set new message
 071    actionMessageTextHandle.text = message;
 072    actionMessageTextHandle.color = color;
 073  }
 74
 075  public void LogActionMessage(string message) {
 076    LogAction(message, Color.white);
 077  }
 78
 079  public void LogActionWarning(string message) {
 080    LogAction(message, Color.yellow);
 081  }
 82
 083  public void LogActionError(string message) {
 084    LogAction(message, Color.red);
 085  }
 86
 087  public void ToggleConfigSelectorPanel() {
 088    _configSelectorPanel.SetActive(!_configSelectorPanel.activeSelf);
 089  }
 90
 091  private void SetupConfigSelectorPanel() {
 092    _configSelectorPanel.GetComponentInChildren<Button>().onClick.AddListener(
 093        delegate { LoadSelectedConfig(); });
 094    _configDropdown = _configSelectorPanel.GetComponentInChildren<TMP_Dropdown>();
 095    PopulateConfigDropdown();
 096  }
 97
 098  private void PopulateConfigDropdown() {
 099    _configDropdown.ClearOptions();
 0100    string configPath = Path.Combine(Application.streamingAssetsPath, "Configs");
 0101    string[] configFiles = Directory.GetFiles(configPath, "*.json");
 102
 0103    List<string> configFileNames = new List<string>();
 0104    foreach (string configFile in configFiles) {
 0105      configFileNames.Add(Path.GetFileName(configFile));
 0106    }
 0107    _configDropdown.AddOptions(configFileNames);
 0108  }
 0109  private void LoadSelectedConfig() {
 0110    string selectedConfig = _configDropdown.options[_configDropdown.value].text;
 0111    SimManager.Instance.LoadNewConfig(selectedConfig);
 0112    _configSelectorPanel.SetActive(false);
 113    // if(!InputManager.Instance.mouseActive){
 114    //     InputManager.Instance.mouseActive = true;
 115    // }
 0116  }
 117
 0118  public void SetUIMode(UIMode mode) {
 0119    curMode = mode;
 0120  }
 121
 0122  public UIMode GetUIMode() {
 0123    return curMode;
 0124  }
 125
 0126  public void SetAgentPanelText(string text) {
 0127    agentPanelText.text = text;
 0128  }
 129
 0130  public string GetSwarmPanelText() {
 0131    return agentPanelText.text;
 0132  }
 133
 0134  private void UpdateSwarmPanel() {
 0135    string agentPanelText = "";
 0136    foreach (Agent agent in SimManager.Instance.GetActiveAgents()) {
 0137      string jobText = agent.name + "| Phase: " + agent.GetFlightPhase().ToString();
 0138      agentPanelText += jobText + "\n";
 0139    }
 0140    SetAgentPanelText(agentPanelText);
 0141  }
 142
 0143  private void UpdateSimTimeText() {
 0144    simTimeText.text =
 145        "Elapsed Sim Time: " + SimManager.Instance.GetElapsedSimulationTime().ToString("F2");
 0146    float expectedSimTimeAdvance = Time.unscaledDeltaTime * Time.timeScale;
 0147    float actualSimTimeAdvance = Time.deltaTime;
 148
 149    // Allow a small epsilon to account for floating-point precision errors
 0150    if (actualSimTimeAdvance < expectedSimTimeAdvance - 0.001f) {
 0151      simTimeText.text += "\nThrottling time to meet physics rate";
 0152    }
 0153  }
 154
 0155  private void UpdateTotalCostText() {
 0156    double interceptorCost = SimManager.Instance.GetCostLaunchedInterceptors();
 0157    double threatCost = SimManager.Instance.GetCostDestroyedThreats();
 0158    double netCost = interceptorCost - threatCost;
 159
 0160    interceptorCostText.text = $"Interceptors\n(launched)\n${FormatCost(interceptorCost)}";
 0161    threatCostText.text = $"Threats\n(destroyed)\n${FormatCost(threatCost)}";
 0162    netCostText.text = $"Cost\ndifference\n${FormatCost(netCost)}";
 0163    if (netCost < 0) {
 0164      netCostText.color = Color.green;
 0165    } else {
 0166      netCostText.color = Color.red;
 0167    }
 0168  }
 169
 0170  private string FormatCost(double cost) {
 0171    double absCost = Math.Abs(cost);
 0172    if (absCost >= 1e9)
 0173      return $"{cost / 1e9:F2}B";
 0174    if (absCost >= 1e6)
 0175      return $"{cost / 1e6:F2}M";
 0176    if (absCost >= 1e3)
 0177      return $"{cost / 1e3:F2}k";
 0178    return $"{cost:F2}";
 0179  }
 180
 0181  private void RegisterSimulationEnded() {
 0182    _intrRemainCount = 0;
 0183    _thrtRemainCount = 0;
 0184    _intrHitCount = 0;
 0185    _intrMissCount = 0;
 0186    UpdateSummaryText();
 0187  }
 188
 0189  private void UpdateSummaryText() {
 0190    intrRemainTextHandle.text = _intrRemainCount.ToString();
 0191    thrtRemainTextHandle.text = _thrtRemainCount.ToString();
 0192    intrHitTextHandle.text = _intrHitCount.ToString();
 0193    intrMissTextHandle.text = _intrMissCount.ToString();
 0194  }
 195
 0196  private void RegisterNewInterceptor(Interceptor interceptor) {
 0197    _intrRemainCount++;
 0198    interceptor.OnInterceptHit += RegisterInterceptorHit;
 0199    interceptor.OnInterceptMiss += RegisterInterceptorMiss;
 0200    interceptor.OnTerminated += RegisterAgentTerminated;
 0201    UpdateSummaryText();
 0202  }
 203
 0204  private void RegisterNewThreat(Threat threat) {
 0205    _thrtRemainCount++;
 0206    threat.OnTerminated += RegisterAgentTerminated;
 0207    UpdateSummaryText();
 0208  }
 209
 0210  private void RegisterAgentTerminated(Agent agent) {
 0211    if (agent is Interceptor) {
 0212      _intrRemainCount--;
 0213    } else if (agent is Threat) {
 0214      _thrtRemainCount--;
 0215    }
 0216    UpdateSummaryText();
 0217  }
 218
 0219  private void RegisterInterceptorHit(Interceptor interceptor, Threat threat) {
 0220    _intrHitCount++;
 0221    UpdateSummaryText();
 0222  }
 223
 0224  private void RegisterInterceptorMiss(Interceptor interceptor, Threat threat) {
 0225    _intrMissCount++;
 0226    UpdateSummaryText();
 0227  }
 228
 229  // Update is called once per frame
 0230  void Update() {
 231    // UpdateSwarmPanel();
 0232    UpdateSimTimeText();
 0233    UpdateTotalCostText();
 0234  }
 235}
 236
 237public enum UIMode { NONE, BUILD, MINE }