| | 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 | |
|
| 0 | 24 | | 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 |
| 0 | 29 | | 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 | |
|
| 0 | 44 | | CellMap tmpCellMap = new CellMap(); |
| | 45 | |
|
| 0 | 46 | | private void OnEnable() { StartCoroutine(UpdatePanel()); } |
| | 47 | |
|
| 0 | 48 | | private void OnDisable() { StopCoroutine(UpdatePanel()); } |
| | 49 | |
|
| | 50 | | public void SetCellText(int x, int y, string text, bool instantUpdate = false) |
| | 51 | | { |
| 0 | 52 | | if (instantUpdate) |
| | 53 | | { |
| 0 | 54 | | SetCellText_Internal(x, y, text); |
| 0 | 55 | | } |
| | 56 | | else |
| | 57 | | { |
| 0 | 58 | | updateQueue[(x, y)] = text; |
| | 59 | | } |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | private void SetCellText_Internal(int x, int y, string text) |
| | 63 | | { |
| 0 | 64 | | tmpCellMap.x = x; |
| 0 | 65 | | tmpCellMap.y = y; |
| | 66 | |
|
| 0 | 67 | | if (!DictionaryContainsColAndRow(mapToText, x, y)) |
| | 68 | | { |
| 0 | 69 | | return; |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | Text textComponent = mapToText[(x, y)]; |
| | 73 | |
|
| 0 | 74 | | if (textComponent.text != text) |
| | 75 | | { |
| 0 | 76 | | textComponent.text = text; |
| | 77 | | } |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public void PopulateTable(int width, int height, float firstColWidth = 240, float secondColWidth = 80) |
| | 81 | | { |
| 0 | 82 | | rows = new List<Row>(height); |
| 0 | 83 | | columns = new List<Column>(width); |
| 0 | 84 | | mapToText = new Dictionary<(int, int), Text>(); |
| | 85 | |
|
| | 86 | | //NOTE(Brian): I'll reuse the same columns array reference for all the rows. |
| 0 | 87 | | for (int x = 0; x < width; x++) |
| | 88 | | { |
| 0 | 89 | | columns.Add(new Column()); |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | for (int y = 0; y < height; y++) |
| | 93 | | { |
| 0 | 94 | | rows.Add(new Row() { tableColumns = columns }); |
| 0 | 95 | | GameObject rowGameObject = Instantiate(row.gameObject, row.parent); |
| 0 | 96 | | rows[y].container = rowGameObject; |
| | 97 | |
|
| 0 | 98 | | for (int x = 0; x < width; x++) |
| | 99 | | { |
| 0 | 100 | | GameObject columnGameObject = Instantiate(column.gameObject, rowGameObject.transform); |
| 0 | 101 | | Text textComponent = columnGameObject.GetComponent<Text>(); |
| 0 | 102 | | columns[x].text = textComponent; |
| 0 | 103 | | textComponent.text = ""; |
| 0 | 104 | | mapToText.Add((x, y), textComponent); |
| | 105 | |
|
| 0 | 106 | | if (x == 0) |
| | 107 | | { |
| 0 | 108 | | columnGameObject.GetComponent<LayoutElement>().preferredWidth = firstColWidth; |
| 0 | 109 | | columnGameObject.GetComponent<LayoutElement>().minWidth = firstColWidth; |
| 0 | 110 | | } |
| | 111 | | else |
| | 112 | | { |
| 0 | 113 | | columnGameObject.GetComponent<LayoutElement>().preferredWidth = secondColWidth; |
| 0 | 114 | | columnGameObject.GetComponent<LayoutElement>().minWidth = secondColWidth; |
| | 115 | | } |
| | 116 | | } |
| | 117 | | } |
| | 118 | |
|
| | 119 | | //NOTE(Brian): I deactivate the base column/row objects used for instantiation. |
| 0 | 120 | | column.gameObject.SetActive(false); |
| 0 | 121 | | row.gameObject.SetActive(false); |
| 0 | 122 | | } |
| | 123 | |
|
| 0 | 124 | | 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 |
| 0 | 129 | | return dictionary.Any(x => x.Key.Item1 == col && x.Key.Item2 == row); |
| | 130 | | } |
| | 131 | |
|
| | 132 | | private IEnumerator UpdatePanel() |
| | 133 | | { |
| 0 | 134 | | while (true) |
| | 135 | | { |
| 0 | 136 | | foreach (var keyValuePair in updateQueue) |
| | 137 | | { |
| 0 | 138 | | SetCellText_Internal(keyValuePair.Key.Item1, keyValuePair.Key.Item2, keyValuePair.Value); |
| | 139 | | } |
| | 140 | |
|
| 0 | 141 | | updateQueue.Clear(); |
| | 142 | |
|
| 0 | 143 | | yield return WaitForSecondsCache.Get(updateInterval); |
| | 144 | | } |
| | 145 | | } |
| | 146 | | } |