aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java46
-rw-r--r--GuiTest/src/main/java/io/github/cottonmc/test/client/TextFieldTestGui.java20
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java23
3 files changed, 59 insertions, 30 deletions
diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java b/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java
index efdcd3f..38545fa 100644
--- a/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java
+++ b/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java
@@ -1,19 +1,25 @@
package io.github.cottonmc.test.client;
+import com.mojang.brigadier.Command;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager;
+import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry;
+import net.minecraft.client.MinecraftClient;
import net.minecraft.text.LiteralText;
import io.github.cottonmc.cotton.gui.client.CottonClientScreen;
import io.github.cottonmc.cotton.gui.client.CottonHud;
import io.github.cottonmc.cotton.gui.client.CottonInventoryScreen;
+import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
import io.github.cottonmc.cotton.gui.impl.modmenu.ConfigGui;
import io.github.cottonmc.cotton.gui.widget.WLabel;
import io.github.cottonmc.test.LibGuiTest;
import io.github.cottonmc.test.ReallySimpleDescription;
import io.github.cottonmc.test.TestDescription;
+import java.util.function.Function;
+
import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.literal;
public class LibGuiTestClient implements ClientModInitializer {
@@ -35,35 +41,19 @@ public class LibGuiTestClient implements ClientModInitializer {
ClientCommandManager.DISPATCHER.register(
literal("libgui")
- .then(literal("config").executes(context -> {
- var client = context.getSource().getClient();
- client.send(() -> {
- client.setScreen(new CottonClientScreen(new ConfigGui(client.currentScreen)));
- });
- return 0;
- }))
- .then(literal("tab").executes(context -> {
- var client = context.getSource().getClient();
- client.send(() -> {
- client.setScreen(new CottonClientScreen(new TabTestGui()));
- });
- return 0;
- }))
- .then(literal("scrolling").executes(context -> {
- var client = context.getSource().getClient();
- client.send(() -> {
- client.setScreen(new CottonClientScreen(new ScrollingTestGui()));
- });
- return 0;
- }))
- .then(literal("insets").executes(context -> {
- var client = context.getSource().getClient();
- client.send(() -> {
- client.setScreen(new CottonClientScreen(new InsetsTestGui()));
- });
- return 0;
- }))
+ .then(literal("config").executes(openScreen(client -> new ConfigGui(client.currentScreen))))
+ .then(literal("tab").executes(openScreen(client -> new TabTestGui())))
+ .then(literal("scrolling").executes(openScreen(client -> new ScrollingTestGui())))
+ .then(literal("insets").executes(openScreen(client -> new InsetsTestGui())))
+ .then(literal("textfield").executes(openScreen(client -> new TextFieldTestGui())))
);
}
+ private static Command<FabricClientCommandSource> openScreen(Function<MinecraftClient, LightweightGuiDescription> screenFactory) {
+ return context -> {
+ var client = context.getSource().getClient();
+ client.send(() -> client.setScreen(new CottonClientScreen(screenFactory.apply(client))));
+ return Command.SINGLE_SUCCESS;
+ };
+ }
}
diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/client/TextFieldTestGui.java b/GuiTest/src/main/java/io/github/cottonmc/test/client/TextFieldTestGui.java
new file mode 100644
index 0000000..dc2b68c
--- /dev/null
+++ b/GuiTest/src/main/java/io/github/cottonmc/test/client/TextFieldTestGui.java
@@ -0,0 +1,20 @@
+package io.github.cottonmc.test.client;
+
+import net.minecraft.item.Items;
+import net.minecraft.text.LiteralText;
+
+import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
+import io.github.cottonmc.cotton.gui.widget.WButton;
+import io.github.cottonmc.cotton.gui.widget.WGridPanel;
+import io.github.cottonmc.cotton.gui.widget.WTextField;
+import io.github.cottonmc.cotton.gui.widget.icon.ItemIcon;
+
+public class TextFieldTestGui extends LightweightGuiDescription {
+ public TextFieldTestGui() {
+ WGridPanel grid = (WGridPanel) rootPanel;
+ WTextField textField = new WTextField(new LiteralText("Type something")).setMaxLength(Integer.MAX_VALUE);
+ grid.add(textField, 0, 0, 6, 1);
+ grid.add(new WButton(new ItemIcon(Items.BARRIER), new LiteralText("Clear")).setOnClick(() -> textField.setText("")), 0, 2, 6, 1);
+ rootPanel.validate(this);
+ }
+}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java
index b566f7e..64bf367 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java
@@ -85,6 +85,13 @@ public class WTextField extends WWidget {
this.suggestion = suggestion;
}
+ /**
+ * Sets the text of this text field.
+ * If the text is more than the {@linkplain #getMaxLength() max length},
+ * it'll be shortened to the max length.
+ *
+ * @param s the new text
+ */
public void setText(String s) {
setTextWithResult(s);
}
@@ -92,13 +99,19 @@ public class WTextField extends WWidget {
private boolean setTextWithResult(String s) {
if (this.textPredicate == null || this.textPredicate.test(s)) {
this.text = (s.length() > maxLength) ? s.substring(0, maxLength) : s;
+ // Call change listener
if (onChanged != null) onChanged.accept(this.text);
+ // Reset cursor if needed
+ if (cursor >= this.text.length()) cursor = this.text.length() - 1;
return true;
}
return false;
}
+ /**
+ * {@return the text in this text field}
+ */
public String getText() {
return this.text;
}
@@ -141,6 +154,11 @@ public class WTextField extends WWidget {
scrollOffset = cursor;
}
+ checkScrollOffset();
+ }
+
+ @Environment(EnvType.CLIENT)
+ private void checkScrollOffset() {
int rightMostScrollOffset = text.length() - font.trimToWidth(text, width - TEXT_PADDING_X * 2, true).length();
scrollOffset = Math.min(rightMostScrollOffset, scrollOffset);
}
@@ -217,6 +235,7 @@ public class WTextField extends WWidget {
protected void renderTextField(MatrixStack matrices, int x, int y) {
if (this.font == null) this.font = MinecraftClient.getInstance().textRenderer;
+ checkScrollOffset();
String visibleText = font.trimToWidth(this.text.substring(this.scrollOffset), this.width - 2 * TEXT_PADDING_X);
renderBox(matrices, x, y);
renderText(matrices, x, y, visibleText);
@@ -263,8 +282,7 @@ public class WTextField extends WWidget {
public WTextField setMaxLength(int max) {
this.maxLength = max;
if (this.text.length() > max) {
- this.text = this.text.substring(0, max);
- this.onChanged.accept(this.text);
+ setText(this.text.substring(0, max));
}
return this;
}
@@ -337,6 +355,7 @@ public class WTextField extends WWidget {
public int getCaretPosition(int clickX) {
if (clickX < 0) return 0;
int lastPos = 0;
+ checkScrollOffset();
String string = text.substring(scrollOffset);
for (int i = 0; i < string.length(); i++) {
int w = font.getWidth(string.charAt(i) + "");