< Summary

Class:StatsPanel
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/Debugging/Panels/StatsPanel.cs
Covered lines:0
Uncovered lines:51
Coverable lines:51
Total lines:146
Line coverage:0% (0 of 51)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
StatsPanel()0%2100%
Equals(...)0%6200%
GetHashCode()0%2100%
OnEnable()0%2100%
OnDisable()0%2100%
SetCellText(...)0%6200%
SetCellText_Internal(...)0%12300%
PopulateTable(...)0%30500%
DictionaryContainsColumn(...)0%2100%
DictionaryContainsColAndRow(...)0%2100%
UpdatePanel()0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/Debugging/Panels/StatsPanel.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using System.Linq;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8public class StatsPanel : MonoBehaviour
 9{
 10    public RectTransform row;
 11    public RectTransform column;
 012    public float updateInterval = 0.5f;
 13
 14    List<Column> columns;
 15    List<Row> rows;
 16    Dictionary<(int, int), Text> mapToText;
 017    readonly Dictionary<(int, int), string> updateQueue = new Dictionary<(int, int), string>();
 18
 19    public class CellMap : IEquatable<CellMap>
 20    {
 21        public int x;
 22        public int y;
 23
 024        public bool Equals(CellMap other) { return x == other.x && y == other.y; }
 25
 26        public override int GetHashCode()
 27        {
 28            //NOTE(Brian): taken from https://stackoverflow.com/questions/682438/hash-function-providing-unique-uint-fro
 029            return (x * 0x1f1f1f1f) ^ y;
 30        }
 31    }
 32
 33    public class Column
 34    {
 35        public Text text;
 36    }
 37
 38    public class Row
 39    {
 40        public List<Column> tableColumns;
 41        public GameObject container;
 42    }
 43
 044    CellMap tmpCellMap = new CellMap();
 45
 046    private void OnEnable() { StartCoroutine(UpdatePanel()); }
 47
 048    private void OnDisable() { StopCoroutine(UpdatePanel()); }
 49
 50    public void SetCellText(int x, int y, string text, bool instantUpdate = false)
 51    {
 052        if (instantUpdate)
 53        {
 054            SetCellText_Internal(x, y, text);
 055        }
 56        else
 57        {
 058            updateQueue[(x, y)] = text;
 59        }
 060    }
 61
 62    private void SetCellText_Internal(int x, int y, string text)
 63    {
 064        tmpCellMap.x = x;
 065        tmpCellMap.y = y;
 66
 067        if (!DictionaryContainsColAndRow(mapToText, x, y))
 68        {
 069            return;
 70        }
 71
 072        Text textComponent = mapToText[(x, y)];
 73
 074        if (textComponent.text != text)
 75        {
 076            textComponent.text = text;
 77        }
 078    }
 79
 80    public void PopulateTable(int width, int height, float firstColWidth = 240, float secondColWidth = 80)
 81    {
 082        rows = new List<Row>(height);
 083        columns = new List<Column>(width);
 084        mapToText = new Dictionary<(int, int), Text>();
 85
 86        //NOTE(Brian): I'll reuse the same columns array reference for all the rows.
 087        for (int x = 0; x < width; x++)
 88        {
 089            columns.Add(new Column());
 90        }
 91
 092        for (int y = 0; y < height; y++)
 93        {
 094            rows.Add(new Row() { tableColumns = columns });
 095            GameObject rowGameObject = Instantiate(row.gameObject, row.parent);
 096            rows[y].container = rowGameObject;
 97
 098            for (int x = 0; x < width; x++)
 99            {
 0100                GameObject columnGameObject = Instantiate(column.gameObject, rowGameObject.transform);
 0101                Text textComponent = columnGameObject.GetComponent<Text>();
 0102                columns[x].text = textComponent;
 0103                textComponent.text = "";
 0104                mapToText.Add((x, y), textComponent);
 105
 0106                if (x == 0)
 107                {
 0108                    columnGameObject.GetComponent<LayoutElement>().preferredWidth = firstColWidth;
 0109                    columnGameObject.GetComponent<LayoutElement>().minWidth = firstColWidth;
 0110                }
 111                else
 112                {
 0113                    columnGameObject.GetComponent<LayoutElement>().preferredWidth = secondColWidth;
 0114                    columnGameObject.GetComponent<LayoutElement>().minWidth = secondColWidth;
 115                }
 116            }
 117        }
 118
 119        //NOTE(Brian): I deactivate the base column/row objects used for instantiation.
 0120        column.gameObject.SetActive(false);
 0121        row.gameObject.SetActive(false);
 0122    }
 123
 0124    private bool DictionaryContainsColumn(Dictionary<(int, int), Text> dictionary, int col) { return dictionary.Any(x =>
 125
 126    private bool DictionaryContainsColAndRow(Dictionary<(int, int), Text> dictionary, int col, int row)
 127    {
 128        //It's faster to check Col again than using DictionaryContainsColumn method
 0129        return dictionary.Any(x => x.Key.Item1 == col && x.Key.Item2 == row);
 130    }
 131
 132    private IEnumerator UpdatePanel()
 133    {
 0134        while (true)
 135        {
 0136            foreach (var keyValuePair in updateQueue)
 137            {
 0138                SetCellText_Internal(keyValuePair.Key.Item1, keyValuePair.Key.Item2, keyValuePair.Value);
 139            }
 140
 0141            updateQueue.Clear();
 142
 0143            yield return WaitForSecondsCache.Get(updateInterval);
 144        }
 145    }
 146}