Add text input events in SDL2 backend

This commit is contained in:
Nikita Lisitsa 2025-02-24 13:02:48 +03:00
parent 12eed4dda5
commit 195a31fa1c
5 changed files with 13 additions and 0 deletions

View file

@ -37,6 +37,7 @@ namespace psemek::app
std::function<void(bool)> show_cursor;
std::function<void(bool)> relative_mouse_mode;
std::function<void(bool)> windowed;
std::function<void(bool)> text_input;
#if defined(PSEMEK_GRAPHICS_API_OPENGL)
std::function<void(bool)> vsync;
#endif

View file

@ -16,6 +16,7 @@ namespace psemek::app
virtual void on_event(touch_up_event const &) {}
virtual void on_event(touch_move_event const &) {}
virtual void on_event(key_event const &) {}
virtual void on_event(text_input_event const &) {}
virtual ~event_handler() {}
};

View file

@ -2,6 +2,8 @@
#include <psemek/math/point.hpp>
#include <string>
namespace psemek::app
{
@ -172,4 +174,9 @@ namespace psemek::app
bool down;
};
struct text_input_event
{
std::string input;
};
}

View file

@ -192,6 +192,9 @@ namespace psemek::sdl2
if (auto key = keycode(e.key.keysym))
handler.on_event(app::key_event{*key, false});
break;
case SDL_TEXTINPUT:
handler.on_event(app::text_input_event{e.text.text});
break;
}
return false;

View file

@ -48,6 +48,7 @@ int main(int argc, char ** argv) try
context.show_cursor = [&](bool show){ window.show_cursor(show); };
context.relative_mouse_mode = [&](bool mode){ window.relative_mouse_mode(mode); };
context.windowed = [&](bool on){ window.windowed(on); };
context.text_input = [](bool on){ if (on) SDL_StartTextInput(); else SDL_StopTextInput(); };
#if defined(PSEMEK_GRAPHICS_API_OPENGL)
context.vsync = [&](bool on){ window.vsync(on); };