Skip to content

Commit

Permalink
feat: the detector now is a proper amount screen
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Apr 1, 2023
1 parent edf7df2 commit 55d0878
Show file tree
Hide file tree
Showing 12 changed files with 412 additions and 181 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- The Detector screen now is a proper amount screen by having increment/decrement buttons and scrollbar support.
- The amount in an amount screen is now colored red if the amount is invalid.

## [2.0.0-milestone.2.9] - 2023-03-31

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public DetectorContainerMenu(final int syncId,
}

private void addSlots(final ResourceFilterContainer config) {
addSlot(new ResourceFilterSlot(config, 0, 107, 20));
addPlayerInventory(player.getInventory(), 8, 55);
addSlot(new ResourceFilterSlot(config, 0, 116, 47));
addPlayerInventory(player.getInventory(), 8, 106);
transferManager.addFilterTransfer(player.getInventory());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,51 @@

import com.refinedmods.refinedstorage2.platform.common.containermenu.detector.DetectorContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.containermenu.property.PropertyTypes;
import com.refinedmods.refinedstorage2.platform.common.screen.amount.AbstractAmountScreen;
import com.refinedmods.refinedstorage2.platform.common.screen.amount.AmountScreenConfiguration;
import com.refinedmods.refinedstorage2.platform.common.screen.amount.DoubleAmountOperations;
import com.refinedmods.refinedstorage2.platform.common.screen.widget.DetectorModeSideButtonWidget;
import com.refinedmods.refinedstorage2.platform.common.screen.widget.FuzzyModeSideButtonWidget;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import javax.annotation.Nullable;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import org.joml.Vector3f;

import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createIdentifier;

public class DetectorScreen extends AbstractBaseScreen<DetectorContainerMenu> {
public class DetectorScreen extends AbstractAmountScreen<DetectorContainerMenu, Double> {
private static final ResourceLocation TEXTURE = createIdentifier("textures/gui/detector.png");
private static final Predicate<String> DECIMAL_PATTERN = Pattern.compile("\\d*\\.?\\d*").asMatchPredicate();
private static final DecimalFormat DECIMAL_FORMAT;

static {
final DecimalFormatSymbols initialAmountSymbols = new DecimalFormatSymbols(Locale.ROOT);
initialAmountSymbols.setDecimalSeparator('.');
DECIMAL_FORMAT = new DecimalFormat("##.###", initialAmountSymbols);
DECIMAL_FORMAT.setGroupingUsed(false);
}

@Nullable
private EditBox amountBox;

public DetectorScreen(final DetectorContainerMenu menu, final Inventory playerInventory, final Component text) {
super(menu, playerInventory, text);
this.inventoryLabelY = 43;
super(
menu,
null,
playerInventory,
text,
AmountScreenConfiguration.AmountScreenConfigurationBuilder.<Double>create()
.withInitialAmount(menu.getAmount())
.withIncrementsTop(1, 10, 64)
.withIncrementsBottom(-1, -10, -64)
.withIncrementsTopStartPosition(new Vector3f(40, 20, 0))
.withIncrementsBottomStartPosition(new Vector3f(40, 70, 0))
.withAmountFieldWidth(59)
.withAmountFieldPosition(new Vector3f(45, 51, 0))
.withActionButtonsEnabled(false)
.withMinAmount(0D)
.withResetAmount(0D)
.build(),
DoubleAmountOperations.INSTANCE
);
this.inventoryLabelY = 94;
this.imageWidth = 176;
this.imageHeight = 137;
this.imageHeight = 188;
}

@Override
protected void init() {
super.init();
if (amountBox == null) {
amountBox = new EditBox(
font,
leftPos + 41,
topPos + 24,
50,
font.lineHeight,
Component.literal("")
);
} else {
amountBox.setX(leftPos + 41);
amountBox.setY(topPos + 24);
}
amountBox.setFocus(false);
amountBox.setCanLoseFocus(true);
amountBox.setBordered(false);
amountBox.setFilter(DECIMAL_PATTERN);
amountBox.setValue(DECIMAL_FORMAT.format(menu.getAmount()));
amountBox.setResponder(value -> {
try {
final double amount = value.trim().isEmpty()
? 0
: Double.parseDouble(value);
menu.changeAmountOnClient(amount);
} catch (final NumberFormatException e) {
// do nothing
}
});
addWidget(amountBox);
addSideButton(new FuzzyModeSideButtonWidget(
getMenu().getProperty(PropertyTypes.FUZZY_MODE),
this::renderComponentTooltip
Expand All @@ -85,26 +58,18 @@ protected void init() {
}

@Override
public void render(final PoseStack poseStack, final int mouseX, final int mouseY, final float partialTicks) {
super.render(poseStack, mouseX, mouseY, partialTicks);
if (amountBox != null) {
amountBox.render(poseStack, mouseX, mouseY, partialTicks);
}
protected void accept(final Double amount) {
getMenu().changeAmountOnClient(amount);
}

@Override
public boolean charTyped(final char unknown1, final int unknown2) {
return (amountBox != null && amountBox.charTyped(unknown1, unknown2)) || super.charTyped(unknown1, unknown2);
}

@Override
public boolean keyPressed(final int key, final int scanCode, final int modifiers) {
return (amountBox != null && amountBox.keyPressed(key, scanCode, modifiers))
|| super.keyPressed(key, scanCode, modifiers);
protected ResourceLocation getTexture() {
return TEXTURE;
}

@Override
protected ResourceLocation getTexture() {
return TEXTURE;
protected void renderLabels(final PoseStack poseStack, final int mouseX, final int mouseY) {
super.renderLabels(poseStack, mouseX, mouseY);
font.draw(poseStack, this.playerInventoryTitle, inventoryLabelX, inventoryLabelY, 4210752);
}
}
Loading

0 comments on commit 55d0878

Please sign in to comment.