diff options
8 files changed, 131 insertions, 38 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; } diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java index a0bb93c..faf3e13 100644 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java @@ -21,14 +21,14 @@ import java.util.HashMap; * @see SVGs */ public final class ImageLoader { - public static ImageLoader INSTANCE = new ImageLoader(); - private final HashMap<String, Integer> imageHashMap = new HashMap<>(); - private final HashMap<String, Integer> svgHashMap = new HashMap<>(); - private ImageLoader() { } + private final HashMap<String, Integer> imageHashMap = new HashMap<>(); + private final HashMap<String, Integer> svgHashMap = new HashMap<>(); + public static ImageLoader INSTANCE = new ImageLoader(); + /** * Loads an image from resources. * diff --git a/src/main/java/cc/polyfrost/oneconfig/mixin/FontRendererMixin.java b/src/main/java/cc/polyfrost/oneconfig/mixin/FontRendererMixin.java new file mode 100644 index 0000000..7eac19d --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/mixin/FontRendererMixin.java @@ -0,0 +1,15 @@ +package cc.polyfrost.oneconfig.mixin; + + +import net.minecraft.client.gui.FontRenderer; +import org.spongepowered.asm.mixin.Mixin; + +@Mixin(FontRenderer.class) +public class FontRendererMixin { + + //@Inject(method = "renderDefaultChar", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/FontRenderer;bindTexture(Lnet/minecraft/util/ResourceLocation;)V", shift = At.Shift.AFTER)) + //public void whoAsked(int ch, boolean italic, CallbackInfoReturnable<Float> cir) { + // GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); + // GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); + //} +} diff --git a/src/main/java/cc/polyfrost/oneconfig/test/ButtonTestPage.java b/src/main/java/cc/polyfrost/oneconfig/test/ButtonTestPage.java index 5e2a864..fb137db 100644 --- a/src/main/java/cc/polyfrost/oneconfig/test/ButtonTestPage.java +++ b/src/main/java/cc/polyfrost/oneconfig/test/ButtonTestPage.java @@ -9,29 +9,44 @@ import java.util.ArrayList; public class ButtonTestPage extends Page { private final ArrayList<BasicButton> row1 = new ArrayList<BasicButton>() {{ - add(new BasicButton(254, 48, "Primary", BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY)); - add(new BasicButton(254, 48, "Primary Destructive", BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY_DESTRUCTIVE)); - add(new BasicButton(254, 48, "Secondary", BasicButton.ALIGNMENT_CENTER, ColorPalette.SECONDARY)); - add(new BasicButton(254, 48, "Secondary Destructive", BasicButton.ALIGNMENT_CENTER, ColorPalette.SECONDARY_DESTRUCTIVE)); - add(new BasicButton(254, 48, "Tertiary", BasicButton.ALIGNMENT_CENTER, ColorPalette.TERTIARY)); - add(new BasicButton(254, 48, "Tertiary Destructive", BasicButton.ALIGNMENT_CENTER, ColorPalette.TERTIARY_DESTRUCTIVE)); + add(new BasicButton(254, 48, "Primary", BasicButton.ALIGNMENT_LEFT, ColorPalette.PRIMARY)); + add(new BasicButton(254, 48, "Primary Destructive", BasicButton.ALIGNMENT_LEFT, ColorPalette.PRIMARY_DESTRUCTIVE)); + add(new BasicButton(254, 48, "Secondary", BasicButton.ALIGNMENT_LEFT, ColorPalette.SECONDARY)); + add(new BasicButton(254, 48, "Secondary Destructive", BasicButton.ALIGNMENT_LEFT, ColorPalette.SECONDARY_DESTRUCTIVE)); + add(new BasicButton(254, 48, "Tertiary", BasicButton.ALIGNMENT_LEFT, ColorPalette.TERTIARY)); + add(new BasicButton(254, 48, "Tertiary Destructive", BasicButton.ALIGNMENT_LEFT, ColorPalette.TERTIARY_DESTRUCTIVE)); + add(new BasicButton(254, 48, "LeftIconLA", SVGs.BOX, null, BasicButton.ALIGNMENT_LEFT, ColorPalette.SECONDARY)); + add(new BasicButton(254, 48, "RightIconLA", null, SVGs.BOX, BasicButton.ALIGNMENT_LEFT, ColorPalette.SECONDARY)); + add(new BasicButton(254, 48, "LeftIconCA", SVGs.BOX, null, BasicButton.ALIGNMENT_CENTER, ColorPalette.SECONDARY)); + add(new BasicButton(254, 48, "RightIconCA", null, SVGs.BOX, BasicButton.ALIGNMENT_CENTER, ColorPalette.SECONDARY)); }}; private final ArrayList<BasicButton> row2 = new ArrayList<BasicButton>() {{ - add(new BasicButton(170, 32, "32", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY)); - add(new BasicButton(193, 36, "36", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY)); - add(new BasicButton(208, 40, "40", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY)); - add(new BasicButton(254, 48, "48", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY)); + add(new BasicButton(170, 32, "32", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_LEFT, ColorPalette.PRIMARY)); + add(new BasicButton(193, 36, "36", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_LEFT, ColorPalette.PRIMARY)); + add(new BasicButton(208, 40, "40", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_LEFT, ColorPalette.PRIMARY)); + add(new BasicButton(254, 48, "48", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_LEFT, ColorPalette.PRIMARY)); + add(new BasicButton(254, 48, "JustifiedRight", null, SVGs.BOX, BasicButton.ALIGNMENT_JUSTIFIED, ColorPalette.SECONDARY_DESTRUCTIVE)); + add(new BasicButton(254, 48, "JustifiedLeft", SVGs.BOX, null, BasicButton.ALIGNMENT_JUSTIFIED, ColorPalette.SECONDARY_DESTRUCTIVE)); + add(new BasicButton(254, 48, "JustifiedNoIcon", null, null, BasicButton.ALIGNMENT_JUSTIFIED, ColorPalette.PRIMARY)); + add(new BasicButton(254, 48, "TertiaryIcon", SVGs.BOX, null, BasicButton.ALIGNMENT_JUSTIFIED, ColorPalette.TERTIARY_DESTRUCTIVE)); + }}; private final ArrayList<BasicButton> row3 = new ArrayList<BasicButton>() {{ add(new BasicButton(254, 48, "Left", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_LEFT, ColorPalette.PRIMARY)); add(new BasicButton(254, 48, "Center", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY)); add(new BasicButton(254, 48, "Justified", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_JUSTIFIED, ColorPalette.PRIMARY)); + add(new BasicButton(254, 48, "Disabled :(", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_LEFT, ColorPalette.PRIMARY)); + add(new BasicButton(254, 48, "Disabled :(", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY)); + add(new BasicButton(254, 48, "Disabled :(", SVGs.BOX, SVGs.BOX, BasicButton.ALIGNMENT_JUSTIFIED, ColorPalette.PRIMARY)); }}; public ButtonTestPage() { super("Buttons"); + row3.get(3).disable(true); + row3.get(4).disable(true); + row3.get(5).disable(true); } @Override diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorUtils.java index ebf0100..efadc51 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorUtils.java @@ -54,7 +54,7 @@ public final class ColorUtils { * @return the RGB color. */ public static int getColor(float red, float green, float blue, float alpha) { - return getColor((int) red * 255, (int) green * 255, (int) blue * 255, (int) alpha * 255); + return getColor((int) (red * 255f), (int) (green * 255f), (int) (blue * 255f), (int) (alpha * 255f)); } /** diff --git a/src/main/resources/mixins.oneconfig.json b/src/main/resources/mixins.oneconfig.json index fa23932..63a1e08 100644 --- a/src/main/resources/mixins.oneconfig.json +++ b/src/main/resources/mixins.oneconfig.json @@ -9,6 +9,7 @@ }, "verbose": true, "client": [ + "FontRendererMixin", "GuiIngameForgeMixin", "MinecraftMixin", "NetHandlerPlayClientMixin", |