-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHandLauncher.cs
More file actions
228 lines (195 loc) · 9.04 KB
/
HandLauncher.cs
File metadata and controls
228 lines (195 loc) · 9.04 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using Nova;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace NovaSamples.HandMenu
{
/// <summary>
/// A set of components used to display the details of a <see cref="PanelItem"/>.
/// </summary>
[Serializable]
public class PanelItemVisuals : ItemVisuals
{
[Tooltip("Will display the icon of the panel to open when selected.")]
public UIBlock2D Icon;
[Tooltip("The background visual to style to the theme of the panel.")]
public UIBlock2D Background;
}
/// <summary>
/// Data associated with a particular <see cref="Panel"/>.
/// </summary>
[Serializable]
public struct PanelItem
{
[Tooltip("The panel itself.")]
public Panel Panel;
[Tooltip("The icon associated with the given panel.")]
public Texture2D Icon;
[Tooltip("The primary panel \"theme\" color.")]
public Color PrimaryColor;
[Tooltip("The secondary panel \"theme\" color.")]
public Color SecondaryColor;
}
/// <summary>
/// A component attached to the users hand which will, when enabled, display a scrollable list of buttons capable of launching different Panel UIs.
/// </summary>
public class HandLauncher : MonoBehaviour
{
/// <summary>
/// The event fired when a given <see cref="PanelItem"/> is selected in the list.
/// </summary>
public event Action<PanelItem> OnPanelSelected;
[Header("List")]
[SerializeField]
[Tooltip("The scrollable List View to display the set of buttons which each, when clicked, launch a different Panel UI.")]
private ListView listView = null;
[SerializeField]
[Tooltip("The set of Panels to display in the List View.")]
private List<PanelItem> panels = null;
[Header("Animations")]
[SerializeField]
[Tooltip("The animation to run to fade in the menu.")]
private ClipMaskTintAnimation fadeInAnimation = default;
[SerializeField]
[Tooltip("The animation to run to fade out the menu.")]
private ClipMaskTintAnimation fadeOutAnimation = default;
[SerializeField]
[Tooltip("The animation to chain with a given fade animation to enable/disable this game object.")]
private ActivateGameObjectAnimation activateAnimation;
[SerializeField]
[Tooltip("The duration, in seconds, of a given fade in/out animation.")]
private float fadeAnimationDuration = .15f;
/// <summary>
/// The handle tracking any active fade in/out animations
/// </summary>
private AnimationHandle fadeAnimationHandle = default;
private void Start()
{
// Ensure faded out on start
fadeOutAnimation.Run(0).Complete();
// Subscribe to PanelItem <-> PanelItemVisuals bind events.
listView.AddDataBinder<PanelItem, PanelItemVisuals>(Bind);
// Subscribe to PanelItemVisuals click events
listView.AddGestureHandler<Gesture.OnClick, PanelItemVisuals>(Click);
// Set the list of panels as the listView's data source.
// If the listView is enabled, this will start populating
// the list with list items, which is why we must subscribe
// to the bind events before assigning a data source.
listView.SetDataSource(panels);
// Subscribe to scroll events on the listView's UIBlock.
// We subscribe on the UIBlock here, as opposed to the listView
// directly, because it's not a "list item" that's going to be scrolled
// but rather the listView itself.
listView.UIBlock.AddGestureHandler<Gesture.OnScroll>(Scrolled);
}
/// <summary>
/// Animate in this hand launcher UI.
/// </summary>
public void Show()
{
fadeAnimationHandle.Cancel();
activateAnimation.TargetActive = true;
fadeAnimationHandle = activateAnimation.Run(0f).Chain(fadeInAnimation, fadeAnimationDuration);
}
/// <summary>
/// Animate out this hand launcher UI.
/// </summary>
public void Hide()
{
fadeAnimationHandle.Cancel();
activateAnimation.TargetActive = false;
fadeAnimationHandle = fadeOutAnimation.Run(fadeAnimationDuration).Chain(activateAnimation, 0f);
}
/// <summary>
/// On click, fire a corresponding OnPanelSelected event.
/// </summary>
private void Click(Gesture.OnClick evt, PanelItemVisuals target, int index)
{
OnPanelSelected?.Invoke(panels[index]);
}
/// <summary>
/// Populate a <see cref="PanelItemVisuals"/> object in the <see cref="listView"/> with
/// the information from its corresponding <see cref="PanelItem"/>
/// object in the data source.
/// </summary>
private void Bind(Data.OnBind<PanelItem> evt, PanelItemVisuals target, int index)
{
// The UserData on this bind event is the same value stored
// at the given `index` into the list of panels.
//
// I.e.
// evt.UserData == panels[index]
PanelItem panel = evt.UserData;
// Assign the icon and theme the visuals with
// the primary/secondary panel colors.
target.Icon.SetImage(panel.Icon);
target.Icon.Gradient.Color = panel.PrimaryColor;
target.Background.Border.Color = panel.PrimaryColor;
target.Background.Shadow.Color = panel.SecondaryColor;
// Because the list item is just now being bound into view
// its layout properties are likely stale or uncalculated, since the
// Nova Engine update won't run until the end of the current frame.
//
// Explicitly call CalculateLayout() here to ensure the size/position
// of this list item have non-zero calculated values before we try to
// use them to determine the radial adjustment.
target.View.UIBlock.CalculateLayout();
// Adjust the X offset along the scrolling arc.
ApplyRadialAdjustment(target.View);
}
/// <summary>
/// On scroll, adjust the z position of the contact cards in view to give the visual effect
/// that they are scrolling along an arc where the most centered item is closer to the user.
/// </summary>
private void Scrolled(Gesture.OnScroll evt)
{
// Get the min/max range of items in view
int minIndex = listView.MinLoadedIndex;
int maxIndex = listView.MaxLoadedIndex;
for (int i = minIndex; i <= maxIndex; ++i)
{
// Get the list item visually representing panels[i]
if (!listView.TryGetItemView(i, out ItemView listItem))
{
// We won't hit this in this sample (assuming no modifications),
// but it's good practice to validate the item hasn't been
// destroyed or detatched.
continue;
}
// Apply X offsets to scroll along an arc.
ApplyRadialAdjustment(listItem);
}
}
/// <summary>
/// A visual effect applied to the items in the <see cref="listView"/>,
/// which will adjust their x position to appear as if the objects are scrolling
/// along an arc.
/// </summary>
private void ApplyRadialAdjustment(ItemView listItem)
{
UIBlock uiBlock = listItem.UIBlock;
// The effective "radius" of the arc determined
// by the height of the viewport and list item
float radius = listView.UIBlock.PaddedSize.y * 0.5f + uiBlock.CalculatedSize.Y.Value;
// The center position of the item in view
float yPos = uiBlock.transform.localPosition.y;
// Convert the y position into a sine value between [-1, 1]
float sinTheta = Mathf.Clamp(yPos / radius, -1, 1);
// Get the angle, theta, from the calculated sine
float theta = Mathf.Asin(sinTheta);
// And get the corresponding cosine value of theta. This is effectively
// our "normalized" x position.
//
// More centered => closer to 1
// towards edges => closer to 0
float cosTheta = Mathf.Cos(theta);
// Ensure the items are aligned to the left of listView bounds
uiBlock.Alignment.X = HorizontalAlignment.Left;
// Here we offset from the left edge of the list by the
// normalizedXPosition - normalizedXSize - normalizedLeftMargin.
// This will lead to center-most items aligned to the right edge
// and items towards the top/bottom will lie on the left edge.
uiBlock.Position.X.Percent = cosTheta - uiBlock.CalculatedSize.X.Percent - uiBlock.CalculatedMargin.Left.Percent;
}
}
}