refactor(输入系统): 移除输入模块及相关代码

重构输入处理逻辑,改为通过事件服务直接处理鼠标和滚轮事件
删除不再使用的输入模块、接口及实现类
更新场景管理器以使用新的事件处理方式
This commit is contained in:
ChestnutYueyue 2026-02-20 14:24:53 +08:00
parent c9092dce3b
commit a38bbf78c9
17 changed files with 68 additions and 981 deletions

View File

@ -9,11 +9,9 @@
namespace extra2d {
class IWindow;
class IInput;
class RenderBackend;
class WindowModule;
class RenderModule;
class InputModule;
/**
* @brief
@ -95,12 +93,6 @@ public:
*/
RenderBackend *renderer();
/**
* @brief
* @return
*/
IInput *input();
/**
* @brief
* @param scene

View File

@ -10,17 +10,13 @@
#include <extra2d/core/registry.h>
#include <extra2d/core/types.h>
// Config removed - app info now in Application class
// Platform
#include <extra2d/platform/iinput.h>
#include <extra2d/platform/input_module.h>
#include <extra2d/platform/iwindow.h>
#include <extra2d/platform/keys.h>
#include <extra2d/platform/window_module.h>
// Graphics
#include <extra2d/graphics/camera/camera.h>
#include <extra2d/graphics/camera/viewport_adapter.h>
@ -33,7 +29,6 @@
#include <extra2d/graphics/texture/texture.h>
#include <extra2d/graphics/texture/texture_pool.h>
// Scene
#include <extra2d/scene/node.h>
#include <extra2d/scene/scene.h>
@ -57,7 +52,6 @@
#include <extra2d/services/scene_service.h>
#include <extra2d/services/timer_service.h>
// Application
#include <extra2d/app/application.h>

View File

@ -1,95 +0,0 @@
#pragma once
#include <extra2d/platform/iinput.h>
#include <GLFW/glfw3.h>
#include <array>
namespace extra2d {
/**
* @brief GLFW
*/
class GLFWInput : public IInput {
public:
GLFWInput();
~GLFWInput() override;
void init() override;
void shutdown() override;
void update() override;
// Keyboard
bool down(Key key) const override;
bool pressed(Key key) const override;
bool released(Key key) const override;
// Mouse
bool down(Mouse btn) const override;
bool pressed(Mouse btn) const override;
bool released(Mouse btn) const override;
Vec2 mouse() const override;
Vec2 mouseDelta() const override;
float scroll() const override;
float scrollDelta() const override;
void setMouse(const Vec2& pos) override;
// Gamepad
bool gamepad() const override;
bool down(Gamepad btn) const override;
bool pressed(Gamepad btn) const override;
bool released(Gamepad btn) const override;
Vec2 leftStick() const override;
Vec2 rightStick() const override;
float leftTrigger() const override;
float rightTrigger() const override;
void vibrate(float left, float right) override;
// Touch
bool touching() const override;
int touchCount() const override;
Vec2 touch(int index) const override;
TouchPoint touchPoint(int index) const override;
// GLFW specific
void handleKeyEvent(int key, int scancode, int action, int mods);
void handleMouseButtonEvent(int button, int action, int mods);
void handleCursorPosEvent(double xpos, double ypos);
void handleScrollEvent(double xoffset, double yoffset);
void handleJoystickEvent(int jid, int event);
void setWindow(GLFWwindow* window) { window_ = window; }
private:
void updateGamepad();
void openGamepad();
void closeGamepad();
float applyDeadzone(float value) const;
// Keyboard state
std::array<bool, static_cast<size_t>(Key::Count)> keyCurrent_;
std::array<bool, static_cast<size_t>(Key::Count)> keyPrevious_;
// Mouse state
std::array<bool, static_cast<size_t>(Mouse::Count)> mouseCurrent_;
std::array<bool, static_cast<size_t>(Mouse::Count)> mousePrevious_;
Vec2 mousePos_;
Vec2 mouseDelta_;
float scroll_ = 0.0f;
float scrollDelta_ = 0.0f;
// Gamepad state
std::array<bool, static_cast<size_t>(Gamepad::Count)> gamepadCurrent_;
std::array<bool, static_cast<size_t>(Gamepad::Count)> gamepadPrevious_;
Vec2 leftStick_;
Vec2 rightStick_;
float leftTrigger_ = 0.0f;
float rightTrigger_ = 0.0f;
int gamepadId_ = -1;
float deadzone_ = 0.15f;
GLFWwindow* window_ = nullptr;
};
} // namespace extra2d

View File

