Skip to content

Commit 6fb1b82

Browse files
committed
Add DisplayStyles for labels.
1 parent 15f67a8 commit 6fb1b82

28 files changed

+157
-117
lines changed

Yafc.UI/Core/ExceptionScreen.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ protected internal override void Close() {
3535

3636
protected override void BuildContents(ImGui gui) {
3737
gui.BuildText(ex.GetType().Name, Font.header);
38-
gui.BuildText(ex.Message, Font.subheader, true);
39-
gui.BuildText(ex.StackTrace, Font.text, true);
38+
gui.BuildText(ex.Message, new TextBlockDisplayStyle(Font.subheader, true));
39+
gui.BuildText(ex.StackTrace, TextBlockDisplayStyle.WrappedText);
4040
using (gui.EnterRow(0.5f, RectAllocator.RightRow)) {
4141
if (gui.BuildButton("Close")) {
4242
Close();

Yafc.UI/ImGui/ImGuiBuilding.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ public SchemeColor textColor {
8888
set => state.textColor = value;
8989
}
9090

91-
public void BuildText(string? text, Font? font = null, bool wrap = false, RectAlignment align = RectAlignment.MiddleLeft, SchemeColor color = SchemeColor.None, float topOffset = 0f, float maxWidth = float.MaxValue) {
91+
public void BuildText(string? text, TextBlockDisplayStyle? displayStyle = null, float topOffset = 0f, float maxWidth = float.MaxValue) {
92+
displayStyle ??= TextBlockDisplayStyle.Default();
93+
SchemeColor color = displayStyle.Color;
9294
if (color == SchemeColor.None) {
9395
color = state.textColor;
9496
}
9597

96-
var rect = AllocateTextRect(out var cache, text, font, wrap, align, topOffset, maxWidth);
98+
Rect rect = AllocateTextRect(out TextCache? cache, text, displayStyle, topOffset, maxWidth);
9799
if (action == ImGuiAction.Build && cache != null) {
98100
DrawRenderable(rect, cache, color);
99101
}
@@ -112,16 +114,16 @@ public Vector2 GetTextDimensions(out TextCache? cache, string? text, Font? font
112114
return new Vector2(textWidth, cache.texRect.h / pixelsPerUnit);
113115
}
114116

115-
public Rect AllocateTextRect(out TextCache? cache, string? text, Font? font = null, bool wrap = false, RectAlignment align = RectAlignment.MiddleLeft, float topOffset = 0f, float maxWidth = float.MaxValue) {
116-
var fontSize = GetFontSize(font);
117+
public Rect AllocateTextRect(out TextCache? cache, string? text, TextBlockDisplayStyle displayStyle, float topOffset = 0f, float maxWidth = float.MaxValue) {
118+
FontFile.FontSize fontSize = GetFontSize(displayStyle.Font);
117119
Rect rect;
118120
if (string.IsNullOrEmpty(text)) {
119121
cache = null;
120122
rect = AllocateRect(0f, topOffset + (fontSize.lineSize / pixelsPerUnit));
121123
}
122124
else {
123-
Vector2 textSize = GetTextDimensions(out cache, text, font, wrap, maxWidth);
124-
rect = AllocateRect(textSize.X, topOffset + (textSize.Y), align);
125+
Vector2 textSize = GetTextDimensions(out cache, text, displayStyle.Font, displayStyle.WrapText, maxWidth);
126+
rect = AllocateRect(textSize.X, topOffset + (textSize.Y), displayStyle.Alignment);
125127
}
126128

127129
if (topOffset != 0f) {

Yafc.UI/ImGui/ImGuiUtils.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static ButtonEvent BuildButton(this ImGui gui, Rect rect, SchemeColor nor
5858
public static string ScanToString(SDL.SDL_Scancode scancode) => SDL.SDL_GetKeyName(SDL.SDL_GetKeyFromScancode(scancode));
5959

6060
public static bool BuildLink(this ImGui gui, string text) {
61-
gui.BuildText(text, color: SchemeColor.Link);
61+
gui.BuildText(text, TextBlockDisplayStyle.Default(SchemeColor.Link));
6262
var rect = gui.lastRect;
6363
switch (gui.action) {
6464
case ImGuiAction.MouseMove:
@@ -105,7 +105,7 @@ public static ButtonEvent BuildButton(this ImGui gui, string text, SchemeColor c
105105
}
106106

107107
using (gui.EnterGroup(padding ?? DefaultButtonPadding, active ? color + 2 : color + 3)) {
108-
gui.BuildText(text, Font.text, align: RectAlignment.Middle);
108+
gui.BuildText(text, TextBlockDisplayStyle.Centered);
109109
}
110110

111111
return active ? gui.BuildButton(gui.lastRect, color, color + 1) : ButtonEvent.None;
@@ -119,10 +119,10 @@ public static ButtonEvent BuildContextMenuButton(this ImGui gui, string text, st
119119
gui.BuildIcon(icon, color: icon >= Icon.FirstCustom ? disabled ? SchemeColor.SourceFaint : SchemeColor.Source : textColor);
120120
}
121121

122-
gui.BuildText(text, Font.text, true, color: textColor);
122+
gui.BuildText(text, TextBlockDisplayStyle.WrappedText with { Color = textColor });
123123
if (rightText != null) {
124124
gui.allocator = RectAllocator.RightRow;
125-
gui.BuildText(rightText, align: RectAlignment.MiddleRight);
125+
gui.BuildText(rightText, new TextBlockDisplayStyle(Alignment: RectAlignment.MiddleRight));
126126
}
127127
}
128128
return gui.BuildButton(gui.lastRect, SchemeColor.None, SchemeColor.Grey);
@@ -142,7 +142,7 @@ public static ButtonEvent BuildRedButton(this ImGui gui, string text) {
142142
Rect textRect;
143143
TextCache? cache;
144144
using (gui.EnterGroup(DefaultButtonPadding)) {
145-
textRect = gui.AllocateTextRect(out cache, text, align: RectAlignment.Middle);
145+
textRect = gui.AllocateTextRect(out cache, text, TextBlockDisplayStyle.Centered);
146146
}
147147

148148
var evt = gui.BuildButton(gui.lastRect, SchemeColor.None, SchemeColor.Error);
@@ -200,7 +200,7 @@ public static bool WithTooltip(this ButtonEvent evt, ImGui gui, string tooltip,
200200
public static bool BuildCheckBox(this ImGui gui, string text, bool value, out bool newValue, SchemeColor color = SchemeColor.None, RectAllocator allocator = RectAllocator.LeftRow) {
201201
using (gui.EnterRow(allocator: allocator)) {
202202
gui.BuildIcon(value ? Icon.CheckBoxCheck : Icon.CheckBoxEmpty, 1.5f, color);
203-
gui.BuildText(text, Font.text, color: color);
203+
gui.BuildText(text, TextBlockDisplayStyle.Default(color));
204204
}
205205

206206
if (gui.OnClick(gui.lastRect)) {
@@ -215,7 +215,7 @@ public static bool BuildCheckBox(this ImGui gui, string text, bool value, out bo
215215
public static bool BuildRadioButton(this ImGui gui, string option, bool selected, SchemeColor color = SchemeColor.None) {
216216
using (gui.EnterRow()) {
217217
gui.BuildIcon(selected ? Icon.RadioCheck : Icon.RadioEmpty, 1.5f, color);
218-
gui.BuildText(option, Font.text, color: color, wrap: true);
218+
gui.BuildText(option, TextBlockDisplayStyle.WrappedText with { Color = color });
219219
}
220220

221221
return !selected && gui.OnClick(gui.lastRect);
@@ -239,7 +239,7 @@ public static bool BuildErrorRow(this ImGui gui, string text) {
239239
closed = true;
240240
}
241241

242-
gui.RemainingRow().BuildText(text, align: RectAlignment.Middle);
242+
gui.RemainingRow().BuildText(text, TextBlockDisplayStyle.Centered);
243243
}
244244
if (gui.isBuilding) {
245245
gui.DrawRectangle(gui.lastRect, SchemeColor.Error);
@@ -263,7 +263,7 @@ public static bool BuildIntegerInput(this ImGui gui, int value, out int newValue
263263

264264
public static void ShowTooltip(this ImGui gui, Rect rect, GuiBuilder builder, float width = 20f) => gui.window?.ShowTooltip(gui, rect, builder, width);
265265

266-
public static void ShowTooltip(this ImGui gui, Rect rect, string text, float width = 20f) => gui.window?.ShowTooltip(gui, rect, x => x.BuildText(text, wrap: true), width);
266+
public static void ShowTooltip(this ImGui gui, Rect rect, string text, float width = 20f) => gui.window?.ShowTooltip(gui, rect, x => x.BuildText(text, TextBlockDisplayStyle.WrappedText), width);
267267

268268
public static void ShowTooltip(this ImGui gui, GuiBuilder builder, float width = 20f) => gui.window?.ShowTooltip(gui, gui.lastRect, builder, width);
269269

Yafc.UI/ImGui/TextDisplayStyles.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace Yafc.UI;
2+
3+
/// <summary>
4+
/// Contains the display parameters for fixed text (<c>TextBlock</c> in WPF, <c>Label</c> in WinForms)
5+
/// </summary>
6+
/// <param name="Font">The <see cref="UI.Font"/> to use when drawing the text, or <see langword="null"/> to use <see cref="Font.text"/>.</param>
7+
/// <param name="WrapText">Specifies whether or not the text should be wrapped.</param>
8+
/// <param name="Alignment">Where the text should be drawn within the renderable area.</param>
9+
/// <param name="Color">The color to use, or <see cref="SchemeColor.None"/> to use the previous color.</param>
10+
public record TextBlockDisplayStyle(Font? Font = null, bool WrapText = false, RectAlignment Alignment = RectAlignment.MiddleLeft, SchemeColor Color = SchemeColor.None) {
11+
/// <summary>
12+
/// Gets the default display style (<see cref="Font.text"/>, not wrapped, left-aligned), with the specified color.
13+
/// </summary>
14+
/// <param name="color">The color to use, or <see cref="SchemeColor.None"/> to use the previous color.</param>
15+
public static TextBlockDisplayStyle Default(SchemeColor color = SchemeColor.None) => new(Color: color);
16+
/// <summary>
17+
/// Gets the display style for nonwrapped centered text.
18+
/// </summary>
19+
public static TextBlockDisplayStyle Centered { get; } = new(Alignment: RectAlignment.Middle);
20+
/// <summary>
21+
/// Gets the display style for hint text.
22+
/// </summary>
23+
public static TextBlockDisplayStyle HintText { get; } = new(Color: SchemeColor.BackgroundTextFaint);
24+
/// <summary>
25+
/// Gets the display style for wrapped, left-aligned text.
26+
/// </summary>
27+
public static TextBlockDisplayStyle WrappedText { get; } = new(WrapText: true);
28+
/// <summary>
29+
/// Gets the display style for most error messages.
30+
/// </summary>
31+
public static TextBlockDisplayStyle ErrorText { get; } = new(WrapText: true, Color: SchemeColor.Error);
32+
33+
/// <summary>
34+
/// Converts a font to the default display style (not wrapped, left-aligned, default color) for that font.
35+
/// </summary>
36+
public static implicit operator TextBlockDisplayStyle(Font font) => new(font);
37+
}

Yafc/Widgets/ImmediateWidgets.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ public static Click BuildFactorioObjectButtonWithText(this ImGui gui, FactorioOb
130130
if (extraText != null) {
131131
gui.AllocateSpacing();
132132
gui.allocator = RectAllocator.RightRow;
133-
gui.BuildText(extraText, color: color);
133+
gui.BuildText(extraText, TextBlockDisplayStyle.Default(color));
134134
}
135135
_ = gui.RemainingRow();
136-
gui.BuildText(obj == null ? "None" : obj.locName, wrap: true, color: color);
136+
gui.BuildText(obj == null ? "None" : obj.locName, TextBlockDisplayStyle.WrappedText with { Color = color });
137137
}
138138

139139
return gui.BuildFactorioObjectButton(gui.lastRect, obj);
@@ -205,13 +205,14 @@ public static void BuildInlineObjectListAndButtonWithNone<T>(this ImGui gui, ICo
205205
/// <param name="goods">Draw the icon for this object, or an empty box if this is <see langword="null"/>.</param>
206206
/// <param name="amount">Display this value and unit.</param>
207207
/// <param name="useScale">If <see langword="true"/>, this icon will be displayed at <see cref="ProjectPreferences.iconScale"/>, instead of at 100% scale.</param>
208-
public static Click BuildFactorioObjectWithAmount(this ImGui gui, FactorioObject? goods, DisplayAmount amount, SchemeColor bgColor = SchemeColor.None, SchemeColor textColor = SchemeColor.None, bool useScale = true, ObjectTooltipOptions tooltipOptions = default) {
208+
public static Click BuildFactorioObjectWithAmount(this ImGui gui, FactorioObject? goods, DisplayAmount amount, SchemeColor bgColor = SchemeColor.None, TextBlockDisplayStyle? textDisplayStyle = null, bool useScale = true, ObjectTooltipOptions tooltipOptions = default) {
209+
textDisplayStyle ??= new(Alignment: RectAlignment.Middle);
209210
using (gui.EnterFixedPositioning(3f, 3f, default)) {
210211
gui.allocator = RectAllocator.Stretch;
211212
gui.spacing = 0f;
212213
Click clicked = gui.BuildFactorioObjectButton(goods, 3f, MilestoneDisplay.Contained, bgColor, useScale, tooltipOptions);
213214
if (goods != null) {
214-
gui.BuildText(DataUtils.FormatAmount(amount.Value, amount.Unit), Font.text, false, RectAlignment.Middle, textColor);
215+
gui.BuildText(DataUtils.FormatAmount(amount.Value, amount.Unit), textDisplayStyle);
215216
if (InputSystem.Instance.control && gui.BuildButton(gui.lastRect, SchemeColor.None, SchemeColor.Grey) == ButtonEvent.MouseOver) {
216217
ShowPrecisionValueTooltip(gui, amount, goods);
217218
}
@@ -241,7 +242,7 @@ public static void ShowPrecisionValueTooltip(ImGui gui, DisplayAmount amount, Fa
241242
}
242243
gui.ShowTooltip(gui.lastRect, x => {
243244
_ = x.BuildFactorioObjectButtonWithText(goods);
244-
x.BuildText(text, wrap: true);
245+
x.BuildText(text, TextBlockDisplayStyle.WrappedText);
245246
}, 10f);
246247
}
247248

0 commit comments

Comments
 (0)