Skip to content

Commit

Permalink
refactor: move pattern grid rendering per type to different classes
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Aug 7, 2024
1 parent 4aee1da commit 949c334
Show file tree
Hide file tree
Showing 14 changed files with 1,081 additions and 652 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.refinedmods.refinedstorage.common.autocrafting;

import com.refinedmods.refinedstorage.common.Platform;
import com.refinedmods.refinedstorage.common.support.ResourceSlotRendering;
import com.refinedmods.refinedstorage.common.support.TextureIds;
import com.refinedmods.refinedstorage.common.support.amount.AbstractAmountScreen;
import com.refinedmods.refinedstorage.common.support.amount.AmountScreenConfiguration;
Expand Down Expand Up @@ -273,7 +274,7 @@ private static int getOverflowRows(final Alternative alternative) {

@Override
protected void renderResourceSlots(final GuiGraphics graphics) {
tryRenderResourceSlot(graphics, getMenu().getAmountSlot());
ResourceSlotRendering.render(graphics, getMenu().getAmountSlot(), leftPos, topPos);
}

@Override
Expand Down Expand Up @@ -416,7 +417,7 @@ private void renderSlots(final List<AlternativeSlot> slots,
final int mouseY) {
for (final ResourceSlot resourceSlot : slots) {
if (resourceSlot.isActive()) {
tryRenderResourceSlot(graphics, resourceSlot);
ResourceSlotRendering.render(graphics, resourceSlot, leftPos, topPos);
if (isHovering(resourceSlot.x, resourceSlot.y, 16, 16, mouseX, mouseY)
&& canInteractWithResourceSlot(resourceSlot, mouseX, mouseY)) {
renderSlotHighlight(graphics, leftPos + resourceSlot.x, topPos + resourceSlot.y, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.refinedmods.refinedstorage.common.autocrafting;

import com.refinedmods.refinedstorage.common.support.widget.CustomCheckboxWidget;

import java.util.function.Consumer;
import javax.annotation.Nullable;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;

import static com.refinedmods.refinedstorage.common.autocrafting.PatternGridScreen.INSET_PADDING;
import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier;
import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createTranslation;

class CraftingPatternGridRenderer implements PatternGridRenderer {
private static final ResourceLocation CRAFTING = createIdentifier("pattern_grid/crafting");
private static final MutableComponent FUZZY_MODE = createTranslation("gui", "pattern_grid.fuzzy_mode");
private static final MutableComponent FUZZY_MODE_ON_HELP =
createTranslation("gui", "pattern_grid.fuzzy_mode.on.help");
private static final MutableComponent FUZZY_MODE_OFF_HELP =
createTranslation("gui", "pattern_grid.fuzzy_mode.off.help");

@Nullable
private CustomCheckboxWidget fuzzyModeCheckbox;

private final PatternGridContainerMenu menu;
private final int leftPos;
private final int x;
private final int y;

CraftingPatternGridRenderer(final PatternGridContainerMenu menu, final int leftPos, final int x, final int y) {
this.menu = menu;
this.leftPos = leftPos;
this.x = x;
this.y = y;
}

@Override
public void addWidgets(final Consumer<AbstractWidget> widgets, final Consumer<AbstractWidget> renderables) {
this.fuzzyModeCheckbox = createFuzzyModeCheckbox();
renderables.accept(fuzzyModeCheckbox);
}

private CustomCheckboxWidget createFuzzyModeCheckbox() {
final CustomCheckboxWidget checkbox = new CustomCheckboxWidget(
x + INSET_PADDING,
y + INSET_PADDING + 54 + INSET_PADDING - 2,
FUZZY_MODE,
Minecraft.getInstance().font,
menu.isFuzzyMode(),
CustomCheckboxWidget.Size.SMALL
);
checkbox.setOnPressed((c, selected) -> menu.setFuzzyMode(selected));
checkbox.setTooltip(getFuzzyModeTooltip(menu.isFuzzyMode()));
checkbox.visible = isFuzzyModeCheckboxVisible();
return checkbox;
}

private static Tooltip getFuzzyModeTooltip(final boolean fuzzyMode) {
return fuzzyMode ? Tooltip.create(FUZZY_MODE_ON_HELP) : Tooltip.create(FUZZY_MODE_OFF_HELP);
}

@Override
public int getClearButtonX() {
return leftPos + 68;
}

@Override
public int getClearButtonY() {
return y + INSET_PADDING;
}

@Override
public void patternTypeChanged(final PatternType newPatternType) {
if (fuzzyModeCheckbox != null) {
fuzzyModeCheckbox.visible = isFuzzyModeCheckboxVisible();
}
}

private boolean isFuzzyModeCheckboxVisible() {
return menu.getPatternType() == PatternType.CRAFTING;
}

@Override
public void fuzzyModeChanged(final boolean newFuzzyMode) {
if (fuzzyModeCheckbox == null) {
return;
}
fuzzyModeCheckbox.setSelected(newFuzzyMode);
fuzzyModeCheckbox.setTooltip(getFuzzyModeTooltip(newFuzzyMode));
}

@Override
public void renderBackground(final GuiGraphics graphics,
final float partialTicks,
final int mouseX,
final int mouseY) {
graphics.blitSprite(CRAFTING, x + INSET_PADDING, y + INSET_PADDING, 130, 54);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class PatternGridContainerMenu extends AbstractGridContainerMenu {
private static final int SPACING_BETWEEN_PATTERN_INPUT_AND_PATTERN_OUTPUT_SLOTS = 36;
private static final int Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_FIRST_CRAFTING_MATRIX_SLOT = 85;
private static final int Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_FIRST_PROCESSING_MATRIX_SLOT = 76;
private static final int Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_STONECUTTER_SLOT = 63;
private static final int Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_SMITHING_TABLE_SLOTS = 63;
private static final int INDIVIDUAL_PROCESSING_MATRIX_SIZE = 54;

private final RateLimiter allowedAlternativesCacheCheckerRateLimiter = RateLimiter.create(2);
Expand Down Expand Up @@ -296,7 +298,8 @@ private void addProcessingMatrixSlots(final int x,
}

private void addStonecutterSlots(final int playerInventoryY) {
addSlot(new FilterSlot(stonecutterInput, 0, 17, playerInventoryY - 63) {
final int slotY = playerInventoryY - Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_STONECUTTER_SLOT;
addSlot(new FilterSlot(stonecutterInput, 0, 13, slotY) {
@Override
public boolean isActive() {
return getPatternType() == PatternType.STONECUTTER;
Expand All @@ -305,9 +308,10 @@ public boolean isActive() {
}

private void addSmithingTableSlots(final int playerInventoryY) {
final int y = playerInventoryY - Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_SMITHING_TABLE_SLOTS;
for (int i = 0; i < 3; ++i) {
final int ii = i;
addSlot(new FilterSlot(smithingTableMatrix, i, 13 + (i * 18), playerInventoryY - 63) {
addSlot(new FilterSlot(smithingTableMatrix, i, 13 + (i * 18), y) {
@Override
public boolean isActive() {
return getPatternType() == PatternType.SMITHING_TABLE;
Expand All @@ -324,7 +328,7 @@ public boolean mayPlace(final ItemStack stack) {
}
});
}
addSlot(new DisabledSlot(smithingTableResult, 0, 93, playerInventoryY - 63) {
addSlot(new DisabledSlot(smithingTableResult, 0, 93, y) {
@Override
public boolean isActive() {
return getPatternType() == PatternType.SMITHING_TABLE;
Expand Down Expand Up @@ -408,8 +412,8 @@ Set<ResourceLocation> getAllowedAlternatives(final int containerSlot) {
}

interface PatternGridListener {
void patternTypeChanged(PatternType value);
void patternTypeChanged(PatternType newPatternType);

void fuzzyModeChanged(boolean value);
void fuzzyModeChanged(boolean newFuzzyMode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.refinedmods.refinedstorage.common.autocrafting;

import com.refinedmods.refinedstorage.common.support.containermenu.ResourceSlot;

import java.util.function.Consumer;
import javax.annotation.Nullable;

import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.world.inventory.Slot;

interface PatternGridRenderer {
default void addWidgets(Consumer<AbstractWidget> widgets,
Consumer<AbstractWidget> renderables) {
// no op
}

default void tick() {
// no op
}

default void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) {
// no op
}

int getClearButtonX();

int getClearButtonY();

void renderBackground(GuiGraphics graphics,
float partialTicks,
int mouseX,
int mouseY);

default void renderTooltip(Font font,
@Nullable Slot hoveredSlot,
GuiGraphics graphics,
int mouseX,
int mouseY) {
// no op
}

default void renderLabels(GuiGraphics graphics, Font font, int mouseX, int mouseY) {
// no op
}

default boolean mouseClicked(double mouseX, double mouseY, int clickedButton) {
return false;
}

default void mouseMoved(double mouseX, double mouseY) {
// no op
}

default boolean mouseReleased(double mouseX, double mouseY, int button) {
return false;
}

default boolean mouseScrolled(double mouseX, double mouseY, double mouseZ, double delta) {
return false;
}

default void patternTypeChanged(PatternType newPatternType) {
// no op
}

default void fuzzyModeChanged(boolean newFuzzyMode) {
// no op
}

default boolean canInteractWithResourceSlot(ResourceSlot resourceSlot,
double mouseX,
double mouseY) {
return true;
}
}
Loading

0 comments on commit 949c334

Please sign in to comment.