using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; public class InterfaceIcon : VisualElement { public enum Icon { // Outputs Acceleration, Deceleration, Strafe, Shield, Crosshair, Logo, } public new class UxmlFactory : UxmlFactory {} public new class UxmlTraits : VisualElement.UxmlTraits { readonly UxmlEnumAttributeDescription _iconAttribute = new() { name = "icon" }; readonly UxmlStringAttributeDescription _detailAttribute = new UxmlStringAttributeDescription() { name = "detail" }; 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); InterfaceIcon.detail = _detailAttribute.GetValueFromBag(bag, cc); } } static readonly Dictionary _iconGlyphMap = new() { { Icon.Acceleration, '\u25bc' }, { Icon.Deceleration, '\u25b2' }, { Icon.Strafe, '\u2b94' }, { Icon.Shield, '\u26c9' }, { Icon.Logo, '\u26d0' }, }; public Icon icon { get => _icon; set { _icon = value; _iconLabel.text = _iconGlyphMap[value].ToString(); } } public string detail { get => _detail; set { _detail = value; _detailLabel.text = value; } } Icon _icon; string _detail; Label _iconLabel; Label _detailLabel; public InterfaceIcon() : this(Icon.Strafe) {} public InterfaceIcon(Icon icon) { _iconLabel = new Label { style = { unityFont = Resources.Load("SpaceRace-Icons"), unityFontDefinition = StyleKeyword.None, }, }; Add(_iconLabel); _detailLabel = new Label { style = { position = Position.Absolute, }, }; _detailLabel.AddToClassList("detail"); Add(_detailLabel); this.icon = icon; } }