From 05ef543615a6da3e964653a5126a2a84f41e9b9f Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Mon, 16 Feb 2026 09:45:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E8=BE=93=E5=85=A5=E7=B3=BB=E7=BB=9F):?= =?UTF-8?q?=20=E9=87=8D=E6=9E=84=E9=94=AE=E7=9B=98=E5=92=8C=E9=BC=A0?= =?UTF-8?q?=E6=A0=87=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将键盘和鼠标事件处理从SDL事件回调移动到独立的update方法 使用SDL_GetKeyboardState和SDL_GetMouseState统一处理输入状态 --- .../src/platform/backends/sdl2/sdl2_input.cpp | 119 +++++++++--------- 1 file changed, 57 insertions(+), 62 deletions(-) diff --git a/Extra2D/src/platform/backends/sdl2/sdl2_input.cpp b/Extra2D/src/platform/backends/sdl2/sdl2_input.cpp index a2f2e0d..14eceb6 100644 --- a/Extra2D/src/platform/backends/sdl2/sdl2_input.cpp +++ b/Extra2D/src/platform/backends/sdl2/sdl2_input.cpp @@ -47,6 +47,8 @@ void SDL2Input::update() { scrollDelta_ = 0.0f; mouseDelta_ = Vec2{0.0f, 0.0f}; + updateKeyboard(); + updateMouse(); updateGamepad(); } @@ -56,66 +58,6 @@ void SDL2Input::setEventCallback(EventCallback callback) { void SDL2Input::handleSDLEvent(const SDL_Event& event) { switch (event.type) { - case SDL_KEYDOWN: { - int key = event.key.keysym.scancode; - if (key >= 0 && key < static_cast(Key::Count)) { - if (!keyCurrent_[key]) { - keyCurrent_[key] = true; - - Event e = Event::createKeyPress( - event.key.keysym.sym, - event.key.keysym.scancode, - event.key.keysym.mod - ); - dispatchEvent(e); - } - } - break; - } - - case SDL_KEYUP: { - int key = event.key.keysym.scancode; - if (key >= 0 && key < static_cast(Key::Count)) { - keyCurrent_[key] = false; - - Event e = Event::createKeyRelease( - event.key.keysym.sym, - event.key.keysym.scancode, - event.key.keysym.mod - ); - dispatchEvent(e); - } - break; - } - - case SDL_MOUSEBUTTONDOWN: { - int btn = event.button.button; - int idx = btn - 1; - if (idx >= 0 && idx < static_cast(Mouse::Count)) { - mouseCurrent_[idx] = true; - - Vec2 pos{static_cast(event.button.x), - static_cast(event.button.y)}; - Event e = Event::createMousePress(btn, 0, pos); - dispatchEvent(e); - } - break; - } - - case SDL_MOUSEBUTTONUP: { - int btn = event.button.button; - int idx = btn - 1; - if (idx >= 0 && idx < static_cast(Mouse::Count)) { - mouseCurrent_[idx] = false; - - Vec2 pos{static_cast(event.button.x), - static_cast(event.button.y)}; - Event e = Event::createMouseRelease(btn, 0, pos); - dispatchEvent(e); - } - break; - } - case SDL_MOUSEMOTION: { Vec2 newPos{static_cast(event.motion.x), static_cast(event.motion.y)}; @@ -344,12 +286,65 @@ TouchPoint SDL2Input::touchPoint(int index) const { } void SDL2Input::updateKeyboard() { + int numKeys = 0; + const Uint8* state = SDL_GetKeyboardState(&numKeys); + + for (int i = 0; i < static_cast(Key::Count) && i < numKeys; ++i) { + bool currentState = state[i] != 0; + + if (currentState != keyCurrent_[i]) { + if (currentState && !keyCurrent_[i]) { + Event e = Event::createKeyPress( + SDL_GetKeyFromScancode(static_cast(i)), + i, + SDL_GetModState() + ); + dispatchEvent(e); + } else if (!currentState && keyCurrent_[i]) { + Event e = Event::createKeyRelease( + SDL_GetKeyFromScancode(static_cast(i)), + i, + SDL_GetModState() + ); + dispatchEvent(e); + } + } + + keyCurrent_[i] = currentState; + } } void SDL2Input::updateMouse() { int x = 0, y = 0; - SDL_GetMouseState(&x, &y); - mousePos_ = Vec2{static_cast(x), static_cast(y)}; + Uint32 state = SDL_GetMouseState(&x, &y); + + Vec2 newPos{static_cast(x), static_cast(y)}; + mouseDelta_ = newPos - mousePos_; + mousePos_ = newPos; + + static const int mouseButtons[] = { + SDL_BUTTON_LEFT, + SDL_BUTTON_MIDDLE, + SDL_BUTTON_RIGHT, + SDL_BUTTON_X1, + SDL_BUTTON_X2 + }; + + for (int i = 0; i < static_cast(Mouse::Count); ++i) { + bool currentState = (state & SDL_BUTTON(mouseButtons[i])) != 0; + + if (currentState != mouseCurrent_[i]) { + if (currentState && !mouseCurrent_[i]) { + Event e = Event::createMousePress(mouseButtons[i], 0, mousePos_); + dispatchEvent(e); + } else if (!currentState && mouseCurrent_[i]) { + Event e = Event::createMouseRelease(mouseButtons[i], 0, mousePos_); + dispatchEvent(e); + } + } + + mouseCurrent_[i] = currentState; + } } void SDL2Input::updateGamepad() {