Skip to content

Commit

Permalink
Merge pull request #1739 from Microsoft/Button_Icon_Profiles
Browse files Browse the repository at this point in the history
CompountButton Icon Profile Change
  • Loading branch information
StephenHodgson authored Feb 23, 2018
2 parents de46b47 + d51d92e commit fb887a7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine;

namespace MixedRealityToolkit.UX.Buttons.Profiles
Expand Down Expand Up @@ -42,7 +43,7 @@ public abstract class ButtonIconProfile : ButtonProfile
/// Gets a list of icon names - used primarily by editor scripts
/// </summary>
/// <returns></returns>
public virtual List<string> GetIconKeys()
public virtual ReadOnlyCollection<string> GetIconKeys()
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using MixedRealityToolkit.Utilities.Attributes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine;

#if ENABLE_WINMD_SUPPORT && !UNITY_EDITOR
Expand Down Expand Up @@ -99,20 +100,23 @@ public override bool GetIcon(string iconName, MeshRenderer targetRenderer, MeshF
/// <summary>
/// (Icons starting with '_' will not be included in icon list)
/// </summary>
public override List<string> GetIconKeys()
public override ReadOnlyCollection<string> GetIconKeys()
{
Initialize();

return new List<string>(iconKeys);
return iconKeys.AsReadOnly();
}

private void Initialize()
{
if (iconLookup != null)
return;
if (iconLookup == null)
{
iconLookup = new Dictionary<string, Texture2D>();
iconKeys = new List<string>();
}

iconLookup = new Dictionary<string, Texture2D>();
iconKeys = new List<string>();
iconLookup.Clear();
iconKeys.Clear();

// Store all icons in iconLookup via reflection
#if ENABLE_WINMD_SUPPORT && !UNITY_EDITOR
Expand Down Expand Up @@ -148,16 +152,17 @@ private void Initialize()
public override string DrawIconSelectField(string iconName)
{
int selectedIconIndex = -1;
List<string> iconKeys = GetIconKeys();
ReadOnlyCollection<string> iconKeys = GetIconKeys();
string[] dropdownKeys = new string[iconKeys.Count];
for (int i = 0; i < iconKeys.Count; i++)
{
if (iconName == iconKeys[i])
{
selectedIconIndex = i;
break;
}
dropdownKeys[i] = iconKeys[i];
}
int newIconIndex = UnityEditor.EditorGUILayout.Popup("Icon", selectedIconIndex, iconKeys.ToArray());
int newIconIndex = UnityEditor.EditorGUILayout.Popup("Icon", selectedIconIndex, dropdownKeys);
// This will automatically set the icon in the editor view
iconName = (newIconIndex < 0 ? string.Empty : iconKeys[newIconIndex]);
return iconName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public float Alpha
{
alphaTarget = value;
if (Application.isPlaying)
{
if (Mathf.Abs (alpha - alphaTarget) < AlphaThreshold)
{
if (Mathf.Abs(alpha - alphaTarget) < AlphaThreshold)
{
return;
}
Expand Down Expand Up @@ -100,8 +100,8 @@ public MeshFilter IconMeshFilter
return targetIconRenderer != null ? targetIconRenderer.GetComponent<MeshFilter>() : null;
}
}
#if UNITY_EDITOR

#if UNITY_EDITOR
/// <summary>
/// Called by CompoundButtonSaveInterceptor
/// Prevents saving a scene with instanced materials / meshes
Expand All @@ -111,10 +111,10 @@ public void OnWillSaveScene()
ClearInstancedAssets();

SetIconName(iconName);

}
#endif
#endif

public string IconName
{
get
Expand All @@ -130,20 +130,27 @@ public string IconName
private void SetIconName(string newName)
{
// Avoid exploding if possible
if (Profile == null) {
if (Profile == null)
{
return;
}

if (targetIconRenderer == null) {
// Set the name regardless
iconName = newName;

if (targetIconRenderer == null)
{
return;
}

if (DisableIcon) {
if (DisableIcon)
{
targetIconRenderer.enabled = false;
return;
}

if (Profile.IconMaterial == null || Profile.IconMesh == null) {
if (Profile.IconMaterial == null || Profile.IconMesh == null)
{
return;
}

Expand All @@ -156,7 +163,7 @@ private void SetIconName(string newName)
};
}
targetIconRenderer.sharedMaterial = instantiatedMaterial;

// Instantiate our local mesh now, if we don't have one
if (instantiatedMesh == null)
{
Expand All @@ -179,9 +186,8 @@ private void SetIconName(string newName)
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))
Expand All @@ -191,7 +197,6 @@ private void SetIconName(string newName)
}

// If we've made it this far we're golden
iconName = newName;
targetIconRenderer.enabled = true;
RefreshAlpha();
}
Expand Down

0 comments on commit fb887a7

Please sign in to comment.