AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ToolTipConnector.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 UnityEngine;
5 using HoloToolkit.Unity;
6 
7 namespace HoloToolkit.UX.ToolTips
8 {
9  [RequireComponent(typeof(MeshFilter))]
10 
15  public class ToolTipConnector : MonoBehaviour
16  {
17  [SerializeField]
18  private GameObject target;
19 
23  public GameObject Target
24  {
25  get
26  {
27  return target;
28  }
29  set
30  {
31  target = value;
32  }
33  }
34 
35  [SerializeField]
36  private ToolTip toolTip;
37 
38  [SerializeField]
39  private ConnectorFollowType connectorFollowType = ConnectorFollowType.AnchorOnly;
43  public ConnectorFollowType FollowingType
44  {
45  get
46  {
47  return connectorFollowType;
48  }
49 
50  set
51  {
52  connectorFollowType = value;
53  }
54  }
55 
56  [SerializeField]
57  private ConnnectorPivotMode pivotMode = ConnnectorPivotMode.Manual;
61  public ConnnectorPivotMode PivotingMode
62  {
63  get
64  {
65  return pivotMode;
66  }
67 
68  set
69  {
70  pivotMode = value;
71  }
72  }
73 
74  [SerializeField]
75  private ConnectorPivotDirection pivotDirection = ConnectorPivotDirection.North;
79  public ConnectorPivotDirection PivotDirection
80  {
81  get
82  {
83  return pivotDirection;
84  }
85 
86  set
87  {
88  pivotDirection = value;
89  }
90  }
91 
92  [SerializeField]
93  private ConnectorOrientType pivotDirectionOrient = ConnectorOrientType.OrientToObject;
97  public ConnectorOrientType PivotDirectionOrient
98  {
99  get
100  {
101  return pivotDirectionOrient;
102  }
103 
104  set
105  {
106  pivotDirectionOrient = value;
107  }
108  }
109 
110  [SerializeField]
111  private Vector3 manualPivotDirection = Vector3.up;
115  public Vector3 ManualPivotDirection
116  {
117  get
118  {
119  return manualPivotDirection;
120  }
121 
122  set
123  {
124  manualPivotDirection = value;
125  }
126  }
127 
128  [SerializeField]
129  private Vector3 manualPivotLocalPosition = Vector3.up;
133  public Vector3 ManualPivotLocalPosition
134  {
135  get
136  {
137  return manualPivotLocalPosition;
138  }
139 
140  set
141  {
142  manualPivotLocalPosition = value;
143  }
144  }
145 
146  [SerializeField]
147  [Range(0f, 2f)]
148  private float pivotDistance = 0.25f;
152  public float PivotDistance
153  {
154  get
155  {
156  return pivotDistance;
157  }
158  set
159  {
160  pivotDistance = Mathf.Min( 2.0f, Mathf.Max( 0,value));
161  }
162  }
163 
164  private void OnEnable()
165  {
166  if (!FindToolTip())
167  {
168  return;
169  }
170 
171  ManualPivotLocalPosition = transform.InverseTransformPoint (toolTip.PivotPosition);
172  }
173 
174  private bool FindToolTip()
175  {
176  toolTip = gameObject.EnsureComponent<ToolTip>();
177  return toolTip != null;
178  }
179 
180  private void UpdatePosition()
181  {
182  if (!FindToolTip())
183  {
184  return;
185  }
186 
187  if (Target == null)
188  {
189  return;
190  }
191 
192  switch (connectorFollowType)
193  {
194  case ConnectorFollowType.AnchorOnly:
195  default:
196  // Set the position of the anchor to the target's position
197  // And do nothing else
198  toolTip.Anchor.transform.position = Target.transform.position;
199  break;
200 
201  case ConnectorFollowType.Position:
202  // Move the entire tooltip transform while maintaining the anchor position offset
203  toolTip.transform.position = Target.transform.position;
204  switch (PivotingMode)
205  {
206  case ConnnectorPivotMode.Automatic:
207  Transform relativeTo = null;
208  switch (PivotDirectionOrient)
209  {
210  case ConnectorOrientType.OrientToCamera:
211  relativeTo = Camera.main.transform;//Veil.Instance.HeadTransform;
212  break;
213 
214  case ConnectorOrientType.OrientToObject:
215  relativeTo = Target.transform;
216  break;
217  }
218  toolTip.PivotPosition = Target.transform.position + GetDirectionFromPivotDirection(
219  PivotDirection,
220  ManualPivotDirection,
221  relativeTo) * PivotDistance;
222  break;
223 
224  case ConnnectorPivotMode.Manual:
225  // Do nothing
226  break;
227  }
228  break;
229 
230  case ConnectorFollowType.YRotation:
231  // Set the transform of the entire tool tip
232  // Set the pivot relative to target/camera
233  toolTip.transform.position = Target.transform.position;
234  Vector3 eulerAngles = Target.transform.eulerAngles;
235  eulerAngles.x = 0f;
236  eulerAngles.z = 0f;
237  toolTip.transform.eulerAngles = eulerAngles;
238  switch (PivotingMode)
239  {
240  case ConnnectorPivotMode.Automatic:
241  Transform relativeTo = null;
242  switch (PivotDirectionOrient)
243  {
244  case ConnectorOrientType.OrientToCamera:
245  relativeTo = Camera.main.transform;//Veil.Instance.HeadTransform;
246  break;
247 
248  case ConnectorOrientType.OrientToObject:
249  relativeTo = Target.transform;
250  break;
251  }
252  Vector3 localPosition = GetDirectionFromPivotDirection(PivotDirection, ManualPivotDirection, relativeTo) * PivotDistance;
253  toolTip.PivotPosition = Target.transform.position + localPosition;
254  break;
255 
256  case ConnnectorPivotMode.Manual:
257  // Do nothing
258  break;
259  }
260  break;
261 
262  case ConnectorFollowType.XRotation:
263  // Set the transform of the entire tool tip
264  // Set the pivot relative to target/camera
265  toolTip.transform.position = Target.transform.position;
266  toolTip.transform.rotation = Target.transform.rotation;
267  switch (PivotingMode)
268  {
269  case ConnnectorPivotMode.Automatic:
270  Transform relativeTo = null;
271  switch (PivotDirectionOrient)
272  {
273  case ConnectorOrientType.OrientToCamera:
274  relativeTo = Camera.main.transform;//Veil.Instance.HeadTransform;
275  break;
276 
277  case ConnectorOrientType.OrientToObject:
278  relativeTo = Target.transform;
279  break;
280  }
281  toolTip.PivotPosition = Target.transform.position + GetDirectionFromPivotDirection(
282  PivotDirection,
283  ManualPivotDirection,
284  relativeTo) * PivotDistance;
285  break;
286 
287  case ConnnectorPivotMode.Manual:
288  // Do nothing
289  break;
290  }
291  break;
292  }
293  }
294 
295  private void Update()
296  {
297  UpdatePosition();
298  }
299 
300  private void OnDrawGizmos()
301  {
302  if (Application.isPlaying)
303  {
304  return;
305  }
306 
307  UpdatePosition();
308  }
309 
317  public static Vector3 GetDirectionFromPivotDirection (ConnectorPivotDirection pivotDirection, Vector3 manualPivotDirection, Transform relativeTo)
318  {
319  Vector3 dir = Vector3.zero;
320  switch (pivotDirection)
321  {
322  case ConnectorPivotDirection.North:
323  dir = Vector3.up;
324  break;
325 
326  case ConnectorPivotDirection.NorthEast:
327  dir = Vector3.Lerp(Vector3.up, Vector3.right, 0.5f).normalized;
328  break;
329 
330  case ConnectorPivotDirection.East:
331  dir = Vector3.right;
332  break;
333 
334  case ConnectorPivotDirection.SouthEast:
335  dir = Vector3.Lerp(Vector3.down, Vector3.right, 0.5f).normalized;
336  break;
337 
338  case ConnectorPivotDirection.South:
339  dir = Vector3.down;
340  break;
341 
342  case ConnectorPivotDirection.SouthWest:
343  dir = Vector3.Lerp(Vector3.down, Vector3.left, 0.5f).normalized;
344  break;
345 
346  case ConnectorPivotDirection.West:
347  dir = Vector3.left;
348  break;
349 
350  case ConnectorPivotDirection.NorthWest:
351  dir = Vector3.Lerp(Vector3.up, Vector3.left, 0.5f).normalized;
352  break;
353 
354  case ConnectorPivotDirection.InFront:
355  dir = Vector3.forward;
356  break;
357 
358  case ConnectorPivotDirection.Manual:
359  dir = manualPivotDirection.normalized;
360  break;
361  }
362 
363  return relativeTo.TransformDirection(dir);
364  }
365  }
366 }
Connects a ToolTip to a target Maintains that connection even if the target moves ...
ConnnectorPivotMode
how is the pivot of the tooltip determined?
Vector3 PivotPosition
point about which ToolTip pivots to face camera
Definition: ToolTip.cs:280
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?
GameObject Anchor
getter/setter for ameObject that the line and text are attached to
Definition: ToolTip.cs:136
ConnectorOrientType
how does the tooltip rotate about the connector
ConnectorFollowType
How does the Tooltip track with its parent object