Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions Barotrauma/BarotraumaClient/ClientSource/GUI/GUIPrefab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,12 @@ protected int ParseSize(XElement element, string attributeName)
}
}

public abstract class GUISelector<T> where T : GUIPrefab
public abstract partial class GUISelector<T> where T : GUIPrefab
{
public readonly PrefabSelector<T> Prefabs = new PrefabSelector<T>();
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;
Expand Down Expand Up @@ -227,10 +221,8 @@ private bool IsValidOverride(XElement element)
}
}

public class GUIFont : GUISelector<GUIFontPrefab>
public partial class GUIFont : GUISelector<GUIFontPrefab>
{
public GUIFont(string identifier) : base(identifier) { }

public bool HasValue => Value is not null;

public ScalableFont? Value => Prefabs.ActivePrefab?.Font;
Expand Down
30 changes: 1 addition & 29 deletions Barotrauma/BarotraumaClient/ClientSource/GUI/GUIStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Identifier, GUIFont> Fonts;
public readonly static ImmutableDictionary<Identifier, GUISprite> Sprites;
public readonly static ImmutableDictionary<Identifier, GUISpriteSheet> SpriteSheets;
public readonly static ImmutableDictionary<Identifier, GUIColor> Colors;
static GUIStyle()
{
var guiClassProperties = typeof(GUIStyle).GetFields(BindingFlags.Public | BindingFlags.Static);

ImmutableDictionary<Identifier, T> getPropertiesOfType<T>() where T : class
{
return guiClassProperties
.Where(p => p.FieldType == typeof(T))
.Select(p => (p.Name.ToIdentifier(), p.GetValue(null) as T))
.ToImmutableDictionary();
}

Fonts = getPropertiesOfType<GUIFont>();
Sprites = getPropertiesOfType<GUISprite>();
SpriteSheets = getPropertiesOfType<GUISpriteSheet>();
Colors = getPropertiesOfType<GUIColor>();
}

public readonly static PrefabCollection<GUIComponentStyle> ComponentStyles = new PrefabCollection<GUIComponentStyle>();

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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -118,7 +41,6 @@ public bool Scrollable
scrollable = value;
IsActive = value || parseSpecialTextTagOnStart;
TextBlock.Wrap = !scrollable;
TextBlock.TextAlignment = scrollable ? Alignment.CenterLeft : Alignment.Center;
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Loading