| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public class StatsPanel : MonoBehaviour |
| | 9 | | { |
| | 10 | | public RectTransform row; |
| | 11 | | public RectTransform column; |
| 0 | 12 | | public float updateInterval = 0.5f; |
| | 13 | |
|
| | 14 | | List<Column> columns; |
| | 15 | | List<Row> rows; |
| | 16 | | Dictionary<(int, int), Text> mapToText; |
| 0 | 17 | | 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 | | { |
| 0 | 26 | | 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 |
| 0 | 32 | | 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 | |
|
| 0 | 47 | | CellMap tmpCellMap = new CellMap(); |
| | 48 | |
|
| | 49 | | private void OnEnable() |
| | 50 | | { |
| 0 | 51 | | StartCoroutine(UpdatePanel()); |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | private void OnDisable() |
| | 55 | | { |
| 0 | 56 | | StopCoroutine(UpdatePanel()); |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public void SetCellText(int x, int y, string text, bool instantUpdate = false) |
| | 60 | | { |
| 0 | 61 | | if (instantUpdate) |
| | 62 | | { |
| 0 | 63 | | SetCellText_Internal(x, y, text); |
| 0 | 64 | | } |
| | 65 | | else |
| | 66 | | { |
| 0 | 67 | | updateQueue[(x, y)] = text; |
| | 68 | | } |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | private void SetCellText_Internal(int x, int y, string text) |
| | 72 | | { |
| 0 | 73 | | tmpCellMap.x = x; |
| 0 | 74 | | tmpCellMap.y = y; |
| | 75 | |
|
| 0 | 76 | | if (!DictionaryContainsColAndRow(mapToText, x, y)) |
| | 77 | | { |
| 0 | 78 | | return; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | Text textComponent = mapToText[(x, y)]; |
| | 82 | |
|
| 0 | 83 | | if (textComponent.text != text) |
| | 84 | | { |
| 0 | 85 | | textComponent.text = text; |
| | 86 | | } |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | public void PopulateTable(int width, int height, float firstColWidth = 240, float secondColWidth = 80) |
| | 90 | | { |
| 0 | 91 | | rows = new List<Row>(height); |
| 0 | 92 | | columns = new List<Column>(width); |
| 0 | 93 | | mapToText = new Dictionary<(int, int), Text>(); |
| | 94 | |
|
| | 95 | | //NOTE(Brian): I'll reuse the same columns array reference for all the rows. |
| 0 | 96 | | for (int x = 0; x < width; x++) |
| | 97 | | { |
| 0 | 98 | | columns.Add(new Column()); |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | for (int y = 0; y < height; y++) |
| | 102 | | { |
| 0 | 103 | | rows.Add(new Row() {tableColumns = columns}); |
| 0 | 104 | | GameObject rowGameObject = Instantiate(row.gameObject, row.parent); |
| 0 | 105 | | rows[y].container = rowGameObject; |
| | 106 | |
|
| 0 | 107 | | for (int x = 0; x < width; x++) |
| | 108 | | { |
| 0 | 109 | | GameObject columnGameObject = Instantiate(column.gameObject, rowGameObject.transform); |
| 0 | 110 | | Text textComponent = columnGameObject.GetComponent<Text>(); |
| 0 | 111 | | columns[x].text = textComponent; |
| 0 | 112 | | textComponent.text = ""; |
| 0 | 113 | | mapToText.Add((x, y), textComponent); |
| | 114 | |
|
| 0 | 115 | | if (x == 0) |
| | 116 | | { |
| 0 | 117 | | columnGameObject.GetComponent<LayoutElement>().preferredWidth = firstColWidth; |
| 0 | 118 | | columnGameObject.GetComponent<LayoutElement>().minWidth = firstColWidth; |
| 0 | 119 | | } |
| | 120 | | else |
| | 121 | | { |
| 0 | 122 | | columnGameObject.GetComponent<LayoutElement>().preferredWidth = secondColWidth; |
| 0 | 123 | | columnGameObject.GetComponent<LayoutElement>().minWidth = secondColWidth; |
| | 124 | | } |
| | 125 | | } |
| | 126 | | } |
| | 127 | |
|
| | 128 | | //NOTE(Brian): I deactivate the base column/row objects used for instantiation. |
| 0 | 129 | | column.gameObject.SetActive(false); |
| 0 | 130 | | row.gameObject.SetActive(false); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | private bool DictionaryContainsColumn(Dictionary<(int, int), Text> dictionary, int col) |
| | 134 | | { |
| 0 | 135 | | 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 |
| 0 | 141 | | return dictionary.Any(x => x.Key.Item1 == col && x.Key.Item2 == row); |
| | 142 | | } |
| | 143 | |
|
| | 144 | | private IEnumerator UpdatePanel() |
| | 145 | | { |
| 0 | 146 | | while (true) |
| | 147 | | { |
| 0 | 148 | | foreach (var keyValuePair in updateQueue) |
| | 149 | | { |
| 0 | 150 | | SetCellText_Internal(keyValuePair.Key.Item1, keyValuePair.Key.Item2, keyValuePair.Value); |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | updateQueue.Clear(); |
| | 154 | |
|
| 0 | 155 | | yield return WaitForSecondsCache.Get(updateInterval); |
| | 156 | | } |
| | 157 | | } |
| | 158 | | } |