diff --git a/include/Cubed/app.hpp b/include/Cubed/app.hpp index d0cf141..abd550f 100644 --- a/include/Cubed/app.hpp +++ b/include/Cubed/app.hpp @@ -40,6 +40,8 @@ public: void start_text_input(); void stop_text_input(); + void update_text_input_area(const glm::vec4& textbox, + float cursor_position_x); private: Config m_game_config; diff --git a/include/Cubed/ui/text_field.hpp b/include/Cubed/ui/text_field.hpp index 87e12c5..a20dc8b 100644 --- a/include/Cubed/ui/text_field.hpp +++ b/include/Cubed/ui/text_field.hpp @@ -31,7 +31,7 @@ public: bool handle_mouse_button_event(const MouseButtonEvent& e) override; bool handle_text_input_event(const TextInputEvent& e) override; bool handle_key_event(const KeyEvent& e) override; - + bool handle_window_resize_event(const WindowResizeEvent& e) override; const std::string& input_text() const; template TextField& set_on_finish(F&& f) { @@ -69,5 +69,6 @@ private: void on_render(Renderer& renderer) override; void on_update(float dt) override; void update_show_text(); + void update_input_area(); }; } // namespace Cubed \ No newline at end of file diff --git a/src/app.cpp b/src/app.cpp index e67f25f..d5cfbc9 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -784,4 +784,15 @@ void App::start_text_input() { } void App::stop_text_input() { SDL_StopTextInput(m_window.get_window()); } +void App::update_text_input_area(const glm::vec4& textbox, + float cursor_position_x) { + + SDL_Rect area{static_cast(textbox.x), static_cast(textbox.y), + static_cast(textbox.z), static_cast(textbox.w)}; + + int cursor_x = cursor_position_x - textbox.x; + + SDL_SetTextInputArea(m_window.get_window(), &area, cursor_x); +} + } // namespace Cubed \ No newline at end of file diff --git a/src/ui/text_field.cpp b/src/ui/text_field.cpp index 8f4db1e..0b27e1f 100644 --- a/src/ui/text_field.cpp +++ b/src/ui/text_field.cpp @@ -92,10 +92,20 @@ void TextField::update_show_text() { m_foreground->set_color(Color::WHITE); } update_text_scale(); - + update_input_area(); m_cursor->set_offset({13.0f + m_foreground->width(), 0.0f}); } +void TextField::update_input_area() { + if (!m_app) { + return; + } + auto p = pos(); + glm::vec4 textbox{p.x, p.y, width(), height()}; + float cursor_position_x = p.x + 13.0f + m_foreground->width(); + m_app->update_text_input_area(textbox, cursor_position_x); +} + TextField& TextField::set_scale(float scale) { m_scale = scale; return *this; @@ -227,4 +237,11 @@ bool TextField::handle_key_event(const KeyEvent& e) { return false; } + +bool TextField::handle_window_resize_event(const WindowResizeEvent& e) { + Widget::handle_window_resize_event(e); + update_input_area(); + return false; +} + } // namespace Cubed \ No newline at end of file