forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompoundButtonIcon.cs
316 lines (274 loc) · 9.56 KB
/
CompoundButtonIcon.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using HoloToolkit.Unity;
using System.Collections;
using UnityEngine;
namespace HoloToolkit.Unity.Buttons
{
[ExecuteInEditMode]
[RequireComponent(typeof(CompoundButton))]
public class CompoundButtonIcon : ProfileButtonBase<ButtonIconProfile>
{
[Header("Icon Settings")]
[SerializeField]
[DropDownComponent]
private MeshRenderer targetIconRenderer = null;
[Tooltip("Turns off the icon entirely")]
public bool DisableIcon = false;
[Tooltip("Disregard the icon in the profile and use Icon Override instead")]
public bool OverrideIcon = false;
[SerializeField]
[HideInMRTKInspector]
private string iconName;
[SerializeField]
[ShowIfBoolValue("OverrideIcon")]
[Tooltip("Icon to use for override")]
private Texture2D iconOverride = null;
[SerializeField]
[Tooltip("Alpha value for the text mesh component")]
private float alpha = 1f;
private Material instantiatedMaterial;
private Mesh instantiatedMesh;
private bool updatingAlpha = false;
private float alphaTarget = 1f;
private const float AlphaThreshold = 0.01f;
/// <summary>
/// Property to use in IconMaterial for alpha control
/// Useful for animating icon transparency
/// </summary>
public float Alpha
{
get
{
return alphaTarget;
}
set
{
if (alphaTarget != value)
{
alphaTarget = value;
if (Application.isPlaying)
{
if (Mathf.Abs (alpha - alphaTarget) < AlphaThreshold)
{
return;
}
if (updatingAlpha)
{
return;
}
if (gameObject.activeSelf && gameObject.activeInHierarchy)
{
// Update over time
updatingAlpha = true;
StartCoroutine(UpdateAlpha());
}
else
{
// If we're not active, just set the alpha immediately
alpha = alphaTarget;
RefreshAlpha();
}
}
else
{
alphaTarget = value;
alpha = alphaTarget;
RefreshAlpha();
}
}
}
}
public MeshFilter IconMeshFilter
{
get
{
return targetIconRenderer != null ? targetIconRenderer.GetComponent<MeshFilter>() : null;
}
}
#if UNITY_EDITOR
/// <summary>
/// Called by CompoundButtonSaveInterceptor
/// Prevents saving a scene with instanced materials / meshes
/// </summary>
public void OnWillSaveScene()
{
ClearInstancedAssets();
SetIconName(iconName);
}
#endif
public string IconName
{
get
{
return iconName;
}
set
{
SetIconName(value);
}
}
private void SetIconName(string newName)
{
// Avoid exploding if possible
if (Profile == null) {
return;
}
if (targetIconRenderer == null) {
return;
}
if (DisableIcon) {
targetIconRenderer.enabled = false;
return;
}
if (Profile.IconMaterial == null || Profile.IconMesh == null) {
return;
}
// Instantiate our local material now, if we don't have one
if (instantiatedMaterial == null)
{
instantiatedMaterial = new Material(Profile.IconMaterial);
instantiatedMaterial.name = Profile.IconMaterial.name;
}
targetIconRenderer.sharedMaterial = instantiatedMaterial;
// Instantiate our local mesh now, if we don't have one
if (instantiatedMesh == null)
{
instantiatedMesh = Instantiate(Profile.IconMesh);
instantiatedMesh.name = Profile.IconMesh.name;
}
IconMeshFilter.sharedMesh = instantiatedMesh;
if (OverrideIcon)
{
// Use the default mesh for override icons
targetIconRenderer.enabled = true;
IconMeshFilter.sharedMesh = Profile.IconMesh;
IconMeshFilter.transform.localScale = Vector3.one;
instantiatedMaterial.mainTexture = iconOverride;
return;
}
// Disable the renderer if the name is empty
if (string.IsNullOrEmpty(newName))
{
targetIconRenderer.enabled = false;
iconName = newName;
return;
}
// Moment of truth - try to get our icon
if (!Profile.GetIcon(newName, targetIconRenderer, IconMeshFilter, true))
{
targetIconRenderer.enabled = false;
return;
}
// If we've made it this far we're golden
iconName = newName;
targetIconRenderer.enabled = true;
RefreshAlpha();
}
private void OnDisable()
{
ClearInstancedAssets();
}
private void OnEnable()
{
if (Application.isPlaying)
{
ClearInstancedAssets();
}
SetIconName(iconName);
}
private void Start()
{
SetIconName(iconName);
}
private void RefreshAlpha()
{
string alphaColorProperty = string.IsNullOrEmpty(Profile.AlphaColorProperty) ? "_Color" : Profile.AlphaColorProperty;
if (instantiatedMaterial != null)
{
Color c = instantiatedMaterial.GetColor(alphaColorProperty);
c.a = alpha;
instantiatedMaterial.SetColor(alphaColorProperty, c);
}
}
private void ClearInstancedAssets()
{
// Prevent material leaks
if (instantiatedMaterial != null)
{
if (Application.isPlaying)
{
Destroy(instantiatedMaterial);
}
else
{
DestroyImmediate(instantiatedMaterial);
}
instantiatedMaterial = null;
}
if (instantiatedMesh != null)
{
if (Application.isPlaying)
{
Destroy(instantiatedMesh);
}
else
{
DestroyImmediate(instantiatedMesh);
}
instantiatedMesh = null;
}
// Reset to default mats and meshes
if (Profile != null)
{
if (targetIconRenderer != null)
{
// Restore the icon material to the renderer
targetIconRenderer.sharedMaterial = Profile.IconMaterial;
}
if (IconMeshFilter != null)
{
IconMeshFilter.sharedMesh = Profile.IconMesh;
}
}
// Reset our alpha to the alpha target
alpha = alphaTarget;
}
private IEnumerator UpdateAlpha()
{
float startTime = Time.time;
Color color = Color.white;
string alphaColorProperty = string.IsNullOrEmpty(Profile.AlphaColorProperty) ? "_Color" : Profile.AlphaColorProperty;
if (instantiatedMaterial != null)
{
color = instantiatedMaterial.GetColor(alphaColorProperty);
color.a = alpha;
}
while (Time.time < startTime + Profile.AlphaTransitionSpeed)
{
alpha = Mathf.Lerp(alpha, alphaTarget, (Time.time - startTime) / Profile.AlphaTransitionSpeed);
if (instantiatedMaterial != null && !string.IsNullOrEmpty(alphaColorProperty))
{
color.a = alpha;
instantiatedMaterial.SetColor(alphaColorProperty, color);
}
yield return null;
}
alpha = alphaTarget;
RefreshAlpha();
updatingAlpha = false;
}
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(CompoundButtonIcon))]
public class CustomEditor : MRTKEditor {
protected override void DrawCustomFooter() {
CompoundButtonIcon iconButton = (CompoundButtonIcon)target;
if (iconButton != null && iconButton.Profile != null)
{
iconButton.IconName = iconButton.Profile.DrawIconSelectField(iconButton.iconName);
}
}
}
#endif
}
}