diff --git a/README.md b/README.md index 1efcad3..c3c2903 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Icarus > rebranded from MineSenseUI -A simple and extremely [lightweight](#lightweightness) Gui and config library for Minecraft Forge 1.8.9 modding heavily inspired by the GameSense/Skeet CS:GO cheat (not an exact copy). A fully working example mod using this library can be found at [test](src/main/java/studio/dreamys/test). +A very simple and [lightweight](#lightweightness) GUI and Config library for Minecraft Forge 1.8.9 modding heavily inspired by the GameSense/Skeet CS:GO cheat (not an exact copy). A fully working example mod using this library can be found at [test](src/main/java/studio/dreamys/test). -## Importing +## Setup ### Gradle ``` repositories { @@ -16,19 +16,29 @@ dependencies { } ``` -Replace $VERSION with any version from the [repo](https://github.com/DxxxxY/repo/tree/master/studio/dreamys/Icarus). +Replace `$VERSION` with the latest from the [repo](https://github.com/DxxxxY/repo/tree/master/studio/dreamys/Icarus). -## Components -> Some components have secondary constructors with default values. +## What's new in v2 +- **.json** +- **Same look (skeet)** +- **Customizability (font, components)** +- **~90% code rewrite** +- **New and redesigned config system (reflection)** +- **Complete separation of UI and Config** +- **Enhanced and simplified developer experience** + +## [Wiki](https://github.com/DxxxxY/Icarus/wiki/) + +## UI Components - Button - Checkbox - Choice - ~~Color~~ (coming soon) - Combo - Field -- Group -- Keybind - Slider +- Keybind +- Group - Page - Window @@ -36,11 +46,12 @@ Replace $VERSION with any version from the [repo](https://github.com/DxxxxY/repo - [Notification](https://www.youtube.com/watch?v=624J5kAZqNw&ab_channel=DxxxxY) - Watermark ![Watermark](.github/watermark.png) - ## Lightweightness -- Everything is created and stored on client init. - Everything is rendered using libraries present on runtime. -- Small size, no extra-dependencies, no bloatware. +- Other than importing this library, nothing is needed. +- Everything is created and stored on client preInit. +- Reflected data is cached and accessed quicker. +- Only dependent on the Reflections library. ## Menu (as of 09/02/2022) is fully functional: ![icarus.gif](.github/icarus.gif) \ No newline at end of file diff --git a/build.gradle b/build.gradle index 81b430b..2ce896b 100644 --- a/build.gradle +++ b/build.gradle @@ -10,11 +10,12 @@ buildscript { plugins { id "java" + id "com.github.johnrengelman.shadow" version "2.0.4" } apply plugin: "net.minecraftforge.gradle.forge" -version = "1.4.2" +version = "2.0.0" group = "dreamys.studio" //http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "Icarus" //name of the output jar @@ -28,12 +29,34 @@ minecraft { makeObfSourceJar = false //disable creation of sources jar } +configurations { + shade + compile.extendsFrom(shade) +} + + repositories { mavenCentral() } dependencies { compile "org.projectlombok:lombok:1.18.24" + shade "org.reflections:reflections:0.10.2" +} + +shadowJar { + dependencies { + //we remove gson because it belongs to the public api and will break shading + //exclude(dependency("com.google.code.gson:gson:2.8.6")) + } + configurations = [project.configurations.shade] + duplicatesStrategy DuplicatesStrategy.EXCLUDE //prevent duplicates + classifier "" //prevent creation of unshadowed jar +} + +reobf { + //reobfuscate the shadowed jar + shadowJar {} } //exclude test files in jar build diff --git a/gradlew.bat b/gradlew.bat index f955316..eb69291 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -13,7 +13,7 @@ if "%DIRNAME%" == "" set DIRNAME=. set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +@rem Add default JVM dropdownOptions here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM dropdownOptions to this script. set DEFAULT_JVM_OPTS= @rem Find java.exe diff --git a/src/main/java/studio/dreamys/icarus/Icarus.java b/src/main/java/studio/dreamys/icarus/Icarus.java index a231677..1db303b 100644 --- a/src/main/java/studio/dreamys/icarus/Icarus.java +++ b/src/main/java/studio/dreamys/icarus/Icarus.java @@ -1,58 +1,62 @@ package studio.dreamys.icarus; import lombok.Getter; -import net.minecraft.client.Minecraft; import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import net.minecraftforge.fml.common.gameevent.InputEvent; -import org.lwjgl.input.Keyboard; -import studio.dreamys.icarus.component.Component; import studio.dreamys.icarus.component.Window; -import studio.dreamys.icarus.component.sub.Button; -import studio.dreamys.icarus.component.sub.Checkbox; -import studio.dreamys.icarus.component.sub.Keybind; import studio.dreamys.icarus.config.Config; +import studio.dreamys.icarus.handler.KeybindHandler; +import studio.dreamys.icarus.component.Component; import studio.dreamys.icarus.util.RenderUtils; +import studio.dreamys.icarus.util.font.GlyphPageFontRenderer; +import java.awt.*; +import java.io.IOException; +import java.io.InputStream; + +@SuppressWarnings("unused") public class Icarus { - @Getter private static Config config; @Getter private static Window window; @Getter private static String modid; public static void init(String modid, Window window) { RenderUtils.loadFonts(); - Icarus.window = window; //create window object and store it forever + + Icarus.window = window; Icarus.modid = modid; - config = new Config(modid); //create config which will load settings into window - config.load(); //load settings from config - MinecraftForge.EVENT_BUS.register(new Icarus()); + + MinecraftForge.EVENT_BUS.register(new KeybindHandler()); + + Config.init(modid); + } + + public static void provideTextFont(InputStream fontStream, String fontName, int fontSize, boolean bold, boolean italic, boolean boldItalic) { + try { + GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(Font.createFont(Font.TRUETYPE_FONT, fontStream)); + RenderUtils.textRenderer = GlyphPageFontRenderer.create(fontName, fontSize, bold, italic, boldItalic); + } catch (FontFormatException | IOException e) { + throw new RuntimeException(e); + } } - @SubscribeEvent - public void key(InputEvent.KeyInputEvent e) { - if (Minecraft.getMinecraft().theWorld == null || Minecraft.getMinecraft().thePlayer == null) return; - if (Keyboard.getEventKeyState()) { //if the key is down - int keyCode = Keyboard.getEventKey(); //get keycode - if (keyCode <= 0) return; //ignore invalid keycode - if (keyCode == window.key) { - Minecraft.getMinecraft().displayGuiScreen(window); - return; - } - for (Component attachment : window.all) { //for every attachment - if (attachment instanceof Keybind) { //if it's a keybind - Keybind keybind = (Keybind) attachment; //cast to keybind - if (keybind.getKey() == keyCode) { //if the keys match - if (keybind.getChild() instanceof Checkbox) { //if the child is a checkbox - ((Checkbox) keybind.getChild()).toggle(); //toggle the checkbox - config.save(); //save the config - keybind.getChild().fireChange(); //fire change event - } - if (keybind.getChild() instanceof Button) { //if the child is a button - ((Button) keybind.getChild()).getRunnable().run(); //click the button - } - } - } - } + public static void provideTitleFont(InputStream fontStream, String fontName, int fontSize, boolean bold, boolean italic, boolean boldItalic) { + try { + GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(Font.createFont(Font.TRUETYPE_FONT, fontStream)); + RenderUtils.titleRenderer = GlyphPageFontRenderer.create(fontName, fontSize, bold, italic, boldItalic); + } catch (FontFormatException | IOException e) { + throw new RuntimeException(e); } } + + public static void provideIconFont(InputStream fontStream, String fontName, int fontSize, boolean bold, boolean italic, boolean boldItalic) { + try { + GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(Font.createFont(Font.TRUETYPE_FONT, fontStream)); + RenderUtils.iconRenderer = GlyphPageFontRenderer.create(fontName, fontSize, bold, italic, boldItalic); + } catch (FontFormatException | IOException e) { + throw new RuntimeException(e); + } + } + + public static void provideComponent(Class newComponent, Class oldComponent) { + Config.componentReplacements.put(oldComponent, newComponent); + } } diff --git a/src/main/java/studio/dreamys/icarus/annotation/field/DropdownOptions.java b/src/main/java/studio/dreamys/icarus/annotation/field/DropdownOptions.java new file mode 100644 index 0000000..e71f9dc --- /dev/null +++ b/src/main/java/studio/dreamys/icarus/annotation/field/DropdownOptions.java @@ -0,0 +1,12 @@ +package studio.dreamys.icarus.annotation.field; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface DropdownOptions { + String[] value(); +} diff --git a/src/main/java/studio/dreamys/icarus/annotation/field/IKeybind.java b/src/main/java/studio/dreamys/icarus/annotation/field/IKeybind.java new file mode 100644 index 0000000..913e458 --- /dev/null +++ b/src/main/java/studio/dreamys/icarus/annotation/field/IKeybind.java @@ -0,0 +1,12 @@ +package studio.dreamys.icarus.annotation.field; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface IKeybind { + int value(); +} diff --git a/src/main/java/studio/dreamys/icarus/annotation/field/SliderOptions.java b/src/main/java/studio/dreamys/icarus/annotation/field/SliderOptions.java new file mode 100644 index 0000000..2df94b7 --- /dev/null +++ b/src/main/java/studio/dreamys/icarus/annotation/field/SliderOptions.java @@ -0,0 +1,15 @@ +package studio.dreamys.icarus.annotation.field; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface SliderOptions { + double min(); + double max(); + boolean onlyInt() default false; + String units() default ""; +} diff --git a/src/main/java/studio/dreamys/icarus/annotation/type/IGroup.java b/src/main/java/studio/dreamys/icarus/annotation/type/IGroup.java new file mode 100644 index 0000000..27280f0 --- /dev/null +++ b/src/main/java/studio/dreamys/icarus/annotation/type/IGroup.java @@ -0,0 +1,13 @@ +package studio.dreamys.icarus.annotation.type; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface IGroup { + double x(); + double y(); +} diff --git a/src/main/java/studio/dreamys/icarus/annotation/type/IPage.java b/src/main/java/studio/dreamys/icarus/annotation/type/IPage.java new file mode 100644 index 0000000..18299fb --- /dev/null +++ b/src/main/java/studio/dreamys/icarus/annotation/type/IPage.java @@ -0,0 +1,12 @@ +package studio.dreamys.icarus.annotation.type; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface IPage { + char value(); +} diff --git a/src/main/java/studio/dreamys/icarus/component/Attachment.java b/src/main/java/studio/dreamys/icarus/component/Attachment.java deleted file mode 100644 index 99b6de6..0000000 --- a/src/main/java/studio/dreamys/icarus/component/Attachment.java +++ /dev/null @@ -1,11 +0,0 @@ -package studio.dreamys.icarus.component; - -public class Attachment extends Component{ - public void attachTo(Component child) { - - } - - public Component getChild() { - return null; - } -} diff --git a/src/main/java/studio/dreamys/icarus/component/Component.java b/src/main/java/studio/dreamys/icarus/component/Component.java index 1bd0a3c..bd041f4 100644 --- a/src/main/java/studio/dreamys/icarus/component/Component.java +++ b/src/main/java/studio/dreamys/icarus/component/Component.java @@ -1,89 +1,69 @@ package studio.dreamys.icarus.component; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.fml.common.eventhandler.Event; -import studio.dreamys.icarus.component.sub.*; -import studio.dreamys.icarus.event.ComponentEvent; +import lombok.Getter; import studio.dreamys.icarus.util.position.Bounds; -//keep this not abstract to avoid some useless empty methods -public class Component { - public void render(int mouseX, int mouseY) { - - } +import java.lang.reflect.Field; - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { +public abstract class Component { + @Getter protected Window window; + @Getter protected Group group; - } - - public void keyTyped(char typedChar, int keyCode) { + @Getter protected double x, y; + @Getter protected double width, height; + protected double relativeX, relativeY; - } + @Getter protected String label; + @Getter protected boolean open; - public void mouseReleased(int mouseX, int mouseY, int state) { + public Field configField; + public Component(String label, double width, double height) { + this.label = label.replaceAll("_", " "); + this.width = width; + this.height = height; } - public void setWindow(Window window) { + public Component() { } - public void setGroup(Group group) { - + public void render(int mouseX, int mouseY) { + x = window.x + relativeX; + y = window.y + relativeY; } - public void setX(double x) { + public void mouseClicked(int mouseX, int mouseY, int mouseButton) { } - public void setY(double y) { - - } + public void keyTyped(char typedChar, int keyCode) { - public Group getGroup() { - return null; } - public Window getWindow() { - return null; - } + public void mouseReleased(int mouseX, int mouseY, int state) { - public double getX() { - return 0; } - public double getY() { - return 0; + public void setWindow(Window window) { + this.window = window; + relativeX = x; + relativeY = y; } - public boolean isOpen() { - return false; + public void setX(double x) { + relativeX = x; } - public double getWidth() { - return 0; + public void setY(double y) { + relativeY = y; } - public double getHeight() { - return 0; + public boolean hovered(double x, double y) { + return x > this.x && x < this.x + width && y > this.y && y < this.y + height; } public Bounds getBounds() { - return null; - } - - public String getLabel() { - return "null"; - } - - public void fireChange() { - Event event = null; - if (this instanceof Checkbox) event = new ComponentEvent.CheckboxEvent((Checkbox) this); - if (this instanceof Choice) event = new ComponentEvent.ChoiceEvent((Choice) this); - if (this instanceof Combo) event = new ComponentEvent.ComboEvent((Combo) this); - if (this instanceof Field) event = new ComponentEvent.FieldEvent((Field) this); - if (this instanceof Keybind) event = new ComponentEvent.KeybindEvent((Keybind) this); - if (this instanceof Slider) event = new ComponentEvent.SliderEvent((Slider) this); - if (event != null) MinecraftForge.EVENT_BUS.post(event); + return new Bounds(width, height); } } \ No newline at end of file diff --git a/src/main/java/studio/dreamys/icarus/component/Group.java b/src/main/java/studio/dreamys/icarus/component/Group.java new file mode 100644 index 0000000..d5ceb6a --- /dev/null +++ b/src/main/java/studio/dreamys/icarus/component/Group.java @@ -0,0 +1,49 @@ +package studio.dreamys.icarus.component; + +import lombok.Getter; +import studio.dreamys.icarus.component.sub.attachment.Attachment; +import studio.dreamys.icarus.util.RenderUtils; + +import java.util.ArrayList; +import java.util.List; + +public class Group extends Component { + @Getter protected Page page; + + @Getter protected List children = new ArrayList<>(); + + public Group(String label, double x, double y) { + super(label, 150, 10); + + this.x = x; + this.y = y; + } + + @Override + public void render(int mouseX, int mouseY) { + //update position + super.render(mouseX, mouseY); + + //outlined box + label + RenderUtils.drawGroupWithString(x, y, width, height + 4, label); + } + + public void addChild(Component child) { + children.add(child); + child.setWindow(window); + child.group = this; + positionChild(child); + } + + public void addChild(Attachment child) { + children.add(child); + child.setWindow(window); + child.group = this; + } + + public void positionChild(Component child) { + child.setX(x + 12.5); + child.setY(y + height + child.getBounds().getOffsetY()); + height += child.getBounds().getHeight() + child.getBounds().getOffsetY() + 7.5; + } +} \ No newline at end of file diff --git a/src/main/java/studio/dreamys/icarus/component/Page.java b/src/main/java/studio/dreamys/icarus/component/Page.java index c34e113..7429477 100644 --- a/src/main/java/studio/dreamys/icarus/component/Page.java +++ b/src/main/java/studio/dreamys/icarus/component/Page.java @@ -1,40 +1,29 @@ package studio.dreamys.icarus.component; import lombok.Getter; -import lombok.Setter; import studio.dreamys.icarus.Icarus; -import studio.dreamys.icarus.component.sub.Group; import studio.dreamys.icarus.util.RenderUtils; import java.awt.*; import java.util.ArrayList; +import java.util.List; -@Getter -@Setter public class Page extends Component { - private Window window; + @Getter private List groups = new ArrayList<>(); - private double x = 3; - private double y = 4; - private double width = 37.5; - private double height = 35; + protected char icon; - private char icon; - private ArrayList groups = new ArrayList<>(); + public Page(String label, char icon) { + super(label, 37.5, 35); - //relative to window, aka x,y passed in constructor - private double relativeX; - private double relativeY; - - public Page(char icon) { this.icon = icon; + x = 3; } @Override public void render(int mouseX, int mouseY) { //update position - x = window.x + relativeX; - y = window.y + relativeY; + super.render(mouseX, mouseY); Color color = Icarus.getWindow().activePageIndex == window.pages.indexOf(this) ? window.color : Color.DARK_GRAY; @@ -48,25 +37,16 @@ public void mouseClicked(int mouseX, int mouseY, int mouseButton) { } } - private boolean hovered(double x, double y) { - return x > this.x && x < this.x + width && y > this.y && y < this.y + height; - } - - public Group addGroup(Group group) { - //add group to list + public void addGroup(Group group) { groups.add(group); - //pass window to group + group.page = this; group.setWindow(window); - return group; } @Override public void setWindow(Window window) { - this.window = window; - - y += height * (window.pages.size() - 1); + y = 4 + height * (window.pages.size() - 1); - relativeX = x; - relativeY = y; + super.setWindow(window); } } diff --git a/src/main/java/studio/dreamys/icarus/component/Window.java b/src/main/java/studio/dreamys/icarus/component/Window.java index 88facdb..e702c0c 100644 --- a/src/main/java/studio/dreamys/icarus/component/Window.java +++ b/src/main/java/studio/dreamys/icarus/component/Window.java @@ -5,13 +5,14 @@ import net.minecraft.util.ResourceLocation; import org.lwjgl.input.Keyboard; import studio.dreamys.icarus.Icarus; -import studio.dreamys.icarus.component.sub.Group; +import studio.dreamys.icarus.component.sub.attachment.Attachment; import studio.dreamys.icarus.util.RenderUtils; import java.awt.*; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; +import java.util.List; public class Window extends GuiScreen { public static Window instance; @@ -25,7 +26,7 @@ public class Window extends GuiScreen { public int activePageIndex; public ArrayList pages = new ArrayList<>(); public ArrayList visible = new ArrayList<>(); - public ArrayList all = new ArrayList<>(); + public List attachments = new ArrayList<>(); //dragging stuff public double dragX; @@ -60,7 +61,7 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) { //draw them sexy bottom strings RenderUtils.drawString("uid: 001", x + 3, y + height - RenderUtils.getFontHeight() - 3, Color.WHITE); - RenderUtils.drawCenteredString(Icarus.getModid(), x + width / 2, y + height - RenderUtils.getFontHeight() - 3, Color.WHITE); + RenderUtils.drawXCenterString(Icarus.getModid(), x + width / 2, y + height - RenderUtils.getFontHeight() - 3, Color.WHITE); RenderUtils.drawString("dxxxxy#0776", x + width - 3 - RenderUtils.getStringWidth("dxxxxy#0776"), y + height - RenderUtils.getFontHeight() - 3, Color.WHITE); update(mouseX, mouseY); @@ -74,8 +75,8 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) { for (Component component : visible) { component.render(mouseX, mouseY); - //draw bounds -// RenderUtils.drawOutline(component.getBounds().getWidth(), component.getBounds().getHeight() + component.getBounds().getOffsetY(), x, y - component.getBounds().getOffsetY(), Color.GREEN); + //render bounds +// RenderUtils.drawOutline(component.x, component.y - component.getBounds().getOffsetY(), component.getBounds().getWidth(), component.getBounds().getHeight() + component.getBounds().getOffsetY(), Color.RED); } } @@ -95,11 +96,8 @@ public void mouseClicked(int mouseX, int mouseY, int mouseButton) { //call for children then for (Component component : Lists.reverse(visible)) { component.mouseClicked(mouseX, mouseY, mouseButton); - if (component.isOpen()) break; //avoid clicking underlying elements when something is open + if (component.open) break; //avoid clicking underlying elements when something is open } - - //save config on mouse click (button, checkbox, choice, etc...) - Icarus.getConfig().save(); } @Override @@ -110,9 +108,6 @@ protected void keyTyped(char typedChar, int keyCode) throws IOException { for (Component component : visible) { component.keyTyped(typedChar, keyCode); } - - //save config on key typed (typically fields) - Icarus.getConfig().save(); } @Override @@ -123,30 +118,22 @@ protected void mouseReleased(int mouseX, int mouseY, int state) { for (Component component : visible) { component.mouseReleased(mouseX, mouseY, state); } - - //save config on mouse release (typically sliders) - Icarus.getConfig().save(); } - private boolean hovered(double x, double y) { + public boolean hovered(double x, double y) { return x > this.x && x < this.x + width && y > this.y && y < this.y + height / 25; //only 1/25 from the top is draggable } - private void update(int mouseX, int mouseY) { + public void update(int mouseX, int mouseY) { if (isDragging) { x = mouseX - dragX; y = mouseY - dragY; } } - public Page addPage(Page page) { - //add page to list + public void addPage(Page page) { pages.add(page); - - //pass window to page page.setWindow(this); - - return page; } public void setActivePage(Page page) { @@ -165,8 +152,4 @@ public void setActivePage(Page page) { //reverse list to render from bottom to top Collections.reverse(visible); } - - public void setKey(int key) { - this.key = key; - } } diff --git a/src/main/java/studio/dreamys/icarus/component/sub/Button.java b/src/main/java/studio/dreamys/icarus/component/sub/Button.java index 1286734..132a6ab 100644 --- a/src/main/java/studio/dreamys/icarus/component/sub/Button.java +++ b/src/main/java/studio/dreamys/icarus/component/sub/Button.java @@ -1,55 +1,42 @@ package studio.dreamys.icarus.component.sub; -import lombok.Getter; -import lombok.Setter; +import lombok.SneakyThrows; import studio.dreamys.icarus.component.Component; -import studio.dreamys.icarus.component.Window; import studio.dreamys.icarus.util.RenderUtils; -import studio.dreamys.icarus.util.position.Bounds; -import java.awt.Color; +import java.awt.*; -@Getter -@Setter public class Button extends Component { - private Window window; - private Group group; + protected boolean held; - private double x; - private double y; - private double width = 80; - private double height = 12; - private String label; - - private Runnable runnable; - private boolean held; - - //relative to window, aka x,y passed in constructor - private double relativeX; - private double relativeY; - - public Button(String label, Runnable runnable) { - this.label = label; + public Button(String label) { + super(label, 80, 12); + } - this.runnable = runnable; + @SneakyThrows + public Runnable getRunnable() { + return (Runnable) configField.get(null); } @Override public void render(int mouseX, int mouseY) { //update position - x = window.x + relativeX; - y = window.y + relativeY; + super.render(mouseX, mouseY); + + //background + RenderUtils.drawGradientRect(x, y, width, height, held ? Color.DARK_GRAY.darker().darker() : Color.DARK_GRAY.darker(), held ? Color.DARK_GRAY.darker() : Color.DARK_GRAY.darker().darker()); - //the component itself + the text written - RenderUtils.drawGradientRect(x, y, x + width, y + height, held ? Color.DARK_GRAY.darker().darker() : Color.DARK_GRAY.darker(), held ? Color.DARK_GRAY.darker() : Color.DARK_GRAY.darker().darker()); - RenderUtils.drawOutline(width, height, x, y, Color.DARK_GRAY); - RenderUtils.drawCenteredString(label, x + width / 2, y + height / 5, Color.WHITE); + //border + RenderUtils.drawOutline(x, y, width, height, Color.DARK_GRAY); + + //label + RenderUtils.drawXYCenterString(label, x + width / 2, y + height / 2, Color.WHITE); } @Override public void mouseClicked(int mouseX, int mouseY, int mouseButton) { if (hovered(mouseX, mouseY) && mouseButton == 0) { - runnable.run(); + getRunnable().run(); held = true; } } @@ -58,30 +45,4 @@ public void mouseClicked(int mouseX, int mouseY, int mouseButton) { public void mouseReleased(int mouseX, int mouseY, int state) { held = false; } - - private boolean hovered(double x, double y) { - return x > this.x && x < this.x + width && y > this.y && y < this.y + height; - } - - @Override - public void setWindow(Window window) { - this.window = window; - relativeX = x; - relativeY = y; - } - - @Override - public void setX(double x) { - relativeX = x; - } - - @Override - public void setY(double y) { - relativeY = y; - } - - @Override - public Bounds getBounds() { - return new Bounds(width, height); - } } \ No newline at end of file diff --git a/src/main/java/studio/dreamys/icarus/component/sub/Checkbox.java b/src/main/java/studio/dreamys/icarus/component/sub/Checkbox.java index 3d2fa01..6e33297 100644 --- a/src/main/java/studio/dreamys/icarus/component/sub/Checkbox.java +++ b/src/main/java/studio/dreamys/icarus/component/sub/Checkbox.java @@ -1,90 +1,61 @@ package studio.dreamys.icarus.component.sub; -import lombok.Getter; -import lombok.Setter; +import lombok.SneakyThrows; +import net.minecraftforge.common.MinecraftForge; import studio.dreamys.icarus.component.Component; -import studio.dreamys.icarus.component.Window; -import studio.dreamys.icarus.util.position.Bounds; +import studio.dreamys.icarus.config.Config; +import studio.dreamys.icarus.event.ComponentEvent; import studio.dreamys.icarus.util.RenderUtils; +import studio.dreamys.icarus.util.position.Bounds; -import java.awt.Color; +import java.awt.*; -@Getter -@Setter public class Checkbox extends Component { - private Window window; - private Group group; - - private double x; - private double y; - private double width = 5; - private double height = 5; - private String label; - - private boolean toggled; - - //relative to window, aka x,y passed in constructor - private double relativeX; - private double relativeY; - public Checkbox(String label) { - this.label = label; + super(label, 5, 5); } - public Checkbox(String label, boolean toggled) { - this.label = label; + @SneakyThrows + public boolean isToggled() { + return configField.getBoolean(null); + } - this.toggled = toggled; + @SneakyThrows + private void setToggled(boolean toggled) { + configField.set(null, toggled); } @Override public void render(int mouseX, int mouseY) { //update position - x = window.x + relativeX; - y = window.y + relativeY; + super.render(mouseX, mouseY); + + //toggle color + Color color = isToggled() ? window.color : Color.DARK_GRAY; - Color color = toggled ? window.color : Color.DARK_GRAY; + //background + RenderUtils.drawGradientRect(x, y, width, height, color, color.darker().darker()); - //the box itself + label next to it - RenderUtils.drawGradientRect(x, y, x + width, y + height, color, color.darker().darker()); - RenderUtils.drawString(label, x + width * 2 - 1, y - height / 2 + 0.75, Color.WHITE); + //label + RenderUtils.drawString(label, x + width * 2 - 1, y - height / 2 + 0.75, Color.WHITE); } @Override public void mouseClicked(int mouseX, int mouseY, int mouseButton) { if (hovered(mouseX, mouseY) && mouseButton == 0) { - toggled = !toggled; - fireChange(); + toggle(); } } - private boolean hovered(double x, double y) { - return x > this.x && x < this.x + width && y > this.y && y < this.y + height; - } - - public void toggle() { - toggled = !toggled; - } - - @Override - public void setWindow(Window window) { - this.window = window; - relativeX = x; - relativeY = y; - } - @Override - public void setX(double x) { - relativeX = x; + public Bounds getBounds() { + return new Bounds(80, height); } - @Override - public void setY(double y) { - relativeY = y; - } + public void toggle() { + setToggled(!isToggled()); - @Override - public Bounds getBounds() { - return new Bounds(80, height); + Config.save(); + MinecraftForge.EVENT_BUS.post(new ComponentEvent.CheckboxEvent()); } } \ No newline at end of file diff --git a/src/main/java/studio/dreamys/icarus/component/sub/Choice.java b/src/main/java/studio/dreamys/icarus/component/sub/Choice.java index 7ac6eb3..e01ad86 100644 --- a/src/main/java/studio/dreamys/icarus/component/sub/Choice.java +++ b/src/main/java/studio/dreamys/icarus/component/sub/Choice.java @@ -1,92 +1,78 @@ package studio.dreamys.icarus.component.sub; -import lombok.Getter; -import lombok.Setter; +import lombok.SneakyThrows; +import net.minecraftforge.common.MinecraftForge; import studio.dreamys.icarus.component.Component; -import studio.dreamys.icarus.component.Window; -import studio.dreamys.icarus.util.position.Bounds; +import studio.dreamys.icarus.config.Config; +import studio.dreamys.icarus.event.ComponentEvent; import studio.dreamys.icarus.util.RenderUtils; +import studio.dreamys.icarus.util.position.Bounds; -import java.awt.Color; -import java.util.ArrayList; -import java.util.concurrent.atomic.AtomicReference; +import java.awt.*; +import java.util.List; -@Getter -@Setter public class Choice extends Component { - private Window window; - private Group group; - - private double x; - private double y; - private double width = 80; - private double height = 12; - private String label; - - private boolean open; - private ArrayList options; - private String selected; + protected List options; - //relative to window, aka x,y passed in constructor - private double relativeX; - private double relativeY; - - public Choice(String label, ArrayList options) { - this.label = label; + public Choice(String label, List options) { + super(label, 80, 12); this.options = options; - selected = this.options.get(0); } - public Choice(String label, ArrayList options, String selected) { - this.label = label; + @SneakyThrows + public String getSelected() { + return (String) configField.get(null); + } - this.options = options; - this.selected = selected; + @SneakyThrows + private void setSelected(String selected) { + configField.set(null, selected); } @Override public void render(int mouseX, int mouseY) { //update position - x = window.x + relativeX; - y = window.y + relativeY; + super.render(mouseX, mouseY); - //the component itself + the chosen option - RenderUtils.drawRect(x, y, x + width, y + height, Color.DARK_GRAY.darker().darker()); - RenderUtils.drawString(selected, x + 4, y + height / 10, Color.WHITE); + //background + RenderUtils.drawRect(x, y, width, height, Color.DARK_GRAY.darker().darker()); + + //selected + RenderUtils.drawYCenterString(getSelected(), x + 4, y + height / 2 - 1, Color.WHITE); //dropdown symbol - RenderUtils.drawString("v", x + width - 8, y, Color.WHITE); + RenderUtils.drawYCenterString("v", x + width - 8, y + height / 2 - 1, Color.WHITE); //label - RenderUtils.drawString(label, x, y - height / 1.5, Color.WHITE); - - //lambda stacking stuff - AtomicReference currentY = new AtomicReference<>(y); - currentY.updateAndGet(v -> v + height); + RenderUtils.drawString(label, x - 1, y - height / 2 - 2, Color.WHITE); //open dropdown menu if (open) { options.forEach(option -> { - Color color = option.equals(selected) ? window.color : Color.WHITE; - RenderUtils.drawRect(x, currentY.get(), x + width, currentY.get() + height, Color.DARK_GRAY.darker().darker()); - RenderUtils.drawString(option, x + 4, currentY.get() + height / 10, color); - currentY.updateAndGet(v -> v + height); + Color color = option.equals(getSelected()) ? window.color : Color.WHITE; + + //background + RenderUtils.drawRect(x, y + height * (options.indexOf(option) + 1), width, height, Color.DARK_GRAY.darker().darker()); + + //option + RenderUtils.drawYCenterString(option, x + 4, y + height * (options.indexOf(option) + 1) + height / 2 - 1, color); }); } } - - @SuppressWarnings("ConstantConditions") @Override public void mouseClicked(int mouseX, int mouseY, int mouseButton) { if (open && mouseButton == 0) { if (mouseX > x && mouseX < x + width && mouseY > y + height && mouseY < y + height * (options.size() + 1)) { double posY = mouseY - y - height; int index = (int) (posY / height); - selected = options.get(index); - fireChange(); - } else open = !open; + + setSelected(options.get(index)); + + Config.save(); + MinecraftForge.EVENT_BUS.post(new ComponentEvent.ChoiceEvent()); + } else open = false; return; } @@ -95,27 +81,6 @@ public void mouseClicked(int mouseX, int mouseY, int mouseButton) { } } - private boolean hovered(double x, double y) { - return x > this.x && x < this.x + width && y > this.y && y < this.y + height; - } - - @Override - public void setWindow(Window window) { - this.window = window; - relativeX = x; - relativeY = y; - } - - @Override - public void setX(double x) { - relativeX = x; - } - - @Override - public void setY(double y) { - relativeY = y; - } - @Override public Bounds getBounds() { return new Bounds(width, height, 7); diff --git a/src/main/java/studio/dreamys/icarus/component/sub/Color.java b/src/main/java/studio/dreamys/icarus/component/sub/Color.java deleted file mode 100644 index edb1474..0000000 --- a/src/main/java/studio/dreamys/icarus/component/sub/Color.java +++ /dev/null @@ -1,15 +0,0 @@ -package studio.dreamys.icarus.component.sub; - -import studio.dreamys.icarus.component.Attachment; -import studio.dreamys.icarus.component.Component; -import studio.dreamys.icarus.util.RenderUtils; - -//TODO: implement this class -public class Color extends Attachment { - private Component child; - private java.awt.Color color = new java.awt.Color(51, 192, 227); - - public void render(int mouseX, int mouseY) { - RenderUtils.drawGradientRect(child.getX() + child.getWidth() + 115, child.getY(), child.getX() + child.getWidth() + 125, child.getY() + 5, color, color.darker().darker().darker()); - } -} diff --git a/src/main/java/studio/dreamys/icarus/component/sub/Combo.java b/src/main/java/studio/dreamys/icarus/component/sub/Combo.java index b917bb5..0c6a0d4 100644 --- a/src/main/java/studio/dreamys/icarus/component/sub/Combo.java +++ b/src/main/java/studio/dreamys/icarus/component/sub/Combo.java @@ -1,81 +1,68 @@ package studio.dreamys.icarus.component.sub; -import lombok.Getter; -import lombok.Setter; +import lombok.SneakyThrows; +import net.minecraftforge.common.MinecraftForge; import studio.dreamys.icarus.component.Component; -import studio.dreamys.icarus.component.Window; -import studio.dreamys.icarus.util.position.Bounds; +import studio.dreamys.icarus.config.Config; +import studio.dreamys.icarus.event.ComponentEvent; import studio.dreamys.icarus.util.RenderUtils; +import studio.dreamys.icarus.util.position.Bounds; -import java.awt.Color; +import java.awt.*; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicReference; +import java.util.Arrays; +import java.util.List; -@Getter -@Setter public class Combo extends Component { - private Window window; - private Group group; - - private double x; - private double y; - private double width = 80; - private double height = 12; - private String label; + protected List options; - private boolean open; - private HashMap options = new HashMap<>(); + public Combo(String label, List options) { + super(label, 80, 12); - //relative to window, aka x,y passed in constructor - private double relativeX; - private double relativeY; - - public Combo(String label, ArrayList options) { - this.label = label; - - options.forEach(option -> this.options.put(option, false)); + this.options = options; } - public Combo(String label, HashMap options) { - this.label = label; + @SneakyThrows + public List getActive() { + return new ArrayList<>(Arrays.asList((String[]) configField.get(null))); + } - this.options = options; + @SneakyThrows + private void setActive(List active) { + configField.set(null, active.toArray(new String[0])); } @Override public void render(int mouseX, int mouseY) { //update position - x = window.x + relativeX; - y = window.y + relativeY; + super.render(mouseX, mouseY); + + //background + RenderUtils.drawRect(x, y, width, height, Color.DARK_GRAY.darker().darker()); - //the component itself + the active options displayed - RenderUtils.drawRect(x, y, x + width, y + height, Color.DARK_GRAY.darker().darker()); - RenderUtils.drawString(activeOptions(), x + 4, y + height / 10, Color.WHITE); + //selected + RenderUtils.drawYCenterString(activeOptions(), x + 4, y + height / 2 - 1, Color.WHITE); //dropdown symbol - RenderUtils.drawString("v", x + width - 8, y, Color.WHITE); + RenderUtils.drawYCenterString("v", x + width - 8, y + height / 2 - 1, Color.WHITE); //label - RenderUtils.drawString(label, x, y - height / 1.5, Color.WHITE); - - //lambda stacking stuff - AtomicReference currentY = new AtomicReference<>(y); - currentY.updateAndGet(v -> v + height); + RenderUtils.drawString(label, x - 1, y - height / 2 - 2, Color.WHITE); //open dropdown menu if (open) { - options.forEach((option, active) -> { - Color color = active ? window.color : Color.WHITE; - RenderUtils.drawRect(x, currentY.get(), x + width, currentY.get() + height, Color.DARK_GRAY.darker().darker()); - RenderUtils.drawString(option, x + 4, currentY.get() + height / 10, color); - currentY.updateAndGet(v -> v + height); + options.forEach(option -> { + Color color = getActive().contains(option) ? window.color : Color.WHITE; + + //background + RenderUtils.drawRect(x, y + height * (options.indexOf(option) + 1), width, height, Color.DARK_GRAY.darker().darker()); + + //option + RenderUtils.drawYCenterString(option, x + 4, y + height * (options.indexOf(option) + 1) + height / 2 - 1, color); }); } } - @SuppressWarnings("ConstantConditions") @Override public void mouseClicked(int mouseX, int mouseY, int mouseButton) { if (open && mouseButton == 0) { @@ -83,14 +70,23 @@ public void mouseClicked(int mouseX, int mouseY, int mouseButton) { double posY = mouseY - y - height; int index = (int) (posY / height); int i = 0; - for (Map.Entry entry : options.entrySet()) { + + List active = getActive(); + + for (String option : options) { if (i == index) { - options.put(entry.getKey(), !entry.getValue()); + if (active.contains(option)) active.remove(option); + else active.add(option); + break; } i++; } - fireChange(); - } else open = !open; + + setActive(active); + + Config.save(); + MinecraftForge.EVENT_BUS.post(new ComponentEvent.ComboEvent()); + } else open = false; return; } @@ -99,17 +95,15 @@ public void mouseClicked(int mouseX, int mouseY, int mouseButton) { } } - private boolean hovered(double x, double y) { - return x > this.x && x < this.x + width && y > this.y && y < this.y + height; + @Override + public Bounds getBounds() { + return new Bounds(width, height, 7); } - //display active options private String activeOptions() { StringBuilder formatted = new StringBuilder(); - for (Map.Entry option : options.entrySet()) { - if (option.getValue()) { - formatted.append(option.getKey()).append(", "); - } + for (String option : getActive()) { + formatted.append(option).append(", "); } //remove last comma @@ -125,50 +119,4 @@ private String activeOptions() { return formatted.toString(); } - - public String getActiveOptions() { - StringBuilder formatted = new StringBuilder(); - for (Map.Entry option : options.entrySet()) { - if (option.getValue()) { - formatted.append(option.getKey()).append(","); - } - } - - //remove last comma - if (formatted.length() > 0) { - formatted.deleteCharAt(formatted.length() - 1); - } - - return formatted.length() > 0 ? formatted.toString() : "null"; - } - - public void setActiveOptions(String activeOptions) { - if (activeOptions.equals("null")) return; - String[] options = activeOptions.split(","); - for (String option : options) { - this.options.put(option, true); - } - } - - @Override - public void setWindow(Window window) { - this.window = window; - relativeX = x; - relativeY = y; - } - - @Override - public void setX(double x) { - relativeX = x; - } - - @Override - public void setY(double y) { - relativeY = y; - } - - @Override - public Bounds getBounds() { - return new Bounds(width, height, 7); - } } \ No newline at end of file diff --git a/src/main/java/studio/dreamys/icarus/component/sub/Field.java b/src/main/java/studio/dreamys/icarus/component/sub/Field.java index 4aed003..7643c95 100644 --- a/src/main/java/studio/dreamys/icarus/component/sub/Field.java +++ b/src/main/java/studio/dreamys/icarus/component/sub/Field.java @@ -1,57 +1,50 @@ package studio.dreamys.icarus.component.sub; -import lombok.Getter; -import lombok.Setter; +import lombok.SneakyThrows; +import net.minecraftforge.common.MinecraftForge; import org.lwjgl.input.Keyboard; import studio.dreamys.icarus.component.Component; -import studio.dreamys.icarus.component.Window; -import studio.dreamys.icarus.util.position.Bounds; +import studio.dreamys.icarus.config.Config; +import studio.dreamys.icarus.event.ComponentEvent; import studio.dreamys.icarus.util.RenderUtils; +import studio.dreamys.icarus.util.position.Bounds; import java.awt.Color; import java.util.regex.Pattern; -@Getter -@Setter public class Field extends Component { - private Window window; - private Group group; - - private double x; - private double y; - private double width = 80; - private double height = 12; - private String label; - - private String text = ""; - private boolean focused; - - //relative to window, aka x,y passed in constructor - private double relativeX; - private double relativeY; + protected boolean focused; public Field(String label) { - this.label = label; + super(label, 80, 12); } - public Field(String label, String text) { - this.label = label; - this.text = text; + @SneakyThrows + public String getText() { + return (String) configField.get(null); + } + + @SneakyThrows + private void setText(String text) { + configField.set(null, text); } @Override public void render(int mouseX, int mouseY) { //update position - x = window.x + relativeX; - y = window.y + relativeY; + super.render(mouseX, mouseY); + + //background + RenderUtils.drawRect(x, y, width, height, Color.DARK_GRAY.darker().darker()); + + //border + RenderUtils.drawOutline(x, y, width, height, Color.DARK_GRAY); - //the component itself + the text written - RenderUtils.drawRect(x, y, x + width, y + height, Color.DARK_GRAY.darker().darker()); - RenderUtils.drawOutline(width, height, x, y, Color.DARK_GRAY); - RenderUtils.drawString(focused ? text + "_" : text, x + 4, y + height / 10, Color.WHITE); + //text + RenderUtils.drawYCenterString(focused ? getText() + "_" : getText(), x + 4, y + height / 2, Color.WHITE); //label - RenderUtils.drawString(label, x, y - height / 1.5, Color.WHITE); + RenderUtils.drawString(label, x - 1, y - height / 2 - 2, Color.WHITE); } @Override @@ -63,40 +56,16 @@ public void mouseClicked(int mouseX, int mouseY, int mouseButton) { public void keyTyped(char typedChar, int keyCode) { if (focused) { if (keyCode == Keyboard.KEY_BACK) { - if (text.length() > 0) { - text = text.substring(0, text.length() - 1); - fireChange(); + if (getText().length() > 0) { + setText(getText().substring(0, getText().length() - 1)); } } else if (Pattern.compile("[a-zA-Z0-9\\t\\n ./<>?;:\"'`!@#$%^&*()\\[\\]{}_+=~|\\\\-]").matcher(String.valueOf(typedChar)).find()) { - text += typedChar; - fireChange(); + setText(getText() + typedChar); } - } - } - private boolean hovered(double x, double y) { - return x > this.x && x < this.x + width && y > this.y && y < this.y + height; - } - - public String getText() { - return text.equals("") ? "null" : text; //for config purposes - } - - @Override - public void setWindow(Window window) { - this.window = window; - relativeX = x; - relativeY = y; - } - - @Override - public void setX(double x) { - relativeX = x; - } - - @Override - public void setY(double y) { - relativeY = y; + Config.save(); + MinecraftForge.EVENT_BUS.post(new ComponentEvent.FieldEvent()); + } } @Override diff --git a/src/main/java/studio/dreamys/icarus/component/sub/Group.java b/src/main/java/studio/dreamys/icarus/component/sub/Group.java deleted file mode 100644 index a442ad4..0000000 --- a/src/main/java/studio/dreamys/icarus/component/sub/Group.java +++ /dev/null @@ -1,97 +0,0 @@ -package studio.dreamys.icarus.component.sub; - -import lombok.Getter; -import lombok.Setter; -import org.lwjgl.util.Rectangle; -import studio.dreamys.icarus.component.Attachment; -import studio.dreamys.icarus.component.Component; -import studio.dreamys.icarus.component.Window; -import studio.dreamys.icarus.util.RenderUtils; - -import java.awt.*; -import java.util.ArrayList; - -@Getter -@Setter -public class Group extends Component { - private Window window; - private Group group; - - private double x; - private double y = 5; - private double width = 150; - private double height = 10; - private String label; - - private ArrayList children = new ArrayList<>(); - - //relative to window, aka x,y passed in constructor - private double relativeX; - private double relativeY; - - public Group(String label) { - this.label = label; - } - - public Group(String label, double x, double y) { - this.label = label; - this.x = x; - this.y = y; - } - - public Group(double x, double y, double width, double height, String label) { - this.x = x; - this.y = y; - this.width = width; - this.height = height; - this.label = label; - } - - @Override - public void render(int mouseX, int mouseY) { - //update position - x = window.x + relativeX; - y = window.y + relativeY; - - //the box itself - RenderUtils.drawGroupWithString(width, height + 4, x, y, label); - } - - @Override - public void setWindow(Window window) { - this.window = window; - relativeX = x; - relativeY = y; - } - - public Group addChild(Component child) { - child.setWindow(window); - child.setGroup(this); - children.add(child); - - positionChild(child); - - return this; - } - - public Group addChild(Component child, Attachment... attachments) { - child.setWindow(window); - child.setGroup(this); - children.add(child); - - positionChild(child); - - for (Attachment attachment : attachments) { - attachment.attachTo(child); - children.add(attachment); - } - - return this; - } - - public void positionChild(Component child) { - child.setX(x + 12.5); - child.setY(y + height + child.getBounds().getOffsetY()); - height += child.getBounds().getHeight() + child.getBounds().getOffsetY() + 5; - } -} \ No newline at end of file diff --git a/src/main/java/studio/dreamys/icarus/component/sub/Slider.java b/src/main/java/studio/dreamys/icarus/component/sub/Slider.java index 0df1675..a3b15e8 100644 --- a/src/main/java/studio/dreamys/icarus/component/sub/Slider.java +++ b/src/main/java/studio/dreamys/icarus/component/sub/Slider.java @@ -1,91 +1,64 @@ package studio.dreamys.icarus.component.sub; -import lombok.Getter; -import lombok.Setter; +import lombok.SneakyThrows; +import net.minecraftforge.common.MinecraftForge; import studio.dreamys.icarus.component.Component; -import studio.dreamys.icarus.component.Window; -import studio.dreamys.icarus.util.position.Bounds; +import studio.dreamys.icarus.config.Config; +import studio.dreamys.icarus.event.ComponentEvent; import studio.dreamys.icarus.util.RenderUtils; +import studio.dreamys.icarus.util.position.Bounds; -import java.awt.Color; +import java.awt.*; import java.math.BigDecimal; import java.math.RoundingMode; -@Getter -@Setter public class Slider extends Component { - private Window window; - private Group group; - - private double x; - private double y; - private double width = 80; - private double height = 3; - private String label; + protected double max; + protected double min; + protected boolean onlyInt; + protected String units; - private double max; - private double min; - private boolean onlyInt; - private double percent; - private double value; - private String units; + protected double percent; - //relative to window, aka x,y passed in constructor - private double relativeX; - private double relativeY; + protected boolean dragging; - //dragging stuff - public boolean dragging; - - public Slider(String label, double min, double max, boolean onlyInt) { - this.label = label; + public Slider(String label, double min, double max, boolean onlyInt, String units) { + super(label, 80, 3); this.min = min; this.max = max; this.onlyInt = onlyInt; - - units = ""; + this.units = units; } - public Slider(String label, double value, double min, double max, boolean onlyInt) { - this.label = label; - - this.value = value; - this.min = min; - this.max = max; - this.onlyInt = onlyInt; - - units = ""; + @SneakyThrows + public double getValue() { + double value = configField.getDouble(null); + percent = (value - min) / (max - min); + return value; } - public Slider(String label, double value, double min, double max, boolean onlyInt, String units) { - this.label = label; - - this.value = value; - this.min = min; - this.max = max; - this.onlyInt = onlyInt; - - this.units = units; + @SneakyThrows + private void setValue(double value) { + configField.set(null, value); } @Override public void render(int mouseX, int mouseY) { //update position - x = window.x + relativeX; - y = window.y + relativeY; + super.render(mouseX, mouseY); - //unfilled - RenderUtils.drawGradientRect(x, y, x + width, y + height, Color.DARK_GRAY, Color.DARK_GRAY.darker().darker()); + //unfilled background + RenderUtils.drawGradientRect(x, y, width, height, Color.DARK_GRAY, Color.DARK_GRAY.darker().darker()); - //filled - RenderUtils.drawGradientRect(x, y, x + width * percent, y + height, window.color, window.color.darker().darker()); + //filled background + RenderUtils.drawGradientRect(x, y, width * percent, height, window.color, window.color.darker().darker()); //label - RenderUtils.drawString(label, x - 1, y - height * 2.5, Color.WHITE); + RenderUtils.drawString(label, x - 1, y - height * 2 - 2, Color.WHITE); //value - RenderUtils.drawString((onlyInt ? Integer.toString((int) value) : String.valueOf(value)) + units, x - 1 + width * percent, y + height - 1, Color.WHITE); + RenderUtils.drawXCenterString((onlyInt ? Integer.toString((int) getValue()) : String.valueOf(getValue())) + units, x + width * percent, y + height - 1, Color.WHITE); update(mouseX); } @@ -102,8 +75,9 @@ public void mouseReleased(int mouseX, int mouseY, int state) { dragging = false; } - private boolean hovered(double x, double y) { - return x > this.x && x < this.x + width && y > this.y && y < this.y + height; + @Override + public Bounds getBounds() { + return new Bounds(width, height + 5, 7); } private double roundToPlace(double value) { @@ -120,35 +94,10 @@ private void update(int mouseX) { percent = value; - this.value = onlyInt ? Math.round(min + (max - min) * percent) : roundToPlace(min + (max - min) * percent); - fireChange(); + //set and save + setValue(onlyInt ? Math.round(min + (max - min) * percent) : roundToPlace(min + (max - min) * percent)); + Config.save(); + MinecraftForge.EVENT_BUS.post(new ComponentEvent.SliderEvent()); } } - - public void setValue(double value) { - this.value = value; - percent = (value - min) / (max - min); - } - - @Override - public void setWindow(Window window) { - this.window = window; - relativeX = x; - relativeY = y; - } - - @Override - public void setX(double x) { - relativeX = x; - } - - @Override - public void setY(double y) { - relativeY = y; - } - - @Override - public Bounds getBounds() { - return new Bounds(width, height + 5, 7); - } } \ No newline at end of file diff --git a/src/main/java/studio/dreamys/icarus/component/sub/attachment/Attachment.java b/src/main/java/studio/dreamys/icarus/component/sub/attachment/Attachment.java new file mode 100644 index 0000000..c37deb2 --- /dev/null +++ b/src/main/java/studio/dreamys/icarus/component/sub/attachment/Attachment.java @@ -0,0 +1,17 @@ +package studio.dreamys.icarus.component.sub.attachment; + +import lombok.Getter; +import studio.dreamys.icarus.Icarus; +import studio.dreamys.icarus.component.Component; + +public abstract class Attachment extends Component { + @Getter protected Component child; + + protected double x, y; + + public void attachTo(Component child) { + this.child = child; //set child + this.child.getGroup().addChild(this); //add for render + Icarus.getWindow().attachments.add(this); //add for config + } +} diff --git a/src/main/java/studio/dreamys/icarus/component/sub/Keybind.java b/src/main/java/studio/dreamys/icarus/component/sub/attachment/sub/Keybind.java similarity index 51% rename from src/main/java/studio/dreamys/icarus/component/sub/Keybind.java rename to src/main/java/studio/dreamys/icarus/component/sub/attachment/sub/Keybind.java index 680f626..3699607 100644 --- a/src/main/java/studio/dreamys/icarus/component/sub/Keybind.java +++ b/src/main/java/studio/dreamys/icarus/component/sub/attachment/sub/Keybind.java @@ -1,44 +1,39 @@ -package studio.dreamys.icarus.component.sub; +package studio.dreamys.icarus.component.sub.attachment.sub; import lombok.Getter; -import lombok.Setter; +import net.minecraftforge.common.MinecraftForge; import org.lwjgl.input.Keyboard; -import studio.dreamys.icarus.component.Attachment; -import studio.dreamys.icarus.component.Component; +import studio.dreamys.icarus.component.sub.attachment.Attachment; +import studio.dreamys.icarus.config.Config; +import studio.dreamys.icarus.event.ComponentEvent; import studio.dreamys.icarus.util.RenderUtils; -import java.awt.Color; +import java.awt.*; -@Getter -@Setter public class Keybind extends Attachment { - private Component child; + @Getter protected int key; - private double x; - private double y; - - private String keybind = "[NONE]"; - private int key; - private boolean capturing; - - public Keybind() { + protected String keybind = "[NONE]"; + protected boolean capturing; + public Keybind(int key) { + setKey(key); } - public Keybind(int key) { + public void setKey(int key) { this.key = key; + keybind = "["+ Keyboard.getKeyName(key) +"]"; + + Config.saveAttachments(); + MinecraftForge.EVENT_BUS.post(new ComponentEvent.KeybindEvent()); } @Override public void render(int mouseX, int mouseY) { - x = child.getX() + child.getGroup().getWidth() - 50; - y = child.getY() + child.getHeight() / 8; - RenderUtils.drawString(keybind, x, y, Color.WHITE); - } + x = getChild().getX() + getChild().getBounds().getWidth() + 10; + y = (getChild().getY() + getChild().getBounds().getHeight() / 2) - RenderUtils.getFontHeight() / 2; - @Override - public void attachTo(Component child) { - this.child = child; + RenderUtils.drawString(keybind, x, y, Color.WHITE); } @Override @@ -53,29 +48,13 @@ public void mouseClicked(int mouseX, int mouseY, int mouseButton) { @Override public void keyTyped(char typedChar, int keyCode) { if (capturing) { - key = keyCode; - keybind = "["+ Keyboard.getKeyName(key) +"]"; + setKey(keyCode); capturing = false; - fireChange(); } } - private boolean hovered(double x, double y) { - return x > this.x && x < this.x + RenderUtils.getStringWidth(keybind) && y > this.y && y < this.y + RenderUtils.getFontHeight(); - } - - public void setKey(int key) { - this.key = key; - keybind = "["+ Keyboard.getKeyName(key) +"]"; - } - @Override - public String getLabel() { - return child.getLabel(); - } - - @Override - public Group getGroup() { - return child.getGroup(); + public boolean hovered(double x, double y) { + return x > this.x && x < this.x + RenderUtils.getStringWidth(keybind) && y > this.y && y < this.y + RenderUtils.getFontHeight(); } } diff --git a/src/main/java/studio/dreamys/icarus/config/Config.java b/src/main/java/studio/dreamys/icarus/config/Config.java index d831a83..ab2d767 100644 --- a/src/main/java/studio/dreamys/icarus/config/Config.java +++ b/src/main/java/studio/dreamys/icarus/config/Config.java @@ -1,171 +1,389 @@ package studio.dreamys.icarus.config; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import net.minecraft.client.Minecraft; +import org.reflections.Reflections; import studio.dreamys.icarus.Icarus; +import studio.dreamys.icarus.annotation.field.DropdownOptions; +import studio.dreamys.icarus.annotation.field.IKeybind; +import studio.dreamys.icarus.annotation.field.SliderOptions; +import studio.dreamys.icarus.annotation.type.IGroup; +import studio.dreamys.icarus.annotation.type.IPage; import studio.dreamys.icarus.component.Component; +import studio.dreamys.icarus.component.Group; +import studio.dreamys.icarus.component.Page; import studio.dreamys.icarus.component.sub.*; +import studio.dreamys.icarus.component.sub.attachment.Attachment; +import studio.dreamys.icarus.component.sub.attachment.sub.Keybind; +import studio.dreamys.icarus.reflection.ReflectionCache; -import java.io.*; -import java.util.ArrayList; -import java.util.HashMap; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.*; -@SuppressWarnings("unused") public class Config { - public static File file; - private Component comp; + public static File configFile, keybindFile; + public static Reflections reflections; + public static Gson gson; + public static List reflectionCaches = new ArrayList<>(); + public static Map, Class> componentReplacements = new HashMap<>(); @SuppressWarnings("ResultOfMethodCallIgnored") - public Config(String modid) { - File dir = new File(Minecraft.getMinecraft().mcDataDir, modid); - file = new File(dir, "config.txt"); + public static void init(String modid) { + //init files + configFile = new File(new File(Minecraft.getMinecraft().mcDataDir, modid), "config.json"); + keybindFile = new File(new File(Minecraft.getMinecraft().mcDataDir, modid), "keybind.json"); try { - if (!dir.exists()) dir.mkdir(); - if (!file.exists()) file.createNewFile(); + configFile.getParentFile().mkdirs(); + configFile.createNewFile(); + keybindFile.createNewFile(); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); + } + + //init gson + gson = new GsonBuilder().setPrettyPrinting().create(); + + //init reflection + reflections = new Reflections(); + for (Class iPage : reflections.getTypesAnnotatedWith(IPage.class)) { + Map, Field[]> iGroupMap = new HashMap<>(); + + Class[] iGroups = iPage.getDeclaredClasses(); + for (Class iGroup : iGroups) { + iGroupMap.put(iGroup, iGroup.getDeclaredFields()); + } + + reflectionCaches.add(new ReflectionCache(iPage, iGroupMap)); } + + load(); + save(); + + generateWindow(); + + loadAttachments(); + saveAttachments(); } - public void save() { - try { - ArrayList toSave = new ArrayList<>(); + public static void generateWindow() { + for (ReflectionCache reflectionCache : reflectionCaches) { + Class iPage = reflectionCache.getIPage(); - for (Component comp : Icarus.getWindow().all) { - //do nothing for button - if (comp instanceof Checkbox) { - toSave.add("Checkbox:" + comp.getLabel() + ":" + comp.getGroup().getLabel() + ":" + ((Checkbox) comp).isToggled()); - } - if (comp instanceof Choice) { - toSave.add("Choice:" + comp.getLabel() + ":" + comp.getGroup().getLabel() + ":" + ((Choice) comp).getSelected()); - } - //TODO: color - if (comp instanceof Combo) { - toSave.add("Combo:" + comp.getLabel() + ":" + comp.getGroup().getLabel() + ":" + ((Combo) comp).getActiveOptions()); - } - if (comp instanceof Field) { - toSave.add("Field:" + comp.getLabel() + ":" + comp.getGroup().getLabel() + ":" + ((Field) comp).getText()); + IPage annotatedPage = iPage.getAnnotation(IPage.class); //get annotation + Component page = new Page(iPage.getSimpleName(), annotatedPage.value()); //create page object + + if (componentReplacements.containsKey(Page.class)) { + try { + page = componentReplacements.get(Page.class).getConstructor(String.class, char.class).newInstance(iPage.getSimpleName(), annotatedPage.value()); + } catch (Exception e) { + e.printStackTrace(); } - //do nothing for Group - if (comp instanceof Keybind) { - toSave.add("Keybind:" + ((Keybind) comp).getChild().getLabel() + ":" + ((Keybind) comp).getChild().getGroup().getLabel() + ":" + ((Keybind) comp).getKey()); + } + + Icarus.getWindow().addPage((Page) page); //add page to window + + for (Map.Entry, Field[]> entry : reflectionCache.getIGroupMap().entrySet()) { + Class iGroup = entry.getKey(); + Field[] iSettings = entry.getValue(); + + IGroup annotatedGroup = iGroup.getAnnotation(IGroup.class); //get annotation + Component group = new Group(iGroup.getSimpleName(), annotatedGroup.x(), annotatedGroup.y()); //create group object + + if (componentReplacements.containsKey(Group.class)) { + try { + group = componentReplacements.get(Group.class).getConstructor(String.class, double.class, double.class).newInstance(iGroup.getSimpleName(), annotatedGroup.x(), annotatedGroup.y()); + } catch (Exception e) { + e.printStackTrace(); + } } - if (comp instanceof Slider) { - toSave.add("Slider:" + comp.getLabel() + ":" + comp.getGroup().getLabel() + ":" + ((Slider) comp).getValue()); + + ((Page) page).addGroup((Group) group); //add group to page + + for (Field iSetting : iSettings) { //for every setting + Component component = null; //create component object + + iSetting.setAccessible(true); //set accessible + + //get additional annotations + DropdownOptions dropdownOptions = iSetting.getAnnotation(DropdownOptions.class); + SliderOptions sliderOptions = iSetting.getAnnotation(SliderOptions.class); + IKeybind iKeybind = iSetting.getAnnotation(IKeybind.class); + + if (iSetting.getType() == Runnable.class) { //if button + component = new Button(iSetting.getName()); + if (componentReplacements.containsKey(Button.class)) { + try { + component = componentReplacements.get(Button.class).getConstructor(String.class).newInstance(iSetting.getName()); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + else if (iSetting.getType() == boolean.class) { //if checkbox + component = new Checkbox(iSetting.getName()); + if (componentReplacements.containsKey(Checkbox.class)) { + try { + System.out.println("using custom checkbox"); + component = componentReplacements.get(Checkbox.class).getConstructor(String.class).newInstance(iSetting.getName()); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + else if (iSetting.getType() == String.class && dropdownOptions != null) { //if choice + component = new Choice(iSetting.getName(), Arrays.asList(dropdownOptions.value())); + if (componentReplacements.containsKey(Choice.class)) { + try { + component = componentReplacements.get(Choice.class).getConstructor(String.class, List.class).newInstance(iSetting.getName(), Arrays.asList(dropdownOptions.value())); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + else if (iSetting.getType() == String[].class && dropdownOptions != null) { //if combo + component = new Combo(iSetting.getName(), Arrays.asList(dropdownOptions.value())); + if (componentReplacements.containsKey(Combo.class)) { + try { + component = componentReplacements.get(Combo.class).getConstructor(String.class, List.class).newInstance(iSetting.getName(), Arrays.asList(dropdownOptions.value())); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + else if (iSetting.getType() == String.class) { //if field + component = new studio.dreamys.icarus.component.sub.Field(iSetting.getName()); + if (componentReplacements.containsKey(studio.dreamys.icarus.component.sub.Field.class)) { + try { + component = componentReplacements.get(studio.dreamys.icarus.component.sub.Field.class).getConstructor(String.class).newInstance(iSetting.getName()); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + else if (iSetting.getType() == double.class && sliderOptions != null) { //if slider + component = new Slider(iSetting.getName(), sliderOptions.min(), sliderOptions.max(), sliderOptions.onlyInt(), sliderOptions.units()); + if (componentReplacements.containsKey(Slider.class)) { + try { + component = componentReplacements.get(Slider.class).getConstructor(String.class, double.class, double.class, boolean.class, String.class).newInstance(iSetting.getName(), sliderOptions.min(), sliderOptions.max(), sliderOptions.onlyInt(), sliderOptions.units()); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + if (component == null) continue; //if component is null, skip it + component.configField = iSetting; + ((Group) group).addChild(component); //add component to group + + //only bind keybind to checkbox or button + if ((iSetting.getType() == Runnable.class || iSetting.getType() == boolean.class) && iKeybind != null) new Keybind(iKeybind.value()).attachTo(component); } } + } + } - PrintWriter pw = new PrintWriter(file); + public static void save() { + JsonObject json = new JsonObject(); + + for (ReflectionCache reflectionCache : reflectionCaches) { //for every reflection + JsonObject pageObject = new JsonObject(); + + for (Map.Entry, Field[]> groupSettings : reflectionCache.getIGroupMap().entrySet()) { + JsonObject groupObject = new JsonObject(); + + Class iGroup = groupSettings.getKey(); + Field[] iSettings = groupSettings.getValue(); + + for (Field iSetting : iSettings) { //for every setting + //get additional annotations + DropdownOptions dropdownOptions = iSetting.getAnnotation(DropdownOptions.class); + SliderOptions sliderOptions = iSetting.getAnnotation(SliderOptions.class); + + iSetting.setAccessible(true); //set accessible + + try { + if (iSetting.getType() == boolean.class) { //if checkbox + groupObject.addProperty(iSetting.getName(), (boolean) iSetting.get(null)); + } - for (String str : toSave) { - pw.println(str); + else if (iSetting.getType() == String.class && dropdownOptions != null) { //if choice + groupObject.addProperty(iSetting.getName(), (String) iSetting.get(null)); + } + + else if (iSetting.getType() == String[].class && dropdownOptions != null) { //if combo + groupObject.add(iSetting.getName(), gson.toJsonTree(iSetting.get(null))); + } + + else if (iSetting.getType() == String.class) { //if field + groupObject.addProperty(iSetting.getName(), (String) iSetting.get(null)); + } + + else if (iSetting.getType() == double.class && sliderOptions != null) { //if slider + groupObject.addProperty(iSetting.getName(), (double) iSetting.get(null)); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + + pageObject.add(iGroup.getSimpleName(), groupObject); } - pw.close(); + json.add(reflectionCache.getIPage().getSimpleName(), pageObject); + } + + try { //write to file + FileWriter writer = new FileWriter(configFile); + writer.write(gson.toJson(json)); + writer.close(); } catch (IOException e) { e.printStackTrace(); } } - public void load() { + private static void load() { + JsonObject json; + try { - ArrayList lines = new ArrayList<>(); - BufferedReader reader = new BufferedReader(new FileReader(file)); - String line = reader.readLine(); - while (line != null) { - lines.add(line); - line = reader.readLine(); - } - reader.close(); - - for (String s : lines) { - String[] args = s.split(":"); - for (Component comp : Icarus.getWindow().all) { - if (comp.getClass().getSimpleName().equals(args[0])) { - if (comp.getLabel().equals(args[1])) { - if (comp.getGroup().getLabel().equals(args[2])) { - //do nothing for button - if (comp instanceof Checkbox) { - ((Checkbox) comp).setToggled(Boolean.parseBoolean(args[3])); - } - if (comp instanceof Choice) { - ((Choice) comp).setSelected(args[3]); - } - //TODO: color - if (comp instanceof Combo) { - ((Combo) comp).setActiveOptions(args[3]); - } - if (comp instanceof Field) { - ((Field) comp).setText(args[3].equals("null") ? "" : args[3]); - } - //do nothing for Group - if (comp instanceof Keybind) { - ((Keybind) comp).setKey(Integer.parseInt(args[3])); - } - if (comp instanceof Slider) { - ((Slider) comp).setValue(Double.parseDouble(args[3])); - } - comp.fireChange(); - } + json = new JsonParser().parse(new FileReader(configFile)).getAsJsonObject(); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Failed to load settings"); + return; + } + + for (ReflectionCache reflectionCache : reflectionCaches) { //for every reflection + JsonObject pageObject = json.getAsJsonObject(reflectionCache.getIPage().getSimpleName()); + + for (Map.Entry, Field[]> groupSettings : reflectionCache.getIGroupMap().entrySet()) { + if (pageObject == null) continue; + JsonObject groupObject = pageObject.getAsJsonObject(groupSettings.getKey().getSimpleName()); + + if (groupObject == null) continue; + Field[] iSettings = groupSettings.getValue(); + + for (Field iSetting : iSettings) { //for every setting + if (groupObject.get(iSetting.getName()) == null) continue; + + //get additional annotations + DropdownOptions dropdownOptions = iSetting.getAnnotation(DropdownOptions.class); + SliderOptions sliderOptions = iSetting.getAnnotation(SliderOptions.class); + + iSetting.setAccessible(true); //set accessible + + try { + if (iSetting.getType() == boolean.class) { //if checkbox + iSetting.set(null, groupObject.get(iSetting.getName()).getAsBoolean()); + } + + else if (iSetting.getType() == String.class && dropdownOptions != null) { //if choice + iSetting.set(null, groupObject.get(iSetting.getName()).getAsString()); } + + else if (iSetting.getType() == String[].class && dropdownOptions != null) { //if combo + iSetting.set(null, gson.fromJson(groupObject.get(iSetting.getName()), String[].class)); + } + + else if (iSetting.getType() == String.class) { //if field + iSetting.set(null, groupObject.get(iSetting.getName()).getAsString()); + } + + else if (iSetting.getType() == double.class && sliderOptions != null) { //if slider + iSetting.set(null, groupObject.get(iSetting.getName()).getAsDouble()); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); } } } - } catch (Exception e) { - e.printStackTrace(); } } - public boolean getCheckbox(String group, String label) { - if ((comp = find(group, label, Checkbox.class)) != null) return ((Checkbox) comp).isToggled(); - return false; - } + public static void saveAttachments() { + JsonObject json = new JsonObject(); - public String getChoice(String group, String label) { - if ((comp = find(group, label, Choice.class)) != null) return ((Choice) comp).getSelected(); - return null; - } + for (Attachment attachment : Icarus.getWindow().attachments) { + if (attachment instanceof Keybind) { + if (json.has(attachment.getChild().getGroup().getPage().getLabel())) { //page already exists + //lets get it + JsonObject pageObject = json.getAsJsonObject(attachment.getChild().getGroup().getPage().getLabel()); - public HashMap getCombo(String group, String label) { - if ((comp = find(group, label, Combo.class)) != null) return ((Combo) comp).getOptions(); - return null; - } + if (pageObject.has(attachment.getChild().getGroup().getLabel())) { //group already exists + //lets get it + JsonObject groupObject = pageObject.getAsJsonObject(attachment.getChild().getGroup().getLabel()); - public String getField(String group, String label) { - if ((comp = find(group, label, Field.class)) != null) return ((Field) comp).getText(); - return null; - } + //add keybind to group + groupObject.addProperty(attachment.getChild().configField.getName(), ((Keybind) attachment).getKey()); + } else { //group doesn't exist + //lets create one + JsonObject groupObject = new JsonObject(); - public double getSlider(String group, String label) { - if ((comp = find(group, label, Slider.class)) != null) return ((Slider) comp).getValue(); - return 0; - } + //add keybind to group + groupObject.addProperty(attachment.getChild().configField.getName(), ((Keybind) attachment).getKey()); - public void setCheckbox(String group, String label, boolean toggled) { - if ((comp = find(group, label, Checkbox.class)) != null) ((Checkbox) comp).setToggled(toggled); - } + //add group to page + pageObject.add(attachment.getChild().getGroup().getLabel(), groupObject); + } + } else { //page doesn't exist + //lets create a page + JsonObject pageObject = new JsonObject(); - public void setChoice(String group, String label, String selected) { - if ((comp = find(group, label, Choice.class)) != null) ((Choice) comp).setSelected(selected); - } + //lets create a group + JsonObject groupObject = new JsonObject(); - public void setCombo(String group, String label, HashMap options) { - if ((comp = find(group, label, Combo.class)) != null) ((Combo) comp).setOptions(options); - } + //add keybind to group + groupObject.addProperty(attachment.getChild().configField.getName(), ((Keybind) attachment).getKey()); - public void setField(String group, String label, String text) { - if ((comp = find(group, label, Field.class)) != null) ((Field) comp).setText(text); - } + //add group to page + pageObject.add(attachment.getChild().getGroup().getLabel(), groupObject); - public void setSlider(String group, String label, double value) { - if ((comp = find(group, label, Slider.class)) != null) ((Slider) comp).setValue(value); + //add page to json + json.add(attachment.getChild().getGroup().getPage().getLabel(), pageObject); + } + } + } + + try { //write to file + FileWriter writer = new FileWriter(keybindFile); + writer.write(gson.toJson(json)); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } - public Component find(String group, String label, Class subType) { - for (Component comp : Icarus.getWindow().all) { - if (comp.getLabel().equals(label) && comp.getGroup().getLabel().equals(group) && subType.isInstance(comp)) { - return comp; + private static void loadAttachments() { + JsonObject json; + + try { + json = new JsonParser().parse(new FileReader(keybindFile)).getAsJsonObject(); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Failed to load settings"); + return; + } + + for (Attachment attachment : Icarus.getWindow().attachments) { + JsonObject pageObject = json.getAsJsonObject(attachment.getChild().getGroup().getPage().getLabel().replaceAll(" ", "_")); //get page object + if (pageObject == null) continue; + JsonObject groupObject = pageObject.getAsJsonObject(attachment.getChild().getGroup().getLabel().replaceAll(" ", "_")); //get group object + if (groupObject == null) continue; + + if (attachment instanceof Keybind) { + ((Keybind) attachment).setKey(groupObject.get(attachment.getChild().getLabel().replaceAll(" ", "_")).getAsInt()); } } - return null; } } diff --git a/src/main/java/studio/dreamys/icarus/event/ComponentEvent.java b/src/main/java/studio/dreamys/icarus/event/ComponentEvent.java index d97e56b..121f123 100644 --- a/src/main/java/studio/dreamys/icarus/event/ComponentEvent.java +++ b/src/main/java/studio/dreamys/icarus/event/ComponentEvent.java @@ -1,75 +1,29 @@ package studio.dreamys.icarus.event; import net.minecraftforge.fml.common.eventhandler.Event; -import studio.dreamys.icarus.component.Component; -import studio.dreamys.icarus.component.sub.*; - -import java.util.HashMap; public abstract class ComponentEvent extends Event { - public Component component; - - public ComponentEvent(Component component) { - this.component = component; - } - public static class CheckboxEvent extends Event { - public Checkbox checkbox; - public boolean toggled; - public CheckboxEvent(Checkbox checkbox) { - this.checkbox = checkbox; - toggled = checkbox.isToggled(); - } } public static class ChoiceEvent extends Event { - public Choice choice; - public String selected; - public ChoiceEvent(Choice choice) { - this.choice = choice; - selected = choice.getSelected(); - } } public static class ComboEvent extends Event { - public Combo combo; - public HashMap activeOptions; - public ComboEvent(Combo combo) { - this.combo = combo; - activeOptions = combo.getOptions(); - } } public static class FieldEvent extends Event { - public Field field; - public String text; - public FieldEvent(Field field) { - this.field = field; - text = field.getText(); - } } public static class KeybindEvent extends Event { - public Keybind keybind; - public int key; - public KeybindEvent(Keybind keybind) { - this.keybind = keybind; - key = keybind.getKey(); - } } public static class SliderEvent extends Event { - public Slider slider; - public double value; - public SliderEvent(Slider slider) { - this.slider = slider; - value = slider.getValue(); - } } } \ No newline at end of file diff --git a/src/main/java/studio/dreamys/icarus/extra/Watermark.java b/src/main/java/studio/dreamys/icarus/extra/Watermark.java index 78f5241..fa67936 100644 --- a/src/main/java/studio/dreamys/icarus/extra/Watermark.java +++ b/src/main/java/studio/dreamys/icarus/extra/Watermark.java @@ -7,8 +7,8 @@ import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import studio.dreamys.icarus.util.position.Position; import studio.dreamys.icarus.util.RenderUtils; +import studio.dreamys.icarus.util.position.Position; import java.awt.*; @@ -19,6 +19,7 @@ public class Watermark { private Position position; private double x, y, width, height = RenderUtils.getFontHeight() + 7.5; private Color textColor; + private boolean enabled; public Watermark(String text) { this(text, Position.TOP_RIGHT); @@ -32,11 +33,13 @@ public Watermark(String text, Position position, Color textColor) { this.text = text; this.position = position; this.textColor = textColor; + + MinecraftForge.EVENT_BUS.register(this); } @SubscribeEvent public void onGameOverlay(RenderGameOverlayEvent e) { - if (e.type != RenderGameOverlayEvent.ElementType.TEXT) return; + if (e.type != RenderGameOverlayEvent.ElementType.TEXT || !enabled) return; //update width in case text changes width = RenderUtils.getStringWidth(text) + 5; @@ -53,12 +56,4 @@ public void onGameOverlay(RenderGameOverlayEvent e) { //draw text RenderUtils.drawString(text, x + 1, y + 3.5, textColor); } - - public void enable() { - MinecraftForge.EVENT_BUS.register(this); - } - - public void disable() { - MinecraftForge.EVENT_BUS.unregister(this); - } } diff --git a/src/main/java/studio/dreamys/icarus/extra/notification/Notification.java b/src/main/java/studio/dreamys/icarus/extra/notification/Notification.java index 8a14c00..d3999a0 100644 --- a/src/main/java/studio/dreamys/icarus/extra/notification/Notification.java +++ b/src/main/java/studio/dreamys/icarus/extra/notification/Notification.java @@ -42,30 +42,32 @@ public Notification(String title, String message, int ticksToStay, Color backgro tick = -20; x = Minecraft.getMinecraft().displayWidth / 2.0; width = 150; - maskWidth = x + width; + maskWidth = width; //basic end of word separation if (RenderUtils.getStringWidth(message) + 20 > 150) { lines = new ArrayList<>(); String[] words = message.split(" "); - String line = ""; + StringBuilder line = new StringBuilder(); for (String word : words) { if (RenderUtils.getStringWidth(line + word) + 20 > 150) { - lines.add(line); - line = word + " "; + lines.add(line.toString()); + line = new StringBuilder(word + " "); } else { - line += word + " "; + line.append(word).append(" "); } } - lines.add(line); + lines.add(line.toString()); height = lines.size() * 10 + 20; } else { height = 30; } + + MinecraftForge.EVENT_BUS.register(this); } @SubscribeEvent - public void onGameOverlay(RenderGameOverlayEvent e) { + public void onGameOverlay(RenderGameOverlayEvent.Text e) { y = NotificationManager.startY; //notification dynamic stacking @@ -76,7 +78,7 @@ public void onGameOverlay(RenderGameOverlayEvent e) { } //draw background - RenderUtils.drawRect(x, y, x + width, y + height, backgroundColor); + RenderUtils.drawRect(x, y, width, height, backgroundColor); //draw title RenderUtils.drawTitle(title, x + 10, y + 5, titleColor); @@ -91,7 +93,7 @@ public void onGameOverlay(RenderGameOverlayEvent e) { } //draw mask - RenderUtils.drawRect(x, y, maskWidth, y + height, maskColor); + RenderUtils.drawRect(x, y, maskWidth, height, maskColor); } @SubscribeEvent @@ -112,7 +114,7 @@ public void onTick(TickEvent.ClientTickEvent e) { //parabola easing (thank you math classes) speed = (((tick / width) / 2) * Math.pow(tick, 2)); x += speed; - maskWidth = x + 10 - speed * 4; + maskWidth = 10 - speed * 4; tick++; } } diff --git a/src/main/java/studio/dreamys/icarus/extra/notification/NotificationManager.java b/src/main/java/studio/dreamys/icarus/extra/notification/NotificationManager.java index d8291a3..4dc8020 100644 --- a/src/main/java/studio/dreamys/icarus/extra/notification/NotificationManager.java +++ b/src/main/java/studio/dreamys/icarus/extra/notification/NotificationManager.java @@ -1,7 +1,5 @@ package studio.dreamys.icarus.extra.notification; -import net.minecraftforge.common.MinecraftForge; - import java.util.ArrayList; public class NotificationManager { @@ -11,6 +9,5 @@ public class NotificationManager { public static void send(Notification notification) { notifications.add(notification); - MinecraftForge.EVENT_BUS.register(notification); } } diff --git a/src/main/java/studio/dreamys/icarus/handler/KeybindHandler.java b/src/main/java/studio/dreamys/icarus/handler/KeybindHandler.java new file mode 100644 index 0000000..013e228 --- /dev/null +++ b/src/main/java/studio/dreamys/icarus/handler/KeybindHandler.java @@ -0,0 +1,40 @@ +package studio.dreamys.icarus.handler; + +import net.minecraft.client.Minecraft; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.InputEvent; +import org.lwjgl.input.Keyboard; +import studio.dreamys.icarus.Icarus; +import studio.dreamys.icarus.component.sub.Button; +import studio.dreamys.icarus.component.sub.Checkbox; +import studio.dreamys.icarus.component.sub.attachment.Attachment; +import studio.dreamys.icarus.component.sub.attachment.sub.Keybind; + +public class KeybindHandler { + @SubscribeEvent + public void onKeyInput(InputEvent.KeyInputEvent e) { + if (Minecraft.getMinecraft().theWorld == null || Minecraft.getMinecraft().thePlayer == null) return; + if (Keyboard.getEventKeyState()) { //if the key is down + int keyCode = Keyboard.getEventKey(); //get keycode + if (keyCode <= 0) return; //ignore invalid keycode + if (keyCode == Icarus.getWindow().key) { + Minecraft.getMinecraft().displayGuiScreen(Icarus.getWindow()); + return; + } + + for (Attachment attachment : Icarus.getWindow().attachments) { + if (attachment instanceof Keybind) { + if (((Keybind) attachment).getKey() == keyCode) { + if (attachment.getChild() instanceof Button) { + ((Button) attachment.getChild()).getRunnable().run(); + } + + if (attachment.getChild() instanceof Checkbox) { + ((Checkbox) attachment.getChild()).toggle(); + } + } + } + } + } + } +} diff --git a/src/main/java/studio/dreamys/icarus/reflection/ReflectionCache.java b/src/main/java/studio/dreamys/icarus/reflection/ReflectionCache.java new file mode 100644 index 0000000..7fc67c4 --- /dev/null +++ b/src/main/java/studio/dreamys/icarus/reflection/ReflectionCache.java @@ -0,0 +1,17 @@ +package studio.dreamys.icarus.reflection; + +import lombok.Getter; + +import java.lang.reflect.Field; +import java.util.Map; + +@Getter +public class ReflectionCache { + private Class iPage; + private Map, Field[]> iGroupMap; + + public ReflectionCache(Class iPage, Map, Field[]> iGroupMap) { + this.iPage = iPage; + this.iGroupMap = iGroupMap; + } +} diff --git a/src/main/java/studio/dreamys/icarus/util/RenderUtils.java b/src/main/java/studio/dreamys/icarus/util/RenderUtils.java index 01bc449..0df8199 100644 --- a/src/main/java/studio/dreamys/icarus/util/RenderUtils.java +++ b/src/main/java/studio/dreamys/icarus/util/RenderUtils.java @@ -12,11 +12,11 @@ import java.io.IOException; import java.util.Objects; -@SuppressWarnings("ALL") +@SuppressWarnings({"unused", "DuplicatedCode", "JavadocReference"}) public class RenderUtils { - public static GlyphPageFontRenderer iconRenderer; - public static GlyphPageFontRenderer fontRenderer; + public static GlyphPageFontRenderer textRenderer; public static GlyphPageFontRenderer titleRenderer; + public static GlyphPageFontRenderer iconRenderer; public static void loadFonts() { //register font in jvm @@ -27,15 +27,15 @@ public static void loadFonts() { } //initialize font renderer - iconRenderer = GlyphPageFontRenderer.create("undefeated", 50, false, false, false); - fontRenderer = GlyphPageFontRenderer.create("Verdana", 11, false, false, false); + textRenderer = GlyphPageFontRenderer.create("Verdana", 11, false, false, false); titleRenderer = GlyphPageFontRenderer.create("Verdana", 20, true, false, false); + iconRenderer = GlyphPageFontRenderer.create("undefeated", 50, false, false, false); } /** * Modified and taken from: {@link Gui#drawGradientRect(int, int, int, int, int, int)} * */ - public static void drawGradientRect(double left, double top, double right, double bottom, Color startColor, Color endColor) { + public static void drawGradientRect(double x, double y, double width, double height, Color startColor, Color endColor) { double zLevel = 0; float f = (float)(startColor.getRGB() >> 24 & 255) / 255.0F; float f1 = (float)(startColor.getRGB() >> 16 & 255) / 255.0F; @@ -53,10 +53,10 @@ public static void drawGradientRect(double left, double top, double right, doubl Tessellator tessellator = Tessellator.getInstance(); WorldRenderer worldrenderer = tessellator.getWorldRenderer(); worldrenderer.begin(7, DefaultVertexFormats.POSITION_COLOR); - worldrenderer.pos(right, top, zLevel).color(f1, f2, f3, f).endVertex(); - worldrenderer.pos(left, top, zLevel).color(f1, f2, f3, f).endVertex(); - worldrenderer.pos(left, bottom, zLevel).color(f5, f6, f7, f4).endVertex(); - worldrenderer.pos(right, bottom, zLevel).color(f5, f6, f7, f4).endVertex(); + worldrenderer.pos(x + width, y, zLevel).color(f1, f2, f3, f).endVertex(); + worldrenderer.pos(x, y, zLevel).color(f1, f2, f3, f).endVertex(); + worldrenderer.pos(x, y + height, zLevel).color(f5, f6, f7, f4).endVertex(); + worldrenderer.pos(x + width, y + height, zLevel).color(f5, f6, f7, f4).endVertex(); tessellator.draw(); GlStateManager.shadeModel(7424); GlStateManager.disableBlend(); @@ -81,62 +81,70 @@ public static void drawIcon(char character, double x, double y, Color color, boo } public static void drawString(String text, double x, double y, Color color) { - fontRenderer.drawString(text, (float) x, (float) y, color.getRGB(), false); + textRenderer.drawString(text, (float) x, (float) y, color.getRGB(), false); } public static void drawString(String text, double x, double y, Color color, boolean shadow) { - fontRenderer.drawString(text, (float) x, (float) y, color.getRGB(), shadow); + textRenderer.drawString(text, (float) x, (float) y, color.getRGB(), shadow); } public static void drawString(String text, double x, double y, float scale, Color color) { GL11.glPushMatrix(); GL11.glScalef(scale, scale, 1); - fontRenderer.drawString(text, (float) (x / scale), (float) (y / scale), color.getRGB(), false); + textRenderer.drawString(text, (float) (x / scale), (float) (y / scale), color.getRGB(), false); GL11.glPopMatrix(); } public static void drawString(String text, double x, double y, float scale, Color color, boolean shadow) { GL11.glPushMatrix(); GL11.glScalef(scale, scale, 1); - fontRenderer.drawString(text, (float) (x / scale), (float) (y / scale), color.getRGB(), shadow); + textRenderer.drawString(text, (float) (x / scale), (float) (y / scale), color.getRGB(), shadow); GL11.glPopMatrix(); } - public static void drawCenteredString(String text, double x, double y, Color color) { - fontRenderer.drawString(text, (float) (x - getStringWidth(text) / 2), (float) (y), color.getRGB(), false); + public static void drawXCenterString(String text, double x, double y, Color color) { + textRenderer.drawString(text, (float) (x - getStringWidth(text) / 2), (float) (y), color.getRGB(), false); + } + + public static void drawXYCenterString(String text, double x, double y, Color color) { + textRenderer.drawString(text, (float) (x - getStringWidth(text) / 2), (float) (y - RenderUtils.getFontHeight() / 2), color.getRGB(), false); + } + + public static void drawXCenterString(String text, double x, double y, Color color, boolean shadow) { + textRenderer.drawString(text, (float) (x - getStringWidth(text) / 2), (float) (y), color.getRGB(), shadow); } - public static void drawCenteredString(String text, double x, double y, Color color, boolean shadow) { - fontRenderer.drawString(text, (float) (x - getStringWidth(text) / 2), (float) (y), color.getRGB(), shadow); + public static void drawYCenterString(String text, double x, double y, Color color) { + textRenderer.drawString(text, (float) (x), (float) (y - RenderUtils.getFontHeight() / 2), color.getRGB(), false); } - public static void drawCenteredString(String text, double x, double y, float scale, Color color) { + public static void drawXCenterString(String text, double x, double y, float scale, Color color) { GL11.glPushMatrix(); GL11.glScalef(scale, scale, 1); - fontRenderer.drawString(text, (float) ((x / scale) - getStringWidth(text, scale) / 2), (float) (y / scale), color.getRGB(), false); + textRenderer.drawString(text, (float) ((x / scale) - getStringWidth(text, scale) / 2), (float) (y / scale), color.getRGB(), false); GL11.glPopMatrix(); } - public static void drawCenteredString(String text, double x, double y, float scale, Color color, boolean shadow) { + public static void drawXCenterString(String text, double x, double y, float scale, Color color, boolean shadow) { GL11.glPushMatrix(); GL11.glScalef(scale, scale, 1); - fontRenderer.drawString(text, (float) ((x / scale) - getStringWidth(text, scale) / 2), (float) (y / scale), color.getRGB(), shadow); + textRenderer.drawString(text, (float) ((x / scale) - getStringWidth(text, scale) / 2), (float) (y / scale), color.getRGB(), shadow); GL11.glPopMatrix(); } public static float getStringWidth(String text) { - return fontRenderer.getStringWidth(text); + return textRenderer.getStringWidth(text); } public static float getStringWidth(String text, float scale) { - return fontRenderer.getStringWidth(text) * scale; + return textRenderer.getStringWidth(text) * scale; } public static float getFontHeight() { - return fontRenderer.getFontHeight(); + return textRenderer.getFontHeight(); } - public static void drawGroupWithString(double width, double height, double x, double y, String label) { + public static void drawGroupWithString(double x, double y, double width, double height, String label) { GL11.glPushMatrix(); GL11.glScalef(0.5f, 0.5f, 1f); @@ -146,28 +154,27 @@ public static void drawGroupWithString(double width, double height, double x, do height /= 0.5f; double offset = 2 / 0.5f; - drawRect(x, y, x + 1, y + height, Color.DARK_GRAY); //LEFT - drawRect(x, y + height, x + width, y + height + 1, Color.DARK_GRAY); //BOTTOM - drawRect(x + width, y, x + width + 1, y + height + 1, Color.DARK_GRAY); //RIGHT + drawRect(x, y, 1, height, Color.DARK_GRAY); //LEFT + drawRect(x, y + height, width, 1, Color.DARK_GRAY); //BOTTOM + drawRect(x + width, y, 1, height + 1, Color.DARK_GRAY); //RIGHT - drawRect(x, y, x + width / 10, y + 1, Color.DARK_GRAY); //TOP LEFT - drawRect(x + width / 10 + getStringWidth(label, 2) + offset, y, x + width, y + 1, Color.DARK_GRAY); //TOP RIGHT + drawRect(x, y, 30, 1, Color.DARK_GRAY); //TOP LEFT + drawRect(x + 30 + getStringWidth(label) * 2 + 4, y, width - 30 - getStringWidth(label) * 2 - 4, 1, Color.DARK_GRAY); //TOP RIGHT GL11.glPopMatrix(); x *= 0.5f; y *= 0.5f; - width *= 0.5f; //label - RenderUtils.drawString(label, x + width / 10, y - 3.5, Color.WHITE); + RenderUtils.drawString(label, x + 15, y - 3.5, Color.WHITE); } - public static void drawOutline(double width, double height, double x, double y, Color color) { - drawRect(x, y, x + 1, y + height, color); //LEFT - drawRect(x, y + height, x + width, y + height + 1, color); //BOTTOM - drawRect(x + width, y, x + width + 1, y + height + 1, color); //RIGHT - drawRect(x, y, x + width, y + 1, color); //TOP + public static void drawOutline(double x, double y, double width, double height, Color color) { + drawRect(x, y, 1, height, color); //LEFT + drawRect(x, y + height, width, 1, color); //BOTTOM + drawRect(x + width, y, 1, height + 1, color); //RIGHT + drawRect(x, y, width, 1, color); //TOP } public static void drawOutline(double width, double height, double x, double y, float scale, Color color) { @@ -179,16 +186,16 @@ public static void drawOutline(double width, double height, double x, double y, width /= scale; height /= scale; - drawRect(x, y, x + 1, y + height, color); //LEFT - drawRect(x, y + height, x + width, y + height + 1, color); //BOTTOM - drawRect(x + width, y, x + width + 1, y + height + 1, color); //RIGHT - drawRect(x, y, x + width, y + 1, color); //TOP + drawRect(x, y, 1, height, color); //LEFT + drawRect(x, y + height, width, 1, color); //BOTTOM + drawRect(x + width, y, 1, height + 1, color); //RIGHT + drawRect(x, y, width, 1, color); //TOP GL11.glPopMatrix(); } public static void drawRect(double x, double y, double width, double height, Color color) { - Gui.drawRect((int) x, (int) y, (int) width, (int) height, color.getRGB()); + Gui.drawRect((int) x, (int) y, (int) (x + width), (int) (y + height), color.getRGB()); } public static void drawTexture(double x, double y, double width, double height) { diff --git a/src/main/java/studio/dreamys/icarus/util/font/GlyphPage.java b/src/main/java/studio/dreamys/icarus/util/font/GlyphPage.java index f139d3c..71e5755 100644 --- a/src/main/java/studio/dreamys/icarus/util/font/GlyphPage.java +++ b/src/main/java/studio/dreamys/icarus/util/font/GlyphPage.java @@ -84,10 +84,6 @@ public void generateGlyphPage(char[] chars) { glyph.width = bounds.getBounds().width + 8; // Leave some additional space glyph.height = bounds.getBounds().height; - if (posY + glyph.height >= imgSize) { -// throw new IllegalStateException("Not all characters will fit"); - } - if (posX + glyph.width >= imgSize) { posX = 0; posY += currentCharHeight; @@ -170,30 +166,12 @@ public int getMaxFontHeight() { return maxFontHeight; } - public boolean isAntiAliasingEnabled() { - return antiAliasing; - } - - public boolean isFractionalMetricsEnabled() { - return fractionalMetrics; - } - static class Glyph { private int x; private int y; private int width; private int height; - Glyph(int x, int y, int width, int height) { - this.x = x; - this.y = y; - this.width = width; - this.height = height; - } - - Glyph() { - } - public int getX() { return x; } diff --git a/src/main/java/studio/dreamys/icarus/util/font/GlyphPageFontRenderer.java b/src/main/java/studio/dreamys/icarus/util/font/GlyphPageFontRenderer.java index 3f6d2fc..4d9166e 100644 --- a/src/main/java/studio/dreamys/icarus/util/font/GlyphPageFontRenderer.java +++ b/src/main/java/studio/dreamys/icarus/util/font/GlyphPageFontRenderer.java @@ -7,12 +7,11 @@ import java.awt.*; import java.util.Locale; -import java.util.Random; import static org.lwjgl.opengl.GL11.*; +@SuppressWarnings("DuplicatedCode") public class GlyphPageFontRenderer { - public Random fontRandom = new Random(); /** * Current X coordinate at which to draw the next character. */ @@ -39,18 +38,10 @@ public class GlyphPageFontRenderer { */ private float green; /** - * Used to speify new alpha value for the current color. + * Used to specify new alpha value for the current color. */ private float alpha; - /** - * Text color of the currently rendering string. - */ - private int textColor; - /** - * Set if the "k" style (random) is active in currently rendering string - */ - private boolean randomStyle; /** * Set if the "l" style (bold) is active in currently rendering string */ @@ -216,7 +207,6 @@ private void renderStringAtPos(String text, boolean shadow) { int i1 = "0123456789abcdefklmnor".indexOf(text.toLowerCase(Locale.ENGLISH).charAt(i + 1)); if (i1 < 16) { - randomStyle = false; boldStyle = false; strikethroughStyle = false; underlineStyle = false; @@ -231,11 +221,8 @@ private void renderStringAtPos(String text, boolean shadow) { } int j1 = colorCode[i1]; - textColor = j1; GlStateManager.color((float) (j1 >> 16) / 255.0F, (float) (j1 >> 8 & 255) / 255.0F, (float) (j1 & 255) / 255.0F, alpha); - } else if (i1 == 16) { - randomStyle = true; } else if (i1 == 17) { boldStyle = true; } else if (i1 == 18) { @@ -245,7 +232,6 @@ private void renderStringAtPos(String text, boolean shadow) { } else if (i1 == 20) { italicStyle = true; } else { - randomStyle = false; boldStyle = false; strikethroughStyle = false; underlineStyle = false; @@ -318,7 +304,6 @@ else if (italicStyle) * Reset all style flag fields in the class to false; called at the start of string rendering */ private void resetStyles() { - randomStyle = false; boldStyle = false; italicStyle = false; underlineStyle = false; @@ -375,68 +360,4 @@ else if (on && character >= '0' && character <= 'r') { return width / 2; } - /** - * Trims a string to fit a specified Width. - */ - public String trimStringToWidth(String text, int width) { - return trimStringToWidth(text, width, false); - } - - /** - * Trims a string to a specified width, and will reverse it if par3 is set. - */ - public String trimStringToWidth(String text, int maxWidth, boolean reverse) { - StringBuilder stringbuilder = new StringBuilder(); - - boolean on = false; - - int j = reverse ? text.length() - 1 : 0; - int k = reverse ? -1 : 1; - int width = 0; - - GlyphPage currentPage; - - for (int i = j; i >= 0 && i < text.length() && i < maxWidth; i += k) { - char character = text.charAt(i); - - if (character == 'ยง') - on = true; - else if (on && character >= '0' && character <= 'r') { - int colorIndex = "0123456789abcdefklmnor".indexOf(character); - if (colorIndex < 16) { - boldStyle = false; - italicStyle = false; - } else if (colorIndex == 17) { - boldStyle = true; - } else if (colorIndex == 20) { - italicStyle = true; - } else if (colorIndex == 21) { - boldStyle = false; - italicStyle = false; - } - i++; - on = false; - } else { - if (on) i--; - - character = text.charAt(i); - - currentPage = getCurrentGlyphPage(); - - width += (currentPage.getWidth(character) - 8) / 2; - } - - if (i > width) { - break; - } - - if (reverse) { - stringbuilder.insert(0, character); - } else { - stringbuilder.append(character); - } - } - - return stringbuilder.toString(); - } } diff --git a/src/main/java/studio/dreamys/test/TestMod.java b/src/main/java/studio/dreamys/test/TestMod.java index e6cd9e4..11834f0 100644 --- a/src/main/java/studio/dreamys/test/TestMod.java +++ b/src/main/java/studio/dreamys/test/TestMod.java @@ -8,23 +8,38 @@ import net.minecraftforge.fml.common.gameevent.InputEvent; import org.lwjgl.input.Keyboard; import studio.dreamys.icarus.Icarus; +import studio.dreamys.icarus.component.Page; +import studio.dreamys.icarus.component.Window; +import studio.dreamys.icarus.config.Config; import studio.dreamys.icarus.event.ComponentEvent; import studio.dreamys.icarus.extra.Watermark; import studio.dreamys.icarus.extra.notification.Notification; import studio.dreamys.icarus.extra.notification.NotificationManager; +import studio.dreamys.test.ui.component.CustomPage; +import studio.dreamys.test.ui.page.Misc; +import studio.dreamys.test.ui.page.Visuals; import java.awt.*; @Mod(modid = "TestMod") public class TestMod { + private static Watermark watermark; + @Mod.EventHandler public void preInit(FMLPreInitializationEvent e) { MinecraftForge.EVENT_BUS.register(this); - Icarus.init(e.getModMetadata().modId, new TestWindow()); - System.out.println(Icarus.getConfig().getCheckbox("Visuals", "haha")); - System.out.println(Icarus.getConfig().getCheckbox("Visuals", "Another Checkbox")); - System.out.println(Icarus.getConfig().getCheckbox("Visuals", "Checkbox")); - new Watermark("dxxxxyware | uid 001 (dxxxxy) | mc.hypixel.net | 23ms").enable(); + Window window = new Window(Minecraft.getMinecraft().displayWidth / 2.0, Minecraft.getMinecraft().displayHeight / 4.0, 731 / 2.0, 617 / 2.0, new Color(29, 122, 215)); + +// Icarus.provideComponent(CustomCheckbox.class, Checkbox.class); + Icarus.provideComponent(CustomPage.class, Page.class); + Icarus.init(e.getModMetadata().modId, window); +// Icarus.provideTitleFont(getClass().getResourceAsStream("/assets/icarus/undefeated.ttf"), "undefeated", 50, false, false, false); +// System.out.println(Icarus.getConfig().getCheckbox("Visuals", "haha")); +// System.out.println(Icarus.getConfig().getCheckbox("Visuals", "Another Checkbox")); +// System.out.println(Icarus.getConfig().getCheckbox("Visuals", "Checkbox")); + +// new Watermark("dxxxxyware | uid 001 (dxxxxy) | mc.hypixel.net | 23ms").enable(); + watermark = new Watermark("dxxxxyware | uid 001 (dxxxxy) | mc.hypixel.net | 23ms"); NotificationManager.startY = 20; //start below watermark //try all except top_right @@ -40,46 +55,36 @@ public void preInit(FMLPreInitializationEvent e) { @SubscribeEvent public void onComponentStateChange(ComponentEvent e) { - System.out.println(e.component); - Icarus.getConfig().setSlider("AHSniper", "Sleep (ms)", 3000); } @SubscribeEvent public void onComponentStateChange(ComponentEvent.CheckboxEvent e) { - System.out.println(e.checkbox); - System.out.println(e.toggled); - Icarus.getConfig().setSlider("AHSniper", "Sleep (ms)", 3000); - + watermark.setEnabled(Misc.Watermark.Enabled); } @SubscribeEvent public void onComponentStateChange(ComponentEvent.ChoiceEvent e) { - System.out.println(e.choice); - System.out.println(e.selected); + } @SubscribeEvent public void onComponentStateChange(ComponentEvent.ComboEvent e) { - System.out.println(e.combo); - System.out.println(e.activeOptions); + } @SubscribeEvent public void onComponentStateChange(ComponentEvent.FieldEvent e) { - System.out.println(e.field); - System.out.println(e.text); + watermark.setText(Misc.Watermark.Text); } @SubscribeEvent public void onComponentStateChange(ComponentEvent.KeybindEvent e) { - System.out.println(e.keybind); - System.out.println(e.key); + } @SubscribeEvent public void onComponentStateChange(ComponentEvent.SliderEvent e) { - System.out.println(e.slider); - System.out.println(e.value); + } @SubscribeEvent @@ -90,6 +95,9 @@ public void key(InputEvent.KeyInputEvent e) { if (keyCode <= 0) return; //ignore invalid keycode if (keyCode == Keyboard.KEY_K) { NotificationManager.send(new Notification("Test", "Test")); + Visuals.ESP.Checkbox = true; + Visuals.ESP.Slider = 50; + Config.save(); } if (keyCode == Keyboard.KEY_J) { NotificationManager.send(new Notification("Test", "This is a very cool informational notification which is much larger in size.", 40)); diff --git a/src/main/java/studio/dreamys/test/TestWindow.java b/src/main/java/studio/dreamys/test/TestWindow.java deleted file mode 100644 index 2a3c48e..0000000 --- a/src/main/java/studio/dreamys/test/TestWindow.java +++ /dev/null @@ -1,74 +0,0 @@ -package studio.dreamys.test; - -import net.minecraft.client.Minecraft; -import studio.dreamys.icarus.component.Page; -import studio.dreamys.icarus.component.Window; -import studio.dreamys.icarus.component.sub.*; - -import java.awt.Color; -import java.util.ArrayList; -import java.util.Arrays; - -public class TestWindow extends Window { - public TestWindow() { - //x, y, width, height, accent color - super(Minecraft.getMinecraft().displayWidth / 2.0, Minecraft.getMinecraft().displayHeight / 4.0, 731 / 2.0, 617 / 2.0, new Color(29, 122, 215)); - - /* BUILDING EXAMPLES */ - - //1. Empty pages - addPage(new Page('t')); - addPage(new Page('u')); - - //2. Variable and chain - Page page = addPage(new Page('v')); - page.addGroup(new Group("Visuals", 47.5, 10)) - .addChild(new Checkbox("haha")) - .addChild(new Choice("hihi", new ArrayList<>(Arrays.asList("Option 1", "Option 2", "Option 3")))) - .addChild(new Checkbox("hoho")); - - //3. Chaining Mania - addPage(new Page('s')) - .addGroup(new Group("Visuals", 47.5, 10)) - .addChild(new Checkbox("Checkbox"), new Keybind()) - .addChild(new Choice("Choice", new ArrayList<>(Arrays.asList("Option 1", "Option 2", "Option 3")))) - .addChild(new Checkbox("Another Checkbox")) - .addChild(new Combo("Combo", new ArrayList<>(Arrays.asList("Option 1", "Option 2", "Option 3")))) - .addChild(new Button("Button", () -> { - System.out.println("Runnable"); - }), new Keybind()) - .addChild(new Button("Another Button", () -> { - System.out.println("Another Runnable"); - })) - .addChild(new Choice("Choice", new ArrayList<>(Arrays.asList("Option 1", "Option 2", "Option 3")))) - .addChild(new Choice("Another Choice", new ArrayList<>(Arrays.asList("Option 1", "Option 2", "Option 3")))) - .addChild(new Combo("Combo", new ArrayList<>(Arrays.asList("Option 1", "Option 2", "Option 3")))) - .addChild(new Combo("Another Combo", new ArrayList<>(Arrays.asList("Option 1", "Option 2", "Option 3")))) - .addChild(new Field("Field")) - .addChild(new Slider("Decimal slider", 1, 10, false)) - .addChild(new Slider("Int slider", 10, 1, 10, true, "ms")) - ; - - addPage(new Page('s')) - .addGroup(new Group("AHSniper", 47.5, 10)) - .addChild(new Checkbox("Enabled"), new Keybind()) - .addChild(new Slider("Sleep (ms)", 2000, 1, 5000, true)) - .addChild(new Slider("Minimum Profit (x)", 5, 2, 30, true)) - .addChild(new Choice("Lowest Bin Prices", new ArrayList<>(Arrays.asList("Moulberry", "Binmaster", "SkyHelper")))) - .addChild(new Button("Force Update Prices", () -> { - System.out.println("Force Update Prices"); - })) - .addChild(new Checkbox("Verbose Chat")) - .addChild(new Checkbox("Auto Buy")) - ; - - /* ADDING COMPONENTS */ - //add all components - for (Page p : pages) { - for (Group g : p.getGroups()) { - all.add(g); - all.addAll(g.getChildren()); - } - } - } -} diff --git a/src/main/java/studio/dreamys/test/ui/component/CustomCheckbox.java b/src/main/java/studio/dreamys/test/ui/component/CustomCheckbox.java new file mode 100644 index 0000000..60082ae --- /dev/null +++ b/src/main/java/studio/dreamys/test/ui/component/CustomCheckbox.java @@ -0,0 +1,12 @@ +package studio.dreamys.test.ui.component; + +import studio.dreamys.icarus.component.sub.Checkbox; + +public class CustomCheckbox extends Checkbox { + public CustomCheckbox(String label) { + super(label); + + width = 80; + height = 80; + } +} diff --git a/src/main/java/studio/dreamys/test/ui/component/CustomPage.java b/src/main/java/studio/dreamys/test/ui/component/CustomPage.java new file mode 100644 index 0000000..9ec2c07 --- /dev/null +++ b/src/main/java/studio/dreamys/test/ui/component/CustomPage.java @@ -0,0 +1,23 @@ +package studio.dreamys.test.ui.component; + +import studio.dreamys.icarus.Icarus; +import studio.dreamys.icarus.component.Page; +import studio.dreamys.icarus.util.RenderUtils; + +import java.awt.*; + +public class CustomPage extends Page { + public CustomPage(String label, char icon) { + super(label, icon); + } + + @Override + public void render(int mouseX, int mouseY) { + x = window.x + relativeX; + y = window.y + relativeY; + + Color color = Icarus.getWindow().activePageIndex == window.pages.indexOf(this) ? window.color : Color.DARK_GRAY; + + RenderUtils.drawIcon(icon, x + 20, y + 25, color); + } +} diff --git a/src/main/java/studio/dreamys/test/ui/page/Combat.java b/src/main/java/studio/dreamys/test/ui/page/Combat.java new file mode 100644 index 0000000..616628a --- /dev/null +++ b/src/main/java/studio/dreamys/test/ui/page/Combat.java @@ -0,0 +1,36 @@ +package studio.dreamys.test.ui.page; + +import org.lwjgl.input.Keyboard; +import studio.dreamys.icarus.annotation.field.DropdownOptions; +import studio.dreamys.icarus.annotation.field.IKeybind; +import studio.dreamys.icarus.annotation.field.SliderOptions; +import studio.dreamys.icarus.annotation.type.IGroup; +import studio.dreamys.icarus.annotation.type.IPage; +import studio.dreamys.icarus.extra.notification.Notification; +import studio.dreamys.icarus.extra.notification.NotificationManager; + +@IPage('v') +public class Combat { + @IGroup(x = 47.5, y = 10) + public static class KillAura { + @IKeybind(Keyboard.KEY_6) + public static Runnable Button = () -> { + NotificationManager.send(new Notification("Button", "You pressed the button!")); + System.out.println("Button pressed!"); + }; + + @IKeybind(Keyboard.KEY_7) + public static boolean Checkbox_Haha = true; + + @DropdownOptions({"Option 1", "Option 2", "Option 3"}) + public static String Choice = "Option 1"; + + @DropdownOptions({"Option 1", "Option 2", "Option 3"}) + public static String[] Combo = {"Option 1", "Option 3"}; + + public static String Field = "Hello World!"; + + @SliderOptions(min = 0, max = 100, onlyInt = true, units = "%") + public static double Slider = 1; + } +} diff --git a/src/main/java/studio/dreamys/test/ui/page/Misc.java b/src/main/java/studio/dreamys/test/ui/page/Misc.java new file mode 100644 index 0000000..b7151d0 --- /dev/null +++ b/src/main/java/studio/dreamys/test/ui/page/Misc.java @@ -0,0 +1,17 @@ +package studio.dreamys.test.ui.page; + +import org.lwjgl.input.Keyboard; +import studio.dreamys.icarus.annotation.field.IKeybind; +import studio.dreamys.icarus.annotation.type.IGroup; +import studio.dreamys.icarus.annotation.type.IPage; + +@IPage('u') +public class Misc { + @IGroup(x = 47.5, y = 10) + public static class Watermark { + @IKeybind(Keyboard.KEY_G) + public static boolean Enabled = true; + + public static String Text = "dxxxxy#0776"; + } +} diff --git a/src/main/java/studio/dreamys/test/ui/page/Visuals.java b/src/main/java/studio/dreamys/test/ui/page/Visuals.java new file mode 100644 index 0000000..bf54128 --- /dev/null +++ b/src/main/java/studio/dreamys/test/ui/page/Visuals.java @@ -0,0 +1,56 @@ +package studio.dreamys.test.ui.page; + +import org.lwjgl.input.Keyboard; +import studio.dreamys.icarus.annotation.field.DropdownOptions; +import studio.dreamys.icarus.annotation.field.IKeybind; +import studio.dreamys.icarus.annotation.field.SliderOptions; +import studio.dreamys.icarus.annotation.type.IGroup; +import studio.dreamys.icarus.annotation.type.IPage; +import studio.dreamys.icarus.extra.notification.Notification; +import studio.dreamys.icarus.extra.notification.NotificationManager; + +@IPage('v') +public class Visuals { + @IGroup(x = 47.5, y = 10) + public static class ESP { + public static Runnable Button = () -> { + NotificationManager.send(new Notification("Button", "You pressed the button!")); + System.out.println("Button pressed!"); + }; + + public static boolean Checkbox = true; + + @DropdownOptions({"Option 1", "Option 2", "Option 3"}) + public static String Choice = "Option 1"; + + @DropdownOptions({"Option 1", "Option 2", "Option 3"}) + public static String[] Combo = {"Option 1", "Option 3"}; + + public static String Field = "Hello World!"; + + @SliderOptions(min = 0, max = 100, onlyInt = true, units = "%") + public static double Slider = 1; + } + + @IGroup(x = 200, y = 10) + public static class Chams { + @IKeybind(Keyboard.KEY_ESCAPE) + public static Runnable Button = () -> { + NotificationManager.send(new Notification("Button", "You pressed the button!")); + System.out.println("Button pressed!"); + }; + + public static boolean Checkbox = true; + + @DropdownOptions({"Option 1", "Option 2", "Option 3"}) + public static String Choice = "Option 1"; + + @DropdownOptions({"Option 1", "Option 2", "Option 3"}) + public static String[] Combo = {"Option 1", "Option 3"}; + + public static String Field = "Hello World!"; + + @SliderOptions(min = 0, max = 100, onlyInt = true, units = "%") + public static double Slider = 1; + } +}