From 5f3721c294021c9a141e745e9cef34af9e54d8ae Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Sun, 8 Nov 2026 00:50:05 +0800 Subject: [PATCH] Feat: Add ClickGUI module bound to RSHIFT - Add modules/impl/render/ClickGUI with default keybind RSHIFT - Let the module open/close the input screen via requestOpen/requestClose - Sync module state when the screen is closed by vanilla handling - Remove the hard-coded GUI toggle from ClientInput/InputController --- .../orangex/event/facade/InputEvents.java | 8 +-- .../orangex/input/ClientInput.java | 22 ------ .../orangex/input/InputController.java | 18 +---- .../xiaozhou233/orangex/input/KeyCodes.java | 3 + .../orangex/modules/ModuleManager.java | 2 + .../orangex/modules/impl/render/ClickGUI.java | 72 +++++++++++++++++++ .../orangex/render/FontDebugOverlay.java | 2 +- docs/render/world-gating-and-input.md | 23 +++--- 8 files changed, 97 insertions(+), 53 deletions(-) create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/impl/render/ClickGUI.java diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/event/facade/InputEvents.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/event/facade/InputEvents.java index 00ec4a1..faf8d73 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/event/facade/InputEvents.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/event/facade/InputEvents.java @@ -55,16 +55,16 @@ public final class InputEvents { } /** - * Called from {@code GuiScreen#keyTyped(char, int)}. Returns {@code true} - * only for the toggle key, so other keys (ESC, etc.) still reach vanilla - * screen handling. + * Called from {@code GuiScreen#keyTyped(char, int)}. The event is posted to + * the EventBus; returning {@code false} keeps vanilla key handling (ESC, + * etc.) intact. */ public static boolean onKeyTyped(Object screen, int keyCode) { if (!isOurs(screen)) { return false; } KeyEvents.fire(keyCode); - return keyCode == KeyCodes.M; + return false; } public static void onDrawScreen(Object screen, int mouseX, int mouseY) { diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/ClientInput.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/ClientInput.java index f25d3d2..d5bdbc7 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/ClientInput.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/ClientInput.java @@ -15,11 +15,6 @@ import cn.xiaozhou233.orangex.wrapper.api.Minecraft; */ public final class ClientInput { - /** - * GUI toggle key name. The canonical key code is {@link KeyCodes#M}. - */ - public static final String TOGGLE_KEY_NAME = "M"; - private static volatile boolean open; private static volatile double mouseX; private static volatile double mouseY; @@ -82,23 +77,6 @@ public final class ClientInput { pendingClose = true; } - /** - * Handles a key event posted on the EventBus. Only the canonical toggle - * key (M) changes the GUI state; all other keys are ignored here but stay - * available to other subscribers. - */ - public static void onKey(Minecraft minecraft, int key) { - if (minecraft == null || key != KeyCodes.M) { - return; - } - - if (minecraft.isInputScreenOpen()) { - requestClose(); - } else if (minecraft.isInWorld() && !minecraft.isScreenOpen()) { - requestOpen(); - } - } - public static void onMouseMove(double x, double y) { mouseX = x; mouseY = y; diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/InputController.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/InputController.java index b01d706..4e43307 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/InputController.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/InputController.java @@ -1,6 +1,5 @@ package cn.xiaozhou233.orangex.input; -import cn.xiaozhou233.orangex.event.impl.EventKey; import cn.xiaozhou233.orangex.event.impl.EventTickPost; import cn.xiaozhou233.orangex.event.impl.EventTickPre; import cn.xiaozhou233.orangex.wrapper.api.Minecraft; @@ -8,8 +7,7 @@ import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; /** - * Drives {@link ClientInput}: handles {@code EventKey} from the EventBus and - * reconciles the input screen state around the game tick. + * Drives {@link ClientInput} around the game tick. */ public final class InputController { @@ -20,20 +18,6 @@ public final class InputController { this.minecraft = minecraft; } - /** - * Consumes key events posted by the injected input hooks. Only the toggle - * key changes the input screen state; every event is still visible to - * other subscribers. - */ - @Subscribe(threadMode = ThreadMode.POSTING) - public void onKey(EventKey event) { - try { - ClientInput.onKey(minecraft, event.getKey()); - } catch (Throwable throwable) { - logFailure(throwable); - } - } - @Subscribe(threadMode = ThreadMode.POSTING) public void onTick(EventTickPre event) { try { diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/KeyCodes.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/KeyCodes.java index a6421d3..23ab9dc 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/KeyCodes.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/input/KeyCodes.java @@ -16,6 +16,9 @@ public final class KeyCodes { /** LWJGL 2 {@code Keyboard.KEY_M}. */ public static final int M = 50; + /** LWJGL 2 {@code Keyboard.KEY_RSHIFT}. */ + public static final int RSHIFT = 54; + /** * Translates a GLFW key code (26.x) to the 1.8.9 LWJGL 2 key code used by * {@code EventKey}. diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/ModuleManager.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/ModuleManager.java index f97e80f..cbdbcc4 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/ModuleManager.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/ModuleManager.java @@ -2,6 +2,7 @@ package cn.xiaozhou233.orangex.modules; import cn.xiaozhou233.orangex.OrangeX; import cn.xiaozhou233.orangex.event.impl.EventKey; +import cn.xiaozhou233.orangex.modules.impl.render.ClickGUI; import cn.xiaozhou233.orangex.modules.impl.render.HUD; import org.greenrobot.eventbus.Subscribe; @@ -24,6 +25,7 @@ public class ModuleManager { public void registerAll() { register(new HUD()); + register(new ClickGUI()); } public void register(Module module) { diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/impl/render/ClickGUI.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/impl/render/ClickGUI.java new file mode 100644 index 0000000..d24a5f2 --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/impl/render/ClickGUI.java @@ -0,0 +1,72 @@ +package cn.xiaozhou233.orangex.modules.impl.render; + +import cn.xiaozhou233.orangex.event.impl.EventRender2D; +import cn.xiaozhou233.orangex.event.impl.EventTickPre; +import cn.xiaozhou233.orangex.input.ClientInput; +import cn.xiaozhou233.orangex.input.KeyCodes; +import cn.xiaozhou233.orangex.modules.Module; +import cn.xiaozhou233.orangex.modules.ModuleCategory; +import org.greenrobot.eventbus.Subscribe; + +/** + * Host module for the ClickGUI. + * + *

RSHIFT is the default keybind. Enabling the module opens the empty input + * screen and disabling it closes the screen again. Draw the actual GUI in + * {@link #onRender2D(EventRender2D)}.

+ */ +public class ClickGUI extends Module { + + private boolean justEnabled; + + public ClickGUI() { + super("ClickGUI", ModuleCategory.RENDER, KeyCodes.RSHIFT); + } + + @Override + protected void onEnable() { + super.onEnable(); + if (!mc.isInWorld() || mc.isScreenOpen()) { + // The input screen cannot open right now (title screen, chat, + // inventory, ...); do not leave the module in a half-enabled state. + setEnabled(false); + return; + } + justEnabled = true; + // Defer opening to EventTickPost: opening inside the keyboard callback + // would let the same RSHIFT event reach the new screen and close it. + ClientInput.requestOpen(); + } + + @Override + protected void onDisable() { + justEnabled = false; + ClientInput.requestClose(); + super.onDisable(); + } + + @Subscribe + public void onTick(EventTickPre event) { + if (justEnabled) { + justEnabled = false; + return; + } + if (!ClientInput.isOpen()) { + // The screen was closed by vanilla handling (for example ESC); + // keep the module state in sync instead of requiring two RSHIFT + // presses to open it again. + setEnabled(false); + } + } + + @Subscribe + public void onRender2D(EventRender2D event) { + if (!ClientInput.isOpen()) { + return; + } + + // TODO: draw the ClickGUI here. + // Cursor: ClientInput.getMouseX() / getMouseY() + // Buttons: ClientInput.isLeftDown() / isRightDown() / isMiddleDown() + } +} diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/FontDebugOverlay.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/FontDebugOverlay.java index 8d50332..60a94d2 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/FontDebugOverlay.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/render/FontDebugOverlay.java @@ -34,7 +34,7 @@ public final class FontDebugOverlay { + " mouse " + (int) ClientInput.getMouseX() + ", " + (int) ClientInput.getMouseY(), 8, 68, 0xFF55FF55); - body.drawString("M: toggle input screen", 8, 82, 0xFF888888); + body.drawString("RSHIFT: ClickGUI", 8, 82, 0xFF888888); // Large high-contrast marker: if this is invisible too, the problem is // GL state/coordinates rather than the glyph atlas. diff --git a/docs/render/world-gating-and-input.md b/docs/render/world-gating-and-input.md index adef55f..445c731 100644 --- a/docs/render/world-gating-and-input.md +++ b/docs/render/world-gating-and-input.md @@ -5,7 +5,7 @@ 1. 为什么 1.8.9 的字体 / GUI 只在进入世界后绘制,而 26.2 在主界面也会绘制,以及 OrangeX 现在如何统一。 2. 客户端 GUI 打开时,如何让鼠标在 GUI 上移动,而不是继续控制游戏视角。 -> 状态:第 1 部分和第 2 部分的空 GUI / 鼠标输入已落地。M 键打开或关闭输入界面。 +> 状态:第 1 部分和第 2 部分的空 GUI / 鼠标输入已落地。RSHIFT(右 Shift)打开或关闭输入界面。 --- @@ -280,7 +280,7 @@ Minecraft262Stubs/ OrangeX/src/main/java/cn/xiaozhou233/orangex/ ├── input/ │ ├── ClientInput.java 跨版本输入状态:开/关、鼠标坐标、按键状态 -│ ├── InputController.java 订阅 EventKey 并驱动 ClientInput +│ ├── InputController.java 在 tick 前后驱动 ClientInput │ ├── KeyCodes.java 统一的按键编号(1.8.9 LWJGL 2)+ GLFW 转换 │ ├── internal/ │ │ ├── VersionInput.java 版本输入接口 @@ -292,6 +292,7 @@ OrangeX/src/main/java/cn/xiaozhou233/orangex/ │ ├── facade/InputEvents.java 输入门面(1.8.9 注入代码) │ ├── facade/KeyEvents.java 按键事件统一投递到 EventBus │ └── impl/EventKey.java 按键事件(统一使用 1.8.9 key code) +├── modules/impl/render/ClickGUI.java ClickGUI 模块,默认 keybind = RSHIFT └── hook/impl/ ├── GuiInputHook.java 1.8.9 GuiScreen 输入 / 按键 Hook └── KeyPressHook.java 1.8.9 KeyBinding#onTick / 26.2 KeyboardHandler#keyPress 按键 Hook @@ -299,16 +300,20 @@ OrangeX/src/main/java/cn/xiaozhou233/orangex/ 行为: -- M 键在**世界内**打开空 GUI;再次按下关闭。 +- RSHIFT 是 `ClickGUI` 模块的默认 keybind。 +- `ModuleManager` 收到 RSHIFT 的 `EventKey` 后 toggle `ClickGUI`: + - 启用:`ClickGUI.onEnable` → `ClientInput.requestOpen()` + - 禁用:`ClickGUI.onDisable` → `ClientInput.requestClose()` + - 如果界面被 ESC 等原版逻辑关闭,`ClickGUI.onTick` 会在下一 tick 自动禁用模块,保证状态同步。 - 所有 `EventKey#getKey()` 都使用 1.8.9 / LWJGL 2 键码,26.2 的 GLFW 键码在投递前通过 `KeyCodes.fromGlfw` 转换。 - 按键统一走 EventBus: - - 1.8.9 打开:`KeyBinding#onTick(int)` → `InputEvents.onKeyPressed` → `KeyEvents.fire` → `EventBus.post(EventKey)` → `InputController.onKey` → `ClientInput.requestOpen()` + - 1.8.9 打开:`KeyBinding#onTick(int)` → `InputEvents.onKeyPressed` → `KeyEvents.fire` → `EventBus.post(EventKey)` → `ModuleManager.onKey` → `ClickGUI.toggle()` → `ClientInput.requestOpen()` - 1.8.9 关闭:`GuiScreen#keyTyped(char, int)` → `InputEvents.onKeyTyped` → 同一条 EventBus 链路 → `requestClose()` - 26.2:`KeyboardHandler#keyPress(long, int, KeyEvent)` 全局 Hook → `InputEvents.onGlfwKeyPressed` → 键码转换 → 同一条 EventBus 链路。行走、无界面时也能收到按键,不再依赖 tick 轮询。 - - 其他功能可以直接 `@Subscribe` `EventKey` 读取按键,不需要再改注入代码。 + - 其他模块可以直接 `@Subscribe` `EventKey` 读取按键,不需要再改注入代码。 - 打开 / 关闭都只在 tick 末尾(`EventTickPost` → `ClientInput.applyPending`)真正执行。 - - 原因:如果在键盘循环里直接打开 Screen,同一个 M 事件会立刻被 `currentScreen.handleKeyboardInput()` 再处理一次,导致刚打开就关闭。 - - 自动重复事件:1.8.9 通过 `Keyboard.isRepeatEvent()` 过滤,26.2 只处理 GLFW action == 1(press),按住 M 不会反复开关。 + - 原因:如果在键盘循环里直接打开 Screen,同一个 RSHIFT 事件会立刻被 `currentScreen.handleKeyboardInput()` 再处理一次,导致刚打开就关闭。 + - 自动重复事件:1.8.9 通过 `Keyboard.isRepeatEvent()` 过滤,26.2 只处理 GLFW action == 1(press),按住 RSHIFT 不会反复开关。 - 打开时: - 1.8.9:运行时生成 `GuiScreen` 具体子类,再 `Minecraft#displayGuiScreen(...)`;`doesGuiPauseGame()` 对自己的空壳返回 `false`。 - 26.2:`Gui#setScreen(new V26_2InputScreen())`,`isPauseScreen()` 返回 `false`。 @@ -377,8 +382,8 @@ int y = scaledHeight - Mouse.getY() * scaledHeight / mc.displayHeight - 1; | 1.8.9 注入报 `no such class: cn$...` | 注入代码写了全限定 `cn.*`,应使用简单类名 + `getInjectedClassNames()` | | 空 GUI 打开后玩家还能动 | 1.8.9 / 26.2 都必须真正打开 Screen;只 ungrab 不够 | | 1.8.9 空 GUI 导致单人暂停 | 已 Hook `GuiScreen#doesGuiPauseGame`,对自己的空壳返回 false | -| 1.8.9 短按 M 没反应 | 已改为 `KeyBinding#onTick` 事件路径;可用 `-Dorangex.debugInput=true` 查看按键日志 | -| 聊天 / 背包界面里按 M 被劫持 | 打开前检查 `isScreenOpen()`,任何原版 Screen 存在时都不响应 | +| 1.8.9 短按 RSHIFT 没反应 | 已改为 `KeyBinding#onTick` 事件路径;可用 `-Dorangex.debugInput=true` 查看按键日志 | +| 聊天 / 背包界面里按 RSHIFT 被劫持 | 打开前检查 `isScreenOpen()`,任何原版 Screen 存在时都不响应 | ---