mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(ui): update text input area on field changes and window resize
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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 <typename F> 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
|
||||
11
src/app.cpp
11
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<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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user