Skip to content

Commit

Permalink
Option complexity, some changes to animations, MathAnimations, change…
Browse files Browse the repository at this point in the history
…s to color option, and chroma text for TextWidget rainbow.
  • Loading branch information
tanishisherewithhh committed Feb 10, 2025
1 parent 59d1de4 commit b0dbca6
Show file tree
Hide file tree
Showing 26 changed files with 335 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void onInitializeClient() {
DHIntegration.initAfter();

// Get the instance of AbstractMoveableScreen
screen = Objects.requireNonNull(DHIntegration.getMovableScreen());
screen = Objects.requireNonNull(DHIntegration.getMovableScreen(), "AbstractMovableScreen instance should not be null!");

// Get the keybind to open the screen instance
binding = DHIntegration.getKeyBind();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,6 @@
* This interface provides methods for integrating DynamicHud into a mod.
*/
public interface DynamicHudIntegration {
/**
* The category for the key binding.
*/
String KEYBIND_CATEGORY = "DynamicHud";

/**
* The translation key for the editor screen.
*/
String TRANSLATION_KEY = "DynamicHud Editor Screen";

/**
* The input type for the key binding.
*/
InputUtil.Type INPUT_TYPE = InputUtil.Type.KEYSYM;

/**
* The key code for the key binding.
*/
int KEY = GLFW.GLFW_KEY_RIGHT_SHIFT;

/**
* The key binding for opening the editor screen.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package com.tanishisherewith.dynamichud.config;

import com.tanishisherewith.dynamichud.helpers.ColorHelper;
import dev.isxander.yacl3.api.*;
import dev.isxander.yacl3.api.controller.BooleanControllerBuilder;
import dev.isxander.yacl3.api.controller.ColorControllerBuilder;
import dev.isxander.yacl3.api.controller.FloatSliderControllerBuilder;
import dev.isxander.yacl3.api.controller.IntegerFieldControllerBuilder;
import dev.isxander.yacl3.api.controller.*;
import dev.isxander.yacl3.config.v2.api.ConfigClassHandler;
import dev.isxander.yacl3.config.v2.api.SerialEntry;
import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder;
import dev.isxander.yacl3.config.v2.impl.autogen.ColorFieldImpl;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import java.awt.*;
import java.util.LinkedList;

public final class GlobalConfig {
public static final ConfigClassHandler<GlobalConfig> HANDLER = ConfigClassHandler.createBuilder(GlobalConfig.class)
Expand All @@ -39,9 +33,14 @@ public final class GlobalConfig {

@SerialEntry
private boolean showColorPickerPreview = true;

@SerialEntry
private boolean forceSameContextMenuSkin = true;

//These package names are getting seriously long
@SerialEntry
private com.tanishisherewith.dynamichud.utils.contextmenu.options.Option.Complexity complexity = com.tanishisherewith.dynamichud.utils.contextmenu.options.Option.Complexity.Simple;

@SerialEntry
private int snapSize = 100;

Expand Down Expand Up @@ -101,6 +100,15 @@ public Screen createYACLGUI() {
.binding(new Color(255, 0, 0, 128), () -> this.hudInactiveColor, newVal -> this.hudInactiveColor = newVal)
.controller(ColorControllerBuilder::create)
.build())
.option(Option.<com.tanishisherewith.dynamichud.utils.contextmenu.options.Option.Complexity>createBuilder()
.name(Text.literal("Settings Complexity"))
.description(OptionDescription.of(Text.literal("The level of options to display. Options equal to or below this level will be displayed")))
.binding(com.tanishisherewith.dynamichud.utils.contextmenu.options.Option.Complexity.Simple, () -> this.complexity, newVal -> this.complexity = newVal)
.controller((option) -> EnumControllerBuilder.create(option)
.enumClass(com.tanishisherewith.dynamichud.utils.contextmenu.options.Option.Complexity.class)
.formatValue(value -> Text.of(value.name()))
)
.build())
.build())
.save(HANDLER::save)
.build()
Expand Down Expand Up @@ -130,4 +138,8 @@ public Color getHudInactiveColor() {
public Color getHudActiveColor() {
return hudActiveColor;
}

public com.tanishisherewith.dynamichud.utils.contextmenu.options.Option.Complexity complexity(){
return complexity;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.tanishisherewith.dynamichud.helpers;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.util.math.MathHelper;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import java.awt.*;
import java.math.BigInteger;
import java.nio.ByteBuffer;

/**
* This class provides helper methods for working with colors.
Expand Down Expand Up @@ -168,6 +174,41 @@ public static Color changeAlpha(Color color, int alpha) {
return new Color(0);
}

public static int[] getMousePixelColor(double mouseX, double mouseY){
Framebuffer framebuffer = MinecraftClient.getInstance().getFramebuffer();
if (framebuffer != null) {
int x = (int) (mouseX * framebuffer.textureWidth / MinecraftClient.getInstance().getWindow().getScaledWidth());
int y = (int) ((MinecraftClient.getInstance().getWindow().getScaledHeight() - mouseY) * framebuffer.textureHeight / MinecraftClient.getInstance().getWindow().getScaledHeight());

try {
// Calculate the size of the buffer needed to store the texture data
int bufferSize = framebuffer.textureWidth * framebuffer.textureHeight * 4;
ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
// Bind the texture from the framebuffer
GL11.glBindTexture(GL11.GL_TEXTURE_2D, framebuffer.getColorAttachment());
// Read the texture data into the buffer
GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, buffer);

// Calculate the index of the pixel in the buffer
int index = (x + y * framebuffer.textureWidth) * 4;

// Check if the index is within the bounds of the buffer
if (index >= 0 && index + 3 < bufferSize) {
int blue = buffer.get(index) & 0xFF;
int green = buffer.get(index + 1) & 0xFF;
int red = buffer.get(index + 2) & 0xFF;

return new int[]{red,green,blue};
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.err.println("Framebuffer is null");
}
return null;
}

public static int fromRGBA(int r, int g, int b, int a) {
return (r << 16) + (g << 8) + (b) + (a << 24);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.text.Text;
import net.minecraft.util.Util;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.NotNull;
import org.joml.Matrix4f;
import org.lwjgl.opengl.GL40C;

Expand Down Expand Up @@ -248,6 +249,35 @@ public static void drawRainbowGradientRectangle(Matrix4f matrix4f, float x, floa
RenderSystem.disableBlend();
}

/**
* Draw chroma text (text with a nice rainbow effect)
* @param drawContext A drawContext object
* @param text The text to display
* @param x X pos of text
* @param y Y pos of text
* @param speed Speed of rainbow
* @param saturation Saturation of the rainbow colors
* @param brightness Brightness of the rainbow colors
* @param spread How much the color difference should be between each character (ideally between 0.001 to 0.2)
* @param shadow Whether to render the text as shadow.
*/
public static void drawChromaText(@NotNull DrawContext drawContext, String text, int x, int y, float speed, float saturation, float brightness, float spread, boolean shadow) {
long time = System.currentTimeMillis(); // Get the current time for animation
int length = text.length();

for (int i = 0; i < length; i++) {
// Calculate the hue for the current character
float hue = (time % (int) (5000 / speed)) / (5000f / speed) + (i * spread); // Adjust the hue based on time and character position
hue = MathHelper.floorMod(hue, 1.0f); // Ensure the hue stays within the range [0, 1]

// Convert the hue to an RGB color
int color = Color.HSBtoRGB(hue, saturation, brightness);

// Draw the character with the calculated color
drawContext.drawText(mc.textRenderer, String.valueOf(text.charAt(i)), x + mc.textRenderer.getWidth(text.substring(0, i)), y, color, shadow);
}
}


public static void drawRainbowGradient(float x, float y, float width, float height) {
Matrix4f matrix4f = RenderSystem.getModelViewMatrix();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,4 @@ public static int getAverageColor(NativeImage image) {

return (redAverage << 16) | (greenAverage << 8) | blueAverage;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void start() {
} else {
// Calculate total duration as sum of children's durations
this.duration = children.stream().mapToLong(a -> a.duration).sum();
childStartTimes = new long[children.size()];
this.childStartTimes = new long[children.size()];
long accumulated = 0;
for (int i = 0; i < children.size(); i++) {
childStartTimes[i] = accumulated;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.tanishisherewith.dynamichud.helpers.animationhelper.animations;

import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.random.Random;

public class MathAnimations {
// SHAKE: Random offset animation with smooth decay
public static float shake(float intensity, float frequency, float decay) {
long time = System.currentTimeMillis();
return (float) (Math.sin(time * frequency) *
Math.exp(-decay * time) * intensity);
}

// 2D Shake with different X/Y frequencies
public static Vec2f shake2D(float intensity, float freqX, float freqY) {
return new Vec2f(
(float) Math.sin(System.currentTimeMillis() * freqX) * intensity,
(float) Math.cos(System.currentTimeMillis() * freqY) * intensity
);
}

// FLICKER: Random flashing effect
public static float flicker(float min, float max, float chance) {
Random rand = Random.create();
return rand.nextFloat() < chance ?
min + (max - min) * rand.nextFloat() :
max;
}

// CIRCULAR MOTION: Perfect for rotation/orbital animations
public static Vec2f circularMotion(float radius, float speed, float phase) {
double angle = Math.toRadians((System.currentTimeMillis() * speed) % 360 + phase);
return new Vec2f(
(float) (Math.cos(angle) * radius),
(float) (Math.sin(angle) * radius)
);
}

// SAWTOOTH WAVE: Linear rise with sudden drop
public static float sawtooth(float period, float min, float max) {
float phase = (System.currentTimeMillis() % period) / period;
return min + (max - min) * phase;
}

// TRIANGULAR WAVE: Linear rise and fall
public static float triangleWave(float period, float min, float max) {
float halfPeriod = period / 2;
float phase = (System.currentTimeMillis() % period);
float value = phase < halfPeriod ?
(phase / halfPeriod) :
2 - (phase / halfPeriod);
return min + (max - min) * value;
}

// BOUNCE: Simulates physical bouncing
public static float bounce(float dropHeight, float gravity, float dampening) {
float t = System.currentTimeMillis() / 1000f;
return (float) (dropHeight * Math.abs(Math.sin(t * Math.sqrt(gravity))) *
Math.exp(-dampening * t));
}

// PULSE: Smooth heartbeat-like effect
public static float pulse1(float base, float amplitude, float frequency) {
return (float) (base + amplitude *
(0.5 + 0.5 * Math.sin(System.currentTimeMillis() * frequency)));
}

// SPIRAL: Circular motion with expanding radius
public static Vec2f spiral(float baseRadius, float expansionRate, float speed) {
float t = System.currentTimeMillis() / 1000f;
return new Vec2f(
(float) ((baseRadius + expansionRate * t) * Math.cos(t * speed)),
(float) ((baseRadius + expansionRate * t) * Math.sin(t * speed))
);
}

// Continuous pulsating effect using sine wave
public static float pulse2(float speed, float min, float max) {
return (float) ((Math.sin(System.currentTimeMillis() * speed) + 1) / 2 * (max - min) + min);
}

// Linear interpolation between values over time
public static float lerp(float start, float end, long startTime, float duration) {
float progress = (System.currentTimeMillis() - startTime) / duration;
progress = Math.min(1, Math.max(0, progress)); // Clamp 0-1
return start + (end - start) * progress;
}

// Bouncing animation using quadratic ease-out
public static float bounce(float start, float end, long startTime, float duration) {
float time = System.currentTimeMillis() - startTime;
time /= duration;
return end * (1 - (time - 1) * (time - 1)) + start;
}

// Continuous rotation using modulo
public static float continuousRotation(float speed) {
return (System.currentTimeMillis() % (360_000 / speed)) * (speed / 1000);
}

// Elastic wobble effect
public static float elasticWobble(float speed, float magnitude) {
return (float) (Math.sin(System.currentTimeMillis() * speed) *
Math.exp(-0.001 * System.currentTimeMillis()) * magnitude);
}

// Infinite rotation
public static float infiniteRotation(float speed) {
return (System.currentTimeMillis() % (360_000 / speed)) * (speed / 1000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ public ValueAnimation(AnimationProperty<Float> property, float start, float end,
this.endValue = end;
this.easing = easingType;
}

public ValueAnimation(AnimationProperty<Float> property, float start, float end) {
this(property,start,end,EasingType.LINEAR);
}

@Override
protected void applyAnimation(float progress) {
value = startValue + (endValue - startValue) * Easing.apply(easing,progress);
this.value = startValue + (endValue - startValue) * Easing.apply(easing,progress);
property.set(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmou
*/
@Override
public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) {
assert this.client != null;
if (this.client.world == null) {
renderInGameBackground(drawContext);
}
Expand All @@ -108,9 +107,7 @@ public void render(DrawContext drawContext, int mouseX, int mouseY, float delta)
}
}

public void handleClickOnWidget(Widget widget, double mouseX, double mouseY, int button) {

}
public void handleClickOnWidget(Widget widget, double mouseX, double mouseY, int button) {}

@Override
public void close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ public void toggleDisplay() {
}
}

public void resetAllOptions(){
for(Option<?> option: options){
option.reset();
}
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (!shouldDisplay) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ColorOption extends Option<Color> {
public ColorOption(String name, ContextMenu<?> parentMenu, Supplier<Color> getter, Consumer<Color> setter) {
super(name,getter, setter);
this.parentMenu = parentMenu;
colorGradient = new ColorGradient(x + this.parentMenu.getWidth(), y - 10, get(), this::set, 50, 100);
this.colorGradient = new ColorGradient(x + this.parentMenu.getWidth(), y - 10, get(), this::set, 50, 100);
this.renderer.init(this);
}

Expand Down
Loading

0 comments on commit b0dbca6

Please sign in to comment.