From 359671053ad0214fb5c476cf07bda2b71a5839ca Mon Sep 17 00:00:00 2001 From: Trytoon Date: Thu, 27 Jul 2023 18:03:26 +0200 Subject: [PATCH 1/2] Create EnergyTextboxWidget.java A Textbox that only accepts numbers and format with comma separator in real time --- .../gui/widget/EnergyTextboxWidget.java | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/machines/java/com/enderio/machines/client/gui/widget/EnergyTextboxWidget.java diff --git a/src/machines/java/com/enderio/machines/client/gui/widget/EnergyTextboxWidget.java b/src/machines/java/com/enderio/machines/client/gui/widget/EnergyTextboxWidget.java new file mode 100644 index 0000000000..bc5e4b74f4 --- /dev/null +++ b/src/machines/java/com/enderio/machines/client/gui/widget/EnergyTextboxWidget.java @@ -0,0 +1,104 @@ +package com.enderio.machines.client.gui.widget; + +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.components.EditBox; +import net.minecraft.network.chat.Component; + +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Locale; +import java.util.function.Supplier; + +public class EnergyTextboxWidget extends EditBox { + private final Supplier maxEnergy; + + public EnergyTextboxWidget(Supplier maxEnergy, Font font, int x, int y, int width, int height, Component message) { + super(font, x, y, width, height, message); + this.maxEnergy = maxEnergy; + setCanLoseFocus(true); + } + + @Override + public void setCanLoseFocus(boolean canLoseFocus) { + super.setCanLoseFocus(canLoseFocus); + } + + //Input only accepts numerals + @Override + public boolean charTyped(char codePoint, int modifiers) { + + if (codePoint >= 48 && codePoint <= 57) { + int cursorPos = getCursorPosition() + 1; + super.charTyped(codePoint, modifiers); + setValue(formatEnergy(getValue())); + moveCursorTo(cursorPos); + } + + return false; + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + super.keyPressed(keyCode, scanCode, modifiers); + + if (keyCode == 259) { + int cursorPos = getCursorPosition(); + setValue(formatEnergy(getValue())); + + if (getValue().equals("0")) { + moveCursorTo(1); + } else { + moveCursorTo(cursorPos); + } + } + + return false; + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + + if (mouseX >= getX() && mouseX <= getX() + width && mouseY >= getY() && mouseY <= getY() + height) { + super.mouseClicked(mouseX, mouseY, button); + return true; + } else { + setFocused(false); + return false; + } + } + + public String formatEnergy(String value) { + + if (value.isEmpty()) { + return "0"; + } + + int energy = 0; + value = value.replace(",", ""); + + try { + energy = Integer.parseInt(value); + energy = Math.min(energy, maxEnergy.get()); + } catch(Exception e) { + energy = 0; + } + + try { + DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(Locale.US); + decimalFormat.applyPattern("#,###"); + return decimalFormat.format(energy); + } catch (NumberFormatException e) { + return "0"; + } + } + + public int getInteger() { + String integer = getValue().replace(",", ""); + + try { + return Integer.parseInt(integer); + } catch(Exception e) { + return 0; + } + } +} From ec2700bfd3ab75d91900f696be41d789fde7336d Mon Sep 17 00:00:00 2001 From: Trytoon Date: Mon, 31 Jul 2023 16:04:58 +0200 Subject: [PATCH 2/2] Return correct boolean values + reset to 0 on right click --- .../gui/widget/EnergyTextboxWidget.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/machines/java/com/enderio/machines/client/gui/widget/EnergyTextboxWidget.java b/src/machines/java/com/enderio/machines/client/gui/widget/EnergyTextboxWidget.java index bc5e4b74f4..91552f9edd 100644 --- a/src/machines/java/com/enderio/machines/client/gui/widget/EnergyTextboxWidget.java +++ b/src/machines/java/com/enderio/machines/client/gui/widget/EnergyTextboxWidget.java @@ -17,21 +17,18 @@ public EnergyTextboxWidget(Supplier maxEnergy, Font font, int x, int y, this.maxEnergy = maxEnergy; setCanLoseFocus(true); } - - @Override - public void setCanLoseFocus(boolean canLoseFocus) { - super.setCanLoseFocus(canLoseFocus); - } - //Input only accepts numerals + //Input only accepts digits (0, 1, ..., 9) @Override public boolean charTyped(char codePoint, int modifiers) { if (codePoint >= 48 && codePoint <= 57) { int cursorPos = getCursorPosition() + 1; - super.charTyped(codePoint, modifiers); + boolean res = super.charTyped(codePoint, modifiers); setValue(formatEnergy(getValue())); moveCursorTo(cursorPos); + + return res; } return false; @@ -39,7 +36,7 @@ public boolean charTyped(char codePoint, int modifiers) { @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - super.keyPressed(keyCode, scanCode, modifiers); + boolean res = super.keyPressed(keyCode, scanCode, modifiers); if (keyCode == 259) { int cursorPos = getCursorPosition(); @@ -52,7 +49,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) { } } - return false; + return res; } @Override @@ -60,6 +57,13 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) { if (mouseX >= getX() && mouseX <= getX() + width && mouseY >= getY() && mouseY <= getY() + height) { super.mouseClicked(mouseX, mouseY, button); + + //Clear content on right click + if (button == 1) { + setValue("0"); + moveCursorTo(1); + } + return true; } else { setFocused(false);