From e1215b0f3f790bd3473ea2b1faaf0a5935791e12 Mon Sep 17 00:00:00 2001 From: Falkreon Date: Sun, 18 Aug 2019 15:19:53 -0500 Subject: clicks and caret repositioning for WTextField --- .../cotton/gui/client/ClientCottonScreen.java | 4 +- .../cottonmc/cotton/gui/client/CottonScreen.java | 4 +- .../cottonmc/cotton/gui/client/ScreenDrawing.java | 3 +- .../cottonmc/cotton/gui/widget/WTextField.java | 215 ++++++++++++++------- .../cottonmc/cotton/gui/widget/WToggleButton.java | 2 +- .../github/cottonmc/cotton/gui/widget/WWidget.java | 4 +- 6 files changed, 153 insertions(+), 79 deletions(-) (limited to 'src/main') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java index 90b6273..d6b32c2 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java @@ -155,14 +155,14 @@ public class ClientCottonScreen extends Screen { @Override public boolean keyPressed(int ch, int keyCode, int modifiers) { if (description.getFocus()==null) return false; - description.getFocus().onKeyPressed(keyCode, modifiers); + description.getFocus().onKeyPressed(ch, keyCode, modifiers); return true; } @Override public boolean keyReleased(int ch, int keyCode, int modifiers) { if (description.getFocus()==null) return false; - description.getFocus().onKeyReleased(keyCode, modifiers); + description.getFocus().onKeyReleased(ch, keyCode, modifiers); return true; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java index 445d612..9f7e374 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java @@ -82,14 +82,14 @@ public class CottonScreen extends AbstractCont @Override public boolean keyPressed(int ch, int keyCode, int modifiers) { if (container.getFocus()==null) return false; - container.getFocus().onKeyPressed(keyCode, modifiers); + container.getFocus().onKeyPressed(ch, keyCode, modifiers); return true; } @Override public boolean keyReleased(int ch, int keyCode, int modifiers) { if (container.getFocus()==null) return false; - container.getFocus().onKeyReleased(keyCode, modifiers); + container.getFocus().onKeyReleased(ch, keyCode, modifiers); return true; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java index f6aaef1..b0c2b05 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java @@ -198,7 +198,8 @@ public class ScreenDrawing { } public static void drawString(String s, int x, int y, int color) { - MinecraftClient.getInstance().getFontManager().getTextRenderer(MinecraftClient.DEFAULT_TEXT_RENDERER_ID).draw(s, x, y, color); + MinecraftClient.getInstance().textRenderer.draw(s, x, y, color); + //MinecraftClient.getInstance().getFontManager().getTextRenderer(MinecraftClient.DEFAULT_TEXT_RENDERER_ID).draw(s, x, y, color); } public static void drawCenteredWithShadow(String s, int x, int y, int color) { 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 d1aa437..14b763d 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 @@ -12,15 +12,16 @@ import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; import net.minecraft.text.Text; import net.minecraft.util.math.MathHelper; public class WTextField extends WWidget { + public static final int OFFSET_X_TEXT = 4; + //public static final int OFFSET_Y_TEXT = 6; protected String text = ""; protected int maxLength = 16; - protected int focusedTicks = 0; - protected boolean focused = false; protected boolean editable = true; protected int enabledColor = 0xE0E0E0; @@ -268,74 +269,76 @@ public class WTextField extends WWidget { } }*/ - /* - public void renderButton(int int_1, int int_2, float float_1) { - if (this.isVisible()) { - if (this.hasBorder()) { - fill(this.x - 1, this.y - 1, this.x + this.width + 1, this.y + this.height + 1, -6250336); - fill(this.x, this.y, this.x + this.width, this.y + this.height, -16777216); - } - - int int_3 = this.editable ? this.editableColor : this.uneditableColor; - int int_4 = this.cursorMax - this.field_2103; - int int_5 = this.cursorMin - this.field_2103; - String string_1 = this.textRenderer.trimToWidth(this.text.substring(this.field_2103), this.method_1859()); - boolean boolean_1 = int_4 >= 0 && int_4 <= string_1.length(); - boolean boolean_2 = this.isFocused() && this.focusedTicks / 6 % 2 == 0 && boolean_1; - int int_6 = this.focused ? this.x + 4 : this.x; - int int_7 = this.focused ? this.y + (this.height - 8) / 2 : this.y; - int int_8 = int_6; - if (int_5 > string_1.length()) { - int_5 = string_1.length(); - } - - if (!string_1.isEmpty()) { - String string_2 = boolean_1 ? string_1.substring(0, int_4) : string_1; - int_8 = this.textRenderer.drawWithShadow((String)this.renderTextProvider.apply(string_2, this.field_2103), (float)int_6, (float)int_7, int_3); - } - - boolean boolean_3 = this.cursorMax < this.text.length() || this.text.length() >= this.getMaxLength(); - int int_9 = int_8; - if (!boolean_1) { - int_9 = int_4 > 0 ? int_6 + this.width : int_6; - } else if (boolean_3) { - int_9 = int_8 - 1; - --int_8; - } + + public void renderButton(int x, int y) { + //if (this.focused) { //has border? + ScreenDrawing.rect(x-1, y-1, width+2, height+2, 0xFFA0A0A0); + ScreenDrawing.rect(x, y, width, height, 0xFF000000); + //} + + int textColor = this.editable ? this.enabledColor : this.uneditableColor; + //int int_4 = this.cursorMax - this.field_2103; + int adjustedCursor = this.cursor;// - this.field_2103; + String trimText = MinecraftClient.getInstance().textRenderer.trimToWidth(this.text, this.width); + //boolean boolean_1 = int_4 >= 0 && int_4 <= string_1.length(); + boolean focused = this.isFocused(); //this.isFocused() && this.focusedTicks / 6 % 2 == 0 && boolean_1; //Blinks the cursor + int textX = x + OFFSET_X_TEXT; + int textY = y + (height - 8) / 2; + int int_8 = textX; + if (adjustedCursor > trimText.length()) { + adjustedCursor = trimText.length(); + } - if (!string_1.isEmpty() && boolean_1 && int_4 < string_1.length()) { - this.textRenderer.drawWithShadow((String)this.renderTextProvider.apply(string_1.substring(int_4), this.cursorMax), (float)int_8, (float)int_7, int_3); - } + if (!trimText.isEmpty()) { + String string_2 = trimText.substring(0,adjustedCursor); + int_8 = MinecraftClient.getInstance().textRenderer.drawWithShadow(string_2, (float)textX, (float)textY, textColor); + } - if (!boolean_3 && this.suggestion != null) { - this.textRenderer.drawWithShadow(this.suggestion, (float)(int_9 - 1), (float)int_7, -8355712); - } + boolean boolean_3 = adjustedCursor < trimText.length(); //false; //this.cursorMax < this.text.length() || this.text.length() >= this.getMaxLength(); + + /*if (!boolean_1) { + int_9 = int_4 > 0 ? int_6 + this.width : int_6; + } else if (boolean_3) { + int_9 = int_8 - 1; + --int_8; + }*/ + + //if (!trimText.isEmpty() && boolean_1 && int_4 < trimText.length()) { + if (adjustedCursorthis.text.length()) cursor = this.text.length(); + + String before = this.text.substring(0, cursor); + String after = this.text.substring(cursor, this.text.length()); + this.text = before+ch+after; + cursor++; + } } @Override - public void onKeyPressed(int key, int modifiers) { - if (key==22) { - if (text.length()>0) text = text.substring(0, text.length()-1); + public void onKeyPressed(int ch, int key, int modifiers) { + if (modifiers==0) { + if (ch==GLFW.GLFW_KEY_DELETE || ch==GLFW.GLFW_KEY_BACKSPACE) { + //if (key==22) { + if (text.length()>0 && cursor>0) { + String before = this.text.substring(0, cursor); + String after = this.text.substring(cursor, this.text.length()); + + before = before.substring(0,before.length()-1); + text = before+after; + cursor--; + } + } else if (ch==GLFW.GLFW_KEY_LEFT) { + if (cursor>0) cursor--; + } else if (ch==GLFW.GLFW_KEY_RIGHT) { + if (cursor