Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
multiline labels
Browse files Browse the repository at this point in the history
  • Loading branch information
galister committed May 15, 2023
1 parent 92b5a96 commit d0923c6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 29 deletions.
9 changes: 7 additions & 2 deletions GFX/FontCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using WlxOverlay.Numerics;

namespace WlxOverlay.GFX;

Expand Down Expand Up @@ -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<Glyph?> GetTextures(string s)
Expand All @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions Overlays/Toast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -100,4 +104,4 @@ public override void Dispose()
_canvas!.Dispose();
base.Dispose();
}
}
}
24 changes: 16 additions & 8 deletions UI/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public string? Text
get => _text;
set
{
_text = value;
_text = value?.ReplaceLineEndings("\n");
Dirty = true;
Canvas?.MarkDirty();
}
Expand Down Expand Up @@ -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;
}
}
}
}
33 changes: 21 additions & 12 deletions UI/LabelCentered.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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();
}
}
}
}

0 comments on commit d0923c6

Please sign in to comment.