1515import net .minecraftforge .items .IItemHandlerModifiable ;
1616
1717import com .cleanroommc .modularui .api .drawable .IDrawable ;
18+ import com .cleanroommc .modularui .api .widget .IWidget ;
1819import com .cleanroommc .modularui .drawable .UITexture ;
1920import com .cleanroommc .modularui .screen .ModularPanel ;
21+ import com .cleanroommc .modularui .utils .Alignment ;
2022import com .cleanroommc .modularui .value .sync .DoubleSyncValue ;
2123import com .cleanroommc .modularui .value .sync .PanelSyncManager ;
2224import com .cleanroommc .modularui .value .sync .SyncHandlers ;
2325import com .cleanroommc .modularui .widget .ParentWidget ;
26+ import com .cleanroommc .modularui .widget .Widget ;
2427import com .cleanroommc .modularui .widget .sizer .Area ;
2528import com .cleanroommc .modularui .widgets .ItemSlot ;
2629import com .cleanroommc .modularui .widgets .ProgressWidget ;
30+ import com .cleanroommc .modularui .widgets .layout .Flow ;
2731import com .cleanroommc .modularui .widgets .layout .Grid ;
2832import com .cleanroommc .modularui .widgets .slot .SlotGroup ;
2933import it .unimi .dsi .fastutil .bytes .Byte2ObjectMap ;
3539import org .jetbrains .annotations .NotNull ;
3640import org .jetbrains .annotations .Nullable ;
3741
42+ import java .util .ArrayList ;
43+ import java .util .List ;
3844import java .util .function .Consumer ;
3945import java .util .function .DoubleSupplier ;
4046
@@ -547,19 +553,30 @@ public ModularPanel constructPanel(ModularPanel panel, DoubleSupplier progressSu
547553 FluidTankList exportFluids , int yOffset , PanelSyncManager syncManager ) {
548554 DoubleSyncValue progressValue = new DoubleSyncValue (progressSupplier );
549555
550- panel .debugName ("recipemapui.parent" );
556+ Flow row = Flow .row ()
557+ .height (3 * 18 + 9 )
558+ .debugName ("recipemapui.parent" )
559+ .crossAxisAlignment (Alignment .CrossAxis .CENTER )
560+ .top (23 - 7 );
551561
552- panel .child (new RecipeProgressWidget ()
562+ int m = calculateCenter (importItems .getSlots (), importFluids .getTanks (), 176 + 20 );
563+
564+ row .child (makeInventorySlotGroup (importItems , importFluids , false )
565+ .marginLeft (m - 4 ));
566+ row .child (new RecipeProgressWidget ()
553567 .recipeMap (recipeMap )
554568 .debugName ("recipe.progress" )
555569 .size (20 )
556- .alignX (0.5f )
557- .top (23 + yOffset )
570+ .margin (4 , 0 )
571+ // .alignX(0.5f)
572+ // .top(23 + yOffset)
558573 .value (progressValue )
559574 .texture (progressTexture , 20 )
560575 .direction (progressDirection ));
561- addInventorySlotGroup (panel , importItems , importFluids , false , yOffset );
562- addInventorySlotGroup (panel , exportItems , exportFluids , true , yOffset );
576+ row .child (makeInventorySlotGroup (exportItems , exportFluids , true ));
577+ // addInventorySlotGroup(panel, importItems, importFluids, false, yOffset);
578+ // addInventorySlotGroup(panel, exportItems, exportFluids, true, yOffset);
579+ panel .child (row );
563580 if (specialDrawableTexture != null ) {
564581 panel .child (specialDrawableTexture .asWidget ()
565582 .debugName ("special_texture" )
@@ -569,6 +586,154 @@ public ModularPanel constructPanel(ModularPanel panel, DoubleSupplier progressSu
569586 return panel ;
570587 }
571588
589+ private int calculateCenter (int inputItems , int inputFluids , int panelSize ) {
590+ int [] ints = determineSlotsGrid (inputItems , inputFluids );
591+ int leftSize = ints [1 ] >= inputFluids && ints [0 ] < 3 ?
592+ (ints [0 ] + ints [2 ]) * 18 :
593+ Math .max (ints [0 ] * 18 , ints [2 ] * 18 );
594+ int p = panelSize / 2 ;
595+ p -= 10 ;
596+ p -= leftSize ;
597+ return p ;
598+ }
599+
600+ private Widget <?> makeItemGroup (int width , IItemHandlerModifiable handler , boolean isOutputs ) {
601+ Flow col = Flow .column ().mainAxisAlignment (Alignment .MainAxis .END )
602+ .coverChildren ().debugName ("item.col" );
603+ int c = handler .getSlots ();
604+ int h = (int ) Math .ceil ((double ) c / width );
605+ SlotGroup slotGroup = new SlotGroup (isOutputs ? "output_items" : "input_items" ,
606+ width , 1 , !isOutputs );
607+ for (int i = 0 ; i < h ; i ++) {
608+ Flow row = Flow .row ().mainAxisAlignment (isOutputs ? Alignment .MainAxis .START : Alignment .MainAxis .END )
609+ .coverChildren ().debugName ("item.row." + i );
610+ for (int j = 0 ; j < width ; j ++) {
611+ row .child (makeItemSlot (slotGroup , (i * h ) + j , handler , isOutputs ));
612+ }
613+ col .child (row );
614+ }
615+ return col ;
616+ }
617+
618+ private Widget <?> makeFluidGroup (int width , FluidTankList handler , boolean isOutputs ) {
619+ Flow col = Flow .column ().mainAxisAlignment (Alignment .MainAxis .START )
620+ .coverChildren ().debugName ("fluid.col" );
621+ int c = handler .getTanks ();
622+ int h = (int ) Math .ceil ((double ) c / width );
623+ for (int i = 0 ; i < h ; i ++) {
624+ Flow row = Flow .row ().mainAxisAlignment (isOutputs ? Alignment .MainAxis .START : Alignment .MainAxis .END )
625+ .coverChildren ().debugName ("fluid.row" );
626+ for (int j = 0 ; j < width ; j ++) {
627+ row .child (makeFluidSlot ((i * h ) + j , handler , isOutputs ));
628+ }
629+ col .child (row );
630+ }
631+ return col ;
632+ }
633+
634+ protected Widget <?> makeInventorySlotGroup (@ NotNull IItemHandlerModifiable itemHandler ,
635+ @ NotNull FluidTankList fluidHandler , boolean isOutputs ) {
636+ final int itemInputsCount = itemHandler .getSlots ();
637+ boolean onlyFluids = itemInputsCount == 0 ;
638+ final int fluidInputsCount = fluidHandler .getTanks ();
639+ if (fluidInputsCount == 0 && onlyFluids )
640+ return null ; // nothing to do here
641+
642+ int [] slotGridSizes = determineSlotsGrid (itemInputsCount , fluidInputsCount );
643+ int itemGridWidth = slotGridSizes [onlyFluids ? 2 : 0 ];
644+ int itemGridHeight = slotGridSizes [onlyFluids ? 3 : 1 ];
645+
646+ int fluidGridWidth = slotGridSizes [2 ];
647+ int fluidGridHeight = slotGridSizes [3 ];
648+ boolean singleRow = itemGridHeight >= fluidInputsCount && itemGridWidth < 3 ;
649+
650+ Flow flow = (singleRow ? Flow .row () : Flow .column ()).coverChildren ()
651+ .debugName (singleRow ? "parent.row" : "parent.col" );
652+ flow .crossAxisAlignment (isOutputs ? Alignment .CrossAxis .START : Alignment .CrossAxis .END );
653+
654+ if (!onlyFluids && fluidGridHeight > 1 ) {
655+ // 1 should be 18, 2 should be 0, 3 should be -18, 4 should be -36
656+ // this is to make the first item row align with progress widget
657+ flow .top ((2 - itemGridHeight ) * 18 );
658+ }
659+
660+ if (itemInputsCount > 6 ) {
661+ flow .top (0 );
662+ }
663+
664+ if (onlyFluids ) {
665+ flow .childIf (fluidInputsCount > 0 , () -> makeFluidGroup (fluidGridWidth , fluidHandler , isOutputs ));
666+ } else {
667+ flow .childIf (!singleRow || isOutputs , () -> makeItemGroup (itemGridWidth , itemHandler , isOutputs ));
668+ flow .childIf (fluidInputsCount > 0 , () -> makeFluidGroup (fluidGridWidth , fluidHandler , isOutputs ));
669+ flow .childIf (singleRow && !isOutputs , () -> makeItemGroup (itemGridWidth , itemHandler , isOutputs ));
670+ }
671+
672+ if (true ) {
673+ return flow ;
674+ }
675+
676+ Grid grid = new Grid ()
677+ .debugName (isOutputs ? "output.grid" : "input.grid" );
678+
679+ SlotGroup slotGroup = new SlotGroup (isOutputs ? "output_items" : "input_items" , itemGridWidth , 1 ,
680+ !isOutputs );
681+ int tHeight = singleRow ? itemGridHeight : itemGridHeight + fluidGridHeight ;
682+ int tWidth = itemGridWidth + fluidGridWidth ;
683+ int total = tWidth * tHeight ;
684+ grid .size (tWidth * 18 , tHeight * 18 );
685+
686+ List <IWidget > list = new ArrayList <>();
687+ int diff = (itemGridHeight * itemGridWidth ) - itemInputsCount ;
688+ int fluidDiff = Math .max (tWidth - fluidInputsCount , 0 );
689+ int fluidIndex = 0 , itemIndex = 0 , emptyIndex = 0 ;
690+
691+ for (int i = 0 ; i < total ; i ++) {
692+ IWidget widget = IDrawable .EMPTY .asWidget ().size (18 )
693+ .debugName ("empty.slot." + emptyIndex ++);
694+ if (!isOutputs ) {
695+ if (singleRow ) {
696+ // fluid slot, then item slots
697+ if (i == 0 && fluidIndex < fluidInputsCount ) {
698+ widget = makeFluidSlot (fluidIndex ++, fluidHandler , false );
699+ } else if (itemIndex < itemInputsCount ) {
700+ widget = makeItemSlot (slotGroup , itemIndex ++, itemHandler , false );
701+ }
702+ } else {
703+ if (i > diff && i < itemGridWidth * itemGridHeight ) {
704+ // top left should be empty
705+ widget = makeItemSlot (slotGroup , itemIndex ++, itemHandler , false );
706+ } else if (i >= itemGridWidth * itemGridHeight && i < total - fluidDiff ) {
707+ // bottom left should be empty
708+ widget = makeFluidSlot (fluidIndex ++, fluidHandler , false );
709+ }
710+ }
711+ } else {
712+ if (singleRow ) {
713+ // item slots, then fluid slots
714+ if (i == total - 1 && fluidIndex < fluidInputsCount ) {
715+ widget = makeFluidSlot (fluidIndex ++, fluidHandler , false );
716+ } else if (itemIndex < itemInputsCount ) {
717+ widget = makeItemSlot (slotGroup , itemIndex ++, itemHandler , false );
718+ }
719+ } else {
720+ if (i < (tWidth - diff ) || i >= tWidth ) {
721+ // top right should be empty
722+ widget = makeItemSlot (slotGroup , itemIndex ++, itemHandler , false );
723+ } else if (i >= itemGridWidth * itemGridHeight && i < total - fluidDiff ) {
724+ // bottom right should be empty
725+ widget = makeFluidSlot (fluidIndex ++, fluidHandler , false );
726+ }
727+ }
728+ }
729+ list .add (widget );
730+ }
731+
732+ grid .mapTo (Math .max (itemGridWidth , fluidGridWidth ), list );
733+
734+ return grid ;
735+ }
736+
572737 protected void addInventorySlotGroup (@ NotNull ParentWidget <?> group ,
573738 @ NotNull IItemHandlerModifiable itemHandler ,
574739 @ NotNull FluidTankList fluidHandler , boolean isOutputs , int yOffset ) {
@@ -653,6 +818,7 @@ protected void addInventorySlotGroup(@NotNull ParentWidget<?> group,
653818 protected ItemSlot makeItemSlot (SlotGroup group , int slotIndex , IItemHandlerModifiable itemHandler ,
654819 boolean isOutputs ) {
655820 return new ItemSlot ()
821+ .debugName ("item.slot." + slotIndex + ":" + group .getName ())
656822 .slot (SyncHandlers .itemSlot (itemHandler , slotIndex )
657823 .slotGroup (group )
658824 .accessibility (!isOutputs , true ))
@@ -661,6 +827,7 @@ protected ItemSlot makeItemSlot(SlotGroup group, int slotIndex, IItemHandlerModi
661827
662828 protected GTFluidSlot makeFluidSlot (int slotIndex , FluidTankList fluidHandler , boolean isOutputs ) {
663829 return new GTFluidSlot ()
830+ .debugName ("fluid.slot." + slotIndex )
664831 .syncHandler (GTFluidSlot .sync (fluidHandler .getTankAt (slotIndex ))
665832 .accessibility (true , !isOutputs )
666833 .drawAlwaysFull (true ))
0 commit comments