|
| 1 | +package com.gregtechceu.gtceu.api.recipe.gui; |
| 2 | + |
| 3 | +import com.gregtechceu.gtceu.api.GTValues; |
| 4 | +import com.gregtechceu.gtceu.api.capability.recipe.IO; |
| 5 | +import com.gregtechceu.gtceu.api.capability.recipe.ItemRecipeCapability; |
| 6 | +import com.gregtechceu.gtceu.api.mui.base.drawable.IDrawable; |
| 7 | +import com.gregtechceu.gtceu.api.mui.utils.Alignment; |
| 8 | +import com.gregtechceu.gtceu.api.mui.widget.ParentWidget; |
| 9 | +import com.gregtechceu.gtceu.api.mui.widgets.ProgressWidget; |
| 10 | +import com.gregtechceu.gtceu.api.mui.widgets.SlotGroupWidget; |
| 11 | +import com.gregtechceu.gtceu.api.mui.widgets.layout.Column; |
| 12 | +import com.gregtechceu.gtceu.api.mui.widgets.layout.Row; |
| 13 | +import com.gregtechceu.gtceu.common.mui.GTGuiTextures; |
| 14 | + |
| 15 | +import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenHashMap; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +public class GTRecipeViewerUILayout { |
| 21 | + |
| 22 | + public final GTRecipeTypeUILayout layout; |
| 23 | + |
| 24 | + private ParentWidget<? extends ParentWidget<?>> overrideWidget; |
| 25 | + |
| 26 | + public GTRecipeViewerUILayout(GTRecipeTypeUILayout layout) { |
| 27 | + this.layout = layout; |
| 28 | + } |
| 29 | + |
| 30 | + public GTRecipeViewerUILayout overrideWidget(ParentWidget<? extends ParentWidget<?>> overrideWidget) { |
| 31 | + this.overrideWidget = overrideWidget; |
| 32 | + return this; |
| 33 | + } |
| 34 | + |
| 35 | + public ParentWidget<?> getRecipeWidget() { |
| 36 | + if (layout.getRecipeType() != null) { |
| 37 | + var parentWidget = new ParentWidget<>(); |
| 38 | + parentWidget.coverChildren(); |
| 39 | + var slotsRow = new Row(); |
| 40 | + slotsRow.coverChildrenHeight(); |
| 41 | + |
| 42 | + int rowWidthPx = 0; |
| 43 | + |
| 44 | + Map<IO, ParentWidget<?>> colWidgetGroups = new Object2ReferenceOpenHashMap<>(); |
| 45 | + |
| 46 | + int slotLeftShiftPx = 0; |
| 47 | + for (var io : layout.getIOs()) { |
| 48 | + boolean in = io == IO.IN; |
| 49 | + |
| 50 | + var caps = (in ? layout.getRecipeType().maxInputs : layout.getRecipeType().maxOutputs); |
| 51 | + int slotGroupHeightPx = 0; |
| 52 | + |
| 53 | + Column ioColumn = new Column(); |
| 54 | + // ioColumn.coverChildrenWidth(); |
| 55 | + int slotGroupWidthPx = 0; |
| 56 | + |
| 57 | + var widgetGroups = new ArrayList<ParentWidget<?>>(); |
| 58 | + |
| 59 | + for (var recipeCap : caps.keySet()) { |
| 60 | + int maxRecipeTypeSlots = caps.get(recipeCap); |
| 61 | + |
| 62 | + var grid = layout.createGrid(io, recipeCap, 's', GTValues.MAX, Integer.MAX_VALUE); |
| 63 | + |
| 64 | + slotGroupHeightPx += 18 * grid.length; |
| 65 | + |
| 66 | + IDrawable defaultSlotBackground = (recipeCap == ItemRecipeCapability.CAP ? |
| 67 | + GTGuiTextures.SLOT : GTGuiTextures.FLUID_SLOT); |
| 68 | + |
| 69 | + SlotGroupWidget.Builder slotWidgetBuilder = SlotGroupWidget.builder() |
| 70 | + .matrix(grid); |
| 71 | + var overlays = layout.getOverlays(); |
| 72 | + slotWidgetBuilder.key('s', i -> { |
| 73 | + var widget = new IDrawable.DrawableWidget(defaultSlotBackground); |
| 74 | + if (overlays.containsKey(io) && overlays.get(io).containsKey(recipeCap)) { |
| 75 | + widget.overlay(overlays.get(io).get(recipeCap).get(i)); |
| 76 | + } |
| 77 | + return widget; |
| 78 | + }); |
| 79 | + |
| 80 | + // calculate full width of each column |
| 81 | + slotGroupWidthPx = Math.max(slotGroupWidthPx, Math.min(maxRecipeTypeSlots, grid[0].length()) * 18); |
| 82 | + |
| 83 | + widgetGroups.add(slotWidgetBuilder.build() |
| 84 | + .name(recipeCap.name + "_" + io.name()) |
| 85 | + .alignX(io == IO.IN ? Alignment.TopLeft : Alignment.TopRight)); |
| 86 | + } |
| 87 | + |
| 88 | + ioColumn.size(slotGroupWidthPx, slotGroupHeightPx); |
| 89 | + for (var g : widgetGroups) { |
| 90 | + ioColumn.child(g); |
| 91 | + } |
| 92 | + slotLeftShiftPx += (slotGroupWidthPx / 2) * ((io == io.IN) ? -1 : 1); |
| 93 | + |
| 94 | + rowWidthPx += slotGroupWidthPx; |
| 95 | + colWidgetGroups.put(io, ioColumn); |
| 96 | + } |
| 97 | + // 2 px padding plus each half of the progress bar (1) |
| 98 | + slotsRow.childPadding((layout.getProgressSize() / 2) + 2); |
| 99 | + for (var ioColumn : colWidgetGroups.entrySet()) { |
| 100 | + var col = ioColumn.getValue(); |
| 101 | + var io = ioColumn.getKey(); |
| 102 | + slotsRow.child(col.align(io == IO.IN ? Alignment.CenterLeft : Alignment.CenterRight)); |
| 103 | + } |
| 104 | + |
| 105 | + // same padding as (1) + half a slot on each side |
| 106 | + rowWidthPx += layout.getProgressSize() + 4 + 18; |
| 107 | + slotsRow.width(rowWidthPx); |
| 108 | + |
| 109 | + parentWidget.child(slotsRow.left(slotLeftShiftPx)); |
| 110 | + |
| 111 | + parentWidget.child(new ProgressWidget() |
| 112 | + .center() |
| 113 | + .progress(() -> 0.5f) |
| 114 | + .name("progressBar") |
| 115 | + .texture(layout.getProgressBar(), layout.getProgressSize()) |
| 116 | + .size(layout.getProgressSize()) |
| 117 | + .direction(layout.getProgressDirection())); |
| 118 | + return parentWidget; |
| 119 | + } |
| 120 | + return new ParentWidget<>(); |
| 121 | + } |
| 122 | +} |
0 commit comments