AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ToolTip.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // Licensed under the MIT License. See LICENSE in the project root for license information.
4 //
5 using System;
6 using UnityEngine;
7 using HoloToolkit.Unity.UX;
9 
10 namespace HoloToolkit.UX.ToolTips
11 {
12  [RequireComponent(typeof(ToolTipConnector))]
13 
18  public class ToolTip : Button
19  {
20  [SerializeField]
21  private bool showBackground = true;
25  public bool ShowBackground
26  {
27  get
28  {
29  return showBackground;
30  }
31  set
32  {
33  showBackground = value;
34  GetComponent<ToolTipBackgroundMesh>().IsVisible = value;
35  }
36  }
37 
38  [SerializeField]
39  private bool showOutline = false;
43  public bool ShowOutline
44  {
45  get
46  {
47  return showOutline;
48  }
49  set
50  {
51  showOutline = value;
52  GameObject TipBackground = contentParent.transform.GetChild(1).gameObject;
53  Rectangle rectangle = TipBackground.GetComponent<Rectangle>();
54  rectangle.enabled = value;
55  }
56  }
57 
58  [SerializeField]
59  private bool showConnector = true;
63  public bool ShowConnector
64  {
65  get
66  {
67  return showConnector;
68  }
69  set
70  {
71  showConnector = value;
72  //todo fix this
73  Line lineScript = GetComponent<Line>();
74  lineScript.enabled = value;
75  }
76  }
77 
78  [SerializeField]
83  public TipDisplayModeEnum TipState
84  {
85  get
86  {
87  return tipState;
88  }
89  set
90  {
91  tipState = value;
92  }
93  }
94 
95  [SerializeField]
100  public TipDisplayModeEnum GroupTipState
101  {
102  set
103  {
104  groupTipState = value;
105  }
106  get
107  {
108  return groupTipState;
109  }
110  }
111 
112  [SerializeField]
117  public TipDisplayModeEnum MasterTipState
118  {
119  set
120  {
121  masterTipState = value;
122  }
123  get
124  {
125  return masterTipState;
126  }
127  }
128 
129  [Tooltip("GameObject that the line and text are attached to")]
130  [SerializeField]
131  protected GameObject anchor;
135  public GameObject Anchor
136  {
137  get
138  {
139  return anchor;
140  }
141  set
142  {
143  anchor = value;
144  }
145  }
146 
147  [Tooltip("Pivot point that text will rotate around as well as the point where the Line will be rendered to.")]
148  [SerializeField]
149  protected GameObject pivot;
153  public GameObject Pivot
154  {
155  get
156  {
157  return pivot;
158  }
159  }
160 
161  [Tooltip("GameObject text that is displayed on the tooltip.")]
162  [SerializeField]
163  protected GameObject label;
164 
165  [Tooltip("Parent of the Text and Background")]
166  [SerializeField]
167  protected GameObject contentParent;
168 
169  [Tooltip("Text for the ToolTip to say")]
170  [SerializeField]
171  [TextArea]
172  protected string toolTipText;
176  public string ToolTipText
177  {
178  set
179  {
180  if (value != toolTipText)
181  {
182  toolTipText = value;
183  RefreshLocalContent();
184  if (ContentChange != null)
185  ContentChange.Invoke();
186  }
187  }
188  get
189  {
190  return toolTipText;
191  }
192  }
193 
194  [Tooltip("The padding around the content (height / width)")]
195  [SerializeField]
196  protected Vector2 backgroundPadding;
197 
198  [Tooltip("The offset of the background (x / y / z)")]
199  [SerializeField]
200  protected Vector3 backgroundOffset;
204  public Vector3 LocalContentOffset
205  {
206  get
207  {
208  return backgroundOffset;
209  }
210  }
211 
212  [Tooltip("The scale of all the content (label, backgrounds, etc.)")]
213  [SerializeField]
214  [Range(0.01f, 3f)]
215  protected float contentScale = 1f;
219  public float ContentScale
220  {
221  get
222  {
223  return contentScale;
224  }
225  set
226  {
227  contentScale = value;
228  RefreshLocalContent();
229  }
230  }
231 
232  [Tooltip("The font size of the tooltip.)")]
233  [SerializeField]
234  [Range(10, 60)]
235  protected int fontSize = 30;
236 
237  [SerializeField]
238  protected ToolTipAttachPointType attachPointType = ToolTipAttachPointType.Closest;
242  public ToolTipAttachPointType PivotType
243  {
244  get
245  {
246  return attachPointType;
247  }
248  set
249  {
250  attachPointType = value;
251  }
252  }
253 
254  [Tooltip("The line connecting the anchor to the pivot. If present, this component will be updated automatically.")]
255  [SerializeField]
257 
258  protected Vector2 localContentSize;
262  public Vector2 LocalContentSize
263  {
264  get
265  {
266  return localContentSize;
267  }
268  }
269 
270  protected Vector3 localAttachPoint;
271 
272  protected Vector3 attachPointOffset;
273 
274  protected Vector3[] localAttachPointPositions;
275 
279  public Vector3 PivotPosition
280  {
281  get
282  {
283  return pivot.transform.position;
284  }
285  set
286  {
287  pivot.transform.position = value;
288  }
289  }
290 
294  public Vector3 AttachPointPosition
295  {
296  get
297  {
298  return contentParent.transform.TransformPoint(localAttachPoint) + attachPointOffset;
299  }
300  set
301  {
302  // apply the difference to the offset
303  attachPointOffset = value - contentParent.transform.TransformPoint(localAttachPoint);
304  }
305  }
306 
310  public Vector3 AnchorPosition
311  {
312  get
313  {
314  return anchor.transform.position;
315  }
316  }
317 
321  public Transform ContentParentTransform
322  {
323  get
324  {
325  return contentParent.transform;
326  }
327  }
328 
332  public bool IsOn
333  {
334  get
335  {
336  switch (masterTipState)
337  {
338  case TipDisplayModeEnum.None:
339  default:
340  // Use our group state
341  switch (groupTipState)
342  {
343  case TipDisplayModeEnum.None:
344  default:
345  // Use our local State
346  switch (tipState)
347  {
348  case TipDisplayModeEnum.None:
349  case TipDisplayModeEnum.Off:
350  default:
351  return false;
352 
353  case TipDisplayModeEnum.On:
354  return true;
355 
356  case TipDisplayModeEnum.OnFocus:
357  return HasFocus;
358  }
359 
360  case TipDisplayModeEnum.On:
361  return true;
362 
363  case TipDisplayModeEnum.Off:
364  return false;
365 
366  case TipDisplayModeEnum.OnFocus:
367  return HasFocus;
368  }
369 
370  case TipDisplayModeEnum.On:
371  return true;
372 
373  case TipDisplayModeEnum.Off:
374  return false;
375 
376  case TipDisplayModeEnum.OnFocus:
377  return HasFocus;
378  }
379  }
380  }
381 
385  public bool HasFocus
386  {
387  get
388  {
389  switch (ButtonState)
390  {
391  case ButtonStateEnum.Targeted:
392  case ButtonStateEnum.ObservationTargeted:
393  case ButtonStateEnum.Pressed:
394  return true;
395 
396  default:
397  return false;
398  }
399  }
400  }
401 
405  public Action ContentChange;
406 
410  protected virtual void OnEnable() {
411 
412  // Get our line if it exists
413  if (toolTipLine == null)
414  toolTipLine = gameObject.GetComponent<LineBase>();
415 
416  //EnforceHeirarchy();
417  RefreshLocalContent();
418  contentParent.SetActive(false);
419  ShowBackground = showBackground;
420  ShowOutline = showOutline;
421  ShowConnector = showConnector;
422  }
423 
424  protected virtual void Update() {
425  // Enable / disable our line if it exists
426  if (toolTipLine != null)
427  {
428  toolTipLine.enabled = IsOn;
429  toolTipLine.FirstPoint = AnchorPosition;
430  toolTipLine.LastPoint = AttachPointPosition;
431  }
432 
433  if (IsOn) {
434  contentParent.SetActive(true);
435  localAttachPoint = ToolTipUtility.FindClosestAttachPointToAnchor(anchor.transform, contentParent.transform, localAttachPointPositions, attachPointType);
436  } else {
437  contentParent.SetActive(false);
438  }
439  }
440 
441  protected virtual void RefreshLocalContent() {
442 
443  // Set the scale of the pivot
444  contentParent.transform.localScale = Vector3.one * contentScale;
445  label.transform.localScale = Vector3.one * 0.005f;
446  // Set the content using a text mesh by default
447  // This function can be overridden for tooltips that use Unity UI
448  TextMesh text = label.GetComponent<TextMesh>();
449  if (text != null && !string.IsNullOrEmpty(toolTipText)) {
450  text.fontSize = fontSize;
451  text.text = toolTipText.Trim();
452  text.lineSpacing = 1;
453  text.anchor = TextAnchor.MiddleCenter;
454  // Get the world scale of the text
455  // Convert that to local scale using the content parent
456  Vector3 localScale = text.transform.localScale;
457  localContentSize.x = localScale.x + backgroundPadding.x;
458  localContentSize.y = localScale.y + backgroundPadding.y;
459  }
460  // Now that we have the size of our content, get our pivots
461  ToolTipUtility.GetAttachPointPositions(ref localAttachPointPositions, localContentSize);
462  localAttachPoint = ToolTipUtility.FindClosestAttachPointToAnchor(anchor.transform, contentParent.transform, localAttachPointPositions, attachPointType);
463  }
464 
465  protected virtual bool EnforceHierarchy() {
466 
467  Transform pivotTransform = transform.Find("Pivot");
468  Transform anchorTransform = transform.Find("Anchor");
469  if (pivotTransform == null || anchorTransform == null) {
470  if (Application.isPlaying) {
471  Debug.LogError("Found error in heirarchy, disabling.");
472  enabled = false;
473  }
474  return false;
475  }
476  Transform contentParentTransform = pivotTransform.Find("ContentParent");
477  if (contentParentTransform == null) {
478  if (Application.isPlaying) {
479  Debug.LogError("Found error in heirarchy, disabling.");
480  enabled = false;
481  }
482  return false;
483  }
484  Transform labelTransform = contentParentTransform.Find("Label");
485  if (labelTransform == null) {
486  if (Application.isPlaying) {
487  Debug.LogError("Found error in heirarchy, disabling.");
488  enabled = false;
489  }
490  return false;
491  }
492 
493  contentParentTransform.localPosition = Vector3.zero;
494  contentParentTransform.localRotation = Quaternion.identity;
495  contentParentTransform.localScale = Vector3.one * contentScale;
496  labelTransform.localPosition = Vector3.zero;
497  labelTransform.localScale = Vector3.one * 0.025f;
498  labelTransform.localRotation = Quaternion.identity;
499  pivotTransform.localScale = Vector3.one;
500 
501  pivot = pivotTransform.gameObject;
502  anchor = anchorTransform.gameObject;
503  contentParent = contentParentTransform.gameObject;
504  label = labelTransform.gameObject;
505 
506  return true;
507  }
508 
509  #if UNITY_EDITOR
510  private void OnDrawGizmos() {
511 
512  if (Application.isPlaying)
513  return;
514 
515  if (!EnforceHierarchy()) {
516  return;
517  }
518 
519  RefreshLocalContent();
520 
521  if (toolTipLine != null)
522  {
523  toolTipLine.FirstPoint = AnchorPosition;
524  toolTipLine.LastPoint = AttachPointPosition;
525  }
526  }
527  #endif
528  }
529 }
virtual void RefreshLocalContent()
Definition: ToolTip.cs:441
TipDisplayModeEnum groupTipState
Definition: ToolTip.cs:96
ButtonStateEnum
State enum for buttons.
Connects a ToolTip to a target Maintains that connection even if the target moves ...
static Vector3 FindClosestAttachPointToAnchor(Transform anchor, Transform contentParent, Vector3[] localPivotPositions, ToolTipAttachPointType pivotType)
Avoid running this query in Update function because calculating Vector3.Distance requires sqr root ca...
Class for Tooltip object Creates a floating tooltip that is attached to an object and moves to stay i...
Definition: ToolTip.cs:18
virtual void OnEnable()
virtual functions
Definition: ToolTip.cs:410
TipDisplayModeEnum masterTipState
Definition: ToolTip.cs:113
static void GetAttachPointPositions(ref Vector3[] pivotPositions, Vector2 localContentSize)
gets an array of pivot positions
TipDisplayModeEnum tipState
Definition: ToolTip.cs:79
Vector3 [] localAttachPointPositions
Definition: ToolTip.cs:274
ToolTipAttachPointType
Used to find a pivot point that is closest to the anchor. This ensures a natural-looking attachment w...
Static class providing useful functions for finding ToolTip Attachpoint information.
virtual bool EnforceHierarchy()
Definition: ToolTip.cs:465
Action ContentChange
Used for Content Change update in text
Definition: ToolTip.cs:405
TipDisplayModeEnum
Enum describing the display mode of a ToolTip