Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause

Expand All @@ -13,12 +12,6 @@ namespace MixedReality.Toolkit.Editor
[CanEditMultipleObjects]
class FontIconSelectorInspector : UnityEditor.Editor
{

private const string defaultShaderName = "TextMeshPro/Distance Field SSD";
private float fontTileSize = 32;

private static Material fontRenderMaterial;

private const string noFontIconsMessage = "No IconFontSet profile selected. No icons available.";
private const string emptyFontIconSetMessage = "The selected IconFontSet profile has no icons defined. Please edit the IconFontSet.";

Expand All @@ -29,6 +22,7 @@ class FontIconSelectorInspector : UnityEditor.Editor
private GUIStyle currentButtonStyle;

private bool initializedStyle = false;
private float fontTileSize = 32;

/// <summary>
/// A Unity event function that is called when the script component has been enabled.
Expand All @@ -38,7 +32,6 @@ private void OnEnable()
fontIconsProp = serializedObject.FindProperty("fontIcons");
currentIconNameProp = serializedObject.FindProperty("currentIconName");
tmProProp = serializedObject.FindProperty("textMeshProComponent");

}

/// <summary>
Expand Down Expand Up @@ -90,7 +83,6 @@ public override void OnInspectorGUI()
}

private int numColumns = 4;

private Vector2 scrollAmount;

public void DrawIconGrid(FontIconSelector fontIconSelector, float tileSize)
Expand All @@ -101,45 +93,42 @@ public void DrawIconGrid(FontIconSelector fontIconSelector, float tileSize)

scrollAmount = EditorGUILayout.BeginScrollView(scrollAmount, GUILayout.MaxHeight(128), GUILayout.MinHeight(64));
EditorGUILayout.BeginHorizontal();

foreach (string iconName in fontIconSet.GlyphIconsByName.Keys)
{
uint unicodeValue = fontIconSet.GlyphIconsByName[iconName];
bool selected = (iconName == fontIconSelector.CurrentIconName);

if (column >= numColumns)
{
column = 0;
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
}

if (GUILayout.Button(" ",
GUILayout.MinHeight(tileSize),
GUILayout.MaxHeight(tileSize),
GUILayout.MinWidth(tileSize),
GUILayout.MaxWidth(tileSize)))
GUILayout.Height(tileSize),
GUILayout.Width(tileSize)))
{
Undo.RecordObjects(new UnityEngine.Object[]{fontIconSelector, fontIconSelector.TextMeshProComponent}, "Changed icon");
Undo.RecordObjects(new Object[] { fontIconSelector, fontIconSelector.TextMeshProComponent }, "Changed icon");
fontIconSelector.CurrentIconName = iconName;
PrefabUtility.RecordPrefabInstancePropertyModifications(fontIconSelector);
PrefabUtility.RecordPrefabInstancePropertyModifications(fontIconSelector.TextMeshProComponent);
}

Rect textureRect = GUILayoutUtility.GetLastRect();
if (textureRect.yMin + 8 < scrollAmount.y || textureRect.yMax - 8 > scrollAmount.y + 128)
{
unicodeValue = 0;
}

textureRect.width = tileSize;
textureRect.height = tileSize;
FontIconSetInspector.EditorDrawTMPGlyph(textureRect, unicodeValue, fontAsset, selected);
FontIconSetInspector.EditorDrawTMPGlyph(textureRect, unicodeValue, fontAsset, iconName == fontIconSelector.CurrentIconName);

column++;
}

if (column > 0)
{
EditorGUILayout.EndHorizontal();
}

EditorGUILayout.EndHorizontal();

