feat(事件系统): 实现完整的事件处理框架
添加事件系统核心组件,包括事件基类、各种输入事件类型和事件分发机制。主要功能: - 新增基础事件类及键盘、鼠标、触摸、手柄等输入事件 - 在Actor类中实现事件监听和分发功能 - 在Scene类中添加事件处理逻辑 - 在Application类中集成SDL事件转换和分发 - 添加测试用例验证手柄事件处理 事件系统支持冒泡和优先级排序,为游戏交互提供基础支持
This commit is contained in:
@@ -3,10 +3,12 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <frostbite2D/core/application.h>
|
||||
#include <frostbite2D/core/window.h>
|
||||
#include <frostbite2D/event/event.h>
|
||||
#include <frostbite2D/graphics/renderer.h>
|
||||
#include <frostbite2D/graphics/texture.h>
|
||||
#include <glad/glad.h>
|
||||
|
||||
|
||||
#include <frostbite2D/2d/sprite.h>
|
||||
#include <frostbite2D/scene/scene.h>
|
||||
#include <frostbite2D/scene/scene_manager.h>
|
||||
@@ -41,8 +43,47 @@ int main(int argc, char **argv) {
|
||||
|
||||
// SDL_Log("Starting main loop...");
|
||||
|
||||
// auto menuScene = MakePtr<Scene>();
|
||||
// SceneManager::get().PushScene(menuScene);
|
||||
auto menuScene = MakePtr<Scene>();
|
||||
SceneManager::get().PushScene(menuScene);
|
||||
|
||||
auto TestActor = MakePtr<Actor>();
|
||||
|
||||
menuScene->AddChild(TestActor);
|
||||
|
||||
TestActor->EnableEventReceive();
|
||||
|
||||
// 监听手柄按键按下
|
||||
TestActor->AddEventListener(
|
||||
EventType::JoystickButtonDown, [](const Event &event) {
|
||||
const auto &ke = static_cast<const JoystickButtonEvent &>(event);
|
||||
SDL_Log("Joystick Button Down: device=%d, button=%d", ke.getDeviceId(),
|
||||
ke.getButton());
|
||||
|
||||
if(ke.getButton() == 6) {
|
||||
Application::get().quit();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
// 监听手柄按键抬起
|
||||
TestActor->AddEventListener(
|
||||
EventType::JoystickButtonUp, [](const Event &event) {
|
||||
const auto &ke = static_cast<const JoystickButtonEvent &>(event);
|
||||
SDL_Log("Joystick Button Up: device=%d, button=%d", ke.getDeviceId(),
|
||||
ke.getButton());
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
// 监听手柄轴
|
||||
TestActor->AddEventListener(EventType::JoystickAxis, [](const Event &event) {
|
||||
const auto &ae = static_cast<const JoystickAxisEvent &>(event);
|
||||
if (std::abs(ae.getNormalizedValue()) > 0.1f) {
|
||||
SDL_Log("Joystick Axis: device=%d, axis=%d, value=%.2f", ae.getDeviceId(),
|
||||
ae.getAxis(), ae.getNormalizedValue());
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
// // 尝试加载精灵
|
||||
// auto sprite = Sprite::createFromFile("assets/player.png");
|
||||
|
||||
Reference in New Issue
Block a user