[Bugfix] ClickGUI

This commit is contained in:
xiaozhou233 committed 2026-01-18 20:23:55 +08:00
1 parent b237af632e
commit 48da90dec1
8 files changed
+148 -127

No files matched your search

@@ -23,4 +23,28 @@ public class ModeValue extends Value<String> {
if (index < 0 || index >= modes.size()) return;
setValue(modes.get(index));
}
public void next() {
int index = getIndex();
if (index < 0) index = 0;
index++;
if (index >= modes.size()) index = 0;
setIndex(index);
}
public void previous() {
int index = getIndex();
if (index < 0) index = 0;
index--;
if (index < 0) index = modes.size() - 1;
setIndex(index);
}
public String getCurrentMode() {
return getValue();
}
public boolean isValidMode(String mode) {
return modes.contains(mode);
}
}
@@ -22,11 +22,13 @@ public class ClickGuiScreen extends GuiScreen {
private final List<Panel> panels = new ArrayList<>();
// 当前正在拖拽的面板
private Panel draggingPanel = null;
@Override
public void initGui() {
panels.clear();
// build panels for each category
double startX = 20;
double startY = 20;
double panelWidth = 120;
@@ -35,11 +37,9 @@ public class ClickGuiScreen extends GuiScreen {
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));
@@ -62,16 +62,8 @@ public class ClickGuiScreen extends GuiScreen {
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
// background
Gui.drawRect(
0,
0,
width,
height,
new Color(50, 50, 50, 100).getRGB()
);
Gui.drawRect(0, 0, width, height, new Color(50, 50, 50, 100).getRGB());
// update and render panels
for (Panel panel : panels) {
panel.update(mouseX, mouseY);
panel.render(mouseX, mouseY, partialTicks);
@@ -82,17 +74,36 @@ public class ClickGuiScreen extends GuiScreen {
@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
for (Panel panel : panels) {
panel.mouseClicked(mouseX, mouseY, mouseButton);
// 从后往前,优先处理最上层面板
for (int i = panels.size() - 1; i >= 0; i--) {
Panel panel = panels.get(i);
if (panel.isHovered(mouseX, mouseY) || panel.isHeaderHovered(mouseX, mouseY)) {
// 置顶:把点击的面板放到列表末尾(最后渲染)
panels.remove(panel);
panels.add(panel);
panel.mouseClicked(mouseX, mouseY, mouseButton);
// 记录拖拽面板
if (mouseButton == 0 && panel.isHeaderHovered(mouseX, mouseY)) {
draggingPanel = panel;
}
return; // 阻止下面面板响应
}
}
super.mouseClicked(mouseX, mouseY, mouseButton);
}
@Override
protected void mouseReleased(int mouseX, int mouseY, int state) {
for (Panel panel : panels) {
panel.mouseReleased(mouseX, mouseY, state);
if (draggingPanel != null) {
draggingPanel.mouseReleased(mouseX, mouseY, state);
draggingPanel = null;
}
super.mouseReleased(mouseX, mouseY, state);
}
@@ -102,7 +113,6 @@ public class ClickGuiScreen extends GuiScreen {
panel.keyTyped(typedChar, keyCode);
}
// close GUI on ESC
if (keyCode == 1) {
mc.displayGuiScreen(null);
}
@@ -19,18 +19,14 @@ public class BooleanComponent extends ValueComponent {
double ax = getAbsoluteX();
double ay = getAbsoluteY();
int bgColor = value.getValue() ? 0xFF2E8B57 : 0xFF444444;
if (isHovered(mouseX, mouseY)) {
bgColor = GuiRenderUtils.blendColor(bgColor, 0xFFFFFFFF, 0.12f);
}
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.drawRect(ax, ay, width, height, bgColor);
GuiRenderUtils.drawString(value.getName(), ax + 6, ay + 4, 0xFFFFFFFF);
GuiRenderUtils.disableBlend();
}
@@ -39,11 +35,12 @@ public class BooleanComponent extends ValueComponent {
if (!isHovered(mouseX, mouseY)) return;
if (button == 0) {
value.setValue(!value.getValue());
applyValue();
}
}
@Override
public void applyValue() {
// optional
// no-op: value 已经直接设置
}
}
@@ -5,14 +5,11 @@ 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();
private boolean listening = false;
public KeybindComponent(double x, double y, double width, KeybindValue value, Component parent) {
super(x, y, width, 14, parent);
@@ -24,24 +21,23 @@ public class KeybindComponent extends ValueComponent {
double ax = getAbsoluteX();
double ay = getAbsoluteY();
int bgColor = 0xFF333333;
if (isHovered(mouseX, mouseY)) {
bgColor = GuiRenderUtils.blendColor(bgColor, 0xFFFFFFFF, 0.12f);
}
GuiRenderUtils.enableBlend();
GuiRenderUtils.drawRect(ax, ay, width, height, bgColor);
// 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);
String keyName = listening ? "Press a key..." : Minecraft.getMinecraft().gameSettings.getKeyDisplayString(value.getValue());
GuiRenderUtils.drawString(value.getName() + ": " + keyName, ax + 6, ay + 4, 0xFFFFFFFF);
GuiRenderUtils.disableBlend();
}
@Override
public void mouseClicked(int mouseX, int mouseY, int button) {
if (!isHovered(mouseX, mouseY)) return;
if (button == 0) {
listening = true;
}
@@ -51,12 +47,18 @@ public class KeybindComponent extends ValueComponent {
public void keyTyped(char typedChar, int keyCode) {
if (!listening) return;
if (keyCode == 1) {
listening = false;
return;
}
value.setKey(keyCode);
listening = false;
applyValue();
}
@Override
public void applyValue() {
// optional
// no-op
}
}
@@ -4,12 +4,8 @@ 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;
@@ -23,33 +19,31 @@ public class ModeComponent extends ValueComponent {
double ax = getAbsoluteX();
double ay = getAbsoluteY();
int bgColor = 0xFF333333;
if (isHovered(mouseX, mouseY)) {
bgColor = GuiRenderUtils.blendColor(bgColor, 0xFFFFFFFF, 0.12f);
}
GuiRenderUtils.enableBlend();
GuiRenderUtils.drawRect(ax, ay, width, height, bgColor);
// 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);
String text = value.getName() + ": " + value.getValue();
GuiRenderUtils.drawString(text, ax + 6, ay + 4, 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);
}
if (button == 0) value.next();
else if (button == 1) value.previous();
applyValue();
}
@Override
public void applyValue() {
// optional
// no-op
}
}
@@ -38,15 +38,12 @@ public class ModuleButton extends Component {
bgColor = GuiRenderUtils.blendColor(bgColor, 0xFFFFFFFF, 0.15f);
}
// draw background
GuiRenderUtils.enableBlend();
GuiRenderUtils.drawRect(ax, ay, width, baseHeight, bgColor);
// draw status bar (left)
int statusColor = module.isEnabled() ? 0xFF00FF00 : 0xFF888888;
GuiRenderUtils.drawRect(ax, ay, 2, baseHeight, statusColor);
// draw module name
GuiRenderUtils.drawString(module.getName(), ax + 6, ay + 5, 0xFFFFFFFF);
GuiRenderUtils.disableBlend();
@@ -58,7 +55,7 @@ public class ModuleButton extends Component {
double offsetY = baseHeight;
for (Component component : components) {
component.setY(offsetY);
component.setWidth(width); // ensure same width
component.setWidth(width);
component.render(mouseX, mouseY, partialTicks);
offsetY += component.getHeight();
}
@@ -76,6 +73,18 @@ public class ModuleButton extends Component {
@Override
public void mouseClicked(int mouseX, int mouseY, int button) {
// 先让子组件处理点击(优先级更高)
if (expanded) {
for (Component component : components) {
if (component.isHovered(mouseX, mouseY)) {
component.mouseClicked(mouseX, mouseY, button);
return; // 子组件处理后,不再继续处理 module 的开关
}
}
}
// 只有点击到 ModuleButton 自己时才处理开关/展开
if (!isHovered(mouseX, mouseY)) return;
if (button == 0) {
@@ -8,10 +8,10 @@ import cn.xiaozhou233.orangex.ui.clickgui.render.GuiRenderUtils;
public class NumberComponent extends ValueComponent {
private final NumberValue value;
private boolean dragging;
private boolean dragging = false;
public NumberComponent(double x, double y, double width, NumberValue value, Component parent) {
super(x, y, width, 16, parent);
super(x, y, width, 14, parent);
this.value = value;
}
@@ -20,38 +20,47 @@ public class NumberComponent extends ValueComponent {
double ax = getAbsoluteX();
double ay = getAbsoluteY();
double min = value.getMin();
double max = value.getMax();
double cur = value.getValue();
double ratio = (cur - min) / (max - min);
double fillWidth = ratio * (width - 10);
int bgColor = 0xFF333333;
int fillColor = 0xFF2E8B57;
if (isHovered(mouseX, mouseY)) {
bgColor = GuiRenderUtils.blendColor(bgColor, 0xFFFFFFFF, 0.12f);
}
GuiRenderUtils.enableBlend();
GuiRenderUtils.drawRect(ax, ay, width, height, bgColor);
GuiRenderUtils.drawRect(ax + 5, ay + 9, fillWidth, 2, fillColor);
// 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);
String text = value.getName() + ": " + String.format("%.2f", cur);
GuiRenderUtils.drawString(text, ax + 6, ay + 3, 0xFFFFFFFF);
GuiRenderUtils.disableBlend();
}
@Override
public void update(int mouseX, int mouseY) {
if (!dragging) return;
double ax = getAbsoluteX();
double rel = mouseX - ax - 5;
double ratio = rel / (width - 10);
ratio = Math.max(0, Math.min(1, ratio));
double newVal = value.getMin() + ratio * (value.getMax() - value.getMin());
value.setValue(newVal);
applyValue();
}
@Override
public void mouseClicked(int mouseX, int mouseY, int button) {
if (!isHovered(mouseX, mouseY)) return;
if (button == 0) {
dragging = true;
updateValue(mouseX);
}
if (button == 0) dragging = true;
}
@Override
@@ -59,27 +68,8 @@ public class NumberComponent extends ValueComponent {
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
// no-op
}
}
@@ -28,18 +28,13 @@ public class Panel extends Component {
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, new Color(32, 32, 32, 255).getRGB());
// draw title
GuiRenderUtils.drawCenteredString(title, ax + width / 2, ay + 5, new Color(255, 255, 255, 255).getRGB());
GuiRenderUtils.disableBlend();
@@ -58,9 +53,6 @@ public class Panel extends Component {
this.height = offsetY;
}
/* ================= 更新 ================= */
@Override
public void update(int mouseX, int mouseY) {
if (dragging) {
@@ -75,8 +67,6 @@ public class Panel extends Component {
}
}
/* ================= 鼠标 ================= */
@Override
public void mouseClicked(int mouseX, int mouseY, int button) {
if (isHeaderHovered(mouseX, mouseY)) {
@@ -110,15 +100,20 @@ public class Panel extends Component {
}
}
/* ================= 工具 ================= */
private boolean isHeaderHovered(int mouseX, int mouseY) {
public boolean isHeaderHovered(int mouseX, int mouseY) {
double px = getAbsoluteX();
double py = getAbsoluteY();
return mouseX >= px && mouseX <= px + width
&& mouseY >= py && mouseY <= py + headerHeight;
}
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 void addComponent(Component component) {
components.add(component);
}