134 lines
4.3 KiB
C++
Raw Normal View History

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>
#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
static std::vector<std::string> &get_history() {
static std::vector<std::string> history = {};
return history;
}
2024-02-01 03:12:24 -05:00
// Structure
2024-11-08 02:34:58 -05:00
struct ChatScreen final : TextInputScreen {
2024-02-01 14:56:16 -05:00
TextInputBox *chat;
Button *send;
int history_pos;
2024-02-01 03:12:24 -05:00
// Init
2024-11-08 02:34:58 -05:00
std::vector<std::string> local_history = {};
void init() override {
TextInputScreen::init();
2024-02-01 14:56:16 -05:00
// Text Input
2024-11-08 02:34:58 -05:00
chat = new TextInputBox;
m_textInputs->push_back(chat);
2024-11-08 20:50:34 -05:00
chat->init(self->font);
2024-11-08 02:34:58 -05:00
chat->setFocused(true);
history_pos = get_history().size();
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();
2024-11-08 02:34:58 -05:00
chat->setMaxLength(max_length);
2024-02-01 14:56:16 -05:00
// Send Button
2024-11-08 02:34:58 -05:00
send = touch_create_button(1, "Send");
2024-11-08 20:50:34 -05:00
self->rendered_buttons.push_back(send);
self->selectable_buttons.push_back(send);
2024-02-01 14:56:16 -05:00
// Hide Chat Messages
2024-02-01 03:12:24 -05:00
is_in_chat = true;
2024-11-08 02:34:58 -05:00
}
2024-02-01 03:12:24 -05:00
// Removal
2024-11-08 02:34:58 -05:00
~ChatScreen() override {
2024-02-01 03:12:24 -05:00
is_in_chat = false;
2024-11-08 02:34:58 -05:00
delete chat;
send->destructor_deleting();
}
2024-02-01 03:12:24 -05:00
// Rendering
2024-11-08 02:34:58 -05:00
void render(const int x, const int y, const float param_1) override {
2024-02-01 03:12:24 -05:00
// Background
2024-11-08 20:50:34 -05:00
self->renderBackground();
2024-02-01 03:12:24 -05:00
// Render Chat
2024-11-08 20:50:34 -05:00
self->minecraft->gui.renderChatMessages(self->height, 20, true, self->font);
2024-02-01 03:12:24 -05:00
// Call Original Method
2024-11-08 02:34:58 -05:00
TextInputScreen::render(x, y, param_1);
}
2024-02-01 03:12:24 -05:00
// Positioning
2024-11-08 02:34:58 -05:00
void setupPositions() override {
TextInputScreen::setupPositions();
send->height = 24;
send->width = 40;
2024-09-20 21:30:47 -04:00
constexpr int x = 0;
2024-11-08 20:50:34 -05:00
const int y = self->height - send->height;
const int width = self->width - send->width;
2024-11-08 02:34:58 -05:00
chat->setSize(x, y, width, send->height);
2024-11-08 20:50:34 -05:00
send->y = self->height - send->height;
2024-11-08 02:34:58 -05:00
send->x = x + width;
}
2024-02-01 03:12:24 -05:00
// Key Presses
2024-11-08 02:34:58 -05:00
void keyPressed(const int key) override {
if (chat->isFocused()) {
2024-09-20 21:30:47 -04:00
if (key == MC_KEY_RETURN) {
2024-11-08 02:34:58 -05:00
if (chat->getText().length() > 0) {
const std::string text = chat->getText();
if (get_history().size() == 0 || text != get_history().back()) {
get_history().push_back(text);
}
2024-11-08 20:50:34 -05:00
_chat_send_message_to_server(self->minecraft, text.c_str());
}
2024-11-08 20:50:34 -05:00
self->minecraft->setScreen(nullptr);
2024-09-20 21:30:47 -04:00
} else if (key == MC_KEY_UP) {
// Up
2024-11-08 02:34:58 -05:00
local_history.at(history_pos) = chat->getText();
// Change
2024-11-08 02:34:58 -05:00
history_pos -= 1;
if (history_pos < 0) history_pos = local_history.size() - 1;
chat->setText(local_history.at(history_pos));
return;
2024-09-20 21:30:47 -04:00
} else if (key == MC_KEY_DOWN) {
// Down
2024-11-08 02:34:58 -05:00
local_history.at(history_pos) = chat->getText();
// Change
2024-11-08 02:34:58 -05:00
history_pos += 1;
if (history_pos > int(local_history.size()) - 1) history_pos = 0;
chat->setText(local_history.at(history_pos));
return;
}
2024-02-01 03:12:24 -05:00
}
// Call Original Method
2024-11-08 02:34:58 -05:00
TextInputScreen::keyPressed(key);
}
2024-02-01 14:56:16 -05:00
// Button Click
2024-11-08 02:34:58 -05:00
void buttonClicked(Button *button) override {
if (button == send) {
2024-02-01 14:56:16 -05:00
// Send
2024-11-08 02:34:58 -05:00
chat->setFocused(true);
2024-11-08 20:50:34 -05:00
self->keyPressed(0x0d);
2024-02-01 14:56:16 -05:00
} else {
// Call Original Method
2024-11-08 02:34:58 -05:00
TextInputScreen::buttonClicked(button);
2024-02-01 14:56:16 -05:00
}
2024-11-08 02:34:58 -05:00
}
};
2024-02-01 03:12:24 -05:00
static Screen *create_chat_screen() {
2024-11-08 02:34:58 -05:00
return extend_struct<Screen, ChatScreen>();
2024-02-01 03:12:24 -05:00
}
// 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
}
});
}