diff options
author | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-04-21 14:19:44 +0100 |
---|---|---|
committer | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-04-21 14:19:44 +0100 |
commit | 260d48126cbedb4341c5c5865bfd8e605f90955a (patch) | |
tree | 563f65d65d708f24df33759c411b0f2338c1933e /src/main/java/io/polyfrost/oneconfig/gui | |
parent | 30df910cf3b2f5b0683ce01e391c35829d8a5850 (diff) | |
download | OneConfig-260d48126cbedb4341c5c5865bfd8e605f90955a.tar.gz OneConfig-260d48126cbedb4341c5c5865bfd8e605f90955a.tar.bz2 OneConfig-260d48126cbedb4341c5c5865bfd8e605f90955a.zip |
more gui things like text field and button
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/gui')
4 files changed, 182 insertions, 26 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/gui/OneConfigGui.java b/src/main/java/io/polyfrost/oneconfig/gui/OneConfigGui.java index cbe1c6a..24bfe19 100644 --- a/src/main/java/io/polyfrost/oneconfig/gui/OneConfigGui.java +++ b/src/main/java/io/polyfrost/oneconfig/gui/OneConfigGui.java @@ -1,16 +1,24 @@ package io.polyfrost.oneconfig.gui; -import io.polyfrost.oneconfig.OneConfig; import io.polyfrost.oneconfig.config.OneConfigConfig; +import io.polyfrost.oneconfig.gui.elements.BasicButton; import io.polyfrost.oneconfig.gui.elements.BasicElement; +import io.polyfrost.oneconfig.gui.elements.TextInputField; import io.polyfrost.oneconfig.lwjgl.RenderManager; +import io.polyfrost.oneconfig.lwjgl.font.Fonts; import net.minecraft.client.gui.GuiScreen; - -import java.awt.*; +import org.lwjgl.input.Keyboard; public class OneConfigGui extends GuiScreen { + public static OneConfigGui INSTANCE; private final BasicElement element = new BasicElement(200, 200, 1, true); + private final TextInputField textInputField = new TextInputField(776, 32, "Search all of OneConfig...", false, false); + private final BasicButton btn = new BasicButton(184, 36, "Socials", "/assets/oneconfig/textures/share.png", "/assets/oneconfig/textures/share2.png", 1, true); + + public OneConfigGui() { + INSTANCE = this; + } @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { super.drawScreen(mouseX, mouseY, partialTicks); @@ -26,18 +34,32 @@ public class OneConfigGui extends GuiScreen { RenderManager.drawLine(vg, 544, 212, 1600, 212, 1, OneConfigConfig.GRAY_700); RenderManager.drawLine(vg, 544, 140, 544, 940, 1, OneConfigConfig.GRAY_700); - RenderManager.drawString(vg, "OneConfig", 389, 163, OneConfigConfig.WHITE, 18f, "inter-bold"); - RenderManager.drawString(vg, "By Polyfrost", 389, 183, OneConfigConfig.WHITE, 12f, "inter-regular"); - element.setColorPalette(0); - element.draw(vg, 0, 0); + RenderManager.drawString(vg, "OneConfig", 389, 163, OneConfigConfig.WHITE, 18f, Fonts.INTER_BOLD); + RenderManager.drawString(vg, "By Polyfrost", 389, 183, OneConfigConfig.WHITE, 12f, Fonts.INTER_REGULAR); + //element.setColorPalette(0); + try { + //element.draw(vg, 0, 0); + textInputField.draw(vg, 792, 548); + btn.draw(vg, 976, 870); + } catch (Exception e) { + e.printStackTrace(); + } //RenderManager.drawGradientRoundedRect(vg, 100, 100, 500, 100, OneConfigConfig.BLUE_600, OneConfigConfig.BLUE_500, OneConfigConfig.CORNER_RADIUS_WIN); - - }); } + protected void keyTyped(char key, int keyCode) { + Keyboard.enableRepeatEvents(true); + try { + super.keyTyped(key, keyCode); + textInputField.keyTyped(key, keyCode); + } catch (Exception e) { + System.out.println("this should literally never happen"); + } + } + @Override public boolean doesGuiPauseGame() { diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicButton.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicButton.java new file mode 100644 index 0000000..e918287 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicButton.java @@ -0,0 +1,47 @@ +package io.polyfrost.oneconfig.gui.elements; + +import io.polyfrost.oneconfig.lwjgl.RenderManager; +import io.polyfrost.oneconfig.lwjgl.font.Fonts; +import io.polyfrost.oneconfig.utils.ColorUtils; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import static org.lwjgl.nanovg.NanoVG.nvgTextBounds; + +public class BasicButton extends BasicElement { + + protected String text; + protected String fileNameLeftIco, fileNameRightIco; + + public BasicButton(int width, int height, @NotNull String text, @Nullable String fileNameLeftIco, @Nullable String fileNameRightIco, int colorPalette, boolean hoverFx) { + super(width, height, colorPalette, hoverFx); + this.text = text; + this.fileNameLeftIco = fileNameLeftIco; + this.fileNameRightIco = fileNameRightIco; + } + + + @Override + public void draw(long vg, int x, int y) { + RenderManager.drawRectangle(vg, x, y, this.width, this.height, this.currentColor); + final float fontSize; + if(colorPalette == -1) { + fontSize = 24f; + } else fontSize = 14f; + float width = RenderManager.getTextWidth(vg, text, fontSize); + int middle = x + this.width / 2; + RenderManager.drawString(vg, text,middle - width / 2, y + ((float) height / 2),-1, fontSize, Fonts.INTER_MEDIUM); + if(fileNameLeftIco != null) { + RenderManager.drawImage(vg, fileNameLeftIco, middle - width - 8, y + 8, 20, 20); + } + if(fileNameRightIco != null) { + RenderManager.drawImage(vg, fileNameRightIco, middle + width - 8, y + 8, 20, 20); + } + this.update(x, y); + if(hoverFx) { + currentColor = ColorUtils.getColor(currentColor, 1, hovered, clicked); + } + } + + +} diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicElement.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicElement.java index 0b7d604..765a271 100644 --- a/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicElement.java +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicElement.java @@ -6,19 +6,14 @@ import net.minecraft.client.Minecraft; import org.lwjgl.input.Mouse; public class BasicElement { - private int width; - private int height; - private int colorPalette; - - private int hitBoxX, hitBoxY; - - private final boolean hoverFx; - - private boolean hovered = false; - private boolean clicked = false; - private boolean toggled = false; - - private int currentColor; + protected int width, height; + protected int colorPalette; + protected int hitBoxX, hitBoxY; + protected final boolean hoverFx; + protected boolean hovered = false; + protected boolean clicked = false; + protected boolean toggled = false; + protected int currentColor; public BasicElement(int width, int height, int colorPalette, boolean hoverFx) { this.height = height; @@ -37,6 +32,14 @@ public class BasicElement { public void draw(long vg, int x, int y) { RenderManager.drawRectangle(vg, x, y, width, height, currentColor); + + update(x, y); + if (hoverFx) { + currentColor = ColorUtils.getColor(currentColor, colorPalette, hovered, clicked); + } + } + + public void update(int x, int y) { int mouseX = Mouse.getX(); int mouseY = Minecraft.getMinecraft().displayHeight - Math.abs(Mouse.getY()); int buttonRight = x + width; @@ -47,10 +50,6 @@ public class BasicElement { toggled = !toggled; } clicked = Mouse.isButtonDown(0) && hovered; - - if (hoverFx) { - currentColor = ColorUtils.getColor(currentColor, colorPalette, hovered, clicked); - } } diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/TextInputField.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/TextInputField.java new file mode 100644 index 0000000..7b56a40 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/TextInputField.java @@ -0,0 +1,88 @@ +package io.polyfrost.oneconfig.gui.elements; + +import io.polyfrost.oneconfig.config.OneConfigConfig; +import io.polyfrost.oneconfig.lwjgl.RenderManager; +import io.polyfrost.oneconfig.lwjgl.font.Fonts; +import net.minecraft.client.gui.GuiScreen; +import org.lwjgl.input.Keyboard; + +public class TextInputField extends BasicElement { + + protected final String defaultText; + protected String input; + protected final boolean mulitLine; + protected boolean password; + + public TextInputField(int width, int height, String defaultText, boolean multiLine, boolean password) { + super(width, height, false); + this.mulitLine = multiLine; + this.defaultText = defaultText; + this.password = password; + this.input = defaultText; + } + + public void setInput(String input) { + this.input = input; + } + + public String getInput() { + return input; + } + + public void setPassword(boolean password) { + this.password = password; + } + + @Override + public void draw(long vg, int x, int y) { + RenderManager.drawRectangle(vg, x, y, width, height, OneConfigConfig.GRAY_700); + RenderManager.drawRectangle(vg, x + 2, y + 2, width - 2, height - 4, OneConfigConfig.GRAY_900); + super.update(x, y); + int color = toggled ? OneConfigConfig.WHITE : OneConfigConfig.WHITE_60; + float width = RenderManager.getTextWidth(vg, input, 14f); + + if(toggled) { + RenderManager.drawLine(vg, x + width + 12, (float) y + 7, x + width + 13, (float) y + height - 7, 1, OneConfigConfig.WHITE); + } + + if(input.equals("")){ + RenderManager.drawString(vg, defaultText, x + 12, y + 17, color, 14f, Fonts.INTER_REGULAR); + } + RenderManager.drawString(vg, input, x + 12, y + 17, color, 14f, Fonts.INTER_REGULAR); + + } + + public void keyTyped(char c, int key) { + if (toggled) { + if(GuiScreen.isCtrlKeyDown()) { + if(key == Keyboard.KEY_BACK) { + try { + input = input.substring(0, input.lastIndexOf(" ")); + } catch (Exception e) { + input = ""; + } + } + return; + } + if (key == Keyboard.KEY_BACK) { + if (input.length() > 0) { + input = input.substring(0, input.length() - 1); + return; + } + } + if(key == Keyboard.KEY_TAB) { + input += " "; + return; + } + + if(key == Keyboard.KEY_RETURN) { + toggled = false; + } + + if(key == Keyboard.KEY_LCONTROL || key == Keyboard.KEY_RCONTROL || key == Keyboard.KEY_LMENU || key == Keyboard.KEY_RMENU || key == Keyboard.KEY_LMETA || key == Keyboard.KEY_RMETA || key == Keyboard.KEY_LSHIFT || key == Keyboard.KEY_RSHIFT || key == Keyboard.KEY_RETURN || key == Keyboard.KEY_CAPITAL || key == 221) { + return; + } + input += c; + } + } +} |