From a183b82c9ee063a6377554ea8c797953b1ded994 Mon Sep 17 00:00:00 2001 From: nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> Date: Wed, 25 May 2022 18:45:34 +0100 Subject: OC-38 almost finish basic button rewrite --- .../oneconfig/gui/elements/BasicButton.java | 225 ++++++++++++--------- .../oneconfig/gui/elements/ColorSelector.java | 4 +- .../gui/elements/config/ConfigButton.java | 6 +- .../gui/elements/config/ConfigKeyBind.java | 4 +- 4 files changed, 138 insertions(+), 101 deletions(-) (limited to 'src/main/java/cc/polyfrost/oneconfig/gui/elements') diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java index 24ea612..58638fd 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java @@ -7,130 +7,155 @@ import cc.polyfrost.oneconfig.lwjgl.RenderManager; import cc.polyfrost.oneconfig.lwjgl.font.Fonts; import cc.polyfrost.oneconfig.lwjgl.image.SVGs; import cc.polyfrost.oneconfig.utils.ColorUtils; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; + +import java.util.zip.ZipEntry; public class BasicButton extends BasicElement { protected String text; - protected SVGs fileNameLeftIco, fileNameRightIco; - private final int thisAlignment; - private final float fontSize; - private final int colorPalette; + protected SVGs icon1, icon2; + private final int alignment, colorPalette; + private final float fontSize, cornerRadius; + private final int xSpacing, xPadding; + private final int iconSize; public int x, y; public static final int ALIGNMENT_LEFT = 0; - public static final int ALIGNMENT_CENTER = 1; - private boolean toggleable; + @Deprecated + public static final int ALIGNMENT_RIGHT = 1; + public static final int ALIGNMENT_CENTER = 2; + public static final int ALIGNMENT_JUSTIFIED = 3; + + public static final int SIZE_32 = 32; + public static final int SIZE_36 = 36; + public static final int SIZE_40 = 40; + public static final int SIZE_48 = 48; + private boolean toggleable = false; private Page page; private Runnable runnable; - private boolean alignIconLeft = false; - - /** - * 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 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 SVGs fileNameLeftIco, @Nullable SVGs fileNameRightIco, int colorPalette, int alignment) { - super(width, height, colorPalette, true); - this.text = text; - if (fileNameLeftIco != null) this.fileNameLeftIco = fileNameLeftIco; - if (fileNameRightIco != null) this.fileNameRightIco = fileNameRightIco; - this.thisAlignment = alignment; - if (colorPalette == -2) { - fontSize = 24f; - this.colorPalette = -1; - } else { - if (colorPalette == 0) fontSize = 12; - else fontSize = 14f; - this.colorPalette = colorPalette; - } + + public BasicButton(int width, int size, String text, int align, int colorPalette) { + this(width, size, text, null, null, colorPalette, align); } - public BasicButton(int width, int height, @NotNull String text, @Nullable SVGs fileNameLeftIco, @Nullable SVGs fileNameRightIco, int colorPalette, int alignment, Page page) { - this(width, height, text, fileNameLeftIco, fileNameRightIco, colorPalette, alignment); - this.page = page; + public BasicButton(int width, int size, String text, SVGs icon1, SVGs icon2, int align, int colorPalette) { + super(width, 32, colorPalette, true); + if(text != null) this.text = text; + if (icon1 != null) this.icon1 = icon1; + if (icon2 != null) this.icon2 = icon2; + this.colorPalette = colorPalette; + this.alignment = align; + this.cornerRadius = size == SIZE_48 ? 16f : 12f; + this.xSpacing = size == SIZE_48 ? 12 : 8; + if(size == SIZE_36 || size == SIZE_40) { + this.xPadding = 16; + } else this.xPadding = size == SIZE_48 ? 20 : 12; + this.height = size; + this.iconSize = this.height / 2; + this.fontSize = size == SIZE_48 ? 20 : (float) (size / 2 - 4); } - public BasicButton(int width, int height, @NotNull String text, @Nullable SVGs fileNameLeftIco, @Nullable SVGs fileNameRightIco, int colorPalette, int alignment, boolean toggleable) { - this(width, height, text, fileNameLeftIco, fileNameRightIco, colorPalette, alignment); - this.toggleable = toggleable; + public BasicButton(int width, int size, SVGs icon, int align, int colorPalette) { + this(width, size, null, icon, null, align, colorPalette); } - public BasicButton(int width, int height, @NotNull String text, @Nullable SVGs fileNameLeftIco, @Nullable SVGs fileNameRightIco, int colorPalette, int alignment, Runnable runnable) { - this(width, height, text, fileNameLeftIco, fileNameRightIco, colorPalette, alignment); - this.runnable = runnable; + public void setToggleable(boolean state) { + this.toggleable = state; } - public BasicButton(int width, int height, @NotNull String text, @Nullable SVGs fileNameLeftIco, @Nullable SVGs fileNameRightIco, int colorPalette, int alignment, boolean toggleable, Runnable runnable) { - this(width, height, text, fileNameLeftIco, fileNameRightIco, colorPalette, alignment, runnable); - this.toggleable = toggleable; + public void setClickAction(Page page) { + this.page = page; + } + + public void setClickAction(Runnable runnable) { + this.runnable = runnable; } @Override public void draw(long vg, int x, int y) { this.x = x; this.y = y; + RenderManager.drawRoundedRect(vg, x, y, this.width, this.height, currentColor, this.cornerRadius); + float contentWidth = 0f; int textColor = -1; - RenderManager.drawRectangle(vg, x, y, this.width, this.height, this.currentColor); - float contentWidth = RenderManager.getTextWidth(vg, text, fontSize, Fonts.MEDIUM); - if (fileNameLeftIco != null && !alignIconLeft) { - contentWidth += 28; - } - if (fileNameRightIco != null) { - contentWidth += 28; + final float middle = x + width / 2f; + final float middleYIcon = y + height / 2f - iconSize / 2f; + final float middleYText = y + height / 2f + fontSize / 8f; + if(this.text != null) { + if (this.colorPalette == -2) { + textColor = OneConfigConfig.WHITE_80; + if (hovered) textColor = OneConfigConfig.WHITE; + if (clicked) textColor = OneConfigConfig.WHITE_80; + if (page == null) textColor = OneConfigConfig.WHITE_50; + } + contentWidth += RenderManager.getTextWidth(vg, text, fontSize, Fonts.MEDIUM); } - - if (this.colorPalette == -3) { - textColor = OneConfigConfig.WHITE_80; - if (hovered) textColor = OneConfigConfig.WHITE; - if (clicked) textColor = OneConfigConfig.WHITE_80; - if (page == null) textColor = OneConfigConfig.WHITE_50; + if(alignment == ALIGNMENT_CENTER) { + if (icon1 != null) { + contentWidth += iconSize + xSpacing; + } + if (icon2 != null) { + contentWidth += iconSize + xSpacing; + } + if(text != null) { + RenderManager.drawString(vg, text, middle - contentWidth / 2 + (icon1 == null ? 0 : iconSize + xSpacing), middleYText, textColor, fontSize, Fonts.MEDIUM); + } + if(icon1 != null) { + RenderManager.drawSvg(vg, icon1, middle - contentWidth / 2, middleYIcon, iconSize, iconSize); + } + if(icon2 != null) { + RenderManager.drawSvg(vg, icon2, middle + contentWidth / 2 - iconSize, middleYIcon, iconSize, iconSize); + } + this.update(x, y); + return; } - - if (thisAlignment == ALIGNMENT_CENTER) { - int middle = x + this.width / 2; - if (alignIconLeft) - RenderManager.drawString(vg, text, middle - contentWidth / 2, y + ((float) height / 2) + 1, textColor, fontSize, Fonts.MEDIUM); - else - RenderManager.drawString(vg, text, middle - contentWidth / 2 + (fileNameLeftIco != null ? 28 : 0), y + ((float) height / 2) + 1, textColor, fontSize, Fonts.MEDIUM); - if (fileNameLeftIco != null) { - if (alignIconLeft) RenderManager.drawSvg(vg, fileNameLeftIco, x + 12, y + height / 2f - 10, 20, 20); - else RenderManager.drawSvg(vg, fileNameLeftIco, middle - contentWidth / 2, y + 8, 20, 20); + if(alignment == ALIGNMENT_JUSTIFIED) { + if(text != null) { + RenderManager.drawString(vg, text, middle - contentWidth / 2, middleYText, textColor, fontSize, Fonts.MEDIUM); } - if (fileNameRightIco != null) { - RenderManager.drawSvg(vg, fileNameRightIco, middle + contentWidth / 2 - (fileNameLeftIco != null ? 20 : 24), y + 8, 20, 20); + if(icon1 != null) { + RenderManager.drawSvg(vg, icon1, x + xSpacing, middleYIcon, iconSize, iconSize); } + if(icon2 != null) { + RenderManager.drawSvg(vg, icon2, x + width - xSpacing - iconSize, middleYIcon, iconSize, iconSize); + } + this.update(x, y); + return; } - if (thisAlignment == ALIGNMENT_LEFT) { - if (fileNameLeftIco != null) { - RenderManager.drawSvg(vg, fileNameLeftIco, x + 12, y + 8, 20, 20, textColor); - RenderManager.drawString(vg, text, x + 40, y + ((float) height / 2) + 1, textColor, fontSize, Fonts.MEDIUM); - } else { - RenderManager.drawString(vg, text, x + 12, y + ((float) height / 2) + 1, textColor, fontSize, Fonts.MEDIUM); + if(alignment == ALIGNMENT_LEFT) { + contentWidth = xSpacing; + if(icon1 != null) { + RenderManager.drawSvg(vg, icon1, x + contentWidth, middleYIcon, iconSize, iconSize); + contentWidth += iconSize + xSpacing; + } + if(text != null) { + RenderManager.drawString(vg, text, x + contentWidth, middleYText, textColor, fontSize, Fonts.MEDIUM); + contentWidth += RenderManager.getTextWidth(vg, text, fontSize, Fonts.MEDIUM) + xSpacing; } - if (fileNameRightIco != null) { - RenderManager.drawSvg(vg, fileNameRightIco, x + width - 28, y + 8, 20, 20); + if(icon2 != null) { + RenderManager.drawSvg(vg, icon2, x + contentWidth, middleYIcon, iconSize, iconSize); } + this.update(x, y); + return; } - this.update(x, y); - if (hoverFx) { - if (colorPalette == -3) { - currentColor = OneConfigConfig.TRANSPARENT; - return; + if(alignment == ALIGNMENT_RIGHT) { + contentWidth = width - xSpacing; + if(icon2 != null) { + contentWidth -= iconSize; + RenderManager.drawSvg(vg, icon2, x + contentWidth, middleYIcon, iconSize, iconSize); + contentWidth -= xSpacing; } - if (!toggleable) { - currentColor = ColorUtils.getColor(currentColor, colorPalette, hovered, clicked); - } else { - if (toggled) { - currentColor = ColorUtils.smoothColor(currentColor, OneConfigConfig.GRAY_500, OneConfigConfig.PRIMARY_600, true, 30f); - } else currentColor = ColorUtils.getColor(currentColor, colorPalette, hovered, clicked); + if(text != null) { + contentWidth -= RenderManager.getTextWidth(vg, text, fontSize, Fonts.MEDIUM); + RenderManager.drawString(vg, text, x + contentWidth, middleYText, textColor, fontSize, Fonts.MEDIUM); + contentWidth -= xSpacing; + } + if(icon1 != null) { + contentWidth -= iconSize; + RenderManager.drawSvg(vg, icon1, x + contentWidth, middleYIcon, iconSize, iconSize); } + this.update(x, y); } + } @@ -145,8 +170,22 @@ public class BasicButton extends BasicElement { @Override public void update(int x, int y) { - if (toggleable && toggled) return; super.update(x, y); + if (hoverFx) { + if (colorPalette == -2) { + currentColor = OneConfigConfig.TRANSPARENT; + return; + } + if (!toggleable) { + currentColor = ColorUtils.getColor(currentColor, colorPalette, hovered, clicked); + } else { + if (toggled) { + currentColor = ColorUtils.smoothColor(currentColor, OneConfigConfig.GRAY_500, OneConfigConfig.PRIMARY_600, true, 30f); + } else currentColor = ColorUtils.getColor(currentColor, colorPalette, hovered, clicked); + } + } + + } public Page getPage() { @@ -160,8 +199,4 @@ public class BasicButton extends BasicElement { public void setText(String text) { this.text = text; } - - public void alignIconLeft(boolean value) { - alignIconLeft = value; - } } diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java index 2040364..3607d24 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java @@ -9,8 +9,8 @@ import cc.polyfrost.oneconfig.lwjgl.RenderManager; import cc.polyfrost.oneconfig.lwjgl.font.Fonts; import cc.polyfrost.oneconfig.lwjgl.image.Images; import cc.polyfrost.oneconfig.lwjgl.image.SVGs; -import cc.polyfrost.oneconfig.utils.IOUtils; import cc.polyfrost.oneconfig.utils.InputUtils; +import cc.polyfrost.oneconfig.utils.InternetUtils; import cc.polyfrost.oneconfig.utils.MathUtils; import org.lwjgl.input.Mouse; @@ -322,7 +322,7 @@ public class ColorSelector { hueInput.setInput(String.format("%.01f", (float) color.getHue())); hexInput.setInput("#" + color.getHex()); } - if(guideBtn.isClicked()) IOUtils.browseLink("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); + if(guideBtn.isClicked()) InternetUtils.browseLink("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); // draw the color preview diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java index afedef4..3caa51f 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java @@ -13,12 +13,14 @@ public class ConfigButton extends BasicOption { public ConfigButton(Runnable runnable, Object parent, String name, int size, String text) { super(null, parent, name, size); - this.button = new BasicButton(size == 1 ? 128 : 256, 32, text, null, null, 1, BasicButton.ALIGNMENT_CENTER, runnable); + this.button = new BasicButton(size == 1 ? 128 : 256, 32, text, BasicButton.ALIGNMENT_CENTER, 1); + this.button.setClickAction(runnable); } public ConfigButton(Field field, Object parent, String name, int size, String text) { super(field, parent, name, size); - this.button = new BasicButton(size == 1 ? 128 : 256, 32, text, null, null, 1, BasicButton.ALIGNMENT_CENTER, getRunnableFromField(field, parent)); + this.button = new BasicButton(size == 1 ? 128 : 256, 32, text, BasicButton.ALIGNMENT_CENTER, 1); + this.button.setClickAction(getRunnableFromField(field, parent)); } @Override diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigKeyBind.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigKeyBind.java index 4ea65f9..898d6d7 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigKeyBind.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigKeyBind.java @@ -18,8 +18,8 @@ public class ConfigKeyBind extends BasicOption { public ConfigKeyBind(Field field, Object parent, String name, int size) { super(field, parent, name, size); - button = new BasicButton(256, 32, "", SVGs.KEYSTROKE, null, 0, BasicButton.ALIGNMENT_CENTER, true); - button.alignIconLeft(true); + button = new BasicButton(256, 32, "", SVGs.KEYSTROKE, null, BasicButton.ALIGNMENT_LEFT, 0); + button.setToggleable(true); } @Override -- cgit