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

66 lines
2.0 KiB
C++
Raw Normal View History

2020-12-02 23:18:49 +00:00
#include <vector>
2021-01-27 21:26:19 +00:00
#include <libreborn/libreborn.h>
2021-06-29 02:59:24 +00:00
#include <libreborn/minecraft.h>
2020-12-02 23:18:49 +00:00
2021-06-29 02:59:24 +00:00
#include "../init/init.h"
2020-12-02 23:18:49 +00:00
#include "../feature/feature.h"
2021-06-29 02:59:24 +00:00
#include "sign.h"
2020-12-02 23:18:49 +00:00
// Open Sign Screen
static void LocalPlayer_openTextEdit_injection(unsigned char *local_player, unsigned char *sign) {
2020-12-18 03:22:56 +00:00
if (*(int32_t *) (sign + TileEntity_id_property_offset) == 4) {
unsigned char *minecraft = *(unsigned char **) (local_player + LocalPlayer_minecraft_property_offset);
unsigned char *screen = (unsigned char *) ::operator new(TEXT_EDIT_SCREEN_SIZE);
2021-02-16 17:26:40 +00:00
ALLOC_CHECK(screen);
2020-12-02 23:18:49 +00:00
screen = (*TextEditScreen)(screen, sign);
(*Minecraft_setScreen)(minecraft, screen);
}
}
#define BACKSPACE_KEY 8
static int is_valid_key(char key) {
return (key >= 32 && key <= 126) || key == BACKSPACE_KEY;
}
// Store Text Input
std::vector<char> input;
2021-06-29 02:59:24 +00:00
void sign_key_press(char key) {
2020-12-02 23:18:49 +00:00
if (is_valid_key(key)) {
input.push_back(key);
}
}
2021-06-29 02:59:24 +00:00
void sign_clear_input() {
2020-12-02 23:18:49 +00:00
input.clear();
}
// Handle Text Input
static void TextEditScreen_updateEvents_injection(unsigned char *screen) {
// Call Original Method
(*Screen_updateEvents)(screen);
if (*(char *)(screen + 4) == '\0') {
uint32_t vtable = *((uint32_t *) screen);
for (char key : input) {
if (key == BACKSPACE_KEY) {
// Handle Backspace
2020-12-18 03:22:56 +00:00
(*(Screen_keyPressed_t *) (vtable + Screen_keyPressed_vtable_offset))(screen, BACKSPACE_KEY);
2020-12-02 23:18:49 +00:00
} else {
// Handle Nrmal Key
2020-12-18 03:22:56 +00:00
(*(Screen_keyboardNewChar_t *) (vtable + Screen_keyboardNewChar_vtable_offset))(screen, key);
2020-12-02 23:18:49 +00:00
}
}
}
2021-06-29 02:59:24 +00:00
sign_clear_input();
2020-12-02 23:18:49 +00:00
}
2021-06-29 02:59:24 +00:00
// Init
void init_sign() {
2020-12-02 23:18:49 +00:00
if (feature_has("Fix Sign Placement")) {
// Fix Signs
patch_address(LocalPlayer_openTextEdit_vtable_addr, (void *) LocalPlayer_openTextEdit_injection);
patch_address(TextEditScreen_updateEvents_vtable_addr, (void *) TextEditScreen_updateEvents_injection);
}
2021-06-29 02:59:24 +00:00
}