Skip to content

Commit

Permalink
Refactoring for production summary group support
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowTheAge committed Feb 11, 2022
1 parent 14a7e60 commit 5357add
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 80 deletions.
18 changes: 18 additions & 0 deletions YAFC/Workspace/ProductionSummary/ProductionSummaryFlatHierarchy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using YAFC.Model;
using YAFC.UI;

namespace YAFC
{
public class ProductionSummaryFlatHierarchy : FlatHierarchy<ProductionSummaryEntry, ProductionSummaryGroup>
{
public ProductionSummaryFlatHierarchy(DataGrid<ProductionSummaryEntry> grid, Action<ImGui, ProductionSummaryGroup> drawTableHeader) : base(grid, drawTableHeader) {}
protected override bool Expanded(ProductionSummaryGroup group) => group.expanded;
protected override ProductionSummaryGroup Subgroup(ProductionSummaryEntry row) => row.group;
protected override List<ProductionSummaryEntry> Elements(ProductionSummaryGroup @group) => group.list;
protected override void SetOwner(ProductionSummaryEntry row, ProductionSummaryGroup newOwner) => row.SetOwner(newOwner);
protected override bool Filter(ProductionSummaryEntry row) => row.filterMatch;
protected override string emptyGroupMessage => "This is an empty group";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace YAFC
public class ProductionSummaryView : ProjectPageView<ProductionSummary>
{
private readonly DataGrid<ProductionSummaryEntry> grid;
private readonly ProductionSummaryFlatHierarchy flatHierarchy;
private readonly SearchableList<ProjectPage> pagesDropdown;
private SearchQuery searchQuery;
private Goods filteredGoods;
Expand All @@ -23,6 +24,7 @@ public ProductionSummaryView()
firstColumn = new SummaryColumn(this);
lastColumn = new RestGoodsColumn(this);
grid = new DataGrid<ProductionSummaryEntry>(firstColumn, lastColumn) {headerHeight = 4.2f};
flatHierarchy = new ProductionSummaryFlatHierarchy(grid, null);
pagesDropdown = new SearchableList<ProjectPage>(30f, new Vector2(20f, 2f), PagesDropdownDrawer, PagesDropdownFilter);
}

Expand Down Expand Up @@ -65,7 +67,7 @@ public override void BuildElement(ImGui gui, ProductionSummaryEntry entry)
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);
view.model.group.RecordUndo().list.Remove(entry);
});

using (gui.EnterFixedPositioning(3f, 2f, default))
Expand Down Expand Up @@ -171,7 +173,7 @@ private void PagesDropdownDrawer(ImGui gui, ProjectPage element, int index)

if (gui.BuildButton(gui.lastRect, SchemeColor.BackgroundAlt, SchemeColor.Background))
{
model.RecordUndo().list.Add(new ProductionSummaryEntry(model, new PageReference(element)));
model.group.RecordUndo().list.Add(new ProductionSummaryEntry(model.group, new PageReference(element)));
}
}

Expand Down Expand Up @@ -245,16 +247,13 @@ protected override void BuildContent(ImGui gui)
if (model == null)
return;

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

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


gui.AllocateSpacing(1f);
using (gui.EnterGroup(new Padding(1)))
{
if (model.list.Count == 0)
if (model.group.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))
Expand All @@ -270,6 +269,12 @@ protected override void BuildContent(ImGui gui)
if (gui.isBuilding)
gui.DrawRectangle(gui.lastRect, SchemeColor.Background, RectangleBorder.Thin);
}

public override void Rebuild(bool visuaOnly = false)
{
flatHierarchy.SetData(model.group);
base.Rebuild(visuaOnly);
}

