Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull UI code out of RecipeMap #2271

Merged
merged 5 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ protected ModularUI.Builder createGuiTemplate(EntityPlayer player) {
yOffset = FONT_HEIGHT;

ModularUI.Builder builder;
if (handlesRecipeOutputs) builder = workableRecipeMap.createUITemplate(workable::getProgressPercent,
importItems, exportItems, importFluids, exportFluids, yOffset);
else builder = workableRecipeMap.createUITemplateNoOutputs(workable::getProgressPercent, importItems,
if (handlesRecipeOutputs)
builder = workableRecipeMap.getRecipeMapUI().createUITemplate(workable::getProgressPercent,
importItems, exportItems, importFluids, exportFluids, yOffset);
else builder = workableRecipeMap.getRecipeMapUI().createUITemplateNoOutputs(workable::getProgressPercent,
importItems,
exportItems, importFluids, exportFluids, yOffset);
builder.widget(new LabelWidget(6, 6, getMetaFullName()))
.bindPlayerInventory(player.inventory, GuiTextures.SLOT, yOffset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IActiveOutputSide;
import gregtech.api.capability.IGhostSlotConfigurable;
import gregtech.api.capability.impl.*;
import gregtech.api.capability.impl.EnergyContainerHandler;
import gregtech.api.capability.impl.FluidHandlerProxy;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.capability.impl.GhostCircuitItemStackHandler;
import gregtech.api.capability.impl.ItemHandlerList;
import gregtech.api.capability.impl.ItemHandlerProxy;
import gregtech.api.cover.Cover;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.resources.TextureArea;
import gregtech.api.gui.widgets.*;
import gregtech.api.gui.widgets.CycleButtonWidget;
import gregtech.api.gui.widgets.GhostCircuitSlotWidget;
import gregtech.api.gui.widgets.ImageWidget;
import gregtech.api.gui.widgets.LabelWidget;
import gregtech.api.gui.widgets.SlotWidget;
import gregtech.api.gui.widgets.ToggleButtonWidget;
import gregtech.api.items.itemhandlers.GTItemStackHandler;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.recipes.RecipeMap;
Expand Down Expand Up @@ -478,7 +488,7 @@ protected ModularUI.Builder createGuiTemplate(EntityPlayer player) {
yOffset = FONT_HEIGHT;
}

ModularUI.Builder builder = workableRecipeMap
ModularUI.Builder builder = workableRecipeMap.getRecipeMapUI()
.createUITemplate(workable::getProgressPercent, importItems, exportItems, importFluids, exportFluids,
yOffset)
.widget(new LabelWidget(5, 5, getMetaFullName()))
Expand Down
392 changes: 210 additions & 182 deletions src/main/java/gregtech/api/recipes/RecipeMap.java

Large diffs are not rendered by default.

236 changes: 236 additions & 0 deletions src/main/java/gregtech/api/recipes/RecipeMapBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package gregtech.api.recipes;

import gregtech.api.gui.resources.TextureArea;
import gregtech.api.gui.widgets.ProgressWidget;
import gregtech.api.recipes.ui.RecipeMapUI;

import it.unimi.dsi.fastutil.bytes.Byte2ObjectArrayMap;
import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;

import static gregtech.api.recipes.ui.RecipeMapUI.computeOverlayKey;

public class RecipeMapBuilder<B extends RecipeBuilder<B>> {

private final String unlocalizedName;
private final B defaultRecipeBuilder;

private final Byte2ObjectMap<TextureArea> slotOverlays = new Byte2ObjectArrayMap<>();

private int itemInputs;
private boolean modifyItemInputs = true;
private int itemOutputs;
private boolean modifyItemOutputs = true;
private int fluidInputs;
private boolean modifyFluidInputs = true;
private int fluidOutputs;
private boolean modifyFluidOutputs = true;

private @Nullable TextureArea progressBar;
private @Nullable ProgressWidget.MoveType moveType;

private @Nullable TextureArea specialTexture;
private int @Nullable [] specialTextureLocation;

private Function<@NotNull RecipeMap<?>, @NotNull RecipeMapUI<?>> recipeMapUIFunction = this::buildUI;

/**
* @param unlocalizedName the name of the recipemap
* @param defaultRecipeBuilder the default recipe builder of the recipemap
*/
public RecipeMapBuilder(@NotNull String unlocalizedName, @NotNull B defaultRecipeBuilder) {
this.unlocalizedName = unlocalizedName;
this.defaultRecipeBuilder = defaultRecipeBuilder;
}

/**
* @param itemInputs the amount of item inputs
* @return this
*/
public @NotNull RecipeMapBuilder<B> itemInputs(int itemInputs) {
this.itemInputs = itemInputs;
return this;
}

/**
* @param modifyItemInputs if item input limit modification should be allowed
* @return this
*/
public @NotNull RecipeMapBuilder<B> modifyItemInputs(boolean modifyItemInputs) {
this.modifyItemInputs = modifyItemInputs;
return this;
}

/**
* @param itemOutputs the amount of item outputs
* @return this
*/
public @NotNull RecipeMapBuilder<B> itemOutputs(int itemOutputs) {
this.itemOutputs = itemOutputs;
return this;
}

/**
* @param modifyItemOutputs if item output limit modification should be allowed
* @return this
*/
public @NotNull RecipeMapBuilder<B> modifyItemOutputs(boolean modifyItemOutputs) {
this.modifyItemOutputs = modifyItemOutputs;
return this;
}

/**
* @param fluidInputs the amount of fluid inputs
* @return this
*/
public @NotNull RecipeMapBuilder<B> fluidInputs(int fluidInputs) {
this.fluidInputs = fluidInputs;
return this;
}

/**
* @param modifyFluidInputs if fluid input limit modification should be allowed
* @return this
*/
public @NotNull RecipeMapBuilder<B> modifyFluidInputs(boolean modifyFluidInputs) {
this.modifyFluidInputs = modifyFluidInputs;
return this;
}

/**
* @param fluidOutputs the amount of fluid outputs
* @return this
*/
public @NotNull RecipeMapBuilder<B> fluidOutputs(int fluidOutputs) {
this.fluidOutputs = fluidOutputs;
return this;
}

/**
* @param modifyFluidOutputs if fluid output limit modification should be allowed
* @return this
*/
public @NotNull RecipeMapBuilder<B> modifyFluidOutputs(boolean modifyFluidOutputs) {
this.modifyFluidOutputs = modifyFluidOutputs;
return this;
}

/**
* @param progressBar the progress bar texture to use
* @return this
*/
public @NotNull RecipeMapBuilder<B> progressBar(@Nullable TextureArea progressBar) {
this.progressBar = progressBar;
return this;
}

/**
* @param progressBar the progress bar texture to use
* @param moveType the progress bar move type to use
* @return this
*/
public @NotNull RecipeMapBuilder<B> progressBar(@Nullable TextureArea progressBar,
@Nullable ProgressWidget.MoveType moveType) {
this.progressBar = progressBar;
this.moveType = moveType;
return this;
}

/**
* @param texture the texture to use
* @param isOutput if the slot is an output slot
* @return this
*/
public @NotNull RecipeMapBuilder<B> itemSlotOverlay(@NotNull TextureArea texture, boolean isOutput) {
this.slotOverlays.put(computeOverlayKey(isOutput, false, false), texture);
this.slotOverlays.put(computeOverlayKey(isOutput, false, true), texture);
return this;
}

/**
* @param texture the texture to use
* @param isOutput if the slot is an output slot
* @param isLastSlot if the slot is the last slot
* @return this
*/
public @NotNull RecipeMapBuilder<B> itemSlotOverlay(@NotNull TextureArea texture, boolean isOutput,
boolean isLastSlot) {
this.slotOverlays.put(computeOverlayKey(isOutput, false, isLastSlot), texture);
return this;
}

/**
* @param texture the texture to use
* @param isOutput if the slot is an output slot
* @return this
*/
public @NotNull RecipeMapBuilder<B> fluidSlotOverlay(@NotNull TextureArea texture, boolean isOutput) {
this.slotOverlays.put(computeOverlayKey(isOutput, true, false), texture);
this.slotOverlays.put(computeOverlayKey(isOutput, true, true), texture);
return this;
}

/**
* @param texture the texture to use
* @param isOutput if the slot is an output slot
* @param isLastSlot if the slot is the last slot
* @return this
*/
public @NotNull RecipeMapBuilder<B> fluidSlotOverlay(@NotNull TextureArea texture, boolean isOutput,
boolean isLastSlot) {
this.slotOverlays.put(computeOverlayKey(isOutput, true, isLastSlot), texture);
return this;
}

public @NotNull RecipeMapBuilder<B> specialTexture(@NotNull TextureArea texture, int x, int y, int width,
int height) {
this.specialTexture = texture;
this.specialTextureLocation = new int[] { x, y, width, height };
return this;
}

/**
* @param recipeMapUIFunction the custom function for creating the RecipeMap's ui
* @return this
*/
public @NotNull RecipeMapBuilder<B> ui(@NotNull Function<@NotNull RecipeMap<?>, @NotNull RecipeMapUI<?>> recipeMapUIFunction) {
this.recipeMapUIFunction = recipeMapUIFunction;
return this;
}

/**
* @param recipeMap the recipemap associated with the ui
* @return the RecipeMap's ui
*/
protected @NotNull RecipeMapUI<?> buildUI(@NotNull RecipeMap<?> recipeMap) {
RecipeMapUI<?> ui = new RecipeMapUI<>(recipeMap, modifyItemInputs, modifyItemOutputs, modifyFluidInputs,
modifyFluidOutputs);
if (progressBar != null) {
ui.setProgressBarTexture(progressBar);
}
if (moveType != null) {
ui.setProgressBarMoveType(moveType);
}
if (specialTexture != null && specialTextureLocation != null) {
ui.setSpecialTexture(specialTexture, specialTextureLocation);
}
for (var entry : slotOverlays.byte2ObjectEntrySet()) {
ui.setSlotOverlay(entry.getByteKey(), entry.getValue());
}

return ui;
}

/**
* <strong>Do not call this twice. RecipeMapBuilders are not re-usable.</strong>
*
* @return a new RecipeMap
*/
public @NotNull RecipeMap<B> build() {
return new RecipeMap<>(unlocalizedName, defaultRecipeBuilder, this.recipeMapUIFunction, itemInputs,
itemOutputs, fluidInputs, fluidOutputs);
}
}
Loading
Loading