< Summary

Class:UISelectableEntry
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/UI/UISelectableEntry.cs
Covered lines:0
Uncovered lines:89
Coverable lines:89
Total lines:136
Line coverage:0% (0 of 89)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:15
Method coverage:0% (0 of 15)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UISelectableEntry()0%2100%
UISelectableEntry()0%2100%
Awake()0%2100%
SetClickCallback(...)0%2100%
OnPointerEnter(...)0%6200%
OnPointerDown(...)0%12300%
OnPointerExit(...)0%6200%
SetIsSelectable(...)0%2100%
GetIsSelectable()0%2100%
AddChildEntry(...)0%6200%
SetParent(...)0%2100%
SetChildEntries(...)0%2100%
GetChildEntries()0%2100%
SetTextContent(...)0%12300%
GetTextTransform(...)0%12300%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using System;
 3using UnityEngine;
 4using UnityEngine.UI;
 5using TMPro;
 6using UnityEngine.EventSystems;
 7using System.Linq;
 8public class UISelectableEntry : EventTrigger {
 09  private List<UISelectableEntry> children = null!;
 010  private List<string> textContent = null!;
 11
 012  private UIDialog parentDialog = null!;
 13
 014  private RectTransform rectTransform = null!;
 15
 016  private Image image = null!;
 17
 18  // Replace the single TextMeshProUGUI with a list to hold multiple columns
 019  private List<TextMeshProUGUI> textHandles = null!;
 20
 021  private bool isSelectable = true;
 22
 023  private static Color baseColor = new Color32(31, 31, 45, 140);
 24
 025  private Action<object> OnClickCallback = null!;
 026  private object callbackArgument = null!;
 27
 028  public void Awake() {
 029    rectTransform = gameObject.AddComponent<RectTransform>();
 30
 031    image = gameObject.AddComponent<Image>();
 032    image.type = Image.Type.Sliced;
 033    image.color = baseColor;
 34
 35    // Initialize the list for text handles
 036    textHandles = new List<TextMeshProUGUI>();
 037  }
 38
 039  public void SetClickCallback(Action<object> callback, object argument) {
 040    OnClickCallback = callback;
 041    callbackArgument = argument;
 042  }
 43
 044  public override void OnPointerEnter(PointerEventData eventData) {
 045    if (isSelectable)
 046      image.color = baseColor + new Color32(20, 20, 20, 40);
 047    base.OnPointerEnter(eventData);
 048  }
 49
 050  public override void OnPointerDown(PointerEventData eventData) {
 051    if (isSelectable && OnClickCallback != null) {
 052      OnClickCallback(callbackArgument);
 053      image.color = baseColor + new Color32(40, 40, 40, 40);
 054    }
 055    base.OnPointerClick(eventData);
 056  }
 57
 058  public override void OnPointerExit(PointerEventData eventData) {
 059    if (isSelectable)
 060      image.color = baseColor;
 061    base.OnPointerExit(eventData);
 062  }
 63
 064  public void SetIsSelectable(bool isSelectable) {
 065    image.enabled = isSelectable;
 066    this.isSelectable = isSelectable;
 067  }
 68
 069  public bool GetIsSelectable() {
 070    return isSelectable;
 071  }
 72
 073  public void AddChildEntry(UISelectableEntry child) {
 074    if (children == null) {
 075      children = new List<UISelectableEntry>();
 076    }
 077    children.Add(child);
 078  }
 79
 080  public void SetParent(UIDialog parentDialog) {
 081    this.parentDialog = parentDialog;
 082  }
 83
 084  public void SetChildEntries(List<UISelectableEntry> children) {
 085    this.children = children;
 086  }
 87
 88  // Get the children of this entry
 089  public List<UISelectableEntry> GetChildEntries() {
 090    return children;
 091  }
 92
 093  public void SetTextContent(List<string> textContent) {
 094    this.textContent = textContent;
 95
 96    // Clear existing text handles
 097    foreach (var textHandle in textHandles) {
 098      Destroy(textHandle.gameObject);
 099    }
 0100    textHandles.Clear();
 101
 0102    int columnCount = textContent.Count;
 0103    float totalWidth = rectTransform.rect.width;
 0104    float columnWidth = totalWidth / columnCount;
 105
 0106    for (int i = 0; i < columnCount; ++i) {
 107      // Create a new TextMeshProUGUI for each column
 0108      var textHandle = Instantiate(Resources.Load<GameObject>("Prefabs/EmptyObject"), rectTransform)
 109                           .AddComponent<TextMeshProUGUI>();
 0110      textHandle.gameObject.name = $"UISelectableEntry::Text_{i}";
 0111      textHandle.fontSize = 6;
 0112      textHandle.font = UIManager.Instance.GlobalFont;
 0113      textHandle.alignment = TextAlignmentOptions.MidlineLeft;
 114
 115      // Set the RectTransform for proper alignment
 0116      var textRect = textHandle.GetComponent<RectTransform>();
 0117      textRect.anchorMin = new Vector2((float)i / columnCount, 0);
 0118      textRect.anchorMax = new Vector2((float)(i + 1) / columnCount, 1);
 0119      textRect.offsetMin = Vector2.zero;
 0120      textRect.offsetMax = Vector2.zero;
 121
 122      // Set the text content
 0123      textHandle.text = textContent[i];
 124
 125      // Add to the list of text handles
 0126      textHandles.Add(textHandle);
 0127    }
 0128  }
 129
 0130  public RectTransform GetTextTransform(int index) {
 0131    if (index >= 0 && index < textHandles.Count) {
 0132      return textHandles[index].GetComponent<RectTransform>();
 133    }
 0134    return null;
 0135  }
 136}