Skip to content

Commit 9e4216c

Browse files
committed
Add DisplayStyles for text input boxes.
1 parent 6fb1b82 commit 9e4216c

File tree

8 files changed

+55
-34
lines changed

8 files changed

+55
-34
lines changed

Yafc.UI/ImGui/ImGuiBuilding.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,17 @@ public void DrawText(Rect rect, string text, RectAlignment alignment = RectAlign
148148

149149
private ImGuiTextInputHelper? textInputHelper;
150150
public bool BuildTextInput(string? text, out string newText, string? placeholder, Icon icon = Icon.None, bool delayed = false, bool setInitialFocus = false) {
151-
Padding padding = new Padding(icon == Icon.None ? 0.8f : 0.5f, 0.5f);
152-
return BuildTextInput(text, out newText, placeholder, icon, delayed, padding, setInitialFocus: setInitialFocus);
151+
TextBoxDisplayStyle displayStyle = TextBoxDisplayStyle.DefaultTextInput;
152+
if (icon != Icon.None) {
153+
displayStyle = displayStyle with { Icon = icon };
154+
}
155+
return BuildTextInput(text, out newText, placeholder, displayStyle, delayed, setInitialFocus);
153156
}
154157

155-
public bool BuildTextInput(string? text, out string newText, string? placeholder, Icon icon, bool delayed, Padding padding, RectAlignment alignment = RectAlignment.MiddleLeft, SchemeColorGroup color = SchemeColorGroup.Grey, bool setInitialFocus = false) {
158+
public bool BuildTextInput(string? text, out string newText, string? placeholder, TextBoxDisplayStyle displayStyle, bool delayed, bool setInitialFocus = false) {
156159
setInitialFocus &= textInputHelper == null;
157160
textInputHelper ??= new ImGuiTextInputHelper(this);
158-
bool result = textInputHelper.BuildTextInput(text, out newText, placeholder, GetFontSize(), delayed, icon, padding, alignment, color);
161+
bool result = textInputHelper.BuildTextInput(text, out newText, placeholder, GetFontSize(), delayed, displayStyle);
159162
if (setInitialFocus) {
160163
SetTextInputFocus(lastRect, "");
161164
}

Yafc.UI/ImGui/ImGuiTextInputHelper.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ private void GetTextParameters(string? textToBuild, Rect textRect, FontFile.Font
5555
}
5656
}
5757

58-
public bool BuildTextInput(string? text, out string newText, string? placeholder, FontFile.FontSize fontSize, bool delayed, Icon icon, Padding padding, RectAlignment alignment, SchemeColorGroup color) {
58+
public bool BuildTextInput(string? text, out string newText, string? placeholder, FontFile.FontSize fontSize, bool delayed, TextBoxDisplayStyle displayStyle) {
5959
newText = text ?? "";
6060
Rect textRect, realTextRect;
61-
using (gui.EnterGroup(padding, RectAllocator.LeftRow)) {
61+
using (gui.EnterGroup(displayStyle.Padding, RectAllocator.LeftRow)) {
6262
float lineSize = gui.PixelsToUnits(fontSize.lineSize);
63-
if (icon != Icon.None) {
64-
gui.BuildIcon(icon, lineSize, (SchemeColor)color + 3);
63+
if (displayStyle.Icon != Icon.None) {
64+
gui.BuildIcon(displayStyle.Icon, lineSize, (SchemeColor)displayStyle.ColorGroup + 3);
6565
}
6666

6767
textRect = gui.RemainingRow(0.3f).AllocateRect(0, lineSize, RectAlignment.MiddleFullRow);
@@ -81,32 +81,32 @@ public bool BuildTextInput(string? text, out string newText, string? placeholder
8181

8282
if (gui.ConsumeMouseDown(boundingRect)) {
8383
SetFocus(boundingRect, text ?? "");
84-
GetTextParameters(this.text, textRect, fontSize, alignment, out _, out _, out _, out realTextRect);
84+
GetTextParameters(this.text, textRect, fontSize, displayStyle.Alignment, out _, out _, out _, out realTextRect);
8585
SetCaret(FindCaretIndex(text, gui.mousePosition.X - realTextRect.X, fontSize, textRect.Width));
8686
}
8787
break;
8888
case ImGuiAction.MouseMove:
8989
if (focused && gui.actionParameter == SDL.SDL_BUTTON_LEFT) {
90-
GetTextParameters(this.text, textRect, fontSize, alignment, out _, out _, out _, out realTextRect);
90+
GetTextParameters(this.text, textRect, fontSize, displayStyle.Alignment, out _, out _, out _, out realTextRect);
9191
SetCaret(caret, FindCaretIndex(this.text, gui.mousePosition.X - realTextRect.X, fontSize, textRect.Width));
9292
}
9393
_ = gui.ConsumeMouseOver(boundingRect, RenderingUtils.cursorCaret, false);
9494
break;
9595
case ImGuiAction.Build:
96-
SchemeColor textColor = (SchemeColor)color + 2;
96+
SchemeColor textColor = (SchemeColor)displayStyle.ColorGroup + 2;
9797
string? textToBuild;
9898
if (focused && !string.IsNullOrEmpty(text)) {
9999
textToBuild = this.text;
100100
}
101101
else if (string.IsNullOrEmpty(text)) {
102102
textToBuild = placeholder;
103-
textColor = (SchemeColor)color + 3;
103+
textColor = (SchemeColor)displayStyle.ColorGroup + 3;
104104
}
105105
else {
106106
textToBuild = text;
107107
}
108108

109-
GetTextParameters(textToBuild, textRect, fontSize, alignment, out var cachedText, out float scale, out float textWidth, out realTextRect);
109+
GetTextParameters(textToBuild, textRect, fontSize, displayStyle.Alignment, out TextCache? cachedText, out float scale, out float textWidth, out realTextRect);
110110
if (cachedText != null) {
111111
gui.DrawRenderable(realTextRect, cachedText, textColor);
112112
}
@@ -125,11 +125,11 @@ public bool BuildTextInput(string? text, out string newText, string? placeholder
125125
gui.SetNextRebuild(nextCaretTimer);
126126
if (caretVisible) {
127127
float caretPosition = GetCharacterPosition(caret, fontSize, textWidth) * scale;
128-
gui.DrawRectangle(new Rect(caretPosition + realTextRect.X - 0.05f, realTextRect.Y, 0.1f, realTextRect.Height), (SchemeColor)color + 2);
128+
gui.DrawRectangle(new Rect(caretPosition + realTextRect.X - 0.05f, realTextRect.Y, 0.1f, realTextRect.Height), (SchemeColor)displayStyle.ColorGroup + 2);
129129
}
130130
}
131131
}
132-
gui.DrawRectangle(boundingRect, (SchemeColor)color);
132+
gui.DrawRectangle(boundingRect, (SchemeColor)displayStyle.ColorGroup);
133133
break;
134134
}
135135

Yafc.UI/ImGui/TextDisplayStyles.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,25 @@ public record TextBlockDisplayStyle(Font? Font = null, bool WrapText = false, Re
3535
/// </summary>
3636
public static implicit operator TextBlockDisplayStyle(Font font) => new(font);
3737
}
38+
39+
/// <summary>
40+
/// Contains the display parameters for editable text (<c>TextBox</c> in both WPF and WinForms)
41+
/// </summary>
42+
/// <param name="Icon">The <see cref="Icon"/> to display to the left of the text, or <see cref="Icon.None"/> to display no icon.</param>
43+
/// <param name="Padding">The <see cref="UI.Padding"/> to place between the text and the edges of the editable area. (The box area not used by <paramref name="Icon"/>.)</param>
44+
/// <param name="Alignment">The <see cref="RectAlignment"/> to apply when drawing the text within the edit box.</param>
45+
/// <param name="ColorGroup">The <see cref="SchemeColorGroup"/> to use when drawing the edit box.</param>
46+
public record TextBoxDisplayStyle(Icon Icon, Padding Padding, RectAlignment Alignment, SchemeColorGroup ColorGroup) {
47+
/// <summary>
48+
/// Gets the default display style, used for the Preferences screen and calls to <see cref="ImGui.BuildTextInput(string?, out string, string?, Icon, bool, bool)"/>.
49+
/// </summary>
50+
public static TextBoxDisplayStyle DefaultTextInput { get; } = new(Icon.None, new Padding(.5f), RectAlignment.MiddleLeft, SchemeColorGroup.Grey);
51+
/// <summary>
52+
/// Gets the display style for input boxes on the Module Filler Parameters screen.
53+
/// </summary>
54+
public static TextBoxDisplayStyle ModuleParametersTextInput { get; } = new(Icon.None, new Padding(.5f, 0), RectAlignment.MiddleLeft, SchemeColorGroup.Grey);
55+
/// <summary>
56+
/// Gets the display style for amounts associated with Factorio objects. (<c><see langword="with"/> { ColorGroup = <see cref="SchemeColorGroup.Grey"/> }</c> for built building counts.)
57+
/// </summary>
58+
public static TextBoxDisplayStyle FactorioObjectInput { get; } = new(Icon.None, default, RectAlignment.Middle, SchemeColorGroup.Secondary);
59+
}

Yafc/Widgets/ImmediateWidgets.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public static void BuildFactorioObjectIcon(this ImGui gui, FactorioObject? obj,
6060
}
6161
}
6262

63-
public static bool BuildFloatInput(this ImGui gui, DisplayAmount amount, Padding padding, bool setInitialFocus = false) {
64-
if (gui.BuildTextInput(DataUtils.FormatAmount(amount.Value, amount.Unit), out string newText, null, Icon.None, true, padding, setInitialFocus: setInitialFocus)
63+
public static bool BuildFloatInput(this ImGui gui, DisplayAmount amount, TextBoxDisplayStyle displayStyle, bool setInitialFocus = false) {
64+
if (gui.BuildTextInput(DataUtils.FormatAmount(amount.Value, amount.Unit), out string newText, null, displayStyle, true, setInitialFocus)
6565
&& DataUtils.TryParseAmount(newText, out float newValue, amount.Unit)) {
6666
amount.Value = newValue;
6767
return true;
@@ -270,11 +270,8 @@ public static GoodsWithAmountEvent BuildFactorioObjectWithEditableAmount(this Im
270270
group.SetWidth(3f);
271271
GoodsWithAmountEvent evt = (GoodsWithAmountEvent)gui.BuildFactorioObjectButton(obj, 3f, MilestoneDisplay.Contained, color, useScale, tooltipOptions);
272272

273-
if (gui.BuildTextInput(DataUtils.FormatAmount(amount.Value, amount.Unit), out string newText, null, Icon.None, true, default, RectAlignment.Middle, SchemeColorGroup.Secondary)) {
274-
if (DataUtils.TryParseAmount(newText, out float newAmount, amount.Unit)) {
275-
amount.Value = newAmount;
276-
return GoodsWithAmountEvent.TextEditing;
277-
}
273+
if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.FactorioObjectInput)) {
274+
return GoodsWithAmountEvent.TextEditing;
278275
}
279276

280277
if (allowScroll && gui.action == ImGuiAction.MouseScroll && gui.ConsumeEvent(gui.lastRect)) {

Yafc/Windows/FilesystemScreen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private void BuildElement(ImGui gui, (EntryType type, string location) element,
147147
using (gui.EnterGroup(default, RectAllocator.LeftRow)) {
148148
gui.BuildIcon(icon);
149149
if (element.type == EntryType.CreateDirectory) {
150-
if (gui.BuildTextInput("", out string dirName, elementText, Icon.None, true, new Padding(0.2f, 0.2f))) {
150+
if (gui.BuildTextInput("", out string dirName, elementText, TextBoxDisplayStyle.DefaultTextInput with { Padding = new Padding(0.2f) }, true)) {
151151
if (!string.IsNullOrWhiteSpace(dirName) && dirName.IndexOfAny(Path.GetInvalidFileNameChars()) == -1) {
152152
string dirPath = Path.Combine(location, dirName);
153153
_ = Directory.CreateDirectory(dirPath);

Yafc/Windows/PreferencesScreen.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override void Build(ImGui gui) {
4141
using (gui.EnterRowWithHelpIcon("0 for off, 100% for old default")) {
4242
gui.BuildText("Pollution cost modifier", topOffset: 0.5f);
4343
DisplayAmount amount = new(settings.PollutionCostModifier, UnitOfMeasure.Percent);
44-
if (gui.BuildFloatInput(amount, new Padding(0.5f)) && amount.Value >= 0) {
44+
if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.DefaultTextInput) && amount.Value >= 0) {
4545
settings.RecordUndo().PollutionCostModifier = amount.Value;
4646
gui.Rebuild();
4747
}
@@ -50,7 +50,7 @@ public override void Build(ImGui gui) {
5050
using (gui.EnterRowWithHelpIcon("Some mod icons have little or no transparency, hiding the background color. This setting reduces the size of icons that could hide link information.")) {
5151
gui.BuildText("Display scale for linkable icons", topOffset: 0.5f);
5252
DisplayAmount amount = new(prefs.iconScale, UnitOfMeasure.Percent);
53-
if (gui.BuildFloatInput(amount, new Padding(0.5f)) && amount.Value > 0 && amount.Value <= 1) {
53+
if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.DefaultTextInput) && amount.Value > 0 && amount.Value <= 1) {
5454
prefs.RecordUndo().iconScale = amount.Value;
5555
gui.Rebuild();
5656
}
@@ -153,7 +153,7 @@ private void BuildUnitPerTime(ImGui gui, bool fluid, ProjectPreferences preferen
153153
}
154154
}
155155
gui.BuildText("per second");
156-
_ = gui.BuildFloatInput(unit, new Padding(.5f));
156+
_ = gui.BuildFloatInput(unit, TextBoxDisplayStyle.DefaultTextInput);
157157
}
158158
gui.AllocateSpacing(1f);
159159

Yafc/Workspace/ProductionTable/ModuleFillerParametersScreen.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public override void Build(ImGui gui) {
123123
using (gui.EnterRow()) {
124124
gui.BuildText("Beacons per building: ");
125125
DisplayAmount amount = modules.beaconsPerBuilding;
126-
if (gui.BuildFloatInput(amount, new Padding(0.5f, 0f)) && (int)amount.Value > 0) {
126+
if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.ModuleParametersTextInput) && (int)amount.Value > 0) {
127127
modules.RecordUndo().beaconsPerBuilding = (int)amount.Value;
128128
}
129129
}
@@ -190,21 +190,21 @@ public override void Build(ImGui gui) {
190190
using (gui.EnterRow()) {
191191
gui.BuildText("Mining productivity bonus (project-wide setting): ");
192192
DisplayAmount amount = new(Project.current.settings.miningProductivity, UnitOfMeasure.Percent);
193-
if (gui.BuildFloatInput(amount, new Padding(.5f, 0)) && amount.Value >= 0) {
193+
if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.ModuleParametersTextInput) && amount.Value >= 0) {
194194
Project.current.settings.RecordUndo().miningProductivity = amount.Value;
195195
}
196196
}
197197
using (gui.EnterRow()) {
198198
gui.BuildText("Research speed bonus (project-wide setting): ");
199199
DisplayAmount amount = new(Project.current.settings.researchSpeedBonus, UnitOfMeasure.Percent);
200-
if (gui.BuildFloatInput(amount, new Padding(.5f, 0)) && amount.Value >= 0) {
200+
if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.ModuleParametersTextInput) && amount.Value >= 0) {
201201
Project.current.settings.RecordUndo().researchSpeedBonus = amount.Value;
202202
}
203203
}
204204
using (gui.EnterRow()) {
205205
gui.BuildText("Research productivity bonus (project-wide setting): ");
206206
DisplayAmount amount = new(Project.current.settings.researchProductivity, UnitOfMeasure.Percent);
207-
if (gui.BuildFloatInput(amount, new Padding(.5f, 0)) && amount.Value >= 0) {
207+
if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.ModuleParametersTextInput) && amount.Value >= 0) {
208208
Project.current.settings.RecordUndo().researchProductivity = amount.Value;
209209
}
210210
}

Yafc/Workspace/ProductionTable/ProductionTableView.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,9 @@ public override void BuildElement(ImGui gui, RecipeRow recipe) {
294294
}
295295

296296
if (recipe.builtBuildings != null) {
297-
if (gui.BuildTextInput(DataUtils.FormatAmount(Convert.ToSingle(recipe.builtBuildings), UnitOfMeasure.None), out string newText, null, Icon.None, true, default, RectAlignment.Middle, SchemeColorGroup.Grey)) {
298-
if (DataUtils.TryParseAmount(newText, out float newAmount, UnitOfMeasure.None)) {
299-
recipe.RecordUndo().builtBuildings = Convert.ToInt32(newAmount);
300-
}
297+
DisplayAmount amount = recipe.builtBuildings.Value;
298+
if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.FactorioObjectInput with { ColorGroup = SchemeColorGroup.Grey }) && amount.Value >= 0) {
299+
recipe.RecordUndo().builtBuildings = (int)amount.Value;
301300
}
302301
}
303302
}

0 commit comments

Comments
 (0)