From fe04136d705ca5f946ee8353c7f7c67e284c9ca5 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Sat, 5 Mar 2022 18:02:27 +0100 Subject: Decent enough hud stuff so I can do some school work --- .../io/polyfrost/oneconfig/hud/gui/HudGui.java | 140 +++++++++++++-------- 1 file changed, 91 insertions(+), 49 deletions(-) (limited to 'src/main/java/io/polyfrost/oneconfig/hud/gui/HudGui.java') diff --git a/src/main/java/io/polyfrost/oneconfig/hud/gui/HudGui.java b/src/main/java/io/polyfrost/oneconfig/hud/gui/HudGui.java index 310a2a8..262e36d 100644 --- a/src/main/java/io/polyfrost/oneconfig/hud/gui/HudGui.java +++ b/src/main/java/io/polyfrost/oneconfig/hud/gui/HudGui.java @@ -11,14 +11,13 @@ import org.lwjgl.input.Keyboard; import java.awt.*; import java.io.IOException; +import java.util.ArrayList; 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; @@ -30,43 +29,38 @@ public class HudGui extends GuiScreen { 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 (isDragging) { + setPosition(mouseX - xOffset, mouseY - yOffset, true); + } + for (BasicHud hud : HudCore.huds) { if (hud == editingHud && isScaling) { - int newWidth = mouseX - x; - if (scaleLeft) - newWidth = x - mouseX + width; - float newScale = (float) newWidth / (width / hud.scale); + float xFloat = hud.getXScaled(this.width); + float yFloat = hud.getYScaled(this.height); + float pos = getXSnapping(mouseX, true); + float newWidth = pos - xFloat; + float newScale = newWidth / ((hud.getWidth(hud.scale) + hud.paddingX * hud.scale) / 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); + + if (xFloat / this.width > 0.5) + editingHud.xUnscaled = (xFloat + (hud.getWidth(hud.scale) + hud.paddingX * hud.scale)) / (double) this.width; + if (yFloat / this.height > 0.5) + editingHud.yUnscaled = (yFloat + (hud.getHeight(hud.scale) + hud.paddingY * hud.scale)) / (double) this.height; } + 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); + hud.drawExampleAll(x, y, hud.scale); int color = new Color(215, 224, 235).getRGB(); if (editingHud == hud) { @@ -80,36 +74,30 @@ public class HudGui extends GuiScreen { 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); + private void setPosition(float newX, float newY, boolean snap) { + float width = editingHud.getWidth(editingHud.scale) + editingHud.paddingX * editingHud.scale; + float height = 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 (snap) { + newX = getXSnapping(newX, false); + newY = getYSnapping(newY); + } + if (newX / this.width <= 0.5) editingHud.xUnscaled = newX / (double) this.width; else @@ -120,6 +108,68 @@ public class HudGui extends GuiScreen { editingHud.yUnscaled = (newY + height) / (double) this.height; } + private float getXSnapping(float pos, boolean rightOnly) { + float width = editingHud.getWidth(editingHud.scale) + editingHud.paddingX * editingHud.scale; + ArrayList verticalLines = new ArrayList<>(); + verticalLines.add(this.width / 2f); + for (BasicHud hud : HudCore.huds) { + if (hud == editingHud) continue; + int hudWidth = (int) (hud.getWidth(hud.scale) + hud.paddingX * hud.scale); + int hudX = (int) hud.getXScaled(this.width); + verticalLines.add((float) hudX); + verticalLines.add((float) (hudX + hudWidth)); + verticalLines.add(hudX + hudWidth / 2f); + } + float smallestDiff = -1; + float smallestLine = 0; + float smallestOffset = 0; + for (float lineX : verticalLines) { + for (float offset = 0; offset <= (rightOnly ? 0 : width); offset += width / 2f) { + if (Math.abs(lineX - pos - offset) < 5 && (Math.abs(lineX - pos - offset) < smallestDiff || smallestDiff == -1)) { + smallestDiff = Math.abs(lineX - pos); + smallestLine = lineX; + smallestOffset = offset; + } + } + } + if (smallestDiff != -1) { + Renderer.drawDottedLine(smallestLine, 0, smallestLine, this.height, 2, 12, new Color(255, 255, 255).getRGB()); + return smallestLine - smallestOffset; + } + return pos; + } + + private float getYSnapping(float pos) { + float height = editingHud.getHeight(editingHud.scale) + editingHud.paddingY * editingHud.scale; + ArrayList horizontalLines = new ArrayList<>(); + horizontalLines.add(this.height / 2f); + for (BasicHud hud : HudCore.huds) { + if (hud == editingHud) continue; + int hudHeight = (int) (hud.getHeight(hud.scale) + hud.paddingY * hud.scale); + int hudY = (int) hud.getYScaled(this.height); + horizontalLines.add((float) hudY); + horizontalLines.add((float) (hudY + hudHeight)); + horizontalLines.add(hudY + hudHeight / 2f); + } + float smallestDiff = -1; + float smallestLine = 0; + float smallestOffset = 0; + for (float lineY : horizontalLines) { + for (float offset = 0; offset <= height; offset += height / 2f) { + if (Math.abs(lineY - pos - offset) < 5 && (Math.abs(lineY - pos - offset) < smallestDiff || smallestDiff == -1)) { + smallestDiff = Math.abs(lineY - pos); + smallestLine = lineY; + smallestOffset = offset; + } + } + } + if (smallestDiff != -1) { + Renderer.drawDottedLine(0, smallestLine, this.width, smallestLine, 2, 12, new Color(255, 255, 255).getRGB()); + return smallestLine - smallestOffset; + } + return pos; + } + @Override protected void mouseClicked(int mouseX, int mouseY, int mouseButton) { if (mouseButton == 0) { @@ -128,11 +178,8 @@ public class HudGui extends GuiScreen { 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)) { + if (mouseX >= x + width - 3 && mouseX <= x + width + 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; } } @@ -186,11 +233,6 @@ public class HudGui extends GuiScreen { 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 -- cgit