< Summary

Class:UIWindow
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/UI/Windows/UIWindow.cs
Covered lines:0
Uncovered lines:58
Coverable lines:58
Total lines:94
Line coverage:0% (0 of 58)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:9
Method coverage:0% (0 of 9)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UIWindow()0%2100%
OnEnable()0%2100%
OnDisable()0%2100%
ToggleWindow()0%2100%
CloseWindow()0%2100%
Reset()0%2100%
Start()0%12300%
CreateWindowTitle()0%2100%
CreateCloseButton()0%2100%

File(s)

/github/workspace/Assets/Scripts/UI/Windows/UIWindow.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System;
 3using UnityEngine;
 4using UnityEngine.UI;
 5using TMPro;
 6using UnityEngine.EventSystems;
 7
 8[RequireComponent(typeof(UIElementMouseCapturer))]
 9[RequireComponent(typeof(UIElementDragger))]
 10[RequireComponent(typeof(Image))]
 11public class UIWindow : MonoBehaviour {
 12  // Window title.
 13  [SerializeField]
 014  private string windowTitle = "Window";
 15
 16  // Close button.
 17  private GameObject closeButton;
 18  [SerializeField]
 19  private CloseButtonCallback closeButtonCallback;
 20  [Serializable]
 21  private enum CloseButtonCallback { CLOSE_WINDOW, TOGGLE_WINDOW }
 22
 23  private bool isOpen;
 024  private void OnEnable() {
 025    isOpen = true;
 026  }
 027  private void OnDisable() {
 028    isOpen = false;
 029  }
 30
 031  public void ToggleWindow() {
 032    gameObject.SetActive(!gameObject.activeSelf);
 033  }
 34
 035  public void CloseWindow() {
 036    Destroy(gameObject);
 037    isOpen = false;
 038  }
 39
 40  /// Called when the UIWindow component is created in the editor.
 41  /// This is used to configure the image component.
 042  private void Reset() {
 043    GetComponent<Image>().color = new Color32(18, 16, 28, 125);
 044  }
 45
 046  public virtual void Start() {
 047    isOpen = gameObject.activeSelf;
 048    CreateCloseButton();
 049    CreateWindowTitle();
 050    if (closeButtonCallback == CloseButtonCallback.CLOSE_WINDOW)
 051      closeButton.AddComponent<Button>().onClick.AddListener(CloseWindow);
 052    else if (closeButtonCallback == CloseButtonCallback.TOGGLE_WINDOW)
 053      closeButton.AddComponent<Button>().onClick.AddListener(ToggleWindow);
 054  }
 55
 056  private void CreateWindowTitle() {
 057    GameObject windowTitleObject = new GameObject("WindowTitle", typeof(RectTransform));
 058    windowTitleObject.transform.SetParent(transform);
 059    TextMeshProUGUI windowTitleHandle = windowTitleObject.AddComponent<TextMeshProUGUI>();
 060    windowTitleHandle.text = windowTitle;
 061    windowTitleHandle.font = UIManager.Instance.GlobalFont;
 062    windowTitleHandle.fontSize = 14;
 063    windowTitleHandle.color = Color.white;
 064    windowTitleHandle.alignment = TextAlignmentOptions.Left;
 065    windowTitleHandle.rectTransform.anchorMin = new Vector2(0, 1);
 066    windowTitleHandle.rectTransform.anchorMax = new Vector2(1, 1);
 067    windowTitleHandle.rectTransform.pivot = new Vector2(0, 1);
 068    windowTitleHandle.rectTransform.sizeDelta = new Vector2(0, 30);
 069    windowTitleHandle.rectTransform.anchoredPosition = new Vector2(5, 0);
 070    windowTitleHandle.rectTransform.SetRight(30);  // Give spacing to the close button
 071  }
 72
 73  /// Create the close [x] button in the top right corner of the window.
 074  private void CreateCloseButton() {
 075    closeButton = new GameObject("CloseButton", typeof(RectTransform));
 076    RectTransform buttonTransform = closeButton.GetComponent<RectTransform>();
 077    buttonTransform.SetParent(transform);
 78    // Anchor top right.
 079    buttonTransform.anchorMin = new Vector2(1, 1);
 080    buttonTransform.anchorMax = new Vector2(1, 1);
 081    buttonTransform.pivot = new Vector2(1, 1);
 82    // Position top right.
 083    buttonTransform.anchoredPosition = new Vector2(0, 0);
 84    // Size.
 085    buttonTransform.sizeDelta = new Vector2(30, 30);
 86    // Add button component.
 087    TextMeshProUGUI textbox = closeButton.AddComponent<TextMeshProUGUI>();
 088    textbox.text = "X";
 089    textbox.font = UIManager.Instance.GlobalFont;
 090    textbox.fontSize = 12;
 091    textbox.alignment = TextAlignmentOptions.Center;
 092    textbox.verticalAlignment = VerticalAlignmentOptions.Middle;
 093  }
 94}