HKI Core
UITooltipController.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 using HKI.Core.Loc;
4 
5 namespace HKI.Core.UI
6 {
10  [AddComponentMenu("HKI/UI/UITooltipController")]
11  [RequireComponent(typeof(Canvas))]
12  [DisallowMultipleComponent]
13  public class UITooltipController : MonoBehaviour
14  {
15  // Public variables
16  public static UITooltipController Instance { get; private set; }
17 
18  [Header("General Properties")]
19  [SerializeField] GameObject TooltipWindowGO = null;
20  [SerializeField] GameObject TooltipTextGO = null;
21 
22  [SerializeField] Language Language = null;
23 
24  [SerializeField] float DelayTime = 2.0f;
25 
26  [SerializeField] Vector3 TopLeftOffset = new Vector3();
27  [SerializeField] Vector3 TopRightOffset = new Vector3();
28  [SerializeField] Vector3 BottomLeftOffset = new Vector3();
29  [SerializeField] Vector3 BottomRightOffset = new Vector3();
30 
31  // Private variables
32  Transform tooltipWindowTransform = null;
33 
34  TMProAndUGUITextAccessor tooltipText = null;
35 
36  UITooltip currentActiveTooltip = null;
37 
38  Coroutine showTooltipCoroutine = null;
39 
40  // Awake function
41  void Awake()
42  {
43  if(Instance == null)
44  {
45  Instance = this;
46  DontDestroyOnLoad(gameObject);
47  }
48  else
49  {
50  Destroy(gameObject);
51  return;
52  }
53 
54  if(TooltipWindowGO == null)
55  {
56  Debug.LogError("UITooltipController.Awake(): TooltipWindowGO is null!");
57  return;
58  }
59 
60  if(TooltipTextGO == null)
61  {
62  Debug.LogError("UITooltipController.Awake(): TooltipTextGO is null!");
63  return;
64  }
65 
66  tooltipWindowTransform = TooltipWindowGO.transform;
67 
68  tooltipText = new TMProAndUGUITextAccessor(TooltipTextGO);
69 
70  TooltipWindowGO.SetActive(false);
71  }
72 
73  // OnDestroy
74  void OnDestroy()
75  {
76  if(Instance == this)
77  Instance = null;
78  }
79 
80  // Interface functions
81  public void ShowTooltip(UITooltip tooltip, TextLocalization textLocalization)
82  {
83  if(textLocalization == null)
84  {
85  Debug.LogError("(UITooltipController) you are trying use a text localization but the given text localization is null!");
86  return;
87  }
88 
89  if(showTooltipCoroutine != null)
90  {
91  StopCoroutine(showTooltipCoroutine);
92  TooltipWindowGO.SetActive(false);
93  showTooltipCoroutine = null;
94  currentActiveTooltip = null;
95  }
96 
97  currentActiveTooltip = tooltip;
98 
99  showTooltipCoroutine = StartCoroutine(ShowTooltipCoroutine(textLocalization));
100  }
101 
102  public void HideTooltip(UITooltip tooltip)
103  {
104  if(currentActiveTooltip == tooltip)
105  {
106  StopCoroutine(showTooltipCoroutine);
107  TooltipWindowGO.SetActive(false);
108  showTooltipCoroutine = null;
109  currentActiveTooltip = null;
110  }
111  }
112 
113  // Helper functions
114  IEnumerator ShowTooltipCoroutine(TextLocalization textLocalization)
115  {
116  // Wait for some time until the tooltip should popup
117  yield return new WaitForSeconds(DelayTime);
118 
119  // Set tooltip text
120  if(Language != null)
121  tooltipText.ChangeText(textLocalization.GetText(Language.Value));
122  else
123  tooltipText.ChangeText(textLocalization.GetDefaultText());
124 
125  switch(currentActiveTooltip.GetTooltipAlingment)
126  {
127  case UITooltip.TooltipAlingments.BottomRight:
128  ((RectTransform)tooltipWindowTransform).pivot = new Vector2(0.0f, 1.0f);
129  break;
130 
131  case UITooltip.TooltipAlingments.TopLeft:
132  ((RectTransform)tooltipWindowTransform).pivot = new Vector2(1.0f, 0.0f);
133  break;
134 
135  case UITooltip.TooltipAlingments.BottomLeft:
136  ((RectTransform)tooltipWindowTransform).pivot = new Vector2(1.0f, 1.0f);
137  break;
138 
139  case UITooltip.TooltipAlingments.TopRight:
140  ((RectTransform)tooltipWindowTransform).pivot = new Vector2(0.0f, 0.0f);
141  break;
142  }
143 
144  UpdatePosition();
145 
146  TooltipWindowGO.SetActive(true);
147 
148  // Update position every frame
149  bool updatePosition = true;
150  while(updatePosition)
151  {
152  UpdatePosition();
153 
154  yield return null;
155  }
156  }
157 
159  {
160  switch (currentActiveTooltip.GetTooltipAlingment)
161  {
162  case UITooltip.TooltipAlingments.BottomRight:
163  tooltipWindowTransform.position = Input.mousePosition + BottomRightOffset;
164  break;
165 
166  case UITooltip.TooltipAlingments.TopLeft:
167  tooltipWindowTransform.position = Input.mousePosition + TopLeftOffset;
168  break;
169 
170  case UITooltip.TooltipAlingments.BottomLeft:
171  tooltipWindowTransform.position = Input.mousePosition + BottomLeftOffset;
172  break;
173 
174  case UITooltip.TooltipAlingments.TopRight:
175  tooltipWindowTransform.position = Input.mousePosition + TopRightOffset;
176  break;
177  }
178  }
179  }
180 }
string GetDefaultText()
This functions returns the default localization this will be the first TextElement. In the case there is no localization at all an empty string will be returned.
IEnumerator ShowTooltipCoroutine(TextLocalization textLocalization)
This ScriptableObject contains as a container for all text localization information.
void ShowTooltip(UITooltip tooltip, TextLocalization textLocalization)
string GetText(Language language)
This function returns the string in a specific language. If there is no localization element in the r...
This MonoBehaviour is responseble for the displaying of a tooltip. It needs a window GameObject and a...
This class allows the use of the standard Unity UI Text component and the TextMeshPro component witho...
Special Variable enables the use of the SystemLanguage data type as a variable of the settings system...
Definition: Language.cs:13
void HideTooltip(UITooltip tooltip)
Attached to a GameObject with a RectTransform this MonoBehaviour will display a tooltip if the mouse ...
Definition: UITooltip.cs:10
TooltipAlingments GetTooltipAlingment
Definition: UITooltip.cs:27