MenuUtilities.cs
1.99 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
using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace Unity.Cloud.Collaborate.Utilities
{
static class MenuUtilities
{
/// <summary>
/// Corner of the anchor element the dialogue should anchor to.
/// </summary>
public enum AnchorPoint
{
TopLeft,
TopRight,
BottomLeft,
BottomRight
}
/// <summary>
/// Direction the dialogue should open from its anchor.
/// </summary>
public enum OpenDirection
{
UpLeft,
UpRight,
DownLeft,
DownRight
}
/// <summary>
/// Given an element and an anchor point, calculate the world coords to draw a menu at.
/// </summary>
/// <param name="e">Element to start at.</param>
/// <param name="anchorPoint">Corner of the element to calculate.</param>
/// <returns>World coordinates from the given values.</returns>
public static (float X, float Y) GetMenuPosition(VisualElement e, AnchorPoint anchorPoint)
{
// Calculate position of the start corner.
(float x, float y) anchorCoords;
switch (anchorPoint)
{
case AnchorPoint.TopLeft:
anchorCoords = (e.worldBound.xMin, e.worldBound.yMin);
break;
case AnchorPoint.TopRight:
anchorCoords = (e.worldBound.xMax, e.worldBound.yMin);
break;
case AnchorPoint.BottomLeft:
anchorCoords = (e.worldBound.xMin, e.worldBound.yMax);
break;
case AnchorPoint.BottomRight:
anchorCoords = (e.worldBound.xMax, e.worldBound.yMax);
break;
default:
throw new ArgumentOutOfRangeException(nameof(anchorPoint), anchorPoint, null);
}
return anchorCoords;
}
}
}