Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an Energy Textbox Widget #364

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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<Integer> maxEnergy;

public EnergyTextboxWidget(Supplier<Integer> 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);
}

//Input only accepts digits (0, 1, ..., 9)
@Override
public boolean charTyped(char codePoint, int modifiers) {

if (codePoint >= 48 && codePoint <= 57) {
int cursorPos = getCursorPosition() + 1;
boolean res = super.charTyped(codePoint, modifiers);
setValue(formatEnergy(getValue()));
moveCursorTo(cursorPos);

return res;
}

return false;
Trytoon marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
boolean res = super.keyPressed(keyCode, scanCode, modifiers);

if (keyCode == 259) {
int cursorPos = getCursorPosition();
setValue(formatEnergy(getValue()));

if (getValue().equals("0")) {
moveCursorTo(1);
} else {
moveCursorTo(cursorPos);
}
}

return res;
}

@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);

//Clear content on right click
if (button == 1) {
setValue("0");
moveCursorTo(1);
}

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be swapped to Locale.ROOT imo :)

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;
}
}
}
Loading