diff options
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/utils')
4 files changed, 190 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java new file mode 100644 index 0000000..30f393e --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java @@ -0,0 +1,86 @@ +package cc.polyfrost.oneconfig.utils; + +import cc.polyfrost.oneconfig.config.OneConfigConfig; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +import java.awt.*; + +public class ColorUtils { + + public static int getColor(int currentColor, int colorPalette, boolean hover, boolean click) { + float[] color = splitColor(currentColor); + if (click) { + switch (colorPalette) { + case -2: + return new Color(0.9f, 0.9f, 0.9f, 0.2f).getRGB(); + case -1: + return OneConfigConfig.GRAY_500_80; + default: + case 2: + case 0: + return OneConfigConfig.GRAY_400_80; + case 1: + return OneConfigConfig.BLUE_600_80; + } + } + + switch (colorPalette) { + case -2: + return getColorComponents(color, splitColor(OneConfigConfig.TRANSPARENT), new float[]{0.9f, 0.9f, 0.9f, 0.3f}, hover, 20f); + case -1: + return getColorComponents(color, splitColor(OneConfigConfig.TRANSPARENT), splitColor(OneConfigConfig.GRAY_500), hover, 10f); + default: + case 0: + return getColorComponents(color, splitColor(OneConfigConfig.GRAY_600), splitColor(OneConfigConfig.GRAY_300), hover, 25f); + case 1: + return getColorComponents(color, splitColor(OneConfigConfig.BLUE_600), splitColor(OneConfigConfig.BLUE_500), hover, 150f); + case 2: + return getColorComponents(color, splitColor(OneConfigConfig.GRAY_500), splitColor(OneConfigConfig.GRAY_300), hover, 50f); + + } + + } + + /** + * Smooths the transition of a color between two colors. + * + * @param currentColor the current color (also the one you want to change) + * @param direction false to move towards initColor, true to move towards finalColor + * @param speed speed of the transition + * @return currentColor but with the new color + */ + public static int smoothColor(int currentColor, int initColor, int finalColor, boolean direction, float speed) { + float[] init = splitColor(initColor); + float[] finalC = splitColor(finalColor); + float[] current = splitColor(currentColor); + return getColorComponents(current, init, finalC, direction, speed); + } + + @Contract(value = "_ -> new", pure = true) + private static float @NotNull [] splitColor(int color) { + return new float[]{(color >> 16 & 255) / 255f, (color >> 8 & 255) / 255f, (color & 255) / 255f, (color >> 24 & 255) / 255f}; + } + + private static int getColorComponents(float[] currentColor, float[] initColor, float[] finalColor, boolean hover, float speed) { + currentColor[0] = smooth(currentColor[0], initColor[0], finalColor[0], hover, speed); + currentColor[1] = smooth(currentColor[1], initColor[1], finalColor[1], hover, speed); + currentColor[2] = smooth(currentColor[2], initColor[2], finalColor[2], hover, speed); + currentColor[3] = smooth(currentColor[3], initColor[3], finalColor[3], hover, speed); + + return new Color(currentColor[0], currentColor[1], currentColor[2], currentColor[3]).getRGB(); + + } + + private static float smooth(float current, float min, float max, boolean moveToFinal, float speed) { + current = MathUtils.easeOut(current, moveToFinal ? 1f : 0f, speed); + if (current <= min) { + current = min; + } + + if (current >= max) { + current = max; + } + return current; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java new file mode 100644 index 0000000..806f6de --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java @@ -0,0 +1,34 @@ +package cc.polyfrost.oneconfig.utils; + +import cc.polyfrost.oneconfig.gui.OneConfigGui; +import net.minecraft.client.Minecraft; +import org.lwjgl.input.Mouse; + +public class InputUtils { + /** + * function to determine weather the mouse is currently over a specific region. Uses the current nvgScale to fix to any scale. + * + * @return true if mouse is over region, false if not. + */ + public static boolean isAreaHovered(int x, int y, int width, int height) { + int mouseX = Mouse.getX(); + int mouseY = Minecraft.getMinecraft().displayHeight - Math.abs(Mouse.getY()); + return mouseX > x && mouseY > y && mouseX < x + width && mouseY < y + height; // TODO add scaling info + } + + public static boolean isAreaClicked(int x, int y, int width, int height) { + return isAreaHovered(x, y, width, height) && isClicked(); + } + + public static boolean isClicked() { + return OneConfigGui.INSTANCE != null && OneConfigGui.INSTANCE.mouseDown && !Mouse.isButtonDown(0); + } + + public static int mouseX() { + return Mouse.getX(); + } + + public static int mouseY() { + return Minecraft.getMinecraft().displayHeight - Math.abs(Mouse.getY()); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/MathUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/MathUtils.java new file mode 100644 index 0000000..2faec69 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/MathUtils.java @@ -0,0 +1,30 @@ +package cc.polyfrost.oneconfig.utils; + +public class MathUtils { + public static float clamp(float number) { + return number < (float) 0.0 ? (float) 0.0 : Math.min(number, (float) 1.0); + } + + public static float easeOut(float current, float goal, float speed) { + if (Math.floor(Math.abs(goal - current) / (float) 0.01) > 0) { + return current + (goal - current) / speed; + } else { + return goal; + } + } + + + public static float easeInQuad(float current) { + return current * current; + } + + /** + * taken from <a href="https://github.com/jesusgollonet/processing-penner-easing">https://github.com/jesusgollonet/processing-penner-easing</a> + */ + public static float easeInOutCirc(float t, float b, float c, float d) { + if ((t /= d / 2) < 1) return -c / 2 * ((float) Math.sqrt(1 - t * t) - 1) + b; + return c / 2 * ((float) Math.sqrt(1 - (t -= 2) * t) + 1) + b; + } + + +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java b/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java new file mode 100644 index 0000000..f467976 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java @@ -0,0 +1,40 @@ +package cc.polyfrost.oneconfig.utils; + +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; + +public class TickDelay { + private int delay; + private final Runnable function; + + public TickDelay(Runnable functionName, int ticks) { + register(); + delay = ticks; + function = functionName; + } + + @SubscribeEvent + public void onTick(TickEvent.ClientTickEvent event) { + if (event.phase == TickEvent.Phase.START) { + // Delay expired + if (delay < 1) { + run(); + destroy(); + } + delay--; + } + } + + private void destroy() { + MinecraftForge.EVENT_BUS.unregister(this); + } + + private void register() { + MinecraftForge.EVENT_BUS.register(this); + } + + private void run() { + function.run(); + } +} |