Skip to content

Commit

Permalink
fix: make string config overlay a bit less strict
Browse files Browse the repository at this point in the history
The textbox now allows text which is considered invalid by the isValid()
method, but will display it in red. Users of the textbox should check its
validity with isTextValid() when deciding if the value should be committed.
  • Loading branch information
desht committed May 14, 2024
1 parent a71029c commit 8179d9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public EditStringConfigOverlay(Panel panel, ConfigFromString<T> config, ConfigCa

titleField = new TextField(this).addFlags(Theme.SHADOW).setText(Objects.requireNonNullElse(title, Component.empty()));
titleField.setSize(0, 0);
textBox = new EditField();
accept = new SimpleButton(this, Component.translatable("gui.accept"), Icons.ACCEPT, this::onAccepted);
cancel = new SimpleButton(this, Component.translatable("gui.cancel"), Icons.CANCEL, this::onCancelled);
textBox = new EditField();
}

public EditStringConfigOverlay<T> atPosition(int x, int y) {
Expand Down Expand Up @@ -91,9 +91,11 @@ public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int
}

private void onAccepted(Button btn, MouseButton mb) {
config.setCurrentValue(currentValue);
callback.save(true);
getGui().popModalPanel();
if (textBox.isTextValid()) {
config.setCurrentValue(currentValue);
callback.save(true);
getGui().popModalPanel();
}
}

private void onCancelled(Button btn, MouseButton mb) {
Expand Down Expand Up @@ -125,6 +127,10 @@ public boolean isValid(String txt) {
@Override
public void onTextChanged() {
config.parse(t -> currentValue = t, getText());

if (accept != null) {
accept.setIcon(isTextValid() ? Icons.ACCEPT : Icons.ACCEPT.withTint(Color4I.DARK_GRAY.withAlpha(160)));
}
}

@Override
Expand Down
8 changes: 6 additions & 2 deletions common/src/main/java/dev/ftb/mods/ftblibrary/ui/TextBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.ftb.mods.ftblibrary.ui.input.KeyModifiers;
import dev.ftb.mods.ftblibrary.ui.input.MouseButton;
import net.minecraft.ChatFormatting;
import net.minecraft.SharedConstants;
import net.minecraft.Util;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
Expand Down Expand Up @@ -164,7 +163,7 @@ public void insertText(String string) {

String newText = (new StringBuilder(text)).replace(selStart, selEnd, filtered).toString();
validText = isValid(newText);
if (validText) {
if (!text.equals(newText)) {
text = newText;
setCursorPosition(selStart + nToInsert);
setSelectionPos(cursorPos);
Expand Down Expand Up @@ -232,11 +231,16 @@ public boolean allowInput() {
}

private void deleteText(int count) {
String prevText = text;
if (Screen.hasControlDown()) {
deleteWords(count);
} else {
deleteChars(count);
}
if (!prevText.equals(text)) {
validText = isValid(text);
onTextChanged();
}
}

public void deleteWords(int count) {
Expand Down

0 comments on commit 8179d9b

Please sign in to comment.