| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using TMPro; |
| | 5 | | using System; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public class UIDialog : MonoBehaviour { |
| | 9 | | [SerializeField] |
| | 10 | | private string dialogTitle; |
| | 11 | |
|
| | 12 | | [SerializeField] |
| | 13 | | private TextMeshProUGUI dialogTitleHandle; |
| | 14 | | [SerializeField] |
| | 15 | | private RectTransform contentHandle; |
| | 16 | |
|
| | 17 | | /// TABS |
| | 18 | | [SerializeField] |
| 0 | 19 | | private float tabWidth = 15f; |
| | 20 | | [SerializeField] |
| 0 | 21 | | private float tabHeight = 8f; |
| | 22 | | // List of dialog tabs |
| | 23 | | private List<GameObject> dialogTabs; |
| | 24 | |
|
| | 25 | | /// ENTRIES |
| | 26 | | private List<UISelectableEntry> entries; |
| | 27 | |
|
| | 28 | | [SerializeField] |
| 0 | 29 | | private float entryHeight = 8f; |
| | 30 | | [SerializeField] |
| 0 | 31 | | private float entryIndentWidth = 4f; |
| | 32 | |
|
| | 33 | | private List<UISelectableEntry> cleanupPool; |
| | 34 | |
|
| | 35 | | private bool isOpen; |
| | 36 | |
|
| | 37 | | // Start is called before the first frame update |
| 0 | 38 | | public virtual void Start() { |
| 0 | 39 | | dialogTitleHandle.text = dialogTitle; |
| 0 | 40 | | dialogTitleHandle.font = UIManager.Instance.Font; |
| 0 | 41 | | isOpen = gameObject.activeSelf; |
| 0 | 42 | | if (dialogTabs != null) { |
| 0 | 43 | | foreach (GameObject tab in dialogTabs) { |
| 0 | 44 | | Destroy(tab); |
| 0 | 45 | | } |
| 0 | 46 | | } |
| | 47 | |
|
| 0 | 48 | | if (cleanupPool != null) { |
| 0 | 49 | | ClearDialogEntries(); |
| 0 | 50 | | } |
| | 51 | | /* |
| | 52 | | if(entries != null) { |
| | 53 | | foreach(UISelectableEntry entry in entries) { |
| | 54 | | Destroy(entry.gameObject); |
| | 55 | | } |
| | 56 | | } |
| | 57 | | */ |
| 0 | 58 | | dialogTabs = new List<GameObject>(); |
| 0 | 59 | | entries = new List<UISelectableEntry>(); |
| 0 | 60 | | cleanupPool = new List<UISelectableEntry>(); |
| 0 | 61 | | } |
| | 62 | |
|
| 0 | 63 | | internal RectTransform GetContentHandle() { |
| 0 | 64 | | return contentHandle; |
| 0 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | public bool IsOpen() { |
| 0 | 68 | | return isOpen; |
| 0 | 69 | | } |
| | 70 | |
|
| 0 | 71 | | protected virtual void OnEnable() { |
| 0 | 72 | | isOpen = true; |
| 0 | 73 | | } |
| 0 | 74 | | protected virtual void OnDisable() { |
| 0 | 75 | | isOpen = false; |
| 0 | 76 | | } |
| | 77 | |
|
| 0 | 78 | | public float GetTabWidth() { |
| 0 | 79 | | return tabWidth; |
| 0 | 80 | | } |
| 0 | 81 | | public float GetTabHeight() { |
| 0 | 82 | | return tabHeight; |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Returns the height of the dialog title bar |
| | 87 | | /// </summary> |
| 0 | 88 | | public float GetTitleBarHeight() { |
| 0 | 89 | | return dialogTitleHandle.rectTransform.sizeDelta.y; |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Adds a new tab to the dialog, when clicked it will call the given callback |
| | 94 | | /// </summary> |
| 0 | 95 | | public void AddDialogTab(string tabName, Action onClick) { |
| 0 | 96 | | dialogTabs.Add(AddTabButton(tabName, onClick)); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Add the tab button to the right of the existing tabs |
| | 101 | | /// </summary> |
| 0 | 102 | | private GameObject AddTabButton(string tabName, Action onClick) { |
| 0 | 103 | | GameObject tabButton = new GameObject("TabButton", typeof(RectTransform)); |
| 0 | 104 | | tabButton.transform.SetParent(transform); // worldPositionStays ? |
| | 105 | | // RectTransform anchors to the right of the content handle |
| 0 | 106 | | RectTransform rTransform = tabButton.GetComponent<RectTransform>(); |
| 0 | 107 | | rTransform.anchorMin = new Vector2(0, 1); |
| 0 | 108 | | rTransform.anchorMax = new Vector2(0, 1); |
| 0 | 109 | | rTransform.pivot = new Vector2(0, 1); |
| 0 | 110 | | rTransform.sizeDelta = new Vector2(tabWidth, tabHeight); |
| | 111 | | // Count tabs * tabSize to get the position from the left |
| 0 | 112 | | rTransform.anchoredPosition = |
| | 113 | | new Vector2((tabWidth / 2) * dialogTabs.Count, -(GetTitleBarHeight())); |
| | 114 | |
|
| | 115 | | // Add the onClick callback to the button |
| 0 | 116 | | Button button = tabButton.AddComponent<Button>(); |
| 0 | 117 | | button.onClick.AddListener(() => onClick()); |
| | 118 | | // Add the image to the button and link it to the tab |
| 0 | 119 | | button.targetGraphic = tabButton.AddComponent<Image>(); |
| | 120 | |
|
| 0 | 121 | | AddTabText(tabName, tabButton); |
| 0 | 122 | | return tabButton; |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | /// <summary> |
| | 126 | | /// Add text as a child of the tab's button object |
| | 127 | | /// </summary> |
| 0 | 128 | | private void AddTabText(string tabName, GameObject tabButton) { |
| 0 | 129 | | GameObject tabText = new GameObject("TabText", typeof(RectTransform)); |
| 0 | 130 | | tabText.transform.SetParent(tabButton.transform); |
| | 131 | | // RectTransform anchors to the center of the button |
| 0 | 132 | | RectTransform textRectTransform = tabText.GetComponent<RectTransform>(); |
| 0 | 133 | | textRectTransform.anchorMin = new Vector2(0.5f, 0.5f); |
| 0 | 134 | | textRectTransform.anchorMax = new Vector2(0.5f, 0.5f); |
| 0 | 135 | | textRectTransform.pivot = new Vector2(0.5f, 0.5f); |
| 0 | 136 | | textRectTransform.sizeDelta = new Vector2(tabWidth, tabHeight); |
| | 137 | | // Text position |
| 0 | 138 | | textRectTransform.anchoredPosition = new Vector2(0, 0); |
| | 139 | |
|
| 0 | 140 | | TextMeshProUGUI buttonText = tabText.AddComponent<TextMeshProUGUI>(); |
| 0 | 141 | | buttonText.text = tabName; |
| 0 | 142 | | buttonText.font = UIManager.Instance.Font; |
| 0 | 143 | | buttonText.fontSize = 10; |
| 0 | 144 | | buttonText.color = Color.black; |
| 0 | 145 | | buttonText.alignment = TextAlignmentOptions.Center; |
| 0 | 146 | | buttonText.verticalAlignment = VerticalAlignmentOptions.Middle; |
| 0 | 147 | | } |
| | 148 | |
|
| 0 | 149 | | public virtual UISelectableEntry CreateSelectableEntry() { |
| | 150 | | // Create a new entry object with content handle as parent |
| 0 | 151 | | GameObject go = Instantiate(Resources.Load<GameObject>("Prefabs/EmptyObject"), contentHandle); |
| 0 | 152 | | go.name = "UISelectableEntry"; |
| 0 | 153 | | UISelectableEntry entry = go.AddComponent<UISelectableEntry>(); |
| 0 | 154 | | entry.SetParent(this); |
| | 155 | | // add to cleanup pool so we can clear later in clear dialog entries |
| 0 | 156 | | cleanupPool.Add(entry); |
| 0 | 157 | | return entry; |
| 0 | 158 | | } |
| | 159 | |
|
| 0 | 160 | | public void ClearDialogEntries() { |
| 0 | 161 | | if (cleanupPool == null) |
| 0 | 162 | | return; |
| 0 | 163 | | foreach (UISelectableEntry entry in cleanupPool) { |
| 0 | 164 | | GameObject.Destroy(entry.gameObject); |
| 0 | 165 | | } |
| 0 | 166 | | cleanupPool.Clear(); |
| 0 | 167 | | if (entries != null) |
| 0 | 168 | | entries.Clear(); |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | /// <summary> |
| | 172 | | /// Clears, sets, and prints the dialog entries in the order they were added |
| | 173 | | /// </summary> |
| 0 | 174 | | public virtual void SetDialogEntries(List<UISelectableEntry> entries) { |
| 0 | 175 | | this.entries = entries; |
| | 176 | | // calculate total height of the content |
| 0 | 177 | | float heightHead = -1 * GetTabHeight(); |
| 0 | 178 | | int count = 0; |
| 0 | 179 | | foreach (UISelectableEntry entry in this.entries) { |
| 0 | 180 | | (heightHead, count) = RecursiveContentPrint(entry, 1, heightHead, count); |
| 0 | 181 | | } |
| 0 | 182 | | contentHandle.sizeDelta = |
| | 183 | | new Vector2(contentHandle.sizeDelta.x, count * entryHeight + Mathf.Abs(heightHead)); |
| 0 | 184 | | } |
| | 185 | |
|
| 0 | 186 | | public virtual void SetDialogEntries(UISelectableEntry entry) { |
| 0 | 187 | | SetDialogEntries(new List<UISelectableEntry>() { entry }); |
| 0 | 188 | | } |
| | 189 | |
|
| | 190 | | private (float, int) |
| 0 | 191 | | RecursiveContentPrint(UISelectableEntry entry, int depth, float heightHead, int count) { |
| 0 | 192 | | RectTransform rTransform = entry.GetComponent<RectTransform>(); |
| 0 | 193 | | rTransform.anchorMin = new Vector2(0, 1); |
| 0 | 194 | | rTransform.anchorMax = new Vector2(1, 1); |
| 0 | 195 | | rTransform.pivot = new Vector2(0.5f, 1f); |
| | 196 | |
|
| 0 | 197 | | rTransform.anchoredPosition = new Vector2(0, heightHead); // positioning from top |
| 0 | 198 | | rTransform.sizeDelta = new Vector2(0, entryHeight); |
| 0 | 199 | | float padding = 5f; |
| 0 | 200 | | rTransform.SetRight(padding); |
| 0 | 201 | | rTransform.SetLeft(padding); |
| | 202 | | // actually indent the text |
| 0 | 203 | | entry.GetTextTransform(0).anchoredPosition = new Vector2(entryIndentWidth * depth, 0); |
| 0 | 204 | | heightHead -= entryHeight; |
| 0 | 205 | | count++; |
| | 206 | | // Print the children |
| 0 | 207 | | if (entry.GetChildEntries() != null) { |
| 0 | 208 | | foreach (UISelectableEntry child in entry.GetChildEntries()) { |
| 0 | 209 | | (heightHead, count) = RecursiveContentPrint(child, depth + 1, heightHead, count); |
| 0 | 210 | | } |
| 0 | 211 | | } |
| 0 | 212 | | return (heightHead, count); |
| 0 | 213 | | } |
| | 214 | |
|
| | 215 | | // Update is called once per frame |
| 0 | 216 | | void Update() {} |
| | 217 | | } |