Skip to content

Commit

Permalink
ColorOption and some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishisherewithhh committed Apr 8, 2024
1 parent 11441ea commit 4eb6360
Show file tree
Hide file tree
Showing 12 changed files with 1,466 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,32 @@ public static float[] getRainbowColor()
rainbow[2] = 0.5F + 0.5F * MathHelper.sin((x + 8F / 3F) * pi);
return rainbow;
}

/**
* Rainbow color with custom speed.
*
* @param speed
* @return Current rainbow color.
*/
public static Color getRainbowColor(int speed) {
float hue = (System.currentTimeMillis() % (speed * 100)) / (speed * 100.0f);
return Color.getHSBColor(hue, 1.0f, 1.0f);
}


/**
* Changes alpha on color.
*
* @param color Target color.
* @param alpha Target alpha.
* @return Color with changed alpha.
*/
public static Color changeAlpha(Color color, int alpha) {
if (color != null)
return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
else
return new Color(0);
}
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 @@ -106,7 +106,7 @@ protected void TextWidgetMenu(TextWidget textWidget, int x, int y) {
textWidget.toggleTextColorOption();

if (textWidget.isTextcolorOptionEnabled())
colorPicker = new ColorGradientPicker(mc, widgetX + 110, widgetY + textWidget.getHeight() + 5, textWidget.getTextcolor(), textWidget::setTextColor, 50, 100, selectedWidget);
colorPicker = new ColorGradientPicker(mc, widgetX +110, widgetY + textWidget.getHeight() + 5, textWidget.getTextcolor(), textWidget::setTextColor, 50, 100, selectedWidget);
else
colorPicker = null;
});
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,8 @@ public void resize(MinecraftClient client, int width, int height) {
public boolean shouldPause() {
return ShouldPause;
}
public void setSnapSize(int size){
this.snapSize = size;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options;

import com.tanishisherewith.dynamichud.newTrial.helpers.DrawHelper;
import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option;
import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.coloroption.ColorGradientPicker;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;

import java.awt.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class ColorOption extends Option<Color> {
public String name = "Empty";
public boolean isVisible = false;
private ColorGradientPicker colorPicker = null;
public ColorOption(String name, Supplier<Color> getter, Consumer<Color> setter) {
super(getter, setter);
this.name = name;
colorPicker = new ColorGradientPicker(x + this.width + 50,y,value,color-> set(new Color(color)),50,100 );
}

@Override
public void render(DrawContext drawContext, int x, int y) {
super.render(drawContext, x, y);
value = get();

int color = isVisible ? Color.GREEN.getRGB() : Color.RED.getRGB();
this.height = mc.textRenderer.fontHeight;
this.width = mc.textRenderer.getWidth(name) + 12;
drawContext.drawText(mc.textRenderer, Text.of(name),x,y, color,false);
DrawHelper.drawRoundedRectangleWithShadowBadWay(drawContext.getMatrices().peek().getPositionMatrix(),
x + width - 8,
y,
8,
8,
2,
value.getRGB(),
90,
1,
1 );

colorPicker.render(drawContext,x + this.width + 50,y);
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if(isMouseOver(mouseX, mouseY)) {
isVisible = !isVisible;
if(isVisible)
{
colorPicker.setPos(x + this.width + 50,y);
colorPicker.display();
}else{
colorPicker.close();
}
}
colorPicker.mouseClicked(mouseX,mouseY,button);
return super.mouseClicked(mouseX, mouseY, button);
}

@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
colorPicker.mouseReleased(mouseX, mouseY, button);
return super.mouseReleased(mouseX, mouseY, button);
}

@Override
public boolean mouseDragged(double mouseX, double mouseY, int button) {
colorPicker.mouseDragged(mouseX, mouseY, button);
return super.mouseDragged(mouseX, mouseY, button);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options;

import com.tanishisherewith.dynamichud.helpers.DrawHelper;
import com.tanishisherewith.dynamichud.newTrial.helpers.DrawHelper;
import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import org.apache.commons.lang3.Validate;

import javax.xml.validation.Validator;
import java.awt.*;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand All @@ -30,7 +29,6 @@ public DoubleOption(String name,double minValue,double maxValue,float step,Suppl
@Override
public void render(DrawContext drawContext, int x, int y) {
super.render(drawContext, x, y);

value = get();

this.width = 30;
Expand All @@ -54,12 +52,23 @@ public void render(DrawContext drawContext, int x, int y) {

// Draw the handle

DrawHelper.fillRoundedRect(drawContext, (int) handleX, (int) handleY, (int) (handleX + handleWidth), (int) (handleY + handleHeight), 0xFFFFFFFF);
DrawHelper.drawRoundedRectangleWithShadowBadWay(drawContext.getMatrices().peek().getPositionMatrix(),
(float)handleX,
(float)handleY,
handleWidth,
handleHeight,
1,
0xFFFFFFFF,
90,
0.6f,
0.6f);
}

private void drawSlider(DrawContext drawContext, int sliderX, int sliderY, int sliderWidth, double handleX) {
DrawHelper.fill(drawContext, sliderX, sliderY, sliderX + sliderWidth, sliderY + 2, 0xFFFFFFFF);
DrawHelper.fill(drawContext, sliderX, sliderY, (int) Math.round(handleX), sliderY + 2, Color.ORANGE.getRGB());
DrawHelper.drawRectangle(drawContext.getMatrices().peek().getPositionMatrix(), sliderX, sliderY, sliderWidth, 2, 0xFFFFFFFF);
if(handleX-sliderX > 0) {
DrawHelper.drawRectangle(drawContext.getMatrices().peek().getPositionMatrix(), (float) sliderX, (float) sliderY, (float) ((value - minValue) / (maxValue - minValue) * (width - 3)), 2, Color.ORANGE.getRGB());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.coloroption;

import com.tanishisherewith.dynamichud.widget.Widget;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.GlAllocationUtils;
import org.lwjgl.opengl.GL11;

import java.awt.*;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.function.Consumer;

public class ColorGradientPicker {
MinecraftClient client = MinecraftClient.getInstance();
private final Consumer<Integer> onColorSelected; // The callback to call when a color is selected
private final GradientSlider gradientSlider;
private final GradientBox gradientBox;
private final ColorPickerButton colorPickerButton;
private int x,y;
private final int boxSize;
private boolean display = false;

public ColorGradientPicker(int x, int y, Color initialColor, Consumer<Integer> onColorSelected, int boxSize, int colors) {
this.x = x;
this.y = y;
this.onColorSelected = onColorSelected;
float[] hsv = new float[3];
Color.RGBtoHSB(initialColor.getRed(), initialColor.getGreen(), initialColor.getBlue(), hsv);
this.boxSize = boxSize;
this.gradientSlider = new GradientSlider(x, y, colors, 10);
this.gradientSlider.setHue(hsv[0]);

this.gradientBox = new GradientBox(x, y + 20, boxSize);
this.gradientBox.setHue(hsv[0]);
this.gradientBox.setSaturation(hsv[1]);
this.gradientBox.setValue(hsv[2]);
this.colorPickerButton = new ColorPickerButton(x + boxSize + 8, y + 20, 35, 20);
}
public void setPos(int x, int y){
this.x = x;
this.y = y;
}
public void display() {
display = true;
}
public void close() {
display = false;
}
public void tick() {
gradientSlider.tick();
gradientBox.tick();
}
public void defaultValues(){
gradientSlider.defaultValues();
gradientBox.defaultValues();
}

public void render(DrawContext drawContext, int x1, int y1) {
setPos(x1,y1);
if(!display){
defaultValues();
return;
}
tick();
gradientSlider.render(drawContext,x + 30, y +client.textRenderer.fontHeight + 4);
gradientBox.render(drawContext,x + 30, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 10);
colorPickerButton.render(drawContext,x+ 40 + boxSize,y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 7);

if (colorPickerButton.isPicking()) {
// Draw the cursor
double mouseX = client.mouse.getX() * client.getWindow().getScaledWidth() / (double) client.getWindow().getWidth();
double mouseY = client.mouse.getY() * client.getWindow().getScaledHeight() / (double) client.getWindow().getHeight();

Framebuffer framebuffer = client.getFramebuffer();
int x = (int) (mouseX * framebuffer.textureWidth / client.getWindow().getScaledWidth());
int y = (int) ((client.getWindow().getScaledHeight() - mouseY) * framebuffer.textureHeight / client.getWindow().getScaledHeight());

ByteBuffer buffer = GlAllocationUtils.allocateByteBuffer(4);
GL11.glReadPixels(x, y, 1, 1, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
int red = buffer.get(0) & 0xFF;
int green = buffer.get(1) & 0xFF;
int blue = buffer.get(2) & 0xFF;

drawContext.fill((int) mouseX + 10, (int) mouseY, (int) mouseX + 26, (int) mouseY + 16, -1);
drawContext.fill((int) mouseX + 11, (int) mouseY + 1, (int) mouseX + 25, (int) mouseY + 15, (red << 16) | (green << 8) | blue | 0xFF000000);
}
}

public boolean mouseClicked(double mouseX, double mouseY, int button) {
if(!display){
return false;
}
if (colorPickerButton.onClick(mouseX, mouseY, button)) {
return true;
} 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()) {
Framebuffer framebuffer = client.getFramebuffer();
int x = (int) (mouseX * framebuffer.textureWidth / client.getWindow().getScaledWidth());
int y = (int) ((client.getWindow().getScaledHeight() - mouseY) * framebuffer.textureHeight / client.getWindow().getScaledHeight());

ByteBuffer buffer = GlAllocationUtils.allocateByteBuffer(4);
GL11.glReadPixels(x, y, 1, 1, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
int red = buffer.get(0) & 0xFF;
int green = buffer.get(1) & 0xFF;
int blue = buffer.get(2) & 0xFF;

float[] hsv = Color.RGBtoHSB(red, green, blue, null);
gradientSlider.setHue(hsv[0]);
gradientBox.setHue(hsv[0]);
gradientBox.setSaturation(hsv[1]);
gradientBox.setValue(hsv[2]);

colorPickerButton.setPicking(false);
}
onColorSelected.accept(gradientBox.getColor());
return true;
}

public void mouseReleased(double mouseX, double mouseY, int button) {
gradientSlider.onRelease(mouseX, mouseY, button);
gradientBox.onRelease(mouseX, mouseY, button);
}

public void mouseDragged(double mouseX, double mouseY, int button) {
if(!display){
return;
}
gradientSlider.onDrag(mouseX, mouseY, button);
gradientBox.setHue(gradientSlider.getHue());
gradientBox.onDrag(mouseX, mouseY, button);
onColorSelected.accept(gradientBox.getColor());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.coloroption;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;

public class ColorPickerButton {
private int x;
private int y;
private final int width;
private final int height;
private boolean isPicking = false;

public ColorPickerButton(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

public void render(DrawContext drawContext, int x, int y) {
this.x = x;
this.y = y;
drawContext.getMatrices().push();
drawContext.getMatrices().translate(0,0,404);
// Draw the button
drawContext.fill(x + 2, y + 2, x + width - 2, y + height - 2, 0xFFAAAAAA);
drawContext.drawCenteredTextWithShadow(MinecraftClient.getInstance().textRenderer, "Pick", x + width / 2, y + (height - 8) / 2, 0xFFFFFFFF);
drawContext.getMatrices().pop();
}

public int getHeight() {
return height;
}

public boolean onClick(double mouseX, double mouseY, int button) {
if (button == 0) {
if (mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height) {
isPicking = true;
return true;
}
}
return false;
}

public boolean isPicking() {
return isPicking;
}

public void setPicking(boolean picking) {
isPicking = picking;
}
}
Loading

0 comments on commit 4eb6360

Please sign in to comment.