Skip to content

Commit

Permalink
feature: Fix popup of 'Target technology for cost analysis' preference (
Browse files Browse the repository at this point in the history
shpaass#62)

The header is too long for the default width of 20f. Make the width
configurable (as it is used in 3 places) and increase the width of this
particular popup.

Also fixed some typos in related code.

Fixes shpaass#27
  • Loading branch information
shpaass authored Feb 27, 2024
2 parents 0f1a501 + 6c00690 commit d930ab7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
13 changes: 8 additions & 5 deletions YAFC/Widgets/ImmediateWidgets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static bool BuildInlineObjectList<T>(this ImGui gui, IEnumerable<T> list,
return selected != null;
}

public static void BuildInlineObejctListAndButton<T>(this ImGui gui, ICollection<T> list, IComparer<T> ordering, Action<T> select, string header, int count = 6, bool multiple = false, Predicate<T> checkmark = null, bool allowNone = false, Func<T, string> extra = null) where T : FactorioObject {
public static void BuildInlineObjectListAndButton<T>(this ImGui gui, ICollection<T> list, IComparer<T> ordering, Action<T> select, string header, int count = 6, bool multiple = false, Predicate<T> checkmark = null, bool allowNone = false, Func<T, string> extra = null) where T : FactorioObject {
using (gui.EnterGroup(default, RectAllocator.Stretch)) {
if (gui.BuildInlineObjectList(list, ordering, header, out var selected, count, checkmark, extra)) {
select(selected);
Expand All @@ -144,13 +144,13 @@ public static bool BuildFactorioObjectWithAmount(this ImGui gui, FactorioObject
if (goods != null) {
gui.BuildText(DataUtils.FormatAmount(amount, unit), Font.text, false, RectAlignment.Middle);
if (InputSystem.Instance.control && gui.BuildButton(gui.lastRect, SchemeColor.None, SchemeColor.Grey) == ButtonEvent.MouseOver)
ShowPrecisionValueTootlip(gui, amount, unit, goods);
ShowPrecisionValueTooltip(gui, amount, unit, goods);
}
return clicked;
}
}

public static void ShowPrecisionValueTootlip(ImGui gui, float amount, UnitOfMeasure unit, FactorioObject goods) {
public static void ShowPrecisionValueTooltip(ImGui gui, float amount, UnitOfMeasure unit, FactorioObject goods) {
string text;
switch (unit) {
case UnitOfMeasure.PerSecond:
Expand All @@ -173,8 +173,11 @@ public static void ShowPrecisionValueTootlip(ImGui gui, float amount, UnitOfMeas
}, 10f);
}

public static void BuildObjectSelectDropDown<T>(this ImGui gui, ICollection<T> list, IComparer<T> ordering, Action<T> select, string header, int count = 6, bool multiple = false, Predicate<T> checkmark = null, bool allowNone = false, Func<T, string> extra = null) where T : FactorioObject {
gui.ShowDropDown(imGui => imGui.BuildInlineObejctListAndButton(list, ordering, select, header, count, multiple, checkmark, allowNone, extra));
/// <summary>Shows a dropdown containing the (partial) <paramref name="list"/> of elements, with an action for when an element is selected.</summary>
/// <param name="count">Maximum number of elements in the list. If there are more another popup can be opened by the user to show the full list.</param>
/// <param name="width">Width of the popup. Make sure the header text fits!</param>
public static void BuildObjectSelectDropDown<T>(this ImGui gui, ICollection<T> list, IComparer<T> ordering, Action<T> select, string header, float width = 20f, int count = 6, bool multiple = false, Predicate<T> checkmark = null, bool allowNone = false, Func<T, string> extra = null) where T : FactorioObject {
gui.ShowDropDown(imGui => imGui.BuildInlineObjectListAndButton(list, ordering, select, header, count, multiple, checkmark, allowNone, extra), width);
}

public static GoodsWithAmountEvent BuildFactorioObjectWithEditableAmount(this ImGui gui, FactorioObject obj, float amount, UnitOfMeasure unit, out float newAmount, SchemeColor color = SchemeColor.None) {
Expand Down
2 changes: 1 addition & 1 deletion YAFC/Windows/NeverEnoughItemsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void DrawIngredients(ImGui gui, Recipe recipe) {
foreach (var ingredient in recipe.ingredients)
if (gui.BuildFactorioObjectWithAmount(ingredient.goods, ingredient.amount, UnitOfMeasure.None)) {
if (ingredient.variants != null)
gui.ShowDropDown(imGui => imGui.BuildInlineObejctListAndButton<Goods>(ingredient.variants, DataUtils.DefaultOrdering, SetItem, "Accepted fluid variants"));
gui.ShowDropDown(imGui => imGui.BuildInlineObjectListAndButton<Goods>(ingredient.variants, DataUtils.DefaultOrdering, SetItem, "Accepted fluid variants"));
else
changing = ingredient.goods;
}
Expand Down
9 changes: 6 additions & 3 deletions YAFC/Windows/PreferencesScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void Build(ImGui gui) {
ChoiceObject(gui, "Target technology for cost analysis: ", Database.technologies.all, prefs.targetTechnology, x => {
prefs.RecordUndo().targetTechnology = x;
gui.Rebuild();
});
}, width: 25f);

if (gui.BuildButton("Done"))
Close();
Expand All @@ -71,11 +71,14 @@ public override void Build(ImGui gui) {
Project.current.RecalculateDisplayPages();
}

private void ChoiceObject<T>(ImGui gui, string text, T[] list, T current, Action<T> select) where T : FactorioObject {
/// <summary>Add a GUI element that opens a popup to allow the user to choose from the <paramref name="list"/>, which triggers <paramref name="select"/>.</summary>
/// <param name="text">Label to show.</param>
/// <param name="width">Width of the popup. Make sure it is wide enough to fit text!</param>
private void ChoiceObject<T>(ImGui gui, string text, T[] list, T current, Action<T> select, float width = 20f) where T : FactorioObject {
using (gui.EnterRow()) {
gui.BuildText(text, topOffset: 0.5f);
if (gui.BuildFactorioObjectButtonWithText(current))
gui.BuildObjectSelectDropDown(list, DataUtils.DefaultOrdering, select, text);
gui.BuildObjectSelectDropDown(list, DataUtils.DefaultOrdering, select, text, width: width);
}
}

Expand Down
2 changes: 1 addition & 1 deletion YAFC/Workspace/ProductionSummary/ProductionSummaryView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public override void BuildElement(ImGui gui, ProductionSummaryEntry data) {
if (evt == ButtonEvent.Click)
view.AddOrRemoveColumn(goods);
else if (evt == ButtonEvent.MouseOver)
ImmediateWidgets.ShowPrecisionValueTootlip(gui, amount, goods.flowUnitOfMeasure, goods);
ImmediateWidgets.ShowPrecisionValueTooltip(gui, amount, goods.flowUnitOfMeasure, goods);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions YAFC/Workspace/ProductionTable/ProductionTableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@ private void BuildSolarPanelAccumulatorView(ImGui gui, RecipeRow recipe) {

private void ShowAccumulatorDropdown(ImGui gui, RecipeRow recipe, Entity accumulator) {
gui.ShowDropDown(imGui => {
imGui.BuildInlineObejctListAndButton<EntityAccumulator>(Database.allAccumulators, DataUtils.DefaultOrdering,
imGui.BuildInlineObjectListAndButton<EntityAccumulator>(Database.allAccumulators, DataUtils.DefaultOrdering,
accum => recipe.RecordUndo().ChangeVariant(accumulator, accum), "Select accumulator",
extra: x => DataUtils.FormatAmount(x.accumulatorCapacity, UnitOfMeasure.Megajoule));
});
}

private void ShowEntityDropPown(ImGui imgui, RecipeRow recipe) {
imgui.ShowDropDown(gui => {
gui.BuildInlineObejctListAndButton(recipe.recipe.crafters, DataUtils.FavouriteCrafter, sel => {
gui.BuildInlineObjectListAndButton(recipe.recipe.crafters, DataUtils.FavouriteCrafter, sel => {
if (recipe.entity == sel)
return;
recipe.RecordUndo().entity = sel;
Expand Down Expand Up @@ -464,7 +464,7 @@ private void ShowModuleDropDown(ImGui gui, RecipeRow recipe) {
}
if (recipe.entity?.moduleSlots > 0)
dropGui.BuildInlineObejctListAndButton(modules, DataUtils.FavouriteModule, recipe.SetFixedModule, "Select fixed module");
dropGui.BuildInlineObjectListAndButton(modules, DataUtils.FavouriteModule, recipe.SetFixedModule, "Select fixed module");
if (moduleTemplateList.data.Count > 0) {
dropGui.BuildText("Use module template:", wrap: true, font: Font.subheader);
Expand Down Expand Up @@ -606,7 +606,7 @@ void DropDownContent(ImGui gui) {
gui.BuildText("This entity has no known fuels");
else if (recipe.entity.energy.fuels.Length > 1 || recipe.entity.energy.fuels[0] != recipe.fuel) {
BuildFavourites(gui, recipe.fuel, "Add fuel to favourites");
gui.BuildInlineObejctListAndButton(recipe.entity.energy.fuels, DataUtils.FavouriteFuel, selectFuel, "Select fuel", extra: fuelDisplayFunc);
gui.BuildInlineObjectListAndButton(recipe.entity.energy.fuels, DataUtils.FavouriteFuel, selectFuel, "Select fuel", extra: fuelDisplayFunc);
}
}

Expand Down Expand Up @@ -644,7 +644,7 @@ void DropDownContent(ImGui gui) {
}

if (type != ProductDropdownType.Product && goods != null && allProduction.Length > 0) {
gui.BuildInlineObejctListAndButton(allProduction, comparer, addRecipe, "Add production recipe", 6, true, recipeExists);
gui.BuildInlineObjectListAndButton(allProduction, comparer, addRecipe, "Add production recipe", 6, true, recipeExists);
if (link == null) {
var iconRect = new Rect(gui.lastRect.Right - 2f, gui.lastRect.Top, 2f, 2f);
gui.DrawIcon(iconRect.Expand(-0.2f), Icon.OpenNew, gui.textColor);
Expand All @@ -657,10 +657,10 @@ void DropDownContent(ImGui gui) {
}

if (type != ProductDropdownType.Fuel && goods != null && type != ProductDropdownType.Ingredient && goods.usages.Length > 0)
gui.BuildInlineObejctListAndButton(goods.usages, DataUtils.DefaultRecipeOrdering, addRecipe, "Add consumption recipe", type == ProductDropdownType.Product ? 6 : 3, true, recipeExists);
gui.BuildInlineObjectListAndButton(goods.usages, DataUtils.DefaultRecipeOrdering, addRecipe, "Add consumption recipe", type == ProductDropdownType.Product ? 6 : 3, true, recipeExists);

if (type == ProductDropdownType.Product && goods != null && allProduction.Length > 0)
gui.BuildInlineObejctListAndButton(allProduction, comparer, addRecipe, "Add production recipe", 1, true, recipeExists);
gui.BuildInlineObjectListAndButton(allProduction, comparer, addRecipe, "Add production recipe", 1, true, recipeExists);

if (link != null && gui.BuildCheckBox("Allow overproduction", link.algorithm == LinkAlgorithm.AllowOverProduction, out var newValue))
link.RecordUndo().algorithm = newValue ? LinkAlgorithm.AllowOverProduction : LinkAlgorithm.Match;
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Version: 0.6.2
Date: soon(tm)
Changes:
- Fix text alignment of about screen
- Fix width of 'Target technology for cost analysis' preference popup
----------------------------------------------------------------------------------------------------------------------
Version: 0.6.1
Date: Feb 2024
Expand Down

0 comments on commit d930ab7

Please sign in to comment.