minecraft-pi-reborn/mods/src/touch/touch.cpp

109 lines
4.7 KiB
C++
Raw Permalink Normal View History

2021-06-19 23:07:09 +00:00
#include <libreborn/libreborn.h>
2022-06-25 21:30:08 +00:00
#include <mods/feature/feature.h>
#include <mods/init/init.h>
#include <mods/touch/touch.h>
2021-06-19 23:07:09 +00:00
2021-09-12 03:18:12 +00:00
#include <symbols/minecraft.h>
2021-06-19 23:07:09 +00:00
// Enable Touch GUI
2024-01-06 11:30:23 +00:00
static int32_t Minecraft_isTouchscreen_injection(__attribute__((unused)) Minecraft *minecraft) {
2021-06-19 23:07:09 +00:00
return 1;
}
2022-04-11 23:59:21 +00:00
// IngameBlockSelectionScreen Memory Allocation Override
static unsigned char *operator_new_IngameBlockSelectionScreen_injection(__attribute__((unused)) uint32_t size) {
2024-05-17 06:52:55 +00:00
return (unsigned char *) ::operator new(sizeof(Touch_IngameBlockSelectionScreen));
2022-04-11 23:59:21 +00:00
}
2022-04-23 20:46:40 +00:00
// Improved Button Hover Behavior
2024-01-06 11:30:23 +00:00
static int32_t Button_hovered_injection(__attribute__((unused)) Button *button, __attribute__((unused)) Minecraft *minecraft, __attribute__((unused)) int32_t click_x, __attribute__((unused)) int32_t click_y) {
2022-04-23 20:46:40 +00:00
// Get Mouse Position
2024-05-17 06:52:55 +00:00
int32_t x = Mouse::getX() * Gui::InvGuiScale;
int32_t y = Mouse::getY() * Gui::InvGuiScale;
2022-04-23 20:46:40 +00:00
// Get Button Position
2024-01-06 11:30:23 +00:00
int32_t button_x1 = button->x;
int32_t button_y1 = button->y;
int32_t button_x2 = button_x1 + button->width;
int32_t button_y2 = button_y1 + button->height;
2022-04-23 20:46:40 +00:00
// Check
2024-02-03 03:28:57 +00:00
return x >= button_x1 && x < button_x2 && y >= button_y1 && y < button_y2;
2022-04-23 20:46:40 +00:00
}
2024-01-06 11:30:23 +00:00
static void LargeImageButton_render_GuiComponent_drawCenteredString_injection(GuiComponent *component, Font *font, std::string *text, int32_t x, int32_t y, int32_t color) {
2022-04-23 20:46:40 +00:00
// Change Color On Hover
2024-04-02 23:22:01 +00:00
if (color == 0xe0e0e0 && Button_hovered_injection((Button *) component, nullptr, 0, 0)) {
2022-04-23 22:49:08 +00:00
color = 0xffffa0;
2022-04-23 20:46:40 +00:00
}
// Call Original Method
2024-05-15 09:02:19 +00:00
component->drawCenteredString(font, text, x, y, color);
2022-04-23 20:46:40 +00:00
}
// Create Button
2024-02-08 02:15:48 +00:00
int touch_gui = 0;
2024-05-15 09:02:19 +00:00
template <typename T>
2024-05-17 04:36:28 +00:00
static Button *create_button(int id, std::string text) {
T *button = new T;
ALLOC_CHECK(button);
2024-05-15 09:02:19 +00:00
button->constructor(id, &text);
return (Button *) button;
}
Button *touch_create_button(int id, std::string text) {
if (touch_gui) {
2024-05-17 04:36:28 +00:00
return create_button<Touch_TButton>(id, text);
} else {
2024-05-17 04:36:28 +00:00
return create_button<Button>(id, text);
}
}
2021-06-19 23:07:09 +00:00
// Init
void init_touch() {
touch_gui = feature_has("Full Touch GUI", server_disabled);
2022-04-23 20:46:40 +00:00
int touch_buttons = touch_gui;
2021-06-19 23:07:09 +00:00
if (touch_gui) {
// Main UI
2024-05-05 00:46:15 +00:00
overwrite(Minecraft_isTouchscreen, Minecraft_isTouchscreen_injection);
2021-06-19 23:07:09 +00:00
2021-06-28 21:10:15 +00:00
// Force Correct Toolbar Size
unsigned char toolbar_patch[4] = {0x01, 0x00, 0x50, 0xe3}; // "cmp r0, #0x1"
patch((void *) 0x257b0, toolbar_patch);
2022-04-23 20:46:40 +00:00
} else {
// Force Touch Inventory
if (feature_has("Force Touch GUI Inventory", server_disabled)) {
overwrite_call((void *) 0x2943c, (void *) operator_new_IngameBlockSelectionScreen_injection);
2024-01-06 11:30:23 +00:00
overwrite_call((void *) 0x29444, (void *) Touch_IngameBlockSelectionScreen_constructor);
2022-04-25 02:30:35 +00:00
// Make "Craft" And "Armor" Buttons Use Classic GUI Style (Button And TButton Have The Same Size)
2024-01-06 11:30:23 +00:00
overwrite_call((void *) 0x3b060, (void *) Button_constructor);
overwrite_call((void *) 0x3b08c, (void *) Button_constructor);
2022-04-23 20:46:40 +00:00
}
// Force Touch Button Behavior
if (feature_has("Force Touch GUI Button Behavior", server_disabled)) {
touch_buttons = 1;
overwrite_call((void *) 0x1baf4, (void *) Minecraft_isTouchscreen_injection);
overwrite_call((void *) 0x1be40, (void *) Minecraft_isTouchscreen_injection);
overwrite_call((void *) 0x1c470, (void *) Minecraft_isTouchscreen_injection);
overwrite_call((void *) 0x1e868, (void *) Minecraft_isTouchscreen_injection);
overwrite_call((void *) 0x290b8, (void *) Minecraft_isTouchscreen_injection);
overwrite_call((void *) 0x29168, (void *) Minecraft_isTouchscreen_injection);
overwrite_call((void *) 0x3e314, (void *) Minecraft_isTouchscreen_injection);
overwrite_call((void *) 0x2cbc0, (void *) Minecraft_isTouchscreen_injection);
overwrite_call((void *) 0x2ea7c, (void *) Minecraft_isTouchscreen_injection);
overwrite_call((void *) 0x4a438, (void *) Minecraft_isTouchscreen_injection);
}
}
// Improved Button Hover Behavior
if (touch_buttons && feature_has("Improved Button Hover Behavior", server_disabled)) {
2024-05-05 00:46:15 +00:00
overwrite(Button_hovered, Button_hovered_injection);
2022-04-23 20:46:40 +00:00
overwrite_call((void *) 0x1ebd4, (void *) LargeImageButton_render_GuiComponent_drawCenteredString_injection);
2021-06-28 21:10:15 +00:00
}
2021-06-19 23:07:09 +00:00
// Show Block Outlines
int block_outlines = feature_has("Show Block Outlines", server_disabled);
2022-04-11 23:59:21 +00:00
unsigned char outline_patch[4] = {(unsigned char) (block_outlines ? !touch_gui : touch_gui), 0x00, 0x50, 0xe3}; // "cmp r0, #0x1" or "cmp r0, #0x0"
2021-06-19 23:07:09 +00:00
patch((void *) 0x4a210, outline_patch);
}