From a336cd1c7b9ff969c6995f9d0e9c9057b513d714 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Fri, 2 Feb 2024 22:25:22 -0500 Subject: [PATCH] Improve Key Handling --- mods/include/mods/chat/chat.h | 4 --- mods/include/mods/input/input.h | 2 -- mods/include/mods/misc/misc.h | 2 ++ mods/src/chat/ui.cpp | 18 +++++----- mods/src/compat/compat.c | 9 ----- mods/src/input/toggle.c | 61 ++++++++++++++------------------- mods/src/misc/api.cpp | 35 ++++++++++++++++--- mods/src/sign/sign.cpp | 4 +++ symbols/src/gui/Gui.def | 1 + 9 files changed, 70 insertions(+), 66 deletions(-) diff --git a/mods/include/mods/chat/chat.h b/mods/include/mods/chat/chat.h index 3ff276c389..f8a1001922 100644 --- a/mods/include/mods/chat/chat.h +++ b/mods/include/mods/chat/chat.h @@ -13,10 +13,6 @@ std::string chat_send_api_command(Minecraft *minecraft, std::string str); extern "C" { #endif -#ifndef MCPI_SERVER_MODE -void chat_open(); -#endif - // Override using the HOOK() macro to provide customized chat behavior. void chat_send_message(ServerSideNetworkHandler *server_side_network_handler, char *username, char *message); void chat_handle_packet_send(Minecraft *minecraft, ChatPacket *packet); diff --git a/mods/include/mods/input/input.h b/mods/include/mods/input/input.h index c738365e04..78b1a4b726 100644 --- a/mods/include/mods/input/input.h +++ b/mods/include/mods/input/input.h @@ -10,8 +10,6 @@ typedef void (*input_tick_function_t)(Minecraft *minecraft); void input_run_on_tick(input_tick_function_t function); void input_set_is_right_click(int val); -void input_hide_gui(); -void input_third_person(); int input_back(); void input_drop(int drop_slot); void input_open_crafting(); diff --git a/mods/include/mods/misc/misc.h b/mods/include/mods/misc/misc.h index 06b8a78690..b8a24c5fe2 100644 --- a/mods/include/mods/misc/misc.h +++ b/mods/include/mods/misc/misc.h @@ -22,6 +22,8 @@ void misc_run_on_creative_inventory_setup(misc_update_function_FillingContainer_ typedef void (*misc_update_function_void_t)(void *obj); void misc_run_on_tiles_setup(misc_update_function_void_t function); // obj == NULL void misc_run_on_items_setup(misc_update_function_void_t function); // obj == NULL +typedef bool (*misc_update_function_key_press_t)(Minecraft *minecrtaft, int key); +void misc_run_on_game_key_press(misc_update_function_key_press_t function); // In-Game Key Presses Only void Level_saveLevelData_injection(Level *level); diff --git a/mods/src/chat/ui.cpp b/mods/src/chat/ui.cpp index 274e2d364a..cb92195e40 100644 --- a/mods/src/chat/ui.cpp +++ b/mods/src/chat/ui.cpp @@ -111,19 +111,17 @@ static Screen *create_chat_screen() { return (Screen *) screen; } -// Open Screen -static bool open_chat_screen = false; -void chat_open() { - open_chat_screen = true; -} - // Init void _init_chat_ui() { - misc_run_on_tick([](Minecraft *minecraft) { - if (open_chat_screen && Minecraft_isLevelGenerated(minecraft) && minecraft->screen == NULL) { - Minecraft_setScreen(minecraft, create_chat_screen()); + misc_run_on_game_key_press([](Minecraft *minecraft, int key) { + if (key == 0x54) { + if (Minecraft_isLevelGenerated(minecraft) && minecraft->screen == NULL) { + Minecraft_setScreen(minecraft, create_chat_screen()); + } + return true; + } else { + return false; } - open_chat_screen = false; }); } diff --git a/mods/src/compat/compat.c b/mods/src/compat/compat.c index aa8f351394..fd016cb287 100644 --- a/mods/src/compat/compat.c +++ b/mods/src/compat/compat.c @@ -63,15 +63,6 @@ HOOK(SDL_PollEvent, int, (SDL_Event *event)) { } else if (event->key.keysym.sym == SDLK_F2) { screenshot_take(home_get()); handled = 1; - } else if (event->key.keysym.sym == SDLK_F1) { - input_hide_gui(); - handled = 1; - } else if (event->key.keysym.sym == SDLK_F5) { - input_third_person(); - handled = 1; - } else if (event->key.keysym.sym == SDLK_t) { - chat_open(); - handled = 1; } else if (event->key.keysym.sym == SDLK_ESCAPE) { // Treat Escape As Back Button Press (This Fixes Issues With Signs) handled = input_back(); diff --git a/mods/src/input/toggle.c b/mods/src/input/toggle.c index 2d117932b9..cbfe1a72c6 100644 --- a/mods/src/input/toggle.c +++ b/mods/src/input/toggle.c @@ -4,40 +4,29 @@ #include "input-internal.h" #include #include - -// Enable Toggles -static int enable_toggles = 0; - -// Store Function Input -static int hide_gui_toggle = 0; -void input_hide_gui() { - hide_gui_toggle++; -} -static int third_person_toggle = 0; -void input_third_person() { - third_person_toggle++; -} +#include // Handle Toggle Options -static void _handle_toggle_options(Minecraft *minecraft) { - if (enable_toggles) { - // Handle Functions - Options *options = &minecraft->options; - if (hide_gui_toggle % 2 != 0) { - // Toggle Hide GUI - options->hide_gui = options->hide_gui ^ 1; - } - hide_gui_toggle = 0; - if (third_person_toggle % 2 != 0) { - // Toggle Third Person - options->third_person = (options->third_person + 1) % 3; - } - third_person_toggle = 0; - // Fix Broken Value From Third-Person OptionsButton Toggle - // (Because Front-Facing Code Repurposes A Boolean As A Ternary) - if (options->third_person == 3) { - options->third_person = 0; - } +static bool _handle_toggle_options(Minecraft *minecraft, int key) { + Options *options = &minecraft->options; + if (key == 0x70) { + // Toggle Hide GUI + options->hide_gui = options->hide_gui ^ 1; + return 1; + } else if (key == 0x74) { + // Toggle Third Person + options->third_person = (options->third_person + 1) % 3; + return 1; + } else { + return 0; + } +} +static void _fix_third_person(Minecraft *minecraft) { + // Fix Broken Value From Third-Person OptionsButton Toggle + // (Because Front-Facing Code Repurposes A Boolean As A Ternary) + Options *options = &minecraft->options; + if (options->third_person == 3) { + options->third_person = 0; } } @@ -99,11 +88,11 @@ static void ParticleEngine_render_injection(ParticleEngine *particle_engine, Ent // Init void _init_toggle() { - enable_toggles = feature_has("Bind Common Toggleable Options To Function Keys", server_disabled); - input_run_on_tick(_handle_toggle_options); + if (feature_has("Bind Common Toggleable Options To Function Keys", server_disabled)) { + misc_run_on_game_key_press(_handle_toggle_options); + misc_run_on_update(_fix_third_person); - // Font-Facing View - if (enable_toggles) { + // Font-Facing View overwrite_calls((void *) GameRenderer_setupCamera, (void *) GameRenderer_setupCamera_injection); overwrite_calls((void *) ParticleEngine_render, (void *) ParticleEngine_render_injection); } diff --git a/mods/src/misc/api.cpp b/mods/src/misc/api.cpp index d8ec95aa1c..196c38822e 100644 --- a/mods/src/misc/api.cpp +++ b/mods/src/misc/api.cpp @@ -7,18 +7,20 @@ #include "misc-internal.h" // Callbacks -#define SETUP_CALLBACK(name, type) \ -static std::vector &get_misc_##name##_functions() { \ +#define STORE_CALLBACK(name, type) \ + static std::vector &get_misc_##name##_functions() { \ static std::vector functions; \ return functions; \ } \ + void misc_run_on_##name(misc_update_function_##type##_t function) { \ + get_misc_##name##_functions().push_back(function); \ + } +#define SETUP_CALLBACK(name, type) \ + STORE_CALLBACK(name, type) \ static void handle_misc_##name(type *obj) { \ for (misc_update_function_##type##_t function : get_misc_##name##_functions()) { \ function(obj); \ } \ - } \ - void misc_run_on_##name(misc_update_function_##type##_t function) { \ - get_misc_##name##_functions().push_back(function); \ } // Run Functions On Update @@ -103,6 +105,27 @@ static void Item_initItems_injection() { Item_initItems(); } +// Run Functions On GUI Key Press +STORE_CALLBACK(game_key_press, key_press) +static bool handle_misc_game_key_press(Minecraft *minecraft, int key) { + for (misc_update_function_key_press_t function : get_misc_game_key_press_functions()) { + if (function(minecraft, key)) { + return true; + } + } + return false; +} +// Handle Key Presses +static void Gui_handleKeyPressed_injection(Gui *self, int key) { + // Run Functions + if (handle_misc_game_key_press(self->minecraft, key)) { + return; + } + + // Call Original Method + Gui_handleKeyPressed(self, key); +} + // Init void _init_misc_api() { // Handle Custom Update Behavior @@ -117,4 +140,6 @@ void _init_misc_api() { // Handle Custom Item/Tile Init Behavior overwrite_calls((void *) Tile_initTiles, (void *) Tile_initTiles_injection); overwrite_calls((void *) Item_initItems, (void *) Item_initItems_injection); + // Handle Key Presses + overwrite_calls((void *) Gui_handleKeyPressed, (void *) Gui_handleKeyPressed_injection); } diff --git a/mods/src/sign/sign.cpp b/mods/src/sign/sign.cpp index 11cbac8664..eeb61ffbb6 100644 --- a/mods/src/sign/sign.cpp +++ b/mods/src/sign/sign.cpp @@ -19,6 +19,10 @@ static int32_t sdl_key_to_minecraft_key_injection(int32_t sdl_key) { return 0x25; } else if (sdl_key == SDLK_RIGHT) { return 0x27; + } else if (sdl_key == SDLK_F1) { + return 0x70; + } else if (sdl_key == SDLK_F5) { + return 0x74; } else { // Call Original Method return Common_sdl_key_to_minecraft_key(sdl_key); diff --git a/symbols/src/gui/Gui.def b/symbols/src/gui/Gui.def index fc7595ac88..2878998400 100644 --- a/symbols/src/gui/Gui.def +++ b/symbols/src/gui/Gui.def @@ -12,6 +12,7 @@ method void addMessage(std::string *text) = 0x27820; method void getSlotPos(int slot, int *x, int *y) = 0x25548; method void renderSlot(int slot, int x, int y, float alpha) = 0x25cc0; method void renderSlotText(ItemInstance *item, float x, float y, bool finite, bool shadow) = 0x25df8; +method void handleKeyPressed(int key) = 0x25a08; property Minecraft *minecraft = 0x9f4; property float selected_item_text_timer = 0x9fc;