From 64bfc2920a4bca9eda7081accf1ccdaf4767e3bf Mon Sep 17 00:00:00 2001 From: tanishisherewithhh <120117618+tanishisherewithhh@users.noreply.github.com> Date: Sun, 7 Apr 2024 12:04:15 +0530 Subject: [PATCH] Fixes --- .../dynamichud/helpers/DrawHelper.java | 6 +-- .../dynamichud/newTrial/DynamicHUD.java | 1 + .../contextmenu/options/DoubleOption.java | 13 +++-- .../utils/contextmenu/options/EnumOption.java | 5 +- .../utils/contextmenu/options/ListOption.java | 6 ++- .../contextmenu/options/RunnableOption.java | 51 +++++++++++++++++++ .../dynamichud/newTrial/widget/Widget.java | 2 +- .../newTrial/widgets/TextWidget.java | 12 ++--- 8 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/RunnableOption.java diff --git a/src/main/java/com/tanishisherewith/dynamichud/helpers/DrawHelper.java b/src/main/java/com/tanishisherewith/dynamichud/helpers/DrawHelper.java index e925221..f997e28 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/helpers/DrawHelper.java +++ b/src/main/java/com/tanishisherewith/dynamichud/helpers/DrawHelper.java @@ -28,11 +28,7 @@ public DrawHelper(MinecraftClient client, VertexConsumerProvider.Immediate verte * @param color The color to fill the rectangle with */ public static void drawBox(DrawContext drawContext, int x, int y, int width, int height, int color) { - int x1 = x - width / 2 - 2; - int y1 = y - height / 2 - 2; - int x2 = x + width / 2 + 2; - int y2 = y + height / 2 + 2; - drawContext.fill(x1, y1, x2, y2, color); + drawContext.fill(x, y, x + width, y + height, color); } /** diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java index f19e65f..92c3277 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java @@ -114,6 +114,7 @@ public void onInitializeClient() { ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, s) -> GlobalConfig.HANDLER.save()); ServerPlayConnectionEvents.DISCONNECT.register((handler, packetSender) -> GlobalConfig.HANDLER.save()); Runtime.getRuntime().addShutdownHook(new Thread(() -> GlobalConfig.HANDLER.save())); + GlobalConfig.HANDLER.save(); HudRenderCallback.EVENT.register(new HudRender()); } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/DoubleOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/DoubleOption.java index b084b56..be23c90 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/DoubleOption.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/DoubleOption.java @@ -7,6 +7,7 @@ import org.apache.commons.lang3.Validate; import javax.xml.validation.Validator; +import java.awt.*; import java.util.function.Consumer; import java.util.function.Supplier; @@ -43,20 +44,22 @@ public void render(DrawContext drawContext, int x, int y) { drawContext.drawTextWithShadow(textRenderer, labelText, x, y + 1, 0xFFFFFFFF); DrawHelper.stopScaling(drawContext.getMatrices()); - // Draw the slider - drawSlider(drawContext, x, y + textRenderer.fontHeight + 1, width); - - // Draw the handle float handleWidth = 3; float handleHeight = 8; double handleX = x + (value - minValue) / (maxValue - minValue) * (width - handleWidth); double handleY = y + textRenderer.fontHeight + 1 + ((2 - handleHeight) / 2); + // Draw the slider + drawSlider(drawContext, x, y + textRenderer.fontHeight + 1, width, handleX); + + // Draw the handle + DrawHelper.fillRoundedRect(drawContext, (int) handleX, (int) handleY, (int) (handleX + handleWidth), (int) (handleY + handleHeight), 0xFFFFFFFF); } - private void drawSlider(DrawContext drawContext, int sliderX, int sliderY, int sliderWidth) { + 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()); } @Override diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/EnumOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/EnumOption.java index 7c49a8f..dde3f95 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/EnumOption.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/EnumOption.java @@ -1,5 +1,6 @@ package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options; +import com.tanishisherewith.dynamichud.helpers.DrawHelper; import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option; import net.minecraft.client.gui.DrawContext; import net.minecraft.text.Text; @@ -34,8 +35,8 @@ public void render(DrawContext drawContext, int x, int y) { this.height = mc.textRenderer.fontHeight + 1; this.width = mc.textRenderer.getWidth(name + ": " + value.name()) + 1; - int color = Color.WHITE.getRGB(); - drawContext.drawText(mc.textRenderer, Text.of(name + ": " + value.name()), x, y, color, false); + drawContext.drawText(mc.textRenderer, Text.of(name + ": "), x, y, Color.WHITE.getRGB(), false); + drawContext.drawText(mc.textRenderer, Text.of(value.name()), x + mc.textRenderer.getWidth(name + ": ") + 1, y, Color.CYAN.getRGB(), false); } @Override diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/ListOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/ListOption.java index 80f605a..3e69407 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/ListOption.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/ListOption.java @@ -1,5 +1,6 @@ package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options; +import com.tanishisherewith.dynamichud.helpers.DrawHelper; import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option; import net.minecraft.client.gui.DrawContext; import net.minecraft.text.Text; @@ -36,8 +37,9 @@ public void render(DrawContext drawContext, int x, int y) { this.height = mc.textRenderer.fontHeight; this.width = mc.textRenderer.getWidth(name + ": " + value.toString()) + 1; - int color = Color.WHITE.getRGB(); - drawContext.drawText(mc.textRenderer, Text.of(name + ": " + value.toString()), x, y, color, false); + drawContext.drawText(mc.textRenderer, Text.of(name + ": "), x, y, Color.WHITE.getRGB(), false); + drawContext.drawText(mc.textRenderer, Text.of(value.toString()), x + mc.textRenderer.getWidth(name + ": ") + 1, y, Color.CYAN.getRGB(), false); + } @Override diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/RunnableOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/RunnableOption.java new file mode 100644 index 0000000..4663609 --- /dev/null +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/RunnableOption.java @@ -0,0 +1,51 @@ +package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options; + +import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option; +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 RunnableOption extends Option { + public String name = "Empty"; + private Runnable task; + + /** + * Runnable option which runs a task when clicked on it. + * @param name The name to be displayed + * @param getter Get a default value to run the task by default + * @param setter Return a boolean based on if the task is running or not. + * @param task The task to run + */ + public RunnableOption(String name, Supplier getter, Consumer setter,Runnable task) { + super(getter, setter); + this.name = "Run: " + name; // prepend the "run" symbol to the name + this.task = task; + } + + @Override + public void render(DrawContext drawContext, int x, int y) { + super.render(drawContext, x, y); + + value = get(); + int color = value ? Color.BLUE.getRGB() : Color.GRAY.getRGB(); + drawContext.drawText(mc.textRenderer,Text.of(name),x,y, color,false); + this.height = mc.textRenderer.fontHeight; + this.width = mc.textRenderer.getWidth(name) + 1; + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + super.mouseClicked(mouseX, mouseY, button); + if(isMouseOver(mouseX, mouseY)) { + value = !value; + set(value); + if(value){ + task.run(); + } + } + return true; + } +} diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/Widget.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/Widget.java index 7cc5c2f..4b8b1ae 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/Widget.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/Widget.java @@ -258,7 +258,7 @@ public void onClose(){ protected void displayBg(DrawContext context) { int backgroundColor = this.shouldDisplay() ? ColorHelper.getColor(0, 0, 0, 128) : ColorHelper.getColor(255, 0, 0, 128); WidgetBox box = this.getWidgetBox(); - DrawHelper.fill(context, (int) (box.x1 - 2), (int) (box.y1 - 2), (int) (box.x2 + 2), (int) (box.y2 + 2), backgroundColor); + DrawHelper.fill(context, (int) box.x1, (int) box.y1, (int) box.x2, (int) box.y2, backgroundColor); } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java index ef6f1b2..c88e184 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java @@ -29,7 +29,7 @@ public class TextWidget extends Widget { private ContextMenu menu; public TextWidget() { - this(null,false,false,"unknown"); + this(null,null,false,false,"unknown"); } /** @@ -75,7 +75,9 @@ public TextWidget(DynamicValueRegistry dynamicValueRegistry,String dynamicRegist super(DATA,modID); this.dynamicRegistryKey = dynamicRegistryKey; this.dynamicValueRegistry = dynamicValueRegistry; - textSupplier = (Supplier) dynamicValueRegistry.get(dynamicRegistryKey); + if(dynamicValueRegistry != null) { + textSupplier = (Supplier) dynamicValueRegistry.get(dynamicRegistryKey); + } this.shadow = shadow; this.rainbow = rainbow; createMenu(); @@ -88,7 +90,7 @@ public void renderWidget(DrawContext drawContext,int mouseX, int mouseY) { if (textSupplier != null) { String text = textSupplier.get(); drawContext.drawText(mc.textRenderer, text, (int) getX(), (int) getY(), color, shadow); - widgetBox.setSizeAndPosition(getX() - 2, getY() - 2, mc.textRenderer.getWidth(text) + 2, mc.textRenderer.fontHeight + 2); + widgetBox.setSizeAndPosition(getX() - 2, getY() - 2, mc.textRenderer.getWidth(text) + 3, mc.textRenderer.fontHeight + 2); } } @@ -143,7 +145,7 @@ public void readFromTag(NbtCompound tag) { // If true then it means that we should use local registry and if false (i.e. null) then use global registry boolean dvrObj = tag.getBoolean("DynamicValueRegistry"); - if(!dvrObj && dynamicRegistryKey != null){ + if(!dvrObj){ textSupplier = (Supplier) DynamicValueRegistry.getGlobal(dynamicRegistryKey); return; } @@ -152,8 +154,6 @@ public void readFromTag(NbtCompound tag) { //Unfortunately, this method takes the value from the first local registry with the key. //It returns to prevent overriding with other registries textSupplier = (Supplier) dvr.get(dynamicRegistryKey); - System.out.println(dvr); - System.out.println(DynamicValueRegistry.getInstances(modId)); return; } createMenu();