diff options
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/gui')
3 files changed, 85 insertions, 23 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/animations/ColorAnimation.java b/src/main/java/cc/polyfrost/oneconfig/gui/animations/ColorAnimation.java index 2dcd634..98b2377 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/animations/ColorAnimation.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/animations/ColorAnimation.java @@ -24,6 +24,13 @@ public class ColorAnimation { alphaAnimation = new DummyAnimation(palette.getNormalColorf()[3]); } + /** + * Return the current color at the current time, according to a EaseInOut quadratic animation. + * + * @param hovered the hover state of the element + * @param pressed the pressed state of the element + * @return the current color + */ public int getColor(boolean hovered, boolean pressed) { int state = pressed ? 2 : hovered ? 1 : 0; if (state != prevState) { @@ -37,6 +44,13 @@ public class ColorAnimation { return ((int) (alphaAnimation.get() * 255) << 24) | ((int) (redAnimation.get() * 255) << 16) | ((int) (greenAnimation.get() * 255) << 8) | ((int) (blueAnimation.get() * 255)); } + /** + * Return the current alpha of the color. This method is used to get the alpha of pressed buttons that have text/icons on them, so they also darken accordingly. + */ + public float getAlpha() { + return alphaAnimation.get(0); + } + public ColorPalette getPalette() { return palette; } 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 40acff6..a55d931 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java @@ -6,25 +6,27 @@ 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.color.ColorPalette; +import cc.polyfrost.oneconfig.utils.color.ColorUtils; import org.jetbrains.annotations.NotNull; public class BasicButton extends BasicElement { + protected String text; + protected SVGs icon1, icon2; + private final int alignment; + private final float fontSize, cornerRadius; + private float alpha; + 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 = 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; - public static final int CUSTOM_COLOR = -100; - private final int alignment; - private final float fontSize, cornerRadius; - private final int xSpacing, xPadding; - private final int iconSize; - public int x, y; - protected String text; - protected SVGs icon1, icon2; private boolean toggleable = false; private Page page; private Runnable runnable; @@ -62,11 +64,12 @@ public class BasicButton extends BasicElement { if (disabled) RenderManager.setAlpha(vg, 0.5f); float contentWidth = 0f; int color = -1; - if (colorPalette == ColorPalette.TERTIARY || colorPalette == ColorPalette.TERTIARY_DESTRUCTIVE) + if (colorPalette == ColorPalette.TERTIARY || colorPalette == ColorPalette.TERTIARY_DESTRUCTIVE) { color = currentColor; - else + } else { RenderManager.drawRoundedRect(vg, x, y, this.width, this.height, currentColor, this.cornerRadius); - + color = ColorUtils.getColor(1f, 1f, 1f, alpha); + } final float middle = x + width / 2f; final float middleYIcon = y + height / 2f - iconSize / 2f; final float middleYText = y + height / 2f + fontSize / 8f; @@ -103,7 +106,6 @@ public class BasicButton extends BasicElement { } if (text != null) { RenderManager.drawText(vg, text, x + contentWidth, middleYText, color, fontSize, Fonts.MEDIUM); - contentWidth += RenderManager.getTextWidth(vg, text, fontSize, Fonts.MEDIUM) + xSpacing; } if (icon2 != null) RenderManager.drawSvg(vg, icon2, x + width - xPadding - iconSize, middleYIcon, iconSize, iconSize, color); @@ -111,6 +113,11 @@ public class BasicButton extends BasicElement { if (disabled) RenderManager.setAlpha(vg, 1f); } + @Override + public void update(int x, int y) { + super.update(x, y); + this.alpha = colorAnimation.getAlpha(); + } @Override public void onClick() { diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java index aab94e5..f0b930c 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java @@ -8,19 +8,49 @@ import org.jetbrains.annotations.NotNull; import org.lwjgl.input.Mouse; public class BasicElement { - protected final float radius; - public int currentColor; protected int width, height; + /** + * The color palette used for this element. + */ protected ColorPalette colorPalette; + /** + * hitBoxX and hitBoxY are integer variables to determine (in pixels) how far past the boundaries of this button it is still able to be interacted with. + */ protected int hitBoxX, hitBoxY; protected boolean hoverFx; + /** + * Whether the element is currently being hovered over + */ protected boolean hovered = false; + /** + * Whether the mouse is actively being held down on the element. + */ protected boolean pressed = false; + /** + * Whether the element is clicked. + */ protected boolean clicked = false; + /** + * The toggle state of the button. Its false, then if it is clicked, it becomes true, and if clicked again, it becomes false. + */ protected boolean toggled = false; + /** + * Whether the element is currently disabled. + */ protected boolean disabled = false; - protected ColorAnimation colorAnimation; + /** + * The ARGB color of this element. + */ + public int currentColor; + protected final float radius; + /** + * Boolean to determine if this element is allowed to be clicked when {@link InputUtils#isBlockingClicks()} is true. + */ private boolean block = false; + /** + * The color animation used by this element. + */ + protected ColorAnimation colorAnimation; public BasicElement(int width, int height, @NotNull ColorPalette colorPalette, boolean hoverFx) { this(width, height, colorPalette, hoverFx, 12f); @@ -40,11 +70,22 @@ public class BasicElement { } + /** + * Draw script for the element. + * <br> <b>Make sure to call {@link #update(int x, int y)} to update the elements states!</b> + * + * @param vg NanoVG context (see {@link RenderManager}) + * @param x x position of the element + * @param y y position of the element + */ public void draw(long vg, int x, int y) { this.update(x, y); RenderManager.drawRoundedRect(vg, x, y, width, height, currentColor, radius); } + /** + * Update this element's clicked, hovered, toggled, and pressed states, invoke any necessary methods, and update the color animation. + */ public void update(int x, int y) { if (disabled) { hovered = false; @@ -79,6 +120,14 @@ public class BasicElement { hitBoxY = y; } + public void setWidth(int width) { + this.width = width; + } + + public void setHeight(int height) { + this.height = height; + } + public void setColorPalette(ColorPalette colorPalette) { if (this.colorPalette.equals(ColorPalette.TERTIARY) || this.colorPalette.equals(ColorPalette.TERTIARY_DESTRUCTIVE)) this.colorAnimation.setColors(colorPalette.getNormalColorf()); @@ -90,18 +139,10 @@ public class BasicElement { return width; } - public void setWidth(int width) { - this.width = width; - } - public int getHeight() { return height; } - public void setHeight(int height) { - this.height = height; - } - public boolean isHovered() { return hovered; } |