Skip to content
This repository has been archived by the owner on May 25, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'AbdielKavash/NumericWidget' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Dream-Master committed Mar 12, 2024
2 parents 4f47324 + 131381f commit 4181be2
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 218 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,14 +26,17 @@
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 {

private static final IIconContainer textureFont = new Textures.BlockIcons.CustomIcon("icons/NeutronSensorFont");
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) {
Expand All @@ -52,13 +56,91 @@ public String[] getDescription() {

@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:
* <ul>
* <li>(operator) is one of "<", ">", "<=", ">=", "==", or "!="</li>
* <li>(value) is a numeric value (sequence of decimal digits)</li>
* <li>(suffix) is "k", "K", "m", or "M" (optional)</li>
* <li>(ev) is the string "ev", case-insensitive.</li>
* </ul>
* 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);
}

Expand Down Expand Up @@ -94,20 +176,13 @@ public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlaye
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
Expand Down Expand Up @@ -164,50 +239,29 @@ public boolean useModularUI() {

@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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.gtnewhorizon.structurelib.structure.IStructureDefinition;
import com.gtnewhorizon.structurelib.structure.ISurvivalBuildEnvironment;
import com.gtnewhorizon.structurelib.structure.StructureDefinition;
import com.gtnewhorizons.modularui.api.NumberFormatMUI;
import com.gtnewhorizons.modularui.common.widget.DynamicPositionedColumn;
import com.gtnewhorizons.modularui.common.widget.FakeSyncWidget;
import com.gtnewhorizons.modularui.common.widget.SlotWidget;
Expand All @@ -34,7 +35,6 @@
import goodgenerator.blocks.tileEntity.GTMetaTileEntity.NeutronSensor;
import goodgenerator.blocks.tileEntity.base.GT_MetaTileEntity_TooltipMultiBlockBase_EM;
import goodgenerator.loader.Loaders;
import goodgenerator.util.CharExchanger;
import goodgenerator.util.DescTextLocalization;
import goodgenerator.util.ItemRefer;
import gregtech.api.GregTech_API;
Expand Down Expand Up @@ -69,6 +69,7 @@ public class NeutronActivator extends GT_MetaTileEntity_TooltipMultiBlockBase_EM
protected int height = 0;
protected int eV = 0, mCeil = 0, mFloor = 0;
private GT_Recipe lastRecipe;
protected static final NumberFormatMUI numberFormat = new NumberFormatMUI();
final XSTR R = new XSTR();

private static final IIconContainer textureFontOn = new Textures.BlockIcons.CustomIcon("icons/NeutronActivator_On");
Expand Down Expand Up @@ -346,12 +347,7 @@ public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
if (this.eV > maxNeutronKineticEnergy()) doExplosion(4 * 32);

for (NeutronSensor tHatch : mNeutronSensor) {
String tText = tHatch.getText();
if (CharExchanger.isValidCompareExpress(rawProcessExp(tText))) {
if (CharExchanger.compareExpression(rawProcessExp(tText), eV)) {
tHatch.outputRedstoneSignal();
} else tHatch.stopOutputRedstoneSignal();
}
tHatch.updateRedstoneOutput(this.eV);
}

if (mProgresstime < mMaxProgresstime && (eV > mCeil || eV < mFloor)) {
Expand All @@ -362,26 +358,6 @@ public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
}
}

public static String rawProcessExp(String exp) {
StringBuilder ret = new StringBuilder();
for (char c : exp.toCharArray()) {
if (exp.length() - ret.length() == 3) {
if (Character.isDigit(c)) ret.append(c);
else {
if (c == 'K' || c == 'k') {
ret.append("000");
}
if (c == 'M' || c == 'm') {
ret.append("000000");
}
}
break;
}
ret.append(c);
}
return ret.toString();
}

@Override
public void construct(ItemStack stackSize, boolean hintsOnly) {
structureBuild_EM(NA_BOTTOM, 2, 0, 0, stackSize, hintsOnly);
Expand Down Expand Up @@ -470,25 +446,12 @@ protected void drawTexts(DynamicPositionedColumn screenElements, SlotWidget inve
new TextWidget(StatCollector.translateToLocal("gui.NeutronActivator.0"))
.setDefaultColor(COLOR_TEXT_WHITE.get()))
.widget(
TextWidget.dynamicString(() -> processNumber(eV) + "eV").setSynced(false)
new TextWidget().setStringSupplier(() -> numberFormat.formatWithSuffix(eV) + "eV")
.setDefaultColor(COLOR_TEXT_WHITE.get())
.setEnabled(widget -> getBaseMetaTileEntity().getErrorDisplayID() == 0))
.widget(new FakeSyncWidget.IntegerSyncer(() -> eV, val -> eV = val));
}

