using Exoa.Json;
using Exoa.Json.Linq;
using System.Collections.Generic;
using System.Linq;
namespace UnityEngine.Style.Icon
{
///
/// Class FontAwesomeInfo.
/// Implements the
///
///
[CreateAssetMenu(menuName = "Plugins/FontAwesome", fileName = "FontAwesomeInfo", order = 0)]
public sealed class FontAwesomeInfo : ScriptableObject
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void FactoryRegistry() => Initialize();
///
/// Gets the instance.
///
/// The instance.
public static FontAwesomeInfo Instance
{
get
{
if (_instance == null)
{
FontAwesomeInfo[] resources = Resources.LoadAll("FontAwesomeInfo");
if (resources != null && resources.Length > 0) _instance = resources[0];
}
return _instance;
}
}
private static FontAwesomeInfo _instance;
///
/// Factories the registry.
///
public static void Initialize()
{
if (Instance == null) return;
if (Application.isEditor)
{
Instance.fontCache.Clear();
foreach (TextAsset textAsset in Instance.iconJson)
{
if (textAsset == null || textAsset.text.IsNullOrEmpty()) continue;
Dictionary output = JsonConvert.DeserializeObject>(textAsset.text);
if (output == null)
{
Debug.LogError("Could not read the icon file");
continue;
}
foreach (KeyValuePair tmp in output)
{
if (tmp.Key.IsNullOrEmpty() || tmp.Value == null) continue;
JObject detail = (JObject)tmp.Value;
JArray styles = (JArray)detail["styles"];
List stylesList = styles.Values().ToList();
string unicode = (string)detail["unicode"];
string id = "fa-" + tmp.Key;
if (UnityEngine.Icon.Contains(id)) continue;
if (styles == null) continue;
FontAwesome font = Instance?.iconFont?.Find(item => stylesList.Contains(item.ID));
if (font == null) continue;
Instance.fontCache.Add(new FontCache(id, unicode, font.font));
UnityEngine.Icon.Add(id, unicode, font.font);
}
}
}
else
{
foreach (FontCache cache in Instance.fontCache)
{
UnityEngine.Icon.Add(cache.ID, cache.unicode, cache.font);
}
}
}
///
/// Gets the icon json.
///
/// The icon json.
public List iconJson { get => this._iconJson; }
[SerializeField]
private List _iconJson = new List();
//private List _iconJson = new List( );
///
/// Gets or sets the localize font.
///
/// The localize font.
public List iconFont { get => this._iconFont; }
[SerializeField]
private List _iconFont = new List();
//private List _iconFont = new List( );
///
/// Class FontAwesome.
///
[System.Serializable]
public sealed class FontAwesome : SerializableBehaviour
{
///
/// The identifier
///
[SerializeField]
public string ID;
///
/// The font
///
[SerializeField]
public Font font;
}
///
/// The font cache
///
[SerializeField]
private List fontCache = new List();
//private List fontCache = new List( );
///
/// Class FontCache. This class cannot be inherited.
///
[System.Serializable]
public sealed class FontCache
{
///
/// The identifier
///
[SerializeField]
public string ID;
///
/// The unicode
///
[SerializeField]
public string unicode;
///
/// The font
///
public Font font;
///
/// Initializes a new instance of the class.
///
/// The identifier.
/// The unicode.
/// The font.
public FontCache(string ID, string unicode, Font font)
{
this.ID = ID;
this.unicode = unicode;
this.font = font;
}
}
}
}