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
9 changes: 9 additions & 0 deletions Chat/Ui/ChatPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void Dispose()
ViewportResizeNotifier.Instance.OnViewportResized -= OnViewportResized;

_model.OnMessageAppended -= OnMessageAppended;
LocManager.Instance.UnsubscribeToLocaleChange(OnLocaleChanged);
_panelStyle.Dispose();
_inputStyle.Dispose();
_container.QueueFree();
Expand Down Expand Up @@ -461,6 +462,7 @@ private void CreateUi()
};
_messageBuffer.AddThemeColorOverride("default_color", Colors.White);
_messageBuffer.AddThemeFontSizeOverride("normal_font_size", ChatConfig.FontSize);
StsUiFonts.Apply(_messageBuffer);
_vboxLayout.AddChild(_messageBuffer);

_messageBuffer.MetaClicked += OnMetaClicked;
Expand Down Expand Up @@ -489,6 +491,7 @@ private void CreateUi()
_inputField.AddThemeColorOverride("font_placeholder_color", ChatConfig.PlaceholderColor);
_inputField.AddThemeColorOverride("caret_color", ChatConfig.CaretColor);
_inputField.AddThemeFontSizeOverride("font_size", ChatConfig.FontSize);
StsUiFonts.Apply(_inputField);

_inputStyle = new StyleBoxFlat { BgColor = ChatConfig.InputBgColor };
_inputField.AddThemeStyleboxOverride("normal", _inputStyle);
Expand Down Expand Up @@ -552,6 +555,7 @@ internal void Initialize()

// 订阅窗口大小变化事件
ViewportResizeNotifier.Instance.OnViewportResized += OnViewportResized;
LocManager.Instance.SubscribeToLocaleChange(OnLocaleChanged);

// 注册到 InputCapture,让其放过 ChatPanel 内部的 Alt+Click
ItemInputCapture.RegisterBlockingControl(_container);
Expand All @@ -563,6 +567,11 @@ private void OnViewportResized(Vector2 _)
RequestCompletionRefresh();
}

private void OnLocaleChanged()
{
StsUiFonts.Refresh(_container);
}

private void ShowWelcome()
{
var welcomeText = new LocString("gameplay_ui", "LEMONSPIRE.chat.welcome").GetFormattedText();
Expand Down
2 changes: 2 additions & 0 deletions Chat/Ui/Completion/ChatCompletionPopupController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Godot;
using lemonSpire2.Chat.Input.Model;
using lemonSpire2.util.Ui;

namespace lemonSpire2.Chat.Ui.Completion;

Expand Down Expand Up @@ -59,6 +60,7 @@ public ChatCompletionPopupController()
SizeFlagsHorizontal = Control.SizeFlags.ExpandFill
};
_list.AddThemeFontSizeOverride("font_size", ChatConfig.FontSize);
StsUiFonts.Apply(_list);
_scroll.AddChild(_list);
_panel.AddChild(_scroll);
}
Expand Down
11 changes: 11 additions & 0 deletions PlayerStateEx/OverlayPanel/PlayerOverlayPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using lemonSpire2.util.Ui;
using MegaCrit.Sts2.Core.Combat;
using MegaCrit.Sts2.Core.Entities.Players;
using MegaCrit.Sts2.Core.Localization;
using MegaCrit.Sts2.Core.Localization.Fonts;
using MegaCrit.Sts2.Core.Nodes;
using MegaCrit.Sts2.Core.Platform;
using MegaCrit.Sts2.Core.Rooms;
Expand Down Expand Up @@ -55,6 +57,7 @@ public override void _Ready()
{
MouseFilter = MouseFilterEnum.Stop;
CreateUi();
LocManager.Instance.SubscribeToLocaleChange(OnLocaleChanged);

// 注册到 InputCapture,让其放过 Panel 内部的 Alt+Click
ItemInputCapture.RegisterBlockingControl(_panel);
Expand Down Expand Up @@ -87,6 +90,8 @@ public override void _ExitTree()
if (_onViewportResized != null)
ViewportResizeNotifier.Instance.OnViewportResized -= _onViewportResized;

LocManager.Instance.UnsubscribeToLocaleChange(OnLocaleChanged);

CombatManager.Instance.CombatSetUp -= OnCombatSetUp;
CombatManager.Instance.CombatEnded -= OnCombatEnded;

Expand Down Expand Up @@ -157,6 +162,7 @@ private void CreateProviderContents()
};
sectionTitle.AddThemeColorOverride("font_color", new Color(0.6f, 0.6f, 0.7f));
sectionTitle.AddThemeFontSizeOverride("font_size", 20);
StsUiFonts.Apply(sectionTitle, FontType.Bold);
sectionContainer.AddChild(sectionTitle);

