Skip to content

Commit

Permalink
Fix text box and int/double/blockpos setting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Wide-Cat committed Aug 4, 2024
1 parent aefbefb commit e32839a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ private void addTextBox() {
lastValue = value;
if (textBoxX.get().isEmpty()) set(new BlockPos(0, 0, 0));
else {
set(new BlockPos(Integer.parseInt(textBoxX.get()), value.getY(), value.getZ()));
try {
set(new BlockPos(Integer.parseInt(textBoxX.get()), value.getY(), value.getZ()));
} catch (NumberFormatException ignored) {}
}
newValueCheck();
};
Expand All @@ -137,7 +139,9 @@ private void addTextBox() {
lastValue = value;
if (textBoxY.get().isEmpty()) set(new BlockPos(0, 0, 0));
else {
set(new BlockPos(value.getX(), Integer.parseInt(textBoxY.get()), value.getZ()));
try {
set(new BlockPos(value.getX(), Integer.parseInt(textBoxY.get()), value.getZ()));
} catch (NumberFormatException ignored) {}
}
newValueCheck();
};
Expand All @@ -146,7 +150,9 @@ private void addTextBox() {
lastValue = value;
if (textBoxZ.get().isEmpty()) set(new BlockPos(0, 0, 0));
else {
set(new BlockPos(value.getX(), value.getY(), Integer.parseInt(textBoxZ.get())));
try {
set(new BlockPos(value.getX(), value.getY(), Integer.parseInt(textBoxZ.get())));
} catch (NumberFormatException ignored) {}
}
newValueCheck();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class WDoubleEdit extends WHorizontalList {
private final double min, max;
private final double sliderMin, sliderMax;

public int decimalPlaces = 3;
public int decimalPlaces;
public boolean noSlider = false;
public boolean small = false;

Expand Down Expand Up @@ -53,7 +53,11 @@ public void init() {
else if (textBox.get().equals("-")) value = -0;
else if (textBox.get().equals(".")) value = 0;
else if (textBox.get().equals("-.")) value = 0;
else value = Double.parseDouble(textBox.get());
else {
try {
value = Double.parseDouble(textBox.get());
} catch (NumberFormatException ignored) {}
}

double preValidationValue = value;

Expand Down Expand Up @@ -114,8 +118,7 @@ private void setButton(double v) {
if (this.value == v) return;

if (v < min) this.value = min;
else if (v > max) this.value = max;
else this.value = v;
else this.value = Math.min(v, max);

if (this.value == v) {
textBox.set(valueString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public void init() {

if (textBox.get().isEmpty()) value = 0;
else if (textBox.get().equals("-")) value = -0;
else value = Integer.parseInt(textBox.get());
else {
try {
value = Integer.parseInt(textBox.get());
} catch (NumberFormatException ignored) {}
}

if (slider != null) slider.set(value);

Expand Down Expand Up @@ -99,8 +103,7 @@ private void setButton(int v) {
if (this.value == v) return;

if (v < min) this.value = min;
else if (v > max) this.value = max;
else this.value = v;
else this.value = Math.min(v, max);

if (this.value == v) {
textBox.set(Integer.toString(this.value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,16 @@ public boolean onKeyRepeated(int key, int mods) {
int addedChars = 0;

StringBuilder sb = new StringBuilder(text.length() + clipboard.length());
sb.append(text, 0, cursor);
sb.append(text);

for (int i = 0; i < clipboard.length(); i++) {
char c = clipboard.charAt(i);
if (filter.filter(text, c)) {
sb.append(c);
if (filter.filter(sb.toString(), c)) {
sb.insert(cursor + addedChars, c);
addedChars++;
}
}

sb.append(text, cursor, text.length());

text = sb.toString();
cursor += addedChars;
resetSelection();
Expand Down Expand Up @@ -319,24 +317,24 @@ else if (cursor != selectionStart || cursor != selectionEnd) {
return true;
}
else if (key == GLFW_KEY_DELETE) {
if (cursor == selectionStart && cursor == selectionEnd) {
if (cursor < text.length()) {
String preText = text;
if (cursor == selectionStart && cursor == selectionEnd) {
if (cursor < text.length()) {
String preText = text;

int count = mods == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_ALT : MinecraftClient.IS_SYSTEM_MAC ? GLFW_MOD_SUPER : GLFW_MOD_CONTROL)
? text.length() - cursor
: (mods == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_CONTROL : GLFW_MOD_ALT))
? countToNextSpace(false)
: 1;
int count = mods == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_ALT : MinecraftClient.IS_SYSTEM_MAC ? GLFW_MOD_SUPER : GLFW_MOD_CONTROL)
? text.length() - cursor
: (mods == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_CONTROL : GLFW_MOD_ALT))
? countToNextSpace(false)
: 1;

text = text.substring(0, cursor) + text.substring(cursor + count);
text = text.substring(0, cursor) + text.substring(cursor + count);

if (!text.equals(preText)) runAction();
}
}
else {
clearSelection();
if (!text.equals(preText)) runAction();
}
}
else {
clearSelection();
}
return true;
}
else if (key == GLFW_KEY_LEFT) {
Expand Down

0 comments on commit e32839a

Please sign in to comment.