#include "client/window/text_input_window.hh" namespace client { namespace window { using tiw = class text_input_window; const std::string& tiw::get_send_text() noexcept { auto& text = client::input::state.text_input; text = std::string{std::begin(text), std::begin(text) + static_cast(std::min(std::size(text), shared::MAX_SAY_LENGTH))}; return text; } proto::packet tiw::make_say_packet() noexcept { proto::packet packet; const auto sub_say_packet = packet.mutable_say_packet(); sub_say_packet->set_text(get_send_text()); return packet; } bool tiw::maybe_handle_keydown(const SDL_Event& event) noexcept { if (event.key.keysym.sym == SDLK_BACKSPACE) { if (!client::input::state.text_input.empty()) { client::input::state.text_input.pop_back(); } return true; } if (event.key.keysym.sym != SDLK_RETURN || event.key.repeat) { return false; } if (!client::input::state.text_input.empty()) { client::state::connection->rsend_packet(make_say_packet()); } pop_window(); return true; } const glm::vec3& tiw::get_draw_colour() noexcept { if (client::input::state.text_input.length() >= shared::MAX_SAY_LENGTH) { return basic_window::highlight_clr; } return basic_window::primary_clr; } bool tiw::maybe_handle_event(const SDL_Event& event) noexcept { switch (event.type) { case SDL_KEYDOWN: return this->maybe_handle_keydown(event); } return basic_window::maybe_handle_event(event); } void tiw::draw() noexcept { basic_window::draw(); client::render::draw_rectangle({.pos = {.offset = this->pos}, .size = {.offset = this->size}, .colour = {this->get_draw_colour(), 1.0f}}); client::render::draw_text( this->get_send_text(), {.pos = {.extent = {0.0f, 0.0f}, .offset = this->pos + (this->size / 2.0f)}, .offset_height = this->size.y / 2.0f, .colour = {this->font_colour, 1.0f}, .has_backing = false, .is_centered = true, .is_vcentered = true}); }; } // namespace window } // namespace client