Skip to content

Commit

Permalink
Fix ShadowTheAge#224: long milestone names could hide remove buttons. (
Browse files Browse the repository at this point in the history
…#129)

Set a maximum width for names in the milestone editor, and then increase
the width of the milestone editor so that limit isn't reached in the
longest found case -- the Russian locale of Exotic Industries.
  • Loading branch information
shpaass authored May 14, 2024
2 parents 46fcb2d + db4802e commit 96a2173
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
9 changes: 5 additions & 4 deletions Yafc.UI/ImGui/ImGuiBuilding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ public SchemeColor textColor {
set => state.textColor = value;
}

public void BuildText(string text, Font font = null, bool wrap = false, RectAlignment align = RectAlignment.MiddleLeft, SchemeColor color = SchemeColor.None, float topOffset = 0f) {
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) {
if (color == SchemeColor.None) {
color = state.textColor;
}

var rect = AllocateTextRect(out var cache, text, font, wrap, align, topOffset);
var rect = AllocateTextRect(out var cache, text, font, wrap, align, topOffset, maxWidth);
if (action == ImGuiAction.Build && cache != null) {
DrawRenderable(rect, cache, color);
}
}

public Rect AllocateTextRect(out TextCache cache, string text, Font font = null, bool wrap = false, RectAlignment align = RectAlignment.MiddleLeft, float topOffset = 0f) {
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) {
var fontSize = GetFontSize(font);
Rect rect;
if (string.IsNullOrEmpty(text)) {
Expand All @@ -106,7 +106,8 @@ public Rect AllocateTextRect(out TextCache cache, string text, Font font = null,
}
else {
cache = textCache.GetCached((fontSize, text, wrap ? (uint)UnitsToPixels(MathF.Max(width, 5f)) : uint.MaxValue));
rect = AllocateRect(cache.texRect.w / pixelsPerUnit, topOffset + (cache.texRect.h / pixelsPerUnit), align);
float textWidth = Math.Min(cache.texRect.w / pixelsPerUnit, maxWidth);
rect = AllocateRect(textWidth, topOffset + (cache.texRect.h / pixelsPerUnit), align);
}

if (topOffset != 0f) {
Expand Down
2 changes: 1 addition & 1 deletion Yafc/Widgets/PseudoScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Yafc {
// Pseudo screen is not an actual screen, it is a panel shown in the middle of the main screen
public abstract class PseudoScreen : IKeyboardFocus {
public readonly ImGui contents;
private readonly float width;
protected readonly float width;
protected bool opened;

protected PseudoScreen(float width = 40f) {
Expand Down
4 changes: 2 additions & 2 deletions Yafc/Windows/MilestonesEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class MilestonesEditor : PseudoScreen {
private static readonly MilestonesEditor Instance = new MilestonesEditor();
private readonly VirtualScrollList<FactorioObject> milestoneList;

public MilestonesEditor() => milestoneList = new VirtualScrollList<FactorioObject>(30f, new Vector2(float.PositiveInfinity, 3f), MilestoneDrawer);
public MilestonesEditor() : base(50) => milestoneList = new VirtualScrollList<FactorioObject>(30f, new Vector2(float.PositiveInfinity, 3f), MilestoneDrawer);

public override void Open() {
base.Open();
Expand All @@ -21,7 +21,7 @@ private void MilestoneDrawer(ImGui gui, FactorioObject element, int index) {
using (gui.EnterRow()) {
var settings = Project.current.settings;
gui.BuildFactorioObjectIcon(element, MilestoneDisplay.None, 3f);
gui.BuildText(element.locName);
gui.BuildText(element.locName, maxWidth: width - 16.6f); // Experimentally determined width of the non-text parts of the editor.
if (gui.BuildButton(Icon.Close, size: 1f)) {
_ = settings.RecordUndo().milestones.Remove(element);
Rebuild();
Expand Down

0 comments on commit 96a2173

Please sign in to comment.