Skip to content

Commit

Permalink
Removed color picker button.
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishisherewithhh committed Feb 11, 2025
1 parent b0dbca6 commit 92073b4
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

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

import java.awt.*;
import java.math.BigInteger;
Expand Down Expand Up @@ -174,39 +176,47 @@ 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");
public static int[] getMousePixelColor(double mouseX, double mouseY) {
MinecraftClient client = MinecraftClient.getInstance();
Framebuffer framebuffer = client.getFramebuffer();
Window window = client.getWindow();

// Get the window and framebuffer dimensions
int windowWidth = window.getWidth();
int windowHeight = window.getHeight();
int framebufferWidth = framebuffer.textureWidth;
int framebufferHeight = framebuffer.textureHeight;

// Calculate scaling factors
double scaleX = (double) framebufferWidth / windowWidth;
double scaleY = (double) framebufferHeight / windowHeight;

// Convert mouse coordinates to framebuffer coordinates
int x = (int) (mouseX * scaleX);
int y = (int) ((windowHeight - mouseY) * scaleY);

// Ensure the coordinates are within the framebuffer bounds
if (x < 0 || x >= framebufferWidth || y < 0 || y >= framebufferHeight) {
System.err.println("Mouse coordinates are out of bounds");
return null;
}
return null;

// Allocate a buffer to store the pixel data
ByteBuffer buffer = ByteBuffer.allocateDirect(4); // 4 bytes for RGBA

// Bind the framebuffer for reading
GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, framebuffer.fbo);

// Read the pixel at the mouse position
GL11.glReadPixels(x, y, 1, 1, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

// Extract the color components from the buffer
int red = buffer.get(0) & 0xFF;
int green = buffer.get(1) & 0xFF;
int blue = buffer.get(2) & 0xFF;
int alpha = buffer.get(3) & 0xFF;

return new int[]{red, green, blue, alpha};
}

public static int fromRGBA(int r, int g, int b, int a) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tanishisherewith.dynamichud.helpers.animationhelper.animations;

import com.tanishisherewith.dynamichud.helpers.animationhelper.Easing;
import com.tanishisherewith.dynamichud.helpers.animationhelper.EasingType;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.random.Random;