@ -5,8 +5,6 @@
namespace extra2d {
class GLFWInput;
/**
* @brief GLFW
*/
@ -46,8 +44,6 @@ public:
void showCursor(bool show) override;
void lockCursor(bool lock) override;
IInput* input() const override;
void onResize(ResizeCb cb) override;
void onClose(CloseCb cb) override;
void onFocus(FocusCb cb) override;
@ -76,8 +72,6 @@ private:
static void joystickCallback(int jid, int event);
GLFWwindow* glfwWindow_ = nullptr;
UniquePtr<GLFWInput> input_;
int width_ = 1280;
int height_ = 720;

View File

@ -1,175 +0,0 @@
#pragma once
#include <extra2d/platform/keys.h>
#include <extra2d/core/math_types.h>
namespace extra2d {
/**
* @brief
*/
struct TouchPoint {
int id = 0;
Vec2 position;
Vec2 delta;
bool pressed = false;
bool released = false;
};
/**
* @brief
*
*/
class IInput {
public:
virtual ~IInput() = default;
/**
* @brief
*/
virtual void init() = 0;
/**
* @brief
*/
virtual void shutdown() = 0;
/**
* @brief
*/
virtual void update() = 0;
// ========== 键盘 ==========
/**
* @brief
*/
virtual bool down(Key key) const = 0;
/**
* @brief
*/
virtual bool pressed(Key key) const = 0;
/**
* @brief
*/
virtual bool released(Key key) const = 0;
// ========== 鼠标 ==========
/**
* @brief
*/
virtual bool down(Mouse btn) const = 0;
/**
* @brief
*/
virtual bool pressed(Mouse btn) const = 0;
/**
* @brief
*/
virtual bool released(Mouse btn) const = 0;
/**
* @brief
*/
virtual Vec2 mouse() const = 0;
/**
* @brief
*/
virtual Vec2 mouseDelta() const = 0;
/**
* @brief
*/
virtual float scroll() const = 0;
/**
* @brief
*/
virtual float scrollDelta() const = 0;
/**
* @brief
*/
virtual void setMouse(const Vec2& pos) = 0;
// ========== 手柄 ==========
/**
* @brief
*/
virtual bool gamepad() const = 0;
/**
* @brief
*/
virtual bool down(Gamepad btn) const = 0;
/**
* @brief
*/
virtual bool pressed(Gamepad btn) const = 0;
/**
* @brief
*/
virtual bool released(Gamepad btn) const = 0;
/**
* @brief
*/
virtual Vec2 leftStick() const = 0;
/**
* @brief
*/
virtual Vec2 rightStick() const = 0;
/**
* @brief
*/
virtual float leftTrigger() const = 0;
/**
* @brief
*/
virtual float rightTrigger() const = 0;
/**
* @brief
* @param left [0, 1]
* @param right [0, 1]
*/
virtual void vibrate(float left, float right) = 0;
// ========== 触摸 ==========
/**
* @brief
*/
virtual bool touching() const = 0;
/**
* @brief
*/
virtual int touchCount() const = 0;
/**
* @brief
* @param index
*/
virtual Vec2 touch(int index) const = 0;
/**
* @brief
* @param index
*/
virtual TouchPoint touchPoint(int index) const = 0;
};
} // namespace extra2d

View File

@ -1,78 +0,0 @@
#pragma once
#include <extra2d/core/module.h>
#include <extra2d/platform/iinput.h>
#include <extra2d/platform/window_module.h>
#include <functional>
#include <typeindex>
namespace extra2d {
/**
* @brief
*/
struct InputCfg {
float deadzone;
float mouseSensitivity;
bool enableVibration;
int maxGamepads;
int priority;
InputCfg()
: deadzone(0.15f)
, mouseSensitivity(1.0f)
, enableVibration(true)
, maxGamepads(4)
, priority(20)
{}
};
/**
* @brief
*
*/
class InputModule : public Module {
public:
/**
* @brief Lambda
* @param configFn
*/
explicit InputModule(std::function<void(InputCfg&)> configFn);
/**
* @brief
*/
~InputModule() override;
bool init() override;
void shutdown() override;
bool ok() const override { return initialized_; }
const char* name() const override { return "input"; }
int priority() const override { return cfg_.priority; }
/**
* @brief
* @return
*/
std::vector<std::type_index> deps() const override {
return {std::type_index(typeid(WindowModule))};
}
/**
* @brief
* @return
*/
IInput* input() const { return input_; }
/**
* @brief
*/
void update();
private:
InputCfg cfg_;
IInput* input_ = nullptr;
bool initialized_ = false;
};
} // namespace extra2d

View File

@ -7,8 +7,6 @@
namespace extra2d {
class IInput;
/**
* @brief
*/
@ -160,11 +158,6 @@ public:
*/
virtual void lockCursor(bool lock) = 0;
/**
* @brief
*/
virtual IInput* input() const = 0;
/**
* @brief
*/

View File

@ -153,6 +153,7 @@ private:
void doSceneSwitch();
void dispatchPointerEvents(Scene &scene);
void setupEventListeners();
std::stack<Ptr<Scene>> sceneStack_;
std::unordered_map<std::string, Ptr<Scene>> namedScenes_;
@ -170,6 +171,18 @@ private:
Node *captureTarget_ = nullptr;
Vec2 lastPointerWorld_ = Vec2::Zero();
bool hasLastPointerWorld_ = false;
// 鼠标状态(通过事件更新)
Vec2 mousePos_ = Vec2::Zero();
Vec2 mouseDelta_ = Vec2::Zero();
float scrollDelta_ = 0.0f;
bool mouseLeftPressed_ = false;
bool mouseLeftReleased_ = false;
bool mouseLeftDown_ = false;
ListenerId mouseMoveListener_ = 0;
ListenerId mousePressListener_ = 0;
ListenerId mouseReleaseListener_ = 0;
ListenerId scrollListener_ = 0;
};
} // namespace extra2d

