diff --git a/Barotrauma/BarotraumaClient/ClientSource/GUI/GUIPrefab.cs b/Barotrauma/BarotraumaClient/ClientSource/GUI/GUIPrefab.cs index 2486e78332..6124983b17 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GUI/GUIPrefab.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GUI/GUIPrefab.cs @@ -41,18 +41,12 @@ protected int ParseSize(XElement element, string attributeName) } } - public abstract class GUISelector where T : GUIPrefab + public abstract partial class GUISelector where T : GUIPrefab { public readonly PrefabSelector Prefabs = new PrefabSelector(); - public readonly Identifier Identifier; - - public GUISelector(string identifier) - { - Identifier = identifier.ToIdentifier(); - } } - public class GUIFontPrefab : GUIPrefab + public partial class GUIFontPrefab : GUIPrefab { private readonly ContentXElement element; private ScalableFont? font; @@ -227,10 +221,8 @@ private bool IsValidOverride(XElement element) } } - public class GUIFont : GUISelector + public partial class GUIFont : GUISelector { - public GUIFont(string identifier) : base(identifier) { } - public bool HasValue => Value is not null; public ScalableFont? Value => Prefabs.ActivePrefab?.Font; diff --git a/Barotrauma/BarotraumaClient/ClientSource/GUI/GUIStyle.cs b/Barotrauma/BarotraumaClient/ClientSource/GUI/GUIStyle.cs index 0195485579..ef367ae3a1 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GUI/GUIStyle.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GUI/GUIStyle.cs @@ -3,45 +3,17 @@ using System; using System.Collections.Immutable; using System.Linq; -using System.Reflection; namespace Barotrauma { - public static class GUIStyle + public static partial class GUIStyle { - public readonly static ImmutableDictionary Fonts; public readonly static ImmutableDictionary Sprites; public readonly static ImmutableDictionary SpriteSheets; public readonly static ImmutableDictionary Colors; - static GUIStyle() - { - var guiClassProperties = typeof(GUIStyle).GetFields(BindingFlags.Public | BindingFlags.Static); - - ImmutableDictionary getPropertiesOfType() where T : class - { - return guiClassProperties - .Where(p => p.FieldType == typeof(T)) - .Select(p => (p.Name.ToIdentifier(), p.GetValue(null) as T)) - .ToImmutableDictionary(); - } - - Fonts = getPropertiesOfType(); - Sprites = getPropertiesOfType(); - SpriteSheets = getPropertiesOfType(); - Colors = getPropertiesOfType(); - } public readonly static PrefabCollection ComponentStyles = new PrefabCollection(); - public readonly static GUIFont Font = new GUIFont("Font"); - public readonly static GUIFont UnscaledSmallFont = new GUIFont("UnscaledSmallFont"); - public readonly static GUIFont SmallFont = new GUIFont("SmallFont"); - public readonly static GUIFont LargeFont = new GUIFont("LargeFont"); - public readonly static GUIFont SubHeadingFont = new GUIFont("SubHeadingFont"); - public readonly static GUIFont DigitalFont = new GUIFont("DigitalFont"); - public readonly static GUIFont HotkeyFont = new GUIFont("HotkeyFont"); - public readonly static GUIFont MonospacedFont = new GUIFont("MonospacedFont"); - public readonly static GUICursor CursorSprite = new GUICursor("Cursor"); public readonly static GUISprite SubmarineLocationIcon = new GUISprite("SubmarineLocationIcon"); diff --git a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/ItemLabel.cs b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/ItemLabel.cs index f73d345545..86b9119fe4 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Items/Components/ItemLabel.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Items/Components/ItemLabel.cs @@ -13,8 +13,6 @@ partial class ItemLabel : ItemComponent, IDrawableComponent, IHasExtraTextPicker { private GUITextBlock textBlock; - private Color textColor; - private float scrollAmount; private string scrollingText; private float scrollPadding; @@ -27,87 +25,12 @@ partial class ItemLabel : ItemComponent, IDrawableComponent, IHasExtraTextPicker private Rectangle prevRect; private StringBuilder sb; - private Vector4 padding; - - [Serialize("0,0,0,0", IsPropertySaveable.Yes, description: "The amount of padding around the text in pixels (left,top,right,bottom).")] - public Vector4 Padding - { - get { return padding; } - set - { - padding = value; - TextBlock.Padding = value * item.Scale; - } - } - - private string text; - [Serialize("", IsPropertySaveable.Yes, translationTextTag: "Label.", description: "The text displayed in the label.", alwaysUseInstanceValues: true), Editable(MaxLength = 100)] - public string Text - { - get { return text; } - set - { - if (value == text || item.Rect.Width < 5) { return; } - - if (TextBlock.Rect.Width != item.Rect.Width || textBlock.Rect.Height != item.Rect.Height) - { - textBlock = null; - } - - text = value; - SetDisplayText(value); - UpdateScrollingText(); - } - } - - private bool ignoreLocalization; - - [Editable, Serialize(false, IsPropertySaveable.Yes, "Whether or not to skip localization and always display the raw value.")] - public bool IgnoreLocalization - { - get => ignoreLocalization; - set - { - ignoreLocalization = value; - SetDisplayText(Text); - } - } - public LocalizedString DisplayText { get; private set; } - [Editable, Serialize("0,0,0,255", IsPropertySaveable.Yes, description: "The color of the text displayed on the label (R,G,B,A).", alwaysUseInstanceValues: true)] - public Color TextColor - { - get { return textColor; } - set - { - if (textBlock != null) { textBlock.TextColor = value; } - textColor = value; - } - } - - [Editable(0.0f, 10.0f), Serialize(1.0f, IsPropertySaveable.Yes, description: "The scale of the text displayed on the label.", alwaysUseInstanceValues: true)] - public float TextScale - { - get { return textBlock == null ? 1.0f : textBlock.TextScale / BaseToRealTextScaleFactor; } - set - { - if (textBlock != null) - { - float prevScale = TextBlock.TextScale; - textBlock.TextScale = MathHelper.Clamp(value * BaseToRealTextScaleFactor, 0.1f, 10.0f); - if (!MathUtils.NearlyEqual(prevScale, TextBlock.TextScale)) - { - SetScrollingText(); - } - } - } - } - private bool scrollable; [Serialize(false, IsPropertySaveable.Yes, description: "Should the text scroll horizontally across the item if it's too long to be displayed all at once.")] public bool Scrollable @@ -118,7 +41,6 @@ public bool Scrollable scrollable = value; IsActive = value || parseSpecialTextTagOnStart; TextBlock.Wrap = !scrollable; - TextBlock.TextAlignment = scrollable ? Alignment.CenterLeft : Alignment.Center; } } @@ -164,12 +86,14 @@ private void SetScrollingText() needsScrolling = true; float spaceWidth = textBlock.Font.MeasureChar(' ').X * TextBlock.TextScale; scrollingText = new string(' ', (int)Math.Ceiling(textAreaWidth / spaceWidth)) + DisplayText.Value; + TextBlock.TextAlignment = Alignment.CenterLeft; } else { //whole text can fit in the textblock, no need to scroll needsScrolling = false; TextBlock.Text = scrollingText = DisplayText.Value; + TextBlock.TextAlignment = alignment; scrollPadding = 0; scrollAmount = 0.0f; scrollIndex = 0; @@ -215,7 +139,7 @@ private void SetDisplayText(string value) private void RecreateTextBlock() { textBlock = new GUITextBlock(new RectTransform(item.Rect.Size), "", - textColor: textColor, font: GUIStyle.UnscaledSmallFont, textAlignment: scrollable ? Alignment.CenterLeft : Alignment.Center, wrap: !scrollable, style: null) + textColor: textColor, font: font, textAlignment: needsScrolling ? Alignment.CenterLeft : alignment, wrap: !scrollable, style: null) { TextDepth = item.SpriteDepth - 0.00001f, RoundToNearestPixel = false, diff --git a/Barotrauma/BarotraumaClient/ClientSource/Serialization/SerializableEntityEditor.cs b/Barotrauma/BarotraumaClient/ClientSource/Serialization/SerializableEntityEditor.cs index 668b1bde84..4995e788a3 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Serialization/SerializableEntityEditor.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Serialization/SerializableEntityEditor.cs @@ -30,7 +30,7 @@ sealed class SerializableEntityEditor : GUIComponent public bool Readonly { get => isReadonly; - set + set { foreach (var component in Fields.SelectMany(f => f.Value)) { @@ -317,8 +317,8 @@ public void UpdateValue(SerializableProperty property, object newValue, bool fla } public SerializableEntityEditor(RectTransform parent, ISerializableEntity entity, bool inGame, bool showName, string style = "", int elementHeight = 24, GUIFont titleFont = null) - : this(parent, entity, inGame ? - SerializableProperty.GetProperties(entity).Union(SerializableProperty.GetProperties(entity).Where(p => p.GetAttribute()?.IsEditable(entity) ?? false)) + : this(parent, entity, inGame ? + SerializableProperty.GetProperties(entity).Union(SerializableProperty.GetProperties(entity).Where(p => p.GetAttribute()?.IsEditable(entity) ?? false)) : SerializableProperty.GetProperties(entity).Where(p => p.GetAttribute()?.IsEditable(entity) ?? true), showName, style, elementHeight, titleFont) { } @@ -344,10 +344,10 @@ public SerializableEntityEditor(RectTransform parent, ISerializableEntity entity }; } - List
headers = new List
() + List
headers = new List
() { //"no header" comes first = properties under no header are listed first - null + null }; //check which header each property is under Dictionary propertyHeaders = new Dictionary(); @@ -360,11 +360,11 @@ public SerializableEntityEditor(RectTransform parent, ISerializableEntity entity prevHeader = header; //Attribute.Equals is based on the equality of the fields, //so in practice we treat identical headers split into different files/classes as the same header - if (!headers.Contains(header)) - { + if (!headers.Contains(header)) + { //collect headers into a list in the order they're encountered in //(to keep them in the same order as they're defined in the code, as the dictionary is not in any particular order) - headers.Add(header); + headers.Add(header); } } propertyHeaders[property] = prevHeader; @@ -430,9 +430,9 @@ public GUIComponent CreateNewField(SerializableProperty property, ISerializableE displayName = TextManager.Get(fallbackTag, $"sp.{fallbackTag}.name".ToIdentifier()); } } - + if (displayName.IsNullOrEmpty()) - { + { displayName = property.Name.FormatCamelCaseWithSpaces(); #if DEBUG InGameEditable editable = property.GetAttribute(); @@ -523,58 +523,62 @@ public GUIComponent CreateNewField(SerializableProperty property, ISerializableE { propertyField = CreateStringField(entity, property, value.ToString(), displayName, toolTip); } + else if (value is GUIFont font) + { + propertyField = CreateFontField(entity, property, font, displayName, toolTip); + } return propertyField; } public GUIComponent CreateBoolField(ISerializableEntity entity, SerializableProperty property, bool value, LocalizedString displayName, LocalizedString toolTip) - { - var editableAttribute = property.GetAttribute(); - if (editableAttribute.ReadOnly) { - var frame = new GUIFrame(new RectTransform(new Point(Rect.Width, Math.Max(elementHeight, 26)), layoutGroup.RectTransform, isFixedSize: true), color: Color.Transparent); - var label = new GUITextBlock(new RectTransform(new Vector2(1.0f - inputFieldWidth, 1), frame.RectTransform), displayName, font: GUIStyle.SmallFont) - { - ToolTip = toolTip - }; - var valueField = new GUITextBlock(new RectTransform(new Vector2(inputFieldWidth, 1), frame.RectTransform, Anchor.TopRight), value.ToString()) + var editableAttribute = property.GetAttribute(); + if (editableAttribute.ReadOnly) { - ToolTip = toolTip, - Font = GUIStyle.SmallFont - }; - return valueField; - } - else - { - GUITickBox propertyTickBox = new GUITickBox(new RectTransform(new Point(Rect.Width, elementHeight), layoutGroup.RectTransform, isFixedSize: true), displayName) + var frame = new GUIFrame(new RectTransform(new Point(Rect.Width, Math.Max(elementHeight, 26)), layoutGroup.RectTransform, isFixedSize: true), color: Color.Transparent); + var label = new GUITextBlock(new RectTransform(new Vector2(1.0f - inputFieldWidth, 1), frame.RectTransform), displayName, font: GUIStyle.SmallFont) + { + ToolTip = toolTip + }; + var valueField = new GUITextBlock(new RectTransform(new Vector2(inputFieldWidth, 1), frame.RectTransform, Anchor.TopRight), value.ToString()) + { + ToolTip = toolTip, + Font = GUIStyle.SmallFont + }; + return valueField; + } + else { - Font = GUIStyle.SmallFont, - Enabled = !Readonly, - Selected = value, - ToolTip = toolTip, - OnSelected = (tickBox) => + GUITickBox propertyTickBox = new GUITickBox(new RectTransform(new Point(Rect.Width, elementHeight), layoutGroup.RectTransform, isFixedSize: true), displayName) { - if (SetPropertyValue(property, entity, tickBox.Selected)) - { - TrySendNetworkUpdate(entity, property); - } - // Ensure that the values stay in sync (could be that we force the value in the property accessor). - bool propertyValue = (bool)property.GetValue(entity); - if (tickBox.Selected != propertyValue) + Font = GUIStyle.SmallFont, + Enabled = !Readonly, + Selected = value, + ToolTip = toolTip, + OnSelected = (tickBox) => { - tickBox.Selected = propertyValue; - tickBox.Flash(Color.Red); + if (SetPropertyValue(property, entity, tickBox.Selected)) + { + TrySendNetworkUpdate(entity, property); + } + // Ensure that the values stay in sync (could be that we force the value in the property accessor). + bool propertyValue = (bool)property.GetValue(entity); + if (tickBox.Selected != propertyValue) + { + tickBox.Selected = propertyValue; + tickBox.Flash(Color.Red); + } + return true; } - return true; - } - }; - refresh += () => - { - propertyTickBox.Selected = (bool)property.GetValue(entity); - }; - if (!Fields.ContainsKey(property.Name)) { Fields.Add(property.Name.ToIdentifier(), new GUIComponent[] { propertyTickBox }); } - return propertyTickBox; + }; + refresh += () => + { + propertyTickBox.Selected = (bool)property.GetValue(entity); + }; + if (!Fields.ContainsKey(property.Name)) { Fields.Add(property.Name.ToIdentifier(), new GUIComponent[] { propertyTickBox }); } + return propertyTickBox; + } } - } public GUIComponent CreateIntField(ISerializableEntity entity, SerializableProperty property, int value, LocalizedString displayName, LocalizedString toolTip) { @@ -1511,6 +1515,38 @@ public void CreateTextPicker(string textTag, ISerializableEntity entity, Seriali } } + public GUIComponent CreateFontField(ISerializableEntity entity, SerializableProperty property, GUIFont value, LocalizedString displayName, LocalizedString toolTip) + { + GUIFrame frame = new(new RectTransform(new Point(Rect.Width, elementHeight), layoutGroup.RectTransform, isFixedSize: true), color: Color.Transparent); + GUITextBlock label = new(new RectTransform(new Vector2(1f - inputFieldWidth, 1f), frame.RectTransform), displayName, font: GUIStyle.SmallFont) + { + ToolTip = toolTip + }; + GUIDropDown dropDown = new GUIDropDown(new RectTransform(new Vector2(inputFieldWidth, 1), frame.RectTransform, Anchor.TopRight), elementCount: GUIStyle.Fonts.Count) + { + ToolTip = toolTip + }; + foreach ((Identifier fontIdentifier, GUIFont font) in GUIStyle.Fonts) + { + dropDown.AddItem(fontIdentifier.Value, font); + } + dropDown.SelectItem(value); + dropDown.OnSelected += (selected, val) => + { + if (SetPropertyValue(property, entity, val)) + { + TrySendNetworkUpdate(entity, property); + } + return true; + }; + refresh += () => + { + if (!dropDown.Dropped) { dropDown.SelectItem(property.GetValue(entity)); } + }; + if (!Fields.ContainsKey(property.Name)) { Fields.Add(property.Name.ToIdentifier(), new GUIComponent[] { dropDown }); } + return frame; + } + private static void TrySendNetworkUpdate(ISerializableEntity entity, SerializableProperty property) { if (IsEntityRemoved(entity)) { return; } diff --git a/Barotrauma/BarotraumaServer/ServerSource/Items/Components/ItemLabel.cs b/Barotrauma/BarotraumaServer/ServerSource/Items/Components/ItemLabel.cs index ea29665f75..811e8e429a 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Items/Components/ItemLabel.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Items/Components/ItemLabel.cs @@ -11,41 +11,6 @@ partial class ItemLabel : ItemComponent, IDrawableComponent private string lastSentText; private float sendStateTimer; - [Serialize("", IsPropertySaveable.Yes, description: "The text to display on the label.", alwaysUseInstanceValues: true), Editable(MaxLength = 100)] - public string Text - { - get; - set; - } - - [Editable, Serialize(false, IsPropertySaveable.Yes)] - public bool IgnoreLocalization - { - get; - set; - } - - [Editable, Serialize("0,0,0,255", IsPropertySaveable.Yes, description: "The color of the text displayed on the label.", alwaysUseInstanceValues: true)] - public Color TextColor - { - get; - set; - } - - [Editable, Serialize(1.0f, IsPropertySaveable.Yes, description: "The scale of the text displayed on the label.", alwaysUseInstanceValues: true)] - public float TextScale - { - get; - set; - } - - [Serialize("0,0,0,0", IsPropertySaveable.Yes, description: "The amount of padding around the text in pixels (left,top,right,bottom).")] - public Vector4 Padding - { - get; - set; - } - public override void Move(Vector2 amount, bool ignoreContacts = false) { //do nothing diff --git a/Barotrauma/BarotraumaShared/SharedSource/GUI/GUIPrefab.cs b/Barotrauma/BarotraumaShared/SharedSource/GUI/GUIPrefab.cs new file mode 100644 index 0000000000..6817c88ad4 --- /dev/null +++ b/Barotrauma/BarotraumaShared/SharedSource/GUI/GUIPrefab.cs @@ -0,0 +1,19 @@ +namespace Barotrauma +{ + // Implemented in shared so clients can share fonts with eachother. + // Required for font selection by `ItemLabel`s. + + public abstract partial class GUISelector + { + public readonly Identifier Identifier; + + public GUISelector(string identifier) => Identifier = identifier.ToIdentifier(); + } + + public partial class GUIFontPrefab { } + + public partial class GUIFont : GUISelector + { + public GUIFont(string identifier) : base(identifier) { } + } +} diff --git a/Barotrauma/BarotraumaShared/SharedSource/GUI/GUIStyle.cs b/Barotrauma/BarotraumaShared/SharedSource/GUI/GUIStyle.cs new file mode 100644 index 0000000000..5118425acf --- /dev/null +++ b/Barotrauma/BarotraumaShared/SharedSource/GUI/GUIStyle.cs @@ -0,0 +1,44 @@ +using Barotrauma.Extensions; +using System.Collections.Immutable; +using System.Linq; +using System.Reflection; + +namespace Barotrauma +{ + // Implemented in shared so clients can share fonts with eachother. + // Required for font selection by `ItemLabel`s. + + public static partial class GUIStyle + { + public static readonly ImmutableDictionary Fonts; + + static GUIStyle() + { + FieldInfo[] guiClassProperties = typeof(GUIStyle).GetFields(BindingFlags.Public | BindingFlags.Static); + + ImmutableDictionary getPropertiesOfType() where T : class + { + return guiClassProperties + .Where(p => p.FieldType == typeof(T)) + .Select(p => (p.Name.ToIdentifier(), p.GetValue(null) as T)) + .ToImmutableDictionary(); + } + + Fonts = getPropertiesOfType(); +#if CLIENT + Sprites = getPropertiesOfType(); + SpriteSheets = getPropertiesOfType(); + Colors = getPropertiesOfType(); +#endif + } + + public static readonly GUIFont Font = new GUIFont("Font"); + public static readonly GUIFont UnscaledSmallFont = new GUIFont("UnscaledSmallFont"); + public static readonly GUIFont SmallFont = new GUIFont("SmallFont"); + public static readonly GUIFont LargeFont = new GUIFont("LargeFont"); + public static readonly GUIFont SubHeadingFont = new GUIFont("SubHeadingFont"); + public static readonly GUIFont DigitalFont = new GUIFont("DigitalFont"); + public static readonly GUIFont HotkeyFont = new GUIFont("HotkeyFont"); + public static readonly GUIFont MonospacedFont = new GUIFont("MonospacedFont"); + } +} \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemLabel.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemLabel.cs index b65b42486d..2ed90453e6 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemLabel.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/ItemLabel.cs @@ -11,6 +11,129 @@ public Vector2 DrawSize get { return Vector2.Zero; } } + private bool ignoreLocalization; + [Editable, Serialize(false, IsPropertySaveable.Yes, "Whether or not to skip localization and always display the raw value.")] + public bool IgnoreLocalization + { + get => ignoreLocalization; + set + { + ignoreLocalization = value; +#if CLIENT + SetDisplayText(Text); +#endif + } + } + + private Vector4 padding; + [Editable(DecimalCount = 0, VectorComponentLabels = new string[] { "inputtype.left", "inputtype.up", "inputtype.right", "inputtype.down" }), Serialize("0,0,0,0", IsPropertySaveable.Yes, description: "The amount of padding around the text in pixels.")] + public Vector4 Padding + { + get => padding; + set + { + padding = value; +#if CLIENT + TextBlock.Padding = value * item.Scale; +#endif + } + } + + private Alignment alignment; + [Editable, Serialize(Alignment.Center, IsPropertySaveable.Yes, description: "The alignment of the label's text.")] + public Alignment TextAlignment + { + get => alignment; + set + { + alignment = value; +#if CLIENT + textBlock.TextAlignment = value; +#endif + } + } + + private string text; + [Serialize("", IsPropertySaveable.Yes, translationTextTag: "Label.", description: "The text displayed in the label.", alwaysUseInstanceValues: true), Editable(MaxLength = 100)] + public string Text + { + get => text; + set + { + if (text == value || item.Rect.Width < 5) { return; } + text = value; + +#if CLIENT + if (TextBlock.Rect.Width != item.Rect.Width || textBlock.Rect.Height != item.Rect.Height) + { + textBlock = null; + } + + SetDisplayText(value); + UpdateScrollingText(); +#endif + } + } + + private GUIFont font; + [Editable, Serialize("UnscaledSmallFont", IsPropertySaveable.Yes, "The label's font.")] + public GUIFont Font + { + get => font; + set + { + font = value; +#if CLIENT + textBlock.Font = value; +#endif + } + } + + private Color textColor; + [Editable, Serialize("0,0,0,255", IsPropertySaveable.Yes, "The color of the text displayed on the label (R,G,B,A).", alwaysUseInstanceValues: true)] + public Color TextColor + { + get => textColor; + set + { + textColor = value; +#if CLIENT + if (textBlock != null) { textBlock.TextColor = value; } +#endif + } + } + + private float textScale; + [Editable(0f, 10f), Serialize(1f, IsPropertySaveable.Yes, "The scale of the text displayed on the label.", alwaysUseInstanceValues: true)] + public float TextScale + { + get + { + return +#if CLIENT + textBlock == null ? textScale : textBlock.TextScale / BaseToRealTextScaleFactor; +#else + textScale; +#endif + } + + set + { + textScale = value; +#if CLIENT + if (textBlock != null) + { + float prevScale = TextBlock.TextScale; + textBlock.TextScale = MathHelper.Clamp(value * BaseToRealTextScaleFactor, 0.1f, 10f); + if (!MathUtils.NearlyEqual(prevScale, TextBlock.TextScale)) + { + SetScrollingText(); + } + } +#endif + } + } + partial void OnStateChanged(); private string prevColorSignal; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Serialization/SerializableProperty/SerializableProperty.cs b/Barotrauma/BarotraumaShared/SharedSource/Serialization/SerializableProperty/SerializableProperty.cs index 00a77cdf5d..db2e931615 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Serialization/SerializableProperty/SerializableProperty.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Serialization/SerializableProperty/SerializableProperty.cs @@ -79,7 +79,8 @@ public sealed class SerializableProperty { typeof(Rectangle), "rectangle" }, { typeof(Color), "color" }, { typeof(string[]), "stringarray" }, - { typeof(Identifier[]), "identifierarray" } + { typeof(Identifier[]), "identifierarray" }, + { typeof(GUIFont), "font" } }.ToImmutableDictionary(); private static readonly Dictionary> cachedProperties = @@ -219,6 +220,9 @@ public bool TrySetValue(object parentObject, string value) case "identifierarray": PropertyInfo.SetValue(parentObject, ParseIdentifierArray(value)); break; + case "font": + PropertyInfo.SetValue(parentObject, GUIStyle.Fonts.TryGetValue(new(value), out GUIFont font) ? font : null); + break; } } catch (Exception e) @@ -314,6 +318,11 @@ public bool TrySetValue(object parentObject, object value) case "identifierarray": PropertyInfo.SetValue(parentObject, ParseIdentifierArray((string)value)); return true; +#if CLIENT + case "font": + PropertyInfo.SetValue(parentObject, GUIStyle.Fonts.TryGetValue(new((string)value), out GUIFont font) ? font : null); + return true; +#endif default: DebugConsole.ThrowError($"Failed to set the value of the property \"{Name}\" of \"{parentObject}\" to {value}"); DebugConsole.ThrowError($"(Cannot convert a string to a {PropertyType})"); @@ -943,6 +952,9 @@ public static void SerializeProperties(ISerializableEntity obj, XElement element Identifier[] identifierArray = (Identifier[])value; stringValue = identifierArray != null ? string.Join(';', identifierArray) : ""; break; + case "font": + stringValue = ((GUIFont)value).Identifier.Value; + break; default: stringValue = value.ToString(); break;