Expand Down Expand Up @@ -81,9 +83,12 @@ public static float pulse2(float speed, float min, float max) {

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

// Bouncing animation using quadratic ease-out
Expand All @@ -103,9 +108,4 @@ 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 @@ -29,17 +29,17 @@ protected void applyAnimation(float progress) {
property.set(value);
}

public ValueAnimation setEasing(EasingType easing) {
public ValueAnimation easing(EasingType easing) {
this.easing = easing;
return this;
}

public ValueAnimation setStartValue(float startValue) {
public ValueAnimation startValue(float startValue) {
this.startValue = startValue;
return this;
}

public ValueAnimation setEndValue(float endValue) {
public ValueAnimation endValue(float endValue) {
this.endValue = endValue;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void render(DrawContext drawContext, int x1, int y1, int mouseX, int mous
}
gradientSlider.render(drawContext, x, y + client.textRenderer.fontHeight + 4);
gradientBox.render(drawContext, x, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 10);
colorPickerButton.render(drawContext, x + 24 + boxSize, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 8);
// colorPickerButton.render(drawContext, x + 24 + boxSize, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 8);
alphaSlider.render(drawContext, x + 10 + boxSize, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 10);

if (colorPickerButton.isPicking() && GlobalConfig.get().showColorPickerPreview()) {
Expand All @@ -87,14 +87,14 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (!display) {
return false;
}
if (colorPickerButton.onClick(mouseX, mouseY, button)) {
/*if (colorPickerButton.onClick(mouseX, mouseY, button)) {
return true;
} else if (gradientSlider.isMouseOver(mouseX, mouseY)) {
} else*/ if (gradientSlider.isMouseOver(mouseX, mouseY)) {
gradientSlider.onClick(mouseX, mouseY, button);
gradientBox.setHue(gradientSlider.getHue());
} else if (gradientBox.isMouseOver(mouseX, mouseY)) {
gradientBox.onClick(mouseX, mouseY, button);
} else if (colorPickerButton.isPicking()) {
} /* else if (colorPickerButton.isPicking()) {
int[] colors = ColorHelper.getMousePixelColor(mouseX,mouseY);
if(colors != null) {
float[] hsv = Color.RGBtoHSB(colors[0], colors[1], colors[2], null);
Expand All @@ -108,6 +108,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
DynamicHUD.logger.error("Invalid RGB pixel color at mouse pointer");
}
}
*/
alphaSlider.setColor(new Color(gradientBox.getColor()));
alphaSlider.onClick(mouseX, mouseY, button);
onColorSelected.accept(alphaSlider.getColor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mojang.blaze3d.systems.RenderSystem;
import com.tanishisherewith.dynamichud.helpers.ColorHelper;
import com.tanishisherewith.dynamichud.helpers.DrawHelper;
import com.tanishisherewith.dynamichud.helpers.animationhelper.EasingType;
import com.tanishisherewith.dynamichud.helpers.animationhelper.animations.MathAnimations;
import com.tanishisherewith.dynamichud.utils.contextmenu.ContextMenu;
import com.tanishisherewith.dynamichud.utils.contextmenu.layout.LayoutContext;
import com.tanishisherewith.dynamichud.utils.contextmenu.options.*;
Expand Down Expand Up @@ -345,33 +347,24 @@ public Color getThemeColor() {
}

public class ModernBooleanRenderer implements SkinRenderer<BooleanOption> {
private float animationProgress = 0f;
private boolean animating = false;
private long animationStartTime;

@Override
public void render(DrawContext drawContext, BooleanOption option, int x, int y, int mouseX, int mouseY) {
int backgroundWidth = (int) (width * 0.8f - 14);

option.setHeight(14);
option.setPosition(x,y);
option.setPosition(x, y);
option.setWidth(backgroundWidth);

MatrixStack matrices = drawContext.getMatrices();

// Animate the toggle
if (animating) {
animationProgress += 0.1f; // Adjust speed as needed
if (animationProgress >= 1f) {
animationProgress = 0f;
animating = false;
}
}

// Calculate the current progress of the animation
int toggleBgX = x + backgroundWidth - 30;
// Background
boolean active = option.get();
Color backgroundColor = active ? getThemeColor() : DARKER_GRAY;
Color hoveredColor = isMouseOver(mouseX,mouseY,toggleBgX,y + 2,14,7) ? backgroundColor.darker() : backgroundColor;
Color hoveredColor = isMouseOver(mouseX, mouseY, toggleBgX, y + 2, 14, 7) ? backgroundColor.darker() : backgroundColor;

DrawHelper.drawRoundedRectangleWithShadowBadWay(
matrices.peek().getPositionMatrix(),
Expand All @@ -382,10 +375,10 @@ public void render(DrawContext drawContext, BooleanOption option, int x, int y,
);

// Draw toggle circle
float toggleX = active ? toggleBgX + 10 : toggleBgX + 4;
if (animating) {
toggleX = MathHelper.lerp(animationProgress, active ? toggleBgX + 4 : toggleBgX + 10, toggleX);
}
float startX = active ? toggleBgX + 4 : toggleBgX + 10;
float endX = active ? toggleBgX + 10 : toggleBgX + 4;
EasingType easingType = active ? EasingType.EASE_IN_CUBIC : EasingType.EASE_OUT_QUAD;
float toggleX = MathAnimations.lerp(startX, endX, animationStartTime, 200f, easingType);

DrawHelper.drawFilledCircle(matrices.peek().getPositionMatrix(), toggleX, y + 2 + 3.3f, 2.8f, Color.WHITE.getRGB());

Expand All @@ -402,21 +395,22 @@ public void render(DrawContext drawContext, BooleanOption option, int x, int y,

@Override
public boolean mouseClicked(BooleanOption option, double mouseX, double mouseY, int button) {
mouseX = mc.mouse.getX()/SCALE_FACTOR;
mouseY = mc.mouse.getY()/SCALE_FACTOR;
mouseX = mc.mouse.getX() / SCALE_FACTOR;
mouseY = mc.mouse.getY() / SCALE_FACTOR;

int backgroundWidth = (int) (width * 0.8f - 14);
int toggleBgX = option.getX() + backgroundWidth - 30;

if(isMouseOver(mouseX,mouseY,toggleBgX,option.getY(),14,option.getHeight())){
if (isMouseOver(mouseX, mouseY, toggleBgX, option.getY(), 14, option.getHeight())) {
option.set(!option.get());
animating = true;
animationStartTime = System.currentTimeMillis();
return true;
}
return false;
}
}


public class ModernColorOptionRenderer implements SkinRenderer<ColorOption> {
private static final float ANIMATION_SPEED = 0.1f;
private float scale = 0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public TextWidget(DynamicValueRegistry dynamicValueRegistry, String dynamicRegis
}

public void createMenu() {
ContextMenuProperties properties = ContextMenuProperties.builder().skin(new MinecraftSkin(MinecraftSkin.PanelColor.MIDNIGHT_PURPLE)).build();
ContextMenuProperties properties = ContextMenuProperties.builder().skin(new ModernSkin()).build();

menu = new ContextMenu<>(getX(), getY(), properties);
menu.addOption(new BooleanOption("Shadow",
Expand Down

0 comments on commit 92073b4

Please sign in to comment.