2024-02-01 08:12:24 +00:00
|
|
|
// Config Needs To Load First
|
|
|
|
#include <libreborn/libreborn.h>
|
|
|
|
|
|
|
|
// Chat UI Code Is Useless In Headless Mode
|
|
|
|
#ifndef MCPI_HEADLESS_MODE
|
|
|
|
|
|
|
|
#include "chat-internal.h"
|
|
|
|
#include <mods/chat/chat.h>
|
|
|
|
#include <mods/text-input-box/TextInputScreen.h>
|
|
|
|
#include <mods/misc/misc.h>
|
2024-02-02 09:20:34 +00:00
|
|
|
#include <mods/touch/touch.h>
|
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;
|
2024-02-01 08:12:24 +00:00
|
|
|
};
|
|
|
|
CUSTOM_VTABLE(chat_screen, Screen) {
|
|
|
|
TextInputScreen::setup(vtable);
|
|
|
|
// Init
|
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
|
2024-02-02 09:20:34 +00:00
|
|
|
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);
|
2024-02-04 02:07:53 +00:00
|
|
|
// Determine Max Length
|
2024-02-02 09:20:34 +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
|
2024-02-02 09:20:34 +00:00
|
|
|
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;
|
|
|
|
self->send->vtable->destructor_deleting(self->send);
|
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
|
|
|
|
super->vtable->renderBackground(super);
|
|
|
|
// Render Chat
|
|
|
|
Gui_renderChatMessages(&super->minecraft->gui, super->height, 20, true, super->font);
|
|
|
|
// Call Original Method
|
|
|
|
original_render(super, x, y, param_1);
|
|
|
|
};
|
|
|
|
// Positioning
|
|
|
|
vtable->setupPositions = [](Screen *super) {
|
|
|
|
Screen_setupPositions_non_virtual(super);
|
|
|
|
ChatScreen *self = (ChatScreen *) super;
|
2024-02-02 09:20:34 +00:00
|
|
|
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;
|
2024-02-02 09:20:34 +00:00
|
|
|
if (key == 0x0d && self->chat->isFocused()) {
|
|
|
|
if (self->chat->getText().length() > 0) {
|
|
|
|
_chat_queue_message(self->chat->getText().c_str());
|
2024-02-01 19:56:16 +00:00
|
|
|
}
|
2024-02-01 08:12:24 +00:00
|
|
|
Minecraft_setScreen(super->minecraft, NULL);
|
|
|
|
}
|
|
|
|
// Call Original Method
|
|
|
|
original_keyPressed(super, key);
|
|
|
|
};
|
2024-02-01 19:56:16 +00:00
|
|
|
// Button Click
|
|
|
|
vtable->buttonClicked = [](Screen *super, Button *button) {
|
|
|
|
ChatScreen *self = (ChatScreen *) super;
|
|
|
|
if (button == self->send) {
|
|
|
|
// Send
|
|
|
|
self->chat->setFocused(true);
|
|
|
|
super->vtable->keyPressed(super, 0x0d);
|
|
|
|
} else {
|
|
|
|
// Call Original Method
|
|
|
|
Screen_buttonClicked_non_virtual(super, button);
|
|
|
|
}
|
|
|
|
};
|
2024-02-01 08:12:24 +00:00
|
|
|
}
|
|
|
|
static Screen *create_chat_screen() {
|
|
|
|
// Construct
|
|
|
|
ChatScreen *screen = new ChatScreen;
|
|
|
|
ALLOC_CHECK(screen);
|
|
|
|
Screen_constructor(&screen->super.super);
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if (Minecraft_isLevelGenerated(minecraft) && minecraft->screen == NULL) {
|
|
|
|
Minecraft_setScreen(minecraft, create_chat_screen());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2024-02-01 08:12:24 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|