-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathFontIconSet.cs
More file actions
146 lines (129 loc) · 6.28 KB
/
FontIconSet.cs
File metadata and controls
146 lines (129 loc) · 6.28 KB
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
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause
using System.Text;
using TMPro;
using UnityEngine;
namespace MixedReality.Toolkit.UX
{
/// <summary>
/// A Unity object for managing a set of character icons for use with MRTK UX via Unity text components.
/// </summary>
/// <remarks>
/// This is intended to be used with <see cref="FontIconSelector"/>, which can be used to set the desired static
/// icon for a Unity text component, and can also bind to a data source that can select an icon
/// by its name.
///
/// When using a style sheet, it is important to use a style that maps to the desired text font asset by its name
/// and any other styling desired when rendered, such as size and color.
/// </remarks>
[CreateAssetMenu(fileName = "MRTK_UX_FontIconSet_New", menuName = "MRTK/UX/Font Icon Set")]
public class FontIconSet : ScriptableObject
{
[SerializeField]
[Tooltip("A mapping between icon names and the unicode value of the glyph it describes.")]
private SerializableDictionary<string, uint> glyphIconsByName = new SerializableDictionary<string, uint>();
/// <summary>
/// A mapping between icon names and the unicode value of the glyph it describes.
/// </summary>
public SerializableDictionary<string, uint> GlyphIconsByName => glyphIconsByName;
[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;
[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>
/// <param name="iconName">The name of the icon to find.</param>
/// <param name="unicodeValue">The returned unicode value, or 0 if not found.</param>
/// <returns><see langword="true"/> if icon name found, otherwise <see langword="false"/>.</returns>
public bool TryGetGlyphIcon(string iconName, out uint unicodeValue)
{
return glyphIconsByName.TryGetValue(iconName, out unicodeValue);
}
/// <summary>
/// Add icon to the available set based on a glyph in the TMP_FontAsset.
/// </summary>
/// <param name="name">Name for this icon glyph,</param>
/// <param name="unicodeValue">Unicode value for the glyph.</param>
/// <returns>Whether it was able to add this icon.</returns>
public bool AddIcon(string name, uint unicodeValue)
{
return !glyphIconsByName.ContainsValue(unicodeValue) && glyphIconsByName.TryAdd(name, unicodeValue);
}
/// <summary>
/// Remove an icon from available set by its name.
/// </summary>
/// <param name="iconName">The named icon to remove.</param>
/// <returns>Whether it was able to find the name and remove it.</returns>
public bool RemoveIcon(string iconName)
{
return glyphIconsByName.Remove(iconName);
}
/// <summary>
/// Update the name of an icon.
/// </summary>
/// <remarks>
/// Note that this will return false if the new name already exists or if the
/// current name can't be found.
/// </remarks>
/// <param name="oldName">The current name of the icon.</param>
/// <param name="newName">The desired new name of the icon.</param>
/// <returns><see langword="true"/> if it was able to find and update the name.</returns>
public bool UpdateIconName(string oldName, string newName)
{
return glyphIconsByName.TryGetValue(oldName, out uint unicodeValue) && glyphIconsByName.TryAdd(newName, unicodeValue) && glyphIconsByName.Remove(oldName);
}
/// <summary>
/// Converts a unicode string to a uint code (for use with TextMeshPro).
/// </summary>
/// <param name="charString">Hex string in the form '\uFFFF'.</param>
/// <returns>The binary unicode value.</returns>
public static uint ConvertHexStringToUnicode(string charString)
{
uint unicode = 0;
if (string.IsNullOrEmpty(charString))
return 0;
for (int i = 0; i < charString.Length; i++)
{
unicode = charString[i];
// Handle surrogate pairs
if (i < charString.Length - 1 && char.IsHighSurrogate((char)unicode) && char.IsLowSurrogate(charString[i + 1]))
{
unicode = (uint)char.ConvertToUtf32(charString[i], charString[i + 1]);
i += 1;
}
}
return unicode;
}
/// <summary>
/// Converts a unicode value to a string.
/// </summary>
/// <remarks>
/// This is used to convert unicode values into a strings that can be applied to text fields.
/// </remarks>
/// <param name="unicode">Unicode value to be converted to a hexadecimal string representation.</param>
/// <returns>The string version of the unicode value in the form of '\uFFFF', where FFFF is replaced with the associated hexadecimal value.</returns>
public static string ConvertUnicodeToHexString(uint unicode)
{
byte[] bytes = System.BitConverter.GetBytes(unicode);
return Encoding.Unicode.GetString(bytes);
}
}
}