feat(engine-core): 添加统一输入系统 (#282)
* perf(core): 优化 EntitySystem 迭代性能,添加 CommandBuffer 延迟命令
ReactiveQuery 快照优化:
- 添加快照机制,避免每帧拷贝数组
- 只在实体列表变化时创建新快照
- 静态场景下多个系统共享同一快照
CommandBuffer 延迟命令系统:
- 支持延迟添加/移除组件、销毁实体、设置实体激活状态
- 每个系统拥有独立的 commands 属性
- 命令在帧末统一执行,避免迭代过程中修改实体列表
Scene 更新:
- 在 lateUpdate 后自动刷新所有系统的命令缓冲区
文档:
- 更新系统文档,添加 CommandBuffer 使用说明
* fix(ci): upgrade first-interaction action to v1.3.0
Fix Docker build failure in welcome workflow.
* fix(ci): upgrade pnpm/action-setup to v4 and fix unused import
- Upgrade pnpm/action-setup@v2 to v4 in all workflow files
- Remove unused CommandType import in CommandBuffer.test.ts
* fix(ci): remove duplicate pnpm version specification
* feat(engine-core): 添加统一输入系统
添加完整的输入系统,支持平台抽象:
- IPlatformInputSubsystem: 扩展接口支持键盘/鼠标/滚轮事件
- WebInputSubsystem: 浏览器实现,支持事件绑定/解绑
- InputManager: 全局输入状态管理器(键盘、鼠标、触摸)
- InputSystem: ECS 系统,连接平台事件到 InputManager
- GameRuntime 集成: 自动创建 InputSystem 并绑定平台子系统
使用方式:
```typescript
import { Input, MouseButton } from '@esengine/engine-core';
if (Input.isKeyDown('KeyW')) { /* 移动 */ }
if (Input.isKeyJustPressed('Space')) { /* 跳跃 */ }
if (Input.isMouseButtonDown(MouseButton.Left)) { /* 射击 */ }
```
* fix(runtime-core): 添加缺失的 platform-common 依赖
* fix(runtime-core): 移除 platform-web 依赖避免循环依赖
* fix(runtime-core): 使用工厂函数注入 InputSubsystem 避免循环依赖
- BrowserPlatformAdapter 通过 inputSubsystemFactory 配置接收输入子系统
- 在 IPlatformInputSubsystem 接口添加可选的 dispose 方法
- 移除对 @esengine/platform-web 的直接依赖
This commit is contained in:
541
packages/engine-core/src/Input/InputManager.ts
Normal file
541
packages/engine-core/src/Input/InputManager.ts
Normal file
@@ -0,0 +1,541 @@
|
||||
/**
|
||||
* 输入管理器 - 统一管理所有输入状态
|
||||
* Input Manager - Unified input state management
|
||||
*
|
||||
* 提供简单易用的 API 供游戏代码查询输入状态。
|
||||
* Provides simple API for game code to query input state.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 在游戏系统中使用 | Use in game system
|
||||
* class PlayerMovementSystem extends EntitySystem {
|
||||
* protected process(entities: readonly Entity[]): void {
|
||||
* const input = Input;
|
||||
*
|
||||
* for (const entity of entities) {
|
||||
* const transform = entity.getComponent(TransformComponent);
|
||||
*
|
||||
* // 键盘移动 | Keyboard movement
|
||||
* if (input.isKeyDown('KeyW') || input.isKeyDown('ArrowUp')) {
|
||||
* transform.position.y -= 5;
|
||||
* }
|
||||
* if (input.isKeyDown('KeyS') || input.isKeyDown('ArrowDown')) {
|
||||
* transform.position.y += 5;
|
||||
* }
|
||||
*
|
||||
* // 鼠标点击 | Mouse click
|
||||
* if (input.isMouseButtonJustPressed(MouseButton.Left)) {
|
||||
* console.log('Clicked at:', input.mousePosition);
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
|
||||
import { MouseButton } from '@esengine/platform-common';
|
||||
import type { KeyboardEventInfo, MouseEventInfo, WheelEventInfo, TouchInfo } from '@esengine/platform-common';
|
||||
|
||||
/**
|
||||
* 按键状态
|
||||
* Key state
|
||||
*/
|
||||
export interface KeyState {
|
||||
/** 是否按下 | Is pressed */
|
||||
pressed: boolean;
|
||||
/** 本帧刚按下 | Just pressed this frame */
|
||||
justPressed: boolean;
|
||||
/** 本帧刚释放 | Just released this frame */
|
||||
justReleased: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 鼠标按钮状态
|
||||
* Mouse button state
|
||||
*/
|
||||
export interface MouseButtonState {
|
||||
/** 是否按下 | Is pressed */
|
||||
pressed: boolean;
|
||||
/** 本帧刚按下 | Just pressed this frame */
|
||||
justPressed: boolean;
|
||||
/** 本帧刚释放 | Just released this frame */
|
||||
justReleased: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 二维向量
|
||||
* 2D Vector
|
||||
*/
|
||||
export interface Vector2 {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输入管理器类
|
||||
* Input Manager class
|
||||
*/
|
||||
export class InputManager {
|
||||
// ========== 键盘状态 | Keyboard state ==========
|
||||
private _keyStates: Map<string, KeyState> = new Map();
|
||||
private _keysJustPressed: Set<string> = new Set();
|
||||
private _keysJustReleased: Set<string> = new Set();
|
||||
|
||||
// ========== 鼠标状态 | Mouse state ==========
|
||||
private _mousePosition: Vector2 = { x: 0, y: 0 };
|
||||
private _mouseMovement: Vector2 = { x: 0, y: 0 };
|
||||
private _mouseButtonStates: Map<MouseButton, MouseButtonState> = new Map();
|
||||
private _mouseButtonsJustPressed: Set<MouseButton> = new Set();
|
||||
private _mouseButtonsJustReleased: Set<MouseButton> = new Set();
|
||||
private _scrollDelta: Vector2 = { x: 0, y: 0 };
|
||||
|
||||
// ========== 触摸状态 | Touch state ==========
|
||||
private _touches: Map<number, TouchInfo> = new Map();
|
||||
private _touchesJustStarted: Set<number> = new Set();
|
||||
private _touchesJustEnded: Set<number> = new Set();
|
||||
|
||||
// ========== 修饰键状态 | Modifier key state ==========
|
||||
private _altKey: boolean = false;
|
||||
private _ctrlKey: boolean = false;
|
||||
private _shiftKey: boolean = false;
|
||||
private _metaKey: boolean = false;
|
||||
|
||||
constructor() {
|
||||
// 初始化鼠标按钮状态 | Initialize mouse button states
|
||||
this._mouseButtonStates.set(MouseButton.Left, { pressed: false, justPressed: false, justReleased: false });
|
||||
this._mouseButtonStates.set(MouseButton.Middle, { pressed: false, justPressed: false, justReleased: false });
|
||||
this._mouseButtonStates.set(MouseButton.Right, { pressed: false, justPressed: false, justReleased: false });
|
||||
}
|
||||
|
||||
// ========== 键盘 API | Keyboard API ==========
|
||||
|
||||
/**
|
||||
* 检查按键是否按下
|
||||
* Check if a key is pressed
|
||||
*
|
||||
* @param code 按键代码 (如 'KeyW', 'Space', 'ArrowUp') | Key code
|
||||
*/
|
||||
isKeyDown(code: string): boolean {
|
||||
return this._keyStates.get(code)?.pressed ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查按键是否本帧刚按下
|
||||
* Check if a key was just pressed this frame
|
||||
*
|
||||
* @param code 按键代码 | Key code
|
||||
*/
|
||||
isKeyJustPressed(code: string): boolean {
|
||||
return this._keyStates.get(code)?.justPressed ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查按键是否本帧刚释放
|
||||
* Check if a key was just released this frame
|
||||
*
|
||||
* @param code 按键代码 | Key code
|
||||
*/
|
||||
isKeyJustReleased(code: string): boolean {
|
||||
return this._keyStates.get(code)?.justReleased ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有按下的按键
|
||||
* Get all pressed keys
|
||||
*/
|
||||
getPressedKeys(): string[] {
|
||||
const result: string[] = [];
|
||||
this._keyStates.forEach((state, code) => {
|
||||
if (state.pressed) {
|
||||
result.push(code);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
// ========== 鼠标 API | Mouse API ==========
|
||||
|
||||
/**
|
||||
* 获取鼠标位置(屏幕坐标)
|
||||
* Get mouse position (screen coordinates)
|
||||
*/
|
||||
get mousePosition(): Readonly<Vector2> {
|
||||
return this._mousePosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取鼠标本帧移动量
|
||||
* Get mouse movement this frame
|
||||
*/
|
||||
get mouseMovement(): Readonly<Vector2> {
|
||||
return this._mouseMovement;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取滚轮滚动量
|
||||
* Get scroll delta
|
||||
*/
|
||||
get scrollDelta(): Readonly<Vector2> {
|
||||
return this._scrollDelta;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查鼠标按钮是否按下
|
||||
* Check if a mouse button is pressed
|
||||
*
|
||||
* @param button 鼠标按钮 | Mouse button
|
||||
*/
|
||||
isMouseButtonDown(button: MouseButton): boolean {
|
||||
return this._mouseButtonStates.get(button)?.pressed ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查鼠标按钮是否本帧刚按下
|
||||
* Check if a mouse button was just pressed this frame
|
||||
*
|
||||
* @param button 鼠标按钮 | Mouse button
|
||||
*/
|
||||
isMouseButtonJustPressed(button: MouseButton): boolean {
|
||||
return this._mouseButtonStates.get(button)?.justPressed ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查鼠标按钮是否本帧刚释放
|
||||
* Check if a mouse button was just released this frame
|
||||
*
|
||||
* @param button 鼠标按钮 | Mouse button
|
||||
*/
|
||||
isMouseButtonJustReleased(button: MouseButton): boolean {
|
||||
return this._mouseButtonStates.get(button)?.justReleased ?? false;
|
||||
}
|
||||
|
||||
// ========== 触摸 API | Touch API ==========
|
||||
|
||||
/**
|
||||
* 获取所有触摸点
|
||||
* Get all touch points
|
||||
*/
|
||||
get touches(): ReadonlyMap<number, TouchInfo> {
|
||||
return this._touches;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取触摸点数量
|
||||
* Get touch count
|
||||
*/
|
||||
get touchCount(): number {
|
||||
return this._touches.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定触摸点
|
||||
* Get a specific touch point
|
||||
*
|
||||
* @param identifier 触摸点标识符 | Touch identifier
|
||||
*/
|
||||
getTouch(identifier: number): TouchInfo | undefined {
|
||||
return this._touches.get(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有触摸
|
||||
* Check if there are any touches
|
||||
*/
|
||||
get isTouching(): boolean {
|
||||
return this._touches.size > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查触摸点是否本帧刚开始
|
||||
* Check if a touch just started this frame
|
||||
*
|
||||
* @param identifier 触摸点标识符 | Touch identifier
|
||||
*/
|
||||
isTouchJustStarted(identifier: number): boolean {
|
||||
return this._touchesJustStarted.has(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查触摸点是否本帧刚结束
|
||||
* Check if a touch just ended this frame
|
||||
*
|
||||
* @param identifier 触摸点标识符 | Touch identifier
|
||||
*/
|
||||
isTouchJustEnded(identifier: number): boolean {
|
||||
return this._touchesJustEnded.has(identifier);
|
||||
}
|
||||
|
||||
// ========== 修饰键 API | Modifier keys API ==========
|
||||
|
||||
/** Alt 键是否按下 | Is Alt key pressed */
|
||||
get altKey(): boolean { return this._altKey; }
|
||||
|
||||
/** Ctrl 键是否按下 | Is Ctrl key pressed */
|
||||
get ctrlKey(): boolean { return this._ctrlKey; }
|
||||
|
||||
/** Shift 键是否按下 | Is Shift key pressed */
|
||||
get shiftKey(): boolean { return this._shiftKey; }
|
||||
|
||||
/** Meta 键是否按下 (Windows/Command) | Is Meta key pressed */
|
||||
get metaKey(): boolean { return this._metaKey; }
|
||||
|
||||
// ========== 内部更新方法 | Internal update methods ==========
|
||||
|
||||
/**
|
||||
* 处理键盘按下事件
|
||||
* Handle key down event
|
||||
* @internal
|
||||
*/
|
||||
handleKeyDown(event: KeyboardEventInfo): void {
|
||||
let state = this._keyStates.get(event.code);
|
||||
if (!state) {
|
||||
state = { pressed: false, justPressed: false, justReleased: false };
|
||||
this._keyStates.set(event.code, state);
|
||||
}
|
||||
|
||||
if (!state.pressed && !event.repeat) {
|
||||
state.justPressed = true;
|
||||
this._keysJustPressed.add(event.code);
|
||||
}
|
||||
state.pressed = true;
|
||||
|
||||
// 更新修饰键 | Update modifier keys
|
||||
this._altKey = event.altKey;
|
||||
this._ctrlKey = event.ctrlKey;
|
||||
this._shiftKey = event.shiftKey;
|
||||
this._metaKey = event.metaKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理键盘释放事件
|
||||
* Handle key up event
|
||||
* @internal
|
||||
*/
|
||||
handleKeyUp(event: KeyboardEventInfo): void {
|
||||
let state = this._keyStates.get(event.code);
|
||||
if (!state) {
|
||||
state = { pressed: false, justPressed: false, justReleased: false };
|
||||
this._keyStates.set(event.code, state);
|
||||
}
|
||||
|
||||
if (state.pressed) {
|
||||
state.justReleased = true;
|
||||
this._keysJustReleased.add(event.code);
|
||||
}
|
||||
state.pressed = false;
|
||||
|
||||
// 更新修饰键 | Update modifier keys
|
||||
this._altKey = event.altKey;
|
||||
this._ctrlKey = event.ctrlKey;
|
||||
this._shiftKey = event.shiftKey;
|
||||
this._metaKey = event.metaKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理鼠标移动事件
|
||||
* Handle mouse move event
|
||||
* @internal
|
||||
*/
|
||||
handleMouseMove(event: MouseEventInfo): void {
|
||||
this._mousePosition.x = event.x;
|
||||
this._mousePosition.y = event.y;
|
||||
this._mouseMovement.x += event.movementX;
|
||||
this._mouseMovement.y += event.movementY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理鼠标按下事件
|
||||
* Handle mouse down event
|
||||
* @internal
|
||||
*/
|
||||
handleMouseDown(event: MouseEventInfo): void {
|
||||
const button = event.button as MouseButton;
|
||||
let state = this._mouseButtonStates.get(button);
|
||||
if (!state) {
|
||||
state = { pressed: false, justPressed: false, justReleased: false };
|
||||
this._mouseButtonStates.set(button, state);
|
||||
}
|
||||
|
||||
if (!state.pressed) {
|
||||
state.justPressed = true;
|
||||
this._mouseButtonsJustPressed.add(button);
|
||||
}
|
||||
state.pressed = true;
|
||||
|
||||
this._mousePosition.x = event.x;
|
||||
this._mousePosition.y = event.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理鼠标释放事件
|
||||
* Handle mouse up event
|
||||
* @internal
|
||||
*/
|
||||
handleMouseUp(event: MouseEventInfo): void {
|
||||
const button = event.button as MouseButton;
|
||||
let state = this._mouseButtonStates.get(button);
|
||||
if (!state) {
|
||||
state = { pressed: false, justPressed: false, justReleased: false };
|
||||
this._mouseButtonStates.set(button, state);
|
||||
}
|
||||
|
||||
if (state.pressed) {
|
||||
state.justReleased = true;
|
||||
this._mouseButtonsJustReleased.add(button);
|
||||
}
|
||||
state.pressed = false;
|
||||
|
||||
this._mousePosition.x = event.x;
|
||||
this._mousePosition.y = event.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理鼠标滚轮事件
|
||||
* Handle mouse wheel event
|
||||
* @internal
|
||||
*/
|
||||
handleWheel(event: WheelEventInfo): void {
|
||||
this._scrollDelta.x += event.deltaX;
|
||||
this._scrollDelta.y += event.deltaY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理触摸开始事件
|
||||
* Handle touch start event
|
||||
* @internal
|
||||
*/
|
||||
handleTouchStart(touches: TouchInfo[]): void {
|
||||
for (const touch of touches) {
|
||||
this._touches.set(touch.identifier, touch);
|
||||
this._touchesJustStarted.add(touch.identifier);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理触摸移动事件
|
||||
* Handle touch move event
|
||||
* @internal
|
||||
*/
|
||||
handleTouchMove(touches: TouchInfo[]): void {
|
||||
for (const touch of touches) {
|
||||
this._touches.set(touch.identifier, touch);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理触摸结束事件
|
||||
* Handle touch end event
|
||||
* @internal
|
||||
*/
|
||||
handleTouchEnd(touches: TouchInfo[]): void {
|
||||
for (const touch of touches) {
|
||||
this._touches.delete(touch.identifier);
|
||||
this._touchesJustEnded.add(touch.identifier);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 帧末清理临时状态
|
||||
* Clear temporary state at end of frame
|
||||
* @internal
|
||||
*/
|
||||
endFrame(): void {
|
||||
// 清理键盘帧状态 | Clear keyboard frame state
|
||||
for (const code of this._keysJustPressed) {
|
||||
const state = this._keyStates.get(code);
|
||||
if (state) {
|
||||
state.justPressed = false;
|
||||
}
|
||||
}
|
||||
this._keysJustPressed.clear();
|
||||
|
||||
for (const code of this._keysJustReleased) {
|
||||
const state = this._keyStates.get(code);
|
||||
if (state) {
|
||||
state.justReleased = false;
|
||||
}
|
||||
}
|
||||
this._keysJustReleased.clear();
|
||||
|
||||
// 清理鼠标帧状态 | Clear mouse frame state
|
||||
for (const button of this._mouseButtonsJustPressed) {
|
||||
const state = this._mouseButtonStates.get(button);
|
||||
if (state) {
|
||||
state.justPressed = false;
|
||||
}
|
||||
}
|
||||
this._mouseButtonsJustPressed.clear();
|
||||
|
||||
for (const button of this._mouseButtonsJustReleased) {
|
||||
const state = this._mouseButtonStates.get(button);
|
||||
if (state) {
|
||||
state.justReleased = false;
|
||||
}
|
||||
}
|
||||
this._mouseButtonsJustReleased.clear();
|
||||
|
||||
// 清理鼠标移动和滚动 | Clear mouse movement and scroll
|
||||
this._mouseMovement.x = 0;
|
||||
this._mouseMovement.y = 0;
|
||||
this._scrollDelta.x = 0;
|
||||
this._scrollDelta.y = 0;
|
||||
|
||||
// 清理触摸帧状态 | Clear touch frame state
|
||||
this._touchesJustStarted.clear();
|
||||
this._touchesJustEnded.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置所有输入状态
|
||||
* Reset all input state
|
||||
*/
|
||||
reset(): void {
|
||||
this._keyStates.clear();
|
||||
this._keysJustPressed.clear();
|
||||
this._keysJustReleased.clear();
|
||||
|
||||
this._mousePosition = { x: 0, y: 0 };
|
||||
this._mouseMovement = { x: 0, y: 0 };
|
||||
this._scrollDelta = { x: 0, y: 0 };
|
||||
this._mouseButtonStates.forEach(state => {
|
||||
state.pressed = false;
|
||||
state.justPressed = false;
|
||||
state.justReleased = false;
|
||||
});
|
||||
this._mouseButtonsJustPressed.clear();
|
||||
this._mouseButtonsJustReleased.clear();
|
||||
|
||||
this._touches.clear();
|
||||
this._touchesJustStarted.clear();
|
||||
this._touchesJustEnded.clear();
|
||||
|
||||
this._altKey = false;
|
||||
this._ctrlKey = false;
|
||||
this._shiftKey = false;
|
||||
this._metaKey = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局输入管理器实例
|
||||
* Global input manager instance
|
||||
*
|
||||
* 游戏代码可以直接使用这个实例查询输入状态。
|
||||
* Game code can use this instance directly to query input state.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { Input, MouseButton } from '@esengine/engine-core';
|
||||
*
|
||||
* // 检查按键
|
||||
* if (Input.isKeyDown('KeyW')) {
|
||||
* // 向上移动
|
||||
* }
|
||||
*
|
||||
* // 检查鼠标
|
||||
* if (Input.isMouseButtonJustPressed(MouseButton.Left)) {
|
||||
* console.log('点击位置:', Input.mousePosition);
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export const Input = new InputManager();
|
||||
264
packages/engine-core/src/Input/InputSystem.ts
Normal file
264
packages/engine-core/src/Input/InputSystem.ts
Normal file
@@ -0,0 +1,264 @@
|
||||
/**
|
||||
* 输入系统 - 将平台输入事件连接到 InputManager
|
||||
* Input System - Connects platform input events to InputManager
|
||||
*
|
||||
* 在 ECS 更新循环中运行,负责:
|
||||
* 1. 在帧开始时已经由事件驱动更新了 InputManager
|
||||
* 2. 在帧末清理临时状态(justPressed, justReleased 等)
|
||||
*
|
||||
* Runs in ECS update loop, responsible for:
|
||||
* 1. InputManager is already updated by events at frame start
|
||||
* 2. Clear temporary state at frame end (justPressed, justReleased, etc.)
|
||||
*/
|
||||
|
||||
import { EntitySystem, Matcher, ECSSystem } from '@esengine/ecs-framework';
|
||||
import type { Entity } from '@esengine/ecs-framework';
|
||||
import type {
|
||||
IPlatformInputSubsystem,
|
||||
KeyboardEventInfo,
|
||||
MouseEventInfo,
|
||||
WheelEventInfo,
|
||||
TouchEvent
|
||||
} from '@esengine/platform-common';
|
||||
import { Input, InputManager } from './InputManager';
|
||||
|
||||
/**
|
||||
* 输入系统配置
|
||||
* Input system configuration
|
||||
*/
|
||||
export interface InputSystemConfig {
|
||||
/**
|
||||
* 输入管理器实例,默认使用全局 Input
|
||||
* Input manager instance, defaults to global Input
|
||||
*/
|
||||
inputManager?: InputManager;
|
||||
|
||||
/**
|
||||
* 是否在编辑器模式下禁用(防止与编辑器输入冲突)
|
||||
* Whether to disable in editor mode (prevent conflict with editor input)
|
||||
*/
|
||||
disableInEditor?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输入系统
|
||||
* Input System
|
||||
*
|
||||
* 处理平台输入事件并更新 InputManager 状态。
|
||||
* Handles platform input events and updates InputManager state.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 在 GameRuntime 中注册
|
||||
* const inputSystem = new InputSystem({
|
||||
* inputSubsystem: webInputSubsystem
|
||||
* });
|
||||
* scene.addSystem(inputSystem);
|
||||
*
|
||||
* // 在游戏系统中使用
|
||||
* import { Input, MouseButton } from '@esengine/engine-core';
|
||||
*
|
||||
* class PlayerSystem extends EntitySystem {
|
||||
* protected process(entities: readonly Entity[]): void {
|
||||
* if (Input.isKeyDown('KeyW')) {
|
||||
* // 移动玩家
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
@ECSSystem('InputSystem', { updateOrder: -1000 }) // 最先更新 | Update first
|
||||
export class InputSystem extends EntitySystem {
|
||||
private _inputManager: InputManager;
|
||||
private _inputSubsystem: IPlatformInputSubsystem | null = null;
|
||||
private _disableInEditor: boolean;
|
||||
private _isInitialized: boolean = false;
|
||||
|
||||
constructor(config: InputSystemConfig = {}) {
|
||||
// 不匹配任何实体,只用于生命周期 | Match no entities, only for lifecycle
|
||||
super(Matcher.nothing());
|
||||
|
||||
this._inputManager = config.inputManager ?? Input;
|
||||
this._disableInEditor = config.disableInEditor ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置平台输入子系统
|
||||
* Set platform input subsystem
|
||||
*
|
||||
* @param subsystem 平台输入子系统 | Platform input subsystem
|
||||
*/
|
||||
setInputSubsystem(subsystem: IPlatformInputSubsystem): void {
|
||||
// 如果已有子系统,先解绑 | Unbind if already has subsystem
|
||||
if (this._inputSubsystem && this._isInitialized) {
|
||||
this.unbindEvents();
|
||||
}
|
||||
|
||||
this._inputSubsystem = subsystem;
|
||||
|
||||
// 如果已初始化,立即绑定 | Bind immediately if initialized
|
||||
if (this._isInitialized) {
|
||||
this.bindEvents();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取输入管理器
|
||||
* Get input manager
|
||||
*/
|
||||
get inputManager(): InputManager {
|
||||
return this._inputManager;
|
||||
}
|
||||
|
||||
protected override onInitialize(): void {
|
||||
this._isInitialized = true;
|
||||
|
||||
if (this._inputSubsystem) {
|
||||
this.bindEvents();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定平台输入事件
|
||||
* Bind platform input events
|
||||
*/
|
||||
private bindEvents(): void {
|
||||
if (!this._inputSubsystem) return;
|
||||
|
||||
const sub = this._inputSubsystem;
|
||||
|
||||
// 键盘事件 | Keyboard events
|
||||
if (sub.onKeyDown) {
|
||||
sub.onKeyDown(this._handleKeyDown);
|
||||
}
|
||||
if (sub.onKeyUp) {
|
||||
sub.onKeyUp(this._handleKeyUp);
|
||||
}
|
||||
|
||||
// 鼠标事件 | Mouse events
|
||||
if (sub.onMouseMove) {
|
||||
sub.onMouseMove(this._handleMouseMove);
|
||||
}
|
||||
if (sub.onMouseDown) {
|
||||
sub.onMouseDown(this._handleMouseDown);
|
||||
}
|
||||
if (sub.onMouseUp) {
|
||||
sub.onMouseUp(this._handleMouseUp);
|
||||
}
|
||||
if (sub.onWheel) {
|
||||
sub.onWheel(this._handleWheel);
|
||||
}
|
||||
|
||||
// 触摸事件 | Touch events
|
||||
sub.onTouchStart(this._handleTouchStart);
|
||||
sub.onTouchMove(this._handleTouchMove);
|
||||
sub.onTouchEnd(this._handleTouchEnd);
|
||||
sub.onTouchCancel(this._handleTouchEnd); // 取消当作结束处理 | Treat cancel as end
|
||||
}
|
||||
|
||||
/**
|
||||
* 解绑平台输入事件
|
||||
* Unbind platform input events
|
||||
*/
|
||||
private unbindEvents(): void {
|
||||
if (!this._inputSubsystem) return;
|
||||
|
||||
const sub = this._inputSubsystem;
|
||||
|
||||
// 键盘事件 | Keyboard events
|
||||
if (sub.offKeyDown) {
|
||||
sub.offKeyDown(this._handleKeyDown);
|
||||
}
|
||||
if (sub.offKeyUp) {
|
||||
sub.offKeyUp(this._handleKeyUp);
|
||||
}
|
||||
|
||||
// 鼠标事件 | Mouse events
|
||||
if (sub.offMouseMove) {
|
||||
sub.offMouseMove(this._handleMouseMove);
|
||||
}
|
||||
if (sub.offMouseDown) {
|
||||
sub.offMouseDown(this._handleMouseDown);
|
||||
}
|
||||
if (sub.offMouseUp) {
|
||||
sub.offMouseUp(this._handleMouseUp);
|
||||
}
|
||||
if (sub.offWheel) {
|
||||
sub.offWheel(this._handleWheel);
|
||||
}
|
||||
|
||||
// 触摸事件 | Touch events
|
||||
sub.offTouchStart(this._handleTouchStart);
|
||||
sub.offTouchMove(this._handleTouchMove);
|
||||
sub.offTouchEnd(this._handleTouchEnd);
|
||||
sub.offTouchCancel(this._handleTouchEnd);
|
||||
}
|
||||
|
||||
// ========== 事件处理函数 | Event handlers ==========
|
||||
// 使用箭头函数保持 this 绑定 | Use arrow functions to preserve this binding
|
||||
|
||||
private _handleKeyDown = (event: KeyboardEventInfo): void => {
|
||||
this._inputManager.handleKeyDown(event);
|
||||
};
|
||||
|
||||
private _handleKeyUp = (event: KeyboardEventInfo): void => {
|
||||
this._inputManager.handleKeyUp(event);
|
||||
};
|
||||
|
||||
private _handleMouseMove = (event: MouseEventInfo): void => {
|
||||
this._inputManager.handleMouseMove(event);
|
||||
};
|
||||
|
||||
private _handleMouseDown = (event: MouseEventInfo): void => {
|
||||
this._inputManager.handleMouseDown(event);
|
||||
};
|
||||
|
||||
private _handleMouseUp = (event: MouseEventInfo): void => {
|
||||
this._inputManager.handleMouseUp(event);
|
||||
};
|
||||
|
||||
private _handleWheel = (event: WheelEventInfo): void => {
|
||||
this._inputManager.handleWheel(event);
|
||||
};
|
||||
|
||||
private _handleTouchStart = (event: TouchEvent): void => {
|
||||
this._inputManager.handleTouchStart(event.changedTouches);
|
||||
};
|
||||
|
||||
private _handleTouchMove = (event: TouchEvent): void => {
|
||||
this._inputManager.handleTouchMove(event.changedTouches);
|
||||
};
|
||||
|
||||
private _handleTouchEnd = (event: TouchEvent): void => {
|
||||
this._inputManager.handleTouchEnd(event.changedTouches);
|
||||
};
|
||||
|
||||
// ========== 系统生命周期 | System lifecycle ==========
|
||||
|
||||
protected override process(_entities: readonly Entity[]): void {
|
||||
// 不处理实体,仅用于生命周期 | No entity processing, only for lifecycle
|
||||
}
|
||||
|
||||
protected override lateProcess(_entities: readonly Entity[]): void {
|
||||
// 在帧末清理临时状态 | Clear temporary state at end of frame
|
||||
this._inputManager.endFrame();
|
||||
}
|
||||
|
||||
protected override onDestroy(): void {
|
||||
this.unbindEvents();
|
||||
this._inputManager.reset();
|
||||
this._isInitialized = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否应该启用输入
|
||||
* Check if input should be enabled
|
||||
*/
|
||||
protected override onCheckProcessing(): boolean {
|
||||
// 如果设置了编辑器模式禁用,检查场景是否在编辑器模式
|
||||
if (this._disableInEditor && this.scene?.isEditorMode) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
41
packages/engine-core/src/Input/index.ts
Normal file
41
packages/engine-core/src/Input/index.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* 输入系统模块
|
||||
* Input System Module
|
||||
*
|
||||
* 提供统一的输入处理能力,支持键盘、鼠标和触摸输入。
|
||||
* Provides unified input handling for keyboard, mouse, and touch.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { Input, InputSystem, MouseButton } from '@esengine/engine-core';
|
||||
*
|
||||
* // 在游戏代码中查询输入状态
|
||||
* if (Input.isKeyDown('KeyW')) {
|
||||
* // 向上移动
|
||||
* }
|
||||
*
|
||||
* if (Input.isMouseButtonJustPressed(MouseButton.Left)) {
|
||||
* console.log('点击位置:', Input.mousePosition);
|
||||
* }
|
||||
*
|
||||
* // 触摸输入
|
||||
* if (Input.isTouching) {
|
||||
* for (const [id, touch] of Input.touches) {
|
||||
* console.log('触摸点:', id, touch.x, touch.y);
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
|
||||
export { InputManager, Input, type KeyState, type MouseButtonState, type Vector2 } from './InputManager';
|
||||
export { InputSystem, type InputSystemConfig } from './InputSystem';
|
||||
|
||||
// 重导出平台公共类型 | Re-export platform common types
|
||||
export { MouseButton } from '@esengine/platform-common';
|
||||
export type {
|
||||
KeyboardEventInfo,
|
||||
MouseEventInfo,
|
||||
WheelEventInfo,
|
||||
TouchInfo,
|
||||
TouchEvent
|
||||
} from '@esengine/platform-common';
|
||||
@@ -18,3 +18,20 @@ export {
|
||||
type ModulePlatform,
|
||||
type ModuleExports
|
||||
} from './ModuleManifest';
|
||||
|
||||
// Input System (keyboard, mouse, touch)
|
||||
export {
|
||||
Input,
|
||||
InputManager,
|
||||
InputSystem,
|
||||
MouseButton,
|
||||
type InputSystemConfig,
|
||||
type KeyState,
|
||||
type MouseButtonState,
|
||||
type Vector2,
|
||||
type KeyboardEventInfo,
|
||||
type MouseEventInfo,
|
||||
type WheelEventInfo,
|
||||
type TouchInfo,
|
||||
type TouchEvent
|
||||
} from './Input';
|
||||
|
||||
Reference in New Issue
Block a user