forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompoundButtonMesh.cs
255 lines (225 loc) · 8.92 KB
/
CompoundButtonMesh.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using UnityEngine;
using HoloToolkit.Unity;
namespace HoloToolkit.Unity.Buttons
{
/// <summary>
/// Mesh button is a mesh renderer interactable with state data for button state
/// </summary>
[RequireComponent(typeof(CompoundButton))]
public class CompoundButtonMesh : ProfileButtonBase<ButtonMeshProfile>
{
const float AnimationSpeedMultiplier = 25f;
[Tooltip("Transform that scale and offset will be applied to.")]
[DropDownComponent]
public Transform TargetTransform;
[Tooltip("Mesh renderer button for mesh button.")]
[DropDownComponent]
public MeshRenderer Renderer;
/// <summary>
/// Mesh Button State Data Set
/// </summary>
[Serializable]
public class MeshButtonDatum
{
/// <summary>
/// Constructor for mesh button datum
/// </summary>
public MeshButtonDatum(ButtonStateEnum state) { this.ActiveState = state; this.Name = state.ToString(); }
/// <summary>
/// Name string for datum entry
/// </summary>
public string Name;
/// <summary>
/// Button state the datum is active in
/// </summary>
public ButtonStateEnum ActiveState;
/// <summary>
/// Button mesh color to use in active state
/// </summary>
public Color StateColor = Color.white;
/// <summary>
/// Button mesh shader property to use in active state
/// </summary>
public float StateValue = 0f;
/// <summary>
/// Offset to translate mesh to in active state.
/// </summary>
public Vector3 Offset;
/// <summary>
/// Scale for mesh button in active state
/// </summary>
public Vector3 Scale;
}
private MeshButtonDatum currentDatum;
/// <summary>
/// The material used by button's Renderer after being modified
/// </summary>
private Material instantiatedMaterial;
/// <summary>
/// The material used by button's Renderer before being modified
/// </summary>
private Material sharedMaterial;
private float lastTimePressed = 0f;
#if UNITY_EDITOR
/// <summary>
/// Called by CompoundButtonSaveInterceptor
/// Prevents saving a scene with instanced materials
/// </summary>
public void OnWillSaveScene ()
{
if (Renderer != null && instantiatedMaterial != null)
{
Renderer.sharedMaterial = sharedMaterial;
GameObject.DestroyImmediate(instantiatedMaterial);
}
}
#endif
protected void Start ()
{
Button button = GetComponent<Button>();
if (button == null)
{
Debug.LogError("No button attached to CompoundButtonMesh in " + name);
enabled = false;
return;
}
if (Profile == null)
{
Debug.LogError("No profile selected for CompoundButtonMesh in " + name);
enabled = false;
return;
}
button.StateChange += StateChange;
// Disable this script if we're not using smooth changes
enabled = Profile.SmoothStateChanges;
// Set the current datum so our first state is activated
currentDatum = Profile.ButtonStates[(int)ButtonStateEnum.Observation];
UpdateButtonProperties(false);
}
/// <summary>
/// On state change swap out the active mesh based on the state
/// </summary>
protected void StateChange(ButtonStateEnum newState)
{
if (newState == ButtonStateEnum.Pressed)
{
lastTimePressed = Time.time;
}
currentDatum = Profile.ButtonStates[(int)newState];
// If we're not using smooth states, just set them now
if (!Profile.SmoothStateChanges)
{
TargetTransform.localScale = currentDatum.Scale;
TargetTransform.localPosition = currentDatum.Offset;
if (Renderer != null)
{
if (instantiatedMaterial == null)
{
sharedMaterial = Renderer.sharedMaterial;
instantiatedMaterial = new Material(sharedMaterial);
Renderer.sharedMaterial = instantiatedMaterial;
}
if (!string.IsNullOrEmpty(Profile.ColorPropertyName))
{
Renderer.sharedMaterial.SetColor(Profile.ColorPropertyName, currentDatum.StateColor);
}
if (!string.IsNullOrEmpty(Profile.ValuePropertyName))
{
Renderer.sharedMaterial.SetFloat(Profile.ValuePropertyName, currentDatum.StateValue);
}
}
}
}
protected void OnDisable()
{
StateChange(ButtonStateEnum.Disabled);
UpdateButtonProperties(false);
}
protected void OnEnable() {
Button button = GetComponent<Button>();
if (button != null) {
StateChange(button.ButtonState);
}
}
protected void Update ()
{
UpdateButtonProperties(true);
}
protected void UpdateButtonProperties(bool smooth)
{
if (currentDatum == null)
{
return;
}
MeshButtonDatum datum = currentDatum;
// If we're using sticky events, and we're still not past the 'sticky' pressed time, use that datum
if (Profile.StickyPressedEvents && Time.time < lastTimePressed + Profile.StickyPressedTime)
{
datum = Profile.ButtonStates[(int)ButtonStateEnum.Pressed];
}
if (TargetTransform != null)
{
if (smooth)
{
TargetTransform.localScale = Vector3.Lerp(
TargetTransform.localScale, datum.Scale,
Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier);
TargetTransform.localPosition = Vector3.Lerp(
TargetTransform.localPosition, datum.Offset,
Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier);
} else
{
TargetTransform.localScale = datum.Scale;
TargetTransform.localPosition = datum.Offset;
}
}
// Set the color from the datum
if (Renderer != null)
{
if (instantiatedMaterial == null)
{
sharedMaterial = Renderer.sharedMaterial;
instantiatedMaterial = new Material(sharedMaterial);
Renderer.sharedMaterial = instantiatedMaterial;
}
if (!string.IsNullOrEmpty(Profile.ColorPropertyName))
{
if (smooth)
{
Renderer.sharedMaterial.SetColor(
Profile.ColorPropertyName,
Color.Lerp(Renderer.material.GetColor(Profile.ColorPropertyName),
datum.StateColor,
Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier));
} else
{
Renderer.sharedMaterial.SetColor(
Profile.ColorPropertyName,
datum.StateColor);
}
}
if (!string.IsNullOrEmpty(Profile.ValuePropertyName))
{
if (smooth)
{
Renderer.sharedMaterial.SetFloat(
Profile.ValuePropertyName,
Mathf.Lerp(Renderer.material.GetFloat(Profile.ValuePropertyName),
datum.StateValue,
Time.deltaTime * Profile.AnimationSpeed * AnimationSpeedMultiplier));
} else
{
Renderer.sharedMaterial.SetFloat(Profile.ValuePropertyName, datum.StateValue);
}
}
}
}
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(CompoundButtonMesh))]
public class CustomEditor : MRTKEditor { }
#endif
}
}