From 5bf074444e24ca6300c52694f6c0c70ca6202d91 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Fri, 21 Aug 2026 20:37:41 +0800 Subject: [PATCH] WIP: ClickGUI --- .../orangex/module/option/Option.java | 6 +- .../module/option/impl/ModeOption.java | 25 +++ .../orangex/ui/clickgui/ClickGUIScreen.java | 177 +++++++++++++++++- .../orangex/ui/clickgui/Panel.java | 143 ++++++++++++++ .../ui/clickgui/component/Component.java | 108 +++++++++++ .../clickgui/component/impl/ModuleButton.java | 154 +++++++++++++++ .../component/option/BooleanComponent.java | 55 ++++++ .../component/option/ColorComponent.java | 88 +++++++++ .../component/option/ModeComponent.java | 60 ++++++ .../component/option/OptionComponent.java | 42 +++++ .../option/OptionComponentFactory.java | 72 +++++++ .../component/option/SliderComponent.java | 158 ++++++++++++++++ .../component/option/StringComponent.java | 122 ++++++++++++ 13 files changed, 1205 insertions(+), 5 deletions(-) create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/Panel.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/Component.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/impl/ModuleButton.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/BooleanComponent.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/ColorComponent.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/ModeComponent.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/OptionComponent.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/OptionComponentFactory.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/SliderComponent.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/StringComponent.java diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/module/option/Option.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/module/option/Option.java index b05723c..b7f1bab 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/module/option/Option.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/module/option/Option.java @@ -3,12 +3,14 @@ package cn.xiaozhou233.orangex.module.option; import cn.xiaozhou233.orangex.module.Module; import com.google.gson.JsonElement; import lombok.Getter; +import lombok.Setter; @Getter public abstract class Option { protected final String name; protected final String description; protected final Module parent; + @Setter protected T value; protected final T defaultValue; @@ -20,10 +22,6 @@ public abstract class Option { this.value = defaultValue; } - public void setValue(T value) { - this.value = value; - } - public void reset() { this.value = defaultValue; } diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/module/option/impl/ModeOption.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/module/option/impl/ModeOption.java index 5164e6d..cdd4df1 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/module/option/impl/ModeOption.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/module/option/impl/ModeOption.java @@ -55,4 +55,29 @@ public class ModeOption extends Option { public void deserialize(JsonElement element) { setValue(element.getAsString()); } + + public void next() { + + int index = getIndex(); + + index++; + + if(index >= modes.length) + index = 0; + + setValue(modes[index]); + } + + + public void previous() { + + int index = getIndex(); + + index--; + + if(index < 0) + index = modes.length - 1; + + setValue(modes[index]); + } } \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/ClickGUIScreen.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/ClickGUIScreen.java index bd59853..d00781a 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/ClickGUIScreen.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/ClickGUIScreen.java @@ -1,6 +1,181 @@ package cn.xiaozhou233.orangex.ui.clickgui; +import cn.xiaozhou233.orangex.module.Module; +import cn.xiaozhou233.orangex.module.ModuleCategory; +import cn.xiaozhou233.orangex.OrangeX; +import cn.xiaozhou233.orangex.ui.clickgui.component.impl.ModuleButton; import net.minecraft.client.gui.GuiScreen; +import java.util.ArrayList; +import java.util.List; + public class ClickGUIScreen extends GuiScreen { -} + + private final List panels = new ArrayList<>(); + + + @Override + public void initGui() { + + panels.clear(); + + + int x = 20; + + + for(ModuleCategory category : ModuleCategory.values()) { + + Panel panel = new Panel( + category, + x, + 30, + 120, + 20 + ); + + + int y = 20; + + + for(Module module : + OrangeX.getInstance() + .getModuleManager() + .getModules()) { + + + if(module.getCategory() != category) + continue; + + + panel.addComponent( + new ModuleButton( + panel, + module, + 0, + y + ) + ); + + + y += 20; + } + + + panels.add(panel); + + x += 140; + } + } + + + @Override + public void drawScreen( + int mouseX, + int mouseY, + float partialTicks + ) { + + + for(Panel panel : panels) { + + panel.drawScreen( + mouseX, + mouseY, + partialTicks + ); + } + + + super.drawScreen( + mouseX, + mouseY, + partialTicks + ); + } + + + @Override + protected void mouseClicked( + int mouseX, + int mouseY, + int mouseButton + ) throws java.io.IOException { + + + for(Panel panel : panels) { + + panel.mouseClicked( + mouseX, + mouseY, + mouseButton + ); + } + + + super.mouseClicked( + mouseX, + mouseY, + mouseButton + ); + } + + + @Override + protected void mouseReleased( + int mouseX, + int mouseY, + int mouseButton + ) { + + + for(Panel panel : panels) { + + panel.mouseReleased( + mouseX, + mouseY, + mouseButton + ); + } + + + super.mouseReleased( + mouseX, + mouseY, + mouseButton + ); + } + + + @Override + protected void keyTyped( + char typedChar, + int keyCode + ) throws java.io.IOException { + + + for(Panel panel : panels) { + + panel.keyTyped( + typedChar, + keyCode + ); + } + + + if(keyCode == 1) { + mc.displayGuiScreen(null); + } + + + super.keyTyped( + typedChar, + keyCode + ); + } + + + @Override + public boolean doesGuiPauseGame() { + return false; + } +} \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/Panel.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/Panel.java new file mode 100644 index 0000000..ec3adc0 --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/Panel.java @@ -0,0 +1,143 @@ +package cn.xiaozhou233.orangex.ui.clickgui; + +import cn.xiaozhou233.orangex.module.ModuleCategory; +import cn.xiaozhou233.orangex.ui.clickgui.component.Component; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.List; + +@Getter +public class Panel { + + private final ModuleCategory category; + + private int x; + private int y; + + private final int width; + private final int height; + + private final int headerHeight = 20; + + private boolean open = true; + + private boolean dragging; + + private int dragX; + private int dragY; + + private final List components = new ArrayList<>(); + + + public Panel(ModuleCategory category,int x,int y,int width,int height) { + this.category = category; + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + + public void drawScreen(int mouseX,int mouseY,float partialTicks) { + + if (dragging) { + x = mouseX - dragX; + y = mouseY - dragY; + } + + + drawBackground(); + + + if (!open) + return; + + + for (Component component : components) { + component.drawScreen(mouseX,mouseY,partialTicks); + } + } + + + private void drawBackground() { + + } + + + public void mouseClicked(int mouseX,int mouseY,int mouseButton) { + + if (isHovered(mouseX,mouseY)) { + + if (mouseButton == 0) { + + dragging = true; + + dragX = mouseX - x; + dragY = mouseY - y; + } + + if (mouseButton == 1) { + open = !open; + } + } + + + if (!open) + return; + + + for (Component component : components) { + component.mouseClicked(mouseX,mouseY,mouseButton); + } + } + + + public void mouseReleased(int mouseX,int mouseY,int mouseButton) { + + if (mouseButton == 0) + dragging = false; + + + for (Component component : components) { + component.mouseReleased(mouseX,mouseY,mouseButton); + } + } + + + public void keyTyped(char typedChar,int keyCode) { + + for (Component component : components) { + component.keyTyped(typedChar,keyCode); + } + } + + + private boolean isHovered(int mouseX,int mouseY) { + + return mouseX >= x && + mouseX <= x + width && + mouseY >= y && + mouseY <= y + headerHeight; + } + + + public void addComponent(Component component) { + components.add(component); + + updateLayout(); + } + + public void updateLayout() { + + int offset = headerHeight; + + + for(Component component : components) { + + component.setOffsetY(offset); + + offset += component.getHeight(); + } + } +} \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/Component.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/Component.java new file mode 100644 index 0000000..1d33eb9 --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/Component.java @@ -0,0 +1,108 @@ +package cn.xiaozhou233.orangex.ui.clickgui.component; + +import cn.xiaozhou233.orangex.OrangeX; +import cn.xiaozhou233.orangex.ui.clickgui.Panel; +import lombok.Getter; +import lombok.Setter; + +@Getter +public abstract class Component { + + protected final Panel parent; + + protected final int offsetX; + @Setter + protected int offsetY; + + protected final int width; + protected int height; + + + public Component( + Panel parent, + int offsetX, + int offsetY, + int width, + int height + ) { + this.parent = parent; + this.offsetX = offsetX; + this.offsetY = offsetY; + this.width = width; + this.height = height; + } + + + public abstract void drawScreen( + int mouseX, + int mouseY, + float partialTicks + ); + + + public void mouseClicked( + int mouseX, + int mouseY, + int mouseButton + ) { + + } + + + public void mouseReleased( + int mouseX, + int mouseY, + int mouseButton + ) { + + } + + + public void keyTyped( + char typedChar, + int keyCode + ) { + + } + + + public int getX() { + return parent.getX() + offsetX; + } + + + public int getY() { + return parent.getY() + offsetY; + } + + + protected boolean isHovered( + int mouseX, + int mouseY + ) { + + return mouseX >= getX() + && mouseX <= getX() + width + && mouseY >= getY() + && mouseY <= getY() + height; + } + + + protected void drawString( + String text, + int x, + int y, + int color + ) { + + OrangeX.getInstance() + .getStbFontManager() + .getProxima(18) + .drawString( + text, + x, + y, + color + ); + } +} \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/impl/ModuleButton.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/impl/ModuleButton.java new file mode 100644 index 0000000..94aca1f --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/impl/ModuleButton.java @@ -0,0 +1,154 @@ +package cn.xiaozhou233.orangex.ui.clickgui.component.impl; + +import cn.xiaozhou233.orangex.module.Module; +import cn.xiaozhou233.orangex.module.option.Option; +import cn.xiaozhou233.orangex.ui.clickgui.Panel; +import cn.xiaozhou233.orangex.ui.clickgui.component.Component; +import cn.xiaozhou233.orangex.ui.clickgui.component.option.OptionComponent; +import cn.xiaozhou233.orangex.ui.clickgui.component.option.OptionComponentFactory; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.List; + +public class ModuleButton extends Component { + + private final Module module; + + @Getter + private boolean expanded; + @Getter + private final List options = new ArrayList<>(); + + public ModuleButton(Panel parent,Module module,int offsetX,int offsetY) { + super(parent,offsetX,offsetY,120,20); + + this.module = module; + + int optionY = 20; + + for (Option option : module.getOptions()) { + + OptionComponent component = + OptionComponentFactory.create( + parent, + option, + offsetX, + offsetY + optionY + ); + + if(component != null) + options.add(component); + + optionY += 18; + } + } + + + @Override + public void drawScreen(int mouseX,int mouseY,float partialTicks) { + + int color = module.isEnabled() + ? 0xff55ffff + : 0xffffffff; + + drawString( + module.getName(), + getX() + 5, + getY() + 5, + color + ); + + + if(expanded) { + for(OptionComponent option : options) { + option.drawScreen(mouseX,mouseY,partialTicks); + } + } + } + + + private void drawOptions(int mouseX,int mouseY,float partialTicks) { + + for(OptionComponent option : options) { + option.drawScreen(mouseX,mouseY,partialTicks); + } + } + + + @Override + public void mouseClicked(int mouseX,int mouseY,int mouseButton) { + + if(isHovered(mouseX,mouseY)) { + + if(mouseButton == 0) + module.toggle(); + + if(mouseButton == 1) { + + expanded = !expanded; + + parent.updateLayout(); + } + } + + + if(expanded) { + for(OptionComponent option : options) { + option.mouseClicked(mouseX,mouseY,mouseButton); + } + } + } + + + private void mouseClickedOptions(int mouseX,int mouseY,int mouseButton) { + + for(OptionComponent option : options) { + option.mouseClicked(mouseX,mouseY,mouseButton); + } + } + + + @Override + public void mouseReleased(int mouseX, int mouseY, int mouseButton) { + + if (expanded) { + mouseReleasedOptions(mouseX,mouseY,mouseButton); + } + } + + + private void mouseReleasedOptions(int mouseX,int mouseY,int mouseButton) { + + } + + + @Override + public void keyTyped(char typedChar, int keyCode) { + + if (expanded) { + keyTypedOptions(typedChar,keyCode); + } + } + + + private void keyTypedOptions(char typedChar,int keyCode) { + + } + + + protected void drawString(String text,int x,int y,int color) { + + } + + @Override + public int getHeight() { + + if(!expanded) + return 20; + + + return 20 + options.size() * 18; + } + +} \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/BooleanComponent.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/BooleanComponent.java new file mode 100644 index 0000000..7aa625b --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/BooleanComponent.java @@ -0,0 +1,55 @@ +package cn.xiaozhou233.orangex.ui.clickgui.component.option; + +import cn.xiaozhou233.orangex.module.option.impl.BooleanOption; +import cn.xiaozhou233.orangex.ui.clickgui.Panel; + +public class BooleanComponent extends OptionComponent{ + + private final BooleanOption option; + + + public BooleanComponent( + Panel parent, + BooleanOption option, + int offsetX, + int offsetY + ) { + super(parent,option,offsetX,offsetY,120,18); + this.option = option; + } + + + @Override + public void drawScreen(int mouseX,int mouseY,float partialTicks) { + + drawString( + option.getName(), + getX() + 5, + getY() + 5, + 0xffffffff + ); + + + drawString( + option.getValue() ? "ON" : "OFF", + getX() + width - 25, + getY() + 5, + option.getValue() + ? 0xff55ffff + : 0xffffffff + ); + } + + + @Override + public void mouseClicked(int mouseX,int mouseY,int mouseButton) { + + if (!isHovered(mouseX,mouseY)) + return; + + + if (mouseButton == 0) { + option.setValue(!option.getValue()); + } + } +} \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/ColorComponent.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/ColorComponent.java new file mode 100644 index 0000000..959c77b --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/ColorComponent.java @@ -0,0 +1,88 @@ +package cn.xiaozhou233.orangex.ui.clickgui.component.option; + +import cn.xiaozhou233.orangex.module.option.impl.ColorOption; +import cn.xiaozhou233.orangex.ui.clickgui.Panel; + +import java.awt.*; + +public class ColorComponent extends OptionComponent { + + private final ColorOption option; + + + public ColorComponent( + Panel parent, + ColorOption option, + int offsetX, + int offsetY + ) { + super(parent,option,offsetX,offsetY,120,18); + this.option = option; + } + + + @Override + public void drawScreen(int mouseX,int mouseY,float partialTicks) { + + drawString( + option.getName(), + getX() + 5, + getY() + 5, + 0xffffffff + ); + + + drawRect( + getX() + width - 25, + getY() + 3, + getX() + width - 5, + getY() + 15, + option.getValue() + ); + } + + + @Override + public void mouseClicked(int mouseX,int mouseY,int mouseButton) { + + if(!isHovered(mouseX,mouseY)) + return; + + + if(mouseButton == 0) { + + option.setValue( + getNextColor() + ); + } + } + + + private int getNextColor() { + + int color = option.getValue(); + + float[] hsb = + Color.RGBtoHSB( + (color >> 16) & 255, + (color >> 8) & 255, + color & 255, + null + ); + + + hsb[0] += 0.1f; + + + if(hsb[0] > 1) + hsb[0] = 0; + + + return Color.HSBtoRGB( + hsb[0], + hsb[1], + hsb[2] + ); + } + +} \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/ModeComponent.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/ModeComponent.java new file mode 100644 index 0000000..9a3712e --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/ModeComponent.java @@ -0,0 +1,60 @@ +package cn.xiaozhou233.orangex.ui.clickgui.component.option; + +import cn.xiaozhou233.orangex.module.option.impl.ModeOption; +import cn.xiaozhou233.orangex.ui.clickgui.Panel; + +public class ModeComponent extends OptionComponent { + + private final ModeOption option; + + + public ModeComponent( + Panel parent, + ModeOption option, + int offsetX, + int offsetY + ) { + super(parent,option,offsetX,offsetY,120,18); + this.option = option; + } + + + @Override + public void drawScreen(int mouseX,int mouseY,float partialTicks) { + + drawString( + option.getName(), + getX() + 5, + getY() + 5, + 0xffffffff + ); + + + drawString( + option.getValue(), + getX() + width - 45, + getY() + 5, + 0xff55ffff + ); + } + + + @Override + public void mouseClicked(int mouseX,int mouseY,int mouseButton) { + + if (!isHovered(mouseX,mouseY)) + return; + + + if(mouseButton == 0) { + + option.next(); + } + + + if(mouseButton == 1) { + + option.previous(); + } + } +} \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/OptionComponent.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/OptionComponent.java new file mode 100644 index 0000000..670d4e2 --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/OptionComponent.java @@ -0,0 +1,42 @@ +package cn.xiaozhou233.orangex.ui.clickgui.component.option; + +import cn.xiaozhou233.orangex.module.option.Option; +import cn.xiaozhou233.orangex.ui.clickgui.Panel; +import cn.xiaozhou233.orangex.ui.clickgui.component.Component; +import lombok.Getter; + +@Getter +public abstract class OptionComponent extends Component { + + protected final Option option; + + + public OptionComponent( + Panel parent, + Option option, + int offsetX, + int offsetY, + int width, + int height + ) { + super(parent,offsetX,offsetY,width,height); + this.option = option; + } + + protected void drawRect( + int left, + int top, + int right, + int bottom, + int color + ) { + + net.minecraft.client.gui.Gui.drawRect( + left, + top, + right, + bottom, + color + ); + } +} \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/OptionComponentFactory.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/OptionComponentFactory.java new file mode 100644 index 0000000..3381a0d --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/OptionComponentFactory.java @@ -0,0 +1,72 @@ +package cn.xiaozhou233.orangex.ui.clickgui.component.option; + +import cn.xiaozhou233.orangex.module.option.Option; +import cn.xiaozhou233.orangex.module.option.impl.*; +import cn.xiaozhou233.orangex.ui.clickgui.Panel; + +public class OptionComponentFactory { + + public static OptionComponent create( + Panel panel, + Option option, + int x, + int y + ) { + + if (option instanceof BooleanOption) { + return new BooleanComponent( + panel, + (BooleanOption) option, + x, + y + ); + } + + if (option instanceof IntOption) { + return new SliderComponent( + panel, + (IntOption) option, + x, + y + ); + } + + if (option instanceof DoubleOption) { + return new SliderComponent( + panel, + (DoubleOption) option, + x, + y + ); + } + + if (option instanceof ModeOption) { + return new ModeComponent( + panel, + (ModeOption) option, + x, + y + ); + } + + if (option instanceof ColorOption) { + return new ColorComponent( + panel, + (ColorOption) option, + x, + y + ); + } + + if (option instanceof StringOption) { + return new StringComponent( + panel, + (StringOption) option, + x, + y + ); + } + + return null; + } +} \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/SliderComponent.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/SliderComponent.java new file mode 100644 index 0000000..b059359 --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/SliderComponent.java @@ -0,0 +1,158 @@ +package cn.xiaozhou233.orangex.ui.clickgui.component.option; + +import cn.xiaozhou233.orangex.module.option.impl.DoubleOption; +import cn.xiaozhou233.orangex.module.option.impl.IntOption; +import cn.xiaozhou233.orangex.ui.clickgui.Panel; + +public class SliderComponent extends OptionComponent { + + private final double min; + private final double max; + + private boolean dragging; + + + public SliderComponent( + Panel parent, + IntOption option, + int offsetX, + int offsetY + ) { + super(parent,option,offsetX,offsetY,120,18); + + this.min = option.getMin(); + this.max = option.getMax(); + } + + public SliderComponent( + Panel parent, + DoubleOption option, + int offsetX, + int offsetY + ) { + super(parent,option,offsetX,offsetY,120,18); + + this.min = option.getMin(); + this.max = option.getMax(); + } + + + @Override + public void drawScreen(int mouseX,int mouseY,float partialTicks) { + + double value = getValue(); + + double percent = + (value - min) / (max - min); + + + int barWidth = width - 10; + + + drawString( + option.getName(), + getX() + 5, + getY() + 3, + 0xffffffff + ); + + + // Background bar + drawRect( + getX() + 5, + getY() + 15, + getX() + 5 + barWidth, + getY() + 18, + 0xff555555 + ); + + + // Value bar + drawRect( + getX() + 5, + getY() + 15, + getX() + 5 + (int)(barWidth * percent), + getY() + 18, + 0xff55ffff + ); + + + drawString( + String.valueOf(value), + getX() + width - 25, + getY() + 3, + 0xffaaaaaa + ); + + + if(dragging) + updateValue(mouseX); + } + + + @Override + public void mouseClicked(int mouseX,int mouseY,int mouseButton) { + + if(mouseButton == 0 && isHovered(mouseX,mouseY)) { + dragging = true; + updateValue(mouseX); + } + } + + + @Override + public void mouseReleased(int mouseX,int mouseY,int mouseButton) { + + if(mouseButton == 0) + dragging = false; + } + + + private void updateValue(int mouseX) { + + double percent = + (mouseX - (getX() + 5)) + / (double)(width - 10); + + + percent = Math.max(0,Math.min(1,percent)); + + + double value = + min + (max - min) * percent; + + + if(option instanceof IntOption) { + + ((IntOption) option) + .setValue((int)value); + + } else if(option instanceof DoubleOption) { + + ((DoubleOption) option) + .setValue(value); + } + } + + + private double getValue() { + + if(option instanceof IntOption) { + + return ((IntOption) option) + .getValue(); + + } + + + if(option instanceof DoubleOption) { + + return ((DoubleOption) option) + .getValue(); + + } + + + return 0; + } +} \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/StringComponent.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/StringComponent.java new file mode 100644 index 0000000..c53fa34 --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/ui/clickgui/component/option/StringComponent.java @@ -0,0 +1,122 @@ +package cn.xiaozhou233.orangex.ui.clickgui.component.option; + +import cn.xiaozhou233.orangex.module.option.impl.StringOption; +import cn.xiaozhou233.orangex.ui.clickgui.Panel; + +public class StringComponent extends OptionComponent { + + private final StringOption option; + + private boolean typing; + + + public StringComponent( + Panel parent, + StringOption option, + int offsetX, + int offsetY + ) { + super(parent,option,offsetX,offsetY,120,18); + this.option = option; + } + + + @Override + public void drawScreen(int mouseX,int mouseY,float partialTicks) { + + drawString( + option.getName(), + getX() + 5, + getY() + 5, + 0xffffffff + ); + + + String value = option.getValue(); + + if(typing) + value += "_"; + + + drawString( + value, + getX() + width - 50, + getY() + 5, + typing + ? 0xff55ffff + : 0xffaaaaaa + ); + } + + + @Override + public void mouseClicked( + int mouseX, + int mouseY, + int mouseButton + ) { + + if(!isHovered(mouseX,mouseY)) + return; + + + if(mouseButton == 0) { + typing = true; + } + } + + + @Override + public void keyTyped( + char typedChar, + int keyCode + ) { + + if(!typing) + return; + + + // Backspace + if(keyCode == 14) { + + String value = option.getValue(); + + if(value.length() > 0) { + option.setValue( + value.substring( + 0, + value.length() - 1 + ) + ); + } + + return; + } + + + // Enter + if(keyCode == 28) { + typing = false; + return; + } + + + if(Character.isISOControl(typedChar)) + return; + + + option.setValue( + option.getValue() + typedChar + ); + } + + + @Override + public void mouseReleased( + int mouseX, + int mouseY, + int mouseButton + ) { + + } +} \ No newline at end of file