Skip to content

Commit

Permalink
Change OptionIntegerField Width
Browse files Browse the repository at this point in the history
  • Loading branch information
slprime committed Dec 20, 2024
1 parent 7ac3e32 commit c824d26
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
16 changes: 12 additions & 4 deletions src/main/java/codechicken/nei/NEIClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public boolean onClick(int button) {

tag.getTag("inventory.history.historyColor").setComment("Color of the history area display")
.getHexValue(0xee555555);
API.addOption(new OptionIntegerField("inventory.history.historyColor"));
API.addOption(new OptionIntegerField("inventory.history.historyColor", 0, OptionIntegerField.UNSIGNED_INT_MAX));

tag.getTag("inventory.history.useRows").setComment("Rows used in historical areas").getIntValue(2);
API.addOption(new OptionIntegerField("inventory.history.useRows", 1, 5));
Expand All @@ -267,11 +267,19 @@ public boolean onClick(int button) {

tag.getTag("inventory.collapsibleItems.expandedColor")
.setComment("Color of the collapsible item expanded state").getHexValue(0x335555EE);
API.addOption(new OptionIntegerField("inventory.collapsibleItems.expandedColor"));
API.addOption(
new OptionIntegerField(
"inventory.collapsibleItems.expandedColor",
0,
OptionIntegerField.UNSIGNED_INT_MAX));

tag.getTag("inventory.collapsibleItems.collapsedColor")
.setComment("Color of the collapsible item collapsed state").getHexValue(0x335555EE);
API.addOption(new OptionIntegerField("inventory.collapsibleItems.collapsedColor"));
API.addOption(
new OptionIntegerField(
"inventory.collapsibleItems.collapsedColor",
0,
OptionIntegerField.UNSIGNED_INT_MAX));

API.addOption(
new OptionButton(
Expand Down Expand Up @@ -309,7 +317,7 @@ public boolean onClick(int button) {
API.addOption(new OptionToggleButton("inventory.itemzoom.showName", true));

tag.getTag("inventory.itemzoom.nameColor").getHexValue(0xFFFFFFFF);
API.addOption(new OptionIntegerField("inventory.itemzoom.nameColor"));
API.addOption(new OptionIntegerField("inventory.itemzoom.nameColor", 0, OptionIntegerField.UNSIGNED_INT_MAX));

tag.getTag("inventory.itemIDs").getIntValue(1);
API.addOption(new OptionCycled("inventory.itemIDs", 3, true));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/codechicken/nei/NEIClientUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public static void setHourForward(int hour) {
}

public static void sendCommand(String command, Object... args) {
if (command.length() == 0) return;
if (command.isEmpty()) return;

NumberFormat numberformat = NumberFormat.getIntegerInstance();
numberformat.setGroupingUsed(false);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/codechicken/nei/NEIServerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private static void loadBannedItems() {
FileReader r = new FileReader(file);
int line = 0;
for (String s : IOUtils.readLines(r)) {
if (s.charAt(0) == '#' || s.trim().length() == 0) continue;
if (s.charAt(0) == '#' || s.trim().isEmpty()) continue;
int delim = s.lastIndexOf('=');
if (delim < 0) {
logger.error("line " + line + ": Missing =");
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/codechicken/nei/config/OptionIntegerField.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package codechicken.nei.config;

import codechicken.lib.gui.GuiDraw;

public class OptionIntegerField extends OptionTextField {

public final int min;
public final int max;
public static final long UNSIGNED_INT_MAX = 0xFFFFFFFFL;

public final long min;
public final long max;

public OptionIntegerField(String name, int min, int max) {
public OptionIntegerField(String name, long min, long max) {
super(name);
this.min = min;
this.max = max;
Expand All @@ -15,13 +19,23 @@ public OptionIntegerField(String name) {
this(name, Integer.MIN_VALUE, Integer.MAX_VALUE);
}

@Override
protected int getMaxInputWidth() {

if (renderTag().getValue().startsWith("0x")) {
return GuiDraw.getStringWidth("0x" + Long.toHexString(this.max).toUpperCase()) + 32;
}

return GuiDraw.getStringWidth(String.valueOf(this.max)) + 32;
}

@Override
public boolean isValidInput(String s) {
if (s.length() == 0) return true;
if (s.isEmpty()) return true;

try {
if (s.startsWith("0x")) {
Long.parseLong(s.substring(2), 16);
Long.parseUnsignedLong(s.substring(2), 16);
} else {
Integer.parseInt(s);
}
Expand All @@ -33,15 +47,15 @@ public boolean isValidInput(String s) {

@Override
public boolean isValidValue(String s) {
if (s.length() == 0 || !isValidInput(s)) return false;
int i = 0;
if (s.isEmpty() || !isValidInput(s)) return false;
long i = 0;

if (s.startsWith("0x")) {
i = (int) Long.parseLong(s.substring(2), 16);
i = Long.parseUnsignedLong(s.substring(2), 16);
} else {
i = Integer.parseInt(s);
}

return i >= min && i <= max;
return i >= this.min && i <= this.max;
}
}
19 changes: 8 additions & 11 deletions src/main/java/codechicken/nei/config/OptionTextField.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package codechicken.nei.config;

import static codechicken.lib.gui.GuiDraw.drawString;
import static codechicken.lib.gui.GuiDraw.getStringWidth;

import java.util.List;

import codechicken.lib.gui.GuiDraw;
import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.TextField;
import codechicken.nei.config.GuiOptionList.OptionScrollSlot;

public class OptionTextField extends Option {

Expand Down Expand Up @@ -36,11 +33,6 @@ public OptionTextField(String name) {
super(name);
}

@Override
public void onAdded(OptionScrollSlot slot) {
super.onAdded(slot);
}

@Override
public void update() {
textField.update();
Expand All @@ -51,11 +43,16 @@ public String getPrefix() {
return translateN(name);
}

protected int getMaxInputWidth() {
return slot.slotWidth();
}

@Override
public void draw(int mousex, int mousey, float frame) {
drawString(getPrefix(), 10, 6, -1);
GuiDraw.drawString(getPrefix(), 10, 6, -1);

textField.w = slot.slotWidth() - getStringWidth(getPrefix()) - 16;
textField.w = Math
.max(60, Math.min(getMaxInputWidth(), slot.slotWidth() - GuiDraw.getStringWidth(getPrefix())) - 16);
textField.x = slot.slotWidth() - textField.w;
textField.draw(mousex, mousey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public boolean state() {
return renderTag().getBooleanValue();
}

@Override
public String getButtonText() {
return translateN(name + (state() ? ".true" : ".false"));
}
Expand Down

0 comments on commit c824d26

Please sign in to comment.