forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTooltipView.cs
More file actions
129 lines (106 loc) · 4.16 KB
/
TooltipView.cs
File metadata and controls
129 lines (106 loc) · 4.16 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
127
128
129
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace UnityEditor
{
class TooltipView : GUIView
{
private const float MAX_WIDTH = 300.0f;
private GUIContent m_tooltip = new GUIContent();
private Vector2 m_optimalSize;
private GUIStyle m_Style;
private Rect m_hoverRect;
static TooltipView s_guiView;
protected override void OnEnable()
{
base.OnEnable();
s_guiView = this;
}
protected override void OnDisable()
{
base.OnDisable();
s_guiView = null;
}
protected override void OldOnGUI()
{
if (window != null)
{
Color prevColor = GUI.color;
GUI.color = Color.white;
GUI.Box(new Rect(0, 0, m_optimalSize.x, m_optimalSize.y) , m_tooltip, m_Style);
GUI.color = prevColor;
}
}
void Setup(string tooltip, Rect rect, GUIView hostView)
{
m_hoverRect = rect;
m_tooltip.text = tooltip;
// Calculate size and position tooltip view
m_Style = EditorStyles.tooltip;
m_Style.wordWrap = false;
m_optimalSize = m_Style.CalcSize(m_tooltip);
if (m_optimalSize.x > MAX_WIDTH)
{
m_Style.wordWrap = true;
m_optimalSize.x = MAX_WIDTH;
m_optimalSize.y = m_Style.CalcHeight(m_tooltip, MAX_WIDTH);
}
var popupPosition = new Rect(
Mathf.Floor(m_hoverRect.x + (m_hoverRect.width / 2) - (m_optimalSize.x / 2)),
Mathf.Floor(m_hoverRect.y + (m_hoverRect.height) + 10.0f),
m_optimalSize.x, m_optimalSize.y);
if (hostView != null)
{
var viewRect = hostView.screenPosition;
if (popupPosition.x < viewRect.x)
popupPosition.x = viewRect.x;
if (popupPosition.xMax > viewRect.xMax)
popupPosition.x -= popupPosition.xMax - viewRect.xMax;
if (popupPosition.y < viewRect.y)
popupPosition.y = viewRect.y;
if (popupPosition.yMax > viewRect.yMax)
popupPosition.y -= popupPosition.yMax - viewRect.yMax;
popupPosition.y = Mathf.Max(popupPosition.y, Mathf.Floor(m_hoverRect.y + (m_hoverRect.height) + 10.0f));
}
window.position = popupPosition;
position = new Rect(0, 0, m_optimalSize.x, m_optimalSize.y);
window.ShowTooltip();
window.SetAlpha(1.0f);
s_guiView.mouseRayInvisible = true;
RepaintImmediately(); // Force repaint to fix that the tooltip text did sometimes not update (we did not get a new OnGUI if the rect had the same size)
}
public static void Show(string tooltip, Rect rect, GUIView hostView = null)
{
if (s_guiView == null)
{
s_guiView = ScriptableObject.CreateInstance<TooltipView>();
}
if (s_guiView.window == null)
{
var newWindow = ScriptableObject.CreateInstance<ContainerWindow>();
newWindow.m_DontSaveToLayout = true;
newWindow.rootView = s_guiView;
newWindow.SetMinMaxSizes(new Vector2(10.0f, 10.0f), new Vector2(2000.0f, 2000.0f));
s_guiView.SetWindow(newWindow);
}
if (s_guiView.m_tooltip.text == tooltip && rect == s_guiView.m_hoverRect)
return;
s_guiView.Setup(tooltip, rect, hostView);
}
public static void Close()
{
if (s_guiView != null)
s_guiView.window.Close();
}
public static void SetAlpha(float percent)
{
if (s_guiView != null)
s_guiView.window.SetAlpha(percent);
}
}
}