Skip to content

Commit

Permalink
Polishing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishisherewithhh committed Apr 14, 2024
1 parent 9e4091c commit 795a8b1
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public class GlobalConfig {
* Common scale for all widgets. Set by the user using YACL.
*/
@SerialEntry
public float scale = 1.0f;
private float scale = 1.0f;

public static GlobalConfig get() {
return INSTANCE;
}

public Screen createYACLGUI() {
public final Screen createYACLGUI() {
return YetAnotherConfigLib.createBuilder()
.title(Text.literal("DynamicHUD config screen."))
.category(ConfigCategory.createBuilder()
Expand All @@ -49,4 +49,7 @@ public Screen createYACLGUI() {
.build()
.generateScreen(null);
}
public float getScale(){
return scale;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class ContextMenu {
public int x, y;
public int width = 0, finalWidth = 0;
public int height = 0;
public Color backgroundColor = new Color(107, 112, 126, 124);// Semi-transparent light greyish - blue color
public Color backgroundColor = new Color(107, 112, 126, 124);
private Color darkerBorderColor = backgroundColor.darker().darker().darker().darker().darker().darker();
//Todo: Add padding around the rectangle instead of just one side.
public int padding = 5; // The amount of padding around the rectangle
public int heightOffset = 4; // Height offset from the widget
public boolean shouldDisplay = false;
Expand All @@ -39,21 +41,21 @@ public void render(DrawContext drawContext, int x, int y, int height, int mouseX
// Draw the background
DrawHelper.drawRoundedRectangle(drawContext.getMatrices().peek().getPositionMatrix(), this.x - 1, this.y, this.width, this.height, 2,backgroundColor.getRGB());
if(drawBorder){
DrawHelper.drawOutlineRoundedBox(drawContext.getMatrices().peek().getPositionMatrix(), this.x - 1,this.y,width + 1,this.height,2,0.7f,backgroundColor.darker().darker().darker().darker().darker().getRGB());
DrawHelper.drawOutlineRoundedBox(drawContext.getMatrices().peek().getPositionMatrix(), this.x - 1,this.y,width + 1,this.height,2,0.7f,darkerBorderColor.getRGB());
}

int yOffset = this.y + 3;
this.width = 10;
for (Option<?> option : options) {
if (!option.shouldRender()) continue;
if(isMouseOver(mouseX,mouseY, this.x +1,yOffset-1,this.finalWidth - 2,option.height)){
DrawHelper.drawRoundedRectangle(drawContext.getMatrices().peek().getPositionMatrix(), this.x,yOffset - 1.24f,this.finalWidth - 2,option.height + 0.48f,2,backgroundColor.darker().darker().getRGB());
DrawHelper.drawRoundedRectangle(drawContext.getMatrices().peek().getPositionMatrix(), this.x,yOffset - 1.24f,this.finalWidth - 2,option.height + 0.48f,2,backgroundColor.darker().darker().getRGB());
}
option.render(drawContext, x + 2, yOffset,mouseX,mouseY);
this.width = Math.max(this.width, option.width + padding);
this.width = Math.max(this.width, option.width);
yOffset += option.height + 1;
}
this.width = this.width + 3;
this.width = this.width + padding;
this.finalWidth = this.width;
this.height = (yOffset - this.y);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public abstract class Option<T> {
protected Consumer<T> setter;
protected T defaultValue = null;
protected MinecraftClient mc = MinecraftClient.getInstance();
private Widget selectedWidget; // The widget that this context menu is associated with

public Option(Supplier<T> getter, Consumer<T> setter) {
this.getter = getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public ColorOption(String name, ContextMenu parentMenu, Supplier<Color> getter,
super(getter, setter);
this.name = name;
this.parentMenu = parentMenu;
colorPicker = new ColorGradientPicker(x + this.parentMenu.width + 10, y - 10, value, this::set, 50, 100);
System.out.println(get());
colorPicker = new ColorGradientPicker(x + this.parentMenu.finalWidth, y - 10, get(), this::set, 50, 100);
}

@Override
Expand All @@ -30,13 +31,13 @@ public void render(DrawContext drawContext, int x, int y) {

int color = isVisible ? Color.GREEN.getRGB() : Color.RED.getRGB();
this.height = mc.textRenderer.fontHeight;
this.width = mc.textRenderer.getWidth(name) + 12;
this.width = mc.textRenderer.getWidth(name) + 8;
drawContext.drawText(mc.textRenderer, Text.of(name), x, y, color, false);

int shadowOpacity = Math.min(value.getAlpha(),90);
DrawHelper.drawRoundedRectangleWithShadowBadWay(drawContext.getMatrices().peek().getPositionMatrix(),
x + width - 8,
y,
x + width - 4,
y - 1,
8,
8,
2,
Expand All @@ -45,7 +46,7 @@ public void render(DrawContext drawContext, int x, int y) {
1,
1);

colorPicker.render(drawContext, this.x + width / 3 + parentMenu.width + 10, y - 10);
colorPicker.render(drawContext, this.x + parentMenu.finalWidth + 7, y - 10);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tanishisherewith.dynamichud.utils.contextmenu.options;

import com.tanishisherewith.dynamichud.helpers.DrawHelper;
import com.tanishisherewith.dynamichud.utils.contextmenu.ContextMenu;
import com.tanishisherewith.dynamichud.utils.contextmenu.Option;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
Expand All @@ -15,8 +16,9 @@ public class DoubleOption extends Option<Double> {
float step = 0.1f;
private boolean isDragging = false;
private double minValue = 0.0, maxValue = 0.0;
ContextMenu parentMenu;

public DoubleOption(String name, double minValue, double maxValue, float step, Supplier<Double> getter, Consumer<Double> setter) {
public DoubleOption(String name, double minValue, double maxValue, float step, Supplier<Double> getter, Consumer<Double> setter, ContextMenu parentMenu) {
super(getter, setter);
this.name = name;
this.value = get();
Expand All @@ -25,6 +27,7 @@ public DoubleOption(String name, double minValue, double maxValue, float step, S
this.width = 30;
this.height = 16;
this.step = step;
this.parentMenu = parentMenu;
Validate.isTrue(this.step > 0.0f, "Step cannot be less than or equal to 0 (zero)");
}

Expand All @@ -33,8 +36,9 @@ public void render(DrawContext drawContext, int x, int y) {
super.render(drawContext, x, y);
value = get();

this.width = 30;
this.width = 35;
this.height = 16;

// Draw the label
TextRenderer textRenderer = mc.textRenderer;
DrawHelper.scaleAndPosition(drawContext.getMatrices(), x, y, 0.7f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void render(DrawContext drawContext, int x, int y) {
super.render(drawContext, x, y);

value = get();
this.height = mc.textRenderer.fontHeight;
this.height = mc.textRenderer.fontHeight + 1;
this.width = mc.textRenderer.getWidth(name + ": " + value.toString()) + 1;

drawContext.drawText(mc.textRenderer, Text.of(name + ": "), x, y, Color.WHITE.getRGB(), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ public RunnableOption(String name, Supplier<Boolean> getter, Consumer<Boolean> s
this.name = "Run: " + name; // prepend the "run" symbol to the name
this.task = task;
}
Color DARK_RED = new Color(116, 0, 0);
Color DARK_GREEN = new Color(24, 132, 0, 226);


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

value = get();
this.height = mc.textRenderer.fontHeight;
this.width = mc.textRenderer.getWidth("Run: " + name) + 1;
int color = value ? Color.BLUE.getRGB() : Color.GRAY.getRGB();
this.width = mc.textRenderer.getWidth("Run: " + name);
int color = value ? DARK_GREEN.getRGB() : DARK_RED.getRGB();
drawContext.drawText(mc.textRenderer, Text.of(name), x, y, color, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public SubMenuOption(String name, @NotNull ContextMenu parentMenu, Supplier<Bool
Objects.requireNonNull(parentMenu, "Parent Menu cannot be null");
this.name = name;
this.parentMenu = parentMenu;
this.subMenu = new ContextMenu(parentMenu.x + parentMenu.width, this.y);
this.subMenu = new ContextMenu(parentMenu.x + parentMenu.finalWidth, this.y);
this.subMenu.heightOffset = 0;
this.subMenu.shouldDisplay = get();
}
Expand All @@ -40,9 +40,11 @@ public void render(DrawContext drawContext, int x, int y,int mouseX,int mouseY)
this.y = y;

int color = value ? Color.GREEN.getRGB() : Color.RED.getRGB();
drawContext.drawText(mc.textRenderer, Text.of(name), x, y, color, false);
drawContext.drawText(mc.textRenderer, Text.of(name), x, y + 1, color, false);
this.height = mc.textRenderer.fontHeight + 2;
this.width = mc.textRenderer.getWidth(name) + 1;

subMenu.render(drawContext, this.x + parentMenu.width, this.y, 0,mouseX, mouseY);
subMenu.render(drawContext, this.x + parentMenu.finalWidth, this.y, 0,mouseX, mouseY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void onRelease(double mouseX, double mouseY, int button) {
}

public void onDrag(double mouseX, double mouseY, int button) {
if (isDragging) {
if (isDragging && mouseY >= y && mouseY <= y + height) {
alphaHandleY = (int) mouseY - y;
alpha = 1.0f - (alphaHandleY / (float) height);
if (alpha < 0.0f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ public class ColorGradientPicker {
private final int boxSize;
private int x, y;
private boolean display = false;
private final Color initialColor;

public ColorGradientPicker(int x, int y, Color initialColor, Consumer<Color> onColorSelected, int boxSize, int colors) {
this.x = x;
this.y = y;
this.initialColor = initialColor;
this.onColorSelected = onColorSelected;
float[] hsv = Color.RGBtoHSB(initialColor.getRed(), initialColor.getGreen(), initialColor.getBlue(), null);
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.alphaSlider = new AlphaSlider(x, y, 10, boxSize, initialColor);

float[] hsv = new float[3];
Color.RGBtoHSB(initialColor.getRed(), initialColor.getGreen(), initialColor.getBlue(), hsv);

this.boxSize = boxSize;
this.gradientSlider.setHue(hsv[0]);
this.gradientBox.setHue(hsv[0]);
this.gradientBox.setSaturation(hsv[1]);
this.gradientBox.setValue(hsv[2]);

this.alphaSlider = new AlphaSlider(x, y, 10, boxSize, initialColor);
this.colorPickerButton = new ColorPickerButton(x + boxSize + 8, y + 20, 30, 18);
}

Expand All @@ -57,10 +61,10 @@ public void render(DrawContext drawContext, int x1, int y1) {
if (!display) {
return;
}
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 + 54 + boxSize, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 8);
alphaSlider.render(drawContext, x + 40 + boxSize, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 10);
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);
alphaSlider.render(drawContext, x + 10 + boxSize, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 10);

if (colorPickerButton.isPicking()) {
// Draw the preview box near cursor
Expand Down Expand Up @@ -92,6 +96,15 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (!display) {
return false;
}

float[] hsv1 = new float[3];
Color.RGBtoHSB(initialColor.getRed(), initialColor.getGreen(), initialColor.getBlue(), hsv1);

this.gradientSlider.setHue(hsv1[0]);
this.gradientBox.setHue(hsv1[0]);
this.gradientBox.setSaturation(hsv1[1]);
this.gradientBox.setValue(hsv1[2]);

if (colorPickerButton.onClick(mouseX, mouseY, button)) {
return true;
} else if (gradientSlider.isMouseOver(mouseX, mouseY)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void render(DrawContext drawContext, int x, int y) {
DrawHelper.drawOutlinedBox(drawContext, x - 2, y - 2, x + size + 2, y + size + 2, -1);

// Draw the gradient
com.tanishisherewith.dynamichud.helpers.DrawHelper.drawRoundedGradientRectangle(drawContext.getMatrices().peek().getPositionMatrix(), Color.BLACK, Color.BLACK, Color.getHSBColor(hue, 1.0f, 1.0f), Color.WHITE, x, y, size, size, 2);
DrawHelper.drawRoundedGradientRectangle(drawContext.getMatrices().peek().getPositionMatrix(), Color.BLACK, Color.BLACK, Color.getHSBColor(hue, 1.0f, 1.0f), Color.WHITE, x, y, size, size, 2);

// Draw the handle
float handleSize = 3;
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/tanishisherewith/dynamichud/widget/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public abstract class Widget {
/**
* Scale of the current widget.
*
* @see GlobalConfig#scale
* @see GlobalConfig#getScale()
*/
protected float scale = 1.0f;
//Dimensions of the widget
Expand All @@ -68,7 +68,7 @@ public Widget(WidgetData<?> DATA, String modId) {
}

/**
* This method is called at the end of the {@link Widget#Widget(WidgetData)} constructor.
* This method is called at the end of the {@link Widget#Widget(WidgetData, String)} constructor.
*/
public void init() {

Expand Down Expand Up @@ -127,11 +127,11 @@ public boolean isOverlapping(Widget other) {
/**
* Renders the widget on the screen.
*/
public void render(DrawContext drawContext, int mouseX, int mouseY) {
public final void render(DrawContext drawContext, int mouseX, int mouseY) {
if (!shouldDisplay()) return;

if (shouldScale) {
DrawHelper.scaleAndPosition(drawContext.getMatrices(), getX(), getY(), GlobalConfig.get().scale);
DrawHelper.scaleAndPosition(drawContext.getMatrices(), getX(), getY(), GlobalConfig.get().getScale());
}
renderWidget(drawContext, mouseX, mouseY);

Expand All @@ -144,11 +144,11 @@ public void render(DrawContext drawContext, int mouseX, int mouseY) {
/**
* Renders the widget on the editor screen.
*/
public void renderInEditor(DrawContext drawContext, int mouseX, int mouseY) {
public final void renderInEditor(DrawContext drawContext, int mouseX, int mouseY) {
displayBg(drawContext);

if (shouldScale) {
DrawHelper.scaleAndPosition(drawContext.getMatrices(), getX(), getY(), GlobalConfig.get().scale);
DrawHelper.scaleAndPosition(drawContext.getMatrices(), getX(), getY(), GlobalConfig.get().getScale());
}
renderWidgetInEditor(drawContext, mouseX, mouseY);

Expand Down Expand Up @@ -183,6 +183,7 @@ private void renderWidgetInEditor(DrawContext context, int mouseX, int mouseY) {

renderWidget(context, mouseX, mouseY);
}

/* Input related methods. Override with super call to add your own input-based code like contextMenu */

public boolean mouseClicked(double mouseX, double mouseY, int button) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void shouldRenderInGameHud(boolean renderInGameHud) {
}

public void renderWidgets(DrawContext context, int mouseX, int mouseY) {
if (WidgetManager.getWidgets().isEmpty()) return;
if (WidgetManager.getWidgets().isEmpty() || DynamicHUD.MC.getDebugHud().shouldShowDebugHud()) return;

Screen currentScreen = DynamicHUD.MC.currentScreen;

Expand Down
Loading

0 comments on commit 795a8b1

Please sign in to comment.