fix utf8 input

This commit is contained in:
Igor Stepanov 2025-10-10 14:56:34 +03:00
parent d896d2debb
commit ad5834b1e1
3 changed files with 15 additions and 15 deletions

View file

@ -9,9 +9,10 @@
#include <SDL3/SDL_scancode.h> #include <SDL3/SDL_scancode.h>
#include <SDL3/SDL_video.h> #include <SDL3/SDL_video.h>
#include <cstring> #include <string>
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "util/stringutil.hpp"
#include "window/detail/window_sdl.hpp" #include "window/detail/window_sdl.hpp"
#include "window/input.hpp" #include "window/input.hpp"
@ -132,6 +133,9 @@ void input_sdl::pollEvents() {
codepoints.clear(); codepoints.clear();
pressedKeys.clear(); pressedKeys.clear();
std::string text {};
uint size {};
bool prevPressed = false; bool prevPressed = false;
static SDL_Event event; static SDL_Event event;
@ -184,15 +188,13 @@ void input_sdl::pollEvents() {
window.setSize({event.window.data1, event.window.data2}); window.setSize({event.window.data1, event.window.data2});
break; break;
case SDL_EVENT_TEXT_INPUT: case SDL_EVENT_TEXT_INPUT:
std::vector<char> vec( text += event.text.text;
event.text.text,
event.text.text + std::strlen(event.text.text)
);
std::copy(
vec.begin(), vec.end(), std::back_inserter(codepoints)
);
} }
} }
if (!text.empty()) {
codepoints.push_back(util::decode_utf8(size, text.c_str()));
}
for (auto& [_, binding] : bindings.getAll()) { for (auto& [_, binding] : bindings.getAll()) {
if (!binding.enabled) { if (!binding.enabled) {
binding.state = false; binding.state = false;
@ -288,6 +290,6 @@ const std::vector<Keycode>& input_sdl::getPressedKeys() const {
return pressedKeys; return pressedKeys;
} }
const std::vector<char>& input_sdl::getCodepoints() const { const std::vector<std::uint32_t>& input_sdl::getCodepoints() const {
return codepoints; return codepoints;
} }

View file

@ -38,7 +38,7 @@ public:
ObserverHandler addKeyCallback(Keycode key, KeyCallback callback) override; ObserverHandler addKeyCallback(Keycode key, KeyCallback callback) override;
const std::vector<Keycode>& getPressedKeys() const override; const std::vector<Keycode>& getPressedKeys() const override;
const std::vector<char>& getCodepoints() const override; const std::vector<std::uint32_t>& getCodepoints() const override;
private: private:
glm::vec2 delta; glm::vec2 delta;
glm::vec2 cursor; glm::vec2 cursor;
@ -49,7 +49,7 @@ private:
bool cursorLocked = false; bool cursorLocked = false;
bool cursorDrag = false; bool cursorDrag = false;
Bindings bindings; Bindings bindings;
std::vector<char> codepoints; std::vector<std::uint32_t> codepoints;
std::vector<Keycode> pressedKeys; std::vector<Keycode> pressedKeys;
std::unordered_map<Keycode, util::HandlersList<>> keyCallbacks; std::unordered_map<Keycode, util::HandlersList<>> keyCallbacks;

View file

@ -6,7 +6,6 @@
#include "util/HandlersList.hpp" #include "util/HandlersList.hpp"
namespace dv { namespace dv {
class value; class value;
} }
@ -124,7 +123,6 @@ inline Mousecode mousecodes_all[] {
Mousecode::BUTTON_5, Mousecode::BUTTON_5,
}; };
namespace input_util { namespace input_util {
void initialize(); void initialize();
Keycode keycode_from(const std::string& name); Keycode keycode_from(const std::string& name);
@ -282,7 +280,7 @@ public:
) = 0; ) = 0;
virtual const std::vector<Keycode>& getPressedKeys() const = 0; virtual const std::vector<Keycode>& getPressedKeys() const = 0;
virtual const std::vector<char>& getCodepoints() const = 0; virtual const std::vector<std::uint32_t>& getCodepoints() const = 0;
ObserverHandler addCallback(const std::string& name, KeyCallback callback) { ObserverHandler addCallback(const std::string& name, KeyCallback callback) {
return getBindings().require(name).onactived.add(callback); return getBindings().require(name).onactived.add(callback);