FloatingDialogue.cs
6.25 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using Unity.Cloud.Collaborate.Assets;
using Unity.Cloud.Collaborate.UserInterface;
using Unity.Cloud.Collaborate.Utilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace Unity.Cloud.Collaborate.Components.Menus
{
/// <summary>
/// Global element that is used to display dialogues throughout the window.
/// </summary>
internal class FloatingDialogue : VisualElement
{
/// <summary>
/// USS class name of elements of this type.
/// </summary>
public const string UssClassName = "unity-floating-dialogue";
/// <summary>
/// Location the uss file for this element is stored.
/// </summary>
static readonly string k_StylePath = $"{CollaborateWindow.StylePath}/{nameof(FloatingDialogue)}.uss";
/// <summary>
/// Backing field for the singleton.
/// </summary>
static FloatingDialogue s_Instance;
/// <summary>
/// Singleton for accessing the global FloatingDialogue
/// </summary>
public static FloatingDialogue Instance => s_Instance ?? (s_Instance = new FloatingDialogue());
/// <summary>
/// Constructor used by the singleton.
/// </summary>
FloatingDialogue()
{
AddToClassList(UssClassName);
AddToClassList(UiConstants.ussHidden);
styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>(k_StylePath));
}
/// <summary>
/// Allows focus to be set when the window opens. Allows for the options to be next up for tab.
/// </summary>
public override bool canGrabFocus => true;
/// <summary>
/// Set the position of the dialogue.
/// </summary>
/// <param name="x">World x coordinate.</param>
/// <param name="y">World y coordinate.</param>
/// <param name="content">Content of the window.</param>
/// <param name="openDirection">Direction to open the dialogue towards.</param>
void SetPosition(float x, float y, VisualElement content, MenuUtilities.OpenDirection openDirection)
{
// Correct for the top left corner of the element based on the direction it opens.
switch (openDirection)
{
case MenuUtilities.OpenDirection.UpLeft:
x -= content.worldBound.width;
y -= content.worldBound.height;
break;
case MenuUtilities.OpenDirection.UpRight:
y -= content.worldBound.height;
break;
case MenuUtilities.OpenDirection.DownLeft:
x -= content.worldBound.width;
break;
case MenuUtilities.OpenDirection.DownRight:
break;
default:
throw new ArgumentOutOfRangeException(nameof(openDirection), openDirection, null);
}
// Set the position.
style.top = y - parent.worldBound.yMin;
style.left = x - parent.worldBound.xMin;
style.right = new StyleLength(StyleKeyword.Auto);
style.bottom = new StyleLength(StyleKeyword.Auto);
}
/// <summary>
/// Open the dialogue.
/// </summary>
/// <param name="x">World x coordinate.</param>
/// <param name="y">World y coordinate.</param>
/// <param name="content">Content to display.</param>
/// <param name="openDirection">Direction to open the dialogue towards.</param>
internal void Open(float x, float y, VisualElement content, MenuUtilities.OpenDirection openDirection = MenuUtilities.OpenDirection.DownLeft)
{
// Set new content
Clear();
Add(content);
// Set visible and give focus
RemoveFromClassList(UiConstants.ussHidden);
Focus();
BringToFront();
// Catch scrolling to avoid menu becoming detached.
parent.RegisterCallback<WheelEvent>(OnScroll, TrickleDown.TrickleDown);
// Catch clicks outside of the dialogue and close it.
parent.RegisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
// Catch window size changes so that the menu position can be fixed.
parent.RegisterCallback<GeometryChangedEvent>(OnGeometryChange, TrickleDown.TrickleDown);
content.RegisterCallback<GeometryChangedEvent>(SizeKnownCallback);
void SizeKnownCallback(GeometryChangedEvent _)
{
// Unregister now that we know it has a size.
content.UnregisterCallback<GeometryChangedEvent>(SizeKnownCallback);
// Set the position in the window
SetPosition(x, y, content, openDirection);
}
}
/// <summary>
/// Close the dialogue.
/// </summary>
internal void Close()
{
AddToClassList(UiConstants.ussHidden);
Clear();
// Avoid wasting extra cycles when closed.
parent.UnregisterCallback<WheelEvent>(OnScroll, TrickleDown.TrickleDown);
parent.UnregisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
}
/// <summary>
/// On scroll event.
/// </summary>
/// <param name="evt">Event data.</param>
void OnScroll(WheelEvent evt)
{
// Close the window if the user scrolls outside the dialogue.
if (!worldBound.Contains(evt.mousePosition))
{
Close();
}
}
/// <summary>
/// Mouse down event.
/// </summary>
/// <param name="evt">Event data.</param>
void OnMouseDown(MouseDownEvent evt)
{
// Close the window if the user clicks outside the dialogue.
if (!worldBound.Contains(evt.mousePosition))
{
Close();
}
}
/// <summary>
/// Geometry changed event.
/// </summary>
/// <param name="evt">Event data.</param>
void OnGeometryChange(GeometryChangedEvent evt)
{
// Close to avoid the dialogue getting detached.
Close();
}
}
}