Skip to content

Commit 450d3a4

Browse files
authored
Use calculated width for first column of Summary page (#167)
With this PR the fixed width (introduced by #38) will be no more and the column width is dynamically calculated depending on the names of the production sheets. --- This change is part of a larger set of changes that I am planning to commit, but those changes need some additional love... (It works, but I broke some things that should not be broken). This change is ready and works separately and adds value to YAFC already. I had to apply one small hack to make this a stand-alone PR, which is making `firstColumnWidth` static. In the upcoming PR it will be a regular field of its class.
2 parents d430d1b + 0ac3048 commit 450d3a4

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

Yafc.UI/ImGui/ImGuiBuilding.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ public void BuildText(string? text, Font? font = null, bool wrap = false, RectAl
100100
}
101101
}
102102

103+
public Vector2 GetTextDimensions(out TextCache? cache, string? text, Font? font = null, bool wrap = false, float maxWidth = float.MaxValue) {
104+
if (string.IsNullOrEmpty(text)) {
105+
cache = null;
106+
return Vector2.Zero;
107+
}
108+
109+
var fontSize = GetFontSize(font);
110+
cache = textCache.GetCached((fontSize, text, wrap ? (uint)UnitsToPixels(MathF.Max(width, 5f)) : uint.MaxValue));
111+
float textWidth = Math.Min(cache.texRect.w / pixelsPerUnit, maxWidth);
112+
113+
return new Vector2(textWidth, cache.texRect.h / pixelsPerUnit);
114+
}
115+
103116
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) {
104117
var fontSize = GetFontSize(font);
105118
Rect rect;
@@ -108,9 +121,8 @@ public Rect AllocateTextRect(out TextCache? cache, string? text, Font? font = nu
108121
rect = AllocateRect(0f, topOffset + (fontSize.lineSize / pixelsPerUnit));
109122
}
110123
else {
111-
cache = textCache.GetCached((fontSize, text, wrap ? (uint)UnitsToPixels(MathF.Max(width, 5f)) : uint.MaxValue));
112-
float textWidth = Math.Min(cache.texRect.w / pixelsPerUnit, maxWidth);
113-
rect = AllocateRect(textWidth, topOffset + (cache.texRect.h / pixelsPerUnit), align);
124+
Vector2 textSize = GetTextDimensions(out cache, text, font, wrap, maxWidth);
125+
rect = AllocateRect(textSize.X, topOffset + (textSize.Y), align);
114126
}
115127

116128
if (topOffset != 0f) {

Yafc/Workspace/SummaryView.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics.CodeAnalysis;
4+
using System.Numerics;
45
using Yafc.Model;
56
using Yafc.UI;
67
using YAFC.Model;
78

89
namespace Yafc {
910
public class SummaryView : ProjectPageView<Summary> {
11+
/// <summary>Some padding to have the contents of the first column not 'stick' to the rest of the UI</summary>
12+
private readonly Padding FirstColumnPadding = new Padding(1f, 1.5f, 0, 0);
13+
private static float firstColumnWidth;
14+
1015
private class SummaryScrollArea(GuiBuilder builder) : ScrollArea(DefaultHeight, builder, MainScreen.Instance.InputSystem, horizontal: true) {
1116
private static readonly float DefaultHeight = 10;
1217

@@ -16,23 +21,23 @@ private class SummaryScrollArea(GuiBuilder builder) : ScrollArea(DefaultHeight,
1621
}
1722

1823
private class SummaryTabColumn : TextDataColumn<ProjectPage> {
19-
private const float FirstColumnWidth = 14f; // About 20 'o' wide
20-
21-
public SummaryTabColumn() : base("Tab", FirstColumnWidth) {
24+
public SummaryTabColumn() : base("Tab", firstColumnWidth) {
2225
}
2326

2427
public override void BuildElement(ImGui gui, ProjectPage page) {
28+
width = firstColumnWidth;
29+
2530
if (page?.contentType != typeof(ProductionTable)) {
2631
return;
2732
}
2833

2934
using (gui.EnterGroup(new Padding(0.5f, 0.2f, 0.2f, 0.5f))) {
3035
gui.spacing = 0.2f;
3136
if (page.icon != null) {
32-
gui.BuildIcon(page.icon.icon);
37+
gui.BuildIcon(page.icon.icon, FirstColumnIconSize);
3338
}
3439
else {
35-
_ = gui.AllocateRect(0f, 1.5f);
40+
_ = gui.AllocateRect(0f, FirstColumnIconSize);
3641
}
3742

3843
gui.BuildText(page.name);
@@ -142,6 +147,7 @@ private static void SetProviderAmount(ProductionLink element, ProjectPage page,
142147
private static readonly float Epsilon = 1e-5f;
143148
private static readonly float ElementWidth = 3;
144149
private static readonly float ElementSpacing = 1;
150+
private static readonly float FirstColumnIconSize = 1.5f;
145151

146152
private struct GoodDetails {
147153
public float totalProvided;
@@ -201,12 +207,34 @@ protected override void BuildHeader(ImGui gui) {
201207
gui.allocator = RectAllocator.LeftAlign;
202208
}
203209

210+
private float CalculateFirstColumWidth(ImGui gui) {
211+
// At the least the icons should fit
212+
float width = FirstColumnIconSize;
213+
214+
foreach (Guid displayPage in project.displayPages) {
215+
ProjectPage? page = project.FindPage(displayPage);
216+
if (page?.contentType != typeof(ProductionTable)) {
217+
continue;
218+
}
219+
220+
Vector2 textSize = gui.GetTextDimensions(out _, page.name);
221+
width = MathF.Max(width, textSize.X);
222+
}
223+
224+
// Include padding to perfectly match
225+
return width + FirstColumnPadding.left + FirstColumnPadding.right;
226+
}
227+
204228
protected override void BuildContent(ImGui gui) {
205229
if (gui.BuildCheckBox("Only show issues", model.showOnlyIssues, out bool newValue)) {
206230
model.showOnlyIssues = newValue;
207231
Recalculate();
208232
}
209233

234+
if (gui.isBuilding) {
235+
firstColumnWidth = CalculateFirstColumWidth(gui);
236+
}
237+
210238
scrollArea.Build(gui);
211239
}
212240

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Date: soon
1717
- Add several right-click and keyboard shortcuts, notably Enter/Return to close most dialogs.
1818
- Add UI rebuilding itself on resize.
1919
- When opening the main window, use the same size it was when it was last closed.
20+
- Use calculated width for first column of Summary page.
2021
Bugfixes:
2122
- Fix that some pages couldn't be deleted.
2223
- Fix that returning to the Welcome Screen could break the panels in the main window.

0 commit comments

Comments
 (0)