diff options
7 files changed, 297 insertions, 46 deletions
diff --git a/build.gradle b/build.gradle index 5469f36..e320d14 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,6 @@ plugins { id "java" id "com.github.johnrengelman.shadow" version "6.1.0" id "net.minecraftforge.gradle.forge" version "6f53277" - id "org.jetbrains.kotlin.jvm" version "1.5.31" } group "io.polyfrost" @@ -25,9 +24,11 @@ configurations { } repositories { +mavenCentral() } dependencies { +implementation 'org.jetbrains:annotations:22.0.0' } /** @@ -71,12 +72,6 @@ shadowJar { duplicatesStrategy DuplicatesStrategy.EXCLUDE } -compileKotlin { - kotlinOptions { - jvmTarget = "1.8" - } -} - reobf { shadowJar { mappingType = "SEARGE" } } tasks.reobfShadowJar.mustRunAfter shadowJar afterEvaluate { diff --git a/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java b/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java index d49cb06..575fc3f 100644 --- a/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java +++ b/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java @@ -1,10 +1,10 @@ package io.polyfrost.oneconfig.command; import io.polyfrost.oneconfig.gui.Window; +import io.polyfrost.oneconfig.hud.gui.HudGui; import io.polyfrost.oneconfig.themes.Themes; import io.polyfrost.oneconfig.utils.TickDelay; import net.minecraft.client.Minecraft; -import net.minecraft.command.CommandException; import net.minecraft.command.ICommand; import net.minecraft.command.ICommandSender; import net.minecraft.util.BlockPos; @@ -17,15 +17,8 @@ import java.util.List; public class OneConfigCommand implements ICommand { - private final List<String> aliases; private static final Minecraft mc = Minecraft.getMinecraft(); - public OneConfigCommand() { - aliases = new ArrayList<>(); - aliases.add("oneconfig"); - aliases.add("ocfg"); - } - @Override public String getCommandName() { return "oneconfig"; @@ -38,15 +31,26 @@ public class OneConfigCommand implements ICommand { @Override public List<String> getCommandAliases() { - return this.aliases; + return new ArrayList<String>() {{ + add("oneconfig"); + add("ocfg"); + }}; } @Override - public void processCommand(ICommandSender sender, String[] args) throws CommandException { - new TickDelay(() -> mc.displayGuiScreen(new Window()), 1); - if(args.length != 0) { - mc.thePlayer.addChatMessage(new ChatComponentText("reloading theme!")); - Themes.openTheme(new File("OneConfig/themes/one.zip").getAbsoluteFile()); + public void processCommand(ICommandSender sender, String[] args) { + if (args.length == 0) + new TickDelay(() -> mc.displayGuiScreen(new Window()), 1); + else { + switch (args[0]) { + case "hud": + new TickDelay(() -> mc.displayGuiScreen(new HudGui()), 1); + break; + case "theme": + mc.thePlayer.addChatMessage(new ChatComponentText("reloading theme!")); + Themes.openTheme(new File("OneConfig/themes/one.zip").getAbsoluteFile()); + break; + } } } diff --git a/src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java b/src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java index 1e0940b..a66a8a0 100644 --- a/src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java +++ b/src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java @@ -2,6 +2,7 @@ package io.polyfrost.oneconfig.config.core; import io.polyfrost.oneconfig.config.data.ModData; import io.polyfrost.oneconfig.config.interfaces.Option; +import io.polyfrost.oneconfig.hud.HudCore; import java.util.ArrayList; import java.util.HashMap; @@ -18,6 +19,7 @@ public class ConfigCore { public static void reInitAll () { ArrayList<ModData> data = new ArrayList<>(settings.keySet()); settings.clear(); + HudCore.huds.clear(); for (ModData modData : data) { modData.config.init(modData); } diff --git a/src/main/java/io/polyfrost/oneconfig/hud/HudCore.java b/src/main/java/io/polyfrost/oneconfig/hud/HudCore.java index c0e29d1..a292905 100644 --- a/src/main/java/io/polyfrost/oneconfig/hud/HudCore.java +++ b/src/main/java/io/polyfrost/oneconfig/hud/HudCore.java @@ -1,6 +1,8 @@ package io.polyfrost.oneconfig.hud; import io.polyfrost.oneconfig.hud.interfaces.BasicHud; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.ScaledResolution; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @@ -8,11 +10,14 @@ import java.util.ArrayList; public class HudCore { public static ArrayList<BasicHud> huds = new ArrayList<>(); + public static boolean editing = false; @SubscribeEvent public void onRender(RenderGameOverlayEvent.Post event) { - if (event.type != RenderGameOverlayEvent.ElementType.ALL) return; - for (BasicHud hud : huds) - hud.drawAll(20,20, 5); + if (event.type != RenderGameOverlayEvent.ElementType.ALL || editing) return; + ScaledResolution sr = new ScaledResolution(Minecraft.getMinecraft()); + for (BasicHud hud : huds) { + hud.drawAll(hud.getXScaled(sr.getScaledWidth()), hud.getYScaled(sr.getScaledHeight()), hud.scale); + } } } diff --git a/src/main/java/io/polyfrost/oneconfig/hud/gui/HudGui.java b/src/main/java/io/polyfrost/oneconfig/hud/gui/HudGui.java new file mode 100644 index 0000000..310a2a8 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/hud/gui/HudGui.java @@ -0,0 +1,200 @@ +package io.polyfrost.oneconfig.hud.gui; + +import io.polyfrost.oneconfig.hud.HudCore; +import io.polyfrost.oneconfig.hud.interfaces.BasicHud; +import io.polyfrost.oneconfig.renderer.Renderer; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.ScaledResolution; +import org.lwjgl.input.Keyboard; + +import java.awt.*; +import java.io.IOException; + +public class HudGui extends GuiScreen { + private final ScaledResolution sr = new ScaledResolution(Minecraft.getMinecraft()); + private BasicHud editingHud; + private boolean isDragging; + private boolean isScaling; + private boolean scaleLeft; + private boolean scaleBottom; + private int xOffset; + private int yOffset; + private boolean wereKeypressesEnabled; + + @Override + public void initGui() { + HudCore.editing = true; + wereKeypressesEnabled = Keyboard.areRepeatEventsEnabled(); + Keyboard.enableRepeatEvents(true); + } + + //TODO: making scaling work properly everywhere instead of only in first quadrant + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + Gui.drawRect(0, 0, this.width, this.height, new Color(80, 80, 80, 50).getRGB()); + + for (BasicHud hud : HudCore.huds) { + int width = (int) (hud.getWidth(hud.scale) + hud.paddingX * hud.scale); + int height = (int) (hud.getHeight(hud.scale) + hud.paddingY * hud.scale); + int x = (int) hud.getXScaled(this.width); + int y = (int) hud.getYScaled(this.height); + + if (hud == editingHud && isScaling) { + int newWidth = mouseX - x; + if (scaleLeft) + newWidth = x - mouseX + width; + float newScale = (float) newWidth / (width / hud.scale); + if (newScale > 20) + newScale = 20; + else if (newScale < 0.3) + newScale = 0.3f; + hud.scale = newScale; + if (scaleLeft || scaleBottom) { + int newX = x; + int newY = y; + if (scaleLeft) + newX = x + width - (hud.getWidth(newScale) + (int) (hud.paddingX * newScale)); + if (scaleBottom) + newY = y + height - (hud.getHeight(newScale) + (int) (hud.paddingY * newScale)); + setPosition(newX, newY, false); + } + // updating everything to new values + width = (int) (hud.getWidth(hud.scale) + hud.paddingX * hud.scale); + height = (int) (hud.getHeight(hud.scale) + hud.paddingY * hud.scale); + x = (int) hud.getXScaled(this.width); + y = (int) hud.getYScaled(this.height); + } + + hud.drawExampleAll(x, y, hud.scale); + int color = new Color(215, 224, 235).getRGB(); + if (editingHud == hud) { + color = new Color(43, 159, 235).getRGB(); + if (isDragging) + Gui.drawRect(x, y, x + width, y + height, new Color(108, 176, 255, 60).getRGB()); + } + Renderer.drawLine(x - 2 / 4f, y, x + width + 2 / 4f, y, 2, color); + Renderer.drawLine(x, y, x, y + height, 2, color); + Renderer.drawLine(x + width, y, x + width, y + height, 2, color); + Renderer.drawLine(x - 2 / 4f, y + height, x + width + 2 / 4f, y + height, 2, color); + + if (hud == editingHud && !isDragging) { + Gui.drawRect(x - 3, y - 3, x + 3, y + 3, new Color(43, 159, 235).getRGB()); + Gui.drawRect(x - 2, y - 2, x + 2, y + 2, new Color(252, 252, 252).getRGB()); + Gui.drawRect(x - 3, y + height - 3, x + 3, y + height + 3, new Color(43, 159, 235).getRGB()); + Gui.drawRect(x - 2, y + height - 2, x + 2, y + height + 2, new Color(252, 252, 252).getRGB()); + Gui.drawRect(x + width - 3, y - 3, x + width + 3, y + 3, new Color(43, 159, 235).getRGB()); + Gui.drawRect(x + width - 2, y - 2, x + width + 2, y + 2, new Color(252, 252, 252).getRGB()); + Gui.drawRect(x + width - 3, y + height - 3, x + width + 3, y + height + 3, new Color(43, 159, 235).getRGB()); + Gui.drawRect(x + width - 2, y + height - 2, x + width + 2, y + height + 2, new Color(252, 252, 252).getRGB()); + } + } + + if (isDragging) { + setPosition(mouseX - xOffset, mouseY - yOffset, true); + } + } + + private void setPosition(double newX, double newY, boolean snap) { + double width = (int) (editingHud.getWidth(editingHud.scale) + editingHud.paddingX * editingHud.scale); + double height = (int) (editingHud.getHeight(editingHud.scale) + editingHud.paddingY * editingHud.scale); + + if (newX < 0) + newX = 0; + else if (newX + width > this.width) + newX = this.width - width; + + if (newY < 0) + newY = 0; + else if (newY + height > this.height) + newY = this.height - height; + + if (newX / this.width <= 0.5) + editingHud.xUnscaled = newX / (double) this.width; + else + editingHud.xUnscaled = (newX + width) / (double) this.width; + if (newY / this.height <= 0.5) + editingHud.yUnscaled = newY / (double) this.height; + else + editingHud.yUnscaled = (newY + height) / (double) this.height; + } + + @Override + protected void mouseClicked(int mouseX, int mouseY, int mouseButton) { + if (mouseButton == 0) { + if (editingHud != null) { + int width = (int) (editingHud.getWidth(editingHud.scale) + editingHud.paddingX * editingHud.scale); + int height = (int) (editingHud.getHeight(editingHud.scale) + editingHud.paddingY * editingHud.scale); + float x = editingHud.getXScaled(this.width); + float y = editingHud.getYScaled(this.height); + if ((mouseX >= x - 3 && mouseX <= x + 3 || mouseX >= x + width - 3 && mouseX <= x + width + 3) && + (mouseY >= y - 3 && mouseY <= y + 3 || mouseY >= y + height - 3 && mouseY <= y + height + 3)) { + isScaling = true; + scaleLeft = mouseX >= x - 3 && mouseX <= x + 3; + scaleBottom = mouseY >= y - 3 && mouseY <= y + 3; + return; + } + } + editingHud = null; + for (BasicHud hud : HudCore.huds) { + int width = (int) (hud.getWidth(hud.scale) + hud.paddingX * hud.scale); + int height = (int) (hud.getHeight(hud.scale) + hud.paddingY * hud.scale); + float x = hud.getXScaled(this.width); + float y = hud.getYScaled(this.height); + if (mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height) { + editingHud = hud; + xOffset = (int) (mouseX - x); + yOffset = (int) (mouseY - y); + isDragging = true; + break; + } + } + } + } + + @Override + protected void mouseReleased(int mouseX, int mouseY, int state) { + isDragging = false; + isScaling = false; + } + + @Override + protected void keyTyped(char typedChar, int keyCode) throws IOException { + if (editingHud != null) { + float x = editingHud.getXScaled(this.width); + float y = editingHud.getYScaled(this.height); + switch (keyCode) { + case Keyboard.KEY_UP: + setPosition(x, y - 1, false); + break; + case Keyboard.KEY_DOWN: + setPosition(x, y + 1, false); + break; + case Keyboard.KEY_LEFT: + setPosition(x - 1, y, false); + break; + case Keyboard.KEY_RIGHT: + setPosition(x + 1, y, false); + break; + } + } + super.keyTyped(typedChar, keyCode); + } + + @Override + public void onGuiClosed() { + HudCore.editing = false; + Keyboard.enableRepeatEvents(wereKeypressesEnabled); + for (BasicHud hud : HudCore.huds) { + hud.xUnscaled = 0.1; + hud.yUnscaled = 0.1; + hud.scale = 2; + } + } + + @Override + public boolean doesGuiPauseGame() { + return false; + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/hud/interfaces/BasicHud.java b/src/main/java/io/polyfrost/oneconfig/hud/interfaces/BasicHud.java index 121c258..ebc5459 100644 --- a/src/main/java/io/polyfrost/oneconfig/hud/interfaces/BasicHud.java +++ b/src/main/java/io/polyfrost/oneconfig/hud/interfaces/BasicHud.java @@ -5,8 +5,9 @@ import io.polyfrost.oneconfig.renderer.Renderer; import java.awt.*; public abstract class BasicHud { - public int normalX; - public int normalY; + public double xUnscaled = 0; + public double yUnscaled = 0; + public float scale = 1; public int paddingX = 5; public int paddingY = 5; public boolean background = true; @@ -26,23 +27,37 @@ public abstract class BasicHud { return getHeight(scale); } - public void drawAll(int x, int y, float scale) { - drawBackGround(x, y, scale); - draw(x, y, scale); + public void drawAll(float x, float y, float scale) { + drawBackground(x, y, scale); + draw((int) (x + paddingX * scale / 2f), (int) (y + paddingY * scale / 2f), scale); } - public void drawExampleAll(int x, int y, float scale) { - drawBackGround(x, y, scale); - drawExample(x, y, scale); + public void drawExampleAll(float x, float y, float scale) { + drawBackground(x, y, scale); + drawExample((int) (x + paddingX * scale / 2f), (int) (y + paddingY * scale / 2f), scale); } public void drawExample(int x, int y, float scale) { draw(x, y, scale); } - private void drawBackGround(int x, int y, float scale) { - Renderer.drawRoundRect(x - paddingX * scale / 2f, y - paddingY * scale / 2f, - getWidth(scale) + paddingX * scale, getHeight(scale) + paddingY * scale, - (2 * scale), new Color(0, 0, 0, 100).getRGB()); + private void drawBackground(float x, float y, float scale) { + Renderer.drawRoundRect((int) x, (int) y, + (int) (getWidth(scale) + paddingX * scale), (int) (getHeight(scale) + paddingY * scale), + (int) (2 * scale), new Color(0, 0, 0, 120).getRGB()); + } + + public float getXScaled(int screenWidth) { + if (xUnscaled <= 0.5) { + return (int) (screenWidth * xUnscaled); + } + return (float) (screenWidth - (1d - xUnscaled) * screenWidth - (getWidth(scale) + paddingX * scale)); + } + + public float getYScaled(int screenHeight) { + if (yUnscaled <= 0.5) { + return (int) (screenHeight * yUnscaled); + } + return (float) (screenHeight - (1d - yUnscaled) * screenHeight - (getHeight(scale) + paddingY * scale)); } } diff --git a/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java b/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java index 8329bcc..44e1b34 100644 --- a/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java +++ b/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java @@ -40,7 +40,7 @@ public class Renderer extends Gui { Gui.drawScaledCustomSizeModalRect(x, y, 0, 0, targetX, targetY, targetX, targetY, targetX, targetY); } - public static void drawRegularPolygon(double x, double y, float radius, int sides, int color, double lowerAngle, double upperAngle) { + public static void drawRegularPolygon(double x, double y, int radius, int sides, int color, double lowerAngle, double upperAngle) { GL11.glDisable(GL11.GL_TEXTURE_2D); setGlColor(color); GlStateManager.enableBlend(); @@ -68,7 +68,7 @@ public class Renderer extends Gui { GL11.glEnable(GL11.GL_TEXTURE_2D); } - public static void drawRegularPolygon(double x, double y, float radius, int sides, int color) { + public static void drawRegularPolygon(double x, double y, int radius, int sides, int color) { drawRegularPolygon(x, y, radius, sides, color, 0d, 10000d); } @@ -78,16 +78,16 @@ public class Renderer extends Gui { * @param radius radius of the corners * @param color color as a rgba integer */ - public static void drawRoundRect(double x, double y, double width, double height, float radius, int color) { + public static void drawRoundRect(int x, int y, int width, int height, int radius, int color) { GL11.glEnable(GL11.GL_BLEND); //GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE); - Gui.drawRect((int) (x + radius), (int) y, (int) (x + width - radius), (int) (y + radius), color); // top - Gui.drawRect((int) (x + radius), (int) (y + height - radius), (int) (x + width - radius), (int) (y + height), color); // bottom - Gui.drawRect((int) x, (int) (y + radius), (int) (x + width), (int) (y + height - radius), color); // main - drawRegularPolygon(x + radius - 0.5, y + radius - 0.5, radius, 80, color, 0d, 4.75d); // top left - drawRegularPolygon(x + width - radius - 0.5, y + radius - 0.5, radius, 80, color, 7.8d, 10d); // top right - drawRegularPolygon(x + radius - 0.5, y + height - radius - 0.5, radius, 80, color, 4.7d, 6.3d); // bottom left - drawRegularPolygon(x + width - radius - 0.5, y + height - radius - 0.5, radius, 80, color, 6.25d, 7.9d); // bottom right + Gui.drawRect(x + radius, y, (x + width - radius), (y + radius), color); // top + Gui.drawRect(x + radius, (y + height - radius), (x + width - radius), (y + height), color); // bottom + Gui.drawRect(x, (y + radius), (x + width), (y + height - radius), color); // main + drawRegularPolygon(x + radius, y + radius, radius, 80, color, 0d, 4.75d); // top left + drawRegularPolygon(x + width - radius, y + radius, radius, 80, color, 7.8d, 10d); // top right + drawRegularPolygon(x + radius, y + height - radius, radius, 80, color, 4.7d, 6.3d); // bottom left + drawRegularPolygon(x + width - radius, y + height - radius, radius, 80, color, 6.25d, 7.9d); // bottom right GL11.glDisable(GL11.GL_BLEND); GL11.glColor4f(1f, 1f, 1f, 1f); } @@ -135,4 +135,34 @@ public class Renderer extends Gui { GlStateManager.color(f, f1, f2, f3); } + public static void drawLine(float sx, float sy, float ex, float ey, int width, int color) { + float f = (float) (color >> 24 & 255) / 255.0F; + float f1 = (float) (color >> 16 & 255) / 255.0F; + float f2 = (float) (color >> 8 & 255) / 255.0F; + float f3 = (float) (color & 255) / 255.0F; + GlStateManager.pushMatrix(); + GlStateManager.disableTexture2D(); + GlStateManager.enableBlend(); + GlStateManager.disableAlpha(); + GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0); + GlStateManager.color(f1, f2, f3, f); + GL11.glLineWidth(width); + GL11.glBegin(GL11.GL_LINES); + GL11.glVertex2d(sx, sy); + GL11.glVertex2d(ex, ey); + GL11.glEnd(); + GlStateManager.disableBlend(); + GlStateManager.enableAlpha(); + GlStateManager.enableTexture2D(); + GlStateManager.popMatrix(); + } + + public static void drawDottedLine(float sx, float sy, float ex, float ey, int width, int factor, int color) { + GlStateManager.pushMatrix(); + GL11.glLineStipple(factor, (short) 0xAAAA); + GL11.glEnable(GL11.GL_LINE_STIPPLE); + drawLine(sx, sy, ex, ey, width, color); + GL11.glDisable(GL11.GL_LINE_STIPPLE); + GlStateManager.popMatrix(); + } } |