_contentContainer.AddChild(sectionContainer);
Expand Down Expand Up @@ -354,6 +360,11 @@ private void OnCloseButtonPressed()
QueueFree();
}

private void OnLocaleChanged()
{
StsUiFonts.Refresh(this);
}

private void OnCombatSetUp(CombatState _)
{
Log.Debug("CombatSetUp, refreshing");
Expand Down
1 change: 1 addition & 0 deletions PlayerStateEx/PanelProvider/CardRewardProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ private static Control CreateCardControl(Player player, CardEntry cardEntry)
MouseFilter = Control.MouseFilterEnum.Ignore
};
brokenLabel.AddThemeColorOverride("font_color", new Color(0.8f, 0.2f, 0.2f));
StsUiFonts.Apply(brokenLabel);
return brokenLabel;
}

Expand Down
1 change: 1 addition & 0 deletions PlayerStateEx/PanelProvider/HandCardProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void UpdateContent(Player player, Control content)
};
countLabel.AddThemeColorOverride("font_color", new Color(0.7f, 0.7f, 0.75f));
countLabel.AddThemeFontSizeOverride("font_size", 16);
StsUiFonts.Apply(countLabel);
container.AddChild(countLabel);

var groupCards = LemonSpireConfig.GroupHandCards;
Expand Down
4 changes: 4 additions & 0 deletions PlayerStateEx/PanelProvider/ShopProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public void UpdateContent(Player player, Control content)
MouseFilter = Control.MouseFilterEnum.Ignore
};
emptyLabel.AddThemeColorOverride("font_color", new Color(0.5f, 0.5f, 0.5f));
StsUiFonts.Apply(emptyLabel);
container.AddChild(emptyLabel);
return;
}
Expand Down Expand Up @@ -195,6 +196,7 @@ private static HBoxContainer CreateGoldRow(Player player)
};
goldLabel.AddThemeColorOverride("font_color", StsColors.gold);
goldLabel.AddThemeFontSizeOverride("font_size", 16);
StsUiFonts.Apply(goldLabel);
row.AddChild(goldLabel);

var titleLabel = new Label
Expand All @@ -204,6 +206,7 @@ private static HBoxContainer CreateGoldRow(Player player)
};
titleLabel.AddThemeColorOverride("font_color", new Color(0.6f, 0.6f, 0.65f));
titleLabel.AddThemeFontSizeOverride("font_size", 16);
StsUiFonts.Apply(titleLabel);
row.AddChild(titleLabel);

return row;
Expand Down Expand Up @@ -338,6 +341,7 @@ private static Label CreatePriceLabel(Player player, int cost, bool isOnSale = f
priceLabel.AddThemeColorOverride("font_color", StsColors.cream);

priceLabel.AddThemeFontSizeOverride("font_size", 14);
StsUiFonts.Apply(priceLabel);
return priceLabel;
}

Expand Down
3 changes: 3 additions & 0 deletions util/Ui/DraggableTitleBar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Godot;
using MegaCrit.Sts2.Core.Localization.Fonts;

