173 lines
4.2 KiB
C++
173 lines
4.2 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <extra2d/core/math_types.h>
|
|
#include <extra2d/core/types.h>
|
|
#include <variant>
|
|
|
|
namespace extra2d {
|
|
|
|
// ============================================================================
|
|
// 事件类型枚举
|
|
// ============================================================================
|
|
enum class EventType {
|
|
None = 0,
|
|
|
|
// 窗口事件
|
|
WindowClose,
|
|
WindowResize,
|
|
WindowFocus,
|
|
WindowLostFocus,
|
|
WindowMoved,
|
|
|
|
// 键盘事件
|
|
KeyPressed,
|
|
KeyReleased,
|
|
KeyRepeat,
|
|
|
|
// 鼠标事件
|
|
MouseButtonPressed,
|
|
MouseButtonReleased,
|
|
MouseMoved,
|
|
MouseScrolled,
|
|
|
|
// UI 事件
|
|
UIHoverEnter,
|
|
UIHoverExit,
|
|
UIPressed,
|
|
UIReleased,
|
|
UIClicked,
|
|
|
|
// 游戏手柄事件
|
|
GamepadConnected,
|
|
GamepadDisconnected,
|
|
GamepadButtonPressed,
|
|
GamepadButtonReleased,
|
|
GamepadAxisMoved,
|
|
|
|
// 触摸事件 (移动端)
|
|
TouchBegan,
|
|
TouchMoved,
|
|
TouchEnded,
|
|
TouchCancelled,
|
|
|
|
// 自定义事件
|
|
Custom
|
|
};
|
|
|
|
// ============================================================================
|
|
// 键盘事件数据
|
|
// ============================================================================
|
|
struct KeyEvent {
|
|
i32 key;
|
|
i32 scancode;
|
|
i32 mods; // 修饰键 (Shift, Ctrl, Alt, etc.)
|
|
};
|
|
|
|
// ============================================================================
|
|
// 鼠标事件数据
|
|
// ============================================================================
|
|
struct MouseButtonEvent {
|
|
i32 button;
|
|
i32 mods;
|
|
Vec2 pos;
|
|
};
|
|
|
|
struct MouseMoveEvent {
|
|
Vec2 pos;
|
|
Vec2 delta;
|
|
};
|
|
|
|
struct MouseScrollEvent {
|
|
Vec2 offset;
|
|
Vec2 pos;
|
|
};
|
|
|
|
// ============================================================================
|
|
// 窗口事件数据
|
|
// ============================================================================
|
|
struct WindowResizeEvent {
|
|
i32 w;
|
|
i32 h;
|
|
};
|
|
|
|
struct WindowMoveEvent {
|
|
i32 x;
|
|
i32 y;
|
|
};
|
|
|
|
// ============================================================================
|
|
// 游戏手柄事件数据
|
|
// ============================================================================
|
|
struct GamepadButtonEvent {
|
|
i32 gamepadId;
|
|
i32 button;
|
|
};
|
|
|
|
struct GamepadAxisEvent {
|
|
i32 gamepadId;
|
|
i32 axis;
|
|
f32 value;
|
|
};
|
|
|
|
// ============================================================================
|
|
// 触摸事件数据
|
|
// ============================================================================
|
|
struct TouchEvent {
|
|
i32 touchId;
|
|
Vec2 pos;
|
|
};
|
|
|
|
// ============================================================================
|
|
// 自定义事件数据
|
|
// ============================================================================
|
|
struct CustomEvent {
|
|
u32 id;
|
|
void *data;
|
|
};
|
|
|
|
// ============================================================================
|
|
// 事件结构
|
|
// ============================================================================
|
|
struct Event {
|
|
EventType type = EventType::None;
|
|
f64 timestamp = 0.0;
|
|
bool handled = false;
|
|
|
|
// 事件数据联合体
|
|
std::variant<std::monostate, KeyEvent, MouseButtonEvent, MouseMoveEvent,
|
|
MouseScrollEvent, WindowResizeEvent, WindowMoveEvent,
|
|
GamepadButtonEvent, GamepadAxisEvent, TouchEvent, CustomEvent>
|
|
data;
|
|
|
|
// 便捷访问方法
|
|
bool window() const {
|
|
return type == EventType::WindowClose || type == EventType::WindowResize ||
|
|
type == EventType::WindowFocus ||
|
|
type == EventType::WindowLostFocus || type == EventType::WindowMoved;
|
|
}
|
|
|
|
bool keyboard() const {
|
|
return type == EventType::KeyPressed || type == EventType::KeyReleased ||
|
|
type == EventType::KeyRepeat;
|
|
}
|
|
|
|
bool mouse() const {
|
|
return type == EventType::MouseButtonPressed ||
|
|
type == EventType::MouseButtonReleased ||
|
|
type == EventType::MouseMoved || type == EventType::MouseScrolled;
|
|
}
|
|
|
|
// 静态工厂方法
|
|
static Event windowResize(i32 w, i32 h);
|
|
static Event windowClose();
|
|
static Event keyPress(i32 key, i32 scancode, i32 mods);
|
|
static Event keyRelease(i32 key, i32 scancode, i32 mods);
|
|
static Event mousePress(i32 btn, i32 mods, Vec2 pos);
|
|
static Event mouseRelease(i32 btn, i32 mods, Vec2 pos);
|
|
static Event mouseMove(Vec2 pos, Vec2 delta);
|
|
static Event mouseScroll(Vec2 offset, Vec2 pos);
|
|
};
|
|
|
|
} // namespace extra2d
|