< 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:100
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  // IsOpen property
 24  private bool isOpen;
 025  private void OnEnable() {
 026    isOpen = true;
 027  }
 028  private void OnDisable() {
 029    isOpen = false;
 030  }
 31
 032  public void ToggleWindow() {
 033    gameObject.SetActive(!gameObject.activeSelf);
 034  }
 35
 036  public void CloseWindow() {
 037    Destroy(gameObject);
 038    isOpen = false;
 039  }
 40
 41  /// <summary>
 42  /// Called when the UIWindow component is created in the editor
 43  /// We will use it to configure the image component
 44  /// </summary>
 045  private void Reset() {
 46    // 18 16 28 125
 047    GetComponent<Image>().color = new Color32(18, 16, 28, 125);
 048  }
 49
 050  public virtual void Start() {
 051    isOpen = gameObject.activeSelf;
 052    CreateCloseButton();
 053    CreateWindowTitle();
 054    if (closeButtonCallback == CloseButtonCallback.CLOSE_WINDOW)
 055      closeButton.AddComponent<Button>().onClick.AddListener(CloseWindow);
 056    else if (closeButtonCallback == CloseButtonCallback.TOGGLE_WINDOW)
 057      closeButton.AddComponent<Button>().onClick.AddListener(ToggleWindow);
 058  }
 59
 060  private void CreateWindowTitle() {
 061    GameObject windowTitleObject = new GameObject("WindowTitle", typeof(RectTransform));
 062    windowTitleObject.transform.SetParent(transform);
 063    TextMeshProUGUI windowTitleHandle = windowTitleObject.AddComponent<TextMeshProUGUI>();
 064    windowTitleHandle.text = windowTitle;
 065    windowTitleHandle.font = UIManager.Instance.Font;
 066    windowTitleHandle.fontSize = 14;
 067    windowTitleHandle.color = Color.white;
 068    windowTitleHandle.alignment = TextAlignmentOptions.Left;
 069    windowTitleHandle.rectTransform.anchorMin = new Vector2(0, 1);
 070    windowTitleHandle.rectTransform.anchorMax = new Vector2(1, 1);
 071    windowTitleHandle.rectTransform.pivot = new Vector2(0, 1);
 072    windowTitleHandle.rectTransform.sizeDelta = new Vector2(0, 30);
 073    windowTitleHandle.rectTransform.anchoredPosition = new Vector2(5, 0);
 074    windowTitleHandle.rectTransform.SetRight(30);  // Give spacing to the close button
 075  }
 76
 77  /// <summary>
 78  /// Create the close [x] button in the top right corner of the window
 79  /// </summary>
 080  private void CreateCloseButton() {
 081    closeButton = new GameObject("CloseButton", typeof(RectTransform));
 082    RectTransform buttonTransform = closeButton.GetComponent<RectTransform>();
 083    buttonTransform.SetParent(transform);
 84    // anchor top right
 085    buttonTransform.anchorMin = new Vector2(1, 1);
 086    buttonTransform.anchorMax = new Vector2(1, 1);
 087    buttonTransform.pivot = new Vector2(1, 1);
 88    // position top right
 089    buttonTransform.anchoredPosition = new Vector2(0, 0);
 90    // size
 091    buttonTransform.sizeDelta = new Vector2(30, 30);
 92    // add button component
 093    TextMeshProUGUI textbox = closeButton.AddComponent<TextMeshProUGUI>();
 094    textbox.text = "X";
 095    textbox.font = UIManager.Instance.Font;
 096    textbox.fontSize = 12;
 097    textbox.alignment = TextAlignmentOptions.Center;
 098    textbox.verticalAlignment = VerticalAlignmentOptions.Middle;
 099  }
 100}