Skip to content

Commit

Permalink
Merge pull request #134 from FTBTeam/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
desht authored Nov 7, 2024
2 parents a30b3fe + 6997800 commit e779e6a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
* Overhauled and cleaned up many icon textures

### Fixed
* Fixed a client crash with certain inputs to IntTextBox (as used in FTB Chunks waypoint editing)

## [2101.1.4]

### Added
Expand Down
24 changes: 13 additions & 11 deletions common/src/main/java/dev/ftb/mods/ftblibrary/ui/IntTextBox.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package dev.ftb.mods.ftblibrary.ui;

import dev.ftb.mods.ftblibrary.ui.input.KeyModifiers;
import net.minecraft.util.Mth;

import java.util.function.Predicate;

public class IntTextBox extends TextBox {
// note: empty text and text with only '-' need to be allowed so numbers can be typed (treat as 0)
private static final Predicate<String> IS_NUMBER = s -> s.matches("^-?[0-9]*$");

private static final Predicate<String> IS_NUMBER = s -> s.matches("^-?[0-9]+$");
private int min = Integer.MIN_VALUE;
private int max = Integer.MAX_VALUE;

public IntTextBox(Panel panel) {
super(panel);
setFilter(IS_NUMBER);
setStrictValidity(true);
}

public int getIntValue() {
return Integer.parseInt(getText());
String text = getText();
if (text.isEmpty() || text.equals("-")) {
return Mth.clamp(0, min, max);
}
try {
return Integer.parseInt(text);
} catch (NumberFormatException ignored) {
return Mth.clamp(0, min, max);
}
}

public void setMin(int min) {
Expand Down Expand Up @@ -59,12 +69,4 @@ public void ensureValue() {
setAmount(max);
}
}

@Override
public boolean charTyped(char c, KeyModifiers modifiers) {
if (Character.isDigit(c)) {
return super.charTyped(c, modifiers);
}
return false;
}
}
10 changes: 9 additions & 1 deletion common/src/main/java/dev/ftb/mods/ftblibrary/ui/TextBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class TextBox extends Widget implements IFocusableWidget {
private Predicate<String> filter;
private Component label;
private Color4I labelColor = Color4I.WHITE;
private boolean strictValidity = false;

public TextBox(Panel panel) {
super(panel);
Expand Down Expand Up @@ -69,6 +70,10 @@ public final void setFocused(boolean focused) {
}
}

public void setStrictValidity(boolean strictValidity) {
this.strictValidity = strictValidity;
}

public void setFilter(Predicate<String> filter) {
this.filter = filter;
}
Expand Down Expand Up @@ -172,12 +177,15 @@ public void insertText(String string) {
}

String newText = (new StringBuilder(text)).replace(selStart, selEnd, filtered).toString();
boolean prevValid = validText;
validText = isValid(newText);
if (!text.equals(newText)) {
if (!text.equals(newText) && (validText || !strictValidity)) {
text = newText;
setCursorPosition(selStart + nToInsert);
setSelectionPos(cursorPos);
onTextChanged();
} else {
validText = prevValid;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public TopPanel() {
super(AbstractThreePanelScreen.this);

closeButton = new SimpleButton(this, Component.translatable("gui.close"),
Icons.CLOSE, (btn, mb) -> doCancel());
Icons.CANCEL, (btn, mb) -> doCancel());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public KeyReferenceScreen(String... translationKeys) {

textPanel = new TextPanel(this);

closeButton = new SimpleTextButton(this, Component.translatable("gui.close"), Icons.CLOSE) {
closeButton = new SimpleTextButton(this, Component.translatable("gui.close"), Icons.CANCEL) {
@Override
public void onClicked(MouseButton button) {
onBack();
Expand Down

0 comments on commit e779e6a

Please sign in to comment.