feat(ui): update text input area on field changes and window resize

This commit is contained in:
2026-07-17 16:40:40 +08:00
parent fa38b20a3e
commit 5a1f2dd239
4 changed files with 33 additions and 2 deletions

View File

@@ -40,6 +40,8 @@ public:
void start_text_input(); void start_text_input();
void stop_text_input(); void stop_text_input();
void update_text_input_area(const glm::vec4& textbox,
float cursor_position_x);
private: private:
Config m_game_config; Config m_game_config;

View File

@@ -31,7 +31,7 @@ public:
bool handle_mouse_button_event(const MouseButtonEvent& e) override; bool handle_mouse_button_event(const MouseButtonEvent& e) override;
bool handle_text_input_event(const TextInputEvent& e) override; bool handle_text_input_event(const TextInputEvent& e) override;
bool handle_key_event(const KeyEvent& 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; const std::string& input_text() const;
template <typename F> TextField& set_on_finish(F&& f) { template <typename F> TextField& set_on_finish(F&& f) {
@@ -69,5 +69,6 @@ private:
void on_render(Renderer& renderer) override; void on_render(Renderer& renderer) override;
void on_update(float dt) override; void on_update(float dt) override;
void update_show_text(); void update_show_text();
void update_input_area();
}; };
} // namespace Cubed } // namespace Cubed

View File

@@ -784,4 +784,15 @@ void App::start_text_input() {
} }
void App::stop_text_input() { SDL_StopTextInput(m_window.get_window()); } 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<int>(textbox.x), static_cast<int>(textbox.y),
static_cast<int>(textbox.z), static_cast<int>(textbox.w)};
int cursor_x = cursor_position_x - textbox.x;
SDL_SetTextInputArea(m_window.get_window(), &area, cursor_x);
}
} // namespace Cubed } // namespace Cubed

View File

@@ -92,10 +92,20 @@ void TextField::update_show_text() {
m_foreground->set_color(Color::WHITE); m_foreground->set_color(Color::WHITE);
} }
update_text_scale(); update_text_scale();
update_input_area();
m_cursor->set_offset({13.0f + m_foreground->width(), 0.0f}); 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) { TextField& TextField::set_scale(float scale) {
m_scale = scale; m_scale = scale;
return *this; return *this;
@@ -227,4 +237,11 @@ bool TextField::handle_key_event(const KeyEvent& e) {
return false; return false;
} }
bool TextField::handle_window_resize_event(const WindowResizeEvent& e) {
Widget::handle_window_resize_event(e);
update_input_area();
return false;
}
} // namespace Cubed } // namespace Cubed