From 3038f79077ff8cc3d36d7196084f06ca64aa2f19 Mon Sep 17 00:00:00 2001 From: tanishisherewithhh <120117618+tanishisherewithhh@users.noreply.github.com> Date: Wed, 29 May 2024 10:32:47 +0530 Subject: [PATCH] Few changes --- .../dynamichud/DynamicHudIntegration.java | 12 ++++++++++-- .../dynamichud/DynamicHudTest.java | 8 ++++---- .../dynamichud/config/GlobalConfig.java | 13 +++++++++++++ .../screens/AbstractMoveableScreen.java | 13 ++++++------- .../dynamichud/utils/contextmenu/ContextMenu.java | 2 +- .../utils/contextmenu/options/ColorOption.java | 1 - .../utils/contextmenu/options/SubMenuOption.java | 3 +-- .../dynamichud/widget/Widget.java | 15 ++++----------- .../dynamichud/widget/WidgetManager.java | 2 +- .../dynamichud/widget/WidgetRenderer.java | 4 ++++ .../dynamichud/widgets/ItemWidget.java | 1 + 11 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/tanishisherewith/dynamichud/DynamicHudIntegration.java b/src/main/java/com/tanishisherewith/dynamichud/DynamicHudIntegration.java index bc4fe2c..484f981 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/DynamicHudIntegration.java +++ b/src/main/java/com/tanishisherewith/dynamichud/DynamicHudIntegration.java @@ -8,8 +8,8 @@ import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; -import org.jetbrains.annotations.NotNull; import org.lwjgl.glfw.GLFW; +import org.lwjgl.system.NonnullDefault; import java.io.File; @@ -85,6 +85,9 @@ public interface DynamicHudIntegration { * WidgetManager.registerCustomWidget(TextWidget.DATA); * } * + * + * Custom widgets can be registered in any method in the interface + * but to avoid any errors and mishaps it is recommended you add them here */ default void registerCustomWidgets() { } @@ -118,10 +121,15 @@ default KeyBinding getKeyBind() { /** * Returns the movable screen for the DynamicHud. + *

+ *

+ * !! Should never be null !! + *

+ *

* * @return The movable screen. */ - @NotNull AbstractMoveableScreen getMovableScreen(); + AbstractMoveableScreen getMovableScreen(); /** * To return a {@link WidgetRenderer} object. diff --git a/src/main/java/com/tanishisherewith/dynamichud/DynamicHudTest.java b/src/main/java/com/tanishisherewith/dynamichud/DynamicHudTest.java index 5ef9656..0883606 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/DynamicHudTest.java +++ b/src/main/java/com/tanishisherewith/dynamichud/DynamicHudTest.java @@ -7,9 +7,7 @@ import com.tanishisherewith.dynamichud.widget.WidgetRenderer; import com.tanishisherewith.dynamichud.widgets.TextWidget; import net.minecraft.client.gui.screen.TitleScreen; -import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; import net.minecraft.text.Text; -import org.jetbrains.annotations.NotNull; import java.util.List; @@ -27,7 +25,7 @@ public void init() { //Local registry registry = new DynamicValueRegistry(DynamicHUD.MOD_ID); - registry.registerLocal("Hello", () -> "Hello!"); + registry.registerLocal("Hello", () -> "Hello " + DynamicHUD.MC.getSession().getUsername() + "!"); registry.registerLocal("DynamicHUD", () -> "DynamicHUD"); @@ -82,11 +80,13 @@ public void initAfter() { renderer = new WidgetRenderer(widgets); renderer.shouldRenderInGameHud(true); + + //This will make widgets render in the titlescreen as well. renderer.addScreen(TitleScreen.class); } @Override - public @NotNull AbstractMoveableScreen getMovableScreen() { + public AbstractMoveableScreen getMovableScreen() { return new AbstractMoveableScreen(Text.literal("Editor Screen"), renderer) { }; } diff --git a/src/main/java/com/tanishisherewith/dynamichud/config/GlobalConfig.java b/src/main/java/com/tanishisherewith/dynamichud/config/GlobalConfig.java index 0a18f68..fde8a7a 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/config/GlobalConfig.java +++ b/src/main/java/com/tanishisherewith/dynamichud/config/GlobalConfig.java @@ -26,6 +26,9 @@ public final class GlobalConfig { @SerialEntry private float scale = 1.0f; + @SerialEntry + private boolean displayDescriptions = false; + @SerialEntry private boolean showColorPickerPreview = true; @@ -54,6 +57,12 @@ public final Screen createYACLGUI() { .binding(true, () -> this.showColorPickerPreview, newVal -> this.showColorPickerPreview = newVal) .controller(booleanOption -> BooleanControllerBuilder.create(booleanOption).yesNoFormatter()) .build()) + .option(Option.createBuilder() + .name(Text.literal("Show widget descriptions/tooltips")) + .description(OptionDescription.of(Text.literal("Shows the description of widgets as tooltips."))) + .binding(true, () -> this.displayDescriptions, newVal -> this.displayDescriptions = newVal) + .controller(booleanOption -> BooleanControllerBuilder.create(booleanOption).yesNoFormatter()) + .build()) .build()) .build()) .build() @@ -66,4 +75,8 @@ public float getScale(){ public boolean showColorPickerPreview() { return showColorPickerPreview; } + + public boolean shouldDisplayDescriptions() { + return displayDescriptions; + } } diff --git a/src/main/java/com/tanishisherewith/dynamichud/screens/AbstractMoveableScreen.java b/src/main/java/com/tanishisherewith/dynamichud/screens/AbstractMoveableScreen.java index 00f0885..3ef82ab 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/screens/AbstractMoveableScreen.java +++ b/src/main/java/com/tanishisherewith/dynamichud/screens/AbstractMoveableScreen.java @@ -1,5 +1,6 @@ package com.tanishisherewith.dynamichud.screens; +import com.tanishisherewith.dynamichud.config.GlobalConfig; import com.tanishisherewith.dynamichud.widget.Widget; import com.tanishisherewith.dynamichud.widget.WidgetRenderer; import net.minecraft.client.gui.DrawContext; @@ -9,8 +10,6 @@ public abstract class AbstractMoveableScreen extends Screen { public final WidgetRenderer widgetRenderer; public int snapSize = 100; - protected boolean shouldPause = false; // To pause if the screen is opened or not - /** * Constructs a AbstractMoveableScreen object. */ @@ -81,6 +80,10 @@ public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) // Draw each widget widgetRenderer.renderWidgets(drawContext, mouseX, mouseY); + + if(widgetRenderer.selectedWidget != null && GlobalConfig.get().shouldDisplayDescriptions()){ + drawContext.drawTooltip(client.textRenderer,Text.of(widgetRenderer.selectedWidget.DATA.description()),mouseX,mouseY); + } } public void handleClickOnWidget(Widget widget, double mouseX, double mouseY, int button){ @@ -93,13 +96,9 @@ public void close() { super.close(); } - public void setShouldPause(boolean shouldPause) { - this.shouldPause = shouldPause; - } - @Override public boolean shouldPause() { - return shouldPause; + return false; } public void setSnapSize(int size) { diff --git a/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/ContextMenu.java b/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/ContextMenu.java index 272019c..688d7b8 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/ContextMenu.java +++ b/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/ContextMenu.java @@ -16,7 +16,7 @@ public class ContextMenu { public int finalWidth = 0; public int height = 0; public Color backgroundColor = new Color(107, 112, 126, 124); - private Color darkerBorderColor = backgroundColor.darker().darker().darker().darker().darker().darker(); + private final 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 diff --git a/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/options/ColorOption.java b/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/options/ColorOption.java index 7092ddb..6ad4d49 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/options/ColorOption.java +++ b/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/options/ColorOption.java @@ -21,7 +21,6 @@ public ColorOption(String name, ContextMenu parentMenu, Supplier getter, super(getter, setter); this.name = name; this.parentMenu = parentMenu; - System.out.println(get()); colorPicker = new ColorGradientPicker(x + this.parentMenu.finalWidth, y - 10, get(), this::set, 50, 100); } diff --git a/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/options/SubMenuOption.java b/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/options/SubMenuOption.java index c917ee8..5d23702 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/options/SubMenuOption.java +++ b/src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/options/SubMenuOption.java @@ -4,7 +4,6 @@ import com.tanishisherewith.dynamichud.utils.contextmenu.Option; import net.minecraft.client.gui.DrawContext; import net.minecraft.text.Text; -import org.jetbrains.annotations.NotNull; import java.awt.*; import java.util.Objects; @@ -24,7 +23,7 @@ public class SubMenuOption extends Option { public String name = "Empty"; - public SubMenuOption(String name, @NotNull ContextMenu parentMenu, Supplier getter, Consumer setter) { + public SubMenuOption(String name, ContextMenu parentMenu, Supplier getter, Consumer setter) { super(getter, setter); Objects.requireNonNull(parentMenu, "Parent Menu cannot be null"); this.name = name; diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/Widget.java b/src/main/java/com/tanishisherewith/dynamichud/widget/Widget.java index 6b36576..3898d1f 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/Widget.java +++ b/src/main/java/com/tanishisherewith/dynamichud/widget/Widget.java @@ -145,6 +145,8 @@ public final void render(DrawContext drawContext, int mouseX, int mouseY) { * Renders the widget on the editor screen. */ public final void renderInEditor(DrawContext drawContext, int mouseX, int mouseY) { + if(!isInEditor) return; + displayBg(drawContext); if (shouldScale) { @@ -255,7 +257,6 @@ public void onClose() { * Displays a faint grayish background if enabled or faint reddish background if disabled. * Drawn with 2 pixel offset to all sides * - * @param context */ protected void displayBg(DrawContext context) { int backgroundColor = this.shouldDisplay() ? ColorHelper.getColor(0, 0, 0, 128) : ColorHelper.getColor(255, 0, 0, 128); @@ -346,11 +347,7 @@ public abstract static class WidgetBuilder { /** - * X Position of the widget relative to the screen. - * Should be between 0f - 1f - * - * @param x - * @return Builder + * X Position of the widget of the scaled screen. */ public T setX(int x) { this.x = x; @@ -358,11 +355,7 @@ public T setX(int x) { } /** - * Y Position of the widget relative to the screen. - * Should be between 0f - 1f - * - * @param y - * @return + * Y Position of the widget of the scaled screen. */ public T setY(int y) { this.y = y; diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetManager.java b/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetManager.java index f36c3ad..978feac 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetManager.java +++ b/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetManager.java @@ -100,7 +100,7 @@ public static void removeWidget(Widget widget) { */ public static void onScreenResized(int newWidth, int newHeight, int previousWidth, int previousHeight) { for (Widget widget : widgets) { - // To ensure that infinite coordinates is not returned + // To ensure that infinite coordinates is not returned for the first time its resized. if (widget.xPercent <= 0.0f) { widget.xPercent = (float) widget.getX() / previousWidth; } diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetRenderer.java b/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetRenderer.java index 6b9f8a6..5a68452 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetRenderer.java +++ b/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetRenderer.java @@ -46,6 +46,7 @@ public void renderWidgets(DrawContext context, int mouseX, int mouseY) { Screen currentScreen = DynamicHUD.MC.currentScreen; + //Render in game hud if (currentScreen == null && renderInGameHud) { for (Widget widget : widgets) { widget.isInEditor = false; @@ -53,6 +54,8 @@ public void renderWidgets(DrawContext context, int mouseX, int mouseY) { } return; } + + //Render in editing screen if (currentScreen instanceof AbstractMoveableScreen) { for (Widget widget : widgets) { widget.isInEditor = true; @@ -60,6 +63,7 @@ public void renderWidgets(DrawContext context, int mouseX, int mouseY) { } return; } + //Render in any other screen if (currentScreen != null && allowedScreens.contains(DynamicHUD.MC.currentScreen.getClass()) && !this.isInEditor) { for (Widget widget : widgets) { widget.isInEditor = false; diff --git a/src/main/java/com/tanishisherewith/dynamichud/widgets/ItemWidget.java b/src/main/java/com/tanishisherewith/dynamichud/widgets/ItemWidget.java index deb1571..7b570ba 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/widgets/ItemWidget.java +++ b/src/main/java/com/tanishisherewith/dynamichud/widgets/ItemWidget.java @@ -44,6 +44,7 @@ public void readFromTag(NbtCompound tag) { public void setItemStack(ItemStack item) { this.item = item; } + public static class Builder extends WidgetBuilder{ ItemStack itemStack; public Builder setItemStack(ItemStack itemStack) {