From d0923c6082228cdaf5e55803d4536d7c4678e593 Mon Sep 17 00:00:00 2001 From: galister <22305755+galister@users.noreply.github.com> Date: Mon, 15 May 2023 10:20:22 +0200 Subject: [PATCH] multiline labels --- GFX/FontCollection.cs | 9 +++++++-- Overlays/Toast.cs | 18 +++++++++++------- UI/Label.cs | 24 ++++++++++++++++-------- UI/LabelCentered.cs | 33 +++++++++++++++++++++------------ 4 files changed, 55 insertions(+), 29 deletions(-) diff --git a/GFX/FontCollection.cs b/GFX/FontCollection.cs index 9284000..80c0bdf 100644 --- a/GFX/FontCollection.cs +++ b/GFX/FontCollection.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using WlxOverlay.Numerics; namespace WlxOverlay.GFX; @@ -34,9 +35,11 @@ private FontCollection(int size, FontStyle style) _codePointToFont[0] = _codePointToFont['a']; } - public int GetTextWidth(string s) + public (int w, int h) GetTextSize(string s) { - return GetTextures(s).Sum(x => x?.AdvX ?? _size / 3); + var lines = s.Split('\n'); + var maxW = lines.Max(l => GetTextures(l).Sum(x => x?.AdvX ?? _size / 3)); + return (maxW, lines.Length * LineSpacing()); } public IEnumerable GetTextures(string s) @@ -56,6 +59,8 @@ public int GetTextWidth(string s) public int Size() => _size; + public int LineSpacing() => (int)(_size * 1.5f); + public static void CloseHandles() { lock (Lock) diff --git a/Overlays/Toast.cs b/Overlays/Toast.cs index ea30506..6bca852 100644 --- a/Overlays/Toast.cs +++ b/Overlays/Toast.cs @@ -37,26 +37,30 @@ public Toast(string title, string? content, float opacity, uint height, float ti protected override void Initialize() { - var width = (uint)Font.GetTextWidth(_content ?? _title) + Padding; + var (w, h) = Font.GetTextSize(_content ?? _title); + + var width = (uint)(w + Padding); + var height = (uint)(_height - Font.Size() + h); + WidthInMeters = width / 2000f; - _canvas = new Canvas(width, _height); + _canvas = new Canvas(width, height); Canvas.CurrentFont = Font; Canvas.CurrentBgColor = HexColor.FromRgb("#353535"); Canvas.CurrentFgColor = HexColor.FromRgb("#aaaaaa"); - _canvas.AddControl(new Panel(0, 0, width, _height)); + _canvas.AddControl(new Panel(0, 0, width, height)); if (_content == null) { - _canvas.AddControl(new LabelCentered(_title, 0, 0, width, _height)); + _canvas.AddControl(new LabelCentered(_title, 0, 0, width, height)); } else { - _canvas.AddControl(new LabelCentered(_content, 0, 0, width, _height - 36U)); + _canvas.AddControl(new Label(_content, 0, 0, width, height - 36U)); Canvas.CurrentBgColor = HexColor.FromRgb("#666666"); - _canvas.AddControl(new Panel(0, (int)_height - 36, width, _height)); + _canvas.AddControl(new Panel(0, (int)_height - 36, width, height)); Canvas.CurrentFgColor = HexColor.FromRgb("#000000"); _canvas.AddControl(new LabelCentered(_title, 0, (int)_height - 36, width, 36U)); } @@ -100,4 +104,4 @@ public override void Dispose() _canvas!.Dispose(); base.Dispose(); } -} \ No newline at end of file +} diff --git a/UI/Label.cs b/UI/Label.cs index b5b74b1..a698e32 100644 --- a/UI/Label.cs +++ b/UI/Label.cs @@ -19,7 +19,7 @@ public string? Text get => _text; set { - _text = value; + _text = value?.ReplaceLineEndings("\n"); Dirty = true; Canvas?.MarkDirty(); } @@ -50,15 +50,23 @@ public override void Render() if (_text == null) return; - var curX = X; - foreach (var g in Font.GetTextures(_text)) + var lines = _text.Split('\n'); + var height = (int)(Font.Size() * 1.5f); + + var curY = Y; + foreach (var line in lines.Reverse()) { - if (g == null) - continue; + var curX = X; + foreach (var g in Font.GetTextures(_text)) + { + if (g == null) + continue; - GraphicsEngine.UiRenderer.DrawFont(g, FgColor, curX, Y, g.Texture.GetWidth(), g.Texture.GetHeight()); + GraphicsEngine.UiRenderer.DrawFont(g, FgColor, curX, Y, g.Texture.GetWidth(), g.Texture.GetHeight()); - curX += g.AdvX; + curX += g.AdvX; + } + curY -= height; } } -} \ No newline at end of file +} diff --git a/UI/LabelCentered.cs b/UI/LabelCentered.cs index 396c001..fdfc3c1 100644 --- a/UI/LabelCentered.cs +++ b/UI/LabelCentered.cs @@ -4,7 +4,7 @@ namespace WlxOverlay.UI; public class LabelCentered : Label { - private int _textWidth = -1; + private List<(int w, int h)> _textSizes = new(); public LabelCentered(string text, int x, int y, uint w, uint h) : base(text, x, y, w, h) { @@ -15,26 +15,35 @@ public override void Render() if (Text == null) return; + var lines = Text.Split('\n'); + if (Dirty) { - _textWidth = Font.GetTextWidth(Text); + _textSizes.Clear(); + _textSizes.AddRange(lines.Select(l => Font.GetTextSize(l))); Dirty = false; } - var y = (int)(Y + Height / 2 - Font.Size() / 2); - var curX = (int)(X + Width / 2 - _textWidth / 2); + var linesHeight = Font.Size() + Font.LineSpacing() * (lines.Length - 1); + var curY = (int)(Y + Height / 2 - linesHeight / 2); - foreach (var g in Font.GetTextures(Text)) + for (var i = 0; i < lines.Length; i++) { - if (g == null) + var curX = (int)(X + Width / 2 - _textSizes[i].w / 2); + + foreach (var g in Font.GetTextures(lines[i])) { - curX += Font.Size() / 3; - continue; - } + if (g == null) + { + curX += Font.Size() / 3; + continue; + } - GraphicsEngine.UiRenderer.DrawFont(g, FgColor, curX, y, g.Texture.GetWidth(), g.Texture.GetHeight()); + GraphicsEngine.UiRenderer.DrawFont(g, FgColor, curX, curY, g.Texture.GetWidth(), g.Texture.GetHeight()); - curX += g.AdvX; + curX += g.AdvX; + } + curY += Font.LineSpacing(); } } -} \ No newline at end of file +}