mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 21:15:53 +08:00
[Refactor] Simple ClickGUI
This commit is contained in:
1 parent
cc242c402e
commit
547f57fc4c
18 files changed
+807
-2
No files matched your search
@@ -1,11 +1,15 @@
|
||||
package cn.xiaozhou233.orangex.module;
|
||||
|
||||
import cn.xiaozhou233.orangex.OrangeX;
|
||||
import cn.xiaozhou233.orangex.module.value.Value;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
public abstract class Module {
|
||||
protected static final Minecraft mc = Minecraft.getMinecraft();
|
||||
@@ -16,6 +20,8 @@ public abstract class Module {
|
||||
protected int keyBind = -1;
|
||||
protected final ModuleCategory category;
|
||||
protected boolean enabled = false;
|
||||
@Getter
|
||||
protected final List<Value<?>> values = new ArrayList<>();
|
||||
|
||||
public Module(String name, ModuleCategory category) {
|
||||
this.name = name;
|
||||
@@ -56,4 +62,18 @@ public abstract class Module {
|
||||
public void toggle() {
|
||||
setEnabled(!this.enabled);
|
||||
}
|
||||
|
||||
protected void addValue(Value<?> value) {
|
||||
values.add(value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Value<T> getValue(String name) {
|
||||
for (Value<?> v : values) {
|
||||
if (v.getName().equalsIgnoreCase(name)) {
|
||||
return (Value<T>) v;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,25 @@ import cn.xiaozhou233.orangex.event.impl.EventKey;
|
||||
import cn.xiaozhou233.orangex.event.impl.EventWorldLoad;
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.module.ModuleCategory;
|
||||
import cn.xiaozhou233.orangex.module.value.BooleanValue;
|
||||
import cn.xiaozhou233.orangex.module.value.KeybindValue;
|
||||
import cn.xiaozhou233.orangex.module.value.ModeValue;
|
||||
import cn.xiaozhou233.orangex.module.value.NumberValue;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
|
||||
public class Test extends Module {
|
||||
public class Test extends Module{
|
||||
|
||||
private final BooleanValue enabled = new BooleanValue("Enabled", "Toggle autoclicker", false);
|
||||
private final NumberValue cps = new NumberValue("CPS", "Clicks per second", 10.0, 1, 20, 1);
|
||||
private final KeybindValue bind = new KeybindValue("Bind", "Toggle key", 0);
|
||||
|
||||
public Test() {
|
||||
super("Test", ModuleCategory.MISC);
|
||||
setEnabled(true);
|
||||
|
||||
addValue(enabled);
|
||||
addValue(cps);
|
||||
addValue(bind);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.xiaozhou233.orangex.module.impl.render;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.module.ModuleCategory;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.ClickGuiScreen;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
public class ClickGUI extends Module {
|
||||
@@ -15,7 +16,7 @@ public class ClickGUI extends Module {
|
||||
protected void onEnable() {
|
||||
super.onEnable();
|
||||
|
||||
// mc.displayGuiScreen(new ClickGuiScreen());
|
||||
mc.displayGuiScreen(new ClickGuiScreen());
|
||||
|
||||
setEnabled(false);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package cn.xiaozhou233.orangex.module.value;
|
||||
|
||||
public class BooleanValue extends Value<Boolean> {
|
||||
public BooleanValue(String name, String description, Boolean defaultValue) {
|
||||
super(name, description, defaultValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.xiaozhou233.orangex.module.value;
|
||||
|
||||
public class KeybindValue extends Value<Integer> {
|
||||
|
||||
public KeybindValue(String name, String description, Integer defaultValue) {
|
||||
super(name, description, defaultValue);
|
||||
}
|
||||
|
||||
public void setKey(int key) {
|
||||
setValue(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.xiaozhou233.orangex.module.value;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ModeValue extends Value<String> {
|
||||
|
||||
private final List<String> modes;
|
||||
|
||||
public ModeValue(String name, String description, String defaultValue, List<String> modes) {
|
||||
super(name, description, defaultValue);
|
||||
this.modes = modes;
|
||||
}
|
||||
|
||||
public List<String> getModes() {
|
||||
return modes;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return modes.indexOf(getValue());
|
||||
}
|
||||
|
||||
public void setIndex(int index) {
|
||||
if (index < 0 || index >= modes.size()) return;
|
||||
setValue(modes.get(index));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.xiaozhou233.orangex.module.value;
|
||||
|
||||
public class NumberValue extends Value<Double> {
|
||||
|
||||
private final double min;
|
||||
private final double max;
|
||||
private final double increment;
|
||||
|
||||
public NumberValue(String name, String description, Double defaultValue, double min, double max, double increment) {
|
||||
super(name, description, defaultValue);
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
this.increment = increment;
|
||||
}
|
||||
|
||||
public double getMin() {
|
||||
return min;
|
||||
}
|
||||
|
||||
public double getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
public double getIncrement() {
|
||||
return increment;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package cn.xiaozhou233.orangex.module.value;
|
||||
|
||||
public abstract class Value<T> {
|
||||
|
||||
private final String name;
|
||||
private final String description;
|
||||
private T value;
|
||||
|
||||
public Value(String name, String description, T defaultValue) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.value = defaultValue;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public T getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package cn.xiaozhou233.orangex.ui.clickgui;
|
||||
|
||||
import cn.xiaozhou233.orangex.OrangeX;
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.module.ModuleCategory;
|
||||
import cn.xiaozhou233.orangex.module.value.*;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.panel.Panel;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.impl.ModuleButton;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.impl.BooleanComponent;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.impl.NumberComponent;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.impl.ModeComponent;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.impl.KeybindComponent;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ClickGuiScreen extends GuiScreen {
|
||||
|
||||
private final List<Panel> panels = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void initGui() {
|
||||
System.out.println("ClickGui init");
|
||||
panels.clear();
|
||||
|
||||
// build panels for each category
|
||||
double startX = 20;
|
||||
double startY = 20;
|
||||
double panelWidth = 120;
|
||||
double gap = 10;
|
||||
|
||||
for (ModuleCategory category : ModuleCategory.values()) {
|
||||
Panel panel = new Panel(category.name(), startX, startY, panelWidth);
|
||||
|
||||
// add module buttons
|
||||
for (Module module : OrangeX.getInstance().getModuleManager().getModulesByCategory(category)) {
|
||||
ModuleButton button = new ModuleButton(0, 0, panelWidth, module, panel);
|
||||
|
||||
// add all values
|
||||
for (Value<?> value : module.getValues()) {
|
||||
if (value instanceof BooleanValue) {
|
||||
button.addComponent(new BooleanComponent(0, 0, panelWidth, (BooleanValue) value, button));
|
||||
} else if (value instanceof NumberValue) {
|
||||
button.addComponent(new NumberComponent(0, 0, panelWidth, (NumberValue) value, button));
|
||||
} else if (value instanceof ModeValue) {
|
||||
button.addComponent(new ModeComponent(0, 0, panelWidth, (ModeValue) value, button));
|
||||
} else if (value instanceof KeybindValue) {
|
||||
button.addComponent(new KeybindComponent(0, 0, panelWidth, (KeybindValue) value, button));
|
||||
}
|
||||
}
|
||||
|
||||
panel.addComponent(button);
|
||||
}
|
||||
|
||||
panels.add(panel);
|
||||
startX += panelWidth + gap;
|
||||
}
|
||||
|
||||
System.out.println("Panels count: " + panels.size());
|
||||
for (Panel p : panels) {
|
||||
System.out.println("Panel " + p.getTitle() + " components: " + p.getComponents().size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
|
||||
// background
|
||||
drawDefaultBackground();
|
||||
|
||||
// update and render panels
|
||||
for (Panel panel : panels) {
|
||||
panel.update(mouseX, mouseY);
|
||||
panel.render(mouseX, mouseY, partialTicks);
|
||||
}
|
||||
|
||||
super.drawScreen(mouseX, mouseY, partialTicks);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
|
||||
for (Panel panel : panels) {
|
||||
panel.mouseClicked(mouseX, mouseY, mouseButton);
|
||||
}
|
||||
super.mouseClicked(mouseX, mouseY, mouseButton);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void mouseReleased(int mouseX, int mouseY, int state) {
|
||||
for (Panel panel : panels) {
|
||||
panel.mouseReleased(mouseX, mouseY, state);
|
||||
}
|
||||
super.mouseReleased(mouseX, mouseY, state);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void keyTyped(char typedChar, int keyCode) throws IOException {
|
||||
for (Panel panel : panels) {
|
||||
panel.keyTyped(typedChar, keyCode);
|
||||
}
|
||||
|
||||
// close GUI on ESC
|
||||
if (keyCode == 1) {
|
||||
mc.displayGuiScreen(null);
|
||||
}
|
||||
|
||||
super.keyTyped(typedChar, keyCode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package cn.xiaozhou233.orangex.ui.clickgui.component;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public abstract class Component {
|
||||
|
||||
@Setter
|
||||
protected double x, y;
|
||||
@Getter
|
||||
protected double width, height;
|
||||
|
||||
protected Component parent;
|
||||
@Getter
|
||||
@Setter
|
||||
protected boolean visible = true;
|
||||
|
||||
public Component(double x, double y, double width, double height, Component parent) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public abstract void render(int mouseX, int mouseY, float partialTicks);
|
||||
|
||||
public void update(int mouseX, int mouseY) {
|
||||
|
||||
}
|
||||
|
||||
public void mouseClicked(int mouseX, int mouseY, int button) {
|
||||
|
||||
}
|
||||
|
||||
public void mouseReleased(int mouseX, int mouseY, int button) {
|
||||
|
||||
}
|
||||
|
||||
public void keyTyped(char typedChar, int keyCode) {
|
||||
|
||||
}
|
||||
|
||||
public boolean isHovered(int mouseX, int mouseY) {
|
||||
double px = getAbsoluteX();
|
||||
double py = getAbsoluteY();
|
||||
return mouseX >= px && mouseX <= px + width
|
||||
&& mouseY >= py && mouseY <= py + height;
|
||||
}
|
||||
|
||||
public double getAbsoluteX() {
|
||||
return parent == null ? x : parent.getAbsoluteX() + x;
|
||||
}
|
||||
|
||||
public double getAbsoluteY() {
|
||||
return parent == null ? y : parent.getAbsoluteY() + y;
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package cn.xiaozhou233.orangex.ui.clickgui.component;
|
||||
|
||||
public abstract class ValueComponent extends Component {
|
||||
|
||||
public ValueComponent(double x, double y, double width, double height, Component parent) {
|
||||
super(x, y, width, height, parent);
|
||||
}
|
||||
|
||||
public abstract void applyValue();
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package cn.xiaozhou233.orangex.ui.clickgui.component.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.value.BooleanValue;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.Component;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.ValueComponent;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.render.GuiRenderUtils;
|
||||
|
||||
public class BooleanComponent extends ValueComponent {
|
||||
|
||||
private final BooleanValue value;
|
||||
|
||||
public BooleanComponent(double x, double y, double width, BooleanValue value, Component parent) {
|
||||
super(x, y, width, 14, parent);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(int mouseX, int mouseY, float partialTicks) {
|
||||
double ax = getAbsoluteX();
|
||||
double ay = getAbsoluteY();
|
||||
|
||||
GuiRenderUtils.enableBlend();
|
||||
|
||||
// background
|
||||
GuiRenderUtils.drawRect(ax, ay, width, height, 0x80000000);
|
||||
|
||||
// name text
|
||||
GuiRenderUtils.drawString(value.getName(), ax + 4, ay + 3, 0xFFFFFFFF);
|
||||
|
||||
// toggle indicator
|
||||
int color = value.getValue() ? 0xFF00FF00 : 0xFFAAAAAA;
|
||||
GuiRenderUtils.drawRect(ax + width - 14, ay + 3, 10, 8, color);
|
||||
|
||||
GuiRenderUtils.disableBlend();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(int mouseX, int mouseY, int button) {
|
||||
if (!isHovered(mouseX, mouseY)) return;
|
||||
if (button == 0) {
|
||||
value.setValue(!value.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyValue() {
|
||||
// optional
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package cn.xiaozhou233.orangex.ui.clickgui.component.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.value.KeybindValue;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.Component;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.ValueComponent;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.render.GuiRenderUtils;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
public class KeybindComponent extends ValueComponent {
|
||||
|
||||
private final KeybindValue value;
|
||||
private boolean listening;
|
||||
|
||||
private final Minecraft mc = Minecraft.getMinecraft();
|
||||
|
||||
public KeybindComponent(double x, double y, double width, KeybindValue value, Component parent) {
|
||||
super(x, y, width, 14, parent);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(int mouseX, int mouseY, float partialTicks) {
|
||||
double ax = getAbsoluteX();
|
||||
double ay = getAbsoluteY();
|
||||
|
||||
GuiRenderUtils.enableBlend();
|
||||
|
||||
// background
|
||||
GuiRenderUtils.drawRect(ax, ay, width, height, 0x80000000);
|
||||
|
||||
// name
|
||||
GuiRenderUtils.drawString(value.getName(), ax + 4, ay + 3, 0xFFFFFFFF);
|
||||
|
||||
// key text
|
||||
String keyText = listening ? "Press..." : Keyboard.getKeyName(value.getValue());
|
||||
GuiRenderUtils.drawString(keyText, ax + width - 4 - mc.fontRendererObj.getStringWidth(keyText), ay + 3, 0xFFFFFFFF);
|
||||
|
||||
GuiRenderUtils.disableBlend();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(int mouseX, int mouseY, int button) {
|
||||
if (!isHovered(mouseX, mouseY)) return;
|
||||
if (button == 0) {
|
||||
listening = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(char typedChar, int keyCode) {
|
||||
if (!listening) return;
|
||||
|
||||
value.setKey(keyCode);
|
||||
listening = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyValue() {
|
||||
// optional
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package cn.xiaozhou233.orangex.ui.clickgui.component.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.value.ModeValue;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.Component;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.ValueComponent;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.render.GuiRenderUtils;
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ModeComponent extends ValueComponent {
|
||||
private final Minecraft mc = Minecraft.getMinecraft();
|
||||
|
||||
private final ModeValue value;
|
||||
|
||||
public ModeComponent(double x, double y, double width, ModeValue value, Component parent) {
|
||||
super(x, y, width, 14, parent);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(int mouseX, int mouseY, float partialTicks) {
|
||||
double ax = getAbsoluteX();
|
||||
double ay = getAbsoluteY();
|
||||
|
||||
GuiRenderUtils.enableBlend();
|
||||
|
||||
// background
|
||||
GuiRenderUtils.drawRect(ax, ay, width, height, 0x80000000);
|
||||
|
||||
// name text
|
||||
GuiRenderUtils.drawString(value.getName(), ax + 4, ay + 3, 0xFFFFFFFF);
|
||||
|
||||
// mode text
|
||||
String mode = value.getValue();
|
||||
GuiRenderUtils.drawString(mode, ax + width - 4 - mc.fontRendererObj.getStringWidth(mode), ay + 3, 0xFFFFFFFF);
|
||||
|
||||
GuiRenderUtils.disableBlend();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(int mouseX, int mouseY, int button) {
|
||||
if (!isHovered(mouseX, mouseY)) return;
|
||||
if (button == 0) {
|
||||
List<String> modes = value.getModes();
|
||||
int idx = (value.getIndex() + 1) % modes.size();
|
||||
value.setIndex(idx);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyValue() {
|
||||
// optional
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
package cn.xiaozhou233.orangex.ui.clickgui.component.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.Component;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.render.GuiRenderUtils;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ModuleButton extends Component {
|
||||
|
||||
private final Module module;
|
||||
private final List<Component> components = new ArrayList<>();
|
||||
|
||||
@Getter
|
||||
private boolean expanded = false;
|
||||
|
||||
private final double baseHeight = 16;
|
||||
|
||||
public ModuleButton(double x, double y, double width, Module module, Component parent) {
|
||||
super(x, y, width, 16, parent);
|
||||
this.module = module;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(int mouseX, int mouseY, float partialTicks) {
|
||||
double ax = getAbsoluteX();
|
||||
double ay = getAbsoluteY();
|
||||
|
||||
// draw background
|
||||
GuiRenderUtils.enableBlend();
|
||||
GuiRenderUtils.drawRect(ax, ay, width, baseHeight, 0x80000000);
|
||||
|
||||
// draw module name
|
||||
GuiRenderUtils.drawString(module.getName(), ax + 4, ay + 3, 0xFFFFFFFF);
|
||||
GuiRenderUtils.disableBlend();
|
||||
|
||||
if (!expanded) return;
|
||||
|
||||
double offsetY = baseHeight;
|
||||
for (Component component : components) {
|
||||
component.setY(offsetY);
|
||||
component.render(mouseX, mouseY, partialTicks);
|
||||
offsetY += component.getHeight();
|
||||
}
|
||||
this.height = offsetY;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update(int mouseX, int mouseY) {
|
||||
if (!expanded) return;
|
||||
|
||||
for (Component component : components) {
|
||||
component.update(mouseX, mouseY);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(int mouseX, int mouseY, int button) {
|
||||
if (!isHovered(mouseX, mouseY)) return;
|
||||
|
||||
if (button == 0) {
|
||||
module.toggle();
|
||||
} else if (button == 1) {
|
||||
expanded = !expanded;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(int mouseX, int mouseY, int button) {
|
||||
if (!expanded) return;
|
||||
|
||||
for (Component component : components) {
|
||||
component.mouseReleased(mouseX, mouseY, button);
|
||||
}
|
||||
}
|
||||
|
||||
public void addComponent(Component component) {
|
||||
components.add(component);
|
||||
}
|
||||
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package cn.xiaozhou233.orangex.ui.clickgui.component.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.value.NumberValue;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.Component;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.ValueComponent;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.render.GuiRenderUtils;
|
||||
|
||||
public class NumberComponent extends ValueComponent {
|
||||
|
||||
private final NumberValue value;
|
||||
private boolean dragging;
|
||||
|
||||
public NumberComponent(double x, double y, double width, NumberValue value, Component parent) {
|
||||
super(x, y, width, 16, parent);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(int mouseX, int mouseY, float partialTicks) {
|
||||
double ax = getAbsoluteX();
|
||||
double ay = getAbsoluteY();
|
||||
|
||||
GuiRenderUtils.enableBlend();
|
||||
|
||||
// background
|
||||
GuiRenderUtils.drawRect(ax, ay, width, height, 0x80000000);
|
||||
|
||||
// name + value text
|
||||
String text = value.getName() + ": " + String.format("%.1f", value.getValue());
|
||||
GuiRenderUtils.drawString(text, ax + 4, ay + 3, 0xFFFFFFFF);
|
||||
|
||||
// slider background
|
||||
double barX = ax + 4;
|
||||
double barY = ay + 12;
|
||||
double barW = width - 8;
|
||||
double barH = 3;
|
||||
|
||||
GuiRenderUtils.drawRect(barX, barY, barW, barH, 0xFF444444);
|
||||
|
||||
// slider fill
|
||||
double ratio = (value.getValue() - value.getMin()) / (value.getMax() - value.getMin());
|
||||
double fillW = barW * ratio;
|
||||
GuiRenderUtils.drawRect(barX, barY, fillW, barH, 0xFF00AAFF);
|
||||
|
||||
GuiRenderUtils.disableBlend();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(int mouseX, int mouseY, int button) {
|
||||
if (!isHovered(mouseX, mouseY)) return;
|
||||
if (button == 0) {
|
||||
dragging = true;
|
||||
updateValue(mouseX);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(int mouseX, int mouseY, int button) {
|
||||
dragging = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(int mouseX, int mouseY) {
|
||||
if (dragging) {
|
||||
updateValue(mouseX);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateValue(int mouseX) {
|
||||
double rel = (mouseX - getAbsoluteX() - 4) / (width - 8);
|
||||
rel = Math.max(0, Math.min(1, rel));
|
||||
double newVal = value.getMin() + (value.getMax() - value.getMin()) * rel;
|
||||
|
||||
// snap to increment
|
||||
double inc = value.getIncrement();
|
||||
newVal = Math.round(newVal / inc) * inc;
|
||||
|
||||
value.setValue(newVal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyValue() {
|
||||
// optional
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package cn.xiaozhou233.orangex.ui.clickgui.panel;
|
||||
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.component.Component;
|
||||
import cn.xiaozhou233.orangex.ui.clickgui.render.GuiRenderUtils;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Panel extends Component {
|
||||
|
||||
@Getter
|
||||
private final String title;
|
||||
@Getter
|
||||
private final List<Component> components = new ArrayList<>();
|
||||
|
||||
private boolean dragging;
|
||||
private double dragX, dragY;
|
||||
|
||||
@Getter
|
||||
private boolean open = true;
|
||||
private final double headerHeight = 18;
|
||||
|
||||
public Panel(String title, double x, double y, double width) {
|
||||
super(x, y, width, 0, null);
|
||||
this.title = title;
|
||||
this.height = headerHeight;
|
||||
}
|
||||
|
||||
/* ================= 渲染 ================= */
|
||||
|
||||
@Override
|
||||
public void render(int mouseX, int mouseY, float partialTicks) {
|
||||
double ax = getAbsoluteX();
|
||||
double ay = getAbsoluteY();
|
||||
|
||||
// draw header background
|
||||
GuiRenderUtils.enableBlend();
|
||||
GuiRenderUtils.drawRect(ax, ay, width, headerHeight, 0x80000000);
|
||||
|
||||
// draw title
|
||||
GuiRenderUtils.drawString(title, ax + 4, ay + 4, 0xFFFFFFFF);
|
||||
GuiRenderUtils.disableBlend();
|
||||
|
||||
if (!open) return;
|
||||
|
||||
double offsetY = headerHeight;
|
||||
|
||||
for (Component component : components) {
|
||||
if (!component.isVisible()) continue;
|
||||
|
||||
component.setY(offsetY);
|
||||
component.render(mouseX, mouseY, partialTicks);
|
||||
offsetY += component.getHeight();
|
||||
}
|
||||
|
||||
this.height = offsetY;
|
||||
}
|
||||
|
||||
|
||||
/* ================= 更新 ================= */
|
||||
|
||||
@Override
|
||||
public void update(int mouseX, int mouseY) {
|
||||
if (dragging) {
|
||||
this.x = mouseX - dragX;
|
||||
this.y = mouseY - dragY;
|
||||
}
|
||||
|
||||
if (!open) return;
|
||||
|
||||
for (Component component : components) {
|
||||
component.update(mouseX, mouseY);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================= 鼠标 ================= */
|
||||
|
||||
@Override
|
||||
public void mouseClicked(int mouseX, int mouseY, int button) {
|
||||
if (isHeaderHovered(mouseX, mouseY)) {
|
||||
if (button == 0) {
|
||||
dragging = true;
|
||||
dragX = mouseX - x;
|
||||
dragY = mouseY - y;
|
||||
}
|
||||
|
||||
if (button == 1) {
|
||||
open = !open;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!open) return;
|
||||
|
||||
for (Component component : components) {
|
||||
component.mouseClicked(mouseX, mouseY, button);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(int mouseX, int mouseY, int button) {
|
||||
dragging = false;
|
||||
|
||||
if (!open) return;
|
||||
|
||||
for (Component component : components) {
|
||||
component.mouseReleased(mouseX, mouseY, button);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================= 工具 ================= */
|
||||
|
||||
private boolean isHeaderHovered(int mouseX, int mouseY) {
|
||||
double px = getAbsoluteX();
|
||||
double py = getAbsoluteY();
|
||||
return mouseX >= px && mouseX <= px + width
|
||||
&& mouseY >= py && mouseY <= py + headerHeight;
|
||||
}
|
||||
|
||||
public void addComponent(Component component) {
|
||||
components.add(component);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package cn.xiaozhou233.orangex.ui.clickgui.render;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.gui.Gui;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
public class GuiRenderUtils {
|
||||
|
||||
private static final Minecraft mc = Minecraft.getMinecraft();
|
||||
|
||||
public static void drawRect(double x, double y, double w, double h, int color) {
|
||||
Gui.drawRect((int) x, (int) y, (int) (x + w), (int) (y + h), color);
|
||||
}
|
||||
|
||||
public static void drawString(String text, double x, double y, int color) {
|
||||
FontRenderer fr = mc.fontRendererObj;
|
||||
fr.drawString(text, (int) x, (int) y, color);
|
||||
}
|
||||
|
||||
public static void enableBlend() {
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
public static void disableBlend() {
|
||||
GL11.glDisable(GL11.GL_BLEND);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user