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