Skip to content

Commit f87cf71

Browse files
committed
Merge branch 'master' into build-amount-v2
2 parents 5a0e37e + 7e7225e commit f87cf71

36 files changed

+1612
-755
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ obj/
66
desktop.ini
77
Build/
88
*DotSettings
9-
*DotSettings.user
9+
*DotSettings.user
10+
.vs

CommandLineToolExample/CommandLineToolExample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>netcoreapp6.0</TargetFramework>
55
<RootNamespace>YafcCommandLineToolExample</RootNamespace>
66
<LangVersion>8</LangVersion>
77
<OutputType>Exe</OutputType>

YAFC/Widgets/DataGrid.cs

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,68 @@
55

66
namespace YAFC.UI
77
{
8-
public class DataColumn<TData>
8+
public abstract class DataColumn<TData>
99
{
10-
public readonly Action<ImGui, TData> build;
11-
public readonly GuiBuilder menuBuilder;
12-
public readonly string header;
1310
public readonly float minWidth;
1411
public readonly float maxWidth;
1512
public readonly bool isFixedSize;
1613
public float width;
1714

18-
public DataColumn(string header, Action<ImGui, TData> build, GuiBuilder menuBuilder, float width, float minWidth = 0f, float maxWidth = 0f)
15+
public DataColumn(float width, float minWidth = 0f, float maxWidth = 0f)
1916
{
20-
this.build = build;
21-
this.menuBuilder = menuBuilder;
22-
23-
this.header = header;
2417
this.width = width;
2518
this.minWidth = minWidth == 0f ? width : minWidth;
2619
this.maxWidth = maxWidth == 0f ? width : maxWidth;
2720
isFixedSize = minWidth == maxWidth;
2821
}
22+
23+
public abstract void BuildHeader(ImGui gui);
24+
public abstract void BuildElement(ImGui gui, TData data);
2925
}
30-
26+
27+
public abstract class TextDataColumn<TData> : DataColumn<TData>
28+
{
29+
public readonly string header;
30+
private readonly bool hasMenu;
31+
32+
protected TextDataColumn(string header, float width, float minWidth = 0, float maxWidth = 0, bool hasMenu = false) : base(width, minWidth, maxWidth)
33+
{
34+
this.header = header;
35+
this.hasMenu = hasMenu;
36+
}
37+
public override void BuildHeader(ImGui gui)
38+
{
39+
gui.BuildText(header);
40+
if (hasMenu)
41+
{
42+
var rect = gui.statePosition;
43+
var menuRect = new Rect(rect.Right-1.7f, rect.Y, 1.5f, 1.5f);
44+
if (gui.isBuilding)
45+
gui.DrawIcon(menuRect, Icon.DropDown, SchemeColor.BackgroundText);
46+
if (gui.BuildButton(menuRect, SchemeColor.None, SchemeColor.Grey))
47+
gui.ShowDropDown(menuRect, BuildMenu, new Padding(1f));
48+
}
49+
}
50+
51+
public virtual void BuildMenu(ImGui gui) {}
52+
}
53+
3154
public class DataGrid<TData> where TData:class
3255
{
33-
private readonly DataColumn<TData>[] columns;
56+
public readonly List<DataColumn<TData>> columns;
3457
private readonly Padding innerPadding = new Padding(0.2f);
3558
public float width { get; private set; }
3659
private readonly float spacing;
3760
private Vector2 buildingStart;
3861
private ImGui contentGui;
62+
public float headerHeight = 1.3f;
3963

40-
public DataGrid(DataColumn<TData>[] columns)
64+
public DataGrid(params DataColumn<TData>[] columns)
4165
{
42-
this.columns = columns;
66+
this.columns = new List<DataColumn<TData>>(columns);
4367
spacing = innerPadding.left + innerPadding.right;
4468
}
69+
4570

4671
private void BuildHeaderResizer(ImGui gui, DataColumn<TData> column, Rect rect)
4772
{
@@ -83,34 +108,26 @@ public void BuildHeader(ImGui gui)
83108
var x = 0f;
84109
var topSeparator = gui.AllocateRect(0f, 0.1f);
85110
var y = gui.statePosition.Y;
86-
using (var group = gui.EnterFixedPositioning(0f, 1f, innerPadding))
111+
using (var group = gui.EnterFixedPositioning(0f, headerHeight, innerPadding))
87112
{
88-
foreach (var column in columns)
113+
for (var index = 0; index < columns.Count; index++) // Do not change to foreach
89114
{
115+
var column = columns[index];
90116
if (column.width < column.minWidth)
91117
column.width = column.minWidth;
92118
var rect = new Rect(x, y, column.width, 0f);
93-
group.SetManualRectRaw(rect, RectAllocator.LeftRow);
94-
gui.BuildText(column.header);
119+
@group.SetManualRectRaw(rect, RectAllocator.LeftRow);
120+
column.BuildHeader(gui);
95121
rect.Bottom = gui.statePosition.Y;
96122
x += column.width + spacing;
97123

98124
if (!column.isFixedSize)
99125
{
100-
BuildHeaderResizer(gui, column, new Rect(x-0.7f, y, 1f, 2.2f));
101-
}
102-
103-
if (column.menuBuilder != null)
104-
{
105-
var menuRect = new Rect(rect.Right-1.7f, rect.Y + 0.3f, 1.5f, 1.5f);
106-
if (gui.isBuilding)
107-
gui.DrawIcon(menuRect, Icon.DropDown, SchemeColor.BackgroundText);
108-
if (gui.BuildButton(menuRect, SchemeColor.None, SchemeColor.Grey))
109-
gui.ShowDropDown(menuRect, column.menuBuilder, new Padding(1f));
126+
BuildHeaderResizer(gui, column, new Rect(x - 0.7f, y, 1f, headerHeight + 0.9f));
110127
}
111128
}
112129
}
113-
width = x + 0.2f - spacing;
130+
width = MathF.Max(x + 0.2f - spacing, gui.width - 1f);
114131

115132
var separator = gui.AllocateRect(x, 0.1f);
116133
if (gui.isBuilding)
@@ -138,7 +155,7 @@ public Rect BuildRow(ImGui gui, TData element, float startX = 0f)
138155
if (column.width < column.minWidth)
139156
column.width = column.minWidth;
140157
@group.SetManualRect(new Rect(x, 0, column.width, 0f), RectAllocator.LeftRow);
141-
column.build(gui, element);
158+
column.BuildElement(gui, element);
142159
x += column.width + spacing;
143160
}
144161
}
@@ -148,7 +165,7 @@ public Rect BuildRow(ImGui gui, TData element, float startX = 0f)
148165
var rect = gui.lastRect;
149166
var bottom = gui.lastRect.Bottom;
150167
if (gui.isBuilding)
151-
gui.DrawRectangle(new Rect(startX, bottom - 0.1f, x-startX, 0.1f), SchemeColor.Grey);
168+
gui.DrawRectangle(new Rect(startX, bottom - 0.1f, width-startX, 0.1f), SchemeColor.Grey);
152169
return rect;
153170
}
154171

@@ -164,16 +181,18 @@ public Rect EndBuildingContent(ImGui gui)
164181
return new Rect(buildingStart.X, buildingStart.Y, width, bottom-buildingStart.Y);
165182
}
166183

167-
public bool BuildContent(ImGui gui, IReadOnlyList<TData> data, out (TData from, TData to) reorder, out Rect rect)
184+
public bool BuildContent(ImGui gui, IReadOnlyList<TData> data, out (TData from, TData to) reorder, out Rect rect, Func<TData, bool> filter = null)
168185
{
169186
BeginBuildingContent(gui);
170187
reorder = default;
171188
var hasReorder = false;
172189
for (var i = 0; i < data.Count; i++) // do not change to foreach
173190
{
174191
var t = data[i];
192+
if (filter != null && !filter(t))
193+
continue;
175194
var rowRect = BuildRow(gui, t);
176-
if (gui.DoListReordering(rowRect, rowRect, t, out var from))
195+
if (!hasReorder && gui.DoListReordering(rowRect, rowRect, t, out var from, SchemeColor.PureBackground, false))
177196
{
178197
reorder = (@from, t);
179198
hasReorder = true;

YAFC/Widgets/ImmediateWidgets.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public static void BuildFactorioObjectIcon(this ImGui gui, FactorioObject obj, M
4949
}
5050
}
5151
}
52+
53+
public static bool BuildFloatInput(this ImGui gui, float value, out float newValue, UnitOfMeasure unit, Padding padding)
54+
{
55+
if (gui.BuildTextInput(DataUtils.FormatAmount(value, unit), out var newText, null, Icon.None, true, padding) && DataUtils.TryParseAmount(newText, out newValue, unit))
56+
return true;
57+
newValue = value;
58+
return false;
59+
}
5260

5361
public static bool BuildFactorioObjectButton(this ImGui gui, Rect rect, FactorioObject obj, SchemeColor bgColor = SchemeColor.None, bool extendHeader = false)
5462
{
@@ -168,21 +176,26 @@ public static bool BuildFactorioObjectWithAmount(this ImGui gui, FactorioObject
168176

169177
public static void ShowPrecisionValueTootlip(ImGui gui, float amount, UnitOfMeasure unit, FactorioObject goods)
170178
{
179+
string text;
171180
switch (unit)
172181
{
173182
case UnitOfMeasure.PerSecond: case UnitOfMeasure.FluidPerSecond: case UnitOfMeasure.ItemPerSecond:
174183
var perSecond = DataUtils.FormatAmountRaw(amount, 1f, "/s", formatSpec:DataUtils.PreciseFormat);
175184
var perMinute = DataUtils.FormatAmountRaw(amount, 60f, "/m", formatSpec:DataUtils.PreciseFormat);
176185
var perHour = DataUtils.FormatAmountRaw(amount, 3600f, "/h", formatSpec:DataUtils.PreciseFormat);
177-
var text = perSecond + "\n" + perMinute + "\n" + perHour;
186+
text = perSecond + "\n" + perMinute + "\n" + perHour;
178187
if (goods is Item item)
179-
text += DataUtils.FormatAmount(item.stackSize / amount, UnitOfMeasure.Second, "\n", " per stack");
180-
gui.ShowTooltip(gui.lastRect, text, 10f);
188+
text += DataUtils.FormatAmount(MathF.Abs(item.stackSize / amount), UnitOfMeasure.Second, "\n", " per stack");
181189
break;
182190
default:
183-
gui.ShowTooltip(gui.lastRect, DataUtils.FormatAmount(amount, unit, precise:true), 10f);
191+
text = DataUtils.FormatAmount(amount, unit, precise: true);
184192
break;
185193
}
194+
gui.ShowTooltip(gui.lastRect, x =>
195+
{
196+
x.BuildFactorioObjectButtonWithText(goods);
197+
x.BuildText(text, wrap:true);
198+
}, 10f);
186199
}
187200

188201
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

YAFC/Widgets/MainScreenTabBar.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ private void BuildContents(ImGui gui)
6969
changePageTo = page;
7070
}
7171
else if (evt == ButtonEvent.MouseOver)
72-
{
73-
MainScreen.Instance.ShowTooltip(gui, page, false);
74-
}
72+
MainScreen.Instance.ShowTooltip(gui, page, false, gui.lastRect);
7573

7674
prevPage = page;
7775

YAFC/Widgets/ObjectTooltip.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,22 @@ private void BuildRecipe(RecipeOrTechnology recipe, ImGui gui)
391391
BuildSubHeader(gui, "Allowed modules");
392392
using (gui.EnterGroup(contentPadding))
393393
BuildIconRow(gui, recipe.modules, 1);
394+
var crafterCommonModules = AllowedEffects.All;
395+
foreach (var crafter in recipe.crafters)
396+
{
397+
if (crafter.moduleSlots > 0)
398+
crafterCommonModules &= crafter.allowedEffects;
399+
}
400+
401+
foreach (var module in recipe.modules)
402+
{
403+
if (!EntityWithModules.CanAcceptModule(module.module, crafterCommonModules))
404+
{
405+
using (gui.EnterGroup(contentPadding))
406+
gui.BuildText("Some crafters restrict module usage");
407+
break;
408+
}
409+
}
394410
}
395411

396412
if (recipe is Recipe lockedRecipe && !lockedRecipe.enabled)

YAFC/Windows/ImageSharePanel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override void Build(ImGui gui)
4242
Ui.VisitLink("file:///"+TempImageFile);
4343
}
4444

45-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && gui.BuildButton(copied ? "Copied to clipboard" : "Copy to clipboard (Ctrl+C)", active:!copied))
45+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && gui.BuildButton(copied ? "Copied to clipboard" : "Copy to clipboard (Ctrl+"+ImGuiUtils.ScanToString(SDL.SDL_Scancode.SDL_SCANCODE_C)+")", active:!copied))
4646
{
4747
WindowsClipboard.CopySurfaceToClipboard(surface);
4848
copied = true;

YAFC/Windows/MainScreen.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public MainScreen(int display, Project project) : base(default)
4545
{
4646
RegisterPageView<ProductionTable>(new ProductionTableView());
4747
RegisterPageView<AutoPlanner>(new AutoPlannerView());
48+
RegisterPageView<ProductionSummary>(new ProductionSummaryView());
4849
searchGui = new ImGui(BuildSearch, new Padding(1f)) {boxShadow = RectangleBorder.Thin, boxColor = SchemeColor.Background};
4950
Instance = this;
5051
tabBar = new MainScreenTabBar(this);
@@ -121,7 +122,7 @@ private void BuildPage(ImGui gui, ProjectPage element, int index)
121122
else SetActivePage(element);
122123
}
123124
else if (evt == ButtonEvent.MouseOver)
124-
ShowTooltip(gui, element, true);
125+
ShowTooltip(gui, element, true, gui.lastRect);
125126
}
126127

127128
private void ProjectOnMetaInfoChanged()
@@ -287,9 +288,11 @@ private void BuildPage(ImGui gui)
287288
}
288289
}
289290

