Skip to content

Commit

Permalink
Summary view WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowTheAge committed Feb 8, 2022
1 parent b467fa2 commit 155e7e6
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 43 deletions.
4 changes: 2 additions & 2 deletions YAFC/Widgets/DataGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void BuildHeader(ImGui gui)
}
}
}
width = x + 0.2f - spacing;
width = MathF.Max(x + 0.2f - spacing, gui.width - 1f);

var separator = gui.AllocateRect(x, 0.1f);
if (gui.isBuilding)
Expand Down Expand Up @@ -165,7 +165,7 @@ public Rect BuildRow(ImGui gui, TData element, float startX = 0f)
var rect = gui.lastRect;
var bottom = gui.lastRect.Bottom;
if (gui.isBuilding)
gui.DrawRectangle(new Rect(startX, bottom - 0.1f, x-startX, 0.1f), SchemeColor.Grey);
gui.DrawRectangle(new Rect(startX, bottom - 0.1f, width-startX, 0.1f), SchemeColor.Grey);
return rect;
}

Expand Down
13 changes: 9 additions & 4 deletions YAFC/Widgets/ImmediateWidgets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,26 @@ public static bool BuildFactorioObjectWithAmount(this ImGui gui, FactorioObject

public static void ShowPrecisionValueTootlip(ImGui gui, float amount, UnitOfMeasure unit, FactorioObject goods)
{
string text;
switch (unit)
{
case UnitOfMeasure.PerSecond: case UnitOfMeasure.FluidPerSecond: case UnitOfMeasure.ItemPerSecond:
var perSecond = DataUtils.FormatAmountRaw(amount, 1f, "/s", formatSpec:DataUtils.PreciseFormat);
var perMinute = DataUtils.FormatAmountRaw(amount, 60f, "/m", formatSpec:DataUtils.PreciseFormat);
var perHour = DataUtils.FormatAmountRaw(amount, 3600f, "/h", formatSpec:DataUtils.PreciseFormat);
var text = perSecond + "\n" + perMinute + "\n" + perHour;
text = perSecond + "\n" + perMinute + "\n" + perHour;
if (goods is Item item)
text += DataUtils.FormatAmount(item.stackSize / amount, UnitOfMeasure.Second, "\n", " per stack");
gui.ShowTooltip(gui.lastRect, text, 10f);
text += DataUtils.FormatAmount(MathF.Abs(item.stackSize / amount), UnitOfMeasure.Second, "\n", " per stack");
break;
default:
gui.ShowTooltip(gui.lastRect, DataUtils.FormatAmount(amount, unit, precise:true), 10f);
text = DataUtils.FormatAmount(amount, unit, precise: true);
break;
}
gui.ShowTooltip(gui.lastRect, x =>
{
x.BuildFactorioObjectButtonWithText(goods);
x.BuildText(text, wrap:true);
}, 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
Expand Down
119 changes: 91 additions & 28 deletions YAFC/Workspace/ProductionSummaryView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ public class ProductionSummaryView : ProjectPageView<ProductionSummary>
private Goods filteredGoods;
private Func<ProductionSummaryEntry, bool> filteredGoodsFilter;
private readonly Dictionary<ProductionSummaryColumn, GoodsColumn> goodsToColumn = new Dictionary<ProductionSummaryColumn, GoodsColumn>();
private readonly SummaryColumn firstColumn;
private readonly RestGoodsColumn lastColumn;

public ProductionSummaryView()
{
grid = new DataGrid<ProductionSummaryEntry>(new SummaryColumn(this)) {headerHeight = 4f};
firstColumn = new SummaryColumn(this);
lastColumn = new RestGoodsColumn(this);
grid = new DataGrid<ProductionSummaryEntry>(firstColumn, lastColumn) {headerHeight = 4.2f};
pagesDropdown = new SearchableList<ProjectPage>(30f, new Vector2(20f, 2f), PagesDropdownDrawer, PagesDropdownFilter);
}

Expand All @@ -44,21 +48,31 @@ public override void BuildHeader(ImGui gui)
public override void BuildElement(ImGui gui, ProductionSummaryEntry entry)
{
gui.allocator = RectAllocator.LeftAlign;
using (gui.EnterRow(0.2f))
using (gui.EnterGroup(new Padding(0.3f), RectAllocator.LeftRow, SchemeColor.None, 0.2f))
{
var icon = entry.icon;
if (icon != Icon.None)
gui.BuildIcon(entry.icon);
gui.BuildText(entry.name);
}
if (gui.action == ImGuiAction.MouseMove && gui.ConsumeMouseOver(gui.lastRect))

var buttonEvent = gui.BuildButton(gui.lastRect, SchemeColor.None, SchemeColor.BackgroundAlt);
if (buttonEvent == ButtonEvent.MouseOver)
MainScreen.Instance.ShowTooltip(gui, entry.page.page, false, gui.lastRect);
else if (buttonEvent == ButtonEvent.Click)
gui.ShowDropDown(tgui =>
{
if (tgui.BuildButton("Go to page") && tgui.CloseDropdown())
MainScreen.Instance.SetActivePage(entry.page.page);
if (tgui.BuildRedButton("Remove") && tgui.CloseDropdown())
view.model.RecordUndo().list.Remove(entry);
});

using (gui.EnterFixedPositioning(3f, 2f, default))
{
gui.allocator = RectAllocator.LeftRow;
gui.BuildText("x");
if (gui.BuildFloatInput(entry.multiplier, out var newMultiplier, UnitOfMeasure.None, default))
if (gui.BuildFloatInput(entry.multiplier, out var newMultiplier, UnitOfMeasure.None, default) && newMultiplier >= 0)
entry.RecordUndo().multiplier = newMultiplier;
}
}
Expand Down Expand Up @@ -88,11 +102,7 @@ public override void BuildHeader(ImGui gui)
moveHandle.Height = 5f;

if (gui.BuildFactorioObjectWithAmount(goods, view.model.GetTotalFlow(goods), goods.flowUnitOfMeasure, view.filteredGoods == goods ? SchemeColor.Primary : SchemeColor.None))
{
if (view.filteredGoods == goods)
view.ApplyFilter(null);
else view.ApplyFilter(goods);
}
view.ApplyFilter(goods);

if (!gui.InitiateDrag(moveHandle, moveHandle, column) && gui.ConsumeDrag(moveHandle.Center, column))
{
Expand All @@ -105,14 +115,46 @@ public override void BuildElement(ImGui gui, ProductionSummaryEntry data)
{
var amount = data.GetAmount(goods);
if (amount != 0)
gui.BuildFactorioObjectWithAmount(goods, data.GetAmount(goods), goods.flowUnitOfMeasure);
if (gui.BuildFactorioObjectWithAmount(goods, data.GetAmount(goods), goods.flowUnitOfMeasure))
view.ApplyFilter(goods);
}
}

private class RestGoodsColumn : TextDataColumn<ProductionSummaryEntry>
{
private readonly ProductionSummaryView view;
public RestGoodsColumn(ProductionSummaryView view) : base("Other", 30f, 5f, 40f)
{
this.view = view;
}

public override void BuildElement(ImGui gui, ProductionSummaryEntry data)
{
using (var grid = gui.EnterInlineGrid(2.1f))
{
foreach (var (goods, amount) in data.flow)
{
if (amount == 0f)
continue;
if (!view.model.columnsExist.Contains(goods))
{
grid.Next();
var evt = gui.BuildButton(goods.icon, amount > 0f ? SchemeColor.Green : SchemeColor.None, size:1.5f);
if (evt == ButtonEvent.Click)
view.AddOrRemoveColumn(goods);
else if (evt == ButtonEvent.MouseOver)
ImmediateWidgets.ShowPrecisionValueTootlip(gui, amount, goods.flowUnitOfMeasure, goods);
}
}
}
}
}

private void ApplyFilter(Goods goods)
{
filteredGoods = goods;
filteredGoodsFilter = goods == null ? null : (Func<ProductionSummaryEntry, bool>) (entry => entry.flow.TryGetValue(goods, out var amount) && amount != 0);
var filter = filteredGoods == goods ? null : goods;
filteredGoods = filter;
filteredGoodsFilter = filter == null ? null : (Func<ProductionSummaryEntry, bool>) (entry => entry.flow.TryGetValue(filter, out var amount) && amount != 0);
Rebuild();
}

Expand All @@ -135,7 +177,7 @@ private void PagesDropdownDrawer(ImGui gui, ProjectPage element, int index)

private bool IsColumnsSynced()
{
if (grid.columns.Count != model.columns.Count + 1)
if (grid.columns.Count != model.columns.Count + 2)
return false;
var index = 1;
foreach (var column in model.columns)
Expand All @@ -157,7 +199,8 @@ private void SyncGridHeaderWithColumns()
{
var columns = grid.columns;
var modelColumns = model.columns;
columns.RemoveRange(1, grid.columns.Count - 1);
columns.Clear();
columns.Add(firstColumn);
foreach (var column in modelColumns)
{
if (!goodsToColumn.TryGetValue(column, out var currentColumn))
Expand All @@ -167,6 +210,7 @@ private void SyncGridHeaderWithColumns()
}
columns.Add(currentColumn);
}
columns.Add(lastColumn);
}

protected override void BuildHeader(ImGui gui)
Expand All @@ -177,30 +221,54 @@ protected override void BuildHeader(ImGui gui)
base.BuildHeader(gui);
}

private void AddOrRemoveColumn(Goods goods)
{
model.RecordUndo();
var found = false;
for (var i = 0; i < model.columns.Count; i++)
{
var column = model.columns[i];
if (column.goods == goods)
{
model.columns.RemoveAt(i);
found = true;
break;
}
}

if (!found)
model.columns.Add(new ProductionSummaryColumn(model, goods));
}

protected override void BuildContent(ImGui gui)
{
if (model == null)
return;

var hasReorder = grid.BuildContent(gui, model.list, out var reorder, out var rect, filteredGoodsFilter);
gui.SetMinWidth(grid.width);

if (hasReorder)
model.RecordUndo(true).list.MoveListElement(reorder.from, reorder.to);

if (model.list.Count == 0)
gui.BuildText("Add your existing sheets here to keep track of what you have in your base and to see what shortages you may have");
else gui.BuildText("List of other things produced/consumed by added blocks. Click on any of these to add it to the table.");
using (var igrid = gui.EnterInlineGrid(3f, 1f))

gui.AllocateSpacing(1f);
using (gui.EnterGroup(new Padding(1)))
{
foreach (var element in model.nonCapturedFlow)
if (model.list.Count == 0)
gui.BuildText("Add your existing sheets here to keep track of what you have in your base and to see what shortages you may have");
else gui.BuildText("List of goods produced/consumed by added blocks. Click on any of these to add it to (or remove it from) the table.");
using (var igrid = gui.EnterInlineGrid(3f, 1f))
{
igrid.Next();
if (gui.BuildFactorioObjectWithAmount(element.goods, element.amount, element.goods.flowUnitOfMeasure))
foreach (var element in model.sortedFlow)
{
model.RecordUndo().columns.Add(new ProductionSummaryColumn(model, element.goods));
igrid.Next();
if (gui.BuildFactorioObjectWithAmount(element.goods, element.amount, element.goods.flowUnitOfMeasure, model.columnsExist.Contains(element.goods) ? SchemeColor.Primary : SchemeColor.None))
AddOrRemoveColumn(element.goods);
}
}
}
if (gui.isBuilding)
gui.DrawRectangle(gui.lastRect, SchemeColor.Background, RectangleBorder.Thin);
}

private void AddProductionTableDropdown(ImGui gui)
Expand All @@ -213,11 +281,6 @@ private void AddProductionTableDropdown(ImGui gui)
pagesDropdown.Build(gui);
}

private void BuildSummary(ImGui gui)
{

}

public override void CreateModelDropdown(ImGui gui, Type type, Project project)
{
if (gui.BuildContextMenuButton("Create production summary (Preview)") && gui.CloseDropdown())
Expand Down
4 changes: 2 additions & 2 deletions YAFC/YAFC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
<PlatformTarget>x64</PlatformTarget>
<AssemblyVersion>0.5.8</AssemblyVersion>
<FileVersion>0.5.8</FileVersion>
<AssemblyVersion>0.6.0</AssemblyVersion>
<FileVersion>0.6.0</FileVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
13 changes: 6 additions & 7 deletions YAFCmodel/Model/ProductionSummary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void RefreshFlow()
if (link.amount != 0)
{
flow.TryGetValue(link.goods, out var prevValue);
flow[link.goods] = prevValue + link.amount;
flow[link.goods] = prevValue + link.amount * multiplier;
}
}
}
Expand All @@ -94,10 +94,10 @@ public class ProductionSummary : ProjectPageContents, IComparer<(Goods goods, fl
public ProductionSummary(ModelObject page) : base(page) {}
public List<ProductionSummaryEntry> list { get; } = new List<ProductionSummaryEntry>();
public List<ProductionSummaryColumn> columns { get; } = new List<ProductionSummaryColumn>();
[SkipSerialization] public List<(Goods goods, float amount)> nonCapturedFlow { get; } = new List<(Goods goods, float amount)>();
[SkipSerialization] public List<(Goods goods, float amount)> sortedFlow { get; } = new List<(Goods goods, float amount)>();

private readonly Dictionary<Goods, float> totalFlow = new Dictionary<Goods, float>();
private readonly HashSet<Goods> columnsExist = new HashSet<Goods>();
[SkipSerialization] public HashSet<Goods> columnsExist { get; } = new HashSet<Goods>();

public override void InitNew()
{
Expand Down Expand Up @@ -134,11 +134,10 @@ public override async Task<string> Solve(ProjectPage page)

foreach (var column in columns)
columnsExist.Add(column.goods);
nonCapturedFlow.Clear();
sortedFlow.Clear();
foreach (var element in totalFlow)
if (!columnsExist.Contains(element.Key))
nonCapturedFlow.Add((element.Key, element.Value));
nonCapturedFlow.Sort(this);
sortedFlow.Add((element.Key, element.Value));
sortedFlow.Sort(this);
return null;
}

Expand Down

0 comments on commit 155e7e6

Please sign in to comment.