diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java index 53028db..8ac82e1 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHUD.java @@ -25,16 +25,16 @@ import java.util.List; public class DynamicHUD implements ClientModInitializer { - public static MinecraftClient MC = MinecraftClient.getInstance(); - public static String MOD_ID = "dynamichud"; - private static final Logger logger = LoggerFactory.getLogger("DynamicHud"); - private static final List widgetRenderers = new ArrayList<>(); /** * This is a map to store the list of widgets for each widget file to be saved. *

* Allows saving widgets across different mods with same save file name. */ - public static final HashMap> fileMap = new HashMap<>(); + public static final HashMap> fileMap = new HashMap<>(); + private 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"; public static void addWidgetRenderer(WidgetRenderer widgetRenderer) { widgetRenderers.add(widgetRenderer); @@ -52,6 +52,18 @@ public static void printWarn(String msg) { logger.warn(msg); } + /** + * 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()) { + MC.setScreen(screen); + } + } + @Override public void onInitializeClient() { printInfo("Initialising DynamicHud"); @@ -67,62 +79,62 @@ public void onInitializeClient() { FabricLoader.getInstance() .getEntrypointContainers("dynamicHud", DynamicHudIntegration.class) .forEach(entrypoint -> { - ModMetadata metadata = entrypoint.getProvider().getMetadata(); - String modId = metadata.getId(); - AbstractMoveableScreen screen; - try { - DynamicHudIntegration DHIntegration = entrypoint.getEntrypoint(); - DHIntegration.init(); - - File widgetsFile = DHIntegration.getWidgetsFile(); - - if(widgetsFile.exists()) { - WidgetManager.loadWidgets(widgetsFile); - }else{ - DHIntegration.addWidgets(); - } - DHIntegration.initAfter(); - - screen = DHIntegration.getMovableScreen(); - - KeyBinding binding = DHIntegration.getKeyBind(); - - DHIntegration.registerCustomWidgets(); - - WidgetRenderer widgetRenderer = DHIntegration.getWidgetRenderer(); - addWidgetRenderer(widgetRenderer); - - List widgets = fileMap.get(widgetsFile.getName()); - - if(widgets == null) { - fileMap.put(widgetsFile.getName(), widgetRenderer.getWidgets()); - }else{ - widgetRenderer.getWidgets().addAll(widgets); - fileMap.put(widgetsFile.getName(), widgetRenderer.getWidgets()); - } - - //Register events for rendering, saving, loading, and opening the hudEditor - ClientTickEvents.START_CLIENT_TICK.register((client)-> { - openDynamicScreen(binding, screen); + ModMetadata metadata = entrypoint.getProvider().getMetadata(); + String modId = metadata.getId(); + AbstractMoveableScreen screen; + try { + DynamicHudIntegration DHIntegration = entrypoint.getEntrypoint(); + DHIntegration.init(); + + File widgetsFile = DHIntegration.getWidgetsFile(); + + if (widgetsFile.exists()) { + WidgetManager.loadWidgets(widgetsFile); + } else { + DHIntegration.addWidgets(); + } + DHIntegration.initAfter(); + + screen = DHIntegration.getMovableScreen(); + + KeyBinding binding = DHIntegration.getKeyBind(); + + DHIntegration.registerCustomWidgets(); + + WidgetRenderer widgetRenderer = DHIntegration.getWidgetRenderer(); + addWidgetRenderer(widgetRenderer); + + List widgets = fileMap.get(widgetsFile.getName()); + + if (widgets == null) { + fileMap.put(widgetsFile.getName(), widgetRenderer.getWidgets()); + } else { + widgetRenderer.getWidgets().addAll(widgets); + fileMap.put(widgetsFile.getName(), widgetRenderer.getWidgets()); + } + + //Register events for rendering, saving, loading, and opening the hudEditor + ClientTickEvents.START_CLIENT_TICK.register((client) -> { + openDynamicScreen(binding, screen); + }); + + // Save during exiting a world, server or Minecraft itself + // Also saved when a resource pack is reloaded. + ServerLifecycleEvents.SERVER_STOPPING.register(server -> saveWidgetsSafely(widgetsFile, fileMap.get(widgetsFile.getName()))); + ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, s) -> saveWidgetsSafely(widgetsFile, fileMap.get(widgetsFile.getName()))); + ServerPlayConnectionEvents.DISCONNECT.register((handler, packetSender) -> saveWidgetsSafely(widgetsFile, fileMap.get(widgetsFile.getName()))); + Runtime.getRuntime().addShutdownHook(new Thread(() -> saveWidgetsSafely(widgetsFile, fileMap.get(widgetsFile.getName())))); + + printInfo(String.format("Integration of mod %s was successful", modId)); + } catch (Throwable e) { + if (e instanceof IOException) { + logger.warn("An error has occurred while loading widgets of mod {}", modId, e); + } else { + logger.warn("Mod {} has incorrect implementation of DynamicHUD", modId, e); + } + } }); - // Save during exiting a world, server or Minecraft itself - // Also saved when a resource pack is reloaded. - ServerLifecycleEvents.SERVER_STOPPING.register(server -> saveWidgetsSafely(widgetsFile,fileMap.get(widgetsFile.getName()))); - ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, s) -> saveWidgetsSafely(widgetsFile,fileMap.get(widgetsFile.getName()))); - ServerPlayConnectionEvents.DISCONNECT.register((handler, packetSender) -> saveWidgetsSafely(widgetsFile,fileMap.get(widgetsFile.getName()))); - Runtime.getRuntime().addShutdownHook(new Thread(() -> saveWidgetsSafely(widgetsFile,fileMap.get(widgetsFile.getName())))); - - printInfo(String.format("Integration of mod %s was successful",modId)); - } catch (Throwable e) { - if(e instanceof IOException){ - logger.warn("An error has occurred while loading widgets of mod {}", modId,e); - }else { - logger.warn("Mod {} has incorrect implementation of DynamicHUD", modId, e); - } - } - }); - ServerLifecycleEvents.SERVER_STOPPING.register(server -> GlobalConfig.HANDLER.save()); ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, s) -> GlobalConfig.HANDLER.save()); ServerPlayConnectionEvents.DISCONNECT.register((handler, packetSender) -> GlobalConfig.HANDLER.save()); @@ -131,24 +143,14 @@ public void onInitializeClient() { HudRenderCallback.EVENT.register(new HudRender()); } + private void saveWidgetsSafely(File widgetsFile, List widgets) { try { - WidgetManager.saveWidgets(widgetsFile,widgets); + WidgetManager.saveWidgets(widgetsFile, widgets); } catch (IOException e) { logger.error("Failed to save widgets. Widgets passed: {}", widgets); throw new RuntimeException(e); } } - /** - * 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()) { - MC.setScreen(screen); - } - } } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudIntegration.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudIntegration.java index 6e76760..1a7ef92 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudIntegration.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudIntegration.java @@ -63,7 +63,7 @@ public interface DynamicHudIntegration { /** * Initializes the DynamicHud integration. *

- * Suggested to be used to initialize {@link com.tanishisherewith.dynamichud.newTrial.utils.DynamicValueRegistry} and widgets with their respective values + * Suggested to be used to initialize {@link com.tanishisherewith.dynamichud.newTrial.utils.DynamicValueRegistry} and widgets with their respective values *

*/ void init(); @@ -76,15 +76,17 @@ public interface DynamicHudIntegration { /** * To register custom widgets. This method can be overridden by implementations. */ - default void registerCustomWidgets(){} + default void registerCustomWidgets() { + } /** * Performs any necessary initialization after the widgets have been added. This method can be overridden by implementations. *

- * Suggested to be used to initialize a {@link WidgetRenderer} object with the added widgets. + * Suggested to be used to initialize a {@link WidgetRenderer} object with the added widgets. *

*/ - default void initAfter(){} + default void initAfter() { + } /** * Returns the file where widgets are to be saved and loaded from. @@ -94,6 +96,7 @@ default void initAfter(){} default File getWidgetsFile() { return WIDGETS_FILE; } + /** * Returns the keybind to open the {@link AbstractMoveableScreen} instance. * @@ -116,7 +119,7 @@ default KeyBinding getKeyBind() { * * @return The widget renderer. */ - default WidgetRenderer getWidgetRenderer(){ + default WidgetRenderer getWidgetRenderer() { return new WidgetRenderer(WidgetManager.getWidgets()); } } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudTest.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudTest.java index ff97576..9c4a431 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudTest.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudTest.java @@ -17,16 +17,17 @@ public class DynamicHudTest implements DynamicHudIntegration { TextWidget Example2Widget; DynamicValueRegistry registry; WidgetRenderer renderer; + @Override public void init() { //Global registry - DynamicValueRegistry.registerGlobal("FPS",() -> "FPS: "+ DynamicHUD.MC.getCurrentFps()); + DynamicValueRegistry.registerGlobal("FPS", () -> "FPS: " + DynamicHUD.MC.getCurrentFps()); //Local registry registry = new DynamicValueRegistry(DynamicHUD.MOD_ID); - registry.registerLocal("FPS",()-> "FPS C-DVR: "+ DynamicHUD.MC.getCurrentFps()); + registry.registerLocal("FPS", () -> "FPS C-DVR: " + DynamicHUD.MC.getCurrentFps()); - textWidget = new TextWidget.Builder() + textWidget = new TextWidget.Builder() .setX(300) .setY(100) .setDraggable(true) @@ -36,7 +37,7 @@ public void init() { .shouldScale(false) .build(); - Example2Widget = new TextWidget.Builder() + Example2Widget = new TextWidget.Builder() .setX(200) .setY(100) .setDraggable(true) @@ -53,7 +54,8 @@ public void addWidgets() { WidgetManager.addWidget(textWidget); WidgetManager.addWidget(Example2Widget); } - public void initAfter(){ + + public void initAfter() { List widgets = WidgetManager.getWidgetsForMod(DynamicHUD.MOD_ID); renderer = new WidgetRenderer(widgets); @@ -64,7 +66,8 @@ public void initAfter(){ @Override public AbstractMoveableScreen getMovableScreen() { - return new AbstractMoveableScreen(Text.literal("Editor"), renderer) {}; + return new AbstractMoveableScreen(Text.literal("Editor"), renderer) { + }; } @Override diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudTestTWO.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudTestTWO.java index a449c5e..9615f2c 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudTestTWO.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/DynamicHudTestTWO.java @@ -2,19 +2,16 @@ import com.tanishisherewith.dynamichud.newTrial.screens.AbstractMoveableScreen; import com.tanishisherewith.dynamichud.newTrial.utils.DynamicValueRegistry; -import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option; import com.tanishisherewith.dynamichud.newTrial.widget.Widget; import com.tanishisherewith.dynamichud.newTrial.widget.WidgetManager; import com.tanishisherewith.dynamichud.newTrial.widget.WidgetRenderer; import com.tanishisherewith.dynamichud.newTrial.widgets.TextWidget; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import net.minecraft.client.gui.screen.TitleScreen; -import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; import net.minecraft.client.gui.screen.option.OptionsScreen; import net.minecraft.client.option.KeyBinding; import net.minecraft.text.Text; import org.lwjgl.glfw.GLFW; -import org.lwjgl.opengl.GL; import java.io.File; import java.util.List; @@ -22,10 +19,11 @@ public class DynamicHudTestTWO implements DynamicHudIntegration { TextWidget exampleWidget; WidgetRenderer renderer; + @Override public void init() { //Global registry - DynamicValueRegistry.registerGlobal("CPS",() -> "NOT FPS"); + DynamicValueRegistry.registerGlobal("CPS", () -> "NOT FPS"); exampleWidget = new TextWidget.Builder() @@ -51,14 +49,15 @@ public KeyBinding getKeyBind() { @Override public File getWidgetsFile() { - return new File(FILE_DIRECTORY,"widgets_new.nbt"); + return new File(FILE_DIRECTORY, "widgets_new.nbt"); } @Override public void addWidgets() { WidgetManager.addWidget(exampleWidget); } - public void initAfter(){ + + public void initAfter() { List widgets = WidgetManager.getWidgetsForMod("CustomMod"); renderer = new WidgetRenderer(widgets); @@ -69,7 +68,8 @@ public void initAfter(){ @Override public AbstractMoveableScreen getMovableScreen() { - return new AbstractMoveableScreen(Text.literal("Editor 2"), renderer) {}; + return new AbstractMoveableScreen(Text.literal("Editor 2"), renderer) { + }; } @Override diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/HudRender.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/HudRender.java index bd842b0..48a7014 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/HudRender.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/HudRender.java @@ -7,8 +7,8 @@ public class HudRender implements HudRenderCallback { @Override public void onHudRender(DrawContext drawContext, float tickDelta) { - for(WidgetRenderer widgetRenderer: DynamicHUD.getWidgetRenderers()){ - widgetRenderer.renderWidgets(drawContext,-120,-120); + for (WidgetRenderer widgetRenderer : DynamicHUD.getWidgetRenderers()) { + widgetRenderer.renderWidgets(drawContext, -120, -120); } } } 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 e59ad0c..64a97f7 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,6 @@ import net.minecraft.util.Identifier; public class GlobalConfig { - private static GlobalConfig INSTANCE = new GlobalConfig(); public static ConfigClassHandler HANDLER = ConfigClassHandler.createBuilder(GlobalConfig.class) .id(new Identifier("dynamichud", "dynamichud_config")) .serializer(config -> GsonConfigSerializerBuilder.create(config) @@ -19,15 +18,19 @@ public class GlobalConfig { .setJson5(true) .build()) .build(); - + private static final GlobalConfig INSTANCE = new GlobalConfig(); /** * Common scale for all widgets. Set by the user using YACL. */ @SerialEntry public float scale = 1.0f; + public static GlobalConfig get() { + return INSTANCE; + } + public Screen createYACLGUI() { - return YetAnotherConfigLib.createBuilder() + return YetAnotherConfigLib.createBuilder() .title(Text.literal("DynamicHUD config screen.")) .category(ConfigCategory.createBuilder() .name(Text.literal("General")) @@ -39,14 +42,11 @@ public Screen createYACLGUI() { .name(Text.literal("Scale")) .description(OptionDescription.of(Text.literal("Set scale for all widgets."))) .binding(1.0f, () -> this.scale, newVal -> this.scale = newVal) - .controller(floatOption -> FloatSliderControllerBuilder.create(floatOption).range(0.1f,2.5f).step(0.1f)) + .controller(floatOption -> FloatSliderControllerBuilder.create(floatOption).range(0.1f, 2.5f).step(0.1f)) .build()) .build()) .build()) .build() .generateScreen(null); } - public static GlobalConfig get(){ - return INSTANCE; - } } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/ColorHelper.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/ColorHelper.java index 8a35f38..7426687 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/ColorHelper.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/ColorHelper.java @@ -9,28 +9,18 @@ */ 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; + ColorHelper.r = r; + ColorHelper.g = g; + ColorHelper.b = b; + 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. * @@ -82,10 +72,10 @@ public static int getColorFromHue(float hue) { public static int ColorToInt(Color color) { return color.getRGB(); } - public static float[] getRainbowColor() - { + + public static float[] getRainbowColor() { float x = System.currentTimeMillis() % 2000 / 1000F; - float pi = (float)Math.PI; + float pi = (float) Math.PI; float[] rainbow = new float[3]; rainbow[0] = 0.5F + 0.5F * MathHelper.sin(x * pi); @@ -105,7 +95,6 @@ public static Color getRainbowColor(int speed) { return Color.getHSBColor(hue, 1.0f, 1.0f); } - /** * Changes alpha on color. * @@ -119,6 +108,7 @@ public static Color changeAlpha(Color color, int 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); } @@ -138,8 +128,22 @@ public static int toRGBAB(int color) { public static int toRGBAA(int color) { return (color >> 24) & 0x000000FF; } - public int toInt() - { - return new Color(r,b,g,a).getRGB(); + + 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; + } + + public int toInt() { + return new Color(r, b, g, a).getRGB(); } } 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 d036afd..ea1d60f 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/DrawHelper.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/DrawHelper.java @@ -281,6 +281,7 @@ public static void drawRainbowGradient(Matrix4f matrix, float x, float y, float /* ==== Drawing filled and outline circles ==== */ + /** * Draws an outline of a circle * @@ -783,10 +784,6 @@ public static void drawHorizontalLine(Matrix4f matrix4f, float x1, float width, drawRectangle(matrix4f, x1, y, width, thickness, color); } - public enum Direction { - /* LEFT_RIGHT means from left to right. Same for others */ - LEFT_RIGHT, TOP_BOTTOM, RIGHT_LEFT, BOTTOM_TOP - } /** * Draws an outlined box on the screen. * @@ -805,8 +802,9 @@ public static void drawOutlinedBox(DrawContext drawContext, int x1, int y1, int /** * 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 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) { @@ -818,17 +816,19 @@ public static void scaleAndPosition(MatrixStack matrices, float x, float y, floa // Scale the matrix matrices.scale(scale, scale, 1.0F); - matrices.translate(-x,-y,0); + 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 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 + * @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) { + 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 @@ -840,8 +840,13 @@ public static void scaleAndPosition(MatrixStack matrices, float x, float y,float matrices.translate(-(x + width / 2.0f), -(y + height / 2.0f), 0); } - public static void stopScaling(MatrixStack matrices){ + public static void stopScaling(MatrixStack matrices) { matrices.pop(); // Restore the previous transformation state } + public enum Direction { + /* LEFT_RIGHT means from left to right. Same for others */ + LEFT_RIGHT, TOP_BOTTOM, RIGHT_LEFT, BOTTOM_TOP + } + } \ No newline at end of file diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/TextureHelper.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/TextureHelper.java index 37c36c6..f73aaae 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/TextureHelper.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/helpers/TextureHelper.java @@ -202,7 +202,7 @@ public static void drawItemTextureWithTextAndScale(DrawContext drawContext, 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.drawText(textRenderer, text, (int) scaledX + 1, (int) scaledY + 1, color, false); drawContext.getMatrices().pop(); } customItemRenderer = new CustomItemRenderer(itemStack, itemScale); 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 ef22c98..72194f5 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/screens/AbstractMoveableScreen.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/screens/AbstractMoveableScreen.java @@ -6,13 +6,12 @@ import net.minecraft.text.Text; public abstract class AbstractMoveableScreen extends Screen { - protected boolean shouldPause = false; // To pause if the screen is opened or not public final WidgetRenderer widgetRenderer; public int snapSize = 100; + protected boolean shouldPause = false; // To pause if the screen is opened or not /** * Constructs a AbstractMoveableScreen object. - * */ public AbstractMoveableScreen(Text title, WidgetRenderer renderer) { super(title); @@ -21,19 +20,19 @@ public AbstractMoveableScreen(Text title, WidgetRenderer renderer) { @Override public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { - widgetRenderer.mouseDragged(mouseX,mouseY,button, snapSize); + widgetRenderer.mouseDragged(mouseX, mouseY, button, snapSize); return false; } @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { - widgetRenderer.mouseClicked(mouseX,mouseY,button); + widgetRenderer.mouseClicked(mouseX, mouseY, button); return false; } @Override public boolean mouseReleased(double mouseX, double mouseY, int button) { - widgetRenderer.mouseReleased(mouseX,mouseY,button); + widgetRenderer.mouseReleased(mouseX, mouseY, button); return false; } @@ -51,7 +50,7 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) { @Override public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) { - widgetRenderer.mouseScrolled(mouseX,mouseY,verticalAmount,horizontalAmount); + widgetRenderer.mouseScrolled(mouseX, mouseY, verticalAmount, horizontalAmount); return super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount); } @@ -65,12 +64,12 @@ public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmou */ @Override public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) { - if(this.client.world == null) { + if (this.client.world == null) { this.renderBackgroundTexture(drawContext); } // Draw each widget widgetRenderer.isInEditor = true; - widgetRenderer.renderWidgets(drawContext,mouseX,mouseY); + widgetRenderer.renderWidgets(drawContext, mouseX, mouseY); } @Override @@ -88,7 +87,8 @@ public void setShouldPause(boolean shouldPause) { public boolean shouldPause() { return shouldPause; } - public void setSnapSize(int size){ + + public void setSnapSize(int size) { this.snapSize = size; } } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/DynamicValueRegistry.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/DynamicValueRegistry.java index 393a969..811ef62 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/DynamicValueRegistry.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/DynamicValueRegistry.java @@ -1,6 +1,8 @@ package com.tanishisherewith.dynamichud.newTrial.utils; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import java.util.function.Supplier; /** @@ -20,12 +22,14 @@ public class DynamicValueRegistry extends System { /** * A map that holds the global registry of suppliers. + * * @see #localRegistry */ private static final Map> globalRegistry = new HashMap<>(); /** * A map that holds the local registry of suppliers. + * * @see #globalRegistry */ private final Map> localRegistry = new HashMap<>(); @@ -43,23 +47,13 @@ public DynamicValueRegistry(String modId) { /** * Registers a supplier in the global registry. * - * @param key The key under which the supplier is to be registered. + * @param key The key under which the supplier is to be registered. * @param supplier The supplier to be registered. */ public static void registerGlobal(String key, Supplier supplier) { globalRegistry.put(key, supplier); } - /** - * Registers a supplier in the local registry. - * - * @param key The key under which the supplier is to be registered. - * @param supplier The supplier to be registered. - */ - public void registerLocal(String key, Supplier supplier) { - localRegistry.put(key, supplier); - } - /** * Retrieves a supplier from the global registry. * @@ -70,6 +64,16 @@ public static Supplier getGlobal(String key) { return globalRegistry.get(key); } + /** + * Registers a supplier in the local registry. + * + * @param key The key under which the supplier is to be registered. + * @param supplier The supplier to be registered. + */ + public void registerLocal(String key, Supplier supplier) { + localRegistry.put(key, supplier); + } + /** * Retrieves a supplier from the local registry, falling back to the global registry if necessary. * @@ -93,7 +97,7 @@ public Supplier get(String key) { * * @param map The map to be set as the local registry. */ - public void setLocalRegistry(Map> map){ + public void setLocalRegistry(Map> map) { localRegistry.putAll(map); } } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/System.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/System.java index 8022537..f741907 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/System.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/System.java @@ -5,20 +5,19 @@ import java.util.concurrent.ConcurrentHashMap; public abstract class System { - protected final String modId; - // A map to store all instances of DynamicValueRegistry by modId protected static final Map> instances = new ConcurrentHashMap<>(); + protected final String modId; public System(String modId) { this.modId = modId; } - public String getModId() { - return modId; - } - public static List getInstances(String modId) { return instances.get(modId); } + + public String getModId() { + return modId; + } } \ No newline at end of file diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/Util.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/Util.java index 0bfe69b..d1e04c4 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/Util.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/Util.java @@ -3,10 +3,6 @@ import com.tanishisherewith.dynamichud.newTrial.DynamicHUD; public class Util { - public enum Quadrant { - UPPER_LEFT, UPPER_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT - } - public static Quadrant getQuadrant(int x, int y) { int screenWidth = DynamicHUD.MC.getWindow().getScaledWidth(); int screenHeight = DynamicHUD.MC.getWindow().getScaledHeight(); @@ -26,4 +22,8 @@ public static Quadrant getQuadrant(int x, int y) { } } + public enum Quadrant { + UPPER_LEFT, UPPER_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT + } + } 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 398037c..2f1d401 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 @@ -8,32 +8,32 @@ import java.util.List; public class ContextMenu { - public int x,y; - protected float scale = 0.0f; private final List> options = new ArrayList<>(); // The list of options in the context menu + public int x, y; public int width = 0; public int height = 0; public int backgroundColor = new Color(107, 112, 126, 124).getRGB();// Semi-transparent light grey color public int padding = 5; // The amount of padding around the rectangle public int heightOffset = 4; // Height offset from the widget public boolean shouldDisplay = false; + protected float scale = 0.0f; - public ContextMenu(int x,int y){ + public ContextMenu(int x, int y) { this.x = x; this.y = y + heightOffset; } - public void addOption(Option option){ + public void addOption(Option option) { options.add(option); } - public void render(DrawContext drawContext, int x, int y, int height){ + public void render(DrawContext drawContext, int x, int y, int height) { this.x = x; this.y = y + heightOffset + height; - if(!shouldDisplay) return; + if (!shouldDisplay) return; update(); - DrawHelper.scaleAndPosition(drawContext.getMatrices(),x,y,scale); + DrawHelper.scaleAndPosition(drawContext.getMatrices(), x, y, scale); int x1 = this.x - 1; int y1 = this.y; @@ -45,8 +45,8 @@ public void render(DrawContext drawContext, int x, int y, int height){ int yOffset = y1 + 3; this.width = 10; - for(Option option: options){ - option.render(drawContext,x + 2,yOffset); + for (Option option : options) { + option.render(drawContext, x + 2, yOffset); this.width = Math.max(this.width, option.width + padding); yOffset += option.height + 1; } @@ -55,6 +55,7 @@ public void render(DrawContext drawContext, int x, int y, int height){ DrawHelper.stopScaling(drawContext.getMatrices()); } + public void update() { // Update the scale float scaleSpeed = 0.1f; @@ -64,49 +65,55 @@ public void update() { } } - public void close(){ + public void close() { shouldDisplay = false; scale = 0.0f; } - public void open(){ + + public void open() { shouldDisplay = true; update(); } - public void toggleDisplay(){ - if(shouldDisplay){ + + public void toggleDisplay() { + if (shouldDisplay) { close(); - }else{ + } else { open(); } } - public void mouseClicked(double mouseX, double mouseY, int button){ - if(!shouldDisplay) return; - for(Option option: options){ + public void mouseClicked(double mouseX, double mouseY, int button) { + if (!shouldDisplay) return; + for (Option option : options) { option.mouseClicked(mouseX, mouseY, button); } } - public void mouseReleased(double mouseX, double mouseY, int button){ - if(!shouldDisplay) return; - for(Option option: options){ + + public void mouseReleased(double mouseX, double mouseY, int button) { + if (!shouldDisplay) return; + for (Option option : options) { option.mouseReleased(mouseX, mouseY, button); } } - public void mouseDragged(double mouseX, double mouseY, int button){ - if(!shouldDisplay) return; - for(Option option: options){ + + public void mouseDragged(double mouseX, double mouseY, int button) { + if (!shouldDisplay) return; + for (Option option : options) { option.mouseDragged(mouseX, mouseY, button); } } - public void keyPressed(int key){ - if(!shouldDisplay) return; - for(Option option: options){ + + public void keyPressed(int key) { + if (!shouldDisplay) return; + for (Option option : options) { option.keyPressed(key); } } - public void keyReleased(int key){ - if(!shouldDisplay) return; - for(Option option: options){ + + public void keyReleased(int key) { + if (!shouldDisplay) return; + for (Option option : options) { option.keyReleased(key); } } 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 2c4907e..676af06 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 @@ -8,52 +8,58 @@ import java.util.function.Supplier; public abstract class Option { - public int x,y; - protected float scale = 0.0f; - private Widget selectedWidget; // The widget that this context menu is associated with + public int x, y; public int width = 0; public int height = 0; - protected Supplier getter; - protected Consumer setter; public T value = null; + protected float scale = 0.0f; + protected Supplier getter; + protected Consumer setter; protected T defaultValue = null; protected MinecraftClient mc = MinecraftClient.getInstance(); + private Widget selectedWidget; // The widget that this context menu is associated with - public Option(Supplier getter, Consumer setter){ + public Option(Supplier getter, Consumer setter) { this.getter = getter; this.setter = setter; value = get(); defaultValue = get(); } - protected T get(){ + protected T get() { return getter.get(); } - protected void set(T value){ + + protected void set(T value) { this.value = value; setter.accept(value); } - public void render(DrawContext drawContext, int x, int y){ + public void render(DrawContext drawContext, int x, int y) { this.x = x; this.y = y; } - public boolean mouseClicked(double mouseX, double mouseY, int button){ + public boolean mouseClicked(double mouseX, double mouseY, int button) { return isMouseOver(mouseX, mouseY); } - public boolean mouseReleased(double mouseX, double mouseY, int button){ + + public boolean mouseReleased(double mouseX, double mouseY, int button) { return isMouseOver(mouseX, mouseY); } - public boolean mouseDragged(double mouseX, double mouseY, int button){ + + public boolean mouseDragged(double mouseX, double mouseY, int button) { return isMouseOver(mouseX, mouseY); } - public void keyPressed(int key){ + + public void keyPressed(int key) { } - public void keyReleased(int key){ + + public void keyReleased(int key) { } + public boolean isMouseOver(double mouseX, double mouseY) { return mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height; } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/BooleanOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/BooleanOption.java index d7f9fb6..bc23270 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/BooleanOption.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/BooleanOption.java @@ -22,7 +22,7 @@ public void render(DrawContext drawContext, int x, int y) { value = get(); int color = value ? Color.GREEN.getRGB() : Color.RED.getRGB(); - drawContext.drawText(mc.textRenderer,Text.of(name),x,y, color,false); + drawContext.drawText(mc.textRenderer, Text.of(name), x, y, color, false); this.height = mc.textRenderer.fontHeight; this.width = mc.textRenderer.getWidth(name) + 1; } @@ -30,7 +30,7 @@ public void render(DrawContext drawContext, int x, int y) { @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { super.mouseClicked(mouseX, mouseY, button); - if(isMouseOver(mouseX, mouseY)) { + if (isMouseOver(mouseX, mouseY)) { value = !value; set(value); } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/DoubleOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/DoubleOption.java index 644f2ed..05a6319 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/DoubleOption.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/DoubleOption.java @@ -12,10 +12,11 @@ public class DoubleOption extends Option { public String name = "Empty"; - private boolean isDragging = false; - private double minValue = 0.0,maxValue = 0.0; float step = 0.1f; - public DoubleOption(String name,double minValue,double maxValue,float step,Supplier getter, Consumer setter) { + private boolean isDragging = false; + private double minValue = 0.0, maxValue = 0.0; + + public DoubleOption(String name, double minValue, double maxValue, float step, Supplier getter, Consumer setter) { super(getter, setter); this.name = name; this.value = get(); @@ -24,8 +25,9 @@ public DoubleOption(String name,double minValue,double maxValue,float step,Suppl this.width = 30; this.height = 16; this.step = step; - Validate.isTrue(this.step>0.0f,"Step cannot be less than or equal to 0 (zero)"); + Validate.isTrue(this.step > 0.0f, "Step cannot be less than or equal to 0 (zero)"); } + @Override public void render(DrawContext drawContext, int x, int y) { super.render(drawContext, x, y); @@ -35,10 +37,10 @@ public void render(DrawContext drawContext, int x, int y) { this.height = 16; // Draw the label TextRenderer textRenderer = mc.textRenderer; - DrawHelper.scaleAndPosition(drawContext.getMatrices(),x,y,0.7f); + DrawHelper.scaleAndPosition(drawContext.getMatrices(), x, y, 0.7f); String labelText = name + ": " + String.format("%.1f", value); int labelWidth = textRenderer.getWidth(labelText); - this.width = Math.max(this.width,labelWidth); + this.width = Math.max(this.width, labelWidth); drawContext.drawTextWithShadow(textRenderer, labelText, x, y + 1, 0xFFFFFFFF); DrawHelper.stopScaling(drawContext.getMatrices()); @@ -53,10 +55,10 @@ public void render(DrawContext drawContext, int x, int y) { // Draw the handle DrawHelper.drawRoundedRectangleWithShadowBadWay(drawContext.getMatrices().peek().getPositionMatrix(), - (float)handleX, - (float)handleY, - handleWidth, - handleHeight, + (float) handleX, + (float) handleY, + handleWidth, + handleHeight, 1, 0xFFFFFFFF, 90, @@ -65,8 +67,8 @@ public void render(DrawContext drawContext, int x, int y) { } private void drawSlider(DrawContext drawContext, int sliderX, int sliderY, int sliderWidth, double handleX) { - DrawHelper.drawRectangle(drawContext.getMatrices().peek().getPositionMatrix(), sliderX, sliderY, sliderWidth, 2, 0xFFFFFFFF); - if(handleX-sliderX > 0) { + DrawHelper.drawRectangle(drawContext.getMatrices().peek().getPositionMatrix(), sliderX, sliderY, sliderWidth, 2, 0xFFFFFFFF); + if (handleX - sliderX > 0) { DrawHelper.drawRectangle(drawContext.getMatrices().peek().getPositionMatrix(), (float) sliderX, (float) sliderY, (float) ((value - minValue) / (maxValue - minValue) * (width - 3)), 2, Color.ORANGE.getRGB()); } } @@ -74,7 +76,7 @@ private void drawSlider(DrawContext drawContext, int sliderX, int sliderY, int s @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { super.mouseClicked(mouseX, mouseY, button); - if(isMouseOver(mouseX, mouseY)) { + if (isMouseOver(mouseX, mouseY)) { step(mouseX); isDragging = true; } @@ -86,7 +88,8 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) { isDragging = false; return super.mouseReleased(mouseX, mouseY, button); } - private void step(double mouseX){ + + private void step(double mouseX) { double newValue = minValue + (float) (mouseX - x) / width * (maxValue - minValue); // Round the new value to the nearest step newValue = Math.round(newValue / step) * step; @@ -95,7 +98,7 @@ private void step(double mouseX){ @Override public boolean mouseDragged(double mouseX, double mouseY, int button) { - if(isMouseOver(mouseX, mouseY) && isDragging) { + if (isMouseOver(mouseX, mouseY) && isDragging) { step(mouseX); } return super.mouseDragged(mouseX, mouseY, button); diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/EnumOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/EnumOption.java index dde3f95..5cd0725 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/EnumOption.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/EnumOption.java @@ -1,18 +1,17 @@ package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options; -import com.tanishisherewith.dynamichud.helpers.DrawHelper; import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option; import net.minecraft.client.gui.DrawContext; import net.minecraft.text.Text; -import java.awt.Color; +import java.awt.*; import java.util.function.Consumer; import java.util.function.Supplier; public class EnumOption> extends Option { private final E[] values; - private int currentIndex = 0; public String name = "Empty"; + private int currentIndex = 0; public EnumOption(String name, Supplier getter, Consumer setter, E[] values) { super(getter, setter); @@ -43,15 +42,15 @@ public void render(DrawContext drawContext, int x, int y) { public boolean mouseClicked(double mouseX, double mouseY, int button) { super.mouseClicked(mouseX, mouseY, button); if (isMouseOver(mouseX, mouseY)) { - if(button == 0) { + if (button == 0) { currentIndex = (currentIndex + 1) % values.length; - if(currentIndex > values.length - 1){ + if (currentIndex > values.length - 1) { currentIndex = 0; } value = values[currentIndex]; - }else if(button == 1){ + } else if (button == 1) { currentIndex = (currentIndex - 1) % values.length; - if(currentIndex < 0){ + if (currentIndex < 0) { currentIndex = values.length - 1; } value = values[currentIndex]; diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/ListOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/ListOption.java index 3e69407..3622dd3 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/ListOption.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/ListOption.java @@ -1,20 +1,18 @@ package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options; -import com.tanishisherewith.dynamichud.helpers.DrawHelper; import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option; import net.minecraft.client.gui.DrawContext; import net.minecraft.text.Text; import java.awt.*; -import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; import java.util.function.Supplier; public class ListOption extends Option { private final List values; - private int currentIndex = 0; public String name = "Empty"; + private int currentIndex = 0; public ListOption(String name, Supplier getter, Consumer setter, List values) { super(getter, setter); @@ -46,15 +44,15 @@ public void render(DrawContext drawContext, int x, int y) { public boolean mouseClicked(double mouseX, double mouseY, int button) { super.mouseClicked(mouseX, mouseY, button); if (isMouseOver(mouseX, mouseY)) { - if(button == 0) { + if (button == 0) { currentIndex = (currentIndex + 1) % values.size(); - if(currentIndex > values.size() - 1){ + if (currentIndex > values.size() - 1) { currentIndex = 0; } value = values.get(currentIndex); - }else if(button == 1){ + } else if (button == 1) { currentIndex = (currentIndex - 1) % values.size(); - if(currentIndex < 0){ + if (currentIndex < 0) { currentIndex = values.size() - 1; } value = values.get(currentIndex); diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/RunnableOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/RunnableOption.java index c62fc3b..43d230e 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/RunnableOption.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/RunnableOption.java @@ -10,16 +10,17 @@ public class RunnableOption extends Option { public String name = "Empty"; - private Runnable task; + private final Runnable task; /** * Runnable option which runs a task when clicked on it. - * @param name The name to be displayed + * + * @param name The name to be displayed * @param getter Get a default value to run the task by default * @param setter Return a boolean based on if the task is running or not. - * @param task The task to run + * @param task The task to run */ - public RunnableOption(String name, Supplier getter, Consumer setter,Runnable task) { + public RunnableOption(String name, Supplier getter, Consumer setter, Runnable task) { super(getter, setter); this.name = "Run: " + name; // prepend the "run" symbol to the name this.task = task; @@ -33,16 +34,16 @@ public void render(DrawContext drawContext, int x, int y) { this.height = mc.textRenderer.fontHeight; this.width = mc.textRenderer.getWidth("Run: " + name) + 1; int color = value ? Color.BLUE.getRGB() : Color.GRAY.getRGB(); - drawContext.drawText(mc.textRenderer,Text.of(name),x,y, color,false); + drawContext.drawText(mc.textRenderer, Text.of(name), x, y, color, false); } @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { super.mouseClicked(mouseX, mouseY, button); - if(isMouseOver(mouseX, mouseY)) { + if (isMouseOver(mouseX, mouseY)) { value = !value; set(value); - if(value){ + if (value) { task.run(); } } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/AlphaSlider.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/AlphaSlider.java index 4193f9e..b85fbea 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/AlphaSlider.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/AlphaSlider.java @@ -3,7 +3,6 @@ import com.tanishisherewith.dynamichud.newTrial.helpers.ColorHelper; import com.tanishisherewith.dynamichud.newTrial.helpers.DrawHelper; import net.minecraft.client.gui.DrawContext; -import net.minecraft.util.math.MathHelper; import java.awt.*; @@ -16,7 +15,8 @@ public class AlphaSlider { private Color color; private int alphaHandleY = 0; private float alpha; - public AlphaSlider(int x,int y,int width, int height, Color color) { + + public AlphaSlider(int x, int y, int width, int height, Color color) { this.width = width; this.height = height; this.color = color; @@ -25,29 +25,30 @@ public AlphaSlider(int x,int y,int width, int height, Color color) { alpha = color.getAlpha() / 255f; } - public void setColor(Color color) { - this.color = color; - } public void render(DrawContext drawContext, int x, int y) { this.x = x; this.y = y; - DrawHelper.drawOutlinedBox(drawContext,x - 2,y - 2,x + width + 2,y + height + 2,Color.WHITE.getRGB()); - DrawHelper.drawGradient(drawContext.getMatrices().peek().getPositionMatrix(),x,y,width,height,color.getRGB(), ColorHelper.changeAlpha(color,0).getRGB(), DrawHelper.Direction.TOP_BOTTOM); - drawContext.fill(x-2, y + alphaHandleY-1,x+width+2, y +alphaHandleY+1,Color.WHITE.getRGB()); + DrawHelper.drawOutlinedBox(drawContext, x - 2, y - 2, x + width + 2, y + height + 2, Color.WHITE.getRGB()); + DrawHelper.drawGradient(drawContext.getMatrices().peek().getPositionMatrix(), x, y, width, height, color.getRGB(), ColorHelper.changeAlpha(color, 0).getRGB(), DrawHelper.Direction.TOP_BOTTOM); + drawContext.fill(x - 2, y + alphaHandleY - 1, x + width + 2, y + alphaHandleY + 1, Color.WHITE.getRGB()); } public Color getColor() { - return ColorHelper.changeAlpha(color, (int) (alpha*255f)); + return ColorHelper.changeAlpha(color, (int) (alpha * 255f)); + } + + public void setColor(Color color) { + this.color = color; } public void onClick(double mouseX, double mouseY, int button) { if (button == 0 && isMouseOver(mouseX, mouseY)) { - alphaHandleY = (int) mouseY-y; + alphaHandleY = (int) mouseY - y; alpha = 1.0f - (alphaHandleY / (float) height); - if(alpha < 0.0f){ + if (alpha < 0.0f) { alpha = 0.0f; - }else if(alpha > 1.0f){ + } else if (alpha > 1.0f) { alpha = 1.0f; } this.isDragging = true; @@ -74,11 +75,11 @@ public void onRelease(double mouseX, double mouseY, int button) { public void onDrag(double mouseX, double mouseY, int button) { if (isDragging && isMouseOver(mouseX, mouseY)) { - alphaHandleY = (int) mouseY-y; + alphaHandleY = (int) mouseY - y; alpha = 1.0f - (alphaHandleY / (float) height); - if(alpha < 0.0f){ + if (alpha < 0.0f) { alpha = 0.0f; - }else if(alpha > 1.0f){ + } else if (alpha > 1.0f) { alpha = 1.0f; } } 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 afc4880..b9e69f8 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 @@ -1,6 +1,5 @@ package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.coloroption; -import com.tanishisherewith.dynamichud.widget.Widget; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gl.Framebuffer; import net.minecraft.client.gui.DrawContext; @@ -9,18 +8,17 @@ import java.awt.*; import java.nio.ByteBuffer; -import java.util.Arrays; import java.util.function.Consumer; public class ColorGradientPicker { - MinecraftClient client = MinecraftClient.getInstance(); private final Consumer onColorSelected; // The callback to call when a color is selected private final GradientSlider gradientSlider; private final GradientBox gradientBox; private final ColorPickerButton colorPickerButton; private final AlphaSlider alphaSlider; - private int x,y; private final int boxSize; + MinecraftClient client = MinecraftClient.getInstance(); + private int x, y; private boolean display = false; public ColorGradientPicker(int x, int y, Color initialColor, Consumer onColorSelected, int boxSize, int colors) { @@ -37,29 +35,32 @@ public ColorGradientPicker(int x, int y, Color initialColor, Consumer onC this.gradientBox.setSaturation(hsv[1]); this.gradientBox.setValue(hsv[2]); - this.alphaSlider = new AlphaSlider(x,y,10,boxSize,initialColor); + this.alphaSlider = new AlphaSlider(x, y, 10, boxSize, initialColor); this.colorPickerButton = new ColorPickerButton(x + boxSize + 8, y + 20, 30, 18); } - public void setPos(int x, int y){ + + public void setPos(int x, int y) { this.x = x; this.y = y; } + public void display() { display = true; } + public void close() { display = false; } public void render(DrawContext drawContext, int x1, int y1) { - setPos(x1,y1); - if(!display){ + setPos(x1, y1); + if (!display) { return; } - gradientSlider.render(drawContext,x + 30, y +client.textRenderer.fontHeight + 4); - gradientBox.render(drawContext,x + 30, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 10); - colorPickerButton.render(drawContext,x+ 55 + boxSize,y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 8); - alphaSlider.render(drawContext,x + 40 + boxSize,y + client.textRenderer.fontHeight +gradientSlider.getHeight() + 10); + gradientSlider.render(drawContext, x + 30, y + client.textRenderer.fontHeight + 4); + gradientBox.render(drawContext, x + 30, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 10); + colorPickerButton.render(drawContext, x + 55 + boxSize, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 8); + alphaSlider.render(drawContext, x + 40 + boxSize, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 10); if (colorPickerButton.isPicking()) { // Draw the cursor @@ -82,7 +83,7 @@ public void render(DrawContext drawContext, int x1, int y1) { } public boolean mouseClicked(double mouseX, double mouseY, int button) { - if(!display){ + if (!display) { return false; } if (colorPickerButton.onClick(mouseX, mouseY, button)) { @@ -124,7 +125,7 @@ public void mouseReleased(double mouseX, double mouseY, int button) { } public void mouseDragged(double mouseX, double mouseY, int button) { - if(!display){ + if (!display) { return; } gradientSlider.onDrag(mouseX, mouseY, button); diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorOption.java index bf870c6..9d90874 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorOption.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorOption.java @@ -11,15 +11,16 @@ import java.util.function.Supplier; public class ColorOption extends Option { - public String name = "Empty"; - public boolean isVisible = false; - private ColorGradientPicker colorPicker = null; + public String name = "Empty"; + public boolean isVisible = false; public ContextMenu parentMenu = null; - public ColorOption(String name,ContextMenu parentMenu, Supplier getter, Consumer setter) { + private ColorGradientPicker colorPicker = null; + + public ColorOption(String name, ContextMenu parentMenu, Supplier getter, Consumer setter) { super(getter, setter); this.name = name; this.parentMenu = parentMenu; - colorPicker = new ColorGradientPicker(x + this.parentMenu.width + 10,y - 10,value, this::set,50,100 ); + colorPicker = new ColorGradientPicker(x + this.parentMenu.width + 10, y - 10, value, this::set, 50, 100); } @Override @@ -29,7 +30,7 @@ public void render(DrawContext drawContext, int x, int y) { int color = isVisible ? Color.GREEN.getRGB() : Color.RED.getRGB(); this.height = mc.textRenderer.fontHeight; this.width = mc.textRenderer.getWidth(name) + 12; - drawContext.drawText(mc.textRenderer, Text.of(name),x,y, color,false); + drawContext.drawText(mc.textRenderer, Text.of(name), x, y, color, false); DrawHelper.drawRoundedRectangleWithShadowBadWay(drawContext.getMatrices().peek().getPositionMatrix(), x + width - 8, y, @@ -39,24 +40,23 @@ public void render(DrawContext drawContext, int x, int y) { value.getRGB(), 90, 1, - 1 ); + 1); - colorPicker.render(drawContext,this.x + width/3 + parentMenu.width + 10,y - 10); + colorPicker.render(drawContext, this.x + width / 3 + parentMenu.width + 10, y - 10); } @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { - if(isMouseOver(mouseX, mouseY)) { + if (isMouseOver(mouseX, mouseY)) { isVisible = !isVisible; - if(isVisible) - { - colorPicker.setPos(x + parentMenu.width + 10,y - 10); + if (isVisible) { + colorPicker.setPos(x + parentMenu.width + 10, y - 10); colorPicker.display(); - }else{ + } else { colorPicker.close(); } } - colorPicker.mouseClicked(mouseX,mouseY,button); + colorPicker.mouseClicked(mouseX, mouseY, button); return super.mouseClicked(mouseX, mouseY, button); } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorPickerButton.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorPickerButton.java index 149c595..39f541e 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorPickerButton.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/coloroption/ColorPickerButton.java @@ -4,10 +4,10 @@ import net.minecraft.client.gui.DrawContext; public class ColorPickerButton { - private int x; - private int y; private final int width; private final int height; + private int x; + private int y; private boolean isPicking = false; public ColorPickerButton(int x, int y, int width, int height) { @@ -21,7 +21,7 @@ public void render(DrawContext drawContext, int x, int y) { this.x = x; this.y = y; drawContext.getMatrices().push(); - drawContext.getMatrices().translate(0,0,404); + drawContext.getMatrices().translate(0, 0, 404); // Draw the button drawContext.fill(x + 2, y + 2, x + width - 2, y + height - 2, 0xFFAAAAAA); drawContext.drawCenteredTextWithShadow(MinecraftClient.getInstance().textRenderer, "Pick", x + width / 2, y + (height - 8) / 2, 0xFFFFFFFF); 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 e1b5fc8..ce0f0f9 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,8 +1,6 @@ package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.coloroption; 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.*; @@ -22,14 +20,15 @@ public GradientBox(int x, int y, int size) { this.y = y; this.size = size; } + public void render(DrawContext drawContext, int x, int y) { - setPosition(x,y); + setPosition(x, y); drawContext.getMatrices().push(); - drawContext.getMatrices().translate(0,0,406); + drawContext.getMatrices().translate(0, 0, 406); DrawHelper.drawOutlinedBox(drawContext, x - 2, y - 2, x + size + 2, y + size + 2, -1); // Draw the gradient - com.tanishisherewith.dynamichud.newTrial.helpers.DrawHelper.drawRoundedGradientRectangle(drawContext.getMatrices().peek().getPositionMatrix(), Color.BLACK, Color.BLACK, Color.getHSBColor(hue, 1.0f, 1.0f), Color.WHITE, x, y, size, size,2); + com.tanishisherewith.dynamichud.newTrial.helpers.DrawHelper.drawRoundedGradientRectangle(drawContext.getMatrices().peek().getPositionMatrix(), Color.BLACK, Color.BLACK, Color.getHSBColor(hue, 1.0f, 1.0f), Color.WHITE, x, y, size, size, 2); // Draw the handle float handleSize = 3; 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 8aa7e32..60498df 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,8 +1,6 @@ package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.coloroption; 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.*; @@ -35,9 +33,9 @@ public void setPosition(int x, int y) { } public void render(DrawContext drawContext, int x, int y) { - setPosition(x,y); + setPosition(x, y); drawContext.getMatrices().push(); - drawContext.getMatrices().translate(0,0,401); + drawContext.getMatrices().translate(0, 0, 401); DrawHelper.drawOutlinedBox(drawContext, x - 2, y - 2, x + width + 2, y + height + 2, -1); // Draw the gradient @@ -51,12 +49,12 @@ public void render(DrawContext drawContext, int x, int y) { // Draw the handle - float handleWidth = 3; - float handleHeight = height + 4; - float handleX = x + hue * width - handleWidth / 2.0f; - float handleY = y - (handleHeight - height) / 2.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); + DrawHelper.fillRoundedRect(drawContext, (int) handleX, (int) handleY, (int) (handleX + handleWidth), (int) (handleY + handleHeight), -1); drawContext.getMatrices().pop(); } diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/Widget.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/Widget.java index a5abc47..8512958 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/Widget.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/Widget.java @@ -14,12 +14,12 @@ public abstract class Widget { - protected MinecraftClient mc = MinecraftClient.getInstance(); - + public static WidgetData DATA; /** * This is the UID of the widget used to identify during loading and saving. *

* It's different from modID because this is unique to each widget. + * * @see #modId */ public UID uid = UID.generate(); @@ -33,35 +33,32 @@ public abstract class Widget { //To enable/disable snapping public boolean shiftDown = false; - int startX, startY; - // Absolute position of the widget on screen in pixels. public int x, y; - + public boolean shouldScale = true; + /** + * An identifier for widgets to group them under one ID. + *

+ * Doesn't necessarily have to be the mod ID of mod, but it's preferred to use mod ID if you are only grouping widgets under one ID. + * Can be any string if wanted. + * + * @see #uid + */ + public String modId = "unknown"; + protected MinecraftClient mc = MinecraftClient.getInstance(); // The x position of the widget as a percentage of the screen width, i.e. the relative x position of the widget for resizing and scaling protected float xPercent; // The y position of the widget as a percentage of the screen height, i.e. the relative y position of the widget for resizing and scaling protected float yPercent; - public boolean shouldScale = true; - /** * Scale of the current widget. + * * @see GlobalConfig#scale */ protected float scale = 1.0f; - - /** - * An identifier for widgets to group them under one ID. - *

- * Doesn't necessarily have to be the mod ID of mod, but it's preferred to use mod ID if you are only grouping widgets under one ID. - * Can be any string if wanted. - * @see #uid - */ - public String modId = "unknown"; - //Dimensions of the widget protected WidgetBox widgetBox; - public static WidgetData DATA; + int startX, startY; public Widget(WidgetData DATA, String modId) { Widget.DATA = DATA; @@ -104,8 +101,8 @@ public float getHeight() { } public void setPosition(int x, int y) { - this.x = x; - this.y = y; + this.x = x; + this.y = y; } public void setDraggable(boolean draggable) { @@ -134,9 +131,9 @@ public void render(DrawContext drawContext, int mouseX, int mouseY) { if (!shouldDisplay()) return; if (shouldScale) { - DrawHelper.scaleAndPosition(drawContext.getMatrices(), getX(), getY(),GlobalConfig.get().scale); + DrawHelper.scaleAndPosition(drawContext.getMatrices(), getX(), getY(), GlobalConfig.get().scale); } - renderWidget(drawContext,mouseX,mouseY); + renderWidget(drawContext, mouseX, mouseY); if (shouldScale) { DrawHelper.stopScaling(drawContext.getMatrices()); @@ -151,9 +148,9 @@ public void renderInEditor(DrawContext drawContext, int mouseX, int mouseY) { displayBg(drawContext); if (shouldScale) { - DrawHelper.scaleAndPosition(drawContext.getMatrices(), getX(), getY(),GlobalConfig.get().scale); + DrawHelper.scaleAndPosition(drawContext.getMatrices(), getX(), getY(), GlobalConfig.get().scale); } - renderWidgetInEditor(drawContext,mouseX,mouseY); + renderWidgetInEditor(drawContext, mouseX, mouseY); if (shouldScale) { DrawHelper.stopScaling(drawContext.getMatrices()); @@ -168,10 +165,10 @@ public void renderInEditor(DrawContext drawContext, int mouseX, int mouseY) { *

* * @param context - * @param mouseX X position of mouse. - * @param mouseY Y position of mouse + * @param mouseX X position of mouse. + * @param mouseY Y position of mouse */ - public abstract void renderWidget(DrawContext context,int mouseX, int mouseY); + public abstract void renderWidget(DrawContext context, int mouseX, int mouseY); /** @@ -181,10 +178,10 @@ public void renderInEditor(DrawContext drawContext, int mouseX, int mouseY) { * * @param context */ - private void renderWidgetInEditor(DrawContext context,int mouseX, int mouseY) { + private void renderWidgetInEditor(DrawContext context, int mouseX, int mouseY) { //displayBg(context); - renderWidget(context,mouseX,mouseY); + renderWidget(context, mouseX, mouseY); } public boolean mouseClicked(double mouseX, double mouseY, int button) { @@ -220,7 +217,7 @@ public boolean mouseDragged(double mouseX, double mouseY, int button, int snapSi this.y = (int) MathHelper.clamp(newY, 0, mc.getWindow().getScaledHeight() - getHeight()); this.xPercent = (float) this.x / mc.getWindow().getScaledWidth(); - this.yPercent = (float) this.y / mc.getWindow().getScaledHeight(); + this.yPercent = (float) this.y / mc.getWindow().getScaledHeight(); return true; } @@ -237,14 +234,15 @@ public void mouseReleased(double mouseX, double mouseY, int button) { * @param vAmount vertical amount of scrolling * @param hAmount horizontal amount of scrolling */ - public void mouseScrolled(double mouseX, double mouseY, double vAmount,double hAmount) { + public void mouseScrolled(double mouseX, double mouseY, double vAmount, double hAmount) { } public boolean toggle() { return this.display = !this.display; } - public void onClose(){ + + public void onClose() { this.shiftDown = false; } @@ -257,7 +255,7 @@ public void onClose(){ protected void displayBg(DrawContext context) { int backgroundColor = this.shouldDisplay() ? ColorHelper.getColor(0, 0, 0, 128) : ColorHelper.getColor(255, 0, 0, 128); WidgetBox box = this.getWidgetBox(); - DrawHelper.drawRectangle(context.getMatrices().peek().getPositionMatrix(), + DrawHelper.drawRectangle(context.getMatrices().peek().getPositionMatrix(), box.x, box.y, box.getWidth(), diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetBox.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetBox.java index 4ac9f25..0082496 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetBox.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetBox.java @@ -1,9 +1,9 @@ package com.tanishisherewith.dynamichud.newTrial.widget; public class WidgetBox { + public float x = 0, y = 0; private float width; private float height; - public float x = 0, y = 0; public WidgetBox(int x, int y, int width, int height) { this.x = x; @@ -45,22 +45,24 @@ public boolean intersects(WidgetBox other) { return !(this.y + this.height < other.y); } - public void setSizeAndPosition(float x, float y, float width, float height){ + public void setSizeAndPosition(float x, float y, float width, float height) { this.x = x; this.y = y; this.height = height; this.width = width; } - public void setSizeAndPosition(float x, float y, float width, float height,boolean shouldScale, float scale){ + + public void setSizeAndPosition(float x, float y, float width, float height, boolean shouldScale, float scale) { this.x = x; this.y = y; - this.height = height * (shouldScale? scale : 1.0f); - this.width = width * (shouldScale? scale : 1.0f); + this.height = height * (shouldScale ? scale : 1.0f); + this.width = width * (shouldScale ? scale : 1.0f); } - public void setSize(double width, double height,boolean shouldScale, float scale) { + + public void setSize(double width, double height, boolean shouldScale, float scale) { if (width >= 0) - this.width = (int) Math.ceil(width * (shouldScale? scale : 1.0f)); + this.width = (int) Math.ceil(width * (shouldScale ? scale : 1.0f)); if (height >= 0) - this.height = (int) Math.ceil(height * (shouldScale? scale : 1.0f)); + this.height = (int) Math.ceil(height * (shouldScale ? scale : 1.0f)); } } 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 08758df..75208a4 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetManager.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetManager.java @@ -1,16 +1,22 @@ package com.tanishisherewith.dynamichud.newTrial.widget; -import java.io.*; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; -import java.util.*; - import com.tanishisherewith.dynamichud.DynamicHUD; import net.fabricmc.fabric.api.util.NbtType; -import net.minecraft.nbt.*; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtIo; +import net.minecraft.nbt.NbtList; import net.minecraft.util.math.MathHelper; -import static com.tanishisherewith.dynamichud.DynamicHUD.*; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.util.*; + +import static com.tanishisherewith.dynamichud.DynamicHUD.printInfo; +import static com.tanishisherewith.dynamichud.DynamicHUD.printWarn; /** * Manages a collection of widgets, providing methods to add, remove, save, and load widgets. @@ -31,8 +37,8 @@ public class WidgetManager { * * @param data The WidgetData object to add. */ - public static void addWidgetData(WidgetData data){ - widgetDataMap.put(data.name(),data); + public static void addWidgetData(WidgetData data) { + widgetDataMap.put(data.name(), data); } /** @@ -40,8 +46,8 @@ public static void addWidgetData(WidgetData data){ * * @param widgetDatas The WidgetData objects to add. */ - public static void addWidgetDatas(WidgetData... widgetDatas){ - for(WidgetData data: widgetDatas) { + public static void addWidgetDatas(WidgetData... widgetDatas) { + for (WidgetData data : widgetDatas) { widgetDataMap.put(data.name(), data); } } @@ -106,41 +112,41 @@ public static void removeWidget(Widget widget) { * Larger the GUI scale, more accurate is the position. *

*

- * Called in {@link com.tanishisherewith.dynamichud.mixins.ScreenMixin} + * Called in {@link com.tanishisherewith.dynamichud.mixins.ScreenMixin} *

*

* - * @param newWidth Screen width after resize - * @param newHeight Screen height after resize - * @param previousWidth Screen width before resize + * @param newWidth Screen width after resize + * @param newHeight Screen height after resize + * @param previousWidth Screen width before resize * @param previousHeight Screen height before resize */ - public static void onScreenResized(int newWidth, int newHeight, int previousWidth, int previousHeight) { - for (Widget widget : widgets) { - // To ensure that infinite coords is not returned - if(widget.xPercent <= 0.0f){ - widget.xPercent = (float) widget.getX()/previousWidth; - } - if(widget.yPercent <= 0.0f){ - widget.yPercent = (float) widget.getY()/previousHeight; - } - - // Use the stored percentages to calculate the new position - float newX = widget.xPercent * newWidth; - float newY = widget.yPercent * newHeight; - - // Ensure the widget is within the screen bounds - newX = MathHelper.clamp(newX, 0, newWidth - widget.getWidth()); - newY = MathHelper.clamp(newY, 0, newHeight - widget.getHeight()); - - // Update the widget's position - widget.setPosition((int) newX, (int) newY); - - // Update the stored percentages with the new screen size (after resize). - widget.xPercent = (float) widget.getX() / newWidth; - widget.yPercent = (float) widget.getY() / newHeight; - } - } + public static void onScreenResized(int newWidth, int newHeight, int previousWidth, int previousHeight) { + for (Widget widget : widgets) { + // To ensure that infinite coords is not returned + if (widget.xPercent <= 0.0f) { + widget.xPercent = (float) widget.getX() / previousWidth; + } + if (widget.yPercent <= 0.0f) { + widget.yPercent = (float) widget.getY() / previousHeight; + } + + // Use the stored percentages to calculate the new position + float newX = widget.xPercent * newWidth; + float newY = widget.yPercent * newHeight; + + // Ensure the widget is within the screen bounds + newX = MathHelper.clamp(newX, 0, newWidth - widget.getWidth()); + newY = MathHelper.clamp(newY, 0, newHeight - widget.getHeight()); + + // Update the widget's position + widget.setPosition((int) newX, (int) newY); + + // Update the stored percentages with the new screen size (after resize). + widget.xPercent = (float) widget.getX() / newWidth; + widget.yPercent = (float) widget.getY() / newHeight; + } + } /** @@ -148,7 +154,7 @@ public static void onScreenResized(int newWidth, int newHeight, int previousWidt * * @param file The file to save to */ - public static void saveWidgets(File file,List widgets) throws IOException { + public static void saveWidgets(File file, List widgets) throws IOException { NbtCompound rootTag = new NbtCompound(); NbtList widgetList = new NbtList(); @@ -202,18 +208,18 @@ public static void loadWidgets(File file) throws IOException { widgets.clear(); if (file.exists()) { - NbtCompound rootTag = NbtIo.read(file.toPath()); - NbtList widgetList = rootTag.getList("widgets", NbtType.COMPOUND); - - for (int i = 0; i < widgetList.size(); i++) { - NbtCompound widgetTag = widgetList.getCompound(i); - WidgetData widgetData = widgetDataMap.get(widgetTag.getString("name")); - Widget widget = widgetData.createWidget(); - printInfo("Loading Widget: " + widget); - widget.readFromTag(widgetTag); - widgets.add(widget); - } - }else{ + NbtCompound rootTag = NbtIo.read(file.toPath()); + NbtList widgetList = rootTag.getList("widgets", NbtType.COMPOUND); + + for (int i = 0; i < widgetList.size(); i++) { + NbtCompound widgetTag = widgetList.getCompound(i); + WidgetData widgetData = widgetDataMap.get(widgetTag.getString("name")); + Widget widget = widgetData.createWidget(); + printInfo("Loading Widget: " + widget); + widget.readFromTag(widgetTag); + widgets.add(widget); + } + } else { printWarn("Widget File does not exist. Try saving one first"); } } @@ -226,6 +232,7 @@ public static void loadWidgets(File file) throws IOException { public static List getWidgets() { return widgets; } + /** * Returns the list of managed widgets with the same modID. * 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 f299f26..34baac0 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetRenderer.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widget/WidgetRenderer.java @@ -12,10 +12,10 @@ public class WidgetRenderer { public final List> allowedScreens = new CopyOnWriteArrayList<>(); - private boolean renderInGameHud = true; public boolean isInEditor = false; - List widgets; public Widget selectedWidget = null; + List widgets; + private boolean renderInGameHud = true; /** * Add the list of widgets the widgetRenderer should render @@ -24,15 +24,16 @@ public class WidgetRenderer { * * @param widgets List of widgets to render */ - public WidgetRenderer(List widgets){ + public WidgetRenderer(List widgets) { this.widgets = widgets; addScreen(GameMenuScreen.class); } - public void addWidget(Widget widget){ + + public void addWidget(Widget widget) { widgets.add(widget); } - public void addScreen(Class screen){ + public void addScreen(Class screen) { allowedScreens.add(screen); } @@ -41,93 +42,98 @@ public void shouldRenderInGameHud(boolean renderInGameHud) { } public void renderWidgets(DrawContext context, int mouseX, int mouseY) { - if(WidgetManager.getWidgets().isEmpty()) return; + if (WidgetManager.getWidgets().isEmpty()) return; Screen currentScreen = DynamicHUD.MC.currentScreen; - if(currentScreen == null && renderInGameHud){ + if (currentScreen == null && renderInGameHud) { for (Widget widget : widgets) { widget.isInEditor = false; - widget.render(context,0,0); + widget.render(context, 0, 0); } return; } - if(currentScreen instanceof AbstractMoveableScreen){ + if (currentScreen instanceof AbstractMoveableScreen) { for (Widget widget : widgets) { widget.isInEditor = true; - widget.renderInEditor(context,mouseX,mouseY); + widget.renderInEditor(context, mouseX, mouseY); } return; } if (currentScreen != null && allowedScreens.contains(DynamicHUD.MC.currentScreen.getClass()) && !this.isInEditor) { for (Widget widget : widgets) { widget.isInEditor = false; - widget.render(context,0,0); + widget.render(context, 0, 0); } } } - public void mouseClicked(double mouseX, double mouseY, int button){ + + public void mouseClicked(double mouseX, double mouseY, int button) { Screen currentScreen = DynamicHUD.MC.currentScreen; - if(currentScreen == null) { + if (currentScreen == null) { return; } - if(currentScreen instanceof AbstractMoveableScreen){ + if (currentScreen instanceof AbstractMoveableScreen) { for (Widget widget : widgets) { // This essentially acts as a Z - layer where the widget first in the list is moved and dragged // if they are overlapped on each other. - if(widget.mouseClicked(mouseX,mouseY,button)){ + if (widget.mouseClicked(mouseX, mouseY, button)) { selectedWidget = widget; return; } } } } - public void mouseDragged(double mouseX, double mouseY, int button, int snapSize){ + + public void mouseDragged(double mouseX, double mouseY, int button, int snapSize) { Screen currentScreen = DynamicHUD.MC.currentScreen; - if(currentScreen == null) { + if (currentScreen == null) { return; } - if(currentScreen instanceof AbstractMoveableScreen){ + if (currentScreen instanceof AbstractMoveableScreen) { for (Widget widget : widgets) { // This essentially acts as a Z - layer where the widget first in the list is moved and dragged // if they are overlapped on each other. - if(widget.mouseDragged(mouseX,mouseY,button,snapSize)){ + if (widget.mouseDragged(mouseX, mouseY, button, snapSize)) { selectedWidget = widget; return; } } } } - public void mouseScrolled(double mouseX, double mouseY, double vAmount,double hAmount){ + + public void mouseScrolled(double mouseX, double mouseY, double vAmount, double hAmount) { Screen currentScreen = DynamicHUD.MC.currentScreen; - if(currentScreen == null) { + if (currentScreen == null) { return; } - if(currentScreen instanceof AbstractMoveableScreen){ + if (currentScreen instanceof AbstractMoveableScreen) { for (Widget widget : widgets) { - widget.mouseScrolled(mouseX,mouseY,vAmount,hAmount); + widget.mouseScrolled(mouseX, mouseY, vAmount, hAmount); } } } - public void keyPressed(int keyCode){ + public void keyPressed(int keyCode) { Screen currentScreen = DynamicHUD.MC.currentScreen; - if(currentScreen instanceof AbstractMoveableScreen && (keyCode == GLFW.GLFW_KEY_LEFT_SHIFT || keyCode == GLFW.GLFW_KEY_RIGHT_SHIFT)) { + if (currentScreen instanceof AbstractMoveableScreen && (keyCode == GLFW.GLFW_KEY_LEFT_SHIFT || keyCode == GLFW.GLFW_KEY_RIGHT_SHIFT)) { for (Widget widget : widgets) { widget.shiftDown = true; } } } - public void keyReleased(int keyCode){ + + public void keyReleased(int keyCode) { Screen currentScreen = DynamicHUD.MC.currentScreen; - if(currentScreen instanceof AbstractMoveableScreen && (keyCode == GLFW.GLFW_KEY_LEFT_SHIFT || keyCode == GLFW.GLFW_KEY_RIGHT_SHIFT)) { + if (currentScreen instanceof AbstractMoveableScreen && (keyCode == GLFW.GLFW_KEY_LEFT_SHIFT || keyCode == GLFW.GLFW_KEY_RIGHT_SHIFT)) { for (Widget widget : widgets) { widget.shiftDown = false; } } } - public void onCloseScreen(){ - if(DynamicHUD.MC.currentScreen instanceof AbstractMoveableScreen){ + + public void onCloseScreen() { + if (DynamicHUD.MC.currentScreen instanceof AbstractMoveableScreen) { for (Widget widget : widgets) { widget.onClose(); } @@ -138,14 +144,14 @@ public List getWidgets() { return widgets; } - public void mouseReleased(double mouseX, double mouseY, int button){ + public void mouseReleased(double mouseX, double mouseY, int button) { Screen currentScreen = DynamicHUD.MC.currentScreen; - if(currentScreen == null) { + if (currentScreen == null) { return; } - if(currentScreen instanceof AbstractMoveableScreen){ + if (currentScreen instanceof AbstractMoveableScreen) { for (Widget widget : widgets) { - widget.mouseReleased(mouseX,mouseY,button); + widget.mouseReleased(mouseX, mouseY, button); } } } 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 a75269f..5715ce1 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java @@ -4,33 +4,34 @@ import com.tanishisherewith.dynamichud.newTrial.config.GlobalConfig; import com.tanishisherewith.dynamichud.newTrial.utils.DynamicValueRegistry; import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.ContextMenu; -import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.*; +import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.BooleanOption; +import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.DoubleOption; +import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.EnumOption; +import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.ListOption; import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.coloroption.ColorOption; import com.tanishisherewith.dynamichud.newTrial.widget.Widget; import com.tanishisherewith.dynamichud.newTrial.widget.WidgetData; import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.util.math.MatrixStack; import net.minecraft.nbt.NbtCompound; import org.lwjgl.glfw.GLFW; -import java.awt.Color; -import java.util.*; +import java.awt.*; +import java.util.Arrays; +import java.util.List; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; public class TextWidget extends Widget { - public static WidgetData DATA = new WidgetData<>("TextWidget","Display Text on screen",TextWidget::new); - Supplier textSupplier; - String dynamicRegistryKey = ""; - DynamicValueRegistry dynamicValueRegistry = null; + public Color textColor; public static WidgetData DATA = new WidgetData<>("TextWidget", "Display Text on screen", TextWidget::new); 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 rainbowSpeed = 2; //Speed of the rainbow effect + Supplier textSupplier; + String dynamicRegistryKey = ""; + DynamicValueRegistry dynamicValueRegistry = null; private ContextMenu menu; - public Color textColor; - public TextWidget() { - this(null,null,false,false,Color.WHITE,"unknown"); + this(null, null, false, false, Color.WHITE, "unknown"); } /** @@ -40,8 +41,8 @@ public TextWidget() { * @param shadow * @param rainbow */ - public TextWidget(String dynamicRegistryKey, boolean shadow, boolean rainbow,Color color,String modID) { - super(DATA,modID); + public TextWidget(String dynamicRegistryKey, boolean shadow, boolean rainbow, Color color, String modID) { + super(DATA, modID); this.dynamicRegistryKey = dynamicRegistryKey; textSupplier = (Supplier) DynamicValueRegistry.getGlobal(dynamicRegistryKey); this.shadow = shadow; @@ -57,11 +58,11 @@ public TextWidget(String dynamicRegistryKey, boolean shadow, boolean rainbow,Col * @param shadow * @param rainbow */ - public TextWidget(DynamicValueRegistry dynamicValueRegistry,String dynamicRegistryKey, boolean shadow, boolean rainbow,Color color,String modID) { - super(DATA,modID); + public TextWidget(DynamicValueRegistry dynamicValueRegistry, String dynamicRegistryKey, boolean shadow, boolean rainbow, Color color, String modID) { + super(DATA, modID); this.dynamicRegistryKey = dynamicRegistryKey; this.dynamicValueRegistry = dynamicValueRegistry; - if(dynamicValueRegistry != null) { + if (dynamicValueRegistry != null) { textSupplier = (Supplier) dynamicValueRegistry.get(dynamicRegistryKey); } this.textColor = color; @@ -69,49 +70,46 @@ public TextWidget(DynamicValueRegistry dynamicValueRegistry,String dynamicRegist this.rainbow = rainbow; createMenu(); } - public void createMenu(){ - menu = new ContextMenu(getX(),getY()); - menu.addOption(new BooleanOption("Shadow",()->this.shadow,value-> this.shadow = value)); - menu.addOption(new BooleanOption("Rainbow",()->this.rainbow,value-> this.rainbow = value)); - menu.addOption(new ColorOption("TextColor",menu,()-> textColor, value -> textColor = value)); - menu.addOption(new DoubleOption("RainbowSpeed",1,4,1.0f, ()->(double)this.rainbowSpeed, value-> this.rainbowSpeed = value.intValue())); + + public void createMenu() { + menu = new ContextMenu(getX(), getY()); + menu.addOption(new BooleanOption("Shadow", () -> this.shadow, value -> this.shadow = value)); + menu.addOption(new BooleanOption("Rainbow", () -> this.rainbow, value -> this.rainbow = value)); + menu.addOption(new ColorOption("TextColor", menu, () -> textColor, value -> textColor = value)); + menu.addOption(new DoubleOption("RainbowSpeed", 1, 4, 1.0f, () -> (double) this.rainbowSpeed, value -> this.rainbowSpeed = value.intValue())); /* TEST */ AtomicReference enums = new AtomicReference<>(Enum.Enum1); AtomicReference option = new AtomicReference<>("Enum1"); menu.addOption(new EnumOption<>("Enum", enums::get, enums::set, Enum.values())); - List options = Arrays.asList("List1","List2","List3"); + List options = Arrays.asList("List1", "List2", "List3"); menu.addOption(new ListOption<>("List", option::get, option::set, options)); } - public enum Enum{ - Enum1, - Enum2, - Enum3 - } + @Override - public void renderWidget(DrawContext drawContext,int mouseX, int mouseY) { + public void renderWidget(DrawContext drawContext, int mouseX, int mouseY) { int color = rainbow ? ColorHelper.getColorFromHue((System.currentTimeMillis() % (5000 * rainbowSpeed) / (5000f * rainbowSpeed))) : textColor.getRGB(); if (textSupplier != null) { String text = textSupplier.get(); - drawContext.drawText(mc.textRenderer, text,getX() + 2, getY() + 2, color, shadow); - widgetBox.setSizeAndPosition(getX(), getY(), mc.textRenderer.getWidth(text) + 3, mc.textRenderer.fontHeight + 2,this.shouldScale,GlobalConfig.get().scale); + drawContext.drawText(mc.textRenderer, text, getX() + 2, getY() + 2, color, shadow); + widgetBox.setSizeAndPosition(getX(), getY(), mc.textRenderer.getWidth(text) + 3, mc.textRenderer.fontHeight + 2, this.shouldScale, GlobalConfig.get().scale); } menu.render(drawContext, getX() - 2, getY(), (int) Math.ceil(getHeight())); } @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { - if(button == GLFW.GLFW_MOUSE_BUTTON_RIGHT && widgetBox.isMouseOver(mouseX,mouseY)){ + if (button == GLFW.GLFW_MOUSE_BUTTON_RIGHT && widgetBox.isMouseOver(mouseX, mouseY)) { menu.toggleDisplay(); } - menu.mouseClicked(mouseX,mouseY,button); + menu.mouseClicked(mouseX, mouseY, button); return super.mouseClicked(mouseX, mouseY, button); } @Override public void mouseReleased(double mouseX, double mouseY, int button) { - menu.mouseReleased(mouseX,mouseY,button); + menu.mouseReleased(mouseX, mouseY, button); super.mouseReleased(mouseX, mouseY, button); } @@ -130,11 +128,11 @@ public void onClose() { @Override public void writeToTag(NbtCompound tag) { super.writeToTag(tag); - tag.putString("DynamicRegistryKey",dynamicRegistryKey); - tag.putBoolean("Shadow",shadow); - tag.putBoolean("Rainbow",rainbow); - tag.putInt("TextColor",textColor.getRGB()); - tag.putInt("RainbowSpeed",rainbowSpeed); + tag.putString("DynamicRegistryKey", dynamicRegistryKey); + tag.putBoolean("Shadow", shadow); + tag.putBoolean("Rainbow", rainbow); + tag.putInt("TextColor", textColor.getRGB()); + tag.putInt("RainbowSpeed", rainbowSpeed); // If true then it means that we should use local registry and if false (i.e. null) then use global registry tag.putBoolean("DynamicValueRegistry", dynamicValueRegistry != null); @@ -152,20 +150,27 @@ public void readFromTag(NbtCompound tag) { // If true then it means that we should use local registry and if false (i.e. null) then use global registry boolean dvrObj = tag.getBoolean("DynamicValueRegistry"); - if(!dvrObj){ + if (!dvrObj) { this.textSupplier = (Supplier) DynamicValueRegistry.getGlobal(dynamicRegistryKey); return; } - for(DynamicValueRegistry dvr: DynamicValueRegistry.getInstances(modId)){ + for (DynamicValueRegistry dvr : DynamicValueRegistry.getInstances(modId)) { //Unfortunately, this method takes the value from the first local registry with the key. //It returns to prevent overriding with other registries this.textSupplier = (Supplier) dvr.get(dynamicRegistryKey); return; } createMenu(); - } - public static class Builder extends WidgetBuilder { + } + + public enum Enum { + Enum1, + Enum2, + Enum3 + } + + public static class Builder extends WidgetBuilder { protected boolean shadow = false; protected boolean rainbow = false; protected String dynamicRegistryKey = ""; @@ -181,14 +186,17 @@ public Builder rainbow(boolean rainbow) { this.rainbow = rainbow; return self(); } + public Builder setDRKey(String dynamicRegistryKey) { this.dynamicRegistryKey = dynamicRegistryKey; return self(); } + public Builder setDVR(DynamicValueRegistry dynamicValueRegistry) { this.dynamicValueRegistry = dynamicValueRegistry; return self(); } + public Builder setTextColor(Color textColor) { this.textColor = textColor; return self(); @@ -202,15 +210,17 @@ protected Builder self() { @Override public TextWidget build() { TextWidget widget; - if(dynamicValueRegistry == null) { - widget = new TextWidget(dynamicRegistryKey, shadow, rainbow,textColor,modID); - }else{ - widget = new TextWidget(dynamicValueRegistry,dynamicRegistryKey, shadow, rainbow,textColor,modID); + if (dynamicValueRegistry == null) { + widget = new TextWidget(dynamicRegistryKey, shadow, rainbow, textColor, modID); + } else { + widget = new TextWidget(dynamicValueRegistry, dynamicRegistryKey, shadow, rainbow, textColor, modID); } - widget.setPosition(x,y); + widget.setPosition(x, y); widget.setDraggable(isDraggable); widget.setShouldScale(shouldScale); return widget; } } + + }