if (Event.current.type == EventType.Repaint)
{
float editorWindowWidth = GUILayoutUtility.GetLastRect().width;
Expand Down

Large diffs are not rendered by default.

37 changes: 31 additions & 6 deletions org.mixedrealitytoolkit.uxcore/FontIcons/FontIconSelector.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause

using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

Expand All @@ -11,7 +11,7 @@ namespace MixedReality.Toolkit.UX
/// Allows the user to select a specific icon for display via a Unity text component.
/// </summary>
[AddComponentMenu("MRTK/UX/Font Icon Selector")]
public class FontIconSelector : MonoBehaviour
public class FontIconSelector : MonoBehaviour, ISerializationCallbackReceiver
{
[Tooltip("The FontIconSet that contains the icons available for use.")]
[SerializeField]
Expand Down Expand Up @@ -52,6 +52,11 @@ public string CurrentIconName
/// </summary>
public TMP_Text TextMeshProComponent => textMeshProComponent;

// A temporary variable used to migrate instances of FontIconSelector to use new FontIconSetDefinition names.
// TODO: Remove this after some time to ensure users have successfully migrated.
[SerializeField, HideInInspector]
private bool migratedSuccessfully = false;

/// <summary>
/// A Unity event function that is called when an enabled script instance is being loaded.
/// </summary>
Expand Down Expand Up @@ -83,12 +88,32 @@ private void OnValidate()

private void SetIcon(string newIconName)
{
if (fontIcons != null && textMeshProComponent != null)
if (fontIcons != null && textMeshProComponent != null && fontIcons.TryGetGlyphIcon(newIconName, out uint unicodeValue))
{
currentIconName = newIconName;
textMeshProComponent.text = FontIconSet.ConvertUnicodeToHexString(unicodeValue);
}
}

void ISerializationCallbackReceiver.OnBeforeSerialize() { }

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (!migratedSuccessfully && fontIcons != null && fontIcons.FontIconSetDefinition != null && textMeshProComponent != null)
{
if (fontIcons.TryGetGlyphIcon(newIconName, out uint unicodeValue))
uint unicodeValue = FontIconSet.ConvertHexStringToUnicode(textMeshProComponent.text);
foreach (KeyValuePair<string, uint> kv in fontIcons.GlyphIconsByName)
{
currentIconName = newIconName;
textMeshProComponent.text = FontIconSet.ConvertUnicodeToHexString(unicodeValue);
if (kv.Value == unicodeValue)
{
if (currentIconName != kv.Key)
{
Debug.Log($"[{nameof(FontIconSelector)}] Successfully migrated icon: \"{currentIconName}\" to \"{kv.Key}\"", this);
currentIconName = kv.Key;
migratedSuccessfully = true;
}
break;
}
}
}
}
Expand Down
45 changes: 14 additions & 31 deletions org.mixedrealitytoolkit.uxcore/FontIcons/FontIconSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,33 @@ public class FontIconSet : ScriptableObject
/// </summary>
public SerializableDictionary<string, uint> GlyphIconsByName => glyphIconsByName;

[Tooltip("Any TextMeshPro Font Asset that contains the desired icons as glyphs that map to Unicode character values.")]
[SerializeField]
[Tooltip("Any TextMeshPro Font Asset that contains the desired icons as glyphs that map to Unicode character values.")]
private TMP_FontAsset iconFontAsset = null;

/// <summary>
/// Any text font asset that contains the desired icons as glyphs that map to Unicode character values.
/// </summary>
public TMP_FontAsset IconFontAsset => iconFontAsset;

[Tooltip("Optional material to use for rendering glyphs in editor.")]
[SerializeField]
[Tooltip("Optional material to use for rendering glyphs in editor.")]
private Material optionalEditorMaterial;

/// <summary>
/// Optional material to use for rendering glyphs in editor.
/// </summary>
public Material OptionalEditorMaterial => optionalEditorMaterial;

[SerializeField]
[Tooltip("Optional definition to provide consistent icon names.")]
private FontIconSetDefinition fontIconSetDefinition;

/// <summary>
/// Optional definition to provide consistent icon names.
/// </summary>
public FontIconSetDefinition FontIconSetDefinition => fontIconSetDefinition;

/// <summary>
/// Try to get a glyph icon's unicode value by name.
/// </summary>
Expand All @@ -56,7 +65,6 @@ public class FontIconSet : ScriptableObject
/// <returns><see langword="true"/> if icon name found, otherwise <see langword="false"/>.</returns>
public bool TryGetGlyphIcon(string iconName, out uint unicodeValue)
{
unicodeValue = 0;
return glyphIconsByName.TryGetValue(iconName, out unicodeValue);
}

Expand All @@ -68,15 +76,7 @@ public bool TryGetGlyphIcon(string iconName, out uint unicodeValue)
/// <returns>Whether it was able to add this icon.</returns>
public bool AddIcon(string name, uint unicodeValue)
{
if (glyphIconsByName.ContainsValue(unicodeValue))
{
return false;
}
else
{
glyphIconsByName[name] = unicodeValue;
return true;
}
return !glyphIconsByName.ContainsValue(unicodeValue) && glyphIconsByName.TryAdd(name, unicodeValue);
}

/// <summary>
Expand All @@ -86,15 +86,7 @@ public bool AddIcon(string name, uint unicodeValue)
/// <returns>Whether it was able to find the name and remove it.</returns>
public bool RemoveIcon(string iconName)
{
if (glyphIconsByName.ContainsKey(iconName))
{
glyphIconsByName.Remove(iconName);
return true;
}
else
{
return false;
}
return glyphIconsByName.Remove(iconName);
}

/// <summary>
Expand All @@ -109,16 +101,7 @@ public bool RemoveIcon(string iconName)
/// <returns><see langword="true"/> if it was able to find and update the name.</returns>
public bool UpdateIconName(string oldName, string newName)
{
if (glyphIconsByName.ContainsKey(oldName) && !glyphIconsByName.ContainsKey(newName))
{
glyphIconsByName[newName] = glyphIconsByName[oldName];
glyphIconsByName.Remove(oldName);
return true;
}
else
{
return false;
}
return glyphIconsByName.TryGetValue(oldName, out uint unicodeValue) && glyphIconsByName.TryAdd(newName, unicodeValue) && glyphIconsByName.Remove(oldName);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause

using System.Collections.Generic;
using UnityEngine;

namespace MixedReality.Toolkit.UX
{
[CreateAssetMenu(fileName = "FontIconSetDefinition", menuName = "MRTK/UX/Font Icon Set Definition")]
public class FontIconSetDefinition : ScriptableObject
{
[SerializeField]
private string[] iconNames;

/// <summary>
/// The list of icon names defined by this asset.
/// </summary>
public IReadOnlyList<string> IconNames => iconNames;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading