Files
spacerace/Assets/Src/UI/InterfaceIcon.cs
T
2023-08-22 00:12:06 +10:00

64 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class InterfaceIcon : VisualElement {
public enum Icon {
// Outputs
Acceleration,
Deceleration,
Strafing,
Shield,
Crosshair,
Logo,
}
public new class UxmlFactory : UxmlFactory<InterfaceIcon, UxmlTraits> {}
public new class UxmlTraits : VisualElement.UxmlTraits {
readonly UxmlEnumAttributeDescription<Icon> _iconAttribute = new() { name = "icon" };
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
base.Init(ve, bag, cc);
var InterfaceIcon = (InterfaceIcon)ve;
InterfaceIcon.icon = _iconAttribute.GetValueFromBag(bag, cc);
}
}
static readonly Dictionary<Icon, char> _iconGlyphMap = new() {
{ Icon.Acceleration, '\ue901' },
{ Icon.Deceleration, '\ue902' },
{ Icon.Strafing, '\ue903' },
{ Icon.Shield, '\ue904' },
{ Icon.Logo, '\ue900' },
};
public Icon icon {
get => _icon;
set {
_icon = value;
_iconLabel.text = _iconGlyphMap[value].ToString();
}
}
Icon _icon;
Label _iconLabel;
public InterfaceIcon() : this(Icon.Strafing) {}
public InterfaceIcon(Icon icon) {
_iconLabel = new Label {
style = {
unityFont = Resources.Load<Font>("SpaceRace-Icons"),
unityFontDefinition = StyleKeyword.None,
},
};
Add(_iconLabel);
this.icon = icon;
}
}