From b6faa000aac7adfb39672cbe768c18e49095b693 Mon Sep 17 00:00:00 2001 From: refactoring Date: Mon, 1 Jan 2024 17:58:14 -0500 Subject: [PATCH] Basic element system & added some helper functions to `RenderUtil` --- .../client/gui/util/RenderUtil.java | 13 +++++- .../client/ui/framework/UIElement.java | 25 ++++++++++++ .../client/ui/framework/UIScreen.java | 10 +++++ .../client/ui/screen/ScreenModMenu.java | 13 ++++++ .../screen/component/ElementIconButton.java | 40 +++++++++++++++++++ 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 src/main/java/net/silentclient/client/ui/framework/UIElement.java create mode 100644 src/main/java/net/silentclient/client/ui/framework/UIScreen.java create mode 100644 src/main/java/net/silentclient/client/ui/screen/ScreenModMenu.java create mode 100644 src/main/java/net/silentclient/client/ui/screen/component/ElementIconButton.java diff --git a/src/main/java/net/silentclient/client/gui/util/RenderUtil.java b/src/main/java/net/silentclient/client/gui/util/RenderUtil.java index 8bcd615..28fecb3 100644 --- a/src/main/java/net/silentclient/client/gui/util/RenderUtil.java +++ b/src/main/java/net/silentclient/client/gui/util/RenderUtil.java @@ -18,9 +18,13 @@ import org.lwjgl.opengl.GL11; import static org.lwjgl.opengl.GL11.*; public class RenderUtil { - final static Minecraft mc = Minecraft.getMinecraft(); + final static Minecraft mc = Minecraft.getMinecraft(); final static FontRenderer fr = mc.fontRendererObj; + public static void drawRoundXYWH(float x, float y, float width, float height, float r, int col) { + drawRoundedRect(x, y, x + width, y + height, r, col); + } + public static void drawRoundedRect(float x, float y, float x1, float y1, float radius, int color) { x1 = x + x1; y1 = y + y1; @@ -65,7 +69,12 @@ public class RenderUtil { glPopAttrib(); glLineWidth(1); GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); - } + } + + public static void drawRoundOutlineXYWH(float x, float y, float w, float h, float r, float lw, int col) { + drawRoundedOutline(x, y, x + w, y + h, r, lw, col); + } + public static void drawRoundedOutline(float x, float y, float x1, float y1, float radius,float lineWidth, int color) { x1 = x + x1; y1 = y + y1; diff --git a/src/main/java/net/silentclient/client/ui/framework/UIElement.java b/src/main/java/net/silentclient/client/ui/framework/UIElement.java new file mode 100644 index 0000000..4eb7d9d --- /dev/null +++ b/src/main/java/net/silentclient/client/ui/framework/UIElement.java @@ -0,0 +1,25 @@ +package net.silentclient.client.ui.framework; + +import net.minecraft.client.gui.Gui; + +/** + * @author refactoring + */ +public abstract class UIElement extends Gui { + public float x, y, w, h; + + public UIElement(float x, float y, float w, float h) { + this.x = x; + this.y = y; + this.w = w; + this.h = h; + } + + public boolean isMouseInside(float mx, float my) { + return mx >= this.x && my >= this.y && mx < this.x + this.w && my < this.y + this.h; + } + + public void render(float mouseX, float mouseY) {} + public void click(float mouseX, float mouseY, int button) {} + public void released(float mouseX, float mouseY, int button) {} +} diff --git a/src/main/java/net/silentclient/client/ui/framework/UIScreen.java b/src/main/java/net/silentclient/client/ui/framework/UIScreen.java new file mode 100644 index 0000000..aad70be --- /dev/null +++ b/src/main/java/net/silentclient/client/ui/framework/UIScreen.java @@ -0,0 +1,10 @@ +package net.silentclient.client.ui.framework; + +import net.minecraft.client.gui.GuiScreen; + +/** + * Solely exists for the purpose of making everything custom ;) + * + * @author refactoring + */ +public class UIScreen extends GuiScreen { } diff --git a/src/main/java/net/silentclient/client/ui/screen/ScreenModMenu.java b/src/main/java/net/silentclient/client/ui/screen/ScreenModMenu.java new file mode 100644 index 0000000..0904de6 --- /dev/null +++ b/src/main/java/net/silentclient/client/ui/screen/ScreenModMenu.java @@ -0,0 +1,13 @@ +package net.silentclient.client.ui.screen; + +import net.silentclient.client.ui.framework.UIScreen; + +/** + * @author refactoring + */ +public class ScreenModMenu extends UIScreen { + @Override + public void drawScreen(int mx, int my, float btn) { + + } +} diff --git a/src/main/java/net/silentclient/client/ui/screen/component/ElementIconButton.java b/src/main/java/net/silentclient/client/ui/screen/component/ElementIconButton.java new file mode 100644 index 0000000..9c40234 --- /dev/null +++ b/src/main/java/net/silentclient/client/ui/screen/component/ElementIconButton.java @@ -0,0 +1,40 @@ +package net.silentclient.client.ui.screen.component; + +import net.silentclient.client.Client; +import net.silentclient.client.gui.animation.SimpleAnimation; +import net.silentclient.client.gui.font.SilentFontRenderer; +import net.silentclient.client.gui.util.RenderUtil; +import net.silentclient.client.ui.framework.UIElement; + +import java.awt.*; + +/** + * @author refactoring + */ +public class ElementIconButton extends UIElement { + private final Runnable clickHandler; + private final String icon; + private final SimpleAnimation anim = new SimpleAnimation(0f); + + public ElementIconButton(String icon, float x, float y, float w, float h, Runnable handler) { + super(x, y, w, h); + this.icon = icon; + this.clickHandler = handler; + } + + @Override + public void render(float mouseX, float mouseY) { + RenderUtil.drawRoundXYWH(x, y, w, h, 7f, new Color(0, 0, 0, 125).getRGB()); + RenderUtil.drawRoundOutlineXYWH(x, y, w, h, 7f, 1f, new Color(255, 255, 255, 125).getRGB()); + RenderUtil.drawRoundXYWH(x, y, w, h, 7f, new Color(255, 255, 255, (int)anim.getValue()).getRGB()); + + Client.getInstance().getSilentFontRenderer().drawCenteredString(icon, (int) (x + w / 2), (int) (y + h / 2), 25, SilentFontRenderer.FontType.TEXT, 25); + } + + @Override + public void click(float mouseX, float mouseY, int button) { + if(isMouseInside(mouseX, mouseY) && button == 0) { + clickHandler.run(); + } + } +}