Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion YAFC/Widgets/ImmediateWidgets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ public static void BuildInlineObjectListAndButton<T>(this ImGui gui, ICollection
}

if (list.Count > count && gui.BuildButton("See full list") && gui.CloseDropdown()) {
SelectObjectPanel.Select(list, header, select, ordering, allowNone);
if (multiple) {
SelectMultiObjectPanel.Select(list, header, select, ordering, allowNone, checkMark);
}
else {
SelectSingleObjectPanel.Select(list, header, select, ordering, allowNone);
}
}

if (multiple && list.Count > 1) {
Expand Down
5 changes: 5 additions & 0 deletions YAFC/Widgets/PseudoScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ public virtual bool KeyDown(SDL.SDL_Keysym key) {
if (key.scancode == SDL.SDL_Scancode.SDL_SCANCODE_ESCAPE) {
Close(false);
}
if (key.scancode == SDL.SDL_Scancode.SDL_SCANCODE_RETURN || key.scancode == SDL.SDL_Scancode.SDL_SCANCODE_RETURN2 || key.scancode == SDL.SDL_Scancode.SDL_SCANCODE_KP_ENTER) {
ReturnPressed();
}

return true;
}

protected virtual void ReturnPressed() { }

public virtual bool TextInput(string input) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion YAFC/Windows/DependencyExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public override void Build(ImGui gui) {
using (gui.EnterRow()) {
gui.BuildText("Currently inspecting:", Font.subheader);
if (gui.BuildFactorioObjectButtonWithText(current)) {
SelectObjectPanel.Select(Database.objects.all, "Select something", Change);
SelectSingleObjectPanel.Select(Database.objects.all, "Select something", Change);
}

gui.BuildText("(Click to change)", color: SchemeColor.BackgroundTextFaint);
Expand Down
4 changes: 2 additions & 2 deletions YAFC/Windows/MainScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public void BuildSubHeader(ImGui gui, string text) {
}

private void ShowNeie() {
SelectObjectPanel.Select(Database.goods.all, "Open NEIE", NeverEnoughItemsPanel.Show);
SelectSingleObjectPanel.Select(Database.goods.all, "Open NEIE", NeverEnoughItemsPanel.Show);
}

private void SetSearch(SearchQuery searchQuery) {
Expand Down Expand Up @@ -420,7 +420,7 @@ private void SettingsDropdown(ImGui gui) {
}

if (gui.BuildContextMenuButton("Dependency Explorer") && gui.CloseDropdown()) {
SelectObjectPanel.Select(Database.objects.all, "Open Dependency Explorer", DependencyExplorer.Show);
SelectSingleObjectPanel.Select(Database.objects.all, "Open Dependency Explorer", DependencyExplorer.Show);
}

BuildSubHeader(gui, "Extra");
Expand Down
9 changes: 2 additions & 7 deletions YAFC/Windows/MessageBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
namespace YAFC {
public class MessageBox : PseudoScreen<bool> {
public MessageBox() : base(30f) { }
private static readonly MessageBox Instance = new MessageBox();

private string title, message, yes, no;

public static void Show(Action<bool, bool> result, string title, string message, string yes, string no) {
Instance.title = title;
Instance.complete = result;
Instance.message = message;
Instance.yes = yes;
Instance.no = no;
_ = MainScreen.Instance.ShowPseudoScreen(Instance);
MessageBox instance = new MessageBox { title = title, complete = result, message = message, yes = yes, no = no };
_ = MainScreen.Instance.ShowPseudoScreen(instance);
}

public static void Show(string title, string message, string yes) {
Expand Down
5 changes: 3 additions & 2 deletions YAFC/Windows/MilestonesEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using System.Linq;
using System.Numerics;
using YAFC.Model;
using YAFC.UI;

Expand Down Expand Up @@ -53,7 +54,7 @@ public override void Build(ImGui gui) {
milestoneList.RebuildContents();
}
if (gui.BuildButton("Add milestone")) {
SelectObjectPanel.Select(Database.objects.all, "Add new milestone", AddMilestone);
SelectMultiObjectPanel.Select(Database.objects.all.Except(Project.current.settings.milestones), "Add new milestone", AddMilestone);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion YAFC/Windows/NeverEnoughItemsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public override void Build(ImGui gui) {
}

if (gui.BuildFactorioObjectButton(gui.lastRect, current, SchemeColor.Grey)) {
SelectObjectPanel.Select(Database.goods.all, "Select item", SetItem);
SelectSingleObjectPanel.Select(Database.goods.all, "Select item", SetItem);
}

using (var split = gui.EnterHorizontalSplit(2)) {
Expand Down
2 changes: 1 addition & 1 deletion YAFC/Windows/ProjectPageSettingsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ProjectPageSettingsPanel : PseudoScreen {
public static void Build(ImGui gui, ref string name, FactorioObject icon, Action<FactorioObject> setIcon) {
_ = gui.BuildTextInput(name, out name, "Input name");
if (gui.BuildFactorioObjectButton(icon, 4f, MilestoneDisplay.None, SchemeColor.Grey)) {
SelectObjectPanel.Select(Database.objects.all, "Select icon", setIcon);
SelectSingleObjectPanel.Select(Database.objects.all, "Select icon", setIcon);
}

if (icon == null && gui.isBuilding) {
Expand Down
64 changes: 64 additions & 0 deletions YAFC/Windows/SelectMultiObjectPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using YAFC.Model;
using YAFC.UI;

namespace YAFC {
public class SelectMultiObjectPanel : SelectObjectPanel<IEnumerable<FactorioObject>> {
private static readonly SelectMultiObjectPanel Instance = new SelectMultiObjectPanel();
private readonly HashSet<FactorioObject> results = new HashSet<FactorioObject>();
private bool allowAutoClose;
private Predicate<FactorioObject> checkMark;

public SelectMultiObjectPanel() : base() { }

public static void Select<T>(IEnumerable<T> list, string header, Action<T> select, bool allowNone = false, Predicate<T> checkMark = null) where T : FactorioObject {
Select(list, header, select, DataUtils.DefaultOrdering, allowNone, checkMark);
}

public static void Select<T>(IEnumerable<T> list, string header, Action<T> select, IComparer<T> ordering, bool allowNone = false, Predicate<T> checkMark = null) where T : FactorioObject {
Instance.allowAutoClose = true;
Instance.results.Clear();
Instance.checkMark = (o) => checkMark?.Invoke((T)o) ?? false; // This is messy, but pushing T all the way around the call stack and type tree was messier.
Instance.Select(list, header, select, ordering, (xs, selectItem) => {
foreach (var x in xs ?? Enumerable.Empty<T>()) {
selectItem(x);
}
}, allowNone);
}

protected override void NonNullElementDrawer(ImGui gui, FactorioObject element, int index) {
bool click = gui.BuildFactorioObjectButton(element, display: MilestoneDisplay.Contained, bgColor: results.Contains(element) ? SchemeColor.Primary : SchemeColor.None, extendHeader: extendHeader);

if (checkMark(element)) {
gui.DrawIcon(Rect.SideRect(gui.lastRect.TopLeft + new Vector2(1, 0), gui.lastRect.BottomRight - new Vector2(0, 1)), Icon.Check, SchemeColor.Green);
}

if (click) {
if (!results.Add(element)) {
results.Remove(element);
}
if (!InputSystem.Instance.control && allowAutoClose) {
CloseWithResult(results);
}
allowAutoClose = false;
}
}

public override void Build(ImGui gui) {
base.Build(gui);
using (gui.EnterGroup(default, RectAllocator.Center)) {
if (gui.BuildButton("OK")) {
CloseWithResult(results);
}
gui.BuildText("Hint: ctrl+click to select multiple", color: SchemeColor.BackgroundTextFaint);
}
}

protected override void ReturnPressed() {
CloseWithResult(results);
}
}
}
62 changes: 31 additions & 31 deletions YAFC/Windows/SelectObjectPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,64 @@
using YAFC.UI;

namespace YAFC {
public class SelectObjectPanel : PseudoScreen<FactorioObject> {
private static readonly SelectObjectPanel Instance = new SelectObjectPanel();
private readonly SearchableList<FactorioObject> list;
private string header;
private Rect searchBox;
private bool extendHeader;
public SelectObjectPanel() : base(40f) {
list = new SearchableList<FactorioObject>(30, new Vector2(2.5f, 2.5f), ElementDrawer, ElementFilter);
}
public abstract class SelectObjectPanel<T> : PseudoScreen<T> {
protected readonly SearchableList<FactorioObject> list;
protected string header;
protected Rect searchBox;
protected bool extendHeader;

private bool ElementFilter(FactorioObject data, SearchQuery query) {
return data.Match(query);
protected SelectObjectPanel() : base(40f) {
list = new SearchableList<FactorioObject>(30, new Vector2(2.5f, 2.5f), ElementDrawer, ElementFilter);
}

public static void Select<T>(IEnumerable<T> list, string header, Action<T> select, IComparer<T> ordering, bool allowNone) where T : FactorioObject {
_ = MainScreen.Instance.ShowPseudoScreen(Instance);
Instance.extendHeader = typeof(T) == typeof(FactorioObject);
List<T> data = new List<T>(list);
protected void Select<U>(IEnumerable<U> list, string header, Action<U> select, IComparer<U> ordering, Action<T, Action<FactorioObject>> process, bool allowNone) where U : FactorioObject {
_ = MainScreen.Instance.ShowPseudoScreen(this);
extendHeader = typeof(U) == typeof(FactorioObject);
List<U> data = new List<U>(list);
data.Sort(ordering);
if (allowNone) {
data.Insert(0, null);
}

Instance.list.filter = default;
Instance.list.data = data;
Instance.header = header;
Instance.Rebuild();
Instance.complete = (selected, x) => {
if (x is T t) {
if (ordering is DataUtils.FavoritesComparer<T> favoritesComparer) {
favoritesComparer.AddToFavorite(t);
this.list.filter = default;
this.list.data = data;
this.header = header;
Rebuild();
complete = (selected, x) => process(x, x => {
if (x is U u) {
if (ordering is DataUtils.FavoritesComparer<U> favoritesComparer) {
favoritesComparer.AddToFavorite(u);
}

select(t);
select(u);
}
else if (allowNone && selected) {
select(null);
}
};
});
}

public static void Select<T>(IEnumerable<T> list, string header, Action<T> select, bool allowNone = false) where T : FactorioObject {
Select(list, header, select, DataUtils.DefaultOrdering, allowNone);
protected void Select<U>(IEnumerable<U> list, string header, Action<U> select, Action<T, Action<FactorioObject>> process, bool allowNone = false) where U : FactorioObject {
Select(list, header, select, DataUtils.DefaultOrdering, process, allowNone);
}

private void ElementDrawer(ImGui gui, FactorioObject element, int index) {
if (element == null) {
if (gui.BuildRedButton(Icon.Close)) {
CloseWithResult(null);
CloseWithResult(default);
}
}
else {
if (gui.BuildFactorioObjectButton(element, display: MilestoneDisplay.Contained, extendHeader: extendHeader)) {
CloseWithResult(element);
}
NonNullElementDrawer(gui, element, index);
}
}

protected abstract void NonNullElementDrawer(ImGui gui, FactorioObject element, int index);

private bool ElementFilter(FactorioObject data, SearchQuery query) {
return data.Match(query);
}

public override void Build(ImGui gui) {
BuildHeader(gui, header);
if (gui.BuildSearchBox(list.filter, out var newFilter, "Start typing for search")) {
Expand Down
25 changes: 25 additions & 0 deletions YAFC/Windows/SelectSingleObjectPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using YAFC.Model;
using YAFC.UI;

namespace YAFC {
public class SelectSingleObjectPanel : SelectObjectPanel<FactorioObject> {
private static readonly SelectSingleObjectPanel Instance = new SelectSingleObjectPanel();
public SelectSingleObjectPanel() : base() { }

public static void Select<T>(IEnumerable<T> list, string header, Action<T> select, bool allowNone = false) where T : FactorioObject {
Select(list, header, select, DataUtils.DefaultOrdering, allowNone);
}

public static void Select<T>(IEnumerable<T> list, string header, Action<T> select, IComparer<T> ordering, bool allowNone = false) where T : FactorioObject {
Instance.Select(list, header, select, ordering, (x, selectItem) => selectItem(x), allowNone);
}

protected override void NonNullElementDrawer(ImGui gui, FactorioObject element, int index) {
if (gui.BuildFactorioObjectButton(element, display: MilestoneDisplay.Contained, extendHeader: extendHeader)) {
CloseWithResult(element);
}
}
}
}
2 changes: 1 addition & 1 deletion YAFC/Workspace/AutoPlannerView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void Page1(ImGui gui, ref bool valid) {
}
grid.Next();
if (gui.BuildButton(Icon.Plus, SchemeColor.Primary, SchemeColor.PrimaryAlt, size: 2.5f)) {
SelectObjectPanel.Select(Database.goods.all, "New production goal", x => {
SelectSingleObjectPanel.Select(Database.goods.all, "New production goal", x => {
goal.Add(new AutoPlannerGoal { amount = 1f, item = x });
gui.Rebuild();
});
Expand Down
6 changes: 3 additions & 3 deletions YAFC/Workspace/ProductionTable/ModuleCustomizationScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override void Build(ImGui gui) {
if (template != null) {
using (gui.EnterRow()) {
if (gui.BuildFactorioObjectButton(template.icon)) {
SelectObjectPanel.Select(Database.objects.all, "Select icon", x => {
SelectSingleObjectPanel.Select(Database.objects.all, "Select icon", x => {
template.RecordUndo().icon = x;
Rebuild();
});
Expand All @@ -53,7 +53,7 @@ public override void Build(ImGui gui) {
}
grid.Next();
if (gui.BuildButton(Icon.Plus, SchemeColor.Primary, SchemeColor.PrimaryAlt, size: 1.5f)) {
SelectObjectPanel.Select(Database.allCrafters.Where(x => x.allowedEffects != AllowedEffects.None && !template.filterEntities.Contains(x)), "Add module template filter", sel => {
SelectSingleObjectPanel.Select(Database.allCrafters.Where(x => x.allowedEffects != AllowedEffects.None && !template.filterEntities.Contains(x)), "Add module template filter", sel => {
template.RecordUndo().filterEntities.Add(sel);
gui.Rebuild();
});
Expand Down Expand Up @@ -162,7 +162,7 @@ private void DrawRecipeModules(ImGui gui, EntityBeacon beacon, ref ModuleEffects
grid.Next();
var evt = gui.BuildFactorioObjectWithEditableAmount(module.module, module.fixedCount, UnitOfMeasure.None, out float newAmount);
if (evt == GoodsWithAmountEvent.ButtonClick) {
SelectObjectPanel.Select(GetModules(beacon), "Select module", sel => {
SelectSingleObjectPanel.Select(GetModules(beacon), "Select module", sel => {
if (sel == null) {
_ = modules.RecordUndo().list.Remove(module);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public override void Build(ImGui gui) {
gui.BuildText("Filler module:", Font.subheader);
gui.BuildText("Use this module when aufofill doesn't add anything (for example when productivity modules doesn't fit)", wrap: true);
if (gui.BuildFactorioObjectButtonWithText(modules.fillerModule)) {
SelectObjectPanel.Select(Database.allModules, "Select filler module", select => { modules.RecordUndo().fillerModule = select; }, true);
SelectSingleObjectPanel.Select(Database.allModules, "Select filler module", select => { modules.RecordUndo().fillerModule = select; }, true);
}

gui.AllocateSpacing();
gui.BuildText("Beacons & beacon modules:", Font.subheader);
if (gui.BuildFactorioObjectButtonWithText(modules.beacon)) {
SelectObjectPanel.Select(Database.allBeacons, "Select beacon", select => {
SelectSingleObjectPanel.Select(Database.allBeacons, "Select beacon", select => {
_ = modules.RecordUndo();
modules.beacon = select;
if (modules.beaconModule != null && (modules.beacon == null || !modules.beacon.CanAcceptModule(modules.beaconModule.module))) {
Expand All @@ -64,7 +64,7 @@ public override void Build(ImGui gui) {
}

if (gui.BuildFactorioObjectButtonWithText(modules.beaconModule)) {
SelectObjectPanel.Select(Database.allModules.Where(x => modules.beacon?.CanAcceptModule(x.module) ?? false), "Select module for beacon", select => { modules.RecordUndo().beaconModule = select; }, true);
SelectSingleObjectPanel.Select(Database.allModules.Where(x => modules.beacon?.CanAcceptModule(x.module) ?? false), "Select module for beacon", select => { modules.RecordUndo().beaconModule = select; }, true);
}

using (gui.EnterRow()) {
Expand Down
Loading