minecraft-pi-reborn/mods/src/chat/ui.cpp

156 lines
5.5 KiB
C++
Raw Permalink Normal View History

2024-02-01 08:12:24 +00:00
#include "chat-internal.h"
2024-05-09 05:25:53 +00:00
#include <libreborn/libreborn.h>
2024-02-01 08:12:24 +00:00
#include <mods/chat/chat.h>
#include <mods/text-input-box/TextInputScreen.h>
#include <mods/misc/misc.h>
#include <mods/touch/touch.h>
2024-02-01 08:12:24 +00:00
static std::vector<std::string> &get_history() {
static std::vector<std::string> history = {};
return history;
}
2024-02-01 08:12:24 +00:00
// Structure
struct ChatScreen {
TextInputScreen super;
2024-02-01 19:56:16 +00:00
TextInputBox *chat;
Button *send;
int history_pos;
2024-02-01 08:12:24 +00:00
};
CUSTOM_VTABLE(chat_screen, Screen) {
TextInputScreen::setup(vtable);
// Init
static std::vector<std::string> local_history = {};
2024-02-01 19:56:16 +00:00
static Screen_init_t original_init = vtable->init;
2024-02-01 08:12:24 +00:00
vtable->init = [](Screen *super) {
2024-02-01 19:56:16 +00:00
original_init(super);
2024-02-01 08:12:24 +00:00
ChatScreen *self = (ChatScreen *) super;
2024-02-01 19:56:16 +00:00
// Text Input
self->chat = TextInputBox::create();
2024-02-01 19:56:16 +00:00
self->super.m_textInputs->push_back(self->chat);
self->chat->init(super->font);
self->chat->setFocused(true);
self->history_pos = get_history().size();
local_history = get_history();
local_history.push_back("");
2024-02-04 02:07:53 +00:00
// Determine Max Length
2024-05-17 06:52:55 +00:00
std::string prefix = _chat_get_prefix(Strings::default_username);
int max_length = MAX_CHAT_MESSAGE_LENGTH - prefix.length();
self->chat->setMaxLength(max_length);
2024-02-01 19:56:16 +00:00
// Send Button
self->send = touch_create_button(1, "Send");
2024-02-01 19:56:16 +00:00
super->rendered_buttons.push_back(self->send);
super->selectable_buttons.push_back(self->send);
// Hide Chat Messages
2024-02-01 08:12:24 +00:00
is_in_chat = true;
};
// Removal
2024-02-01 19:56:16 +00:00
static Screen_removed_t original_removed = vtable->removed;
2024-02-01 08:12:24 +00:00
vtable->removed = [](Screen *super) {
2024-02-01 19:56:16 +00:00
original_removed(super);
2024-02-01 08:12:24 +00:00
is_in_chat = false;
2024-02-01 19:56:16 +00:00
ChatScreen *self = (ChatScreen *) super;
delete self->chat;
2024-05-15 09:02:19 +00:00
self->send->destructor_deleting();
2024-02-01 08:12:24 +00:00
};
// Rendering
static Screen_render_t original_render = vtable->render;
vtable->render = [](Screen *super, int x, int y, float param_1) {
// Background
2024-05-15 09:02:19 +00:00
super->renderBackground();
2024-02-01 08:12:24 +00:00
// Render Chat
2024-05-15 09:02:19 +00:00
super->minecraft->gui.renderChatMessages(super->height, 20, true, super->font);
2024-02-01 08:12:24 +00:00
// Call Original Method
original_render(super, x, y, param_1);
};
// Positioning
2024-04-03 07:19:12 +00:00
static Screen_setupPositions_t original_setupPositions = vtable->setupPositions;
2024-02-01 08:12:24 +00:00
vtable->setupPositions = [](Screen *super) {
2024-04-03 07:19:12 +00:00
original_setupPositions(super);
2024-02-01 08:12:24 +00:00
ChatScreen *self = (ChatScreen *) super;
self->send->height = 24;
2024-02-01 19:56:16 +00:00
self->send->width = 40;
2024-02-01 08:12:24 +00:00
int x = 0;
2024-02-01 19:56:16 +00:00
int y = super->height - self->send->height;
int width = super->width - self->send->width;
self->chat->setSize(x, y, width, self->send->height);
self->send->y = super->height - self->send->height;
self->send->x = x + width;
2024-02-01 08:12:24 +00:00
};
// Key Presses
static Screen_keyPressed_t original_keyPressed = vtable->keyPressed;
vtable->keyPressed = [](Screen *super, int key) {
// Handle Enter
ChatScreen *self = (ChatScreen *) super;
if (self->chat->isFocused()) {
if (key == 0x0d) {
if (self->chat->getText().length() > 0) {
std::string text = self->chat->getText();
if (get_history().size() == 0 || text != get_history().back()) {
get_history().push_back(text);
}
2024-05-09 05:25:53 +00:00
_chat_send_message(super->minecraft, text.c_str());
}
2024-05-15 09:02:19 +00:00
super->minecraft->setScreen(nullptr);
} else if (key == 0x26) {
// Up
local_history.at(self->history_pos) = self->chat->getText();
// Change
self->history_pos -= 1;
if (self->history_pos < 0) self->history_pos = local_history.size() - 1;
2024-03-18 23:58:38 +00:00
self->chat->setText(local_history.at(self->history_pos));
return;
} else if (key == 0x28) {
// Down
local_history.at(self->history_pos) = self->chat->getText();
// Change
self->history_pos += 1;
if (self->history_pos > int(local_history.size()) - 1) self->history_pos = 0;
2024-03-18 23:58:38 +00:00
self->chat->setText(local_history.at(self->history_pos));
return;
}
2024-02-01 08:12:24 +00:00
}
// Call Original Method
original_keyPressed(super, key);
};
2024-02-01 19:56:16 +00:00
// Button Click
2024-04-03 07:19:12 +00:00
static Screen_buttonClicked_t original_buttonClicked = vtable->buttonClicked;
2024-02-01 19:56:16 +00:00
vtable->buttonClicked = [](Screen *super, Button *button) {
ChatScreen *self = (ChatScreen *) super;
if (button == self->send) {
// Send
self->chat->setFocused(true);
2024-05-15 09:02:19 +00:00
super->keyPressed(0x0d);
2024-02-01 19:56:16 +00:00
} else {
// Call Original Method
2024-04-03 07:19:12 +00:00
original_buttonClicked(super, button);
2024-02-01 19:56:16 +00:00
}
};
2024-02-01 08:12:24 +00:00
}
static Screen *create_chat_screen() {
// Construct
ChatScreen *screen = new ChatScreen;
ALLOC_CHECK(screen);
2024-05-15 09:02:19 +00:00
screen->super.super.constructor();
2024-02-01 08:12:24 +00:00
// Set VTable
screen->super.super.vtable = get_chat_screen_vtable();
// Return
return (Screen *) screen;
}
// Init
void _init_chat_ui() {
2024-02-03 03:25:22 +00:00
misc_run_on_game_key_press([](Minecraft *minecraft, int key) {
if (key == 0x54) {
2024-05-15 09:08:39 +00:00
if (minecraft->isLevelGenerated() && minecraft->screen == nullptr) {
2024-05-15 09:02:19 +00:00
minecraft->setScreen(create_chat_screen());
2024-02-03 03:25:22 +00:00
}
return true;
} else {
return false;
2024-02-01 08:12:24 +00:00
}
});
}