Skip to content

Commit

Permalink
feat: crafting preview and amount screens button icons
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Jan 1, 2025

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent e325a5e commit 37495c8
Showing 16 changed files with 111 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
import net.minecraft.world.entity.player.Inventory;

import static com.refinedmods.refinedstorage.common.support.Sprites.ERROR;
import static com.refinedmods.refinedstorage.common.support.Sprites.ERROR_SIZE;
import static com.refinedmods.refinedstorage.common.support.Sprites.ICON_SIZE;
import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier;
import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createTranslation;

@@ -295,10 +295,10 @@ && isHoveringOverItems(mouseX, mouseY)) {
private static void renderItemErrorIcon(final GuiGraphics graphics, final int x, final int y) {
graphics.blitSprite(
ERROR,
x + 73 - ERROR_SIZE - 3,
y + (29 / 2) - (ERROR_SIZE / 2),
ERROR_SIZE,
ERROR_SIZE
x + 73 - ICON_SIZE - 3,
y + (29 / 2) - (ICON_SIZE / 2),
ICON_SIZE,
ICON_SIZE
);
}

Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@

import static com.refinedmods.refinedstorage.common.autocrafting.patterngrid.AlternativesScreen.ALTERNATIVE_HEIGHT;
import static com.refinedmods.refinedstorage.common.autocrafting.patterngrid.AlternativesScreen.ALTERNATIVE_ROW_HEIGHT;
import static com.refinedmods.refinedstorage.common.autocrafting.patterngrid.AlternativesScreen.RESOURCES_PER_ROW;
import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.getTagTranslationKey;

class AlternativeContainerMenu extends AbstractResourceContainerMenu {
@@ -28,7 +29,7 @@ class AlternativeContainerMenu extends AbstractResourceContainerMenu {

AlternativeContainerMenu(final ResourceSlot amountSlot) {
super(null, 0);
this.amountSlot = amountSlot.forAmountScreen(118, 48);
this.amountSlot = amountSlot.forAmountScreen(127, 48);
final List<ResourceTag> tags = amountSlot.getResource() == null
? Collections.emptyList()
: amountSlot.getResource().getTags();
@@ -62,12 +63,12 @@ private void addAlternativeSlots() {
);
for (int j = 0; j < resources.size(); ++j) {
resources.set(j, new ResourceAmount(alternative.getResources().get(j), 1));
final int row = j / 8;
final int col = j % 8;
final int row = j / RESOURCES_PER_ROW;
final int col = j % RESOURCES_PER_ROW;
final int slotX = x + 1 + 1 + col * 18;
final int slotY = y + ALTERNATIVE_ROW_HEIGHT + (row * 18) + 1;
final AlternativeSlot resourceSlot = new AlternativeSlot(resources, j, slotX, slotY);
if (j < 8) {
if (j < RESOURCES_PER_ROW) {
alternative.getMainSlots().add(resourceSlot);
} else {
alternative.getOverflowSlots().add(resourceSlot);
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
public class AlternativesScreen extends AbstractAmountScreen<AlternativeContainerMenu, Double> {
static final int ALTERNATIVE_ROW_HEIGHT = 18;
static final int ALTERNATIVE_HEIGHT = ALTERNATIVE_ROW_HEIGHT * 2;
static final int RESOURCES_PER_ROW = 9;

private static final ResourceLocation TEXTURE = createIdentifier("textures/gui/alternatives.png");
private static final MutableComponent TITLE = createTranslation("gui", "configure_amount");
@@ -58,8 +59,7 @@ public class AlternativesScreen extends AbstractAmountScreen<AlternativeContaine

private static final int ALTERNATIVES_DISPLAYED = 2;
private static final int ROWS_PER_ALTERNATIVE = 2;
private static final int RESOURCES_PER_ROW = 8;
private static final int INSET_WIDTH = 146;
private static final int INSET_WIDTH = 164;
private static final int INSET_HEIGHT = ALTERNATIVE_HEIGHT * ALTERNATIVES_DISPLAYED;

private final ResourceSlot slot;
@@ -85,10 +85,10 @@ public class AlternativesScreen extends AbstractAmountScreen<AlternativeContaine
AmountScreenConfiguration.AmountScreenConfigurationBuilder.<Double>create()
.withInitialAmount(slot.getDisplayAmount())
.withIncrementsTop(1, 10, 64)
.withIncrementsTopStartPosition(new Vector3f(40, 20, 0))
.withIncrementsTopStartPosition(new Vector3f(49, 20, 0))
.withIncrementsBottom(-1, -10, -64)
.withIncrementsBottomStartPosition(new Vector3f(40, 71, 0))
.withAmountFieldPosition(new Vector3f(38, 51, 0))
.withIncrementsBottomStartPosition(new Vector3f(49, 71, 0))
.withAmountFieldPosition(new Vector3f(47, 51, 0))
.withActionButtonsStartPosition(new Vector3f(7, 199, 0))
.withHorizontalActionButtons(true)
.withMinAmount(1D)
@@ -98,7 +98,7 @@ public class AlternativesScreen extends AbstractAmountScreen<AlternativeContaine
DoubleAmountOperations.INSTANCE
);
this.slot = slot;
this.imageWidth = 175;
this.imageWidth = 193;
this.imageHeight = 226;
this.initialAllowedAlternativeIds = allowedAlternativeIds;
}
@@ -113,7 +113,7 @@ protected void init() {
addWidgetsForAlternative(i, x);
}
scrollbar = new ScrollbarWidget(
leftPos + 155,
leftPos + 173,
topPos + 122,
ScrollbarWidget.Type.NORMAL,
INSET_HEIGHT
@@ -130,7 +130,7 @@ protected void init() {
font,
leftPos + 24,
topPos + 109,
144 - 6,
162 - 6,
font.lineHeight,
Component.empty()
);
@@ -169,7 +169,7 @@ private void addWidgetsForAlternative(final int idx, final int x) {
final CheckboxWidget alternativeCheckbox = new CheckboxWidget(
x + 2,
y + (ALTERNATIVE_ROW_HEIGHT / 2) - (9 / 2),
144 - 16 - 1 - 4,
164 - 2 - 16 - 1 - 4,
hasTranslation ? Component.translatable(alternative.getTranslationKey()) : id,
font,
initialAllowedAlternativeIds.contains(alternative.getId()),
@@ -263,7 +263,7 @@ private void updateAlternativeSlots(final List<AlternativeSlot> slots,
final int rowOffset,
final boolean visible) {
for (int i = 0; i < slots.size(); i++) {
final int row = (i / 8) + rowOffset;
final int row = (i / RESOURCES_PER_ROW) + rowOffset;
final AlternativeSlot resourceSlot = slots.get(i);
Platform.INSTANCE.setSlotY(
resourceSlot,
@@ -491,9 +491,9 @@ public boolean keyPressed(final int key, final int scanCode, final int modifiers

private boolean isOverAlternativesArea(final double x, final double y) {
return x >= leftPos + 7
&& (x < leftPos + 7 + 161)
&& (x < leftPos + 7 + 179)
&& y >= topPos + 121
&& (y < topPos + 121 + 88);
&& (y < topPos + 121 + 74);
}

@Override
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
import com.refinedmods.refinedstorage.common.support.amount.AbstractAmountScreen;
import com.refinedmods.refinedstorage.common.support.amount.AmountScreenConfiguration;
import com.refinedmods.refinedstorage.common.support.amount.DoubleAmountOperations;
import com.refinedmods.refinedstorage.common.support.amount.IconButton;
import com.refinedmods.refinedstorage.common.support.tooltip.HelpClientTooltipComponent;
import com.refinedmods.refinedstorage.common.support.tooltip.SmallText;
import com.refinedmods.refinedstorage.common.support.widget.CheckboxWidget;
@@ -200,6 +201,12 @@ protected void init() {
));
}

@Nullable
@Override
protected IconButton.Icon getConfirmButtonIcon() {
return null;
}

private void requestMaxAmount(final Button button) {
button.active = false;
getMenu().requestMaxAmount();
@@ -276,7 +283,9 @@ private void setPreview(@Nullable final Preview preview) {
previewItemsScrollbar.setEnabled(rows > 0);
confirmButton.setMessage(START);
confirmButton.active = preview.type() == PreviewType.SUCCESS;
confirmButton.setError(preview.type() != PreviewType.SUCCESS);
confirmButton.setIcon(preview.type() == PreviewType.SUCCESS
? IconButton.Icon.START
: IconButton.Icon.ERROR);
confirmButton.setTooltip(preview.type() == PreviewType.MISSING_RESOURCES
? Tooltip.create(MISSING_RESOURCES)
: null);
@@ -603,7 +612,7 @@ private void setPending() {
return;
}
confirmButton.active = false;
confirmButton.setError(false);
confirmButton.setIcon(null);
confirmButton.setTooltip(null);
confirmButton.setMessage(PENDING);
}
@@ -613,7 +622,7 @@ private void setStartDisabled() {
return;
}
confirmButton.active = false;
confirmButton.setError(false);
confirmButton.setIcon(null);
confirmButton.setTooltip(null);
confirmButton.setMessage(START);
}
Original file line number Diff line number Diff line change
@@ -11,8 +11,12 @@ public final class Sprites {
public static final int LIGHT_ARROW_HEIGHT = 15;
public static final ResourceLocation WARNING = createIdentifier("warning");
public static final int WARNING_SIZE = 10;
public static final int ERROR_SIZE = 12;
public static final int ICON_SIZE = 12;
public static final ResourceLocation ERROR = createIdentifier("error");
public static final ResourceLocation START = createIdentifier("start");
public static final ResourceLocation CANCEL = createIdentifier("cancel");
public static final ResourceLocation RESET = createIdentifier("reset");
public static final ResourceLocation SET = createIdentifier("set");

private Sprites() {
}
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
import org.joml.Vector3f;
import org.lwjgl.glfw.GLFW;

import static com.refinedmods.refinedstorage.common.support.Sprites.ERROR_SIZE;
import static com.refinedmods.refinedstorage.common.support.Sprites.ICON_SIZE;
import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createTranslation;

public abstract class AbstractAmountScreen<T extends AbstractContainerMenu, N extends Number>
@@ -30,11 +30,11 @@ public abstract class AbstractAmountScreen<T extends AbstractContainerMenu, N ex

private static final int INCREMENT_BUTTON_WIDTH = 30;
private static final int ACTION_BUTTON_HEIGHT = 20;
private static final int ACTION_BUTTON_WIDTH = 50;
private static final int ACTION_BUTTON_WIDTH = 58;
private static final int ACTION_BUTTON_SPACING = 20;

@Nullable
protected ConfirmButton confirmButton;
protected IconButton confirmButton;
@Nullable
protected EditBox amountField;

@@ -83,36 +83,55 @@ private void addActionButtons() {

private Button addResetButton(final int x, final int y) {
final int width = configuration.isHorizontalActionButtons()
? font.width(RESET_TEXT) + ACTION_BUTTON_SPACING
? font.width(RESET_TEXT) + ACTION_BUTTON_SPACING + ICON_SIZE
: ACTION_BUTTON_WIDTH;
return addRenderableWidget(Button.builder(RESET_TEXT, btn -> reset())
.pos(leftPos + x, topPos + y)
.size(width, ACTION_BUTTON_HEIGHT)
.build());
final IconButton button = new IconButton(
leftPos + x,
topPos + y,
width,
ACTION_BUTTON_HEIGHT,
RESET_TEXT,
btn -> reset()
);
button.setIcon(IconButton.Icon.RESET);
return addRenderableWidget(button);
}

private void addConfirmButton(final int x, final int y) {
final int width = configuration.isHorizontalActionButtons()
? font.width(configuration.getConfirmButtonText()) + ACTION_BUTTON_SPACING + ERROR_SIZE
? font.width(configuration.getConfirmButtonText()) + ACTION_BUTTON_SPACING + ICON_SIZE
: ACTION_BUTTON_WIDTH;
confirmButton = addRenderableWidget(new ConfirmButton(
final IconButton button = new IconButton(
leftPos + x,
topPos + y,
width,
ACTION_BUTTON_HEIGHT,
configuration.getConfirmButtonText(),
btn -> tryConfirmAndCloseToParent()
));
);
button.setIcon(getConfirmButtonIcon());
confirmButton = addRenderableWidget(button);
}

@Nullable
protected IconButton.Icon getConfirmButtonIcon() {
return IconButton.Icon.SET;
}

private Button addCancelButton(final int x, final int y) {
final int width = configuration.isHorizontalActionButtons()
? font.width(CANCEL_TEXT) + ACTION_BUTTON_SPACING
? font.width(CANCEL_TEXT) + ACTION_BUTTON_SPACING + ICON_SIZE
: ACTION_BUTTON_WIDTH;
return addRenderableWidget(Button.builder(CANCEL_TEXT, btn -> close())
.pos(leftPos + x, topPos + y)
.size(width, ACTION_BUTTON_HEIGHT)
.build());
final IconButton button = new IconButton(
leftPos + x,
topPos + y,
width,
ACTION_BUTTON_HEIGHT,
CANCEL_TEXT,
btn -> close()
);
button.setIcon(IconButton.Icon.CANCEL);
return addRenderableWidget(button);
}

private void addAmountField() {
@@ -157,7 +176,7 @@ protected void onAmountFieldChanged() {
final boolean valid = getAndValidateAmount().isPresent();
if (confirmButton != null) {
confirmButton.active = valid;
confirmButton.setError(!valid);
confirmButton.setIcon(valid ? getConfirmButtonIcon() : IconButton.Icon.ERROR);
} else {
tryConfirm();
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.refinedmods.refinedstorage.common.support.amount;

import com.refinedmods.refinedstorage.common.support.Sprites;

import javax.annotation.Nullable;

import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import static com.refinedmods.refinedstorage.common.support.Sprites.ERROR;
import static com.refinedmods.refinedstorage.common.support.Sprites.ERROR_SIZE;
import static com.refinedmods.refinedstorage.common.support.Sprites.ICON_SIZE;

public class ConfirmButton extends Button {
private boolean error;
public class IconButton extends Button {
@Nullable
private Icon icon;

public ConfirmButton(final int x,
final int y,
final int width,
final int height,
final Component message,
final OnPress onPress) {
public IconButton(final int x,
final int y,
final int width,
final int height,
final Component message,
final OnPress onPress) {
super(x, y, width, height, message, onPress, DEFAULT_NARRATION);
}

@@ -26,8 +31,8 @@ protected void renderWidget(final GuiGraphics graphics,
final int mouseY,
final float partialTick) {
super.renderWidget(graphics, mouseX, mouseY, partialTick);
if (error) {
graphics.blitSprite(ERROR, getX() + 4, getY() + 4, ERROR_SIZE, ERROR_SIZE);
if (icon != null) {
graphics.blitSprite(icon.sprite, getX() + 4, getY() + 4, ICON_SIZE, ICON_SIZE);
}
}

@@ -36,13 +41,27 @@ protected void renderScrollingString(final GuiGraphics graphics,
final Font font,
final int width,
final int color) {
final int offset = error ? (ERROR_SIZE - 6) : 0;
final int offset = icon != null ? (ICON_SIZE - 6) : 0;
final int start = offset + getX() + width;
final int end = offset + getX() + getWidth() - width;
renderScrollingString(graphics, font, getMessage(), start, getY(), end, getY() + getHeight(), color);
}

public void setError(final boolean error) {
this.error = error;
public void setIcon(@Nullable final Icon icon) {
this.icon = icon;
}

public enum Icon {
ERROR(Sprites.ERROR),
START(Sprites.START),
CANCEL(Sprites.CANCEL),
RESET(Sprites.RESET),
SET(Sprites.SET);

private final ResourceLocation sprite;

Icon(final ResourceLocation sprite) {
this.sprite = sprite;
}
}
}
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ public PriorityScreen(final MutableComponent title,
IntegerAmountOperations.INSTANCE
);
this.priorityChanged = priorityChanged;
this.imageWidth = 164;
this.imageWidth = 172;
this.imageHeight = 92;
}

Loading

0 comments on commit 37495c8

Please sign in to comment.