View File

@ -3,8 +3,6 @@
#include <extra2d/graphics/core/render_backend.h>
#include <extra2d/graphics/core/render_module.h>
#include <extra2d/graphics/memory/vram_manager.h>
#include <extra2d/platform/iinput.h>
#include <extra2d/platform/input_module.h>
#include <extra2d/platform/iwindow.h>
#include <extra2d/platform/window_module.h>
#include <extra2d/services/camera_service.h>
@ -183,11 +181,6 @@ void Application::mainLoop() {
void Application::update() {
ServiceLocator::instance().updateAll(deltaTime_);
auto *inputMod = get<InputModule>();
if (inputMod) {
inputMod->update();
}
}
void Application::render() {
@ -230,11 +223,6 @@ RenderBackend *Application::renderer() {
return renderMod ? renderMod->renderer() : nullptr;
}
IInput *Application::input() {
auto *winMod = get<WindowModule>();
return (winMod && winMod->win()) ? winMod->win()->input() : nullptr;
}
void Application::enterScene(Ptr<Scene> scene) {
auto sceneService = ServiceLocator::instance().getService<ISceneService>();
auto *winMod = get<WindowModule>();

View File

@ -1,491 +0,0 @@
#include <cmath>
#include <extra2d/services/logger_service.h>
#include <glfw/glfw_input.h>
namespace extra2d {
// GLFW 按键到引擎按键的映射
static Key glfwToKey(int glfwKey) {
switch (glfwKey) {
// 字母键
case GLFW_KEY_A:
return Key::A;
case GLFW_KEY_B:
return Key::B;
case GLFW_KEY_C:
return Key::C;
case GLFW_KEY_D:
return Key::D;
case GLFW_KEY_E:
return Key::E;
case GLFW_KEY_F:
return Key::F;
case GLFW_KEY_G:
return Key::G;
case GLFW_KEY_H:
return Key::H;
case GLFW_KEY_I:
return Key::I;
case GLFW_KEY_J:
return Key::J;
case GLFW_KEY_K:
return Key::K;
case GLFW_KEY_L:
return Key::L;
case GLFW_KEY_M:
return Key::M;
case GLFW_KEY_N:
return Key::N;
case GLFW_KEY_O:
return Key::O;
case GLFW_KEY_P:
return Key::P;
case GLFW_KEY_Q:
return Key::Q;
case GLFW_KEY_R:
return Key::R;
case GLFW_KEY_S:
return Key::S;
case GLFW_KEY_T:
return Key::T;
case GLFW_KEY_U:
return Key::U;
case GLFW_KEY_V:
return Key::V;
case GLFW_KEY_W:
return Key::W;
case GLFW_KEY_X:
return Key::X;
case GLFW_KEY_Y:
return Key::Y;
case GLFW_KEY_Z:
return Key::Z;
// 数字键
case GLFW_KEY_0:
return Key::Num0;
case GLFW_KEY_1:
return Key::Num1;
case GLFW_KEY_2:
return Key::Num2;
case GLFW_KEY_3:
return Key::Num3;
case GLFW_KEY_4:
return Key::Num4;
case GLFW_KEY_5:
return Key::Num5;
case GLFW_KEY_6:
return Key::Num6;
case GLFW_KEY_7:
return Key::Num7;
case GLFW_KEY_8:
return Key::Num8;
case GLFW_KEY_9:
return Key::Num9;
// 功能键
case GLFW_KEY_F1:
return Key::F1;
case GLFW_KEY_F2:
return Key::F2;
case GLFW_KEY_F3:
return Key::F3;
case GLFW_KEY_F4:
return Key::F4;
case GLFW_KEY_F5:
return Key::F5;
case GLFW_KEY_F6:
return Key::F6;
case GLFW_KEY_F7:
return Key::F7;
case GLFW_KEY_F8:
return Key::F8;
case GLFW_KEY_F9:
return Key::F9;
case GLFW_KEY_F10:
return Key::F10;
case GLFW_KEY_F11:
return Key::F11;
case GLFW_KEY_F12:
return Key::F12;
// 特殊键
case GLFW_KEY_SPACE:
return Key::Space;
case GLFW_KEY_ENTER:
return Key::Enter;
case GLFW_KEY_ESCAPE:
return Key::Escape;
case GLFW_KEY_TAB:
return Key::Tab;
case GLFW_KEY_BACKSPACE:
return Key::Backspace;
case GLFW_KEY_INSERT:
return Key::Insert;
case GLFW_KEY_DELETE:
return Key::Delete;
case GLFW_KEY_HOME:
return Key::Home;
case GLFW_KEY_END:
return Key::End;
case GLFW_KEY_PAGE_UP:
return Key::PageUp;
case GLFW_KEY_PAGE_DOWN:
return Key::PageDown;
// 方向键
case GLFW_KEY_UP:
return Key::Up;
case GLFW_KEY_DOWN:
return Key::Down;
case GLFW_KEY_LEFT:
return Key::Left;
case GLFW_KEY_RIGHT:
return Key::Right;
// 修饰键
case GLFW_KEY_LEFT_SHIFT:
return Key::LShift;
case GLFW_KEY_RIGHT_SHIFT:
return Key::RShift;
case GLFW_KEY_LEFT_CONTROL:
return Key::LCtrl;
case GLFW_KEY_RIGHT_CONTROL:
return Key::RCtrl;
case GLFW_KEY_LEFT_ALT:
return Key::LAlt;
case GLFW_KEY_RIGHT_ALT:
return Key::RAlt;
// 锁定键
case GLFW_KEY_CAPS_LOCK:
return Key::CapsLock;
case GLFW_KEY_NUM_LOCK:
return Key::NumLock;
case GLFW_KEY_SCROLL_LOCK:
return Key::ScrollLock;
default:
return Key::None;
}
}
GLFWInput::GLFWInput() {
keyCurrent_.fill(false);
keyPrevious_.fill(false);
mouseCurrent_.fill(false);
mousePrevious_.fill(false);
gamepadCurrent_.fill(false);
gamepadPrevious_.fill(false);
}
GLFWInput::~GLFWInput() { shutdown(); }
void GLFWInput::init() {
E2D_LOG_INFO("GLFWInput 已初始化");
openGamepad();
}
void GLFWInput::shutdown() {
closeGamepad();
E2D_LOG_INFO("GLFWInput 已关闭");
}
void GLFWInput::update() {
// 保存上一帧状态
keyPrevious_ = keyCurrent_;
mousePrevious_ = mouseCurrent_;
gamepadPrevious_ = gamepadCurrent_;
// 重置增量
scrollDelta_ = 0.0f;
mouseDelta_ = Vec2{0.0f, 0.0f};
// 更新游戏手柄
updateGamepad();
// 更新键盘状态(通过轮询 GLFW
if (window_) {
for (int i = GLFW_KEY_SPACE; i <= GLFW_KEY_LAST; ++i) {
Key key = glfwToKey(i);
if (key != Key::None) {
int state = glfwGetKey(window_, i);
keyCurrent_[static_cast<size_t>(key)] = (state == GLFW_PRESS);
}
}
// 更新鼠标按钮状态
for (int i = 0; i < static_cast<int>(Mouse::Count); ++i) {
int glfwButton = GLFW_MOUSE_BUTTON_1 + i;
if (glfwButton <= GLFW_MOUSE_BUTTON_LAST) {
int state = glfwGetMouseButton(window_, glfwButton);
mouseCurrent_[i] = (state == GLFW_PRESS);
}
}
// 获取鼠标位置
double x, y;
glfwGetCursorPos(window_, &x, &y);
mousePos_ = Vec2{static_cast<float>(x), static_cast<float>(y)};
}
}
bool GLFWInput::down(Key key) const {
size_t idx = static_cast<size_t>(key);
if (idx < keyCurrent_.size()) {
return keyCurrent_[idx];
}
return false;
}
bool GLFWInput::pressed(Key key) const {
size_t idx = static_cast<size_t>(key);
if (idx < keyCurrent_.size()) {
return keyCurrent_[idx] && !keyPrevious_[idx];
}
return false;
}
bool GLFWInput::released(Key key) const {
size_t idx = static_cast<size_t>(key);
if (idx < keyCurrent_.size()) {
return !keyCurrent_[idx] && keyPrevious_[idx];
}
return false;
}
bool GLFWInput::down(Mouse btn) const {
size_t idx = static_cast<size_t>(btn);
if (idx < mouseCurrent_.size()) {
return mouseCurrent_[idx];
}
return false;
}
bool GLFWInput::pressed(Mouse btn) const {
size_t idx = static_cast<size_t>(btn);
if (idx < mouseCurrent_.size()) {
return mouseCurrent_[idx] && !mousePrevious_[idx];
}
return false;
}
bool GLFWInput::released(Mouse btn) const {
size_t idx = static_cast<size_t>(btn);
if (idx < mouseCurrent_.size()) {
return !mouseCurrent_[idx] && mousePrevious_[idx];
}
return false;
}
Vec2 GLFWInput::mouse() const { return mousePos_; }
Vec2 GLFWInput::mouseDelta() const { return mouseDelta_; }
float GLFWInput::scroll() const { return scroll_; }
float GLFWInput::scrollDelta() const { return scrollDelta_; }
void GLFWInput::setMouse(const Vec2 &pos) {
if (window_) {
glfwSetCursorPos(window_, pos.x, pos.y);
}
}
bool GLFWInput::gamepad() const { return gamepadId_ != -1; }
bool GLFWInput::down(Gamepad btn) const {
size_t idx = static_cast<size_t>(btn);
if (idx < gamepadCurrent_.size()) {
return gamepadCurrent_[idx];
}
return false;
}
bool GLFWInput::pressed(Gamepad btn) const {
size_t idx = static_cast<size_t>(btn);
if (idx < gamepadCurrent_.size()) {
return gamepadCurrent_[idx] && !gamepadPrevious_[idx];
}
return false;
}
bool GLFWInput::released(Gamepad btn) const {
size_t idx = static_cast<size_t>(btn);
if (idx < gamepadCurrent_.size()) {
return !gamepadCurrent_[idx] && gamepadPrevious_[idx];
}
return false;
}
Vec2 GLFWInput::leftStick() const { return leftStick_; }
Vec2 GLFWInput::rightStick() const { return rightStick_; }
float GLFWInput::leftTrigger() const { return leftTrigger_; }
float GLFWInput::rightTrigger() const { return rightTrigger_; }
void GLFWInput::vibrate(float left, float right) {
// GLFW 本身不支持震动,需要平台特定的代码
// 这里可以扩展为使用平台特定的 API
(void)left;
(void)right;
}
bool GLFWInput::touching() const { return false; }
int GLFWInput::touchCount() const { return 0; }
Vec2 GLFWInput::touch(int index) const {
(void)index;
return Vec2{0.0f, 0.0f};
}
TouchPoint GLFWInput::touchPoint(int index) const {
(void)index;
return TouchPoint{};
}
// 事件处理函数
void GLFWInput::handleKeyEvent(int key, int scancode, int action, int mods) {
(void)scancode;
(void)mods;
Key eKey = glfwToKey(key);
if (eKey != Key::None) {
size_t idx = static_cast<size_t>(eKey);
if (action == GLFW_PRESS) {
keyCurrent_[idx] = true;
} else if (action == GLFW_RELEASE) {
keyCurrent_[idx] = false;
}
}
}
void GLFWInput::handleMouseButtonEvent(int button, int action, int mods) {
(void)mods;
if (button >= GLFW_MOUSE_BUTTON_1 && button <= GLFW_MOUSE_BUTTON_LAST) {
size_t idx = static_cast<size_t>(button - GLFW_MOUSE_BUTTON_1);
if (idx < mouseCurrent_.size()) {
if (action == GLFW_PRESS) {
mouseCurrent_[idx] = true;
} else if (action == GLFW_RELEASE) {
mouseCurrent_[idx] = false;
}
}
}
}
void GLFWInput::handleCursorPosEvent(double xpos, double ypos) {
Vec2 newPos{static_cast<float>(xpos), static_cast<float>(ypos)};
mouseDelta_ = newPos - mousePos_;
mousePos_ = newPos;
}
void GLFWInput::handleScrollEvent(double xoffset, double yoffset) {
(void)xoffset;
scroll_ += static_cast<float>(yoffset);
scrollDelta_ += static_cast<float>(yoffset);
}
void GLFWInput::handleJoystickEvent(int jid, int event) {
if (event == GLFW_CONNECTED) {
E2D_LOG_INFO("游戏手柄已连接: {}", jid);
if (gamepadId_ == -1) {
openGamepad();
}
} else if (event == GLFW_DISCONNECTED) {
if (jid == gamepadId_) {
E2D_LOG_INFO("游戏手柄已断开: {}", jid);
closeGamepad();
}
}
}
void GLFWInput::updateGamepad() {
if (gamepadId_ == -1) {
return;
}
GLFWgamepadstate state;
if (!glfwGetGamepadState(gamepadId_, &state)) {
return;
}
// 更新按钮状态
gamepadCurrent_[static_cast<size_t>(Gamepad::A)] =
state.buttons[GLFW_GAMEPAD_BUTTON_A] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::B)] =
state.buttons[GLFW_GAMEPAD_BUTTON_B] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::X)] =
state.buttons[GLFW_GAMEPAD_BUTTON_X] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::Y)] =
state.buttons[GLFW_GAMEPAD_BUTTON_Y] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::Back)] =
state.buttons[GLFW_GAMEPAD_BUTTON_BACK] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::Start)] =
state.buttons[GLFW_GAMEPAD_BUTTON_START] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::LStick)] =
state.buttons[GLFW_GAMEPAD_BUTTON_LEFT_THUMB] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::RStick)] =
state.buttons[GLFW_GAMEPAD_BUTTON_RIGHT_THUMB] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::LB)] =
state.buttons[GLFW_GAMEPAD_BUTTON_LEFT_BUMPER] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::RB)] =
state.buttons[GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::DUp)] =
state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_UP] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::DDown)] =
state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_DOWN] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::DLeft)] =
state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_LEFT] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::DRight)] =
state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_RIGHT] == GLFW_PRESS;
gamepadCurrent_[static_cast<size_t>(Gamepad::Guide)] =
state.buttons[GLFW_GAMEPAD_BUTTON_GUIDE] == GLFW_PRESS;
// 更新摇杆值(应用死区)
leftStick_.x = applyDeadzone(state.axes[GLFW_GAMEPAD_AXIS_LEFT_X]);
leftStick_.y = applyDeadzone(state.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]);
rightStick_.x = applyDeadzone(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_X]);
rightStick_.y = applyDeadzone(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y]);
// 更新扳机值(范围 [0, 1]
leftTrigger_ = (state.axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER] + 1.0f) * 0.5f;
rightTrigger_ = (state.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER] + 1.0f) * 0.5f;
}
void GLFWInput::openGamepad() {
for (int jid = GLFW_JOYSTICK_1; jid <= GLFW_JOYSTICK_LAST; ++jid) {
if (glfwJoystickPresent(jid) && glfwJoystickIsGamepad(jid)) {
gamepadId_ = jid;
E2D_LOG_INFO("游戏手柄已打开: {}", glfwGetGamepadName(jid));
return;
}
}
}
void GLFWInput::closeGamepad() {
if (gamepadId_ != -1) {
gamepadId_ = -1;
gamepadCurrent_.fill(false);
gamepadPrevious_.fill(false);
leftStick_ = Vec2{0.0f, 0.0f};
rightStick_ = Vec2{0.0f, 0.0f};
leftTrigger_ = 0.0f;
rightTrigger_ = 0.0f;
}
}
float GLFWInput::applyDeadzone(float value) const {
if (std::abs(value) < deadzone_) {
return 0.0f;
}
float sign = value >= 0.0f ? 1.0f : -1.0f;
return sign * (std::abs(value) - deadzone_) / (1.0f - deadzone_);
}
} // namespace extra2d

