Improve Key Handling

This commit is contained in:
TheBrokenRail 2024-02-02 22:25:22 -05:00
parent eaf6dd2fe2
commit a336cd1c7b
9 changed files with 70 additions and 66 deletions

View File

@ -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);

View File

@ -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();

View File

@ -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);

View File

@ -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;
});
}

View File

@ -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();

View File

@ -4,40 +4,29 @@
#include "input-internal.h"
#include <mods/input/input.h>
#include <mods/feature/feature.h>
// 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 <mods/misc/misc.h>
// 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);
}

View File

@ -7,18 +7,20 @@
#include "misc-internal.h"
// Callbacks
#define SETUP_CALLBACK(name, type) \
static std::vector<misc_update_function_##type##_t> &get_misc_##name##_functions() { \
#define STORE_CALLBACK(name, type) \
static std::vector<misc_update_function_##type##_t> &get_misc_##name##_functions() { \
static std::vector<misc_update_function_##type##_t> 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);
}

View File

@ -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);

View File

@ -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;