< Summary

Class:UIDialog
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/UI/UIDialog.cs
Covered lines:0
Uncovered lines:127
Coverable lines:127
Total lines:212
Line coverage:0% (0 of 127)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:18
Method coverage:0% (0 of 18)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UIDialog()0%2100%
Start()0%20400%
GetContentHandle()0%2100%
IsOpen()0%2100%
OnEnable()0%2100%
OnDisable()0%2100%
GetTabWidth()0%2100%
GetTabHeight()0%2100%
GetTitleBarHeight()0%2100%
AddDialogTab(...)0%2100%
AddTabButton(...)0%2100%
AddTabText(...)0%2100%
CreateSelectableEntry()0%2100%
ClearDialogEntries()0%20400%
SetDialogEntries(...)0%6200%
SetDialogEntries(...)0%2100%
RecursiveContentPrint(...)0%12300%
Update()0%2100%

File(s)

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

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using TMPro;
 5using System;
 6using UnityEngine.UI;
 7
 8public class UIDialog : MonoBehaviour {
 9  [SerializeField]
 10  private string dialogTitle;
 11
 12  [SerializeField]
 13
 14  private TextMeshProUGUI dialogTitleHandle;
 15  [SerializeField]
 16  private RectTransform contentHandle;
 17
 18  /// Tabs
 19  [SerializeField]
 020  private float _tabWidth = 15f;
 21  [SerializeField]
 022  private float _tabHeight = 8f;
 23  // List of dialog tabs.
 24  private List<GameObject> dialogTabs;
 25
 26  /// Entries
 27  private List<UISelectableEntry> entries;
 28
 29  [SerializeField]
 030  private float entryHeight = 8f;
 31  [SerializeField]
 032  private float entryIndentWidth = 4f;
 33
 34  private List<UISelectableEntry> cleanupPool;
 35
 36  private bool isOpen;
 37
 038  public virtual void Start() {
 039    dialogTitleHandle.text = dialogTitle;
 040    dialogTitleHandle.font = UIManager.Instance.GlobalFont;
 041    isOpen = gameObject.activeSelf;
 042    if (dialogTabs != null) {
 043      foreach (GameObject tab in dialogTabs) {
 044        Destroy(tab);
 045      }
 046    }
 47
 048    if (cleanupPool != null) {
 049      ClearDialogEntries();
 050    }
 051    dialogTabs = new List<GameObject>();
 052    entries = new List<UISelectableEntry>();
 053    cleanupPool = new List<UISelectableEntry>();
 054  }
 55
 056  internal RectTransform GetContentHandle() {
 057    return contentHandle;
 058  }
 59
 060  public bool IsOpen() {
 061    return isOpen;
 062  }
 63
 064  protected virtual void OnEnable() {
 065    isOpen = true;
 066  }
 067  protected virtual void OnDisable() {
 068    isOpen = false;
 069  }
 70
 071  public float GetTabWidth() {
 072    return _tabWidth;
 073  }
 074  public float GetTabHeight() {
 075    return _tabHeight;
 076  }
 77
 78  /// Returns the height of the dialog title bar.
 079  public float GetTitleBarHeight() {
 080    return dialogTitleHandle.rectTransform.sizeDelta.y;
 081  }
 82
 83  /// Adds a new tab to the dialog. When clicked, it will call the given callback.
 084  public void AddDialogTab(string tabName, Action onClick) {
 085    dialogTabs.Add(AddTabButton(tabName, onClick));
 086  }
 87
 88  /// Add the tab button to the right of the existing tabs.
 089  private GameObject AddTabButton(string tabName, Action onClick) {
 090    GameObject tabButton = new GameObject("TabButton", typeof(RectTransform));
 91
 92    // Pass false in the second parameter to keep local scale at (1, 1, 1).
 093    tabButton.transform.SetParent(transform, false);
 94
 95    // Optional: Force localScale to one, if you want to be explicit.
 96    // tabButton.transform.localScale = Vector3.one;
 97
 98    // RectTransform setup.
 099    RectTransform rTransform = tabButton.GetComponent<RectTransform>();
 0100    rTransform.anchorMin = new Vector2(0, 1);
 0101    rTransform.anchorMax = new Vector2(0, 1);
 0102    rTransform.pivot = new Vector2(0, 1);
 0103    rTransform.sizeDelta = new Vector2(_tabWidth, _tabHeight);
 104
 105    // Calculate anchoredPosition based on how many tabs exist.
 0106    rTransform.anchoredPosition = new Vector2(_tabWidth * dialogTabs.Count, -(GetTitleBarHeight()));
 107
 108    // Add the onClick callback to the button.
 0109    Button button = tabButton.AddComponent<Button>();
 0110    button.onClick.AddListener(() => onClick());
 111
 112    // Add the image to the button and link it to the tab.
 0113    button.targetGraphic = tabButton.AddComponent<Image>();
 114
 115    // Create the child text object.
 0116    AddTabText(tabName, tabButton);
 117
 0118    return tabButton;
 0119  }
 120
 121  /// Add text as a child of the tab's button object.
 0122  private void AddTabText(string tabName, GameObject tabButton) {
 0123    GameObject tabText = new GameObject("TabText", typeof(RectTransform));
 124
 125    // Again, pass false to avoid messing up the new child's scale.
 0126    tabText.transform.SetParent(tabButton.transform, false);
 127
 128    // Optional: Force localScale to one, if you want to be explicit.
 129    // tabText.transform.localScale = Vector3.one;
 130
 131    // RectTransform setup.
 0132    RectTransform textRectTransform = tabText.GetComponent<RectTransform>();
 0133    textRectTransform.anchorMin = new Vector2(0.5f, 0.5f);
 0134    textRectTransform.anchorMax = new Vector2(0.5f, 0.5f);
 0135    textRectTransform.pivot = new Vector2(0.5f, 0.5f);
 0136    textRectTransform.sizeDelta = new Vector2(_tabWidth, _tabHeight);
 0137    textRectTransform.anchoredPosition = new Vector2(0, 0);
 138
 139    // Create the TextMeshProUGUI component.
 0140    TextMeshProUGUI buttonText = tabText.AddComponent<TextMeshProUGUI>();
 0141    buttonText.text = tabName;
 0142    buttonText.font = UIManager.Instance.GlobalFont;
 0143    buttonText.fontSize = 6;
 0144    buttonText.color = Color.black;
 0145    buttonText.alignment = TextAlignmentOptions.Center;
 0146    buttonText.verticalAlignment = VerticalAlignmentOptions.Middle;
 0147  }
 148
 0149  public virtual UISelectableEntry CreateSelectableEntry() {
 150    // Create a new entry object with content handle as parent.
 0151    GameObject go = Instantiate(Resources.Load<GameObject>("Prefabs/EmptyObject"), contentHandle);
 0152    go.name = "UISelectableEntry";
 0153    UISelectableEntry entry = go.AddComponent<UISelectableEntry>();
 0154    entry.SetParent(this);
 155    // add to cleanup pool so we can clear later in clear dialog entries.
 0156    cleanupPool.Add(entry);
 0157    return entry;
 0158  }
 159
 0160  public void ClearDialogEntries() {
 0161    if (cleanupPool == null)
 0162      return;
 0163    foreach (UISelectableEntry entry in cleanupPool) {
 0164      GameObject.Destroy(entry.gameObject);
 0165    }
 0166    cleanupPool.Clear();
 0167    if (entries != null)
 0168      entries.Clear();
 0169  }
 170
 171  /// Clears, sets, and prints the dialog entries in the order they were added
 0172  public virtual void SetDialogEntries(List<UISelectableEntry> entries) {
 0173    this.entries = entries;
 174    // Calculate total height of the content.
 0175    float heightHead = -1 * GetTabHeight();
 0176    int count = 0;
 0177    foreach (UISelectableEntry entry in this.entries) {
 0178      (heightHead, count) = RecursiveContentPrint(entry, 1, heightHead, count);
 0179    }
 0180    contentHandle.sizeDelta =
 181        new Vector2(contentHandle.sizeDelta.x, count * entryHeight + Mathf.Abs(heightHead));
 0182  }
 183
 0184  public virtual void SetDialogEntries(UISelectableEntry entry) {
 0185    SetDialogEntries(new List<UISelectableEntry>() { entry });
 0186  }
 187
 188  private (float, int)
 0189      RecursiveContentPrint(UISelectableEntry entry, int depth, float heightHead, int count) {
 0190    RectTransform rTransform = entry.GetComponent<RectTransform>();
 0191    rTransform.anchorMin = new Vector2(0, 1);
 0192    rTransform.anchorMax = new Vector2(1, 1);
 0193    rTransform.pivot = new Vector2(0.5f, 1f);
 194
 0195    rTransform.anchoredPosition = new Vector2(0, heightHead);  // Positioning from top.
 0196    rTransform.sizeDelta = new Vector2(0, entryHeight);
 0197    float padding = 5f;
 0198    rTransform.SetRight(padding);
 0199    rTransform.SetLeft(padding);
 0200    entry.GetTextTransform(0).anchoredPosition = new Vector2(entryIndentWidth * depth, 0);
 0201    heightHead -= entryHeight;
 0202    ++count;
 0203    if (entry.GetChildEntries() != null) {
 0204      foreach (UISelectableEntry child in entry.GetChildEntries()) {
 0205        (heightHead, count) = RecursiveContentPrint(child, depth + 1, heightHead, count);
 0206      }
 0207    }
 0208    return (heightHead, count);
 0209  }
 210
 0211  void Update() {}
 212}