aboutsummaryrefslogtreecommitdiff
path: root/src/client/window/text_input_window.cc
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
commite4483eca01b48b943cd0461e24a74ae1a3139ed4 (patch)
treeed58c3c246e3af1af337697695d780aa31f6ad9a /src/client/window/text_input_window.cc
parent1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (diff)
Update to most recent version (old initial commit)
Diffstat (limited to 'src/client/window/text_input_window.cc')
-rw-r--r--src/client/window/text_input_window.cc81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/client/window/text_input_window.cc b/src/client/window/text_input_window.cc
new file mode 100644
index 0000000..0d556a4
--- /dev/null
+++ b/src/client/window/text_input_window.cc
@@ -0,0 +1,81 @@
+#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<long>(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