refactor(输入处理): 将SDL控制器按钮替换为引擎定义的GamepadButton枚举

统一使用引擎定义的GamepadButton枚举来处理游戏手柄输入,提高代码可维护性并减少对SDL的直接依赖
This commit is contained in:
ChestnutYueyue 2026-02-11 15:47:25 +08:00
parent 1f10ce999c
commit c6a5557d89
2 changed files with 9 additions and 9 deletions

View File

@ -118,20 +118,20 @@ void PlayScene::onUpdate(float dt) {
auto& input = app.input(); auto& input = app.input();
// B 键返回主菜单 // B 键返回主菜单
if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_B)) { if (input.isButtonPressed(extra2d::GamepadButton::B)) {
app.scenes().replaceScene( app.scenes().replaceScene(
extra2d::makePtr<StartScene>(), extra2d::TransitionType::Fade, 0.2f); extra2d::makePtr<StartScene>(), extra2d::TransitionType::Fade, 0.2f);
return; return;
} }
// Y 键重开 // Y 键重开
if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_Y)) { if (input.isButtonPressed(extra2d::GamepadButton::Y)) {
setLevel(g_CurrentLevel); setLevel(g_CurrentLevel);
return; return;
} }
// X键直接切换音效 // X键直接切换音效
if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_X)) { if (input.isButtonPressed(extra2d::GamepadButton::X)) {
g_SoundOpen = !g_SoundOpen; g_SoundOpen = !g_SoundOpen;
AudioManager::instance().setEnabled(g_SoundOpen); AudioManager::instance().setEnabled(g_SoundOpen);
updateSoundIcon(); updateSoundIcon();
@ -139,22 +139,22 @@ void PlayScene::onUpdate(float dt) {
} }
// A 键执行选中的菜单项 // A 键执行选中的菜单项
if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_A)) { if (input.isButtonPressed(extra2d::GamepadButton::A)) {
executeMenuItem(); executeMenuItem();
return; return;
} }
// 方向键移动 // 方向键移动
if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_DPAD_UP)) { if (input.isButtonPressed(extra2d::GamepadButton::DPadUp)) {
move(0, -1, 1); move(0, -1, 1);
flush(); flush();
} else if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_DPAD_DOWN)) { } else if (input.isButtonPressed(extra2d::GamepadButton::DPadDown)) {
move(0, 1, 2); move(0, 1, 2);
flush(); flush();
} else if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_DPAD_LEFT)) { } else if (input.isButtonPressed(extra2d::GamepadButton::DPadLeft)) {
move(-1, 0, 3); move(-1, 0, 3);
flush(); flush();
} else if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) { } else if (input.isButtonPressed(extra2d::GamepadButton::DPadRight)) {
move(1, 0, 4); move(1, 0, 4);
flush(); flush();
} else { } else {

View File

@ -67,7 +67,7 @@ void SuccessScene::onUpdate(float dt) {
auto& input = app.input(); auto& input = app.input();
// A键确认返回主菜单 // A键确认返回主菜单
if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_A)) { if (input.isButtonPressed(extra2d::GamepadButton::A)) {
auto& scenes = extra2d::Application::instance().scenes(); auto& scenes = extra2d::Application::instance().scenes();
scenes.popScene(extra2d::TransitionType::Fade, 0.2f); scenes.popScene(extra2d::TransitionType::Fade, 0.2f);
scenes.popScene(extra2d::TransitionType::Fade, 0.2f); scenes.popScene(extra2d::TransitionType::Fade, 0.2f);