Skip to content

Commit

Permalink
feat(#297): Focus the built and fixed count textboxes when opening them.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaleStan committed Oct 25, 2024
1 parent f0593e8 commit 9666a5f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
29 changes: 29 additions & 0 deletions Yafc.Model/Model/ProductionTableContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,35 @@ public IEnumerable<RecipeRowProduct> Products {
}
}

private SetKeyboardFocus _focusBuiltCount, _focusFixedCount;

/// <summary>
/// Returns <see cref="SetKeyboardFocus.Always"/> exactly once after each call to <see cref="FocusBuiltCountOnNextDraw"/>, to focus the newly created edit box on that draw cycle.
/// </summary>
public SetKeyboardFocus ShouldFocusBuiltCountThisTime() {
SetKeyboardFocus result = _focusBuiltCount;
_focusBuiltCount = SetKeyboardFocus.No;
return result;
}
/// <summary>
/// Call when preparing to add a built building count edit box, so the new box will be focused as part of the next draw loop.
/// </summary>
public void FocusBuiltCountOnNextDraw() => _focusBuiltCount = SetKeyboardFocus.Always;

/// <summary>
/// Returns <see cref="SetKeyboardFocus.Always"/> exactly once after each call to <see cref="FocusFixedCountOnNextDraw"/>, to focus the newly created edit box on that draw cycle.
/// </summary>
public SetKeyboardFocus ShouldFocusFixedCountThisTime() {
SetKeyboardFocus result = _focusFixedCount;
_focusFixedCount = SetKeyboardFocus.No;
return result;
}
/// <summary>
/// Call when preparing to add or move a fixed count edit box (building, fuel, ingredient, or product), so the new box will be focused as part of the next draw loop.
/// </summary>
public void FocusFixedCountOnNextDraw() => _focusFixedCount = SetKeyboardFocus.Always;


// Computed variables
internal RecipeParameters parameters { get; set; } = RecipeParameters.Empty;
public double recipesPerSecond { get; internal set; }
Expand Down
4 changes: 2 additions & 2 deletions Yafc/Widgets/ImmediateWidgets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,13 @@ public static void BuildObjectSelectDropDownWithNone<T>(this ImGui gui, ICollect
/// <param name="allowScroll">If <see langword="true"/>, the default, the user can adjust the value by using the scroll wheel while hovering over the editable text.
/// If <see langword="false"/>, the scroll wheel will be ignored when hovering.</param>
public static GoodsWithAmountEvent BuildFactorioObjectWithEditableAmount(this ImGui gui, FactorioObject? obj, DisplayAmount amount, ButtonDisplayStyle buttonDisplayStyle,
bool allowScroll = true, ObjectTooltipOptions tooltipOptions = default) {
bool allowScroll = true, ObjectTooltipOptions tooltipOptions = default, SetKeyboardFocus setKeyboardFocus = SetKeyboardFocus.No) {

using var group = gui.EnterGroup(default, RectAllocator.Stretch, spacing: 0f);
group.SetWidth(3f);
GoodsWithAmountEvent evt = (GoodsWithAmountEvent)gui.BuildFactorioObjectButton(obj, buttonDisplayStyle, tooltipOptions);

if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.FactorioObjectInput)) {
if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.FactorioObjectInput, setKeyboardFocus)) {
return GoodsWithAmountEvent.TextEditing;
}

Expand Down
14 changes: 11 additions & 3 deletions Yafc/Workspace/ProductionTable/ProductionTableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ public override void BuildElement(ImGui gui, RecipeRow recipe) {
group.SetWidth(3f);
if (recipe.fixedBuildings > 0 && !recipe.fixedFuel && recipe.fixedIngredient == null && recipe.fixedProduct == null) {
DisplayAmount amount = recipe.fixedBuildings;
GoodsWithAmountEvent evt = gui.BuildFactorioObjectWithEditableAmount(recipe.entity, amount, ButtonDisplayStyle.ProductionTableUnscaled);
GoodsWithAmountEvent evt = gui.BuildFactorioObjectWithEditableAmount(recipe.entity, amount, ButtonDisplayStyle.ProductionTableUnscaled,
setKeyboardFocus: recipe.ShouldFocusFixedCountThisTime());

if (evt == GoodsWithAmountEvent.TextEditing && amount.Value >= 0) {
recipe.RecordUndo().fixedBuildings = amount.Value;
Expand All @@ -313,7 +314,9 @@ public override void BuildElement(ImGui gui, RecipeRow recipe) {
if (recipe.builtBuildings != null) {
DisplayAmount amount = recipe.builtBuildings.Value;

if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.FactorioObjectInput with { ColorGroup = SchemeColorGroup.Grey }) && amount.Value >= 0) {
if (gui.BuildFloatInput(amount, TextBoxDisplayStyle.FactorioObjectInput with { ColorGroup = SchemeColorGroup.Grey }, recipe.ShouldFocusBuiltCountThisTime())
&& amount.Value >= 0) {

recipe.RecordUndo().builtBuildings = (int)amount.Value;
}
}
Expand Down Expand Up @@ -428,6 +431,7 @@ private static void ShowEntityDropdown(ImGui imgui, RecipeRow recipe) => imgui.S
recipe.fixedFuel = false;
recipe.fixedIngredient = null;
recipe.fixedProduct = null;
recipe.FocusFixedCountOnNextDraw();
}
}
Expand All @@ -447,6 +451,7 @@ private static void ShowEntityDropdown(ImGui imgui, RecipeRow recipe) => imgui.S
}
else if (gui.BuildButton("Set built building count") && gui.CloseDropdown()) {
recipe.RecordUndo().builtBuildings = Math.Max(0, Convert.ToInt32(Math.Ceiling(recipe.buildingCount)));
recipe.FocusBuiltCountOnNextDraw();
}
}
Expand Down Expand Up @@ -1016,6 +1021,7 @@ void dropDownContent(ImGui gui) {
default:
break;
}
recipe.FocusFixedCountOnNextDraw();
targetGui.Rebuild();
}
}
Expand Down Expand Up @@ -1151,7 +1157,9 @@ private void BuildGoodsIcon(ImGui gui, Goods? goods, ProductionLink? link, float
&& ((dropdownType == ProductDropdownType.Fuel && recipe.fixedFuel)
|| (dropdownType == ProductDropdownType.Ingredient && recipe.fixedIngredient == goods)
|| (dropdownType == ProductDropdownType.Product && recipe.fixedProduct == goods))) {
evt = gui.BuildFactorioObjectWithEditableAmount(goods, displayAmount, ButtonDisplayStyle.ProductionTableScaled(iconColor), tooltipOptions: tooltipOptions);

evt = gui.BuildFactorioObjectWithEditableAmount(goods, displayAmount, ButtonDisplayStyle.ProductionTableScaled(iconColor), tooltipOptions: tooltipOptions,
setKeyboardFocus: recipe.ShouldFocusFixedCountThisTime());
}
else {
evt = (GoodsWithAmountEvent)gui.BuildFactorioObjectWithAmount(goods, displayAmount, ButtonDisplayStyle.ProductionTableScaled(iconColor),
Expand Down
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
----------------------------------------------------------------------------------------------------------------------
Version: 2.x
Date:
Features:
- Focus the built count and fixed count edit boxes when first opening them.
Bugfixes:
- Recipes no longer have excessive question marks in their names
----------------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 9666a5f

Please sign in to comment.