AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ToolTipSpawner.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 //
4 using System.Collections;
5 using UnityEngine;
7 
8 namespace HoloToolkit.UX.ToolTips
9 {
14  public class ToolTipSpawner : MonoBehaviour , IInputHandler, IPointerSpecificFocusable
15  {
16  private enum VanishType
17  {
18  VanishOnFocusExit,
19  VanishOnTap,
20  }
21 
22  private enum AppearType
23  {
24  AppearOnFocusEnter,
25  AppearOnTap,
26  }
27 
28  public enum RemainType
29  {
30  Indefinite,
31  Timeout,
32  }
33 
34  [SerializeField]
35  private Vector3 defaultDimensions = new Vector3(0.182f, 0.028f, 1.0f);
36 
37  [SerializeField]
38  private bool showBackground = true;
39 
40  [SerializeField]
41  private bool showOutline = false;
42 
43  [SerializeField]
44  private bool showConnector = true;
45 
46  [SerializeField]
47  private AppearType appearType = AppearType.AppearOnFocusEnter;
48 
49  [SerializeField]
50  private VanishType vanishType = VanishType.VanishOnFocusExit;
51 
52  [SerializeField]
53  private RemainType remainType = RemainType.Timeout;
54 
55  [SerializeField]
56  [Range(0f, 5f)]
57  private float appearDelay = 0.0f;
58 
59  [SerializeField]
60  [Range(0f, 5f)]
61  private float vanishDelay = 2.0f;
62 
63  [SerializeField]
64  [Range(0.5f, 10.0f)]
65  private float lifetime = 1.0f;
66 
67  [SerializeField]
68  private GameObject toolTipPrefab = null;
69 
70  [SerializeField]
71  private ConnectorFollowType followType = ConnectorFollowType.AnchorOnly;
72 
73  [SerializeField]
74  private ConnnectorPivotMode pivotMode = ConnnectorPivotMode.Manual;
75 
76  [SerializeField]
77  private ConnectorPivotDirection pivotDirection = ConnectorPivotDirection.North;
78 
79  [SerializeField]
80  private ConnectorOrientType pivotDirectionOrient = ConnectorOrientType.OrientToObject;
81 
82  [SerializeField]
83  private Vector3 manualPivotDirection = Vector3.up;
84 
85  [SerializeField]
86  private Vector3 manualPivotLocalPosition = Vector3.up;
87 
88 #if UNITY_EDITOR
89  [SerializeField]
90  [Range(0f, 1f)]
91  private float pivotDistance = 0.25f;
92 #endif
93 
94  [SerializeField]
95  private string ToolTipText = "New Tooltip";
96 
97  [SerializeField]
98  private Transform Anchor = null;
99 
100  private float focusEnterTime = 0f;
101 
102  private float focusExitTime = 0f;
103 
104  private float tappedTime = 0f;
105 
106  private bool hasFocus;
107 
108  private ToolTip toolTip;
109 
114  public void OnFocusEnter(PointerSpecificEventData eventData)
115  {
116  focusEnterTime = Time.unscaledTime;
117  hasFocus = true;
118  if (toolTip == null || !toolTip.gameObject.activeSelf)
119  {
120  switch (appearType)
121  {
122  case AppearType.AppearOnFocusEnter:
123  ShowToolTip();
124  break;
125 
126  default:
127  break;
128  }
129  }
130  }
131 
132  public void OnFocusExit(PointerSpecificEventData eventData)
133  {
134  focusExitTime = Time.unscaledTime;
135  hasFocus = false;
136  }
137 
138  public void OnInputDown(InputEventData eventData)
139  {
140  tappedTime = Time.unscaledTime;
141  if (toolTip == null || !toolTip.gameObject.activeSelf)
142  {
143  if( appearType == AppearType.AppearOnTap)
144  {
145  ShowToolTip();
146  }
147  }
148  }
149 
154  public void OnInputUp(InputEventData eventData){}
155 
156  private void ShowToolTip()
157  {
158  StartCoroutine(UpdateTooltip(focusEnterTime, tappedTime));
159  }
160 
161  private IEnumerator UpdateTooltip(float focusEnterTimeOnStart, float tappedTimeOnStart)
162  {
163  if (toolTip == null)
164  {
165  GameObject toolTipGo = GameObject.Instantiate(toolTipPrefab) as GameObject;
166  toolTip = toolTipGo.GetComponent<ToolTip>();
167  toolTip.gameObject.SetActive(false);
168  toolTip.ShowBackground = showBackground;
169  toolTip.ShowOutline = showOutline;
170  toolTip.ShowConnector = showConnector;
171  toolTip.transform.position = transform.position;
172  toolTip.transform.parent = transform;
173  toolTip.ContentParentTransform.localScale = defaultDimensions;
174  }
175 
176  if( appearType == AppearType.AppearOnFocusEnter)
177  {
178  // Wait for the appear delay
179  yield return new WaitForSeconds(appearDelay);
180  // If we don't have focus any more, get out of here
181  if (!hasFocus)
182  {
183  yield break;
184  }
185  }
186 
187  toolTip.ToolTipText = ToolTipText;
188  toolTip.gameObject.SetActive(true);
189  ToolTipConnector connector = toolTip.GetComponent<ToolTipConnector>();
190  connector.Target = (Anchor != null) ? Anchor.gameObject : gameObject;
191  connector.PivotDirection = pivotDirection;
192  connector.PivotDirectionOrient = pivotDirectionOrient;
193  connector.ManualPivotLocalPosition = manualPivotLocalPosition;
194  connector.ManualPivotDirection = manualPivotDirection;
195  connector.FollowingType = followType;
196  connector.PivotingMode = pivotMode;
197 
198  if (pivotMode == ConnnectorPivotMode.Manual)
199  {
200  toolTip.PivotPosition = transform.TransformPoint(manualPivotLocalPosition);
201  }
202 
203  while (toolTip.gameObject.activeSelf)
204  {
205  if (remainType == RemainType.Timeout)
206  {
207  if (appearType == AppearType.AppearOnTap)
208  {
209  if (Time.unscaledTime - tappedTime >= lifetime)
210  {
211  toolTip.gameObject.SetActive(false);
212  yield break;
213  }
214  }
215  else if (appearType == AppearType.AppearOnFocusEnter)
216  {
217  if (Time.unscaledTime - focusEnterTime >= lifetime)
218  {
219  toolTip.gameObject.SetActive(false);
220  yield break;
221  }
222  }
223 
224  }
225  //check whether we're suppose to disappear
226  switch (vanishType)
227  {
228  case VanishType.VanishOnFocusExit:
229  break;
230 
231  case VanishType.VanishOnTap:
232  if (tappedTime != tappedTimeOnStart)
233  {
234  toolTip.gameObject.SetActive(false);
235  }
236  break;
237 
238  default:
239  if (!hasFocus)
240  {
241  if (Time.time - focusExitTime > vanishDelay)
242  {
243  toolTip.gameObject.SetActive(false);
244  }
245  }
246  break;
247  }
248  yield return null;
249  }
250  yield break;
251  }
252 
253  #if UNITY_EDITOR
254  private void OnDrawGizmos()
255  {
256  if (Application.isPlaying)
257  return;
258 
259  if (gameObject == UnityEditor.Selection.activeGameObject)
260  {
261  Gizmos.color = Color.cyan;
262  Transform relativeTo = null;
263  switch (pivotDirectionOrient) {
264  case ConnectorOrientType.OrientToCamera:
265  relativeTo = Camera.main.transform;//Veil.Instance.HeadTransform;
266  break;
267 
268  case ConnectorOrientType.OrientToObject:
269  relativeTo = (Anchor != null) ? Anchor.transform : transform;
270  break;
271  }
272  if (pivotMode == ConnnectorPivotMode.Automatic) {
273  Vector3 targetPosition = (Anchor != null) ? Anchor.transform.position : transform.position;
274  Vector3 toolTipPosition = targetPosition + ToolTipConnector.GetDirectionFromPivotDirection(
275  pivotDirection,
276  manualPivotDirection,
277  relativeTo) * pivotDistance;
278  Gizmos.DrawLine(targetPosition, toolTipPosition);
279  Gizmos.DrawWireCube(toolTipPosition, Vector3.one * 0.05f);
280  } else {
281  Vector3 targetPosition = (Anchor != null) ? Anchor.transform.position : transform.position;
282  Vector3 toolTipPosition = transform.TransformPoint (manualPivotLocalPosition);
283  Gizmos.DrawLine(targetPosition, toolTipPosition);
284  Gizmos.DrawWireCube(toolTipPosition, Vector3.one * 0.05f);
285  }
286  }
287  }
288  #endif
289  }
290 }
bool ShowBackground
getter/setter for showing the opaque background of tooltip.
Definition: ToolTip.cs:26
Interface to implement to react to per-pointer focus enter/exit.
ConnectorPivotDirection PivotDirection
getter/setter for the direction of the connector
ConnectorOrientType PivotDirectionOrient
orientation style for connector
Connects a ToolTip to a target Maintains that connection even if the target moves ...
Vector3 ManualPivotDirection
getter/setter for direction of pivot manually set
ConnnectorPivotMode
how is the pivot of the tooltip determined?
Vector3 ManualPivotLocalPosition
getter/setter for local pivot position
Vector3 PivotPosition
point about which ToolTip pivots to face camera
Definition: ToolTip.cs:280
GameObject Target
The GameObject to which the tooltip is connected
void OnFocusExit(PointerSpecificEventData eventData)
static Vector3 GetDirectionFromPivotDirection(ConnectorPivotDirection pivotDirection, Vector3 manualPivotDirection, Transform relativeTo)
Computes the director of the connector
Class for Tooltip object Creates a floating tooltip that is attached to an object and moves to stay i...
Definition: ToolTip.cs:18
ConnectorPivotDirection
In which direction does the tooltip connector project?
Event dispatched associated with a specific pointer.
bool ShowConnector
getter/setter for showing connecting stem between tooltip and parent object
Definition: ToolTip.cs:64
Add to any Object to spawn ToolTips on tap or on focus, according to preference Applies its follow se...
void OnInputDown(InputEventData eventData)
Transform ContentParentTransform
Tramsform of object to which ToolTip is attached
Definition: ToolTip.cs:322
ConnectorFollowType FollowingType
getter /setter for the follow style of the tooltip connector
void OnFocusEnter(PointerSpecificEventData eventData)
methods associated with IInputHandler
string ToolTipText
Text for the ToolTip to display
Definition: ToolTip.cs:177
Interface to implement to react to simple pointer-like input.
ConnectorOrientType
how does the tooltip rotate about the connector
Describes an input event that has a source id and a press kind.
ConnnectorPivotMode PivotingMode
is the connector pivot set manually or automatically
ConnectorFollowType
How does the Tooltip track with its parent object
bool ShowOutline
getter/setter for showing white trim around edge of tooltip
Definition: ToolTip.cs:44
void OnInputUp(InputEventData eventData)
this Handler intentionally empty