Skip to content

Commit 85eedce

Browse files
committed
create CalculatedGrid to simplify and cleanup logic
1 parent 7d70dfc commit 85eedce

1 file changed

Lines changed: 98 additions & 61 deletions

File tree

src/main/java/gregtech/api/recipes/ui/RecipeMapUI.java

Lines changed: 98 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,12 @@ public ModularPanel constructPanel(MetaTileEntity mte, DoubleSupplier progressSu
555555
IItemHandlerModifiable importItems, IItemHandlerModifiable exportItems,
556556
FluidTankList importFluids, FluidTankList exportFluids,
557557
int yOffset, PanelSyncManager syncManager) {
558-
int inputHeight = calculateHeight(determineSlotsGrid(importItems.getSlots(), importFluids.getTanks()),
559-
importFluids.getTanks());
560-
int outputHeight = calculateHeight(determineSlotsGrid(exportItems.getSlots(), exportFluids.getTanks()),
561-
exportFluids.getTanks());
558+
CalculatedGrid inputGrid = CalculatedGrid.of(importItems, importFluids);
559+
CalculatedGrid outputGrid = CalculatedGrid.of(exportItems, exportFluids);
560+
561+
int inputHeight = inputGrid.getMaxHeight();
562+
int outputHeight = outputGrid.getMaxHeight();
563+
562564
ModularPanel panel = GTGuis.createPanel(mte, this.width, adjustHeight(inputHeight, outputHeight));
563565

564566
DoubleSyncValue progressValue = new DoubleSyncValue(progressSupplier);
@@ -577,14 +579,14 @@ public ModularPanel constructPanel(MetaTileEntity mte, DoubleSupplier progressSu
577579

578580
int progressSize = 20;
579581
int margin = 6;
580-
row.width(calculateWidth(importItems.getSlots(), importFluids.getTanks(),
581-
exportItems.getSlots(), exportFluids.getTanks(),
582-
progressSize, margin));
582+
row.width(calculatePixelWidth(inputGrid, outputGrid, progressSize, margin));
583583

584-
if (!this.isLeftGreater) row.mainAxisAlignment(Alignment.MainAxis.END);
584+
if (inputGrid.getMaxWidth() < outputGrid.getMaxWidth()) {
585+
row.mainAxisAlignment(Alignment.MainAxis.END);
586+
}
585587

586588
if (importItems.getSlots() > 0 || importFluids.getTanks() > 0) {
587-
row.child(makeInventorySlotGroup(importItems, importFluids, false));
589+
row.child(makeInventorySlotGroup(inputGrid, importItems, importFluids, false));
588590
}
589591
RecipeProgressWidget progressWidget = new RecipeProgressWidget();
590592
if (this.extraOverlays != null) {
@@ -599,34 +601,17 @@ public ModularPanel constructPanel(MetaTileEntity mte, DoubleSupplier progressSu
599601
.texture(progressTexture, 20)
600602
.direction(progressDirection));
601603
if (exportItems.getSlots() > 0 || exportFluids.getTanks() > 0) {
602-
row.child(makeInventorySlotGroup(exportItems, exportFluids, true));
604+
row.child(makeInventorySlotGroup(outputGrid, exportItems, exportFluids, true));
603605
}
604606
return panel.child(row);
605607
}
606608

607-
private int calculateWidth(int inputItems, int inputFluids,
608-
int outputItems, int outputFluids,
609-
int progressSize, int margin) {
610-
int[] inputGrid = determineSlotsGrid(inputItems, inputFluids);
611-
int[] outputGrid = determineSlotsGrid(outputItems, outputFluids);
612-
613-
int w1 = getGridWidth(inputGrid, inputFluids);
614-
int w2 = getGridWidth(outputGrid, outputFluids);
609+
private static int calculatePixelWidth(CalculatedGrid inputGrid, CalculatedGrid outputGrid,
610+
int progressSize, int margin) {
611+
int leftWidth = inputGrid.getMaxWidth() * 18;
612+
int rightWidth = outputGrid.getMaxWidth() * 18;
615613

616-
this.isLeftGreater = w1 >= w2;
617-
return (Math.max(w1, w2) + margin) * 2 + progressSize;
618-
}
619-
620-
private int getGridWidth(int[] grid, int fluidCount) {
621-
if (isSingleRow(grid, fluidCount)) {
622-
return (grid[0] + grid[2]) * 18;
623-
} else {
624-
return Math.max(grid[0] * 18, (grid[2] * 18));
625-
}
626-
}
627-
628-
private boolean isSingleRow(int[] grid, int fluidCount) {
629-
return grid[1] >= fluidCount && grid[0] < 3;
614+
return (Math.max(leftWidth, rightWidth) + margin) * 2 + progressSize;
630615
}
631616

632617
private int adjustHeight(int inputHeight, int outputHeight) {
@@ -636,25 +621,14 @@ private int adjustHeight(int inputHeight, int outputHeight) {
636621
return this.height;
637622
}
638623

639-
private int calculateHeight(int[] inputs, int inputFluids) {
640-
int inputHeight;
641-
if (isSingleRow(inputs, inputFluids)) {
642-
inputHeight = Math.max(inputs[1], inputs[3]);
643-
} else {
644-
inputHeight = inputs[1] + inputs[3];
645-
}
646-
return inputHeight;
647-
}
648-
649-
/**
650-
* @param grid [item grid width, item grid height, fluid grid width, fluid grid height]
651-
*/
652-
private Widget<?> makeItemGroup(int[] grid, IItemHandlerModifiable handler, boolean isOutputs) {
624+
private Widget<?> makeItemGroup(CalculatedGrid grid, IItemHandlerModifiable handler, boolean isOutputs) {
653625
Flow col = Flow.column()
654626
.mainAxisAlignment(Alignment.MainAxis.END)
655627
.coverChildren()
656628
.debugName("col:item_grid");
657-
int width = grid[0], height = grid[1];
629+
int width = grid.getItemGridWidth();
630+
int height = grid.getItemGridHeight();
631+
658632
SlotGroup slotGroup = new SlotGroup(isOutputs ? "output_items" : "input_items", width, 1, !isOutputs);
659633
for (int i = 0; i < height; i++) {
660634
Flow row = Flow.row()
@@ -669,15 +643,15 @@ private Widget<?> makeItemGroup(int[] grid, IItemHandlerModifiable handler, bool
669643
return col;
670644
}
671645

672-
/**
673-
* @param grid [item grid width, item grid height, fluid grid width, fluid grid height]
674-
*/
675-
private Widget<?> makeFluidGroup(int[] grid, FluidTankList handler, boolean isOutputs) {
646+
private Widget<?> makeFluidGroup(CalculatedGrid grid, FluidTankList handler, boolean isOutputs) {
676647
Flow col = Flow.column()
677648
.mainAxisAlignment(Alignment.MainAxis.START)
678649
.coverChildren()
679650
.debugName("col:fluid_grid");
680-
int width = grid[2], height = grid[3];
651+
652+
int width = grid.getFluidGridWidth();
653+
int height = grid.getFluidGridHeight();
654+
681655
for (int i = 0; i < height; i++) {
682656
Flow row = Flow.row()
683657
.mainAxisAlignment(isOutputs ? Alignment.MainAxis.START : Alignment.MainAxis.END)
@@ -692,7 +666,7 @@ private Widget<?> makeFluidGroup(int[] grid, FluidTankList handler, boolean isOu
692666
}
693667

694668
@NotNull
695-
protected Widget<?> makeInventorySlotGroup(@NotNull IItemHandlerModifiable itemHandler,
669+
protected Widget<?> makeInventorySlotGroup(CalculatedGrid grid, @NotNull IItemHandlerModifiable itemHandler,
696670
@NotNull FluidTankList fluidHandler, boolean isOutputs) {
697671
final int itemInputsCount = itemHandler.getSlots();
698672
boolean onlyFluids = itemInputsCount == 0;
@@ -702,11 +676,13 @@ protected Widget<?> makeInventorySlotGroup(@NotNull IItemHandlerModifiable itemH
702676
throw new IllegalArgumentException("item and fluid handlers are empty!");
703677
}
704678

705-
int[] slotGridSizes = determineSlotsGrid(itemInputsCount, fluidInputsCount);
706-
int itemGridHeight = slotGridSizes[onlyFluids ? 3 : 1];
707-
int fluidGridHeight = slotGridSizes[3];
679+
int itemGridHeight = grid.getFluidGridHeight();
680+
int fluidGridHeight = grid.getFluidGridHeight();
681+
if (!onlyFluids) {
682+
itemGridHeight = grid.getItemGridHeight();
683+
}
708684

709-
boolean singleRow = isSingleRow(slotGridSizes, fluidInputsCount);
685+
boolean singleRow = grid.isSingleRow();
710686

711687
Flow flow = (singleRow ? Flow.row() : Flow.column())
712688
.coverChildren()
@@ -724,11 +700,11 @@ protected Widget<?> makeInventorySlotGroup(@NotNull IItemHandlerModifiable itemH
724700
}
725701

726702
if (onlyFluids) {
727-
flow.childIf(fluidInputsCount > 0, () -> makeFluidGroup(slotGridSizes, fluidHandler, isOutputs));
703+
flow.childIf(fluidInputsCount > 0, () -> makeFluidGroup(grid, fluidHandler, isOutputs));
728704
} else {
729-
flow.childIf(!singleRow || isOutputs, () -> makeItemGroup(slotGridSizes, itemHandler, isOutputs));
730-
flow.childIf(fluidInputsCount > 0, () -> makeFluidGroup(slotGridSizes, fluidHandler, isOutputs));
731-
flow.childIf(singleRow && !isOutputs, () -> makeItemGroup(slotGridSizes, itemHandler, isOutputs));
705+
flow.childIf(!singleRow || isOutputs, () -> makeItemGroup(grid, itemHandler, isOutputs));
706+
flow.childIf(fluidInputsCount > 0, () -> makeFluidGroup(grid, fluidHandler, isOutputs));
707+
flow.childIf(singleRow && !isOutputs, () -> makeItemGroup(grid, itemHandler, isOutputs));
732708
}
733709

734710
return flow;
@@ -875,4 +851,65 @@ public boolean canModifyFluidOutputs() {
875851
public @NotNull R recipeMap() {
876852
return recipeMap;
877853
}
854+
855+
protected static class CalculatedGrid {
856+
857+
private final int itemCount;
858+
private final int fluidCount;
859+
private final int[] grid;
860+
861+
protected static CalculatedGrid of(IItemHandlerModifiable itemHandler, FluidTankList fluidTankList) {
862+
return new CalculatedGrid(itemHandler.getSlots(), fluidTankList.getTanks());
863+
}
864+
865+
private CalculatedGrid(int itemCount, int fluidCount) {
866+
this.itemCount = itemCount;
867+
this.fluidCount = fluidCount;
868+
this.grid = RecipeMapUI.determineSlotsGrid(this.itemCount, this.fluidCount);
869+
}
870+
871+
public int getItemCount() {
872+
return this.itemCount;
873+
}
874+
875+
public int getItemGridWidth() {
876+
return this.grid[0];
877+
}
878+
879+
public int getItemGridHeight() {
880+
return this.grid[1];
881+
}
882+
883+
public int getFluidCount() {
884+
return this.fluidCount;
885+
}
886+
887+
public int getFluidGridWidth() {
888+
return this.grid[2];
889+
}
890+
891+
public int getFluidGridHeight() {
892+
return this.grid[3];
893+
}
894+
895+
public int getMaxWidth() {
896+
if (isSingleRow()) {
897+
return getFluidGridWidth() + getItemGridWidth();
898+
} else {
899+
return Math.max(getFluidGridWidth(), getItemGridWidth());
900+
}
901+
}
902+
903+
public int getMaxHeight() {
904+
if (isSingleRow()) {
905+
return Math.max(getFluidGridHeight(), getItemGridHeight());
906+
} else {
907+
return getFluidGridHeight() + getItemGridHeight();
908+
}
909+
}
910+
911+
private boolean isSingleRow() {
912+
return getItemGridHeight() >= getFluidCount() && getItemGridWidth() < 3;
913+
}
914+
}
878915
}

0 commit comments

Comments
 (0)