private String processNumber(int num) {
float num2;
num2 = ((float) num) / 1000F;
if (num2 <= 0) {
return String.format("%d", num);
}
if (num2 < 1000.0) {
return String.format("%.1fK", num2);
}
num2 /= 1000F;
return String.format("%.1fM", num2);
}

private enum NeutronHatchElement implements IHatchElement<NeutronActivator> {

NeutronSensor(NeutronActivator::addAcceleratorAndSensor, NeutronSensor.class) {
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/goodgenerator/blocks/tileEntity/YottaFluidTank.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.gtnewhorizon.structurelib.structure.IStructureElement;
import com.gtnewhorizon.structurelib.structure.ISurvivalBuildEnvironment;
import com.gtnewhorizon.structurelib.structure.StructureDefinition;
import com.gtnewhorizons.modularui.api.NumberFormatMUI;
import com.gtnewhorizons.modularui.api.drawable.IDrawable;
import com.gtnewhorizons.modularui.api.drawable.UITexture;
import com.gtnewhorizons.modularui.common.widget.ButtonWidget;
Expand Down Expand Up @@ -82,6 +83,7 @@ public class YottaFluidTank extends GT_MetaTileEntity_TooltipMultiBlockBase_EM
protected final String YOTTANK_BOTTOM = mName + "buttom";
protected final String YOTTANK_MID = mName + "mid";
protected final String YOTTANK_TOP = mName + "top";
protected final NumberFormatMUI numberFormat = new NumberFormatMUI();

protected boolean voidExcessEnabled = false;

Expand Down Expand Up @@ -523,34 +525,34 @@ protected void drawTexts(DynamicPositionedColumn screenElements, SlotWidget inve

screenElements
.widget(
TextWidget
.dynamicString(
new TextWidget()
.setStringSupplier(
() -> StatCollector.translateToLocal("gui.YOTTank.0") + " "
+ CharExchanger.formatNumber(mStorage.toString())
+ numberFormat.format(mStorage)
+ " L")
.setSynced(false).setDefaultColor(COLOR_TEXT_WHITE.get())
.setDefaultColor(COLOR_TEXT_WHITE.get())
.setEnabled(widget -> getBaseMetaTileEntity().getErrorDisplayID() == 0))
.widget(new FakeSyncWidget.BigIntegerSyncer(() -> mStorage, val -> mStorage = val))
.widget(
TextWidget
.dynamicString(
new TextWidget()
.setStringSupplier(
() -> StatCollector.translateToLocal("gui.YOTTank.1") + " " + getFluidName())
.setSynced(false).setDefaultColor(COLOR_TEXT_WHITE.get())
.setDefaultColor(COLOR_TEXT_WHITE.get())
.setEnabled(widget -> getBaseMetaTileEntity().getErrorDisplayID() == 0))
.widget(new FakeSyncWidget.StringSyncer(() -> mFluidName, val -> mFluidName = val))
.widget(
TextWidget
.dynamicString(
new TextWidget()
.setStringSupplier(
() -> StatCollector.translateToLocal("gui.YOTTank.2") + " "
+ CharExchanger.formatNumber(mStorageCurrent.toString())
+ numberFormat.format(mStorageCurrent)
+ " L")
.setSynced(false).setDefaultColor(COLOR_TEXT_WHITE.get())
.setDefaultColor(COLOR_TEXT_WHITE.get())
.setEnabled(widget -> getBaseMetaTileEntity().getErrorDisplayID() == 0))
.widget(new FakeSyncWidget.BigIntegerSyncer(() -> mStorageCurrent, val -> mStorageCurrent = val))
.widget(
TextWidget.dynamicString(
new TextWidget().setStringSupplier(
() -> StatCollector.translateToLocal("gui.YOTTank.3") + " " + getLockedFluidName())
.setSynced(false).setDefaultColor(COLOR_TEXT_WHITE.get())
.setDefaultColor(COLOR_TEXT_WHITE.get())
.setEnabled(widget -> getBaseMetaTileEntity().getErrorDisplayID() == 0))
.widget(new FakeSyncWidget.StringSyncer(() -> mLockedFluidName, val -> mLockedFluidName = val))
.widget(new FakeSyncWidget.BooleanSyncer(() -> isFluidLocked, val -> isFluidLocked = val))
Expand Down
Loading

0 comments on commit 4181be2

Please sign in to comment.