diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-03-01 21:03:37 +0100 |
---|---|---|
committer | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-03-01 21:03:37 +0100 |
commit | a7a7f5a9ab21c9d0abfb9c2caf13df833c8c51a8 (patch) | |
tree | 26fa29480dbe0490aab862ebc9446aeb7759f2b0 /src/main/java/io/polyfrost/oneconfig/hud | |
parent | 321ea5ca5b4feebc4c695fc739db7765cac77ff1 (diff) | |
download | OneConfig-a7a7f5a9ab21c9d0abfb9c2caf13df833c8c51a8.tar.gz OneConfig-a7a7f5a9ab21c9d0abfb9c2caf13df833c8c51a8.tar.bz2 OneConfig-a7a7f5a9ab21c9d0abfb9c2caf13df833c8c51a8.zip |
more hud stuff and hud scaling that works sometimes
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/hud')
3 files changed, 235 insertions, 15 deletions
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)); } } |