From c6a5557d89046595164f2a2e6ddbbbe47a8dd8af Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Wed, 11 Feb 2026 15:47:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E8=BE=93=E5=85=A5=E5=A4=84=E7=90=86):?= =?UTF-8?q?=20=E5=B0=86SDL=E6=8E=A7=E5=88=B6=E5=99=A8=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E4=B8=BA=E5=BC=95=E6=93=8E=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E7=9A=84GamepadButton=E6=9E=9A=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 统一使用引擎定义的GamepadButton枚举来处理游戏手柄输入,提高代码可维护性并减少对SDL的直接依赖 --- examples/push_box/PlayScene.cpp | 16 ++++++++-------- examples/push_box/SuccessScene.cpp | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/push_box/PlayScene.cpp b/examples/push_box/PlayScene.cpp index 098999d..fe84c96 100644 --- a/examples/push_box/PlayScene.cpp +++ b/examples/push_box/PlayScene.cpp @@ -118,20 +118,20 @@ void PlayScene::onUpdate(float dt) { auto& input = app.input(); // B 键返回主菜单 - if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_B)) { + if (input.isButtonPressed(extra2d::GamepadButton::B)) { app.scenes().replaceScene( extra2d::makePtr(), extra2d::TransitionType::Fade, 0.2f); return; } // Y 键重开 - if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_Y)) { + if (input.isButtonPressed(extra2d::GamepadButton::Y)) { setLevel(g_CurrentLevel); return; } // X键直接切换音效 - if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_X)) { + if (input.isButtonPressed(extra2d::GamepadButton::X)) { g_SoundOpen = !g_SoundOpen; AudioManager::instance().setEnabled(g_SoundOpen); updateSoundIcon(); @@ -139,22 +139,22 @@ void PlayScene::onUpdate(float dt) { } // A 键执行选中的菜单项 - if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_A)) { + if (input.isButtonPressed(extra2d::GamepadButton::A)) { executeMenuItem(); return; } // 方向键移动 - if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_DPAD_UP)) { + if (input.isButtonPressed(extra2d::GamepadButton::DPadUp)) { move(0, -1, 1); flush(); - } else if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_DPAD_DOWN)) { + } else if (input.isButtonPressed(extra2d::GamepadButton::DPadDown)) { move(0, 1, 2); flush(); - } else if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_DPAD_LEFT)) { + } else if (input.isButtonPressed(extra2d::GamepadButton::DPadLeft)) { move(-1, 0, 3); flush(); - } else if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) { + } else if (input.isButtonPressed(extra2d::GamepadButton::DPadRight)) { move(1, 0, 4); flush(); } else { diff --git a/examples/push_box/SuccessScene.cpp b/examples/push_box/SuccessScene.cpp index be2c1ab..2cb2cb3 100644 --- a/examples/push_box/SuccessScene.cpp +++ b/examples/push_box/SuccessScene.cpp @@ -67,7 +67,7 @@ void SuccessScene::onUpdate(float dt) { auto& input = app.input(); // A键确认返回主菜单 - if (input.isButtonPressed(SDL_CONTROLLER_BUTTON_A)) { + if (input.isButtonPressed(extra2d::GamepadButton::A)) { auto& scenes = extra2d::Application::instance().scenes(); scenes.popScene(extra2d::TransitionType::Fade, 0.2f); scenes.popScene(extra2d::TransitionType::Fade, 0.2f);