|
@@ -20,6 +20,11 @@ namespace {
|
|
|
int text_w, text_h;
|
|
|
SDL_QueryTexture(texture, nullptr, nullptr, &text_w, &text_h);
|
|
|
|
|
|
+ // TODO: the height value is based on the default font height, therefor it should not be hard coded
|
|
|
+ // this hack here is a solution for when the text is empty,
|
|
|
+ // because in that case we cannot determine the height from the rendered texture
|
|
|
+ text_h = 32;
|
|
|
+
|
|
|
int const w = std::min(text_w, area.w-LineInput::MIN_WIDTH-8);
|
|
|
int const h = std::min(text_h, area.h-LineInput::MIN_HEIGHT-8);
|
|
|
|
|
@@ -64,6 +69,8 @@ SDL_Rect LineInput::get_bounding_box() const
|
|
|
void LineInput::set_focus(bool const focus)
|
|
|
{
|
|
|
focus_ = focus;
|
|
|
+ if (!focus_)
|
|
|
+ SDL_StopTextInput();
|
|
|
}
|
|
|
|
|
|
bool LineInput::has_focus() const
|
|
@@ -71,9 +78,36 @@ bool LineInput::has_focus() const
|
|
|
return focus_;
|
|
|
}
|
|
|
|
|
|
+void LineInput::on_event(SDL_Event const& evt)
|
|
|
+{
|
|
|
+ if (!focus_)
|
|
|
+ return;
|
|
|
+
|
|
|
+ if (evt.type==SDL_TEXTINPUT) {
|
|
|
+ value_ += evt.text.text;
|
|
|
+ }
|
|
|
+ else if (evt.type==SDL_KEYUP && evt.key.keysym.sym==SDLK_BACKSPACE) {
|
|
|
+ if (!value_.empty()) {
|
|
|
+ auto const begin = value_.begin();
|
|
|
+ auto const end = value_.end();
|
|
|
+ auto it = value_.end()-1;
|
|
|
+ // For UTF-8 multibyte characters
|
|
|
+ while (it!=begin && ((*it & 0xC0)==0x80)) {
|
|
|
+ --it;
|
|
|
+ }
|
|
|
+ value_.erase(it, end);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
void LineInput::update(std::chrono::milliseconds const delta_time)
|
|
|
{
|
|
|
+ if (focus_ && !SDL_IsTextInputActive())
|
|
|
+ SDL_StartTextInput();
|
|
|
+
|
|
|
blink_timer_ = (blink_timer_+delta_time)%1'000u;
|
|
|
+ auto const rect = get_bounding_box();
|
|
|
+ SDL_SetTextInputRect(&rect);
|
|
|
}
|
|
|
|
|
|
void LineInput::render(SDLRenderer& renderer)
|
|
@@ -106,14 +140,24 @@ void LineInput::render(SDLRenderer& renderer)
|
|
|
SDL_RenderCopy(renderer, text_ure, &text_texture_rect, &text_rect);
|
|
|
SDL_DestroyTexture(text_ure);
|
|
|
|
|
|
- if(focus_) {
|
|
|
+ if (focus_) {
|
|
|
SDL_Rect cursor_rect{
|
|
|
- .x = text_rect.x + text_rect.w + 1, .y = text_rect.y, .w = 2, .h = text_rect.h,
|
|
|
+ .x = text_rect.x+text_rect.w+1, .y = text_rect.y, .w = 2, .h = text_rect.h,
|
|
|
};
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
|
|
|
- if(blink_timer_ < 500ms) {
|
|
|
- SDL_RenderFillRect(renderer, &cursor_rect);
|
|
|
+ if (blink_timer_<500ms) {
|
|
|
+ SDL_RenderFillRect(renderer, &cursor_rect);
|
|
|
}
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
|
|
|
}
|
|
|
-}
|
|
|
+}
|
|
|
+
|
|
|
+char const* LineInput::value() const
|
|
|
+{
|
|
|
+ return value_.c_str();
|
|
|
+}
|
|
|
+
|
|
|
+void LineInput::set_value(std::string value)
|
|
|
+{
|
|
|
+ value_ = std::move(value);
|
|
|
+}
|