forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolTipUtility.cs
More file actions
126 lines (113 loc) · 5.97 KB
/
ToolTipUtility.cs
File metadata and controls
126 lines (113 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
namespace HoloToolkit.UX.ToolTips
{
/// <summary>
/// Static class providing useful functions for
/// finding ToolTip Attachpoint information.
/// </summary>
public static class ToolTipUtility
{
private const int NumPivotLocations = 8;
/// <summary>
/// Avoid running this query in Update function because calculating Vector3.Distance requires sqr root calculation (expensive)
/// Instead, find strategic moments to update nearest pivot (i.e. only once when ToolTip becomes active)
/// </summary>
/// <param name="anchor">Transform of object serving as anchor for tooltip</param>
/// <param name="contentParent">Transform for the tooltip content</param>
/// <param name="localPivotPositions">list of positions to find the closest</param>
/// <param name="pivotType">pivot type needed for calculation of closest</param>
/// <returns>Vector3 the point in localPivotPositions which is closest to the anchor position</returns>
public static Vector3 FindClosestAttachPointToAnchor(Transform anchor, Transform contentParent, Vector3[] localPivotPositions, ToolTipAttachPointType pivotType)
{
Vector3 nearPivot = Vector3.zero;
Vector3 currentPivot = Vector3.zero;
Vector3 anchorPosition = anchor.position;
float nearDist = Mathf.Infinity;
if (localPivotPositions == null || localPivotPositions.Length < NumPivotLocations)
{
return nearPivot;
}
switch (pivotType)
{
case ToolTipAttachPointType.Center:
return nearPivot;
// Search all pivots
case ToolTipAttachPointType.Closest:
for (int i = 0; i < localPivotPositions.Length; i++)
{
currentPivot = localPivotPositions[i];
float dist = Vector3.Distance(anchorPosition, contentParent.TransformPoint(currentPivot));
if (dist < nearDist)
{
nearDist = dist;
nearPivot = currentPivot;
}
}
break;
// Search corner pivots
case ToolTipAttachPointType.ClosestCorner:
for (int i = (int)ToolTipAttachPointType.BotRightCorner; i < (int)ToolTipAttachPointType.TopLeftCorner; i++)
{
currentPivot = localPivotPositions[i];
float dist = Vector3.Distance(anchorPosition, contentParent.TransformPoint(currentPivot));
if (dist < nearDist)
{
nearDist = dist;
nearPivot = currentPivot;
}
}
break;
// Search middle pivots
case ToolTipAttachPointType.ClosestMiddle:
for (int i = (int)ToolTipAttachPointType.BotMiddle; i < (int)ToolTipAttachPointType.LeftMiddle; i++)
{
currentPivot = localPivotPositions[i];
float dist = Vector3.Distance(anchorPosition, contentParent.TransformPoint(currentPivot));
if (dist < nearDist)
{
nearDist = dist;
nearPivot = currentPivot;
}
}
break;
default:
// For all other types, just use the array position or contentParent
//position if there is no array provided.
if (localPivotPositions == null || localPivotPositions.Length == 0)
{
nearPivot = contentParent.position;
}
else
{
nearPivot = localPivotPositions[(int)pivotType];
}
break;
}
return nearPivot;
}
/// <summary>
/// gets an array of pivot positions
/// </summary>
/// <param name="pivotPositions">ref array that gets filled with positions</param>
/// <param name="localContentSize">the xy scale of the tooltip</param>
public static void GetAttachPointPositions(ref Vector3[] pivotPositions, Vector2 localContentSize)
{
if (pivotPositions == null || pivotPositions.Length < NumPivotLocations)
{
pivotPositions = new Vector3[NumPivotLocations];
}
//Get the extents of our content size
localContentSize *= 0.5f;
pivotPositions[(int)ToolTipAttachPointType.BotMiddle] = new Vector3(0f, -localContentSize.y, 0f);
pivotPositions[(int)ToolTipAttachPointType.TopMiddle] = new Vector3(0f, localContentSize.y, 0f);
pivotPositions[(int)ToolTipAttachPointType.LeftMiddle] = new Vector3(-localContentSize.x, 0f, 0f); // was right
pivotPositions[(int)ToolTipAttachPointType.RightMiddle] = new Vector3(localContentSize.x, 0f, 0f); // was left
pivotPositions[(int)ToolTipAttachPointType.BotLeftCorner] = new Vector3(-localContentSize.x, -localContentSize.y, 0f); // was right
pivotPositions[(int)ToolTipAttachPointType.BotRightCorner] = new Vector3(localContentSize.x, -localContentSize.y, 0f); // was left
pivotPositions[(int)ToolTipAttachPointType.TopLeftCorner] = new Vector3(-localContentSize.x, localContentSize.y, 0f); // was right
pivotPositions[(int)ToolTipAttachPointType.TopRightCorner] = new Vector3(localContentSize.x, localContentSize.y, 0f); // was left
}
}
}