forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppBarButton.cs
187 lines (163 loc) · 6.37 KB
/
AppBarButton.cs
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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
//
using HoloToolkit.Unity.Buttons;
using UnityEngine;
namespace HoloToolkit.Unity.UX
{
/// <summary>
/// Button logic for the App Bar. Determines position of the button in the App Bar, visibility based on the current mode.
/// </summary>
public class AppBarButton : MonoBehaviour
{
private ButtonIconProfile customIconProfile;
private AppBar.ButtonTemplate template;
private Vector3 targetPosition;
private Vector3 defaultOffset;
private Vector3 hiddenOffset;
private Vector3 manipulationOffset;
private CompoundButton cButton;
private Renderer highlightMeshRenderer;
private CompoundButtonText text;
private CompoundButtonIcon icon;
private AppBar parentToolBar;
private bool initialized = false;
public const float ButtonWidth = 0.12f;
public const float ButtonDepth = 0.0001f;
const float MoveSpeed = 5f;
public void Initialize(AppBar newParentToolBar, AppBar.ButtonTemplate newTemplate, ButtonIconProfile newCustomProfile)
{
template = newTemplate;
customIconProfile = newCustomProfile;
parentToolBar = newParentToolBar;
cButton = GetComponent<CompoundButton>();
cButton.MainRenderer.enabled = false;
text = GetComponent<CompoundButtonText>();
text.Text = template.Text;
icon = GetComponent<CompoundButtonIcon>();
highlightMeshRenderer = cButton.GetComponent<CompoundButtonMesh>().Renderer;
if (customIconProfile != null)
{
icon.Profile = customIconProfile;
icon.IconName = string.Empty;
}
icon.IconName = template.Icon;
initialized = true;
Hide();
if (newTemplate.EventTarget != null)
{
// Register the button with its target interactable
newTemplate.EventTarget.Registerinteractable(gameObject);
} else
{
// Register the button with the parent app bar
newParentToolBar.Registerinteractable(gameObject);
}
}
protected void OnEnable ()
{
Hide();
}
protected void Update()
{
if (!initialized)
return;
RefreshOffsets();
switch (parentToolBar.State)
{
case AppBar.AppBarStateEnum.Default:
// Show hide, adjust, remove buttons
// The rest are hidden
targetPosition = defaultOffset;
switch (template.Type)
{
case AppBar.ButtonTypeEnum.Hide:
case AppBar.ButtonTypeEnum.Remove:
case AppBar.ButtonTypeEnum.Adjust:
case AppBar.ButtonTypeEnum.Custom:
Show();
break;
default:
Hide();
break;
}
break;
case AppBar.AppBarStateEnum.Hidden:
// Show show button
// The rest are hidden
targetPosition = hiddenOffset;
switch (template.Type)
{
case AppBar.ButtonTypeEnum.Show:
Show();
break;
default:
Hide();
break;
}
break;
case AppBar.AppBarStateEnum.Manipulation:
// Show done / remove buttons
// The rest are hidden
targetPosition = manipulationOffset;
switch (template.Type)
{
case AppBar.ButtonTypeEnum.Done:
case AppBar.ButtonTypeEnum.Remove:
Show();
break;
default:
Hide();
break;
}
break;
}
transform.localPosition = Vector3.Lerp(transform.localPosition, targetPosition, 0.5f);
}
private void Hide()
{
if (!initialized)
return;
icon.Alpha = 0f;
text.DisableText = true;
cButton.enabled = false;
highlightMeshRenderer.enabled = false;
cButton.gameObject.layer = LayerMask.NameToLayer("Ignore Raycast");
}
private void Show()
{
if (!initialized)
return;
icon.Alpha = 1f;
text.DisableText = false;
cButton.enabled = true;
highlightMeshRenderer.enabled = true;
cButton.gameObject.layer = LayerMask.NameToLayer("UI");
}
private void RefreshOffsets()
{
// Apply offset based on total number of buttons
float xDefaultOffset = (parentToolBar.NumDefaultButtons / 2) * ButtonWidth;
float xManipulationOffset = (parentToolBar.NumManipulationButtons / 2) * ButtonWidth;
// For odd numbers of buttons, add an additional 1/2 button offset
if (parentToolBar.NumDefaultButtons > 1 && parentToolBar.NumDefaultButtons % 2 == 0)
{
xDefaultOffset -= (ButtonWidth / 2);
}
if (parentToolBar.NumManipulationButtons > 1 && parentToolBar.NumManipulationButtons % 2 == 0)
{
xManipulationOffset -= (ButtonWidth / 2);
}
defaultOffset = new Vector3(
template.DefaultPosition * ButtonWidth - xDefaultOffset,
0f,
template.DefaultPosition * ButtonDepth);
manipulationOffset = new Vector3(
template.ManipulationPosition * ButtonWidth - xManipulationOffset,
0f,
template.ManipulationPosition * ButtonDepth);
hiddenOffset = Vector3.zero;
}
}
}