diff options
author | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-04-20 13:46:50 +0100 |
---|---|---|
committer | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-04-20 13:46:50 +0100 |
commit | 30df910cf3b2f5b0683ce01e391c35829d8a5850 (patch) | |
tree | df5b03bd6da6041f344bca41304da6133e0a85d4 /src/main/java/io/polyfrost/oneconfig/gui | |
parent | 1bad96837b69d0a04940985f6206d3fe91594822 (diff) | |
download | OneConfig-30df910cf3b2f5b0683ce01e391c35829d8a5850.tar.gz OneConfig-30df910cf3b2f5b0683ce01e391c35829d8a5850.tar.bz2 OneConfig-30df910cf3b2f5b0683ce01e391c35829d8a5850.zip |
hella gui stuff
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/gui')
3 files changed, 389 insertions, 0 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/gui/HudGui.java b/src/main/java/io/polyfrost/oneconfig/gui/HudGui.java new file mode 100644 index 0000000..77521f9 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/gui/HudGui.java @@ -0,0 +1,248 @@ +package io.polyfrost.oneconfig.gui; + +import io.polyfrost.oneconfig.hud.HudCore; +import io.polyfrost.oneconfig.hud.interfaces.BasicHud; +import io.polyfrost.oneconfig.lwjgl.RenderManager; +import io.polyfrost.oneconfig.test.TestHud; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.GuiScreen; +import org.lwjgl.input.Keyboard; + +import java.awt.*; +import java.io.IOException; +import java.util.ArrayList; + +public class HudGui extends GuiScreen { + private BasicHud editingHud; + private boolean isDragging; + private boolean isScaling; + private int xOffset; + private int yOffset; + + @Override + public void initGui() { + HudCore.editing = true; + Keyboard.enableRepeatEvents(true); + for (BasicHud hud : HudCore.huds) { + hud.childBottom = new TestHud(); + hud.childBottom.parent = hud; + hud.childRight = new TestHud(); + hud.childRight.parent = hud; + } + } + + @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()); + + if (isDragging) { + setPosition(mouseX - xOffset, mouseY - yOffset, true); + } + + for (BasicHud hud : HudCore.huds) { + if (hud == editingHud && isScaling) { + 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 (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, true); + 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()); + } + int finalColor = color; + RenderManager.setupAndDraw(true, (vg) -> { + RenderManager.drawLine(vg, x - 2 / 4f, y, x + width + 2 / 4f, y, 1, finalColor); + RenderManager.drawLine(vg, x, y, x, y + height, 1, finalColor); + RenderManager.drawLine(vg, x + width, y, x + width, y + height, 1, finalColor); + RenderManager.drawLine(vg, x - 2 / 4f, y + height, x + width + 2 / 4f, y + height, 1, finalColor); + }); + + if (hud == editingHud && !isDragging) { + 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()); + } + } + } + + 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 + 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; + } + + private float getXSnapping(float pos, boolean rightOnly) { + float width = editingHud.getWidth(editingHud.scale) + editingHud.paddingX * editingHud.scale; + ArrayList<Float> 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) { + RenderManager.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<Float> 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) { + RenderManager.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) { + 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 + width - 3 && mouseX <= x + width + 3 && mouseY >= y + height - 3 && mouseY <= y + height + 3) { + isScaling = true; + 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(false); + } + + @Override + public boolean doesGuiPauseGame() { + return false; + } +}
\ No newline at end of file diff --git a/src/main/java/io/polyfrost/oneconfig/gui/OneConfigGui.java b/src/main/java/io/polyfrost/oneconfig/gui/OneConfigGui.java new file mode 100644 index 0000000..cbe1c6a --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/gui/OneConfigGui.java @@ -0,0 +1,47 @@ +package io.polyfrost.oneconfig.gui; + +import io.polyfrost.oneconfig.OneConfig; +import io.polyfrost.oneconfig.config.OneConfigConfig; +import io.polyfrost.oneconfig.gui.elements.BasicElement; +import io.polyfrost.oneconfig.lwjgl.RenderManager; +import net.minecraft.client.gui.GuiScreen; + +import java.awt.*; + +public class OneConfigGui extends GuiScreen { + private final BasicElement element = new BasicElement(200, 200, 1, true); + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + super.drawScreen(mouseX, mouseY, partialTicks); + RenderManager.setupAndDraw((vg) -> { + if(OneConfigConfig.ROUNDED_CORNERS) { + RenderManager.drawRoundedRect(vg, 544, 140, 1056, 800, OneConfigConfig.GRAY_800, OneConfigConfig.CORNER_RADIUS_WIN); + RenderManager.drawRoundedRect(vg, 320, 140, 244, 800, OneConfigConfig.GRAY_900_80, OneConfigConfig.CORNER_RADIUS_WIN); + RenderManager.drawRect(vg, 544, 140, 20, 800, OneConfigConfig.GRAY_800); + } else { + // L; + } + + RenderManager.drawLine(vg, 544, 212, 1600, 212, 1, OneConfigConfig.GRAY_700); + RenderManager.drawLine(vg, 544, 140, 544, 940, 1, OneConfigConfig.GRAY_700); + + RenderManager.drawString(vg, "OneConfig", 389, 163, OneConfigConfig.WHITE, 18f, "inter-bold"); + RenderManager.drawString(vg, "By Polyfrost", 389, 183, OneConfigConfig.WHITE, 12f, "inter-regular"); + element.setColorPalette(0); + element.draw(vg, 0, 0); + + //RenderManager.drawGradientRoundedRect(vg, 100, 100, 500, 100, OneConfigConfig.BLUE_600, OneConfigConfig.BLUE_500, OneConfigConfig.CORNER_RADIUS_WIN); + + + + }); + } + + + @Override + public boolean doesGuiPauseGame() { + return false; + } + +} diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicElement.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicElement.java new file mode 100644 index 0000000..0b7d604 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/BasicElement.java @@ -0,0 +1,94 @@ +package io.polyfrost.oneconfig.gui.elements; + +import io.polyfrost.oneconfig.lwjgl.RenderManager; +import io.polyfrost.oneconfig.utils.ColorUtils; +import net.minecraft.client.Minecraft; +import org.lwjgl.input.Mouse; + +public class BasicElement { + private int width; + private int height; + private int colorPalette; + + private int hitBoxX, hitBoxY; + + private final boolean hoverFx; + + private boolean hovered = false; + private boolean clicked = false; + private boolean toggled = false; + + private int currentColor; + + public BasicElement(int width, int height, int colorPalette, boolean hoverFx) { + this.height = height; + this.width = width; + this.colorPalette = colorPalette; + this.hoverFx = hoverFx; + } + + public BasicElement(int width, int height, boolean hoverFx) { + this.height = height; + this.width = width; + this.colorPalette = -1; + this.hoverFx = hoverFx; + } + + + public void draw(long vg, int x, int y) { + RenderManager.drawRectangle(vg, x, y, width, height, currentColor); + int mouseX = Mouse.getX(); + int mouseY = Minecraft.getMinecraft().displayHeight - Math.abs(Mouse.getY()); + int buttonRight = x + width; + int buttonBottom = y + height; + + hovered = mouseX > x - hitBoxX && mouseY > y - hitBoxY && mouseX < buttonRight + hitBoxX && mouseY < buttonBottom + hitBoxY; + if (Mouse.isButtonDown(0) && clicked) { + toggled = !toggled; + } + clicked = Mouse.isButtonDown(0) && hovered; + + if (hoverFx) { + currentColor = ColorUtils.getColor(currentColor, colorPalette, hovered, clicked); + } + } + + + public void setCustomHitbox(int x, int y) { + hitBoxX = x; + hitBoxY = y; + } + + public void setWidth(int width) { + this.width = width; + } + + public void setHeight(int height) { + this.height = height; + } + + public void setColorPalette(int colorPalette) { + this.colorPalette = colorPalette; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public boolean isHovered() { + return hovered; + } + + public boolean isClicked() { + return clicked; + } + + public boolean isToggled() { + return toggled; + } + +} |