private void AddProductionTableDropdown(ImGui gui)
{
Expand Down
94 changes: 56 additions & 38 deletions YAFC/Workspace/ProductionTable/ProductionTableFlatHierarchy.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,56 @@
using System;
using System.Collections.Generic;
using YAFC.Model;
using YAFC.Parser;
using YAFC.UI;

namespace YAFC
{
public class ProductionTableFlatHierarchy
public abstract class FlatHierarchy<TRow, TGroup> where TRow : ModelObject<TGroup> where TGroup:ModelObject<ModelObject>
{
private readonly DataGrid<RecipeRow> grid;
private readonly List<RecipeRow> flatRecipes = new List<RecipeRow>();
private readonly List<ProductionTable> flatGroups = new List<ProductionTable>();
private RecipeRow draggingRecipe;
private ProductionTable root;
private readonly DataGrid<TRow> grid;
private readonly List<TRow> flatRecipes = new List<TRow>();
private readonly List<TGroup> flatGroups = new List<TGroup>();
private TRow draggingRecipe;
private TGroup root;
private bool rebuildRequired;
private readonly Action<ImGui, ProductionTable> drawTableHeader;
private readonly Action<ImGui, TGroup> drawTableHeader;

public ProductionTableFlatHierarchy(DataGrid<RecipeRow> grid, Action<ImGui, ProductionTable> drawTableHeader)
protected abstract bool Expanded(TGroup group);
protected abstract TGroup Subgroup(TRow row);
protected abstract List<TRow> Elements(TGroup group);
protected abstract void SetOwner(TRow row, TGroup newOwner);
protected abstract bool Filter(TRow row);
protected abstract string emptyGroupMessage { get; }

protected FlatHierarchy(DataGrid<TRow> grid, Action<ImGui, TGroup> drawTableHeader)
{
this.grid = grid;
this.drawTableHeader = drawTableHeader;
}

public float width => grid.width;
public void SetData(ProductionTable table)
public void SetData(TGroup table)
{
root = table;
rebuildRequired = true;
}

private (ProductionTable, int) FindDragginRecipeParentAndIndex()
private (TGroup, int) FindDragginRecipeParentAndIndex()
{
var index = flatRecipes.IndexOf(draggingRecipe);
if (index == -1)
return default;
var currentIndex = 0;
for (var i = index - 1; i >= 0; i--)
{
if (flatRecipes[i] is RecipeRow recipe)
if (flatRecipes[i] is TRow recipe)
{
if (recipe.hasVisibleChildren)
return (recipe.subgroup, currentIndex);
var group = Subgroup(recipe);
if (group != null && Expanded(group))
return (group, currentIndex);
}
else
i = flatRecipes.LastIndexOf(flatGroups[i].owner as RecipeRow, i);
i = flatRecipes.LastIndexOf(flatGroups[i].owner as TRow, i);
currentIndex++;
}
return (root, currentIndex);
Expand All @@ -54,15 +61,15 @@ private void ActuallyMoveDraggingRecipe()
var (parent, index) = FindDragginRecipeParentAndIndex();
if (parent == null)
return;
if (draggingRecipe.owner == parent && parent.recipes[index] == draggingRecipe)
if (draggingRecipe.owner == parent && Elements(parent)[index] == draggingRecipe)
return;

draggingRecipe.owner.RecordUndo().recipes.Remove(draggingRecipe);
draggingRecipe.SetOwner(parent);
parent.RecordUndo().recipes.Insert(index, draggingRecipe);
Elements(draggingRecipe.owner.RecordUndo()).Remove(draggingRecipe);
SetOwner(draggingRecipe, parent);
Elements(parent.RecordUndo()).Insert(index, draggingRecipe);
}

private void MoveFlatHierarchy(RecipeRow from, RecipeRow to)
private void MoveFlatHierarchy(TRow from, TRow to)
{
draggingRecipe = from;
var indexFrom = flatRecipes.IndexOf(from);
Expand All @@ -71,7 +78,7 @@ private void MoveFlatHierarchy(RecipeRow from, RecipeRow to)
flatGroups.MoveListElementIndex(indexFrom, indexTo);
}

private void MoveFlatHierarchy(RecipeRow from, ProductionTable to)
private void MoveFlatHierarchy(TRow from, TGroup to)
{
draggingRecipe = from;
var indexFrom = flatRecipes.IndexOf(from);
Expand Down Expand Up @@ -104,7 +111,7 @@ public void Build(ImGui gui)
var item = flatGroups[i];
if (recipe != null)
{
if (!recipe.searchMatch)
if (!Filter(recipe))
{
if (item != null)
i = flatGroups.LastIndexOf(item);
Expand All @@ -123,23 +130,22 @@ public void Build(ImGui gui)
if (item == null && gui.InitiateDrag(rect, rect, recipe, bgColor))
draggingRecipe = recipe;
else if (gui.ConsumeDrag(rect.Center, recipe))
MoveFlatHierarchy(gui.GetDraggingObject<RecipeRow>(), recipe);
MoveFlatHierarchy(gui.GetDraggingObject<TRow>(), recipe);
if (item != null)
{
if (item.recipes.Count == 0)
if (Elements(item).Count == 0)
{
using (gui.EnterGroup(new Padding(0.5f+depWidth, 0.5f, 0.5f, 0.5f)))
{
gui.BuildText("This is a nested group. You can drag&drop recipes here. Nested groups can have its own linked materials", wrap:true);
if (gui.BuildLink("Delete empty nested group"))
{
recipe.RecordUndo().subgroup = null;
rebuildRequired = true;
}
gui.BuildText(emptyGroupMessage, wrap:true);
}
}
using (gui.EnterGroup(new Padding(0.5f+depWidth, 0.5f, 0.5f, 0.5f)))
drawTableHeader(gui, item);

if (drawTableHeader != null)
{
using (gui.EnterGroup(new Padding(0.5f+depWidth, 0.5f, 0.5f, 0.5f)))
drawTableHeader(gui, item);
}
}
}
else
Expand Down Expand Up @@ -167,17 +173,18 @@ private void Rebuild()
rebuildRequired = false;
}

private void BuildFlatHierarchy(ProductionTable table)
private void BuildFlatHierarchy(TGroup table)
{
foreach (var recipe in table.recipes)
foreach (var recipe in Elements(table))
{
flatRecipes.Add(recipe);
if (recipe.hasVisibleChildren)
var sub = Subgroup(recipe);
if (sub != null && Expanded(sub))
{
flatGroups.Add(recipe.subgroup);
BuildFlatHierarchy(recipe.subgroup);
flatGroups.Add(sub);
BuildFlatHierarchy(sub);
flatRecipes.Add(null);
flatGroups.Add(recipe.subgroup);
flatGroups.Add(sub);
}
else flatGroups.Add(null);
}
Expand All @@ -188,4 +195,15 @@ public void BuildHeader(ImGui gui)
grid.BuildHeader(gui);
}
}

public class ProductionTableFlatHierarchy : FlatHierarchy<RecipeRow, ProductionTable>
{
public ProductionTableFlatHierarchy(DataGrid<RecipeRow> grid, Action<ImGui, ProductionTable> drawTableHeader) : base(grid, drawTableHeader) {}
protected override bool Expanded(ProductionTable group) => group.expanded;
protected override ProductionTable Subgroup(RecipeRow row) => row.subgroup;
protected override List<RecipeRow> Elements(ProductionTable @group) => group.recipes;
protected override void SetOwner(RecipeRow row, ProductionTable newOwner) => row.SetOwner(newOwner);
protected override bool Filter(RecipeRow row) => row.searchMatch;
protected override string emptyGroupMessage => "This is a nested group. You can drag&drop recipes here. Nested groups can have its own linked materials";
}
}
Loading

0 comments on commit 5357add

Please sign in to comment.