From cec1b2c39315aef3c5e1c123c08595908839bdca Mon Sep 17 00:00:00 2001 From: Falkreon Date: Tue, 20 Aug 2019 22:23:38 -0500 Subject: Poor (minecraft-level) RTL support --- .../cotton/gui/client/modmenu/ConfigGui.java | 5 ++ .../cottonmc/cotton/gui/widget/WTextField.java | 72 +++++++++++++--------- 2 files changed, 48 insertions(+), 29 deletions(-) (limited to 'src/main/java/io') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java index e114131..23c5b6f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java @@ -5,6 +5,7 @@ import io.github.cottonmc.cotton.gui.client.LibGuiClient; 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.WToggleButton; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; @@ -26,6 +27,10 @@ public class ConfigGui extends LightweightGuiDescription { darkmodeButton.setToggle(LibGuiClient.config.darkMode); root.add(darkmodeButton, 0, 2, 6, 1); + WTextField testField = new WTextField(); + testField.setSuggestion("test"); + root.add(testField, 0, 3, 4, 1); + root.add(new WKirbSprite(), 5, 4); WButton doneButton = new WButton(new TranslatableText("gui.done")); 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 17c6490..58f8a97 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 @@ -27,6 +27,9 @@ public class WTextField extends WWidget { public static final int OFFSET_X_TEXT = 4; //public static final int OFFSET_Y_TEXT = 6; + @Environment(EnvType.CLIENT) + private TextRenderer font; + protected String text = ""; protected int maxLength = 16; protected boolean editable = true; @@ -302,55 +305,65 @@ public class WTextField extends WWidget { @Environment(EnvType.CLIENT) 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); - //} + if (this.font==null) this.font = MinecraftClient.getInstance().textRenderer; + + int borderColor = (this.isFocused()) ? 0xFF_FFFFA0 : 0xFF_A0A0A0; + ScreenDrawing.rect(x-1, y-1, width+2, height+2, borderColor); + 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-OFFSET_X_TEXT); - //boolean boolean_1 = int_4 >= 0 && int_4 <= string_1.length(); + + //TODO: Scroll offset + String trimText = font.trimToWidth(this.text, this.width-OFFSET_X_TEXT); + boolean selection = (select!=-1); boolean focused = this.isFocused(); //this.isFocused() && this.focusedTicks / 6 % 2 == 0 && boolean_1; //Blinks the cursor + + //int textWidth = font.getStringWidth(trimText); + //int textAnchor = (font.isRightToLeft()) ? + // x + OFFSET_X_TEXT + textWidth : + // x + OFFSET_X_TEXT; + int textX = x + OFFSET_X_TEXT; + //(font.isRightToLeft()) ? + //textAnchor - textWidth : + //textAnchor; + int textY = y + (height - 8) / 2; - int int_8 = textX; + + //TODO: Adjust by scroll offset + int adjustedCursor = this.cursor; if (adjustedCursor > trimText.length()) { adjustedCursor = trimText.length(); } - + + int preCursorAdvance = textX; if (!trimText.isEmpty()) { String string_2 = trimText.substring(0,adjustedCursor); - int_8 = MinecraftClient.getInstance().textRenderer.drawWithShadow(string_2, (float)textX, (float)textY, textColor); + preCursorAdvance = font.drawWithShadow(string_2, textX, textY, textColor); } - 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 (adjustedCursor