Skip to content

Commit

Permalink
make textinput functional
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHX committed Jan 19, 2024
1 parent bdf1bb7 commit 6427efe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/window_sdl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ void SDL3GameWindow::pollEvents() {
break;
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
if(SDL_TextInputActive() && ev.type == SDL_EVENT_KEY_DOWN) {
if(ev.key.keysym.sym == SDLK_BACKSPACE) {
onKeyboardText("\b");
} else if(ev.key.keysym.sym == SDLK_DELETE) {
onKeyboardText("\x7F");
} else if(ev.key.keysym.sym == SDLK_RETURN) {
onKeyboardText("\n");
}
}
onKeyboard(getKeyMinecraft(ev.key.keysym.sym), ev.type == SDL_EVENT_KEY_DOWN ? ev.key.repeat ? KeyAction::REPEAT : KeyAction::PRESS : KeyAction::RELEASE );
break;
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
Expand All @@ -184,6 +193,9 @@ void SDL3GameWindow::pollEvents() {
height = ev.window.data2;
onWindowSizeChanged(ev.window.data1, ev.window.data2);
break;
case SDL_EVENT_TEXT_INPUT:
onKeyboardText(ev.text.text ? ev.text.text : "");
break;
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
onClose();
default:
Expand Down Expand Up @@ -212,6 +224,14 @@ void SDL3GameWindow::setSwapInterval(int interval) {
SDL_GL_SetSwapInterval(interval);
}

void SDL3GameWindow::startTextInput() {
SDL_StartTextInput();
}

void SDL3GameWindow::stopTextInput() {
SDL_StopTextInput();
}

KeyCode SDL3GameWindow::getKeyMinecraft(int keyCode) {
if (keyCode >= SDLK_F1 && keyCode <= SDLK_F12)
return (KeyCode) (keyCode - SDLK_F1 + (int) KeyCode::FN1);
Expand Down
4 changes: 4 additions & 0 deletions src/window_sdl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ class SDL3GameWindow : public GameWindow {

void setSwapInterval(int interval) override;

void startTextInput() override;

void stopTextInput() override;

};

0 comments on commit 6427efe

Please sign in to comment.