View File

@ -1,4 +1,3 @@
#include <glfw/glfw_input.h>
#include <glfw/glfw_window.h>
#include <extra2d/event/event.h>
@ -92,20 +91,11 @@ bool GLFWWindow::create(const std::string &title, int width, int height,
glfwSetKeyCallback(glfwWindow_, keyCallback);
glfwSetJoystickCallback(joystickCallback);
input_ = makeUnique<GLFWInput>();
input_->setWindow(glfwWindow_);
input_->init();
E2D_LOG_INFO("GLFW 窗口创建成功: {}x{}", width_, height_);
return true;
}
void GLFWWindow::destroy() {
if (input_) {
input_->shutdown();
input_.reset();
}
if (glfwWindow_) {
glfwDestroyWindow(glfwWindow_);
glfwWindow_ = nullptr;
@ -118,10 +108,6 @@ void GLFWWindow::poll() {
if (!glfwWindow_)
return;
if (input_) {
input_->update();
}
glfwPollEvents();
}
@ -313,8 +299,6 @@ void GLFWWindow::lockCursor(bool lock) {
#endif
}
IInput *GLFWWindow::input() const { return input_.get(); }
void GLFWWindow::onResize(ResizeCb cb) { resizeCb_ = cb; }
void GLFWWindow::onClose(CloseCb cb) { closeCb_ = cb; }
@ -415,9 +399,6 @@ void GLFWWindow::cursorPosCallback(GLFWwindow *window, double xpos,
double ypos) {
GLFWWindow *self =
static_cast<GLFWWindow *>(glfwGetWindowUserPointer(window));
if (self && self->input_) {
self->input_->handleCursorPosEvent(xpos, ypos);
}
// 将事件推送到事件服务
auto eventService = ServiceLocator::instance().getService<IEventService>();
@ -433,9 +414,6 @@ void GLFWWindow::mouseButtonCallback(GLFWwindow *window, int button, int action,
int mods) {
GLFWWindow *self =
static_cast<GLFWWindow *>(glfwGetWindowUserPointer(window));
if (self && self->input_) {
self->input_->handleMouseButtonEvent(button, action, mods);
}
// 将事件推送到事件服务
auto eventService = ServiceLocator::instance().getService<IEventService>();
@ -458,9 +436,6 @@ void GLFWWindow::scrollCallback(GLFWwindow *window, double xoffset,
double yoffset) {
GLFWWindow *self =
static_cast<GLFWWindow *>(glfwGetWindowUserPointer(window));
if (self && self->input_) {
self->input_->handleScrollEvent(xoffset, yoffset);
}
// 将事件推送到事件服务
auto eventService = ServiceLocator::instance().getService<IEventService>();
@ -478,9 +453,6 @@ void GLFWWindow::keyCallback(GLFWwindow *window, int key, int scancode,
int action, int mods) {
GLFWWindow *self =
static_cast<GLFWWindow *>(glfwGetWindowUserPointer(window));
if (self && self->input_) {
self->input_->handleKeyEvent(key, scancode, action, mods);
}
// 将事件推送到事件服务
auto eventService = ServiceLocator::instance().getService<IEventService>();

View File

@ -1,53 +0,0 @@
#include <extra2d/core/registry.h>
#include <extra2d/platform/input_module.h>
#include <extra2d/platform/window_module.h>
#include <extra2d/services/event_service.h>
namespace extra2d {
InputModule::InputModule(std::function<void(InputCfg &)> configFn) {
configFn(cfg_);
}
InputModule::~InputModule() {
if (initialized_) {
shutdown();
}
}
bool InputModule::init() {
if (initialized_)
return true;
// 获取WindowModule依赖
auto *winMod = Registry::instance().get<WindowModule>();
if (!winMod || !winMod->win()) {
return false;
}
// 获取输入接口
input_ = winMod->win()->input();
if (!input_) {
return false;
}
initialized_ = true;
return true;
}
void InputModule::shutdown() {
if (!initialized_)
return;
input_ = nullptr;
initialized_ = false;
}
void InputModule::update() {
if (initialized_ && input_) {
input_->update();
}
}
} // namespace extra2d

View File

@ -2,7 +2,7 @@
#include <extra2d/app/application.h>
#include <extra2d/graphics/core/render_backend.h>
#include <extra2d/graphics/core/render_command.h>
#include <extra2d/platform/iinput.h>
#include <extra2d/platform/keys.h>
#include <extra2d/scene/scene_manager.h>
#include <extra2d/scene/transition_box_scene.h>
#include <extra2d/scene/transition_fade_scene.h>
@ -10,6 +10,7 @@
#include <extra2d/scene/transition_scale_scene.h>
#include <extra2d/scene/transition_scene.h>
#include <extra2d/scene/transition_slide_scene.h>
#include <extra2d/services/event_service.h>
#include <extra2d/services/logger_service.h>
namespace extra2d {
@ -71,9 +72,51 @@ void dispatchToNode(Node *node, Event &event) {
*/
SceneManager &SceneManager::get() {
static SceneManager instance;
static bool initialized = false;
if (!initialized) {
instance.setupEventListeners();
initialized = true;
}
return instance;
}
/**
* @brief
*/
void SceneManager::setupEventListeners() {
auto eventService = ServiceLocator::instance().getService<IEventService>();
if (!eventService) {
return;
}
mouseMoveListener_ = eventService->addListener(EventType::MouseMoved, [this](Event &e) {
auto &mouseEvent = std::get<MouseMoveEvent>(e.data);
mousePos_ = mouseEvent.position;
mouseDelta_ = mouseEvent.delta;
});
mousePressListener_ = eventService->addListener(EventType::MouseButtonPressed, [this](Event &e) {
auto &mouseEvent = std::get<MouseButtonEvent>(e.data);
if (mouseEvent.button == static_cast<int>(Mouse::Left)) {
mouseLeftPressed_ = true;
mouseLeftDown_ = true;
}
});
mouseReleaseListener_ = eventService->addListener(EventType::MouseButtonReleased, [this](Event &e) {
auto &mouseEvent = std::get<MouseButtonEvent>(e.data);
if (mouseEvent.button == static_cast<int>(Mouse::Left)) {
mouseLeftReleased_ = true;
mouseLeftDown_ = false;
}
});
scrollListener_ = eventService->addListener(EventType::MouseScrolled, [this](Event &e) {
auto &scrollEvent = std::get<MouseScrollEvent>(e.data);
scrollDelta_ = scrollEvent.offset.y;
});
}
/**
* @brief
* @param scene
@ -747,10 +790,7 @@ void SceneManager::finishTransition() {
*
*/
void SceneManager::dispatchPointerEvents(Scene &scene) {
auto *input = Application::get().input();
if (!input)
return;
Vec2 screenPos = input->mouse();
Vec2 screenPos = mousePos_;
Vec2 worldPos = screenPos;
if (auto *camera = scene.getActiveCamera()) {
@ -787,13 +827,12 @@ void SceneManager::dispatchPointerEvents(Scene &scene) {
dispatchToNode(hoverTarget_, evt);
}
float scrollDelta = input->scrollDelta();
if (hoverTarget_ && scrollDelta != 0.0f) {
Event evt = Event::createMouseScroll(Vec2(0.0f, scrollDelta), worldPos);
if (hoverTarget_ && scrollDelta_ != 0.0f) {
Event evt = Event::createMouseScroll(Vec2(0.0f, scrollDelta_), worldPos);
dispatchToNode(hoverTarget_, evt);
}
if (input->pressed(Mouse::Left)) {
if (mouseLeftPressed_) {
captureTarget_ = hoverTarget_;
if (captureTarget_) {
Event evt = Event::createMouseButtonPress(static_cast<int>(Mouse::Left),
@ -807,7 +846,7 @@ void SceneManager::dispatchPointerEvents(Scene &scene) {
}
}
if (input->released(Mouse::Left)) {
if (mouseLeftReleased_) {
Node *target = captureTarget_ ? captureTarget_ : hoverTarget_;
if (target) {
Event evt = Event::createMouseButtonRelease(static_cast<int>(Mouse::Left),
@ -831,6 +870,12 @@ void SceneManager::dispatchPointerEvents(Scene &scene) {
}
lastPointerWorld_ = worldPos;
// 重置每帧状态
mouseLeftPressed_ = false;
mouseLeftReleased_ = false;
scrollDelta_ = 0.0f;
mouseDelta_ = Vec2::Zero();
}
void SceneManager::doSceneSwitch() {}

View File

@ -105,16 +105,12 @@ int main(int argc, char *argv[]) {
cfg.w = 1280;
cfg.h = 720;
cfg.priority = 0;
cfg.backend = "glfw";
});
app.use<RenderModule>([](auto &cfg) {
cfg.priority = 10;
cfg.backend = "opengl";
});
app.use<InputModule>([](auto &cfg) { cfg.priority = 20; });
std::cout << "Initializing application..." << std::endl;
if (!app.init()) {
std::cerr << "Failed to initialize application!" << std::endl;

View File

@ -43,10 +43,8 @@ int main(int argc, char *argv[]) {
app.use<WindowModule>([](auto &cfg) {
cfg.w = 800;
cfg.h = 600;
cfg.backend = "glfw";
});
app.use<RenderModule>([](auto &cfg) {
cfg.backend = "opengl";
cfg.priority = 10;
});
app.use<HelloModule>([](auto &cfg) {

View File

@ -101,16 +101,12 @@ int main(int argc, char *argv[]) {
cfg.h = 720;
cfg.title = "Extra2D 图片显示示例";
cfg.priority = 0;
cfg.backend = "sdl2";
});
app.use<RenderModule>([](auto &cfg) {
cfg.priority = 10;
cfg.backend = "opengl";
});
app.use<InputModule>([](auto &cfg) { cfg.priority = 20; });
if (!app.init()) {
std::cerr << "Failed to initialize application!" << std::endl;
return -1;

View File

@ -116,8 +116,6 @@ int main(int argc, char *argv[]) {
app.use<RenderModule>([](auto &cfg) { cfg.priority = 10; });
app.use<InputModule>([](auto &cfg) { cfg.priority = 20; });
if (!app.init()) {
E2D_LOG_INFO("Failed to initialize application!");
return -1;