AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ToolTipUtility.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 
6 namespace HoloToolkit.UX.ToolTips
7 {
12  public static class ToolTipUtility
13  {
14  private const int NumPivotLocations = 8;
15 
25  public static Vector3 FindClosestAttachPointToAnchor(Transform anchor, Transform contentParent, Vector3[] localPivotPositions, ToolTipAttachPointType pivotType)
26  {
27  Vector3 nearPivot = Vector3.zero;
28  Vector3 currentPivot = Vector3.zero;
29  Vector3 anchorPosition = anchor.position;
30  float nearDist = Mathf.Infinity;
31 
32  if (localPivotPositions == null || localPivotPositions.Length < NumPivotLocations)
33  {
34  return nearPivot;
35  }
36 
37  switch (pivotType)
38  {
39  case ToolTipAttachPointType.Center:
40  return nearPivot;
41 
42  // Search all pivots
43  case ToolTipAttachPointType.Closest:
44  for (int i = 0; i < localPivotPositions.Length; i++)
45  {
46  currentPivot = localPivotPositions[i];
47  float dist = Vector3.Distance(anchorPosition, contentParent.TransformPoint(currentPivot));
48  if (dist < nearDist)
49  {
50  nearDist = dist;
51  nearPivot = currentPivot;
52  }
53  }
54  break;
55 
56  // Search corner pivots
57  case ToolTipAttachPointType.ClosestCorner:
58  for (int i = (int)ToolTipAttachPointType.BotRightCorner; i < (int)ToolTipAttachPointType.TopLeftCorner; i++)
59  {
60  currentPivot = localPivotPositions[i];
61  float dist = Vector3.Distance(anchorPosition, contentParent.TransformPoint(currentPivot));
62  if (dist < nearDist)
63  {
64  nearDist = dist;
65  nearPivot = currentPivot;
66  }
67  }
68  break;
69 
70  // Search middle pivots
71  case ToolTipAttachPointType.ClosestMiddle:
72  for (int i = (int)ToolTipAttachPointType.BotMiddle; i < (int)ToolTipAttachPointType.LeftMiddle; i++)
73  {
74  currentPivot = localPivotPositions[i];
75  float dist = Vector3.Distance(anchorPosition, contentParent.TransformPoint(currentPivot));
76  if (dist < nearDist)
77  {
78  nearDist = dist;
79  nearPivot = currentPivot;
80  }
81  }
82  break;
83 
84  default:
85  // For all other types, just use the array position or contentParent
86  //position if there is no array provided.
87  if (localPivotPositions == null || localPivotPositions.Length == 0)
88  {
89  nearPivot = contentParent.position;
90  }
91  else
92  {
93  nearPivot = localPivotPositions[(int)pivotType];
94  }
95  break;
96  }
97 
98  return nearPivot;
99  }
105  public static void GetAttachPointPositions(ref Vector3[] pivotPositions, Vector2 localContentSize)
106  {
107  if (pivotPositions == null || pivotPositions.Length < NumPivotLocations)
108  {
109  pivotPositions = new Vector3[NumPivotLocations];
110  }
111 
112  //Get the extents of our content size
113  localContentSize *= 0.5f;
114 
115  pivotPositions[(int)ToolTipAttachPointType.BotMiddle] = new Vector3(0f, -localContentSize.y, 0f);
116  pivotPositions[(int)ToolTipAttachPointType.TopMiddle] = new Vector3(0f, localContentSize.y, 0f);
117  pivotPositions[(int)ToolTipAttachPointType.LeftMiddle] = new Vector3(-localContentSize.x, 0f, 0f); // was right
118  pivotPositions[(int)ToolTipAttachPointType.RightMiddle] = new Vector3(localContentSize.x, 0f, 0f); // was left
119 
120  pivotPositions[(int)ToolTipAttachPointType.BotLeftCorner] = new Vector3(-localContentSize.x, -localContentSize.y, 0f); // was right
121  pivotPositions[(int)ToolTipAttachPointType.BotRightCorner] = new Vector3(localContentSize.x, -localContentSize.y, 0f); // was left
122  pivotPositions[(int)ToolTipAttachPointType.TopLeftCorner] = new Vector3(-localContentSize.x, localContentSize.y, 0f); // was right
123  pivotPositions[(int)ToolTipAttachPointType.TopRightCorner] = new Vector3(localContentSize.x, localContentSize.y, 0f); // was left
124  }
125  }
126 }
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...
static void GetAttachPointPositions(ref Vector3[] pivotPositions, Vector2 localContentSize)
gets an array of pivot positions
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.