290-
public ProjectPage AddProjectPage(string name, FactorioObject icon, Type contentType, bool setActive)
291+
public ProjectPage AddProjectPage(string name, FactorioObject icon, Type contentType, bool setActive, bool initNew)
291292
{
292293
var page = new ProjectPage(project, contentType) {name = name, icon = icon};
294+
if (initNew)
295+
page.content.InitNew();
293296
project.RecordUndo().pages.Add(page);
294297
if (setActive)
295298
SetActivePage(page);
@@ -367,13 +370,13 @@ private void BuildSearch(ImGui gui)
367370
private void SettingsDropdown(ImGui gui)
368371
{
369372
gui.boxColor = SchemeColor.Background;
370-
if (gui.BuildContextMenuButton("Undo", "Ctrl+Z") && gui.CloseDropdown())
373+
if (gui.BuildContextMenuButton("Undo", "Ctrl+" +ImGuiUtils.ScanToString(SDL.SDL_Scancode.SDL_SCANCODE_Z)) && gui.CloseDropdown())
371374
project.undo.PerformUndo();
372-
if (gui.BuildContextMenuButton("Save", "Ctrl+S") && gui.CloseDropdown())
375+
if (gui.BuildContextMenuButton("Save", "Ctrl+" + ImGuiUtils.ScanToString(SDL.SDL_Scancode.SDL_SCANCODE_S)) && gui.CloseDropdown())
373376
SaveProject().CaptureException();
374377
if (gui.BuildContextMenuButton("Save As") && gui.CloseDropdown())
375378
SaveProjectAs().CaptureException();
376-
if (gui.BuildContextMenuButton("Find on page", "Ctrl+F") && gui.CloseDropdown())
379+
if (gui.BuildContextMenuButton("Find on page", "Ctrl+" + ImGuiUtils.ScanToString(SDL.SDL_Scancode.SDL_SCANCODE_F)) && gui.CloseDropdown())
377380
ShowSearch();
378381
if (gui.BuildContextMenuButton("Load another project (Same mods)") && gui.CloseDropdown())
379382
LoadProjectLight();
@@ -386,7 +389,7 @@ private void SettingsDropdown(ImGui gui)
386389
if (gui.BuildContextMenuButton("Preferences") && gui.CloseDropdown())
387390
PreferencesScreen.Show();
388391

389-
if (gui.BuildContextMenuButton("Never Enough Items Explorer", "Ctrl+N") && gui.CloseDropdown())
392+
if (gui.BuildContextMenuButton("Never Enough Items Explorer", "Ctrl+" + ImGuiUtils.ScanToString(SDL.SDL_Scancode.SDL_SCANCODE_N)) && gui.CloseDropdown())
390393
ShowNeie();
391394

392395
if (gui.BuildContextMenuButton("Dependency Explorer") && gui.CloseDropdown())
@@ -650,10 +653,11 @@ public void Report((string, string) value)
650653

651654
public bool IsSameObjectHovered(ImGui gui, FactorioObject obj) => objectTooltip.IsSameObjectHovered(gui, obj);
652655

653-
public void ShowTooltip(ImGui gui, ProjectPage page, bool isMiddleEdit)
656+
public void ShowTooltip(ImGui gui, ProjectPage page, bool isMiddleEdit, Rect rect)
654657
{
655-
registeredPageViews.TryGetValue(page.content.GetType(), out var pageView);
656-
ShowTooltip(gui, gui.lastRect, x =>
658+
if (page == null || !registeredPageViews.TryGetValue(page.content.GetType(), out var pageView))
659+
return;
660+
ShowTooltip(gui, rect, x =>
657661
{
658662
pageView.BuildPageTooltip(x, page.content);
659663
if (isMiddleEdit)

YAFC/Windows/PreferencesScreen.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public override void Build(ImGui gui)
7575
Close();
7676
if (prefs.justChanged)
7777
MainScreen.Instance.RebuildProjectView();
78+
if (settings.justChanged)
79+
Project.current.RecalculateDisplayPages();
7880
}
7981

8082
private void ChoiceObject<T>(ImGui gui, string text, T[] list, T current, Action<T> select) where T:FactorioObject

YAFC/Windows/ProjectPageSettingsPanel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public override void Build(ImGui gui)
8181
gui.allocator = RectAllocator.LeftRow;
8282
if (editingPage != null && gui.BuildRedButton("Delete page"))
8383
{
84-
Project.current.RecordUndo().pages.Remove(editingPage);
84+
Project.current.RemovePage(editingPage);
8585
Close();
8686
}
8787
}
@@ -181,7 +181,7 @@ public static void LoadProjectPageFromClipboard()
181181
if (!haveChoice)
182182
return;
183183
if (choice)
184-
project.RecordUndo().pages.Remove(existing);
184+
project.RemovePage(existing);
185185
else
186186
page.GenerateNewGuid();
187187
project.RecordUndo().pages.Add(page);

0 commit comments

Comments
 (0)