diff options
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/gui/elements')
-rw-r--r-- | src/main/java/io/polyfrost/oneconfig/gui/elements/BasicButton.java | 79 | ||||
-rw-r--r-- | src/main/java/io/polyfrost/oneconfig/gui/elements/TextInputField.java | 71 |
2 files changed, 128 insertions, 22 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicButton.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicButton.java index e918287..39d5e9c 100644 --- a/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicButton.java +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicButton.java @@ -1,5 +1,6 @@ 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 io.polyfrost.oneconfig.utils.ColorUtils; @@ -12,34 +13,88 @@ public class BasicButton extends BasicElement { protected String text; protected String fileNameLeftIco, fileNameRightIco; + private final int thisAlignment; + private final float fontSize; + private final int colorPalette; - public BasicButton(int width, int height, @NotNull String text, @Nullable String fileNameLeftIco, @Nullable String fileNameRightIco, int colorPalette, boolean hoverFx) { - super(width, height, colorPalette, hoverFx); + public int x, y; + public static final int ALIGNMENT_LEFT = 0; + public static final int ALIGNMENT_CENTER = 1; + + /** + * Create a new basic button. Used mostly on the homepage and the sidebar. Note: The button will not be drawn until you call {@link #draw(long, int, int)}. + * The button's content is centered on its total length, so the text is not always in the middle. + * @param text Text to display on the button. Has to be there. + * @param fileNameLeftIco file path of the icon to display on the left. Can be null if you don't want to display an icon on the left. + * @param fileNameRightIco file path of the icon to display on the right. Can be null if you don't want to display an icon on the right. + * @param colorPalette color palette to use. see {@link io.polyfrost.oneconfig.utils.ColorUtils} for more info. Can support color palette of -2, which is larger font and icons. Also supports -3, which is just the text changing color. + * @param alignment alignment of the button. ALIGNMENT_LEFT or ALIGNMENT_CENTER. + */ + public BasicButton(int width, int height, @NotNull String text, @Nullable String fileNameLeftIco, @Nullable String fileNameRightIco, int colorPalette, int alignment) { + super(width, height, colorPalette, true); this.text = text; this.fileNameLeftIco = fileNameLeftIco; this.fileNameRightIco = fileNameRightIco; + this.thisAlignment = alignment; + if(colorPalette == -2) { + fontSize = 24f; + this.colorPalette = -1; + } else { + fontSize = 14f; + this.colorPalette = colorPalette; + } } @Override public void draw(long vg, int x, int y) { + this.x = x; + this.y = y; + int textColor = -1; 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); + float contentWidth = RenderManager.getTextWidth(vg, text, fontSize); if(fileNameLeftIco != null) { - RenderManager.drawImage(vg, fileNameLeftIco, middle - width - 8, y + 8, 20, 20); + contentWidth += 28; } if(fileNameRightIco != null) { - RenderManager.drawImage(vg, fileNameRightIco, middle + width - 8, y + 8, 20, 20); + contentWidth += 28; + } + + if(this.colorPalette == -3) { + textColor = OneConfigConfig.WHITE_80; + if(hovered) textColor = OneConfigConfig.WHITE; + if(clicked) textColor = OneConfigConfig.WHITE_80; + } + + if(thisAlignment == ALIGNMENT_CENTER) { + int middle = x + this.width / 2; + RenderManager.drawString(vg, text, middle - contentWidth / 2 + (fileNameLeftIco != null ? 28 : 0), y + ((float) height / 2), textColor, fontSize, Fonts.INTER_MEDIUM); + if (fileNameLeftIco != null) { + RenderManager.drawImage(vg, fileNameLeftIco, middle - contentWidth / 2, y + 8, 20, 20); + } + if (fileNameRightIco != null) { + RenderManager.drawImage(vg, fileNameRightIco, middle + contentWidth / 2 - (fileNameLeftIco != null ? 20 : 24), y + 8, 20, 20); + } + } + if(thisAlignment == ALIGNMENT_LEFT) { + if(fileNameLeftIco != null) { + RenderManager.drawImage(vg, fileNameLeftIco, x + 12, y + 8, 20, 20); + RenderManager.drawString(vg, text, x + 40, y + ((float) height / 2), textColor, fontSize, Fonts.INTER_MEDIUM); + } else { + RenderManager.drawString(vg, text, x + 12, y + ((float) height / 2), textColor, fontSize, Fonts.INTER_MEDIUM); + } + if(fileNameRightIco != null) { + RenderManager.drawImage(vg, fileNameRightIco, x + width - 28, y + 8, 20, 20); + } } this.update(x, y); if(hoverFx) { - currentColor = ColorUtils.getColor(currentColor, 1, hovered, clicked); + if(colorPalette == -3) { + currentColor = OneConfigConfig.TRANSPARENT; + return; + } + 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 index 7b56a40..e2c7602 100644 --- a/src/main/java/io/polyfrost/oneconfig/gui/elements/TextInputField.java +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/TextInputField.java @@ -1,24 +1,30 @@ package io.polyfrost.oneconfig.gui.elements; +import com.google.common.base.Strings; 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.jetbrains.annotations.NotNull; import org.lwjgl.input.Keyboard; +import static org.lwjgl.nanovg.NanoVG.*; + public class TextInputField extends BasicElement { protected final String defaultText; protected String input; - protected final boolean mulitLine; + protected final boolean multiLine; protected boolean password; + protected int caretPos; + public TextInputField(int width, int height, String defaultText, boolean multiLine, boolean password) { super(width, height, false); - this.mulitLine = multiLine; + this.multiLine = multiLine; this.defaultText = defaultText; this.password = password; - this.input = defaultText; + this.input = ""; } public void setInput(String input) { @@ -35,11 +41,20 @@ public class TextInputField extends BasicElement { @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); + RenderManager.drawHollowRoundRect(vg, x, y, width, height, OneConfigConfig.GRAY_700, 12f, 2f); super.update(x, y); int color = toggled ? OneConfigConfig.WHITE : OneConfigConfig.WHITE_60; - float width = RenderManager.getTextWidth(vg, input, 14f); + float width; + StringBuilder s = new StringBuilder(); + int offset = 12; + if(!password) { + width = RenderManager.getTextWidth(vg, input.substring(0, caretPos), 14f); + } else { + for(int i = 0; i < input.length(); i++) { + s.append("*"); + } + width = RenderManager.getTextWidth(vg, s.substring(0, caretPos), 14f); + } if(toggled) { RenderManager.drawLine(vg, x + width + 12, (float) y + 7, x + width + 13, (float) y + height - 7, 1, OneConfigConfig.WHITE); @@ -48,8 +63,14 @@ public class TextInputField extends BasicElement { 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); + nvgScissor(vg, x, y, this.width, height); + if(!password) { + RenderManager.drawString(vg, input, x + offset, y + 17, color, 14f, Fonts.INTER_REGULAR); + nvgResetScissor(vg); + } else { + RenderManager.drawString(vg, s.toString(), x + offset, y + 17, color, 14f, Fonts.INTER_REGULAR); + } } public void keyTyped(char c, int key) { @@ -58,23 +79,48 @@ public class TextInputField extends BasicElement { if(key == Keyboard.KEY_BACK) { try { input = input.substring(0, input.lastIndexOf(" ")); + caretPos = input.length(); } catch (Exception e) { input = ""; + caretPos = 0; } } return; } if (key == Keyboard.KEY_BACK) { if (input.length() > 0) { - input = input.substring(0, input.length() - 1); - return; + if(caretPos == input.length()) { + input = input.substring(0, input.length() - 1); + } else { + input = input.substring(0, caretPos - 1) + input.substring(caretPos); + } + caretPos--; } + return; } if(key == Keyboard.KEY_TAB) { input += " "; + caretPos += 4; + return; + } + + if (key == Keyboard.KEY_RIGHT) { + caretPos++; + if(caretPos > input.length()) { + caretPos = input.length(); + } + return; + } + if (key == Keyboard.KEY_LEFT) { + caretPos--; + if(caretPos < 0) { + caretPos = 0; + } return; } + + if(key == Keyboard.KEY_RETURN) { toggled = false; } @@ -82,7 +128,12 @@ public class TextInputField extends BasicElement { 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; + input = addCharAtPoint(caretPos, c); + caretPos++; } } + + private @NotNull String addCharAtPoint(int index, char c) { + return input.substring(0, index) + c + input.substring(index); + } } |