refactor(输入系统): 重构键盘和鼠标事件处理逻辑
将键盘和鼠标事件处理从SDL事件回调移动到独立的update方法 使用SDL_GetKeyboardState和SDL_GetMouseState统一处理输入状态
This commit is contained in:
parent
8fc3b794d2
commit
05ef543615
|
|
@ -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<int>(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<int>(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<int>(Mouse::Count)) {
|
||||
mouseCurrent_[idx] = true;
|
||||
|
||||
Vec2 pos{static_cast<float>(event.button.x),
|
||||
static_cast<float>(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<int>(Mouse::Count)) {
|
||||
mouseCurrent_[idx] = false;
|
||||
|
||||
Vec2 pos{static_cast<float>(event.button.x),
|
||||
static_cast<float>(event.button.y)};
|
||||
Event e = Event::createMouseRelease(btn, 0, pos);
|
||||
dispatchEvent(e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_MOUSEMOTION: {
|
||||
Vec2 newPos{static_cast<float>(event.motion.x),
|
||||
static_cast<float>(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<int>(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<SDL_Scancode>(i)),
|
||||
i,
|
||||
SDL_GetModState()
|
||||
);
|
||||
dispatchEvent(e);
|
||||
} else if (!currentState && keyCurrent_[i]) {
|
||||
Event e = Event::createKeyRelease(
|
||||
SDL_GetKeyFromScancode(static_cast<SDL_Scancode>(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<float>(x), static_cast<float>(y)};
|
||||
Uint32 state = SDL_GetMouseState(&x, &y);
|
||||
|
||||
Vec2 newPos{static_cast<float>(x), static_cast<float>(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<int>(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() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue