AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ToolTipBackgroundCorners.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 UnityEngine;
6 
7 namespace HoloToolkit.UX.ToolTips
8 {
13  {
14  private enum ScaleModeEnum
15  {
16  World, // Always keep the corners the same size, regardless of tooltip size
17  Local, // Make the corners scale to the tooltip content parent's lossy scale
18  }
19 
20  [SerializeField]
21  private Transform cornerTopLeft = null;
22  [SerializeField]
23  private Transform cornerTopRight = null;
24  [SerializeField]
25  private Transform cornerBotRight = null;
26  [SerializeField]
27  private Transform cornerBotLeft = null;
28  [SerializeField]
29  [Range(0.01f, 2f)]
30  private float cornerScale = 1f;
31  [SerializeField]
32  private ScaleModeEnum scaleMode = ScaleModeEnum.World;
33 
34  protected override void ContentChange()
35  {
36  ScaleToFitContent();
37  }
38 
39  protected override void ScaleToFitContent()
40  {
41  // Get the local size of the content - this is the scale of the text under the content parent
42  Vector3 localContentSize = toolTip.LocalContentSize;
43  localContentSize.z = 1;
44  // Multiply it by 0.5 to get extents
45  localContentSize *= 0.5f;
46  Vector3 localContentOffset = toolTip.LocalContentOffset;
47  // Put the corner objects at the corners
48  Vector3 topLeft = new Vector3(-localContentSize.x + localContentOffset.x, localContentSize.y + localContentOffset.y, localContentOffset.x);
49  Vector3 topRight = new Vector3(localContentSize.x + localContentOffset.x, localContentSize.y + localContentOffset.y, localContentOffset.x);
50  Vector3 botRight = new Vector3(localContentSize.x + localContentOffset.x, -localContentSize.y + localContentOffset.y, localContentOffset.x);
51  Vector3 botLeft = new Vector3(-localContentSize.x + localContentOffset.x, -localContentSize.y + localContentOffset.y, localContentOffset.x);
52  if (cornerTopLeft != null)
53  {
54  cornerTopLeft.localPosition = topLeft;
55  }
56 
57  if (cornerTopRight != null)
58  {
59  cornerTopRight.localPosition = topRight;
60  }
61 
62  if (cornerBotRight != null)
63  {
64  cornerBotRight.localPosition = botRight;
65  }
66 
67  if (cornerBotLeft != null)
68  {
69  cornerBotLeft.localPosition = botLeft;
70  }
71 
72  // Now set the corner object scale based on preference
73  Vector3 globalScale = Vector3.one;
74  switch (scaleMode)
75  {
76  case ScaleModeEnum.World:
77  Vector3 lossyScale = toolTip.ContentParentTransform.lossyScale;
78  globalScale.x /= lossyScale.x;
79  globalScale.y /= lossyScale.y;
80  globalScale.z /= lossyScale.z;
81  break;
82 
83  case ScaleModeEnum.Local:
84  Vector3 localScale = cornerTopLeft.lossyScale;
85  float smallestDimension = Mathf.Min(Mathf.Min(globalScale.x, globalScale.y), globalScale.z);
86  globalScale = Vector3.one * smallestDimension;
87  break;
88 
89  default:
90  throw new System.ArgumentOutOfRangeException("ScaleMode not set to valid enum value.");
91  }
92 
93  if (cornerTopLeft != null)
94  {
95  cornerTopLeft.localScale = globalScale * cornerScale;
96  }
97 
98  if (cornerTopRight != null)
99  {
100  cornerTopRight.localScale = globalScale * cornerScale;
101  }
102 
103  if (cornerBotRight != null)
104  {
105  cornerBotRight.localScale = globalScale * cornerScale;
106  }
107 
108  if (cornerBotLeft != null)
109  {
110  cornerBotLeft.localScale = globalScale * cornerScale;
111  }
112  }
113 
114  private void OnDrawGizmos()
115  {
116  if (Application.isPlaying)
117  {
118  return;
119  }
120 
121  ScaleToFitContent();
122  }
123  }
124 }
Renders meshes at the corners of a tool tip
Base class for a tool tip background Automatically finds a ToolTip and subscribes to ContentChange ac...