From 18d26278edc30cd5d2988e721fae10d115a69c48 Mon Sep 17 00:00:00 2001 From: tanishisherewithhh <120117618+tanishisherewithhh@users.noreply.github.com> Date: Sat, 13 Apr 2024 21:54:32 +0530 Subject: [PATCH] Removed old code --- .../dynamichud/DynamicHUD.java | 164 ------- .../dynamichud/DynamicHUDtest.java | 177 ------- .../handlers/DefaultDragHandler.java | 32 -- .../handlers/DefaultMouseHandler.java | 78 ---- .../dynamichud/handlers/DragHandler.java | 12 - .../dynamichud/handlers/MouseHandler.java | 21 - .../dynamichud/helpers/ColorHelper.java | 146 ------ .../dynamichud/helpers/DrawHelper.java | 249 ---------- .../dynamichud/helpers/TextureHelper.java | 233 --------- .../huds/AbstractMoveableScreen.java | 214 --------- .../dynamichud/huds/MoveableScreen.java | 175 ------- .../dynamichud/interfaces/IWigdets.java | 12 - .../dynamichud/interfaces/TextGenerator.java | 6 - .../dynamichud/interfaces/WidgetLoading.java | 34 -- .../dynamichud/newTrial/DynamicHUD.java | 6 +- .../newTrial/config/GlobalConfig.java | 2 +- .../newTrial/helpers/DrawHelper.java | 3 +- .../newTrial/helpers/TextureHelper.java | 233 --------- .../screens/AbstractMoveableScreen.java | 1 + .../utils/contextmenu/ContextMenu.java | 2 +- .../newTrial/utils/contextmenu/Option.java | 2 +- .../coloroption/ColorGradientPicker.java | 2 +- .../options/coloroption/GradientBox.java | 4 +- .../options/coloroption/GradientSlider.java | 4 +- .../newTrial/widget/WidgetManager.java | 34 +- .../newTrial/widget/WidgetRenderer.java | 2 +- .../newTrial/widgets/TextWidget.java | 5 +- .../dynamichud/util/CustomItemRenderer.java | 39 -- .../dynamichud/util/CustomTextRenderer.java | 39 -- .../dynamichud/util/DynamicUtil.java | 104 ----- .../util/colorpicker/ColorGradientPicker.java | 116 ----- .../util/colorpicker/ColorPickerButton.java | 46 -- .../util/colorpicker/GradientBox.java | 129 ----- .../util/colorpicker/GradientSlider.java | 125 ----- .../util/contextmenu/ContextMenu.java | 441 ------------------ .../ContextMenuOptionsProvider.java | 6 - .../util/contextmenu/DataInputScreen.java | 57 --- .../util/contextmenu/DoubleInputScreen.java | 60 --- .../util/contextmenu/TextWidgetButtonExt.java | 13 - .../dynamichud/widget/Widget.java | 218 --------- .../dynamichud/widget/WidgetBox.java | 75 --- .../dynamichud/widget/WidgetManager.java | 200 -------- .../dynamichud/widget/armor/ArmorWidget.java | 141 ------ .../dynamichud/widget/item/ItemWidget.java | 141 ------ .../widget/slider/ScaleSliderWidget.java | 34 -- .../widget/slider/SliderWidget.java | 216 --------- .../widget/slider/SliderWidgetBuilder.java | 77 --- .../dynamichud/widget/text/TextWidget.java | 277 ----------- 48 files changed, 22 insertions(+), 4385 deletions(-) delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/DynamicHUD.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/DynamicHUDtest.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/handlers/DefaultDragHandler.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/handlers/DefaultMouseHandler.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/handlers/DragHandler.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/handlers/MouseHandler.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/helpers/ColorHelper.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/helpers/DrawHelper.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/helpers/TextureHelper.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/huds/AbstractMoveableScreen.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/huds/MoveableScreen.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/interfaces/IWigdets.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/interfaces/TextGenerator.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/interfaces/WidgetLoading.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/TextureHelper.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/CustomItemRenderer.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/CustomTextRenderer.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/DynamicUtil.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/ColorGradientPicker.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/ColorPickerButton.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/GradientBox.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/GradientSlider.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/ContextMenu.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/ContextMenuOptionsProvider.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/DataInputScreen.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/DoubleInputScreen.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/TextWidgetButtonExt.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/widget/Widget.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/widget/WidgetBox.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/widget/WidgetManager.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/widget/armor/ArmorWidget.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/widget/item/ItemWidget.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/widget/slider/ScaleSliderWidget.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/widget/slider/SliderWidget.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/widget/slider/SliderWidgetBuilder.java delete mode 100644 src/main/java/com/tanishisherewith/dynamichud/widget/text/TextWidget.java diff --git a/src/main/java/com/tanishisherewith/dynamichud/DynamicHUD.java b/src/main/java/com/tanishisherewith/dynamichud/DynamicHUD.java deleted file mode 100644 index 1c86728..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/DynamicHUD.java +++ /dev/null @@ -1,164 +0,0 @@ -package com.tanishisherewith.dynamichud; - -import com.tanishisherewith.dynamichud.huds.AbstractMoveableScreen; -import com.tanishisherewith.dynamichud.interfaces.IWigdets; -import com.tanishisherewith.dynamichud.newTrial.utils.UID; -import com.tanishisherewith.dynamichud.util.DynamicUtil; -import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; -import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; -import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.util.InputUtil; -import net.minecraft.resource.LifecycledResourceManager; -import net.minecraft.server.MinecraftServer; -import org.lwjgl.glfw.GLFW; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; - -public class DynamicHUD implements ClientModInitializer { - - public static MinecraftClient MC = MinecraftClient.getInstance(); - - public static final Logger logger = LoggerFactory.getLogger("DynamicHud"); - static AbstractMoveableScreen Screen; - private static String keybingCategory = "DynamicHud"; - private static String TranslationKey = "DynamicHud Editor Screen"; - private static InputUtil.Type inputType = InputUtil.Type.KEYSYM; - private static int key = GLFW.GLFW_KEY_RIGHT_SHIFT; - // public static final KeyBinding EditorScreenKeyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding( -// TranslationKey, -// inputType, -// key, -// keybingCategory -// )); - private static String filename = "widgets.nbt"; - private static File fileDirectory = FabricLoader.getInstance().getConfigDir().toFile(); - public static final File WIDGETS_FILE = new File(fileDirectory, filename); - private static DynamicUtil dynamicutil; - private static IWigdets iWigdets; - MinecraftClient mc = MinecraftClient.getInstance(); - - public static File getFileDirectory() { - return fileDirectory; - } - - public static String getFilename() { - return filename; - } - - public static String getTranslationKey() { - return TranslationKey; - } - - public static String getKeybingCategory() { - return keybingCategory; - } - - public static InputUtil.Type getInputType() { - return inputType; - } - - public static void setInputType(InputUtil.Type inputType) { - DynamicHUD.inputType = inputType; - } - - public static void setKeyBindKey(int key) { - DynamicHUD.key = key; - } - - public static void setKeybingCategory(String keybingCategory) { - DynamicHUD.keybingCategory = keybingCategory; - } - - public static void setTranslationKey(String translationKey) { - TranslationKey = translationKey; - } - - public static void setFilename(String filename) { - DynamicHUD.filename = filename; - } - - public static void setFileDirectory(File fileDirectory) { - DynamicHUD.fileDirectory = fileDirectory; - } - - public static void setAbstractScreen(AbstractMoveableScreen screen) { - Screen = screen; - } - - public static AbstractMoveableScreen getScreen() { - return Screen; - } - - public static DynamicUtil getDynamicUtil() { - return dynamicutil; - } - - public static IWigdets getIWigdets() { - return iWigdets; - } - - public static void setIWigdets(IWigdets iWigdets) { - DynamicHUD.iWigdets = iWigdets; - } - - public static void printInfo(String msg) { - logger.info(msg); - } - - public static void printWarn(String msg) { - logger.warn(msg); - } - - @Override - public void onInitializeClient() { - dynamicutil = new DynamicUtil(mc); - printInfo("DynamicHud Initialised"); - - //Save and Load - ClientTickEvents.START_CLIENT_TICK.register(server -> { - if (iWigdets != null) { - if (!WIDGETS_FILE.exists()) { - if (!dynamicutil.WidgetAdded) { - iWigdets.addWigdets(dynamicutil); - } - if (!dynamicutil.MainMenuWidgetAdded) { - iWigdets.addMainMenuWigdets(dynamicutil); - } - } - - if (WIDGETS_FILE.exists() && !dynamicutil.WidgetLoaded) { - iWigdets.loadWigdets(dynamicutil); - printInfo("Widgets loaded"); - File FileDirectory = new File(fileDirectory, filename); - printInfo("Load file Directory: " + FileDirectory); - } - } - // DynamicUtil.openDynamicScreen(EditorScreenKeyBinding, Screen); - }); - - //RenderCallBack - HudRenderCallback.EVENT.register((drawContext, tickDelta) -> dynamicutil.render(drawContext, tickDelta)); - - // Save during exiting a world, server or Minecraft itself - ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStopping); - ServerLifecycleEvents.END_DATA_PACK_RELOAD.register(this::onEndDataPackReload); - ServerPlayConnectionEvents.DISCONNECT.register((handler, packetSender) -> dynamicutil.getWidgetManager().saveWidgets(WIDGETS_FILE)); - Runtime.getRuntime().addShutdownHook(new Thread(() -> dynamicutil.getWidgetManager().saveWidgets(WIDGETS_FILE))); - } - - private void onEndDataPackReload(MinecraftServer server, LifecycledResourceManager lifecycledResourceManager, boolean b) { - dynamicutil.getWidgetManager().saveWidgets(WIDGETS_FILE); - } - - private void onServerStopping(MinecraftServer server) { - dynamicutil.getWidgetManager().saveWidgets(WIDGETS_FILE); - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/DynamicHUDtest.java b/src/main/java/com/tanishisherewith/dynamichud/DynamicHUDtest.java deleted file mode 100644 index d606440..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/DynamicHUDtest.java +++ /dev/null @@ -1,177 +0,0 @@ -package com.tanishisherewith.dynamichud; - -import com.tanishisherewith.dynamichud.helpers.TextureHelper; -import com.tanishisherewith.dynamichud.huds.MoveableScreen; -import com.tanishisherewith.dynamichud.interfaces.IWigdets; -import com.tanishisherewith.dynamichud.interfaces.WidgetLoading; -import com.tanishisherewith.dynamichud.util.DynamicUtil; -import com.tanishisherewith.dynamichud.widget.Widget; -import com.tanishisherewith.dynamichud.widget.armor.ArmorWidget; -import com.tanishisherewith.dynamichud.widget.item.ItemWidget; -import com.tanishisherewith.dynamichud.widget.text.TextWidget; -import net.minecraft.client.MinecraftClient; -import net.minecraft.entity.EquipmentSlot; -import net.minecraft.item.Items; -import net.fabricmc.api.ClientModInitializer; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.text.Text; - -import java.awt.*; -import java.util.HashSet; -import java.util.Set; - -import static com.tanishisherewith.dynamichud.DynamicHUD.WIDGETS_FILE; -import static com.tanishisherewith.dynamichud.DynamicHUD.printInfo; - -public class DynamicHUDtest implements ClientModInitializer, IWigdets, WidgetLoading { - protected Set widgets = new HashSet<>(); - protected Set MainMenuwidgets = new HashSet<>(); - MinecraftClient mc = MinecraftClient.getInstance(); - private DynamicUtil dynamicutil; - - - @Override - public void onInitializeClient() { - dynamicutil = DynamicHUD.getDynamicUtil(); - widgets.clear(); - MainMenuwidgets.clear(); - - DynamicHUD.setAbstractScreen(new MoveableScreen(Text.of("Editor Screen"), dynamicutil)); - DynamicHUD.setIWigdets(new DynamicHUDtest()); - dynamicutil.getWidgetManager().setWidgetLoading(new DynamicHUDtest()); - } - - @Override - public void addWigdets(DynamicUtil dynamicUtil) { - if (mc.player != null) { - printInfo("Widgets added"); - addTextWidgets(dynamicUtil); - addArmorWidgets(dynamicUtil); - addItemWidgets(dynamicUtil); - dynamicUtil.WidgetAdded = true; - } - } - - private void addTextWidgets(DynamicUtil dynamicUtil) { - widgets.add(new TextWidget(mc, "Non Draggable FPS: ", () -> mc.fpsDebugString.split(" ")[0], 0.5f, 0.5f, true, true, -1, -1, true)); - widgets.add(new TextWidget(mc, "Dynamic", () -> "HUD", 0.7f, 0.3f, false, false, -1, -1, true)); - widgets.add(new TextWidget(mc, "Test", () -> "", 0.08f, 0.5f, false, false, -1, -1, true)); - widgets.add(new TextWidget(mc, "", () -> "Data Test", 0.4f, 0.8f, false, false, -1, -1, true)); - widgets.add(new TextWidget(mc, "HUD Test ", () -> "", 0.83f, 0.8f, false, false, -1, -1, true)); - - for (Widget widget : widgets) { - if (widget instanceof TextWidget textWidget) { - if (textWidget.getText().equalsIgnoreCase("Non Draggable FPS: ")) { - textWidget.setDraggable(false); - } - } - dynamicUtil.getWidgetManager().addWidget(widget); - } - } - - private void addArmorWidgets(DynamicUtil dynamicUtil) { - String text = "Text"; - widgets.add(new ArmorWidget(mc, EquipmentSlot.CHEST, 0.01f, 0.01f, true, - TextureHelper.Position.ABOVE, - () -> text, - () -> Color.RED, - true, - "Text")); - widgets.add(new ArmorWidget(mc, - EquipmentSlot.LEGS, - 0.05f, - 0.01f, - true, - TextureHelper.Position.LEFT, - () -> String.valueOf(MinecraftClient.getInstance().getCurrentFps()), - () -> Color.WHITE, - true, - "FPS")); - - for (Widget widget : widgets) { - if (widget instanceof ArmorWidget armorWidget) { - dynamicUtil.getWidgetManager().addWidget(armorWidget); - } - } - } - - private void addItemWidgets(DynamicUtil dynamicUtil) { - widgets.add(new ItemWidget(mc, - Items.DIAMOND_AXE::getDefaultStack, - 0.15f, - 0.15f, - true, - TextureHelper.Position.ABOVE, - () -> "Label", - () -> Color.RED, - true, - "Label")); - - for (Widget widgetItem : widgets) { - if (widgetItem instanceof ItemWidget itemWidgetItem) { - dynamicUtil.getWidgetManager().addWidget(itemWidgetItem); - } - } - } - - - @Override - public void addMainMenuWigdets(DynamicUtil dynamicUtil) { - printInfo("MainMenu Widgets added"); - - MainMenuwidgets.add(new TextWidget(mc, "Test ", () -> "", 0.83f, 0.8f, false, false, -1, -1, true)); - MainMenuwidgets.add(new TextWidget(mc, "E Test ", () -> "", 0.85f, 0.3f, false, false, -1, -1, true)); - MainMenuwidgets.add(new TextWidget(mc, "Non Draggable FPS: ", () -> String.valueOf(mc.getCurrentFps()), 0.67f, 0.5f, false, false, -1, -1, true)); - for (Widget mmwigdet : MainMenuwidgets) { - if (mmwigdet instanceof TextWidget textWidget) { - if (textWidget.getText().equalsIgnoreCase("Non Draggable FPS: ")) { - textWidget.setDraggable(false); - } - } - dynamicUtil.getWidgetManager().addMainMenuWidget(mmwigdet); - } - dynamicUtil.MainMenuWidgetAdded = true; - } - - @Override - public void loadWigdets(DynamicUtil dynamicUtil) { - Set widgets = dynamicUtil.getWidgetManager().loadWigdets(WIDGETS_FILE); - Set MainMenuWidget = dynamicUtil.getWidgetManager().loadMainMenuWigdets(WIDGETS_FILE); - - Widget.addTextGenerator("Non Draggable FPS: ", () -> String.valueOf(mc.getCurrentFps())); - Widget.addTextGenerator("Dynamic", () -> "HUD"); - Widget.addTextGenerator("Test", () -> ""); - Widget.addTextGenerator("", () -> "Data Test"); - Widget.addTextGenerator("HUD Test ", () -> ""); - Widget.addTextGenerator("Text", () -> "Text"); - Widget.addTextGenerator("Test ", () -> String.valueOf(mc.getCurrentFps())); - Widget.addTextGenerator("Non Draggable FPS: ", () -> String.valueOf(mc.getCurrentFps())); - Widget.addTextGenerator("Label", () -> "Label"); - - for (Widget widget : widgets) { - dynamicUtil.getWidgetManager().addWidget(widget); - } - - for (Widget widgetItem : MainMenuWidget) { - dynamicUtil.getWidgetManager().addMainMenuWidget(widgetItem); - } - - dynamicUtil.WidgetLoaded = true; - } - - - @Override - public Widget loadWidgetsFromTag(String className, NbtCompound widgetTag) { - //SAMPLE CODE EXAMPLE : - /*if (className.equals(ItemWidget.class.getName())) { - ItemWidget widget = new ItemWidget(MinecraftClient.getInstance(), ItemStack.EMPTY, 0, 0, true, TextureHelper.Position.ABOVE, () -> "", Color.BLUE); - widget.readFromTag(widgetTag); - return widget; - }*/ - System.out.println("widget tag loaded"); - return WidgetLoading.super.loadWidgetsFromTag(className, widgetTag); - } -} - - - diff --git a/src/main/java/com/tanishisherewith/dynamichud/handlers/DefaultDragHandler.java b/src/main/java/com/tanishisherewith/dynamichud/handlers/DefaultDragHandler.java deleted file mode 100644 index 02e77db..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/handlers/DefaultDragHandler.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.tanishisherewith.dynamichud.handlers; - -import com.tanishisherewith.dynamichud.widget.Widget; - -public class DefaultDragHandler implements DragHandler { - private int dragStartX = 0; - private int dragStartY = 0; - - @Override - public boolean startDragging(Widget widget, double mouseX, double mouseY) { - if (widget.getWidgetBox().contains(widget, mouseX, mouseY, Widget.getScale())) { - dragStartX = (int) (mouseX - widget.getX()); - dragStartY = (int) (mouseY - widget.getY()); - return true; - } - return false; - } - - @Override - public void updateDragging(Widget widget, double mouseX, double mouseY) { - int newX = (int) (dragStartX + mouseX); - int newY = (int) (dragStartY + mouseY); - widget.setX(newX + (newX / 2)); - widget.setY(newY + (newY / 2)); - } - - @Override - public void stopDragging(Widget widget) { - // Nothing to do here in the default implementation - } -} - diff --git a/src/main/java/com/tanishisherewith/dynamichud/handlers/DefaultMouseHandler.java b/src/main/java/com/tanishisherewith/dynamichud/handlers/DefaultMouseHandler.java deleted file mode 100644 index 5a9c03f..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/handlers/DefaultMouseHandler.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.tanishisherewith.dynamichud.handlers; - -import com.tanishisherewith.dynamichud.util.colorpicker.ColorGradientPicker; -import com.tanishisherewith.dynamichud.util.contextmenu.ContextMenu; -import com.tanishisherewith.dynamichud.widget.slider.SliderWidget; -import com.tanishisherewith.dynamichud.widget.text.TextWidget; - -import java.util.List; - -public class DefaultMouseHandler implements MouseHandler { - private final ColorGradientPicker colorPicker; - private final List contextMenu; - private final List sliderWidget; - - public DefaultMouseHandler(ColorGradientPicker colorPicker, List contextMenu, List sliderWidget) { - this.colorPicker = colorPicker; - this.contextMenu = contextMenu; - this.sliderWidget = sliderWidget; - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (contextMenu != null && sliderWidget != null) { - for (ContextMenu contextMenu : contextMenu) { - if (contextMenuClicked(mouseX, mouseY, button, contextMenu)) { - return true; - } - } - for (SliderWidget sliderWidget : sliderWidget) { - if (sliderClicked(mouseX, mouseY, button, sliderWidget)) { - return true; - } - } - } - return colorPickerClicked(mouseX, mouseY, button); - } - - @Override - public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { - if (sliderWidget != null) { - for (SliderWidget sliderWidget : sliderWidget) { - if (sliderWidget != null && sliderWidget.mouseDragged(mouseX, mouseY, button, deltaX, deltaY)) { - return true; - } - } - } - if (this.colorPicker != null) { - colorPicker.mouseDragged(mouseX, mouseY, button); - return true; - } - return false; - } - - @Override - public boolean mouseReleased(double mouseX, double mouseY, int button) { - if (this.colorPicker != null) { - colorPicker.mouseReleased(mouseX, mouseY, button); - return true; - } - return false; - } - - - @Override - public boolean contextMenuClicked(double mouseX, double mouseY, int button, ContextMenu contextMenu) { - return contextMenu != null && contextMenu.mouseClicked(mouseX, mouseY, button); - } - - @Override - public boolean colorPickerClicked(double mouseX, double mouseY, int button) { - return colorPicker != null && colorPicker.mouseClicked(mouseX, mouseY, button); - } - - @Override - public boolean sliderClicked(double mouseX, double mouseY, int button, SliderWidget sliderWidget) { - return sliderWidget != null && sliderWidget.mouseClicked(mouseX, mouseY, button); - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/handlers/DragHandler.java b/src/main/java/com/tanishisherewith/dynamichud/handlers/DragHandler.java deleted file mode 100644 index 8c2c090..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/handlers/DragHandler.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.tanishisherewith.dynamichud.handlers; - -import com.tanishisherewith.dynamichud.widget.Widget; - -public interface DragHandler { - boolean startDragging(Widget widget, double mouseX, double mouseY); - - void updateDragging(Widget widget, double mouseX, double mouseY); - - void stopDragging(Widget widget); -} - diff --git a/src/main/java/com/tanishisherewith/dynamichud/handlers/MouseHandler.java b/src/main/java/com/tanishisherewith/dynamichud/handlers/MouseHandler.java deleted file mode 100644 index bb587f3..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/handlers/MouseHandler.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.tanishisherewith.dynamichud.handlers; - -import com.tanishisherewith.dynamichud.util.contextmenu.ContextMenu; -import com.tanishisherewith.dynamichud.widget.slider.SliderWidget; - -public interface MouseHandler { - boolean mouseClicked(double mouseX, double mouseY, int button); - - boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY); - - boolean mouseReleased(double mouseX, double mouseY, int button); - - boolean contextMenuClicked(double mouseX, double mouseY, int button, ContextMenu contextMenu); - - boolean colorPickerClicked(double mouseX, double mouseY, int button); - - boolean sliderClicked(double mouseX, double mouseY, int button, SliderWidget sliderWidget); - - -} - diff --git a/src/main/java/com/tanishisherewith/dynamichud/helpers/ColorHelper.java b/src/main/java/com/tanishisherewith/dynamichud/helpers/ColorHelper.java deleted file mode 100644 index 9e3884f..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/helpers/ColorHelper.java +++ /dev/null @@ -1,146 +0,0 @@ -package com.tanishisherewith.dynamichud.helpers; - -import net.minecraft.network.packet.Packet; -import net.minecraft.util.math.MathHelper; - -import java.awt.*; - -/** - * This class provides helper methods for working with colors. - */ -public class ColorHelper { - public static int r, g, b, a; - public ColorHelper(int r, int g, int b) { - this.r = r; - this.g = g; - this.b = b; - this.a = 255; - validate(); - } - public ColorHelper() { - } - public void validate() { - if (r < 0) r = 0; - else if (r > 255) r = 255; - - if (g < 0) g = 0; - else if (g > 255) g = 255; - - if (b < 0) b = 0; - else if (b > 255) b = 255; - - if (a < 0) a = 0; - else if (a > 255) a = 255; - } - /** - * Returns a color as an integer value given its red, green and blue components. - * - * @param red The red component of the color - * @param green The green component of the color - * @param blue The blue component of the color - * @return The color as an integer value - */ - public static int getColor(int red, int green, int blue) { - return getColor(red, green, blue, 255); - } - - /** - * Returns a color as an integer value given its red, green, blue and alpha components. - * - * @param red The red component of the color - * @param green The green component of the color - * @param blue The blue component of the color - * @param alpha The alpha component of the color - * @return The color as an integer value - */ - public static int getColor(int red, int green, int blue, int alpha) { - return (alpha << 24) | (red << 16) | (green << 8) | blue; - } - - public static Color getColorFromInt(int color) { - int red = (color >> 16) & 0xFF; - int green = (color >> 8) & 0xFF; - int blue = color & 0xFF; - return new Color(red, green, blue); - } - - /** - * Returns a color as an integer value given its hue. - * - * @param hue The hue of the color - * @return The color as an integer value - */ - public static int getColorFromHue(float hue) { - return Color.HSBtoRGB(hue, 1.0f, 1.0f); - } - - /** - * Converts a color to an integer. - * - * @param color The color to convert - * @return The integer representation of the JWT color - */ - public static int ColorToInt(Color color) { - return color.getRGB(); - } - public static float[] getRainbowColor() - { - float x = System.currentTimeMillis() % 2000 / 1000F; - float pi = (float)Math.PI; - - float[] rainbow = new float[3]; - rainbow[0] = 0.5F + 0.5F * MathHelper.sin(x * pi); - rainbow[1] = 0.5F + 0.5F * MathHelper.sin((x + 4F / 3F) * pi); - 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); - } - - public static int toRGBAR(int color) { - return (color >> 16) & 0x000000FF; - } - - public static int toRGBAG(int color) { - return (color >> 8) & 0x000000FF; - } - - public static int toRGBAB(int color) { - return (color) & 0x000000FF; - } - - public static int toRGBAA(int color) { - return (color >> 24) & 0x000000FF; - } - public int toInt() - { - return new Color(r,b,g,a).getRGB(); - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/helpers/DrawHelper.java b/src/main/java/com/tanishisherewith/dynamichud/helpers/DrawHelper.java deleted file mode 100644 index f997e28..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/helpers/DrawHelper.java +++ /dev/null @@ -1,249 +0,0 @@ -package com.tanishisherewith.dynamichud.helpers; - -import com.mojang.blaze3d.systems.RenderSystem; -import com.tanishisherewith.dynamichud.newTrial.DynamicHUD; -import com.tanishisherewith.dynamichud.util.CustomTextRenderer; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.render.*; -import net.minecraft.client.util.math.MatrixStack; -import org.joml.Matrix4f; - -public class DrawHelper extends DrawContext { - public static CustomTextRenderer customTextRenderer; - - public DrawHelper(MinecraftClient client, VertexConsumerProvider.Immediate vertexConsumers) { - super(client, vertexConsumers); - } - - /** - * Fills a box on the screen with a specified color. - * - * @param drawContext The matrix stack used for rendering - * @param x The x position of the rectangle - * @param y The y position of the rectangle - * @param width The width of the rectangle - * @param height The height of the rectangle - * @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) { - drawContext.fill(x, y, x + width, y + height, color); - } - - /** - * Fills a rectangle on the screen with a specified color. - * - * @param x1 The x position of the top left corner of the rectangle - * @param y1 The y position of the top left corner of the rectangle - * @param x2 The x position of the bottom right corner of the rectangle - * @param y2 The y position of the bottom right corner of the rectangle - * @param color The color to fill the rectangle with - */ - public static void fill(DrawContext drawContext, int x1, int y1, int x2, int y2, int color) { - drawContext.fill(x1, y1, x2, y2, color); - } - - /** - * Draws text on screen. - * - * @param textRenderer - TextRenderer instance used for rendering. - * @param text - Text to be drawn. - * @param x - X position to draw at. - * @param y - Y position to draw at. - * @param color - Color to draw with. - */ - public static void drawText(DrawContext drawContext, - TextRenderer textRenderer, - String text, - int x, - int y, - int color, - boolean shadow) { - drawContext.drawText(textRenderer, text, x, y, color, shadow); - } - - - /** - * Draws text on screen. - * - * @param drawContext - drawContext used for rendering. - * @param text - Text to be drawn. - * @param x - X position to draw at. - * @param y - Y position to draw at. - * @param color - Color to draw with. - */ - public static void drawTextWithScale(DrawContext drawContext, - String text, - int x, - int y, - int color, - boolean shadow, - float scale) { - customTextRenderer = new CustomTextRenderer(MinecraftClient.getInstance(), scale); - customTextRenderer.draw(drawContext, text, x, y, color, shadow); - } - - - public static void drawCutRectangle(DrawContext drawContext, int x1, int y1, int x2, int y2, int z, int color, int cornerRadius) { - // Draw the rectangles - drawContext.fill(x1 + cornerRadius, y1, x2 - cornerRadius, y1 + cornerRadius, z, color); - drawContext.fill(x1 + cornerRadius, y2 - cornerRadius, x2 - cornerRadius, y2, z, color); - drawContext.fill(x1, y1 + cornerRadius, x2, y2 - cornerRadius, z, color); - } - - - /** - * Fills a rounded rectangle on screen with specified color. - * This causes a lot of problems and for some reason does not work for ArmorWidget when used for contextMenu - * - * @param matrix4f - Matrix4f used for rendering. - * @param x1 - X position of top left corner of rectangle. - * @param y1 - Y position of top left corner of rectangle. - * @param x2 - X position of bottom right corner of rectangle. - * @param y2 - Y position of bottom right corner of rectangle. - * @param cornerRadius - Radius of rounded corners. - * @param color - Color to fill rectangle with. - */ - public static void fillRoundedRect(Matrix4f matrix4f, int x1, int y1, int x2, int y2, int cornerRadius, int color) { - float alpha = (float) (color >> 24 & 255) / 255.0F; - float red = (float) (color >> 16 & 255) / 255.0F; - float green = (float) (color >> 8 & 255) / 255.0F; - float blue = (float) (color & 255) / 255.0F; - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder bufferBuilder = tessellator.getBuffer(); - RenderSystem.enableBlend(); - RenderSystem.defaultBlendFunc(); - bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR); - - // Draw the center rectangle - bufferBuilder.vertex(matrix4f, x1 + cornerRadius, y2 - cornerRadius, 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, x2 - cornerRadius, y2 - cornerRadius, 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, x2 - cornerRadius, y1 + cornerRadius, 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, x1 + cornerRadius, y1 + cornerRadius, 0).color(red, green, blue, alpha).next(); - - // Draw the side rectangles - bufferBuilder.vertex(matrix4f, x1, y1 + cornerRadius, 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, x1 + cornerRadius, y1 + cornerRadius, 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, x1 + cornerRadius, y2 - cornerRadius, 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, x1, y2 - cornerRadius, 0).color(red, green, blue, alpha).next(); - - bufferBuilder.vertex(matrix4f, x2, y1 + cornerRadius, 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, x2 - cornerRadius, y1 + cornerRadius, 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, x2 - cornerRadius, y2 - cornerRadius, 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, x2, y2 - cornerRadius, 0).color(red, green, blue, alpha).next(); - - - // Draw the rounded corners - for (int i = 0; i <= 90; i += 5) { - double angle = Math.toRadians(i); - double sin = Math.sin(angle); - double cos = Math.cos(angle); - bufferBuilder.vertex(matrix4f, (float) (x1 + cornerRadius * (1 - cos)), (float) (y1 + cornerRadius * (1 - sin)), 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, (float) (x1 + cornerRadius * (1 - cos)), (float) (y2 - cornerRadius * (1 - sin)), 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, (float) (x2 - cornerRadius * (1 - cos)), (float) (y2 - cornerRadius * (1 - sin)), 0).color(red, green, blue, alpha).next(); - bufferBuilder.vertex(matrix4f, (float) (x2 - cornerRadius * (1 - cos)), (float) (y1 + cornerRadius * (1 - sin)), 0).color(red, green, blue, alpha).next(); - } - tessellator.draw(); - RenderSystem.disableBlend(); - } - - public static void fillRoundedRect(DrawContext drawContext, int left, int top, int right, int bottom, int color) { - drawContext.fill(left + 1, top, right - 1, top + 1, color); - drawContext.fill(left + 1, bottom - 1, right - 1, bottom, color); - drawContext.fill(left, top + 1, left + 1, bottom - 1, color); - drawContext.fill(right - 1, top + 1, right, bottom - 1, color); - drawContext.fill(left + 1, top + 1, right - 1, bottom - 1, color); - } - - /** - * Fills a rectangle on screen with a gradient. - * - * @param matrix4f - Matrix4f used for rendering. - * @param x1 - X position of top left corner of rectangle. - * @param y1 - Y position of top left corner of rectangle. - * @param x2 - X position of bottom right corner of rectangle. - * @param y2 - Y position of bottom right corner of rectangle. - * @param topColor - Color at top of gradient. - * @param bottomColor - Color at bottom of gradient. - */ - public static void fillGradient(Matrix4f matrix4f, int x1, int y1, int x2, int y2, int topColor, int bottomColor) { - float topAlpha = (float) (topColor >> 24 & 255) / 255.0F; - float topRed = (float) (topColor >> 16 & 255) / 255.0F; - float topGreen = (float) (topColor >> 8 & 255) / 255.0F; - float topBlue = (float) (topColor & 255) / 255.0F; - float bottomAlpha = (float) (bottomColor >> 24 & 255) / 255.0F; - float bottomRed = (float) (bottomColor >> 16 & 255) / 255.0F; - float bottomGreen = (float) (bottomColor >> 8 & 255) / 255.0F; - float bottomBlue = (float) (bottomColor & 255) / 255.0F; - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder bufferBuilder = tessellator.getBuffer(); - RenderSystem.enableBlend(); - RenderSystem.defaultBlendFunc(); - bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR); - bufferBuilder.vertex(matrix4f, x1, y2, 0).color(topRed, topGreen, topBlue, topAlpha).next(); - bufferBuilder.vertex(matrix4f, x2, y2, 0).color(topRed, topGreen, topBlue, topAlpha).next(); - bufferBuilder.vertex(matrix4f, x2, y1, 0).color(bottomRed, bottomGreen, bottomBlue, bottomAlpha).next(); - bufferBuilder.vertex(matrix4f, x1, y1, 0).color(bottomRed, bottomGreen, bottomBlue, bottomAlpha).next(); - tessellator.draw(); - RenderSystem.disableBlend(); - } - - /** - * Draws an outlined box on the screen. - * - * @param x1 The x position of the top left corner of the box - * @param y1 The y position of the top left corner of the box - * @param x2 The x position of the bottom right corner of the box - * @param y2 The y position of the bottom right corner of the box - * @param color The color to draw the box with - */ - public static void drawOutlinedBox(DrawContext drawContext, int x1, int y1, int x2, int y2, int color) { - drawContext.fill(x1, y1, x2, y1 + 1, color); - drawContext.fill(x1, y2 - 1, x2, y2, color); - drawContext.fill(x1, y1 + 1, x1 + 1, y2 - 1, color); - drawContext.fill(x2 - 1, y1 + 1, x2, y2 - 1, color); - } - - /** - * This method assumes that the x, y coords are the origin of a widget. - * @param x X position of widget - * @param y Y position of widget - * @param scale Scale the matrices - */ - public static void scaleAndPosition(MatrixStack matrices, float x, float y, float scale) { - matrices.push(); // Save the current transformation state - - // Translate the origin back to the desired position - matrices.translate(x, y, 0); - - // Scale the matrix - matrices.scale(scale, scale, 1.0F); - - matrices.translate(-x,-y,0); - } - /** - * This method scales the matrices by the centre of the widget - * @param x X position of widget - * @param y Y position of widget - * @param height height of widget - * @param width width of widget - * @param scale Scale the matrices - */ - public static void scaleAndPosition(MatrixStack matrices, float x, float y,float width, float height, float scale) { - matrices.push(); // Save the current transformation state - - // Translate the origin back to the desired position - matrices.translate(x + width / 2.0f, y + height / 2.0f, 0); - - // Scale the matrix - matrices.scale(scale, scale, 1.0F); - - matrices.translate(-(x + width / 2.0f), -(y + height / 2.0f), 0); - } - - public static void stopScaling(MatrixStack matrices){ - matrices.pop(); // Restore the previous transformation state - } - -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/helpers/TextureHelper.java b/src/main/java/com/tanishisherewith/dynamichud/helpers/TextureHelper.java deleted file mode 100644 index 0fade01..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/helpers/TextureHelper.java +++ /dev/null @@ -1,233 +0,0 @@ -package com.tanishisherewith.dynamichud.helpers; - -import com.mojang.blaze3d.systems.RenderSystem; -import com.tanishisherewith.dynamichud.util.CustomItemRenderer; -import com.tanishisherewith.dynamichud.util.CustomTextRenderer; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.render.VertexConsumerProvider; -import net.minecraft.item.ItemStack; -import net.minecraft.util.Identifier; - -/** - * This class provides helper methods for drawing textures on the screen. - */ -public class TextureHelper extends DrawContext { - public static CustomTextRenderer customTextRenderer; - public static CustomItemRenderer customItemRenderer; - - - public TextureHelper(MinecraftClient client, VertexConsumerProvider.Immediate vertexConsumers) { - super(client, vertexConsumers); - } - - /** - * Draws an item texture on the screen. - * - * @param itemStack The item stack to render the texture for - * @param x The x position to draw the texture at - * @param y The y position to draw the texture at - */ - public static void drawItemTexture(DrawContext drawContext, - ItemStack itemStack, - int x, - int y) { - drawContext.drawItem(itemStack, x, y); - } - - /** - * Draws the texture of the item in the player's main hand on the screen. - * - * @param client The Minecraft client instance - * @param x The x position to draw the texture at - * @param y The y position to draw the texture at - */ - public static void drawMainHandTexture(DrawContext drawContext, - MinecraftClient client, - int x, - int y) { - assert client.player != null; - ItemStack mainHandItem = client.player.getMainHandStack(); - drawItemTexture(drawContext, mainHandItem, x, y); - } - - /** - * Draws a textured rectangle on the screen. - * - * @param x The x position of the top left corner of the rectangle - * @param y The y position of the top left corner of the rectangle - * @param u The x position of the texture within the texture image - * @param v The y position of the texture within the texture image - * @param width The width of the rectangle - * @param height The height of the rectangle - * @param textureWidth The width of the texture image - * @param textureHeight The height of the texture image - */ - public static void drawTexture(DrawContext drawContext, Identifier texture, int x, int y, int u, int v, int width, int height, int textureWidth, int textureHeight) { - drawContext.drawTexture(texture, x, y, u, v, width, height, textureWidth, textureHeight); - } - - /** - * Draws a textured rectangle on the screen with a specified color. - * - * @param x The x position of the top left corner of the rectangle - * @param y The y position of the top left corner of the rectangle - * @param u The x position of the texture within the texture image - * @param v The y position of the texture within the texture image - * @param width The width of the rectangle - * @param height The height of the rectangle - * @param color The color to draw the rectangle with - */ - public static void drawTexturedRect(DrawContext drawContext, Identifier texture, int x, int y, int u, int v, int width, int height, int color) { - RenderSystem.setShaderColor((color >> 16 & 255) / 255.0F, - (color >> 8 & 255) / 255.0F, - (color & 255) / 255.0F, - (color >> 24 & 255) / 255.0F); - drawContext.drawTexture(texture, x, y, u, v, width, height); - } - - /** - * Draws an item texture on the screen with text at a specified position relative to it. - * - * @param textRenderer The text renderer instance used for rendering the text - * @param itemStack The item stack to render the texture for - * @param x The x position to draw the texture at - * @param y The y position to draw the texture at - * @param text The text to draw relative to the texture - * @param color The color to draw the text with - * @param position The position of the text relative to the texture (ABOVE, BELOW, LEFT, or RIGHT) - * @param scale The scale factor to apply to the text (1.0 is normal size) - */ - public static void drawItemTextureWithText(DrawContext drawContext, - TextRenderer textRenderer, - ItemStack itemStack, - int x, - int y, - String text, - int color, - Position position, - float scale, - boolean textBackground - ) { - // Calculate the position of the text based on its size and the specified position - int textWidth = (int) (textRenderer.getWidth(text) * scale); - int textHeight = (int) (textRenderer.fontHeight * scale); - int textX = 0; - int textY = 0; - switch (position) { - case ABOVE -> { - textX = x + (16 - textWidth) / 2; - textY = y - textHeight; - } - case BELOW -> { - textX = x + (17 - textWidth) / 2; - textY = y + 16; - } - case LEFT -> { - textX = x - textWidth - 2; - textY = y + (16 - textHeight) / 2; - } - case RIGHT -> { - textX = x + 18; - textY = y + (16 - textHeight) / 2; - } - } - - // Draw semi-opaque black rectangle - if (text != null) { - if (textBackground && !text.trim().isEmpty()) { - int backgroundColor = 0x40000000; // ARGB format: 50% opaque black - drawContext.fill(textX - 1, textY - 1, textX + textWidth + 1, textY + textHeight + 1, backgroundColor); - } - // Draw the scaled text at the calculated position - drawContext.getMatrices().push(); - drawContext.getMatrices().scale(scale, scale, 1.0f); - float scaledX = textX / scale; - float scaledY = textY / scale; - drawContext.drawText(textRenderer, text, (int) scaledX, (int) scaledY, color, false); - drawContext.getMatrices().pop(); - } - // Draw the item texture - drawContext.drawItem(itemStack, x, y); - } - - /** - * Draws an item texture on the screen with text at a specified position relative to it. - * - * @param itemScale The scale for the item to be rendered at - * @param textRenderer The text renderer instance used for rendering the text - * @param itemStack The item stack to render the texture for - * @param x The x position to draw the texture at - * @param y The y position to draw the texture at - * @param text The text to draw relative to the texture - * @param color The color to draw the text with - * @param position The position of the text relative to the texture (ABOVE, BELOW, LEFT, or RIGHT) - * @param textScale The scale factor to apply to the text (1.0 is normal size) - */ - public static void drawItemTextureWithTextAndScale(DrawContext drawContext, - float itemScale, - TextRenderer textRenderer, - ItemStack itemStack, - int x, - int y, - String text, - int color, - Position position, - float textScale, - boolean textBackground) { - if (text != null && !text.trim().isEmpty()) { - // Calculate the position of the text based on its size and the specified position - int textWidth = (int) (textRenderer.getWidth(text) * textScale); - int textHeight = (int) (textRenderer.fontHeight * textScale); - int textX = switch (position) { - case ABOVE, BELOW -> x + (int) ((16 * itemScale - textWidth) / 2); - case LEFT -> x - textWidth - 2; - case RIGHT -> x + (int) (16 * itemScale + 2); - }; - int textY = switch (position) { - case ABOVE -> y - textHeight - 2; - case BELOW -> y + (int) (16 * itemScale + 2); - case LEFT, RIGHT -> y + (int) ((16 * itemScale - textHeight) / 2); - }; - - // Draw semi-opaque black rectangle - if (textBackground) { - int backgroundColor = 0x40000000; // ARGB format: 50% opaque black - drawContext.fill(textX, textY - 1, textX + textWidth + 1, textY + textHeight + 1, backgroundColor); - } - - // Draw the scaled text at the calculated position - drawContext.getMatrices().push(); - drawContext.getMatrices().scale(textScale, textScale, 11.0f); - float scaledX = textX / textScale; - float scaledY = textY / textScale; - drawContext.drawText(textRenderer, text, (int) scaledX + 1, (int) scaledY +1, color, false); - drawContext.getMatrices().pop(); - } - customItemRenderer = new CustomItemRenderer(itemStack, itemScale); - customItemRenderer.draw(drawContext, x, y, color); - } - - public enum Position { - ABOVE("Above"), - RIGHT("Right"), - BELOW("Below"), - LEFT("Left"); - private final String name; - - Position(String name) { - this.name = name; - } - - public static Position getByUpperCaseName(String name) { - if (name == null || name.isEmpty()) { - return null; - } - - return Position.valueOf(name.toUpperCase()); - } - } - - -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/huds/AbstractMoveableScreen.java b/src/main/java/com/tanishisherewith/dynamichud/huds/AbstractMoveableScreen.java deleted file mode 100644 index 5d33fb5..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/huds/AbstractMoveableScreen.java +++ /dev/null @@ -1,214 +0,0 @@ -package com.tanishisherewith.dynamichud.huds; - -import com.tanishisherewith.dynamichud.handlers.DefaultDragHandler; -import com.tanishisherewith.dynamichud.handlers.DefaultMouseHandler; -import com.tanishisherewith.dynamichud.handlers.DragHandler; -import com.tanishisherewith.dynamichud.handlers.MouseHandler; -import com.tanishisherewith.dynamichud.util.DynamicUtil; -import com.tanishisherewith.dynamichud.util.colorpicker.ColorGradientPicker; -import com.tanishisherewith.dynamichud.util.contextmenu.ContextMenu; -import com.tanishisherewith.dynamichud.widget.Widget; -import com.tanishisherewith.dynamichud.widget.slider.SliderWidget; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.text.Text; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public abstract class AbstractMoveableScreen extends Screen { - protected final DynamicUtil dynamicutil; // The DynamicUtil instance used by this screen - protected MinecraftClient mc = MinecraftClient.getInstance(); - protected Widget selectedWidget = null; // The currently selected widget - protected int dragStartX = 0, dragStartY = 0; // The starting position of a drag operation - protected List contextMenus = new ArrayList<>(); // The context menu that is currently displayed - protected ColorGradientPicker colorPicker = null; // The color picker that is currently displayed - protected Widget sliderWigdet = null; // The widget that is currently being edited by the slider - protected List Sliders = new ArrayList<>(); // The List of sliders - protected MouseHandler mouseHandler; - protected DragHandler dragHandler; - protected int gridSize = 3; // The size of each grid cell in pixels - protected boolean ShouldPause = false; // To pause if the screen is opened or not - protected boolean ShouldBeAffectedByResize = false; // If the stuff drawn on screen to be affected by screen resize or not - protected int widgetX; - protected int widgetY; - - - /** - * Constructs a AbstractMoveableScreen object. - * - * @param dynamicutil The DynamicUtil instance used by this screen - */ - public AbstractMoveableScreen(Text title, DynamicUtil dynamicutil) { - super(title); - this.dynamicutil = dynamicutil; - updateMouseHandler(this.colorPicker, contextMenus, Sliders); - dragHandler = new DefaultDragHandler(); - } - - /** - * Handles mouse dragging on this screen. - * - * @param mouseX - Current X position of mouse cursor. - * @param mouseY - Current Y position of mouse cursor. - * @param button - Mouse button being dragged. - * @param deltaX - Change in X position since last call to this method. - * @param deltaY - Change in Y position since last call to this method. - * @return true if mouse dragging was handled by this screen. - */ - @Override - public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { - if (mouseHandler.mouseDragged(mouseX, mouseY, button, deltaX, deltaY)) { - return true; - } - if (selectedWidget != null && selectedWidget.isDraggable) { - // Update the position of the widget while dragging - int newX = (int) (mouseX - dragStartX); - int newY = (int) (mouseY - dragStartY); - - // Snap the widget to the grid - newX = (newX / gridSize) * gridSize; - newY = (newY / gridSize) * gridSize; - - selectedWidget.setX(newX); - selectedWidget.setY(newY); - return true; - } - return false; - } - - /** - * Handles mouse clicks on this screen. - * - * @param mouseX - X position of mouse cursor. - * @param mouseY - Y position of mouse cursor. - * @param button - Mouse button that was clicked. - * @return true if mouse click was handled by this screen, false otherwise. - */ - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (mouseHandler.mouseClicked(mouseX, mouseY, button)) { - return true; - } - - for (Widget widget : dynamicutil.getWidgetManager().getWidgets()) { - if (widget.getWidgetBox().contains(widget, mouseX, mouseY, Widget.getScale())) { - // Start dragging the widget - colorPicker = null; - contextMenus.clear(); - Sliders.clear(); - if (button == 1) { // Right-click - handleRightClickOnWidget(widget); - } else if (button == 0) { - widget.enabled = !widget.enabled; - } - if (dragHandler.startDragging(widget, mouseX, mouseY) && button == 0 && widget.isDraggable) { - selectedWidget = widget; - for (ContextMenu contextmenu : contextMenus) { - contextmenu.updatePosition(); - } - for (SliderWidget sliderWidget : Sliders) { - sliderWidget.updatePosition(); - } - return true; - } - } - } - return false; - } - - /** - * Handles mouse release events on this screen. - * - * @param mouseX The current x position of the mouse cursor - * @param mouseY The current y position of the mouse cursor - * @param button The mouse button that was released - * @return True if the mouse release event was handled by this screen, false otherwise - */ - @Override - public boolean mouseReleased(double mouseX, double mouseY, int button) { - // Stop dragging or scaling the widget - if (mouseHandler.mouseReleased(mouseX, mouseY, button)) { - return true; - } - if (selectedWidget != null) { - selectedWidget = null; - return true; - } - return contextMenus != null; - } - - /** - * Renders this screen and its widgets on the screen. - * - * @param drawContext The matrix stack used for rendering - * @param mouseX The current x position of the mouse cursor - * @param mouseY The current y position of the mouse cursor - * @param delta The time elapsed since the last frame in seconds - */ - @Override - public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) { - super.render(drawContext, mouseX, mouseY, delta); - - // Draw each widget - for (Widget widget : dynamicutil.getWidgetManager().getWidgets()) { - widget.render(drawContext); - } - - // Draw the slider and other stuff - for (SliderWidget sliderWidget : Sliders) { - sliderWidget.render(drawContext); - } - for (ContextMenu contextMenu : contextMenus) { - contextMenu.render(drawContext); - } - - if (colorPicker != null) { - colorPicker.render(drawContext); - } - - if (selectedWidget != null) { - widgetX = selectedWidget.getX(); - widgetY = selectedWidget.getY(); - } - - updateMouseHandler(colorPicker, contextMenus, Sliders); - } - - private void updateMouseHandler(ColorGradientPicker colorPicker, List contextMenus, List Sliders) { - this.colorPicker = colorPicker; - this.contextMenus = contextMenus; - this.Sliders = Sliders; - mouseHandler = new DefaultMouseHandler(colorPicker, contextMenus, Sliders); - } - - public void setGridSize(int gridSize) { - this.gridSize = gridSize; - } - - public void setShouldPause(boolean shouldpause) { - this.ShouldPause = shouldpause; - } - - public void setShouldBeAffectedByResize(boolean shouldBeAffectedByResize) { - this.ShouldBeAffectedByResize = shouldBeAffectedByResize; - } - - @Override - public void resize(MinecraftClient client, int width, int height) { - if (ShouldBeAffectedByResize) - super.resize(client, width, height); - } - - @Override - public boolean shouldPause() { - return ShouldPause; - } - - protected abstract boolean handleRightClickOnWidget(Widget widget); - - protected abstract void menu(Widget widget, int x, int y); -} - diff --git a/src/main/java/com/tanishisherewith/dynamichud/huds/MoveableScreen.java b/src/main/java/com/tanishisherewith/dynamichud/huds/MoveableScreen.java deleted file mode 100644 index fd5085d..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/huds/MoveableScreen.java +++ /dev/null @@ -1,175 +0,0 @@ -package com.tanishisherewith.dynamichud.huds; - -import com.tanishisherewith.dynamichud.helpers.ColorHelper; -import com.tanishisherewith.dynamichud.helpers.TextureHelper; -import com.tanishisherewith.dynamichud.util.DynamicUtil; -import com.tanishisherewith.dynamichud.util.colorpicker.ColorGradientPicker; -import com.tanishisherewith.dynamichud.util.contextmenu.ContextMenu; -import com.tanishisherewith.dynamichud.widget.Widget; -import com.tanishisherewith.dynamichud.widget.armor.ArmorWidget; -import com.tanishisherewith.dynamichud.widget.item.ItemWidget; -import com.tanishisherewith.dynamichud.widget.slider.SliderWidget; -import com.tanishisherewith.dynamichud.widget.slider.SliderWidgetBuilder; -import com.tanishisherewith.dynamichud.widget.text.TextWidget; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.text.Text; - -import java.awt.*; - -public class MoveableScreen extends AbstractMoveableScreen { - SliderWidget SliderWidget; - /** - * Constructs a AbstractMoveableScreen object. - * - * @param title - * @param dynamicutil The DynamicUtil instance used by this screen - */ - public MoveableScreen(Text title, DynamicUtil dynamicutil) { - super(title, dynamicutil); - setGridSize(1); - setShouldPause(false); - setShouldBeAffectedByResize(false); - } - - @Override - public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) { - super.render(drawContext, mouseX, mouseY, delta); - assert client != null; - /*SliderWidget = new SliderWidgetBuilder(client) - .setX(client.getWindow().getScaledWidth() - 120) - .setY(client.getWindow().getScaledHeight() - 20) - .setWidth(105) - .setHeight(10) - .setLabel("Scale") - .setValue(Widget.getScale()) - .getValue(Widget::setScale) - .setMinValue(0.5f) - .setMaxValue(4f) - .setSelectedWidget(null) - .build();*/ - drawContext.drawTextWithShadow(textRenderer, "Editors Screen", (int) (MinecraftClient.getInstance().getWindow().getScaledWidth() / 2f - textRenderer.getWidth("Editor Screen") / 2f), 5, ColorHelper.ColorToInt(Color.WHITE)); - // SliderWidget.render(drawContext); - } - - @Override - protected boolean handleRightClickOnWidget(Widget widget) { - selectedWidget = widget; - sliderWigdet = widget; - // Show context menu - menu(widget, widgetX, widgetY); - return true; - } - - @Override - protected void menu(Widget widget, int x, int y) { - contextMenus.clear(); - contextMenus.add(new ContextMenu(mc, x, y + widget.getHeight() + 5, selectedWidget, this)); - if (widget instanceof ArmorWidget armorWidget) { - ArmorWidgetMenu(armorWidget, x, y); - } - if (widget instanceof ItemWidget itemWidget) { - ItemWidgetMenu(itemWidget, x, y); - } - if (widget instanceof TextWidget textWidget) { - TextWidgetMenu(textWidget, x, y); - } - } - - protected void ItemWidgetMenu(ItemWidget itemWidget, int x, int y) { - Sliders.clear(); - colorPicker = null; - contextMenus.clear(); - } - - protected void ArmorWidgetMenu(ArmorWidget armorWidget, int x, int y) { - Sliders.clear(); - contextMenus.get(0).setHeightFromWidget(14); - contextMenus.get(0).setPadding(5); - contextMenus.get(0).addEnumCycleOption("", TextureHelper.Position.values(), () -> armorWidget.currentTextPosition[0], newPosition -> { - armorWidget.currentTextPosition[0] = newPosition; - }); - } - - protected void TextWidgetMenu(TextWidget textWidget, int x, int y) { - contextMenus.get(0).setHeightFromWidget(2); - contextMenus.get(0).setPadding(5); - - contextMenus.get(0).addOption("Shadow", () -> { - textWidget.setShadow(!textWidget.hasShadow()); - }); - contextMenus.get(0).addOption("Rainbow", () -> { - textWidget.setRainbow(!textWidget.hasRainbow()); - }); - if (!textWidget.getText().isEmpty()) { - contextMenus.get(0).addOption("TextColor", () -> { - textWidget.toggleTextColorOption(); - - if (textWidget.isTextcolorOptionEnabled()) - colorPicker = new ColorGradientPicker(mc, widgetX +110, widgetY + textWidget.getHeight() + 5, textWidget.getTextcolor(), textWidget::setTextColor, 50, 100, selectedWidget); - else - colorPicker = null; - }); - } - if (!textWidget.getDataText().trim().isEmpty()) { - contextMenus.get(0).addOption("DataColor", () -> { - textWidget.toggleDataColorOption(); - - if (textWidget.isDatacolorOptionEnabled()) - colorPicker = new ColorGradientPicker(mc, widgetX + 110, widgetY + textWidget.getHeight() + 5, textWidget.getDatacolor(), textWidget::setDataColor, 50, 100, selectedWidget); - else - colorPicker = null; - }); - } - /* contextMenus.get(0).addOption("SubMenu: ",()-> - { - if (contextMenus.size()>1) contextMenus.remove(1); - contextMenus.add(new ContextMenu(mc, contextMenus.get(0).getX() + 40, contextMenus.get(0).getOptionY(6), selectedWidget,this)); - contextMenus.get(1).addOption("Option: 1",()->System.out.println("Pressed 1 ")); - contextMenus.get(1).addOption("Option: 2",()->System.out.println("Pressed 2 ")); - contextMenus.get(1).addOption("Option: 3",()->System.out.println("Pressed 3 ")); - }); - contextMenus.get(0).addDataTextOption(("Enter data"), data -> { - System.out.println("Entered data: " + data); - },widgetX,widgetY); - contextMenus.get(0).addDoubleTextOption(("Enter double"), data -> { - System.out.println("Entered data: " + data); - },widgetX,widgetY);*/ - - SliderWidget sliderWidget =new SliderWidgetBuilder(client) - .setX(x) - .setY(y) - .setWidth(105) - .setHeight(contextMenus.get(0).getHeight() + 7) - .setLabel("Rainbow Speed") - .setValue(textWidget.getRainbowSpeed()) - .setMinValue(5f) - .setMaxValue(25.0f) - .getValue(TextWidget::setRainbowSpeed) - .setSelectedWidget(selectedWidget) - .build(); - Sliders.add(sliderWidget); - } - - - @Override - public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { - // if(SliderWidget.mouseDragged(mouseX,mouseY,button,deltaX,deltaY)) - //{ - // return true; - //} - return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY); - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - // if(SliderWidget.mouseClicked(mouseX,mouseY,button)) - // { - // SliderWidget.updatePosition(); - // return true; - // } - return super.mouseClicked(mouseX, mouseY, button); - } -} - - diff --git a/src/main/java/com/tanishisherewith/dynamichud/interfaces/IWigdets.java b/src/main/java/com/tanishisherewith/dynamichud/interfaces/IWigdets.java deleted file mode 100644 index d27675e..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/interfaces/IWigdets.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.tanishisherewith.dynamichud.interfaces; - -import com.tanishisherewith.dynamichud.util.DynamicUtil; - -public interface IWigdets { - - void addWigdets(DynamicUtil dynamicUtil); - - void addMainMenuWigdets(DynamicUtil dynamicUtil); - - void loadWigdets(DynamicUtil dynamicUtil); -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/interfaces/TextGenerator.java b/src/main/java/com/tanishisherewith/dynamichud/interfaces/TextGenerator.java deleted file mode 100644 index 009f0dc..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/interfaces/TextGenerator.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.tanishisherewith.dynamichud.interfaces; - -@FunctionalInterface -public interface TextGenerator { - String generateText(); -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/interfaces/WidgetLoading.java b/src/main/java/com/tanishisherewith/dynamichud/interfaces/WidgetLoading.java deleted file mode 100644 index fe7a7be..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/interfaces/WidgetLoading.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.tanishisherewith.dynamichud.interfaces; - -import com.tanishisherewith.dynamichud.helpers.TextureHelper; -import com.tanishisherewith.dynamichud.widget.Widget; -import com.tanishisherewith.dynamichud.widget.armor.ArmorWidget; -import com.tanishisherewith.dynamichud.widget.item.ItemWidget; -import com.tanishisherewith.dynamichud.widget.text.TextWidget; -import net.minecraft.client.MinecraftClient; -import net.minecraft.entity.EquipmentSlot; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; - -import java.awt.*; - -public interface WidgetLoading { - default Widget loadWidgetsFromTag(String className, NbtCompound widgetTag) { - if (className.equals(TextWidget.class.getName())) { - TextWidget widget = new TextWidget(MinecraftClient.getInstance(), "", () -> "", 0, 0, false, false, -1, -1, true); - widget.readFromTag(widgetTag); - return widget; - } - if (className.equals(ArmorWidget.class.getName())) { - ArmorWidget widget = new ArmorWidget(MinecraftClient.getInstance(), EquipmentSlot.CHEST, 0, 0, false, TextureHelper.Position.ABOVE, () -> "", () -> Color.RED, true, ""); - widget.readFromTag(widgetTag); - return widget; - } - if (className.equals(ItemWidget.class.getName())) { - ItemWidget widget = new ItemWidget(MinecraftClient.getInstance(), () -> ItemStack.EMPTY, 0, 0, true, TextureHelper.Position.ABOVE, () -> "", () -> Color.WHITE, true, ""); - widget.readFromTag(widgetTag); - return widget; - } - return null; - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java index 5643c84..b3bb2cd 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java @@ -32,7 +32,7 @@ public class DynamicHUD implements ClientModInitializer { * Allows saving widgets across different mods with same save file name. */ public static final HashMap> FILE_MAP = new HashMap<>(); - private static final Logger logger = LoggerFactory.getLogger("DynamicHud"); + public static final Logger logger = LoggerFactory.getLogger("DynamicHud"); private static final List widgetRenderers = new ArrayList<>(); public static MinecraftClient MC = MinecraftClient.getInstance(); public static String MOD_ID = "dynamichud"; @@ -130,9 +130,7 @@ public void onInitializeClient() { } //Register events for rendering, saving, loading, and opening the hudEditor - ClientTickEvents.START_CLIENT_TICK.register((client) -> { - openDynamicScreen(binding, screen); - }); + ClientTickEvents.START_CLIENT_TICK.register((client) -> openDynamicScreen(binding, screen)); /* === Saving === */ diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/config/GlobalConfig.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/config/GlobalConfig.java index 64a97f7..4ce8478 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/config/GlobalConfig.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/config/GlobalConfig.java @@ -11,7 +11,7 @@ import net.minecraft.util.Identifier; public class GlobalConfig { - public static ConfigClassHandler HANDLER = ConfigClassHandler.createBuilder(GlobalConfig.class) + public static final ConfigClassHandler HANDLER = ConfigClassHandler.createBuilder(GlobalConfig.class) .id(new Identifier("dynamichud", "dynamichud_config")) .serializer(config -> GsonConfigSerializerBuilder.create(config) .setPath(FabricLoader.getInstance().getConfigDir().resolve("dynamichud.json5")) diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/DrawHelper.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/DrawHelper.java index ea1d60f..ab6b5b0 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/DrawHelper.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/DrawHelper.java @@ -2,7 +2,6 @@ import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; -import com.tanishisherewith.dynamichud.helpers.ColorHelper; import com.tanishisherewith.dynamichud.newTrial.DynamicHUD; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.render.*; @@ -659,7 +658,7 @@ public static void drawRoundedRectangle(Matrix4f matrix4f, float x, float y, boo } /** - * Draws a outline rounded gradient rectangle + * Draws an outline rounded gradient rectangle * * @param matrix4f Matrix4f object to draw the rounded gradient rectangle * @param color1 is applied to the bottom-left vertex (x, y + height). diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/TextureHelper.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/TextureHelper.java deleted file mode 100644 index f73aaae..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/TextureHelper.java +++ /dev/null @@ -1,233 +0,0 @@ -package com.tanishisherewith.dynamichud.newTrial.helpers; - -import com.mojang.blaze3d.systems.RenderSystem; -import com.tanishisherewith.dynamichud.util.CustomItemRenderer; -import com.tanishisherewith.dynamichud.util.CustomTextRenderer; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.render.VertexConsumerProvider; -import net.minecraft.item.ItemStack; -import net.minecraft.util.Identifier; - -/** - * This class provides helper methods for drawing textures on the screen. - */ -public class TextureHelper extends DrawContext { - public static CustomTextRenderer customTextRenderer; - public static CustomItemRenderer customItemRenderer; - - - public TextureHelper(MinecraftClient client, VertexConsumerProvider.Immediate vertexConsumers) { - super(client, vertexConsumers); - } - - /** - * Draws an item texture on the screen. - * - * @param itemStack The item stack to render the texture for - * @param x The x position to draw the texture at - * @param y The y position to draw the texture at - */ - public static void drawItemTexture(DrawContext drawContext, - ItemStack itemStack, - int x, - int y) { - drawContext.drawItem(itemStack, x, y); - } - - /** - * Draws the texture of the item in the player's main hand on the screen. - * - * @param client The Minecraft client instance - * @param x The x position to draw the texture at - * @param y The y position to draw the texture at - */ - public static void drawMainHandTexture(DrawContext drawContext, - MinecraftClient client, - int x, - int y) { - assert client.player != null; - ItemStack mainHandItem = client.player.getMainHandStack(); - drawItemTexture(drawContext, mainHandItem, x, y); - } - - /** - * Draws a textured rectangle on the screen. - * - * @param x The x position of the top left corner of the rectangle - * @param y The y position of the top left corner of the rectangle - * @param u The x position of the texture within the texture image - * @param v The y position of the texture within the texture image - * @param width The width of the rectangle - * @param height The height of the rectangle - * @param textureWidth The width of the texture image - * @param textureHeight The height of the texture image - */ - public static void drawTexture(DrawContext drawContext, Identifier texture, int x, int y, int u, int v, int width, int height, int textureWidth, int textureHeight) { - drawContext.drawTexture(texture, x, y, u, v, width, height, textureWidth, textureHeight); - } - - /** - * Draws a textured rectangle on the screen with a specified color. - * - * @param x The x position of the top left corner of the rectangle - * @param y The y position of the top left corner of the rectangle - * @param u The x position of the texture within the texture image - * @param v The y position of the texture within the texture image - * @param width The width of the rectangle - * @param height The height of the rectangle - * @param color The color to draw the rectangle with - */ - public static void drawTexturedRect(DrawContext drawContext, Identifier texture, int x, int y, int u, int v, int width, int height, int color) { - RenderSystem.setShaderColor((color >> 16 & 255) / 255.0F, - (color >> 8 & 255) / 255.0F, - (color & 255) / 255.0F, - (color >> 24 & 255) / 255.0F); - drawContext.drawTexture(texture, x, y, u, v, width, height); - } - - /** - * Draws an item texture on the screen with text at a specified position relative to it. - * - * @param textRenderer The text renderer instance used for rendering the text - * @param itemStack The item stack to render the texture for - * @param x The x position to draw the texture at - * @param y The y position to draw the texture at - * @param text The text to draw relative to the texture - * @param color The color to draw the text with - * @param position The position of the text relative to the texture (ABOVE, BELOW, LEFT, or RIGHT) - * @param scale The scale factor to apply to the text (1.0 is normal size) - */ - public static void drawItemTextureWithText(DrawContext drawContext, - TextRenderer textRenderer, - ItemStack itemStack, - int x, - int y, - String text, - int color, - Position position, - float scale, - boolean textBackground - ) { - // Calculate the position of the text based on its size and the specified position - int textWidth = (int) (textRenderer.getWidth(text) * scale); - int textHeight = (int) (textRenderer.fontHeight * scale); - int textX = 0; - int textY = 0; - switch (position) { - case ABOVE -> { - textX = x + (16 - textWidth) / 2; - textY = y - textHeight; - } - case BELOW -> { - textX = x + (17 - textWidth) / 2; - textY = y + 16; - } - case LEFT -> { - textX = x - textWidth - 2; - textY = y + (16 - textHeight) / 2; - } - case RIGHT -> { - textX = x + 18; - textY = y + (16 - textHeight) / 2; - } - } - - // Draw semi-opaque black rectangle - if (text != null) { - if (textBackground && !text.trim().isEmpty()) { - int backgroundColor = 0x40000000; // ARGB format: 50% opaque black - drawContext.fill(textX - 1, textY - 1, textX + textWidth + 1, textY + textHeight + 1, backgroundColor); - } - // Draw the scaled text at the calculated position - drawContext.getMatrices().push(); - drawContext.getMatrices().scale(scale, scale, 1.0f); - float scaledX = textX / scale; - float scaledY = textY / scale; - drawContext.drawText(textRenderer, text, (int) scaledX, (int) scaledY, color, false); - drawContext.getMatrices().pop(); - } - // Draw the item texture - drawContext.drawItem(itemStack, x, y); - } - - /** - * Draws an item texture on the screen with text at a specified position relative to it. - * - * @param itemScale The scale for the item to be rendered at - * @param textRenderer The text renderer instance used for rendering the text - * @param itemStack The item stack to render the texture for - * @param x The x position to draw the texture at - * @param y The y position to draw the texture at - * @param text The text to draw relative to the texture - * @param color The color to draw the text with - * @param position The position of the text relative to the texture (ABOVE, BELOW, LEFT, or RIGHT) - * @param textScale The scale factor to apply to the text (1.0 is normal size) - */ - public static void drawItemTextureWithTextAndScale(DrawContext drawContext, - float itemScale, - TextRenderer textRenderer, - ItemStack itemStack, - int x, - int y, - String text, - int color, - Position position, - float textScale, - boolean textBackground) { - if (text != null && !text.trim().isEmpty()) { - // Calculate the position of the text based on its size and the specified position - int textWidth = (int) (textRenderer.getWidth(text) * textScale); - int textHeight = (int) (textRenderer.fontHeight * textScale); - int textX = switch (position) { - case ABOVE, BELOW -> x + (int) ((16 * itemScale - textWidth) / 2); - case LEFT -> x - textWidth - 2; - case RIGHT -> x + (int) (16 * itemScale + 2); - }; - int textY = switch (position) { - case ABOVE -> y - textHeight - 2; - case BELOW -> y + (int) (16 * itemScale + 2); - case LEFT, RIGHT -> y + (int) ((16 * itemScale - textHeight) / 2); - }; - - // Draw semi-opaque black rectangle - if (textBackground) { - int backgroundColor = 0x40000000; // ARGB format: 50% opaque black - drawContext.fill(textX, textY - 1, textX + textWidth + 1, textY + textHeight + 1, backgroundColor); - } - - // Draw the scaled text at the calculated position - drawContext.getMatrices().push(); - drawContext.getMatrices().scale(textScale, textScale, 11.0f); - float scaledX = textX / textScale; - float scaledY = textY / textScale; - drawContext.drawText(textRenderer, text, (int) scaledX + 1, (int) scaledY + 1, color, false); - drawContext.getMatrices().pop(); - } - customItemRenderer = new CustomItemRenderer(itemStack, itemScale); - customItemRenderer.draw(drawContext, x, y, color); - } - - public enum Position { - ABOVE("Above"), - RIGHT("Right"), - BELOW("Below"), - LEFT("Left"); - private final String name; - - Position(String name) { - this.name = name; - } - - public static Position getByUpperCaseName(String name) { - if (name == null || name.isEmpty()) { - return null; - } - - return Position.valueOf(name.toUpperCase()); - } - } - - -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/screens/AbstractMoveableScreen.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/screens/AbstractMoveableScreen.java index ce53614..313823a 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/screens/AbstractMoveableScreen.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/screens/AbstractMoveableScreen.java @@ -70,6 +70,7 @@ public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmou */ @Override public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) { + assert this.client != null; if (this.client.world == null) { this.renderBackgroundTexture(drawContext); } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/ContextMenu.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/ContextMenu.java index 1911b5d..2750069 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/ContextMenu.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/ContextMenu.java @@ -1,6 +1,6 @@ package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu; -import com.tanishisherewith.dynamichud.helpers.DrawHelper; +import com.tanishisherewith.dynamichud.newTrial.helpers.DrawHelper; import net.minecraft.client.gui.DrawContext; import java.awt.*; diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/Option.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/Option.java index 8244df8..4f2e080 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/Option.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/Option.java @@ -1,6 +1,6 @@ package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu; -import com.tanishisherewith.dynamichud.widget.Widget; +import com.tanishisherewith.dynamichud.newTrial.widget.Widget; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorGradientPicker.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorGradientPicker.java index 701e145..4d00518 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorGradientPicker.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorGradientPicker.java @@ -17,7 +17,7 @@ public class ColorGradientPicker { private final ColorPickerButton colorPickerButton; private final AlphaSlider alphaSlider; private final int boxSize; - MinecraftClient client = MinecraftClient.getInstance(); + final MinecraftClient client = MinecraftClient.getInstance(); private int x, y; private boolean display = false; diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/GradientBox.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/GradientBox.java index ce0f0f9..6f63968 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/GradientBox.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/GradientBox.java @@ -1,6 +1,6 @@ package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.coloroption; -import com.tanishisherewith.dynamichud.helpers.DrawHelper; +import com.tanishisherewith.dynamichud.newTrial.helpers.DrawHelper; import net.minecraft.client.gui.DrawContext; import java.awt.*; @@ -35,7 +35,7 @@ public void render(DrawContext drawContext, int x, int y) { float handleX = x + saturation * size - handleSize / 2.0f; float handleY = y + (1.0f - value) * size - handleSize / 2.0f; - DrawHelper.fillRoundedRect(drawContext, (int) handleX, (int) handleY, (int) (handleX + handleSize), (int) (handleY + handleSize), -1); + DrawHelper.drawFilledCircle(drawContext.getMatrices().peek().getPositionMatrix(), handleX, handleY, 1,-1); drawContext.getMatrices().pop(); } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/GradientSlider.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/GradientSlider.java index 60498df..c56cf16 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/GradientSlider.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/GradientSlider.java @@ -1,6 +1,6 @@ package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.coloroption; -import com.tanishisherewith.dynamichud.helpers.DrawHelper; +import com.tanishisherewith.dynamichud.newTrial.helpers.DrawHelper; import net.minecraft.client.gui.DrawContext; import java.awt.*; @@ -54,7 +54,7 @@ public void render(DrawContext drawContext, int x, int y) { float handleX = x + hue * width - handleWidth / 2.0f; float handleY = y - (handleHeight - height) / 2.0f; - DrawHelper.fillRoundedRect(drawContext, (int) handleX, (int) handleY, (int) (handleX + handleWidth), (int) (handleY + handleHeight), -1); + DrawHelper.drawRoundedRectangle(drawContext.getMatrices().peek().getPositionMatrix(), handleX, handleY, handleWidth, handleHeight, 2,-1); drawContext.getMatrices().pop(); } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetManager.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetManager.java index 6520b51..c4a685e 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetManager.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetManager.java @@ -1,6 +1,6 @@ package com.tanishisherewith.dynamichud.newTrial.widget; -import com.tanishisherewith.dynamichud.DynamicHUD; +import com.tanishisherewith.dynamichud.newTrial.DynamicHUD; import net.fabricmc.fabric.api.util.NbtType; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtIo; @@ -15,8 +15,8 @@ import java.nio.file.StandardCopyOption; import java.util.*; -import static com.tanishisherewith.dynamichud.DynamicHUD.printInfo; -import static com.tanishisherewith.dynamichud.DynamicHUD.printWarn; +import static com.tanishisherewith.dynamichud.newTrial.DynamicHUD.printInfo; +import static com.tanishisherewith.dynamichud.newTrial.DynamicHUD.printWarn; /** * Manages a collection of widgets, providing methods to add, remove, save, and load widgets. @@ -78,29 +78,6 @@ public static void addWidgets(Widget... widget) { public static void removeWidget(Widget widget) { widgets.remove(widget); } - /* public static void onScreenResized(int newWidth, int newHeight) { - float scaleX = (float) newWidth / MC.getWindow().getScaledWidth(); - float scaleY = (float) newHeight / MC.getWindow().getScaledHeight(); - System.out.println("newWidth: " + newWidth); - System.out.println("newHeight: " + newHeight); - - System.out.println("scaleX: " + scaleX); - - for (Widget widget : widgets) { - float newX = widget.getX() * scaleX; - float newY = widget.getY() * scaleY; - System.out.println("newX: " + newX); - - // Ensure the widget is within the screen bounds - newX = MathHelper.clamp(newX,0,newWidth - widget.getWidth()); - newY = MathHelper.clamp(newY,0,newHeight - widget.getHeight()); - System.out.println("newX2: " + newX); - - widget.setPosition(newX, newY); - } - } - - */ /** * Attempts to restore the widgets back to their place on screen resize. @@ -210,7 +187,10 @@ public static void loadWidgets(File file) throws IOException { if (file.exists()) { NbtCompound rootTag = NbtIo.read(file.toPath()); NbtList widgetList = rootTag.getList("widgets", NbtType.COMPOUND); - + if(widgetList == null){ + printWarn("RootTag is null. File is either empty or corrupted," + file); + return; + } for (int i = 0; i < widgetList.size(); i++) { NbtCompound widgetTag = widgetList.getCompound(i); WidgetData widgetData = widgetDataMap.get(widgetTag.getString("name")); diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetRenderer.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetRenderer.java index 34baac0..b57043e 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetRenderer.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetRenderer.java @@ -1,6 +1,6 @@ package com.tanishisherewith.dynamichud.newTrial.widget; -import com.tanishisherewith.dynamichud.DynamicHUD; +import com.tanishisherewith.dynamichud.newTrial.DynamicHUD; import com.tanishisherewith.dynamichud.newTrial.screens.AbstractMoveableScreen; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.GameMenuScreen; 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 2536f77..311bc11 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java @@ -1,7 +1,7 @@ package com.tanishisherewith.dynamichud.newTrial.widgets; -import com.tanishisherewith.dynamichud.helpers.ColorHelper; import com.tanishisherewith.dynamichud.newTrial.config.GlobalConfig; +import com.tanishisherewith.dynamichud.newTrial.helpers.ColorHelper; import com.tanishisherewith.dynamichud.newTrial.utils.DynamicValueRegistry; import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.ContextMenu; import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.BooleanOption; @@ -28,7 +28,7 @@ public class TextWidget extends Widget { protected boolean rainbow; // Whether to apply a rainbow effect to the text protected int rainbowSpeed = 2; //Speed of the rainbow effect Supplier textSupplier; - String dynamicRegistryKey = ""; + String dynamicRegistryKey; DynamicValueRegistry dynamicValueRegistry = null; private ContextMenu menu; public TextWidget() { @@ -161,7 +161,6 @@ public void readFromTag(NbtCompound tag) { dynamicValueRegistry = dvr; return; } - createMenu(); } public enum Enum { diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/CustomItemRenderer.java b/src/main/java/com/tanishisherewith/dynamichud/util/CustomItemRenderer.java deleted file mode 100644 index d5beee6..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/CustomItemRenderer.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.tanishisherewith.dynamichud.util; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.render.item.ItemRenderer; -import net.minecraft.item.ItemStack; - -public class CustomItemRenderer { - private final ItemStack itemStack; // The text renderer instance - private final float scale; // The scaling factor of the text - - /** - * Constructs a CustomTextRenderer object. - * - * @param stack The stack to render - * @param scale The scaling factor of the text - */ - public CustomItemRenderer(ItemStack stack, float scale) { - // The Minecraft client instance - this.itemStack = stack; - this.scale = scale; - } - - /** - * Draws a text with shadow on the screen with the given parameters. - * - * @param context The drawContext to use for rendering - * @param x The x position of the text in pixels - * @param y The y position of the text in pixels - * @param color The color of the text in ARGB format - */ - public void draw(DrawContext context, int x, int y, int color) { - context.getMatrices().push(); // Pushes the current matrix onto the stack - context.getMatrices().scale(scale, scale, scale); // Scales the matrix by the scaling factor - context.drawItem(itemStack,(int) (x/scale), (int) (y/scale),color); - context.getMatrices().pop(); // Pops the current matrix from the stack and restores the previous one - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/CustomTextRenderer.java b/src/main/java/com/tanishisherewith/dynamichud/util/CustomTextRenderer.java deleted file mode 100644 index 0bb0621..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/CustomTextRenderer.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.tanishisherewith.dynamichud.util; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.DrawContext; - -public class CustomTextRenderer { - private final TextRenderer textRenderer; // The text renderer instance - private final float scale; // The scaling factor of the text - - /** - * Constructs a CustomTextRenderer object. - * - * @param client The Minecraft client instance - * @param scale The scaling factor of the text - */ - public CustomTextRenderer(MinecraftClient client, float scale) { - // The Minecraft client instance - this.textRenderer = client.textRenderer; - this.scale = scale; - } - - /** - * Draws a text with shadow on the screen with the given parameters. - * - * @param context The drawContext to use for rendering - * @param text The text to draw - * @param x The x position of the text in pixels - * @param y The y position of the text in pixels - * @param color The color of the text in ARGB format - */ - public void draw(DrawContext context, String text, int x, int y, int color, boolean shadow) { - context.getMatrices().push(); // Pushes the current matrix onto the stack - context.getMatrices().scale(scale, scale, scale); // Scales the matrix by the scaling factor - context.drawText(textRenderer,text, (int) (x/scale), (int) (y/scale),color,shadow); - context.getMatrices().pop(); // Pops the current matrix from the stack and restores the previous one - } -} - diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/DynamicUtil.java b/src/main/java/com/tanishisherewith/dynamichud/util/DynamicUtil.java deleted file mode 100644 index b169aa4..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/DynamicUtil.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.tanishisherewith.dynamichud.util; - - -import com.tanishisherewith.dynamichud.helpers.ColorHelper; -import com.tanishisherewith.dynamichud.helpers.DrawHelper; -import com.tanishisherewith.dynamichud.huds.AbstractMoveableScreen; -import com.tanishisherewith.dynamichud.widget.Widget; -import com.tanishisherewith.dynamichud.widget.WidgetBox; -import com.tanishisherewith.dynamichud.widget.WidgetManager; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.gui.screen.TitleScreen; -import net.minecraft.client.option.KeyBinding; -import net.minecraft.client.render.BufferBuilder; -import net.minecraft.client.render.VertexConsumerProvider; - -import java.util.Set; - - -/** - * This class provides utility methods for working with the DynamicHUD mod. - */ -public class DynamicUtil extends DrawContext { - static MinecraftClient client = MinecraftClient.getInstance(); - private final WidgetManager widgetManager; // The WidgetManager instance used by this class - public boolean WidgetAdded = false; - public boolean MainMenuWidgetAdded = false; - public boolean WidgetLoaded = false; - - - /** - * Constructs a DynamicUtil object. - * - * @param client The Minecraft client instance - */ - public DynamicUtil(MinecraftClient client) { - super(client, VertexConsumerProvider.immediate(new BufferBuilder(3))); - this.widgetManager = new WidgetManager(); - } - - /** - * Opens the MovableScreen when the specified key is pressed. - * - * @param key The key to listen for - * @param screen The AbstractMoveableScreen instance to use to set the screen - */ - public static void openDynamicScreen(KeyBinding key, AbstractMoveableScreen screen) { - if (key.wasPressed()) { - client.setScreen(screen); - } - } - - /** - * Renders widgets on screen. - * - * @param context - MatrixStack used for rendering. - * @param delta - Time elapsed since last frame in seconds. - */ - public void render(DrawContext context, float delta) { - Set mainMenuWidgets = widgetManager.getMainMenuWidgets(); - Set widgets = widgetManager.getWidgets(); - - if (client.currentScreen instanceof TitleScreen) { - // Draw each Menu widget - for (Widget widget : mainMenuWidgets) { - widget.render(context); - widget.updatePosition(); - int backgroundColor = widget.isEnabled() ? ColorHelper.getColor(0, 0, 0, 128) : ColorHelper.getColor(255, 0, 0, 128); - WidgetBox box = widget.getWidgetBox(); - DrawHelper.fill(context, (int) (box.x1 - 2), (int) (box.y1 - 2), (int) (box.x2 + 2), (int) (box.y2 + 2), backgroundColor); - } - return; - } - - // Draw each widget - if (client.currentScreen instanceof AbstractMoveableScreen) { - for (Widget widget : widgets) { - if (client.currentScreen instanceof AbstractMoveableScreen) { - widget.render(context); - } else if (widget.isEnabled()) { - widget.render(context); - } - // Draw a red box around the widget if the HUD is disabled - if (client.currentScreen instanceof AbstractMoveableScreen) { - int backgroundColor = widget.isEnabled() ? ColorHelper.getColor(0, 0, 0, 128) : ColorHelper.getColor(255, 0, 0, 128); - WidgetBox box = widget.getWidgetBox(); - DrawHelper.fill(context, (int) (box.x1 - 2), (int) (box.y1 - 2), (int) (box.x2 + 2), (int) (box.y2 + 2), backgroundColor); - } - widget.updatePosition(); - } - } - } - - - /** - * Returns WidgetManager instance used by this class. - * - * @return WidgetManager instance used by this class. - */ - public WidgetManager getWidgetManager() { - return widgetManager; - } - -} \ No newline at end of file diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/ColorGradientPicker.java b/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/ColorGradientPicker.java deleted file mode 100644 index 6086188..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/ColorGradientPicker.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.tanishisherewith.dynamichud.util.colorpicker; - -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.function.Consumer; - -public class ColorGradientPicker { - private final MinecraftClient client; // The Minecraft client instance - private final Consumer onColorSelected; // The callback to call when a color is selected - private final GradientSlider gradientSlider; - private final GradientBox gradientBox; - private final ColorPickerButton colorPickerButton; - - - public ColorGradientPicker(MinecraftClient client, int x, int y, int initialColor, Consumer onColorSelected, int BoxSize, int Colors, Widget selectedWidget) { - this.client = client; - this.onColorSelected = onColorSelected; - float[] hsv = new float[3]; - Color.RGBtoHSB((initialColor >> 16) & 0xFF, (initialColor >> 8) & 0xFF, initialColor & 0xFF, hsv); - - // The initial color has an alpha component - hsv[0] = 0.0f; // Set hue to default value - hsv[1] = 1.0f; // Set saturation to default value - hsv[2] = 1.0f; // Set value to default value - this.gradientSlider = new GradientSlider(x, y, Colors, 10, selectedWidget); - this.gradientSlider.setHue(hsv[0]); - - this.gradientBox = new GradientBox(x, y + 20, BoxSize, selectedWidget); - 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 tick() { - gradientSlider.tick(); - gradientBox.tick(); - } - - public void render(DrawContext drawContext) { - tick(); - gradientSlider.render(drawContext); - gradientBox.render(drawContext); - colorPickerButton.render(drawContext); - 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 (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) { - gradientSlider.onDrag(mouseX, mouseY, button); - gradientBox.setHue(gradientSlider.getHue()); - gradientBox.onDrag(mouseX, mouseY, button); - onColorSelected.accept(gradientBox.getColor()); - } - -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/ColorPickerButton.java b/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/ColorPickerButton.java deleted file mode 100644 index b47c625..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/ColorPickerButton.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.tanishisherewith.dynamichud.util.colorpicker; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; - -public class ColorPickerButton { - private final int x; - private final 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) { - drawContext.getMatrices().push(); - drawContext.getMatrices().translate(0,0,404); - // Draw the button - drawContext.fill(x, y, x + width, y + height, 0xFFAAAAAA); - drawContext.drawCenteredTextWithShadow(MinecraftClient.getInstance().textRenderer, "Pick", x + width / 2, y + (height - 8) / 2, 0xFFFFFFFF); - drawContext.getMatrices().pop(); - } - - 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; - } -} \ No newline at end of file diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/GradientBox.java b/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/GradientBox.java deleted file mode 100644 index 3df903f..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/GradientBox.java +++ /dev/null @@ -1,129 +0,0 @@ -package com.tanishisherewith.dynamichud.util.colorpicker; - -import com.tanishisherewith.dynamichud.helpers.DrawHelper; -import com.tanishisherewith.dynamichud.widget.Widget; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; - -import java.awt.*; - -public class GradientBox { - private final int size; - private final float alphaSpeed = 0.05f; - private final Widget selectedWidget; - private int x; - private int y; - private float hue = 0.0f; - private float saturation = 1.0f; - private float value = 1.0f; - private boolean isDragging = false; - private float alpha = 0.0f; - - - public GradientBox(int x, int y, int size, Widget selectedWidget) { - this.x = x; - this.y = y; - this.size = size; - this.selectedWidget = selectedWidget; - } - - public void tick() { - // Update the alpha - alpha += alphaSpeed; - if (alpha > 1.0f) { - alpha = 1.0f; - } - } - - public void render(DrawContext drawContext) { - drawContext.getMatrices().push(); - drawContext.getMatrices().translate(0,0,401); - DrawHelper.drawOutlinedBox(drawContext, x - 2, y - 2, x + size + 2, y + size + 2, -1); - - // Draw the gradient - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - float saturation = (float) i / size; - float value = 1.0f - (float) j / size; - int color = Color.HSBtoRGB(hue, saturation, value); - color = (color & 0x00FFFFFF) | ((int) (alpha * 255) << 24); - drawContext.fill(x + i, y + j, x + i + 1, y + j + 1, color); - } - } - - // Draw the handle - float handleSize = 3; - float handleX = x + saturation * size - handleSize / 2.0f; - float handleY = y + (1.0f - value) * size - handleSize / 2.0f; - - DrawHelper.fillRoundedRect(drawContext, (int) handleX, (int) handleY, (int) (handleX + handleSize), (int) (handleY + handleSize), -1); - if (this.selectedWidget != null) - setPosition(selectedWidget.getX() + 30, selectedWidget.getY() + MinecraftClient.getInstance().textRenderer.fontHeight + 4); - drawContext.getMatrices().pop(); - } - - /** - * Sets position. - * - * @param x - X position to set. - * @param y - Y position to set. - */ - public void setPosition(int x, int y) { - this.x = x; - this.y = y; - } - - public void onClick(double mouseX, double mouseY, int button) { - if (button == 0) { - float handleSize = 5; - float handleX = x + saturation * size - handleSize / 2.0f; - float handleY = y + (1.0f - value) * size - handleSize / 2.0f; - - if (mouseX >= handleX && mouseX <= handleX + handleSize && mouseY >= handleY && mouseY <= handleY + handleSize) { - this.isDragging = true; - } else if (mouseX >= x && mouseX <= x + size && mouseY >= y && mouseY <= y + size) { - saturation = (float) (mouseX - x) / size; - value = 1.0f - (float) (mouseY - y) / size; - this.isDragging = true; - } - } - } - - public boolean isMouseOver(double mouseX, double mouseY) { - return mouseX >= x && mouseX <= x + size && mouseY >= y && mouseY <= y + size; - } - - public void onRelease(double mouseX, double mouseY, int button) { - if (button == 0) { - isDragging = false; - } - } - - public void onDrag(double mouseX, double mouseY, int button) { - if (isDragging) { - saturation = (float) (mouseX - x) / size; - saturation = Math.max(0, saturation); - saturation = Math.min(1, saturation); - - value = 1.0f - (float) (mouseY - y) / size; - value = Math.max(0, value); - value = Math.min(1, value); - } - } - - public void setHue(float hue) { - this.hue = hue; - } - - public void setSaturation(float saturation) { - this.saturation = saturation; - } - - public void setValue(float value) { - this.value = value; - } - - public int getColor() { - return Color.HSBtoRGB(hue, saturation, value); - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/GradientSlider.java b/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/GradientSlider.java deleted file mode 100644 index 1e53c6a..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/colorpicker/GradientSlider.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.tanishisherewith.dynamichud.util.colorpicker; - -import com.tanishisherewith.dynamichud.helpers.DrawHelper; -import com.tanishisherewith.dynamichud.widget.Widget; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; - -import java.awt.*; - -public class GradientSlider { - private final int width; - private final int height; - private final float progressSpeed = 0.1f; - private final float alphaSpeed = 0.05f; - private final Widget selectedWidget; - private int x; - private int y; - private float hue = 0.0f; - private float progress = 0.0f; - private boolean isDragging = false; - private float alpha = 0.0f; - - public GradientSlider(int x, int y, int width, int height, Widget selectedWidget) { - this.x = x; - this.y = y; - this.width = width; - this.height = height; - this.selectedWidget = selectedWidget; - } - - public void tick() { - // Update the progress - progress += progressSpeed; - if (progress > 1.0f) { - progress = 1.0f; - } - - // Update the alpha - alpha += alphaSpeed; - if (alpha > 1.0f) { - alpha = 1.0f; - } - } - - /** - * Sets position. - * - * @param x - X position to set. - * @param y - Y position to set. - */ - public void setPosition(int x, int y) { - this.x = x; - this.y = y; - } - - public void render(DrawContext drawContext) { - drawContext.getMatrices().push(); - drawContext.getMatrices().translate(0,0,401); - DrawHelper.drawOutlinedBox(drawContext, x - 2, y - 2, x + width + 2, y + height + 2, -1); - - // Draw the gradient - for (int i = 0; i < width; i++) { - float hue = (float) i / width; - int color = Color.HSBtoRGB(hue, 1.0f, 1.0f); - color = (color & 0x00FFFFFF) | ((int) (alpha * 255) << 24); - drawContext.fill(x + i, y, x + i + 1, y + height, color); - } - - // Draw the handle - if (progress >= 1.0f) { - float handleWidth = 3; - float handleHeight = height + 4; - float handleX = x + hue * width - handleWidth / 2.0f; - float handleY = y - (handleHeight - height) / 2.0f; - - DrawHelper.fillRoundedRect(drawContext, (int) handleX, (int) handleY, (int) (handleX + handleWidth), (int) (handleY + handleHeight), -1); - } - if (this.selectedWidget != null) - setPosition(selectedWidget.getX() + 30, selectedWidget.getY() + MinecraftClient.getInstance().textRenderer.fontHeight + 4); - drawContext.getMatrices().pop(); - } - - public void onClick(double mouseX, double mouseY, int button) { - if (button == 0) { - float handleWidth = 3; - float handleHeight = height + 4; - float handleX = x + hue * width - handleWidth / 2.0f; - float handleY = y - (handleHeight - height) / 2.0f; - - if (mouseX >= handleX && mouseX <= handleX + handleWidth && mouseY >= handleY && mouseY <= handleY + handleHeight) { - this.isDragging = true; - } else if (mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height) { - hue = (float) (mouseX - x) / width; - this.isDragging = true; - } - } - } - - public boolean isMouseOver(double mouseX, double mouseY) { - return mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height; - } - - public void onRelease(double mouseX, double mouseY, int button) { - if (button == 0) { - isDragging = false; - } - } - - public void onDrag(double mouseX, double mouseY, int button) { - if (isDragging) { - hue = (float) (mouseX - x) / width; - hue = Math.max(0, hue); - hue = Math.min(1, hue); - } - } - - public float getHue() { - return hue; - } - - public void setHue(float hue) { - this.hue = hue; - } -} - diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/ContextMenu.java b/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/ContextMenu.java deleted file mode 100644 index 2d533aa..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/ContextMenu.java +++ /dev/null @@ -1,441 +0,0 @@ -package com.tanishisherewith.dynamichud.util.contextmenu; - -import com.tanishisherewith.dynamichud.helpers.ColorHelper; -import com.tanishisherewith.dynamichud.helpers.DrawHelper; -import com.tanishisherewith.dynamichud.widget.Widget; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.gui.screen.Screen; - -import java.awt.*; -import java.util.ArrayList; -import java.util.List; -import java.util.function.Consumer; -import java.util.function.Supplier; - - -public class ContextMenu { - private static int optionY; - private final MinecraftClient client; // The Minecraft client instance - private final List options = new ArrayList<>(); // The list of options in the context menu - private final Widget selectedWidget; // The widget that this context menu is associated with - private final Screen parentScreen; - private int width = 0; // The width of the context menu - private int x; // The x position of the context menu - private int y; // The y position of the context menu - private int backgroundColor = 0x90C0C0C0;// Semi-transparent light grey color - private int padding = 5; // The amount of padding around the rectangle - private int HeightFromWidget = 5; // The amount of padding around the rectangle - private float scale = 0.0f; - private int height = 0; - private String dataInputValue = ""; - private String doubleInputValue = ""; - - - /** - * Constructs a ContextMenu object. - * - * @param client The Minecraft client instance - * @param x The x position of the context menu - * @param y The y position of the context menu - * @param selectedWidget The widget that this context menu is associated with - */ - public ContextMenu(MinecraftClient client, int x, int y, Widget selectedWidget, Screen parentScreen) { - this.client = client; - this.selectedWidget = selectedWidget; - this.parentScreen = parentScreen; - this.x = x; - this.y = Math.round(y + selectedWidget.getWidgetBox().getHeight()); - } - - public static int getOptionY() { - return optionY; - } - - /** - * Sets the options to enable or disable based on values - * - * @param label The label of the option - * @param option Context Menu options - */ - public void setOptions(String label, ContextMenuOption option) { - if (selectedWidget instanceof ContextMenuOptionsProvider optionsProvider) { - option.enabled = optionsProvider.isOptionEnabled(label); - } - } - - /** - * Adds an option to the context menu. - * - * @param label The label of the option - * @param action The action to perform when the option is clicked - */ - public void addOption(String label, Runnable action) { - ContextMenuOption option = new ContextMenuOption(label, action); - if (selectedWidget != null) { - setOptions(label, option); - } - options.add(option); - } - - public void addDataTextOption(String label, Consumer action, int WidgetX, int WidgetY) { - int OptionY = WidgetY + HeightFromWidget + 2; - WidgetX += client.textRenderer.getWidth(label + dataInputValue); - OptionY += options.size() * (client.textRenderer.fontHeight + 2); - DataInputOption option = new DataInputOption(label + dataInputValue, text -> { - action.accept(text); - dataInputValue = text; - }, WidgetX, OptionY); - if (selectedWidget != null) { - setOptions(label, option); - } - options.add(option); - } - - public void addDoubleTextOption(String label, Consumer action, int WidgetX, int WidgetY) { - int OptionY = WidgetY + HeightFromWidget + 2; - WidgetX += client.textRenderer.getWidth(label + dataInputValue); - OptionY += options.size() * (client.textRenderer.fontHeight + 2); - DoubleInputOption option = new DoubleInputOption(label + doubleInputValue, text -> { - action.accept(text); - doubleInputValue = String.valueOf(text); - }, WidgetX, OptionY); - if (selectedWidget != null) { - setOptions(label, option); - } - options.add(option); - } - - public void setBackgroundColor(int backgroundColor) { - this.backgroundColor = backgroundColor; - } - - /** - * Returns whether the given point is within the bounds of this context menu. - * - * @param x - X position of the point. - * @param y - Y position of the point. - * @return true if the point is within the bounds of this context menu, false otherwise. - */ - public boolean contains(double x, double y) { - return x >= this.x - 3 && x <= this.x + width + 13 && y >= this.y + HeightFromWidget - 3 && y <= this.y + height + HeightFromWidget + 3; - } - - public int getX() { - return x; - } - - public int getY() { - return y; - } - - public int getWidth() { - return width; - } - - public Screen getParentScreen() { - return parentScreen; - } - - public Widget getSelectedWidget() { - return selectedWidget; - } - - public float getScale() { - return scale; - } - - public int getHeight() { - return height; - } - - public int getOptionY(int optionIndex) { - int OptionY = y + HeightFromWidget + 2; - OptionY += optionIndex * (client.textRenderer.fontHeight + 2); - return OptionY; - } - - /** - * Adds an option to the context menu that cycles through the values of an enum. - *

- * Usage Example: - * Position currentPosition = Position.ABOVE; - *

- * ContextMenu contextMenu = new ContextMenu(client, x, y); - *

- * contextMenu.addEnumCycleOption("Position", Position.values(), () -> currentPosition, newPosition -> { - * currentPosition = newPosition; - * }); - * - * @param labelPrefix The label to display for this option in the context menu - * @param values An array of enum values that specifies the possible values that this option can cycle through - * @param getter A Supplier that returns the current value of the enum - * @param setter A Consumer that sets the new value of the enum - * @param The type of the enum - */ - public > void addEnumCycleOption(String labelPrefix, T[] values, Supplier getter, Consumer setter) { - ContextMenuOption option = new EnumCycleContextMenuOption<>(labelPrefix, values, getter, () -> { - // Get the current value of the enum - T currentValue = getter.get(); - - // Find the index of the current value in the values array - int index = -1; - for (int i = 0; i < values.length; i++) { - if (values[i] == currentValue) { - index = i; - break; - } - } - - // Increment the index and wrap around if necessary - index = (index + 1) % values.length; - - // Set the new value of the enum - setter.accept(values[index]); - }); - options.add(option); - } - - - public void tick() { - // Update the scale - float scaleSpeed = 0.1f; - scale += scaleSpeed; - if (scale > 1.0f) { - scale = 1.0f; - } - } - - /** - * Updates the position of this context menu to avoid getting out of the screen. - */ - public void updatePosition() { - // Check if the context menu is outside the bounds of the screen - int screenWidth = client.getWindow().getScaledWidth(); - int screenHeight = client.getWindow().getScaledHeight(); - if (x + width + 14 > screenWidth) { - x = screenWidth - width - 14; - } - if (y + HeightFromWidget - 2 < 0) { - y = HeightFromWidget + 2; - } - if (y + height + HeightFromWidget + 2 > screenHeight) { - y = screenHeight - height - HeightFromWidget - 2; - } - } - - /** - * Renders this context menu on screen. - * - * @param drawContext - MatrixStack used for rendering. - */ - public void render(DrawContext drawContext) { - tick(); - TextRenderer textRenderer = client.textRenderer; - calculateSize(textRenderer); - applyScale(drawContext); - int x1 = x - 1; - int y1 = y + HeightFromWidget - 2; - int x2 = x + width + 8; - int y2 = y + height + HeightFromWidget + 2; - // Draw the background - DrawHelper.drawCutRectangle(drawContext, x1, y1, x2, y2, 0, backgroundColor, 1); - optionY = y + HeightFromWidget + 2; - drawOptions(drawContext, textRenderer); - if (selectedWidget != null) - setPosition(selectedWidget.getX(), selectedWidget.getY() + textRenderer.fontHeight + 4); - drawContext.getMatrices().pop(); - updatePosition(); - } - - private void applyScale(DrawContext drawContext) { - // Apply the scale - drawContext.getMatrices().push(); - drawContext.getMatrices().translate(x + width / 2.0f + 5, y + height / 2.0f + HeightFromWidget, 300); - drawContext.getMatrices().scale(scale, scale, 1.0f); - drawContext.getMatrices().translate(-(x + width / 2.0f + 5), -(y + height / 2.0f + HeightFromWidget), 300); - } - - private void calculateSize(TextRenderer textRenderer) { - // Calculate the size of the context menu - width = 0; - height = 0; - for (ContextMenuOption option : options) { - width = Math.max(width, textRenderer.getWidth(option.label) + padding); - height += textRenderer.fontHeight + 2; - } - } - - private void drawOptions(DrawContext drawContext, TextRenderer textRenderer) { - int labelTextcolor; - for (ContextMenuOption option : options) { - if (option instanceof EnumCycleContextMenuOption enumOption) { - enumOption.updateLabel(); - labelTextcolor = Color.WHITE.getRGB(); - } else if (option instanceof DataInputOption) { - labelTextcolor = Color.YELLOW.getRGB(); - - // Draw a black box around the value text - String[] splitLabel = option.label.split(":"); - if (splitLabel.length > 1) { - String valueText = splitLabel[1].trim(); - int valueTextWidth = textRenderer.getWidth(valueText); - int labelWidth = textRenderer.getWidth(splitLabel[0].trim()); - int boxX = x + labelWidth + 9; - int boxY = optionY - 2; - int boxWidth = valueTextWidth + 2; - int boxHeight = textRenderer.fontHeight + 1; - DrawHelper.fill(drawContext, boxX, boxY, boxX + boxWidth, boxY + boxHeight, 0x7F000000); - } - - } else if (option instanceof DoubleInputOption) { - labelTextcolor = ColorHelper.ColorToInt(Color.BLUE.brighter()); - // Draw a black box around the value text - String[] splitLabel = option.label.split(":"); - if (splitLabel.length > 1) { - String valueText = splitLabel[1].trim(); - int valueTextWidth = textRenderer.getWidth(valueText); - int labelWidth = textRenderer.getWidth(splitLabel[0].trim()); - int boxX = x + labelWidth + 9; - int boxY = optionY - 2; - int boxWidth = valueTextWidth + 2; - int boxHeight = textRenderer.fontHeight + 1; - DrawHelper.fill(drawContext, boxX, boxY, boxX + boxWidth, boxY + boxHeight, 0x7F000000); - } - } else { - labelTextcolor = option.enabled ? 0xFF00FF00 : 0xFFFF0000; - } - drawContext.drawText(textRenderer, option.label, x + 5, optionY, labelTextcolor, false); - optionY += textRenderer.fontHeight + 2; - } - } - - /** - * Sets position of this context menu. - * - * @param x - X position to set. - * @param y - Y position to set. - */ - public void setPosition(int x, int y) { - this.x = x; - this.y = y; - } - - public void setPadding(int padding) { - this.padding = padding; - } - - public void setHeightFromWidget(int HeightFromWidget) { - this.HeightFromWidget = HeightFromWidget; - } - - public List getOptions() { - return options; - } - - /** - * Handles mouse clicks on this context menu. - * - * @param mouseX - X position of mouse cursor. - * @param mouseY - Y position of mouse cursor. - * @param button - Mouse button that was clicked. - * @return true if mouse click was handled by this context menu. - */ - public boolean mouseClicked(double mouseX, double mouseY, int button) { - TextRenderer textRenderer = client.textRenderer; - int optionY = y + HeightFromWidget + 2; - for (ContextMenuOption option : options) { - if (mouseX >= x && mouseX <= x + textRenderer.getWidth(option.label) + 10 && mouseY >= optionY && mouseY <= optionY + textRenderer.fontHeight + 2) { - // Run the action of the selected option - option.action.run(); - option.enabled = !option.enabled; - return true; - } - optionY += textRenderer.fontHeight + 2; - } - return false; - } - - - private static class ContextMenuOption { - Runnable action; // The action to perform when the option is clicked - String label; // The label of the option - private boolean enabled = false; // Whether the option is enabled - - /** - * Constructs a ContextMenuOption object. - * - * @param label - Label of this option. - * @param action - Action to perform when this option is clicked. - */ - public ContextMenuOption(String label, Runnable action) { - this.label = label; - this.action = action; - } - - public String getLabel() { - return label; - } - - public void setLabel(String newLabel) { - this.label = newLabel; - } - - public void setAction(Runnable action) { - this.action = action; - } - } - - private static class EnumCycleContextMenuOption> extends ContextMenuOption { - private final String labelPrefix; - private final T[] values; - private final Supplier getter; - - public EnumCycleContextMenuOption(String labelPrefix, T[] values, Supplier getter, Runnable action) { - super(labelPrefix + getter.get(), action); - this.labelPrefix = labelPrefix; - this.values = values; - this.getter = getter; - } - - public void updateLabel() { - label = labelPrefix + getter.get(); - } - } - - public class DataInputOption extends ContextMenuOption { - private final Consumer labelSetter; - - public DataInputOption(String label, Consumer consumer, int x, int y) { - super(label, null); - setAction(() -> { - // Open a new screen to allow the player to input data - MinecraftClient.getInstance().setScreen(new DataInputScreen(consumer, x, y, parentScreen, this)); - }); - this.labelSetter = text -> setLabel(label + ": " + text); - } - - public Consumer getLabelSetter() { - return labelSetter; - } - } - - public class DoubleInputOption extends ContextMenuOption { - private final Consumer labelSetter; - - public DoubleInputOption(String label, Consumer consumer, int x, int y) { - super(label, null); - setAction(() -> { - // Open a new screen to allow the player to input data - MinecraftClient.getInstance().setScreen(new DoubleInputScreen(consumer, x, y, parentScreen, this)); - }); - this.labelSetter = text -> setLabel(label + ": " + text); - } - - public Consumer getLabelSetter() { - return labelSetter; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/ContextMenuOptionsProvider.java b/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/ContextMenuOptionsProvider.java deleted file mode 100644 index 33ab859..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/ContextMenuOptionsProvider.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.tanishisherewith.dynamichud.util.contextmenu; - -public interface ContextMenuOptionsProvider { - boolean isOptionEnabled(String label); -} - diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/DataInputScreen.java b/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/DataInputScreen.java deleted file mode 100644 index 31c68c5..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/DataInputScreen.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.tanishisherewith.dynamichud.util.contextmenu; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.text.Text; -import org.lwjgl.glfw.GLFW; - -import java.util.function.Consumer; - -public class DataInputScreen extends Screen { - private final Consumer consumer; - private final Screen parentScreen; - private final ContextMenu.DataInputOption DataInputOption; - private TextWidgetButtonExt textField; - private int x, y; - - - public DataInputScreen(Consumer consumer, int x, int y, Screen parentScreen, ContextMenu.DataInputOption dataInputOption) { - super(Text.of("Data Input")); - this.consumer = consumer; - this.x = x; - this.y = y; - this.parentScreen = parentScreen; - this.DataInputOption = dataInputOption; - if (this.x > MinecraftClient.getInstance().getWindow().getScaledWidth()) - this.x = MinecraftClient.getInstance().getWindow().getScaledWidth() - 110; - if (this.y > MinecraftClient.getInstance().getWindow().getScaledWidth()) - this.y = MinecraftClient.getInstance().getWindow().getScaledHeight() - 14; - if (this.x < 0) this.x = MinecraftClient.getInstance().getWindow().getScaledWidth() + 110; - if (this.y < 0) this.y = MinecraftClient.getInstance().getWindow().getScaledHeight() + 14; - } - - @Override - protected void init() { - // Create a text field for the player to input data - this.textField = new TextWidgetButtonExt(this.textRenderer, x, y, 100, 14, Text.of("")); - this.addDrawableChild(this.textField); - } - - @Override - public boolean shouldPause() { - return false; - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER) { - // Close the screen and pass the entered data to the consumer when the Enter key is pressed - String text = this.textField.getText(); - this.consumer.accept(text); - MinecraftClient.getInstance().setScreen(parentScreen); - DataInputOption.getLabelSetter().accept(text); - } - return super.keyPressed(keyCode, scanCode, modifiers); - } -} - diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/DoubleInputScreen.java b/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/DoubleInputScreen.java deleted file mode 100644 index cfd074b..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/DoubleInputScreen.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.tanishisherewith.dynamichud.util.contextmenu; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.text.Text; -import org.lwjgl.glfw.GLFW; - -import java.util.function.Consumer; - -public class DoubleInputScreen extends Screen { - private final Consumer consumer; - private final Screen parentScreen; - private final ContextMenu.DoubleInputOption doubleInputOption; - private TextWidgetButtonExt textField; - private int x, y; - - public DoubleInputScreen(Consumer consumer, int x, int y, Screen parentScreen, ContextMenu.DoubleInputOption doubleInputOption) { - super(Text.of("Double Input")); - this.consumer = consumer; - this.x = x; - this.y = y; - this.parentScreen = parentScreen; - this.doubleInputOption = doubleInputOption; - if (this.x > MinecraftClient.getInstance().getWindow().getScaledWidth()) - this.x = MinecraftClient.getInstance().getWindow().getScaledWidth() - 110; - if (this.y > MinecraftClient.getInstance().getWindow().getScaledWidth()) - this.y = MinecraftClient.getInstance().getWindow().getScaledHeight() - 14; - if (this.x < 0) this.x = MinecraftClient.getInstance().getWindow().getScaledWidth() + 110; - if (this.y < 0) this.y = MinecraftClient.getInstance().getWindow().getScaledHeight() + 14; - } - - @Override - protected void init() { - // Create a text field for the player to input data - this.textField = new TextWidgetButtonExt(this.textRenderer, x, y, 100, 14, Text.of("")); - this.addDrawableChild(this.textField); - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER) { - // Close the screen and pass the entered data to the consumer when the Enter key is pressed - try { - double value = Double.parseDouble(this.textField.getText()); - this.consumer.accept(value); - MinecraftClient.getInstance().setScreen(parentScreen); - doubleInputOption.getLabelSetter().accept(value); - } catch (NumberFormatException e) { - // Handle invalid input - this.textField.setText(""); - } - } - return super.keyPressed(keyCode, scanCode, modifiers); - } - - @Override - public boolean shouldPause() { - return false; - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/TextWidgetButtonExt.java b/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/TextWidgetButtonExt.java deleted file mode 100644 index 60be115..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/util/contextmenu/TextWidgetButtonExt.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.tanishisherewith.dynamichud.util.contextmenu; - -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.gui.widget.TextFieldWidget; -import net.minecraft.text.Text; - -public class TextWidgetButtonExt extends TextFieldWidget { - public TextWidgetButtonExt(TextRenderer textRenderer, int x, int y, int width, int height, Text text) { - super(textRenderer, x, y, width, height, text); - } - -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/Widget.java b/src/main/java/com/tanishisherewith/dynamichud/widget/Widget.java deleted file mode 100644 index 30927be..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/Widget.java +++ /dev/null @@ -1,218 +0,0 @@ -package com.tanishisherewith.dynamichud.widget; - -import com.tanishisherewith.dynamichud.interfaces.TextGenerator; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.nbt.NbtCompound; - -import java.awt.*; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.*; - -/** - * This class represents a widget that can be displayed on the screen. - */ -public abstract class Widget { - protected static Map textGenerators = new HashMap<>(); - - protected final MinecraftClient client; // The Minecraft client instance - public boolean enabled = true; // Whether the widget is enabled - public boolean isDraggable = true; - protected float xPercent; // The x position of the widget as a percentage of the screen width - protected float yPercent; // The y position of the widget as a percentage of the screen height - protected String label; - protected static float scale = 1f; // The scaling factor of the widget - - /** - * Constructs a Widget object. - * - * @param client The Minecraft client instance - */ - public Widget(MinecraftClient client, String label) { - this.client = client; - this.label = label; - } - - public static void addTextGenerator(String label, TextGenerator textGenerator) { - textGenerators.put(label, textGenerator); - } - - public abstract void setTextGeneratorFromLabel(); - - /** - * Gets the box around the widget for some purpose - */ - public abstract WidgetBox getWidgetBox(); - - public void setDraggable(boolean draggable) { - isDraggable = draggable; - } - - - public boolean isOverlapping(Set other) { - for (Widget widget : other) { - if ((this.getX() < widget.getX() + widget.getWidgetBox().getWidth() && this.getX() + this.getWidgetBox().getWidth() > widget.getX() && - this.getY() < widget.getY() + widget.getWidgetBox().getHeight() && this.getY() + this.getWidgetBox().getHeight() > widget.getY())) { - return true; - } - } - return false; - } - - public boolean isOverlapping(Widget other) { - return this.getX() < other.getX() + other.getWidgetBox().getWidth() && this.getX() + this.getWidgetBox().getWidth() > other.getX() && - this.getY() < other.getY() + other.getWidgetBox().getHeight() && this.getY() + this.getWidgetBox().getHeight() > other.getY(); - } - - /** - * Renders the widget on the screen. - */ - public abstract void render(DrawContext drawContext); - - public void updatePosition() { - int screenWidth = client.getWindow().getScaledWidth(); - int screenHeight = client.getWindow().getScaledHeight(); - } - - /** - * Returns whether the widget is enabled. - * - * @return True if the widget is enabled, false otherwise - */ - public boolean isEnabled() { - return enabled; - } - - /** - * Returns the x position of the widget. - * - * @return The x position of the widget in pixels - */ - public int getX() { - return (int) (client.getWindow().getScaledWidth() * xPercent); - } - /** - * Returns the scaling factor of the widget. - * - * @return The scaling factor of the widget - */ - public static float getScale() { - return scale; - } - - /** - * Sets the scaling factor of the widget. - * - * @param scale The new scaling factor of the widget - */ - public static void setScale(float scale) { - Widget.scale = scale; - } - /** - * Sets the x position of the widget. - * - * @param x The new x position of the widget in pixels - */ - public void setX(float x) { - int screenWidth = client.getWindow().getScaledWidth(); - if (x < 0) { - x = 0; - } else if (x + getWidgetBox().getWidth() > screenWidth) { - x = screenWidth - getWidgetBox().getWidth(); - } - this.xPercent = (float) x / screenWidth; - } - - /** - * Returns the y position of the widget. - * - * @return The y position of the widget in pixels - */ - public int getY() { - return (int) (client.getWindow().getScaledHeight() * yPercent); - } - - /** - * Sets the y position of the widget. - * - * @param y The new y position of the widget in pixels - */ - public void setY(float y) { - int screenHeight = client.getWindow().getScaledHeight(); - if (y < 0) { - y = 0; - } else if (y + getWidgetBox().getHeight() > screenHeight) { - y = (screenHeight) - getWidgetBox().getHeight(); - } - this.yPercent = (float) y / screenHeight; - } - - /** - * Returns the fontheight - * - * @return fontHeight from TextRenderer - */ - public int getHeight() { - return client.textRenderer.fontHeight; - } - - public void readFromTag(NbtCompound tag) { - xPercent = tag.getFloat("xPercent"); - yPercent = tag.getFloat("yPercent"); - enabled = tag.getBoolean("Enabled"); - isDraggable = tag.getBoolean("isDraggable"); - label = tag.getString("label"); - scale = tag.getFloat("scale"); - - setTextGeneratorFromLabel(); - } - - /** - * Writes the state of this widget to the given tag. - * - * @param tag The tag to write to - */ - public void writeToTag(NbtCompound tag) { - tag.putString("class", getClass().getName()); - tag.putBoolean("isDraggable", isDraggable); - tag.putFloat("xPercent", xPercent); - tag.putFloat("yPercent", yPercent); - tag.putBoolean("Enabled", enabled); - tag.putString("label", label); - tag.putFloat("scale", scale); - - - for (Field field : getClass().getDeclaredFields()) { - if (Modifier.isStatic(field.getModifiers())) continue; - - field.setAccessible(true); - - try { - Object value = field.get(this); - - if (value instanceof Boolean) { - tag.putBoolean(field.getName(), (Boolean) value); - } else if (value instanceof Byte) { - tag.putByte(field.getName(), (Byte) value); - } else if (value instanceof Short) { - tag.putShort(field.getName(), (Short) value); - } else if (value instanceof Integer) { - tag.putInt(field.getName(), (Integer) value); - } else if (value instanceof Long) { - tag.putLong(field.getName(), (Long) value); - } else if (value instanceof Float) { - tag.putFloat(field.getName(), (Float) value); - } else if (value instanceof Double) { - tag.putDouble(field.getName(), (Double) value); - } else if (value instanceof String) { - tag.putString(field.getName(), (String) value); - } else if (value instanceof Color colorvalue) { - tag.putInt(field.getName(), colorvalue.getRGB()); - }// Add more cases here for other data types - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetBox.java b/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetBox.java deleted file mode 100644 index a61db70..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetBox.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.tanishisherewith.dynamichud.widget; - -public class WidgetBox { - private float width; - private float height; - public float x1 = 0, x2 = 0, y1 = 0, y2 = 0; - - public WidgetBox(float x1, float y1, float x2, float y2, float scale) { - this.width = (x2 - x1) * scale; - this.height = (y2 - y1) * scale; - this.x1 = x1; - this.x2 = x1 + width; - this.y1 = y1; - this.y2 = y1 + height; - } - - public WidgetBox(float x1, float y1, double width, double height, float scale) { - this.width = (float) (width * scale); - this.height = (float) (height * scale); - this.x1 = x1; - this.x2 = x1 + this.width; - this.y1 = y1; - this.y2 = y1 + this.height; - } - - public boolean contains(Widget widget, double x, double y, float scale) { - if (x1 == 0 || x2 == 0 || y1 == 0 || y2 == 0) { - x1 = widget.getX() - width / 2; - y1 = widget.getY() - height / 2; - x2 = widget.getX() + width / 2; - y2 = widget.getY() + height / 2; - } - return x >= x1 && x <= x2 && y >= y1 && y <= y2; - } - - public boolean intersects(float otherX1, float otherY1, float otherX2, float otherY2) { - return !(otherX1 > x2 || otherX2 < x1 || otherY1 > y2 || otherY2 < y1); - } - public boolean isMouseOver(double mouseX, double mouseY) { - return mouseX >= x1 && mouseX <= x1 + width && mouseY >= y1 && mouseY <= y2 + height; - } - - public float getWidth() { - return width; - } - - public float getHeight() { - return height; - } - - public void setSizeAndPosition(float x, float y, float width, float height){ - this.x1 = x; - this.y1 = y; - this.height = height; - this.width = width; - } - public void setSizeAndPosition(float x, float y, float width, float height,boolean shouldScale, float scale){ - this.x1 = x; - this.y1 = y; - this.height = height * (shouldScale? scale : 1.0f); - this.width = width * (shouldScale? scale : 1.0f); - } - public void setSize(double width, double height) { - if (width >= 0) - this.width = (int) Math.ceil(width); - if (height >= 0) - this.height = (int) Math.ceil(height); - } - public void setPosition(float x,float y, float x2, float y2){ - this.x1 = x; - this.x2 = x2; - this.y1 = y; - this.y2 = y2; - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetManager.java b/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetManager.java deleted file mode 100644 index a16c90a..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/WidgetManager.java +++ /dev/null @@ -1,200 +0,0 @@ -package com.tanishisherewith.dynamichud.widget; - -import com.tanishisherewith.dynamichud.DynamicHUD; -import com.tanishisherewith.dynamichud.interfaces.WidgetLoading; -import net.fabricmc.fabric.api.util.NbtType; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.nbt.NbtIo; -import net.minecraft.nbt.NbtList; - -import java.io.*; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; -import java.util.HashSet; -import java.util.Set; - -import static com.tanishisherewith.dynamichud.DynamicHUD.printInfo; - -/** - * This class manages a list of widgets that can be added, removed and retrieved. - */ -public class WidgetManager { - private final Set widgets = new HashSet<>(); // The list of widgets - private final Set MainMenuWidgets = new HashSet<>(); // The list of MainMenu widgets - private WidgetLoading widgetLoading = new WidgetLoading() { - }; - - /** - * Adds a widget to the list. - * - * @param widget The widget to add - */ - public void addWidget(Widget widget) { - widget.setTextGeneratorFromLabel(); - widgets.add(widget); - } - - /** - * Adds a MainMenu widget to the list. - * - * @param widget The widget to add - */ - public void addMainMenuWidget(Widget widget) { - widget.setTextGeneratorFromLabel(); - MainMenuWidgets.add(widget); - } - - public void setWidgetLoading(WidgetLoading widgetLoading) { - this.widgetLoading = widgetLoading; - } - - /** - * Removes a widget from the list. - * - * @param widget The widget to remove - */ - public void removeWidget(Widget widget) { - widgets.remove(widget); - } - - /** - * Removes a MainMenu widget from the list. - * - * @param widget The Main Menu widget to remove - */ - public void removeMainMenuWidget(Widget widget) { - MainMenuWidgets.remove(widget); - } - - - /** - * Returns list of all widgets. - * - * @return list of all widgets. - */ - public Set getWidgets() { - return widgets; - } - - /** - * Returns Set of all MainMenu widgets. - * - * @return Set of all MainMenu widgets. - */ - public Set getMainMenuWidgets() { - return MainMenuWidgets; - } - - public Set getOtherWidgets(Widget SelectedWidget) { - Set otherWidgets = new HashSet<>(); - for (Widget widget : getWidgets()) { - if (widget != SelectedWidget) { - otherWidgets.add(widget); - } - } - return otherWidgets; - } - - /** - * Saves the state of all widgets to the given file. - * - * @param file The file to save to - */ - public void saveWidgets(File file) { - NbtCompound rootTag = new NbtCompound(); - NbtList widgetList = new NbtList(); - NbtList MainMenuwidgetList = new NbtList(); - - printInfo("Saving widgets"); - - if (widgets.isEmpty() && MainMenuWidgets.isEmpty()) { - printInfo("Widgets are empty.. Saving interrupted to prevent empty file"); - return; - } - - Set widgetSet = new HashSet<>(); - for (Widget widget : widgets) { - NbtCompound widgetTag = new NbtCompound(); - widget.writeToTag(widgetTag); - // Check for duplicates - if (widgetSet.add(widgetTag.toString())) { - widgetList.add(widgetTag); - } - } - - rootTag.put("Widgets", widgetList); - - Set MainMenuWidgetSet = new HashSet<>(); - for (Widget mmwidget : MainMenuWidgets) { - NbtCompound widgetTag = new NbtCompound(); - mmwidget.writeToTag(widgetTag); - // Check for duplicates - if (MainMenuWidgetSet.add(widgetTag.toString())) { - MainMenuwidgetList.add(widgetTag); - } - } - rootTag.put("MainMenuWidgets", MainMenuwidgetList); - - // Use a temporary file to write the data - File tempFile = new File(file.getAbsolutePath() + ".tmp"); - try (DataOutputStream out = new DataOutputStream(new FileOutputStream(tempFile))) { - NbtIo.writeCompressed(rootTag, out); - // Check if the data has been written successfully - if (tempFile.length() > 0) { - // Check if the temporary file exists and can be renamed - Files.move(tempFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING); - } else { - throw new IOException("Failed to write data to temporary file OR Empty data passed"); - } - } catch (IOException e) { - // Delete the temporary file if an error occurs - boolean temp = tempFile.delete(); - e.printStackTrace(); - } - } - - - - public Set loadWigdets(File file) { - Set widgets = new HashSet<>(); - if (file.exists()) { - printInfo("Widgets File exists"); - try (DataInputStream in = new DataInputStream(new FileInputStream(file))) { - DataInput input = new DataInputStream(in); - NbtCompound rootTag = NbtIo.readCompound(input); - NbtList widgetList = rootTag.getList("Widgets", NbtType.COMPOUND); - for (int i = 0; i < widgetList.size(); i++) { - NbtCompound widgetTag = widgetList.getCompound(i); - String className = widgetTag.getString("class"); - widgets.add(widgetLoading.loadWidgetsFromTag(className, widgetTag)); - printInfo("Wigdet " + i + ": " + widgets.stream().toList().get(i).toString()); - } - } catch (IOException e) { - e.printStackTrace(); - } - } else - DynamicHUD.printWarn("Widgets File does not exist"); - return widgets; - } - - public Set loadMainMenuWigdets(File file) { - Set MainMenuwidgets = new HashSet<>(); - if (file.exists()) { - try (DataInputStream in = new DataInputStream(new FileInputStream(file))) { - DataInput input = new DataInputStream(in); - NbtCompound rootTag = NbtIo.readCompound(input); - NbtList MainMenuwidgetList = rootTag.getList("MainMenuWidgets", NbtType.COMPOUND); - for (int i = 0; i < MainMenuwidgetList.size(); i++) { - NbtCompound widgetTag = MainMenuwidgetList.getCompound(i); - String className = widgetTag.getString("class"); - MainMenuwidgets.add(widgetLoading.loadWidgetsFromTag(className, widgetTag)); - printInfo("MainMenu Wigdet " + i + ": " + MainMenuwidgets.stream().toList().get(i).toString()); - } - } catch (IOException e) { - e.printStackTrace(); - } - } else - DynamicHUD.printWarn("Widgets File does not exist"); - return MainMenuwidgets; - } -} \ No newline at end of file diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/armor/ArmorWidget.java b/src/main/java/com/tanishisherewith/dynamichud/widget/armor/ArmorWidget.java deleted file mode 100644 index f433a5b..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/armor/ArmorWidget.java +++ /dev/null @@ -1,141 +0,0 @@ -package com.tanishisherewith.dynamichud.widget.armor; - -import com.tanishisherewith.dynamichud.helpers.ColorHelper; -import com.tanishisherewith.dynamichud.helpers.TextureHelper; -import com.tanishisherewith.dynamichud.interfaces.TextGenerator; -import com.tanishisherewith.dynamichud.widget.Widget; -import com.tanishisherewith.dynamichud.widget.WidgetBox; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.render.item.ItemRenderer; -import net.minecraft.entity.EquipmentSlot; -import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; -import net.minecraft.nbt.NbtCompound; - -import java.awt.*; -import java.util.function.Supplier; - -/** - * This class represents a widget that displays the armor item in a specified equipment slot. - */ -public class ArmorWidget extends Widget { - public final TextureHelper.Position[] currentTextPosition = TextureHelper.Position.values(); - protected EquipmentSlot slot; // The equipment slot to display the armor item from - protected TextGenerator textGenerator; - protected Supplier color; - protected boolean TextBackground; - - /** - * Constructs an ArmorWidget object. - * - * @param client The Minecraft client instance - * @param slot The equipment slot to display the armor item from - * @param xPercent The x position of the widget as a percentage of the screen width - * @param yPercent The y position of the widget as a percentage of the screen height - */ - public ArmorWidget(MinecraftClient client, EquipmentSlot slot, float xPercent, float yPercent, boolean enabled, TextureHelper.Position currentTextPosition, TextGenerator textGenerator, Supplier color, boolean Textbackground, String label) { - super(client, label); - this.slot = slot; - this.xPercent = xPercent; - this.yPercent = yPercent; - this.enabled = enabled; - this.currentTextPosition[0] = currentTextPosition; - this.textGenerator = textGenerator; - this.color = color; - this.TextBackground = Textbackground; - } - - /** - * Renders the widget on the screen. - */ - @Override - public void render(DrawContext drawContext) { - ItemRenderer itemRenderer = client.getItemRenderer(); - ItemStack armorItem; - TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer; - if (client.player == null) { - armorItem = Items.DIAMOND_CHESTPLATE.getDefaultStack(); - } else { - armorItem = client.player.getEquippedStack(slot); - } - TextureHelper.drawItemTextureWithTextAndScale(drawContext, scale, textRenderer, armorItem, getX(), getY(), getText(), ColorHelper.ColorToInt(getColor()), currentTextPosition[0], scale*0.5f, TextBackground); - } - - @Override - public void setTextGeneratorFromLabel() { - TextGenerator textGenerator = textGenerators.get(label); - if (textGenerator != null) { - setTextGenerator(textGenerator); - } - } - - @Override - public WidgetBox getWidgetBox() { - return new WidgetBox(this.getX(), this.getY(), this.getX() + this.getWidth(), this.getY() + this.getHeight(),scale); - } - - /** - * Returns the width of the widget. - * - * @return The width of the widget in pixels - */ - public int getWidth() { - return 16; // The width of an item texture is 16 pixels - } - - /** - * Returns the height of the widget. - * - * @return The height of the widget in pixels - */ - public int getHeight() { - return 16; // The height of an item texture is 16 pixels - } - - /** - * Returns the text displayed by this widget. - * - * @return The text displayed by this widget - */ - public String getText() { - return textGenerator.generateText(); - } - - public void setTextGenerator(TextGenerator textGenerator) { - this.textGenerator = textGenerator; - } - - public Color getColor() { - return color.get(); - } - - public void setColor(Supplier color) { - this.color = color; - } - - @Override - public void writeToTag(NbtCompound tag) { - super.writeToTag(tag); - tag.putString("slot", slot.getName()); - tag.putString("Position", String.valueOf(this.currentTextPosition[0])); - if (this.getText() != null) tag.putString("text", this.getText()); - tag.putInt("Color", this.getColor().getRGB()); - tag.putBoolean("TextBackground", this.TextBackground); - } - - @Override - public void readFromTag(NbtCompound tag) { - super.readFromTag(tag); - slot = EquipmentSlot.byName(tag.getString("slot")); - String Position = tag.getString("Position"); - color = () -> ColorHelper.getColorFromInt(tag.getInt("Color")); - if (TextureHelper.Position.getByUpperCaseName(Position) != null && !(tag.getString("Position") == null) && !tag.getString("Position").isEmpty()) - currentTextPosition[0] = TextureHelper.Position.getByUpperCaseName(Position); - else - currentTextPosition[0] = TextureHelper.Position.ABOVE; - TextBackground = tag.getBoolean("TextBackground"); - label = tag.getString("label"); - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/item/ItemWidget.java b/src/main/java/com/tanishisherewith/dynamichud/widget/item/ItemWidget.java deleted file mode 100644 index 6c5ac60..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/item/ItemWidget.java +++ /dev/null @@ -1,141 +0,0 @@ -package com.tanishisherewith.dynamichud.widget.item; - -import com.tanishisherewith.dynamichud.helpers.ColorHelper; -import com.tanishisherewith.dynamichud.helpers.TextureHelper; -import com.tanishisherewith.dynamichud.interfaces.TextGenerator; -import com.tanishisherewith.dynamichud.widget.Widget; -import com.tanishisherewith.dynamichud.widget.WidgetBox; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.render.item.ItemRenderer; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; - -import java.awt.*; -import java.util.function.Supplier; - -public class ItemWidget extends Widget { - public final TextureHelper.Position[] currentTextPosition = TextureHelper.Position.values(); - protected Supplier color; - protected TextGenerator textGenerator; - protected Supplier itemStack; - protected boolean TextBackground; - - /** - * Constructs a Widget object. - * - * @param client The Minecraft client instance - */ - public ItemWidget(MinecraftClient client, Supplier itemStack, float xPercent, float yPercent, boolean enabled, TextureHelper.Position currentTextPosition, TextGenerator textGenerator, Supplier color, boolean Textbackground, String label) { - super(client, label); - this.xPercent = xPercent; - this.yPercent = yPercent; - this.enabled = enabled; - this.itemStack = itemStack; - this.currentTextPosition[0] = currentTextPosition; - this.textGenerator = textGenerator; - this.color = color; - this.TextBackground = Textbackground; - } - - @Override - public WidgetBox getWidgetBox() { - return new WidgetBox(this.getX(), this.getY(), this.getX() + this.getWidth(), this.getY() + this.getHeight(),scale); - } - - /** - * Returns the width of the widget. - * - * @return The width of the widget in pixels - */ - public int getWidth() { - return 16; // The width of an item texture is 16 pixels - } - - /** - * Returns the height of the widget. - * - * @return The height of the widget in pixels - */ - public int getHeight() { - return 16; // The height of an item texture is 16 pixels - } - - public ItemStack getItemStack() { - return itemStack.get(); - } - - public void setItemStack(Supplier itemStack) { - this.itemStack = itemStack; - } - - /** - * Returns the text displayed by this widget. - * - * @return The text displayed by this widget - */ - public String getText() { - return textGenerator.generateText(); - } - - public void setTextGenerator(TextGenerator textGenerator) { - this.textGenerator = textGenerator; - } - - public Supplier getColor() { - return color; - } - - public void setColor(Supplier color) { - this.color = color; - } - - @Override - public void setTextGeneratorFromLabel() { - TextGenerator textGenerator = textGenerators.get(label); - if (textGenerator != null) { - setTextGenerator(textGenerator); - } - } - - @Override - public void writeToTag(NbtCompound tag) { - super.writeToTag(tag); - tag.putString("Position", String.valueOf(this.currentTextPosition[0])); - tag.putInt("ItemID", Item.getRawId(getItemStack().getItem())); - tag.putInt("ItemCount", getItemStack().getMaxCount()); - tag.putString("text", getText()); - tag.putBoolean("TextBackground", this.TextBackground); - } - - @Override - public void readFromTag(NbtCompound tag) { - super.readFromTag(tag); - String Position = tag.getString("Position"); - - int itemID = tag.getInt("ItemID"); - int itemCount = tag.getInt("ItemCount"); - itemStack = () -> getItemStack(itemID, itemCount); - - if (TextureHelper.Position.getByUpperCaseName(Position) != null && !tag.getString("Position").isEmpty()) - currentTextPosition[0] = TextureHelper.Position.getByUpperCaseName(Position); - else - currentTextPosition[0] = TextureHelper.Position.ABOVE; - TextBackground = tag.getBoolean("TextBackground"); - label = tag.getString("label"); - } - - public ItemStack getItemStack(int itemID, int itemCount) { - Item item = Item.byRawId(itemID); - return new ItemStack(item, itemCount); - } - - @Override - public void render(DrawContext drawContext) { - ItemRenderer itemRenderer = client.getItemRenderer(); - TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer; - TextureHelper.drawItemTextureWithTextAndScale(drawContext, scale, textRenderer, getItemStack(), getX(), getY(), getText(), ColorHelper.ColorToInt(color.get()), currentTextPosition[0], scale*0.5f, TextBackground); - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/slider/ScaleSliderWidget.java b/src/main/java/com/tanishisherewith/dynamichud/widget/slider/ScaleSliderWidget.java deleted file mode 100644 index 0e60528..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/slider/ScaleSliderWidget.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.tanishisherewith.dynamichud.widget.slider; - -import com.tanishisherewith.dynamichud.widget.Widget; -import net.minecraft.client.gui.widget.SliderWidget; -import net.minecraft.text.Text; -import java.text.DecimalFormat; - -public class ScaleSliderWidget extends SliderWidget { - private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##"); - private final float minValue; - private final float maxValue; - - public ScaleSliderWidget(int x, int y, int width, int height, Text message, double value, float minValue, float maxValue) { - super(x, y, width, height, message, value); - this.minValue = minValue; - this.maxValue = maxValue; - } - - public float getValue(){ - return (float) (minValue + (maxValue - minValue) * value); - } - - @Override - protected void updateMessage() { - String formattedValue = DECIMAL_FORMAT.format(getValue()); - setMessage(Text.of("Widgets Scale: " + formattedValue)); - } - - @Override - protected void applyValue() { - Widget.setScale(getValue()); - } - -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/slider/SliderWidget.java b/src/main/java/com/tanishisherewith/dynamichud/widget/slider/SliderWidget.java deleted file mode 100644 index 84b70ad..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/slider/SliderWidget.java +++ /dev/null @@ -1,216 +0,0 @@ -package com.tanishisherewith.dynamichud.widget.slider; - -import com.tanishisherewith.dynamichud.helpers.DrawHelper; -import com.tanishisherewith.dynamichud.widget.Widget; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.DrawContext; - -import java.util.function.Consumer; - -/** - * This class represents a slider widget that allows the user to select a value within a specified range. - */ -public class SliderWidget { - private final MinecraftClient client; - private final int width; // The width of the widget - private final String label; // The label displayed above the slider - private final float minValue; // The minimum value of the slider - private final float maxValue; // The maximum value of the slider - private final int height; // The height of the widget - private int x; // The x position of the widget - private int y; // The y position of the widget - private float value; // The current value of the slider - private Widget selectedWidget = null; - private Consumer getValue; - private float progress = 0.0f; - private float progressSpeed = 0.1f; - private float textProgress = 0.0f; - private float textProgressSpeed = 0.05f; - private boolean MouseClicked = false; - - /** - * Constructs a SliderWidget object. - * - * @param client The Minecraft client instance - * @param x The x position of the widget - * @param y The y position of the widget - * @param width The width of the widget - * @param height The height of the widget - * @param label The label displayed above the slider - * @param value The initial value of the slider - * @param minValue The minimum value of the slider - * @param maxValue The maximum value of the slider - * @param selectedWidget The widget which was selected to display this slider - */ - public SliderWidget(MinecraftClient client, int x, int y, int width, int height, String label, float value, float minValue, float maxValue, Consumer getValue, Widget selectedWidget) { - this.client = client; - this.x = x; - this.y = y; - this.width = width; - this.height = height; - this.label = label; - this.value = value; - this.minValue = minValue; - this.maxValue = maxValue; - this.getValue=getValue; - this.selectedWidget = selectedWidget; - } - - public void tick() { - // Update the progress - progress += progressSpeed; - if (progress > 1.0f) { - progress = 1.0f; - } - // Update the text progress - textProgress += textProgressSpeed; - if (textProgress > 1.0f) { - textProgress = 1.0f; - } - } - - /** - * Updates the position of this Slider to avoid getting out of the screen. - */ - public void updatePosition() { - // Check if the Slider is outside the bounds of the screen - int screenWidth = client.getWindow().getScaledWidth(); - int screenHeight = client.getWindow().getScaledHeight(); - if (x + width > screenWidth) { - x = screenWidth - width + 1; - } - if (y + 2 < 0) { - y += 2; - } - if (y + height + 2 > screenHeight) { - y = screenHeight - height - 2; - } - } - - /** - * Renders the widget on the screen. - * - * @param drawContext The matrix stack used for rendering - */ - public void render(DrawContext drawContext) { - tick(); - // Draw the label - TextRenderer textRenderer = client.textRenderer; - String labelText = label + ": " + String.format("%.1f", value); - int labelWidth = textRenderer.getWidth(labelText); - int labelX = (int) (x + (width - labelWidth) / 2.0f * textProgress) - 1; - int labelY = y + height - textRenderer.fontHeight - 6; - drawContext.drawTextWithShadow(textRenderer, labelText, labelX, labelY, 0xFFFFFFFF); - - // Draw the slider - int sliderWidth = width - 8; - int sliderHeight = 2; - int sliderX = x; - int sliderY = y + height - sliderHeight; - - drawSlider(drawContext, sliderX, sliderY, sliderWidth, sliderHeight); - - // Draw the handle - float handleWidth = 4; - float handleHeight = 10; - float handleX = sliderX + (value - minValue) / (maxValue - minValue) * (sliderWidth - handleWidth); - float handleY = sliderY + ((sliderHeight - handleHeight) / 2); - - if (progress >= 1.0f) { - DrawHelper.fillRoundedRect(drawContext, (int) handleX, (int) handleY, (int) (handleX + handleWidth), (int) (handleY + handleHeight), 0xFFFFFFFF); - } - - if (selectedWidget != null) - setPosition(selectedWidget.getX(), selectedWidget.getY() + textRenderer.fontHeight + 67); - updatePosition(); - } - - private void drawSlider(DrawContext drawContext, int sliderX, int sliderY, int sliderWidth, int sliderHeight) { - int visibleSliderWidth = (int) (sliderWidth * progress); - DrawHelper.fill(drawContext, sliderX, sliderY, sliderX + visibleSliderWidth, sliderY + sliderHeight, 0xFFFFFFFF); - } - - /** - * Returns whether the given point is within the bounds of this widget. - * - * @param x - X position of the point. - * @param y - Y position of the point. - * @return true if the point is within the bounds of this context menu, false otherwise. - */ - public boolean contains(double x, double y) { - return x >= this.x + 2 && x <= this.x - 2 + width && y >= this.y + 2 && y <= this.y - 2 + height; - } - - - /** - * Sets the position of the widget. - * - * @param x The new x position of the widget - * @param y The new y position of the widget - */ - public void setPosition(int x, int y) { - this.x = x; - this.y = y; - } - - /** - * Handles mouse clicks on the widget. - * - * @param mouseX The x position of the mouse cursor - * @param mouseY The y position of the mouse cursor - * @param button The mouse button that was clicked - * @return True if the mouse click was handled by the widget, false otherwise - */ - public boolean mouseClicked(double mouseX, double mouseY, int button) { - // Check if the mouse is over the slider - if (mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height) { - // Update the value based on the mouse position - MouseClicked = !MouseClicked; - setValue(minValue + (float) (mouseX - x) / width * (maxValue - minValue) - 0.001f); - getValue.accept(value); - return true; - } - MouseClicked = false; - return false; - } - - /** - * Handles mouse dragging on the widget. - * - * @param mouseX The current x position of the mouse cursor - * @param mouseY The current y position of the mouse cursor - * @param button The mouse button that is being dragged - * @param deltaX The change in x position of the mouse cursor since the last call to this method - * @param deltaY The change in y position of the mouse cursor since the last call to this method - * @return True if the mouse dragging was handled by the widget, false otherwise - */ - public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { - // Check if the mouse is over the slider - if (mouseX >= x && mouseX <= x + width && MouseClicked) { - // Update the value based on the mouse position - setValue(minValue + (float) (mouseX - x) / width * (maxValue - minValue)); - getValue.accept(value); - return true; - } - return false; - } - - /** - * Returns the current value of the slider. - * - * @return The current value of the slider - */ - public float getValue() { - return value; - } - - /** - * Sets the value of the slider. - * - * @param value The new value of the slider - */ - public void setValue(float value) { - this.value = Math.min(Math.max(value, minValue), maxValue); - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/slider/SliderWidgetBuilder.java b/src/main/java/com/tanishisherewith/dynamichud/widget/slider/SliderWidgetBuilder.java deleted file mode 100644 index e79cd96..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/slider/SliderWidgetBuilder.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.tanishisherewith.dynamichud.widget.slider; - -import com.tanishisherewith.dynamichud.widget.Widget; -import net.minecraft.client.MinecraftClient; - -import java.util.function.Consumer; - -public class SliderWidgetBuilder { - private final MinecraftClient client; - private int x; - private int y; - private int width; - private int height; - private String label; - private float value; - private float minValue; - private float maxValue; - private Widget selectedWidget; - private Consumer getValue; - - public SliderWidgetBuilder(MinecraftClient client) { - this.client = client; - } - - public SliderWidgetBuilder setX(int x) { - this.x = x; - return this; - } - - public SliderWidgetBuilder setY(int y) { - this.y = y; - return this; - } - - public SliderWidgetBuilder setWidth(int width) { - this.width = width; - return this; - } - - public SliderWidgetBuilder setHeight(int height) { - this.height = height; - return this; - } - - public SliderWidgetBuilder setLabel(String label) { - this.label = label; - return this; - } - - public SliderWidgetBuilder setValue(float value) { - this.value = value; - return this; - } - - public SliderWidgetBuilder setMinValue(float minValue) { - this.minValue = minValue; - return this; - } - - public SliderWidgetBuilder setMaxValue(float maxValue) { - this.maxValue = maxValue; - return this; - } - public SliderWidgetBuilder getValue(Consumer getValue) { - this.getValue = getValue; - return this; - } - - public SliderWidgetBuilder setSelectedWidget(Widget selectedWidget) { - this.selectedWidget = selectedWidget; - return this; - } - - public SliderWidget build() { - return new SliderWidget(client, x, y, width, height, label, value, minValue, maxValue,getValue, selectedWidget); - } -} diff --git a/src/main/java/com/tanishisherewith/dynamichud/widget/text/TextWidget.java b/src/main/java/com/tanishisherewith/dynamichud/widget/text/TextWidget.java deleted file mode 100644 index d8a6fcd..0000000 --- a/src/main/java/com/tanishisherewith/dynamichud/widget/text/TextWidget.java +++ /dev/null @@ -1,277 +0,0 @@ -package com.tanishisherewith.dynamichud.widget.text; - -import com.tanishisherewith.dynamichud.helpers.ColorHelper; -import com.tanishisherewith.dynamichud.helpers.DrawHelper; -import com.tanishisherewith.dynamichud.interfaces.TextGenerator; -import com.tanishisherewith.dynamichud.util.contextmenu.ContextMenuOptionsProvider; -import com.tanishisherewith.dynamichud.widget.Widget; -import com.tanishisherewith.dynamichud.widget.WidgetBox; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.text.Text; - - -/** - * This class represents a text widget that displays a specified text on the screen. - */ -public class TextWidget extends Widget implements ContextMenuOptionsProvider { - protected static float rainbowSpeed = 15f; // The speed of the rainbow effect - protected String text; - protected TextGenerator dataText; - protected boolean shadow; // Whether to draw a shadow behind the text - protected boolean rainbow; // Whether to apply a rainbow effect to the text - protected int Textcolor; // The color of the text - protected int Datacolor; // The color of the Data - protected boolean TextcolorOptionEnabled = false; - protected boolean DatacolorOptionEnabled = false; - - - - /** - * Constructs a TextWidget object. - * - * @param client The Minecraft client instance - * @param text The text to display - * @param xPercent The x position of the widget as a percentage of the screen width - * @param yPercent The y position of the widget as a percentage of the screen height - */ - public TextWidget(MinecraftClient client, String text, TextGenerator dataText, float xPercent, float yPercent, boolean Shadow, boolean Rainbow, int Textcolor, int Datacolor, boolean enabled) { - super(client, text); - this.text = text; - this.dataText = dataText; - this.xPercent = xPercent; - this.yPercent = yPercent; - this.shadow = Shadow; - this.rainbow = Rainbow; - this.Textcolor = Textcolor; - this.Datacolor = Datacolor; - this.enabled = enabled; - } - - /** - * Toggles whether the Data color option is enabled. - */ - public void toggleTextColorOption() { - TextcolorOptionEnabled = !TextcolorOptionEnabled; - } - - /** - * Toggles whether the Text color option is enabled. - */ - public void toggleDataColorOption() { - DatacolorOptionEnabled = !DatacolorOptionEnabled; - } - - /** - * Sets whether the rainbow effect is enabled. - * - * @param rainbow True if the rainbow effect should be enabled, false otherwise - */ - public void setRainbow(boolean rainbow) { - this.rainbow = rainbow; - } - - /** - * Sets whether the shadow is enabled. - * - * @param shadow True if the shadow should be enabled, false otherwise - */ - public void setShadow(boolean shadow) { - this.shadow = shadow; - } - - /** - * Returns the speed of the rainbow effect. - * - * @return The speed of the rainbow effect - */ - public float getRainbowSpeed() { - return rainbowSpeed; - } - - /** - * Sets the speed of the rainbow effect. - * - * @param rainbowSpeed The new speed of the rainbow effect - */ - public static void setRainbowSpeed(float rainbowSpeed) { - TextWidget.rainbowSpeed = rainbowSpeed; - } - - /** - * Returns whether the rainbow effect is enabled. - * - * @return True if the rainbow effect is enabled, false otherwise - */ - public boolean hasRainbow() { - return rainbow; - } - - /** - * Returns whether the shadow is enabled. - * - * @return True if the shadow is enabled, false otherwise - */ - public boolean hasShadow() { - return shadow; - } - - - /** - * Returns the text displayed by this widget. - * - * @return The text displayed by this widget - */ - public String getText() { - return text; - } - - /** - * Returns the text displayed by this widget. - * - * @return The text displayed by this widget - */ - public String getDataText() { - return dataText.generateText(); - } - - - public void setDataTextGenerator(TextGenerator textGenerator) { - this.dataText = textGenerator; - } - - /** - * Returns the color of the text. - * - * @return The color of the text - */ - public int getTextcolor() { - return Textcolor; - } - - /** - * Returns the color of the Data. - * - * @return The color of the Data - */ - public int getDatacolor() { - return Datacolor; - } - - @Override - public void setTextGeneratorFromLabel() { - TextGenerator textGenerator = textGenerators.get(getText()); - if (textGenerator != null) { - setDataTextGenerator(textGenerator); - } - } - - /** - * Sets the color of the text. - * - * @param color The new color of the text - */ - public void setTextColor(int color) { - this.Textcolor = color; - } - - /** - * Sets the color of the text. - * - * @param color The new color of the text - */ - public void setDataColor(int color) { - this.Datacolor = color; - } - - - /** - * Returns whether color options are enabled for this widget. - * - * @return true if color options are enabled for this widget, false otherwise. - */ - public boolean isTextcolorOptionEnabled() { - return TextcolorOptionEnabled; - } - - /** - * Returns whether color options are enabled for this widget. - * - * @return true if color options are enabled for this widget, false otherwise. - */ - public boolean isDatacolorOptionEnabled() { - return DatacolorOptionEnabled; - } - - @Override - public WidgetBox getWidgetBox() { - TextRenderer textRenderer = client.textRenderer; - String cmtxt=getDataText()+getText() + "."; - float x1 = getX(); //- client.textRenderer.getWidth(textWidget.getText()); - float x2 = getX() + textRenderer.getWidth(cmtxt); - float y1 = getY() - 1 ; - float y2 = getY() + textRenderer.fontHeight - 1; - return new WidgetBox(x1, y1, x2, y2, scale); - } - - - /** - * Renders this widget on screen. - */ - @Override - public void render(DrawContext drawContext) { - drawContext.getMatrices().push(); - drawContext.getMatrices().translate(0, 0, 300); - int Textcolour = rainbow ? ColorHelper.getColorFromHue((System.currentTimeMillis() % 10000) / (rainbowSpeed * 400f)) : this.Textcolor; - int Datacolour = rainbow ? ColorHelper.getColorFromHue((System.currentTimeMillis() % 10000) / (rainbowSpeed * 400f)) : this.Datacolor; - drawTwoTexts(drawContext, getText(), getDataText(), ((getX() + 1)), (getY()), Textcolour, Datacolour, scale); - drawContext.getMatrices().pop(); - } - - - - - @Override - public void writeToTag(NbtCompound tag) { - super.writeToTag(tag); - tag.putBoolean("Rainbow", hasRainbow()); - tag.putBoolean("Shadow", hasShadow()); - tag.putInt("TextColor", Textcolor); - tag.putInt("DataColor", Datacolor); - tag.putString("Text", text); - } - - @Override - public void readFromTag(NbtCompound tag) { - super.readFromTag(tag); - shadow = tag.getBoolean("Shadow"); - rainbow = tag.getBoolean("Rainbow"); - Textcolor = tag.getInt("TextColor"); - Datacolor = tag.getInt("DataColor"); - text = tag.getString("Text"); - } - public void drawTwoTexts(DrawContext drawContext, String text1, String text2, int x, int y, int color1, int color2, float scale) { - drawText(drawContext, text1, x, y, color1); - int x2 = Math.round(x + client.textRenderer.getWidth(text1) * scale); - drawText(drawContext, text2, x2, y, color2); - } - - private void drawText(DrawContext drawContext, String text, int x, int y, int color) { - DrawHelper.drawTextWithScale(drawContext, text, x, y, color, shadow, scale); - //client.textRenderer.draw(text, getX(), getY(), color, scale); - - } - - @Override - public boolean isOptionEnabled(String label) { - return switch (label) { - case "Shadow" -> hasShadow(); - case "Rainbow" -> hasRainbow(); - case "TextColor" -> isTextcolorOptionEnabled(); - case "DataColor" -> isDatacolorOptionEnabled(); - default -> false; - }; - } -}