| | 1 | | using System.Collections.Generic; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | | using System.Linq; |
| | 8 | | public class UISelectableEntry : EventTrigger { |
| | 9 | | private List<UISelectableEntry> children; |
| | 10 | | private List<string> textContent; |
| | 11 | |
|
| | 12 | | private UIDialog parentDialog; |
| | 13 | |
|
| | 14 | | private RectTransform rectTransform; |
| | 15 | |
|
| | 16 | | private CanvasRenderer canvasRenderer; |
| | 17 | |
|
| | 18 | | private Image image; |
| | 19 | |
|
| | 20 | | // Replace the single TextMeshProUGUI with a list to hold multiple columns |
| | 21 | | private List<TextMeshProUGUI> textHandles; |
| | 22 | |
|
| 0 | 23 | | private bool isSelectable = true; |
| | 24 | |
|
| 0 | 25 | | private static Color baseColor = new Color32(31, 31, 45, 140); |
| | 26 | |
|
| | 27 | | private Action<object> OnClickCallback; |
| | 28 | | private object callbackArgument; |
| | 29 | |
|
| 0 | 30 | | public void Awake() { |
| 0 | 31 | | rectTransform = gameObject.AddComponent<RectTransform>(); |
| | 32 | |
|
| 0 | 33 | | image = gameObject.AddComponent<Image>(); |
| 0 | 34 | | image.type = Image.Type.Sliced; |
| 0 | 35 | | image.color = baseColor; |
| | 36 | |
|
| | 37 | | // Initialize the list for text handles |
| 0 | 38 | | textHandles = new List<TextMeshProUGUI>(); |
| 0 | 39 | | } |
| | 40 | |
|
| 0 | 41 | | public void SetClickCallback(Action<object> callback, object argument) { |
| 0 | 42 | | OnClickCallback = callback; |
| 0 | 43 | | callbackArgument = argument; |
| 0 | 44 | | } |
| | 45 | |
|
| 0 | 46 | | public override void OnPointerEnter(PointerEventData eventData) { |
| 0 | 47 | | if (isSelectable) |
| 0 | 48 | | image.color = baseColor + new Color32(20, 20, 20, 40); |
| 0 | 49 | | base.OnPointerEnter(eventData); |
| 0 | 50 | | } |
| | 51 | |
|
| 0 | 52 | | public override void OnPointerDown(PointerEventData eventData) { |
| 0 | 53 | | if (isSelectable && OnClickCallback != null) { |
| 0 | 54 | | OnClickCallback(callbackArgument); |
| 0 | 55 | | image.color = baseColor + new Color32(40, 40, 40, 40); |
| 0 | 56 | | } |
| 0 | 57 | | base.OnPointerClick(eventData); |
| 0 | 58 | | } |
| | 59 | |
|
| 0 | 60 | | public override void OnPointerExit(PointerEventData eventData) { |
| 0 | 61 | | if (isSelectable) |
| 0 | 62 | | image.color = baseColor; |
| 0 | 63 | | base.OnPointerExit(eventData); |
| 0 | 64 | | } |
| | 65 | |
|
| 0 | 66 | | public void SetIsSelectable(bool isSelectable) { |
| 0 | 67 | | image.enabled = isSelectable; |
| 0 | 68 | | this.isSelectable = isSelectable; |
| 0 | 69 | | } |
| | 70 | |
|
| 0 | 71 | | public bool GetIsSelectable() { |
| 0 | 72 | | return isSelectable; |
| 0 | 73 | | } |
| | 74 | |
|
| 0 | 75 | | public void AddChildEntry(UISelectableEntry child) { |
| 0 | 76 | | if (children == null) { |
| 0 | 77 | | children = new List<UISelectableEntry>(); |
| 0 | 78 | | } |
| 0 | 79 | | children.Add(child); |
| 0 | 80 | | } |
| | 81 | |
|
| 0 | 82 | | public void SetParent(UIDialog parentDialog) { |
| 0 | 83 | | this.parentDialog = parentDialog; |
| 0 | 84 | | } |
| | 85 | |
|
| 0 | 86 | | public void SetChildEntries(List<UISelectableEntry> children) { |
| 0 | 87 | | this.children = children; |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | // Get the children of this entry |
| 0 | 91 | | public List<UISelectableEntry> GetChildEntries() { |
| 0 | 92 | | return children; |
| 0 | 93 | | } |
| | 94 | |
|
| 0 | 95 | | public void SetTextContent(List<string> textContent) { |
| 0 | 96 | | this.textContent = textContent; |
| | 97 | |
|
| | 98 | | // Clear existing text handles |
| 0 | 99 | | foreach (var textHandle in textHandles) { |
| 0 | 100 | | Destroy(textHandle.gameObject); |
| 0 | 101 | | } |
| 0 | 102 | | textHandles.Clear(); |
| | 103 | |
|
| 0 | 104 | | int columnCount = textContent.Count; |
| 0 | 105 | | float totalWidth = rectTransform.rect.width; |
| 0 | 106 | | float columnWidth = totalWidth / columnCount; |
| | 107 | |
|
| 0 | 108 | | for (int i = 0; i < columnCount; i++) { |
| | 109 | | // Create a new TextMeshProUGUI for each column |
| 0 | 110 | | var textHandle = Instantiate(Resources.Load<GameObject>("Prefabs/EmptyObject"), rectTransform) |
| | 111 | | .AddComponent<TextMeshProUGUI>(); |
| 0 | 112 | | textHandle.gameObject.name = $"UISelectableEntry::Text_{i}"; |
| 0 | 113 | | textHandle.fontSize = 8; |
| 0 | 114 | | textHandle.font = UIManager.Instance.Font; |
| 0 | 115 | | textHandle.alignment = TextAlignmentOptions.MidlineLeft; |
| | 116 | |
|
| | 117 | | // Set the RectTransform for proper alignment |
| 0 | 118 | | var textRect = textHandle.GetComponent<RectTransform>(); |
| 0 | 119 | | textRect.anchorMin = new Vector2((float)i / columnCount, 0); |
| 0 | 120 | | textRect.anchorMax = new Vector2((float)(i + 1) / columnCount, 1); |
| 0 | 121 | | textRect.offsetMin = Vector2.zero; |
| 0 | 122 | | textRect.offsetMax = Vector2.zero; |
| | 123 | |
|
| | 124 | | // Set the text content |
| 0 | 125 | | textHandle.text = textContent[i]; |
| | 126 | |
|
| | 127 | | // Add to the list of text handles |
| 0 | 128 | | textHandles.Add(textHandle); |
| 0 | 129 | | } |
| 0 | 130 | | } |
| | 131 | |
|
| 0 | 132 | | public RectTransform GetTextTransform(int index) { |
| 0 | 133 | | if (index >= 0 && index < textHandles.Count) { |
| 0 | 134 | | return textHandles[index].GetComponent<RectTransform>(); |
| | 135 | | } |
| 0 | 136 | | return null; |
| 0 | 137 | | } |
| | 138 | | } |