< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
StatsPanel()0%110100%
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/DCL/WorldRuntime/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;
 412    public float updateInterval = 0.5f;
 13
 14    List<Column> columns;
 15    List<Row> rows;
 16    Dictionary<(int, int), Text> mapToText;
 417    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
 24        public bool Equals(CellMap other)
 25        {
 026            return x == other.x && y == other.y;
 27        }
 28
 29        public override int GetHashCode()
 30        {
 31            //NOTE(Brian): taken from https://stackoverflow.com/questions/682438/hash-function-providing-unique-uint-fro
 032            return (x * 0x1f1f1f1f) ^ y;
 33        }
 34    }
 35
 36    public class Column
 37    {
 38        public Text text;
 39    }
 40
 41    public class Row
 42    {
 43        public List<Column> tableColumns;
 44        public GameObject container;
 45    }
 46
 447    CellMap tmpCellMap = new CellMap();
 48
 49    private void OnEnable()
 50    {
 051        StartCoroutine(UpdatePanel());
 052    }
 53
 54    private void OnDisable()
 55    {
 056        StopCoroutine(UpdatePanel());
 057    }
 58
 59    public void SetCellText(int x, int y, string text, bool instantUpdate = false)
 60    {
 061        if (instantUpdate)
 62        {
 063            SetCellText_Internal(x, y, text);
 64        }
 65        else
 66        {
 067            updateQueue[(x, y)] = text;
 68        }
 069    }
 70
 71    private void SetCellText_Internal(int x, int y, string text)
 72    {
 073        tmpCellMap.x = x;
 074        tmpCellMap.y = y;
 75
 076        if (!DictionaryContainsColAndRow(mapToText, x, y))
 77        {
 078            return;
 79        }
 80
 081        Text textComponent = mapToText[(x, y)];
 82
 083        if (textComponent.text != text)
 84        {
 085            textComponent.text = text;
 86        }
 087    }
 88
 89    public void PopulateTable(int width, int height, float firstColWidth = 240, float secondColWidth = 80)
 90    {
 091        rows = new List<Row>(height);
 092        columns = new List<Column>(width);
 093        mapToText = new Dictionary<(int, int), Text>();
 94
 95        //NOTE(Brian): I'll reuse the same columns array reference for all the rows.
 096        for (int x = 0; x < width; x++)
 97        {
 098            columns.Add(new Column());
 99        }
 100
 0101        for (int y = 0; y < height; y++)
 102        {
 0103            rows.Add(new Row() {tableColumns = columns});
 0104            GameObject rowGameObject = Instantiate(row.gameObject, row.parent);
 0105            rows[y].container = rowGameObject;
 106
 0107            for (int x = 0; x < width; x++)
 108            {
 0109                GameObject columnGameObject = Instantiate(column.gameObject, rowGameObject.transform);
 0110                Text textComponent = columnGameObject.GetComponent<Text>();
 0111                columns[x].text = textComponent;
 0112                textComponent.text = "";
 0113                mapToText.Add((x, y), textComponent);
 114
 0115                if (x == 0)
 116                {
 0117                    columnGameObject.GetComponent<LayoutElement>().preferredWidth = firstColWidth;
 0118                    columnGameObject.GetComponent<LayoutElement>().minWidth = firstColWidth;
 119                }
 120                else
 121                {
 0122                    columnGameObject.GetComponent<LayoutElement>().preferredWidth = secondColWidth;
 0123                    columnGameObject.GetComponent<LayoutElement>().minWidth = secondColWidth;
 124                }
 125            }
 126        }
 127
 128        //NOTE(Brian): I deactivate the base column/row objects used for instantiation.
 0129        column.gameObject.SetActive(false);
 0130        row.gameObject.SetActive(false);
 0131    }
 132
 133    private bool DictionaryContainsColumn(Dictionary<(int, int), Text> dictionary, int col)
 134    {
 0135        return dictionary.Any(x => x.Key.Item1 == col);
 136    }
 137
 138    private bool DictionaryContainsColAndRow(Dictionary<(int, int), Text> dictionary, int col, int row)
 139    {
 140        //It's faster to check Col again than using DictionaryContainsColumn method
 0141        return dictionary.Any(x => x.Key.Item1 == col && x.Key.Item2 == row);
 142    }
 143
 144    private IEnumerator UpdatePanel()
 145    {
 0146        while (true)
 147        {
 0148            foreach (var keyValuePair in updateQueue)
 149            {
 0150                SetCellText_Internal(keyValuePair.Key.Item1, keyValuePair.Key.Item2, keyValuePair.Value);
 151            }
 152
 0153            updateQueue.Clear();
 154
 0155            yield return WaitForSecondsCache.Get(updateInterval);
 156        }
 157    }
 158}