From 07b5d7452301eca0e84f77c8b53cf27cea2f57a1 Mon Sep 17 00:00:00 2001
From: Abdiel Kavash <19243993+AbdielKavash@users.noreply.github.com>
Date: Wed, 13 Mar 2024 01:47:30 -0600
Subject: Numeric widget and localized number formatting. (#242)
* bs + dep
* Neutron Sensor UI rework.
* Dynamic TextWidgets and number formatting.
* bs + dep
---
.../tileEntity/GTMetaTileEntity/NeutronSensor.java | 176 ++++++++++++++-------
1 file changed, 115 insertions(+), 61 deletions(-)
(limited to 'src/main/java/goodgenerator/blocks/tileEntity/GTMetaTileEntity')
diff --git a/src/main/java/goodgenerator/blocks/tileEntity/GTMetaTileEntity/NeutronSensor.java b/src/main/java/goodgenerator/blocks/tileEntity/GTMetaTileEntity/NeutronSensor.java
index 0f3023719d..6ca0a358f7 100644
--- a/src/main/java/goodgenerator/blocks/tileEntity/GTMetaTileEntity/NeutronSensor.java
+++ b/src/main/java/goodgenerator/blocks/tileEntity/GTMetaTileEntity/NeutronSensor.java
@@ -1,21 +1,22 @@
package goodgenerator.blocks.tileEntity.GTMetaTileEntity;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.util.ForgeDirection;
-import com.gtnewhorizons.modularui.api.drawable.Text;
import com.gtnewhorizons.modularui.api.math.Alignment;
import com.gtnewhorizons.modularui.api.math.Color;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import com.gtnewhorizons.modularui.common.widget.TextWidget;
-import com.gtnewhorizons.modularui.common.widget.textfield.TextFieldWidget;
+import com.gtnewhorizons.modularui.common.widget.textfield.NumericWidget;
-import goodgenerator.blocks.tileEntity.NeutronActivator;
-import goodgenerator.util.CharExchanger;
+import crazypants.enderio.Log;
import gregtech.api.enums.Textures;
import gregtech.api.gui.modularui.GT_UIInfos;
import gregtech.api.gui.modularui.GT_UITextures;
@@ -25,6 +26,8 @@ import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch;
import gregtech.api.render.TextureFactory;
+import gregtech.api.util.GT_Utility;
+import gregtech.common.gui.modularui.widget.CoverCycleButtonWidget;
public class NeutronSensor extends GT_MetaTileEntity_Hatch {
@@ -32,7 +35,8 @@ public class NeutronSensor extends GT_MetaTileEntity_Hatch {
private static final IIconContainer textureFont_Glow = new Textures.BlockIcons.CustomIcon(
"icons/NeutronSensorFont_GLOW");
- protected String texts = "";
+ protected int threshold = 0;
+ protected boolean inverted = false;
boolean isOn = false;
public NeutronSensor(int aID, String aName, String aNameRegional, int aTier) {
@@ -52,13 +56,91 @@ public class NeutronSensor extends GT_MetaTileEntity_Hatch {
@Override
public void loadNBTData(NBTTagCompound aNBT) {
- texts = aNBT.getString("mBoxContext");
+ if (aNBT.hasKey("mBoxContext")) {
+ // Convert legacy settings
+ setThresholdFromString(aNBT.getString("mBoxContext"));
+ } else {
+ threshold = aNBT.getInteger("mThreshold");
+ inverted = aNBT.getBoolean("mInverted");
+ }
super.loadNBTData(aNBT);
}
+ /**
+ * Used to convert legacy setting where the sensor would use a string like ">200keV" to set its threshold. This
+ * method updates the {@link #threshold} and {@link #inverted} fields based on the input string. The string is
+ * assumed to be in format "(operator)(value)[suffix](ev)", where:
+ *
+ * - (operator) is one of "<", ">", "<=", ">=", "==", or "!="
+ * - (value) is a numeric value (sequence of decimal digits)
+ * - (suffix) is "k", "K", "m", or "M" (optional)
+ * - (ev) is the string "ev", case-insensitive.
+ *
+ * Note that operators "==" and "!=" can not be converted exactly, as the new threshold supports only a binary
+ * comparison (less than, or greater than or equal). Thus "==" is interpreted in the same way as "<=", and "!=" as
+ * ">". This shouldn't be a big problem for real setups, because one should probably not be testing for strict
+ * equality here anyway. The possible reasonable conditions "==0eV" and "!=0eV" will continue working as before.
+ *
+ * @param text String to convert.
+ */
+ private void setThresholdFromString(String text) {
+ Matcher matcher = Pattern.compile("^(<|>|<=|>=|==|!=)([0-9]*)(|k|m)(ev)$", Pattern.CASE_INSENSITIVE)
+ .matcher(text);
+
+ if (!matcher.matches()) {
+ Log.error("Failed to parse Neutron Sensor setting: \"" + text + "\"!");
+ return;
+ }
+
+ String operator = matcher.group(1);
+ String value = matcher.group(2);
+ String suffix = matcher.group(3);
+
+ int newThreshold = Integer.parseInt(value);
+
+ switch (suffix) {
+ case "k":
+ case "K":
+ newThreshold *= 1000;
+ break;
+ case "m":
+ case "M":
+ newThreshold *= 1_000_000;
+ break;
+ }
+
+ switch (operator) {
+ case "<":
+ threshold = newThreshold;
+ inverted = true;
+ break;
+ case ">":
+ threshold = newThreshold + 1;
+ inverted = false;
+ break;
+ case "<=":
+ threshold = newThreshold + 1;
+ inverted = true;
+ break;
+ case ">=":
+ threshold = newThreshold;
+ inverted = false;
+ break;
+ case "==": // Interpret as <= to keep "==0eV" working as before.
+ threshold = newThreshold + 1;
+ inverted = true;
+ break;
+ case "!=": // Interpret as > to keep "!=0eV" working as before.
+ threshold = newThreshold + 1;
+ inverted = false;
+ break;
+ }
+ }
+
@Override
public void saveNBTData(NBTTagCompound aNBT) {
- aNBT.setString("mBoxContext", texts);
+ aNBT.setInteger("mThreshold", threshold);
+ aNBT.setBoolean("mInverted", inverted);
super.saveNBTData(aNBT);
}
@@ -94,20 +176,13 @@ public class NeutronSensor extends GT_MetaTileEntity_Hatch {
return true;
}
- public void setText(String text) {
- texts = text == null ? "" : text;
- }
-
- public String getText() {
- return texts == null ? "" : texts;
- }
-
- public void outputRedstoneSignal() {
- isOn = true;
- }
-
- public void stopOutputRedstoneSignal() {
- isOn = false;
+ /**
+ * Updates redstone output strength based on the eV of the multiblock.
+ *
+ * @param eV Amount of eV to compare.
+ */
+ public void updateRedstoneOutput(int eV) {
+ isOn = (eV >= threshold) ^ inverted;
}
@Override
@@ -164,50 +239,29 @@ public class NeutronSensor extends GT_MetaTileEntity_Hatch {
@Override
public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildContext) {
- TextFieldWidget textField = new TextFieldWidget();
+ final String INVERTED = GT_Utility.trans("INVERTED", "Inverted");
+ final String NORMAL = GT_Utility.trans("NORMAL", "Normal");
+
builder.widget(
- textField.setGetter(this::getText).setSetter(this::setText)
- .setValidator(
- str -> isValidExpression(str) ? str
- : textField.getLastText().size() > 0 ? textField.getLastText().get(0) : "")
- .setFocusOnGuiOpen(true).setTextColor(Color.WHITE.dark(1))
- .setTextAlignment(Alignment.CenterLeft)
- .setBackground(GT_UITextures.BACKGROUND_TEXT_FIELD.withOffset(-1, -1, 2, 2)).setPos(8, 48)
- .setSize(100, 18))
+ new CoverCycleButtonWidget().setToggle(() -> inverted, (val) -> inverted = val)
+ .setTextureGetter(
+ (state) -> state == 1 ? GT_UITextures.OVERLAY_BUTTON_REDSTONE_ON
+ : GT_UITextures.OVERLAY_BUTTON_REDSTONE_OFF)
+ .addTooltip(0, NORMAL).addTooltip(1, INVERTED).setPos(10, 8))
.widget(
- new TextWidget(StatCollector.translateToLocal("gui.NeutronSensor.0"))
+ new TextWidget().setStringSupplier(() -> inverted ? INVERTED : NORMAL)
.setDefaultColor(COLOR_TEXT_GRAY.get()).setTextAlignment(Alignment.CenterLeft)
- .setPos(8, 8))
+ .setPos(28, 12))
.widget(
- new TextWidget(StatCollector.translateToLocal("gui.NeutronSensor.1"))
- .setDefaultColor(COLOR_TEXT_GRAY.get()).setPos(8, 32))
+ new NumericWidget().setBounds(0, 1200000000).setGetter(() -> threshold)
+ .setSetter((value) -> threshold = (int) value).setScrollValues(1000, 1, 1_000_000)
+ .setTextColor(Color.WHITE.dark(1)).setTextAlignment(Alignment.CenterLeft)
+ .setFocusOnGuiOpen(true)
+ .setBackground(GT_UITextures.BACKGROUND_TEXT_FIELD.withOffset(-1, -1, 2, 2))
+ .setPos(10, 28).setSize(77, 12))
.widget(
- TextWidget.dynamicText(
- () -> isValidExpression(textField.getText())
- ? new Text(StatCollector.translateToLocal("gui.NeutronSensor.2"))
- .color(0x077d02)
- : new Text(StatCollector.translateToLocal("gui.NeutronSensor.3"))
- .color(COLOR_TEXT_RED.get()))
- .setSynced(false).setPos(120, 53));
- }
-
- private boolean isValidExpression(String exp) {
- return isValidSuffix(exp) && CharExchanger.isValidCompareExpress(NeutronActivator.rawProcessExp(exp));
- }
-
- private boolean isValidSuffix(String exp) {
- int index;
- index = exp.length() - 1;
- if (index < 0) return false;
- if (exp.charAt(index) != 'V' && exp.charAt(index) != 'v') return false;
- index = exp.length() - 2;
- if (index < 0) return false;
- if (exp.charAt(index) != 'E' && exp.charAt(index) != 'e') return false;
- index = exp.length() - 3;
- if (index < 0) return false;
- return exp.charAt(index) == 'M' || exp.charAt(index) == 'm'
- || exp.charAt(index) == 'K'
- || exp.charAt(index) == 'k'
- || Character.isDigit(exp.charAt(index));
+ new TextWidget(StatCollector.translateToLocal("gui.NeutronSensor.4"))
+ .setDefaultColor(COLOR_TEXT_GRAY.get()).setTextAlignment(Alignment.CenterLeft)
+ .setPos(90, 30));
}
}
--
cgit