namespace lemonSpire2.util.Ui;

Expand Down Expand Up @@ -49,10 +50,12 @@ public override void _Ready()
// 内部 HBoxContainer 布局
_titleLabel.AddThemeColorOverride("font_color", new Color(0.9f, 0.9f, 0.95f));
_titleLabel.AddThemeFontSizeOverride("font_size", 20);
StsUiFonts.Apply(_titleLabel, FontType.Bold);
_hbox.AddChild(_titleLabel);

_closeButton.AddThemeColorOverride("font_color", new Color(0.7f, 0.7f, 0.7f));
_closeButton.AddThemeColorOverride("font_hover_color", new Color(1f, 0.3f, 0.3f));
StsUiFonts.Apply(_closeButton, FontType.Bold);
_hbox.AddChild(_closeButton);

AddChild(_hbox);
Expand Down
91 changes: 91 additions & 0 deletions util/Ui/StsUiFonts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Godot;
using MegaCrit.Sts2.Core.Localization;
using MegaCrit.Sts2.Core.Localization.Fonts;

namespace lemonSpire2.util.Ui;

/// <summary>
/// Applies the same font resources and locale substitutions used by the game's UI.
/// </summary>
public static class StsUiFonts
{
private const string RegularFontPath = "res://themes/kreon_regular_shared.tres";
private const string BoldFontPath = "res://themes/kreon_bold_shared.tres";
private const string ItalicFontPath = "res://themes/bitter_medium_italic_glyph_space_one.tres";

private static readonly StringName FontRoleMeta = "lemon_spire_font_role";
private static readonly StringName Font = "font";
private static readonly StringName NormalFont = "normal_font";
private static readonly StringName BoldFont = "bold_font";
private static readonly StringName ItalicsFont = "italics_font";

private static Font? _regularFont;
private static Font? _boldFont;
private static Font? _italicFont;

public static void Apply(Control control, FontType fontType = FontType.Regular)
{
ArgumentNullException.ThrowIfNull(control);

control.SetMeta(FontRoleMeta, (int)fontType);

if (control is RichTextLabel richTextLabel)
{
ApplyRichTextFonts(richTextLabel);
return;
}

control.AddThemeFontOverride(Font, GetFont(fontType));
}

/// <summary>
/// Reapplies fonts to controls previously registered through <see cref="Apply"/>.
/// </summary>
public static void Refresh(Node root)
{
ArgumentNullException.ThrowIfNull(root);

if (root is Control control && control.HasMeta(FontRoleMeta))
{
var fontType = (FontType)control.GetMeta(FontRoleMeta).AsInt32();
Apply(control, fontType);
}

foreach (var child in root.GetChildren())
Refresh(child);
}

private static void ApplyRichTextFonts(RichTextLabel label)
{
label.AddThemeFontOverride(NormalFont, GetFont(FontType.Regular));
label.AddThemeFontOverride(BoldFont, GetFont(FontType.Bold));
label.AddThemeFontOverride(ItalicsFont, GetFont(FontType.Italic));
}

private static Font GetFont(FontType fontType)
{
var locManager = LocManager.Instance;
var substituteFont = locManager is null
? null
: FontManager.GetSubstituteFont(locManager.Language, fontType);

return substituteFont ?? GetDefaultFont(fontType);
}

private static Font GetDefaultFont(FontType fontType)
{
return fontType switch
{
FontType.Regular => _regularFont ??= LoadFont(RegularFontPath),
FontType.Bold => _boldFont ??= LoadFont(BoldFontPath),
FontType.Italic => _italicFont ??= LoadFont(ItalicFontPath),
_ => throw new ArgumentOutOfRangeException(nameof(fontType), fontType, null)
};
}

private static Font LoadFont(string path)
{
return ResourceLoader.Load<Font>(path, null, ResourceLoader.CacheMode.Reuse)
?? throw new InvalidOperationException($"Could not load game font resource: {path}");
}
}