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:
YHH
2025-12-05 18:15:50 +08:00
committed by GitHub
parent 13a149c3a2
commit 823e0c1d94
14 changed files with 1412 additions and 13 deletions

View File

@@ -27,8 +27,11 @@
"type-check": "tsc --noEmit",
"clean": "rimraf dist"
},
"devDependencies": {
"dependencies": {
"@esengine/ecs-framework": "workspace:*",
"@esengine/platform-common": "workspace:*"
},
"devDependencies": {
"@esengine/build-config": "workspace:*",
"rimraf": "^5.0.5",
"tsup": "^8.0.0",

View 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();

View 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;
}
}

View 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';

View File

@@ -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';

View File

@@ -354,6 +354,7 @@ export interface IPlatformNetworkSubsystem {
/**
* 触摸点信息
* Touch point information
*/
export interface TouchInfo {
identifier: number;
@@ -364,6 +365,7 @@ export interface TouchInfo {
/**
* 触摸事件
* Touch event
*/
export interface TouchEvent {
touches: TouchInfo[];
@@ -373,57 +375,270 @@ export interface TouchEvent {
/**
* 触摸事件处理函数
* Touch event handler
*/
export type TouchHandler = (event: TouchEvent) => void;
/**
* 键盘事件信息
* Keyboard event information
*/
export interface KeyboardEventInfo {
/** 按键代码 (如 'KeyW', 'Space', 'ArrowUp') | Key code */
code: string;
/** 按键值 (如 'w', ' ', 'ArrowUp') | Key value */
key: string;
/** Alt 键是否按下 | Alt key pressed */
altKey: boolean;
/** Ctrl 键是否按下 | Ctrl key pressed */
ctrlKey: boolean;
/** Shift 键是否按下 | Shift key pressed */
shiftKey: boolean;
/** Meta 键是否按下 (Windows/Command) | Meta key pressed */
metaKey: boolean;
/** 是否重复触发 | Is repeat */
repeat: boolean;
/** 时间戳 | Timestamp */
timeStamp: number;
}
/**
* 键盘事件处理函数
* Keyboard event handler
*/
export type KeyboardHandler = (event: KeyboardEventInfo) => void;
/**
* 鼠标按钮枚举
* Mouse button enum
*/
export enum MouseButton {
/** 左键 | Left button */
Left = 0,
/** 中键 | Middle button */
Middle = 1,
/** 右键 | Right button */
Right = 2
}
/**
* 鼠标事件信息
* Mouse event information
*/
export interface MouseEventInfo {
/** X 坐标 | X coordinate */
x: number;
/** Y 坐标 | Y coordinate */
y: number;
/** 相对上次的 X 偏移 | X movement delta */
movementX: number;
/** 相对上次的 Y 偏移 | Y movement delta */
movementY: number;
/** 按下的按钮 | Button pressed */
button: MouseButton;
/** 所有按下的按钮位掩码 | Buttons bitmask */
buttons: number;
/** Alt 键是否按下 | Alt key pressed */
altKey: boolean;
/** Ctrl 键是否按下 | Ctrl key pressed */
ctrlKey: boolean;
/** Shift 键是否按下 | Shift key pressed */
shiftKey: boolean;
/** Meta 键是否按下 | Meta key pressed */
metaKey: boolean;
/** 时间戳 | Timestamp */
timeStamp: number;
}
/**
* 鼠标滚轮事件信息
* Mouse wheel event information
*/
export interface WheelEventInfo {
/** X 坐标 | X coordinate */
x: number;
/** Y 坐标 | Y coordinate */
y: number;
/** X 轴滚动量 | Delta X */
deltaX: number;
/** Y 轴滚动量 | Delta Y */
deltaY: number;
/** Z 轴滚动量 | Delta Z */
deltaZ: number;
/** 时间戳 | Timestamp */
timeStamp: number;
}
/**
* 鼠标事件处理函数
* Mouse event handler
*/
export type MouseHandler = (event: MouseEventInfo) => void;
/**
* 鼠标滚轮事件处理函数
* Mouse wheel event handler
*/
export type WheelHandler = (event: WheelEventInfo) => void;
/**
* 输入子系统接口
* Input subsystem interface
*/
export interface IPlatformInputSubsystem {
// ========== 触摸事件 | Touch events ==========
/**
* 监听触摸开始
* Listen for touch start
*/
onTouchStart(handler: TouchHandler): void;
/**
* 监听触摸移动
* Listen for touch move
*/
onTouchMove(handler: TouchHandler): void;
/**
* 监听触摸结束
* Listen for touch end
*/
onTouchEnd(handler: TouchHandler): void;
/**
* 监听触摸取消
* Listen for touch cancel
*/
onTouchCancel(handler: TouchHandler): void;
/**
* 取消监听触摸开始
* Stop listening for touch start
*/
offTouchStart(handler: TouchHandler): void;
/**
* 取消监听触摸移动
* Stop listening for touch move
*/
offTouchMove(handler: TouchHandler): void;
/**
* 取消监听触摸结束
* Stop listening for touch end
*/
offTouchEnd(handler: TouchHandler): void;
/**
* 取消监听触摸取消
* Stop listening for touch cancel
*/
offTouchCancel(handler: TouchHandler): void;
/**
* 获取触摸点是否支持压感
* Check if touch supports pressure
*/
supportsPressure?(): boolean;
// ========== 键盘事件 | Keyboard events ==========
/**
* 监听键盘按下
* Listen for key down
*/
onKeyDown?(handler: KeyboardHandler): void;
/**
* 监听键盘释放
* Listen for key up
*/
onKeyUp?(handler: KeyboardHandler): void;
/**
* 取消监听键盘按下
* Stop listening for key down
*/
offKeyDown?(handler: KeyboardHandler): void;
/**
* 取消监听键盘释放
* Stop listening for key up
*/
offKeyUp?(handler: KeyboardHandler): void;
// ========== 鼠标事件 | Mouse events ==========
/**
* 监听鼠标移动
* Listen for mouse move
*/
onMouseMove?(handler: MouseHandler): void;
/**
* 监听鼠标按下
* Listen for mouse down
*/
onMouseDown?(handler: MouseHandler): void;
/**
* 监听鼠标释放
* Listen for mouse up
*/
onMouseUp?(handler: MouseHandler): void;
/**
* 监听鼠标滚轮
* Listen for mouse wheel
*/
onWheel?(handler: WheelHandler): void;
/**
* 取消监听鼠标移动
* Stop listening for mouse move
*/
offMouseMove?(handler: MouseHandler): void;
/**
* 取消监听鼠标按下
* Stop listening for mouse down
*/
offMouseDown?(handler: MouseHandler): void;
/**
* 取消监听鼠标释放
* Stop listening for mouse up
*/
offMouseUp?(handler: MouseHandler): void;
/**
* 取消监听鼠标滚轮
* Stop listening for mouse wheel
*/
offWheel?(handler: WheelHandler): void;
// ========== 输入能力查询 | Input capability queries ==========
/**
* 是否支持键盘输入
* Check if keyboard input is supported
*/
supportsKeyboard?(): boolean;
/**
* 是否支持鼠标输入
* Check if mouse input is supported
*/
supportsMouse?(): boolean;
// ========== 生命周期 | Lifecycle ==========
/**
* 释放资源
* Dispose resources
*/
dispose?(): void;
}
// ============================================================================

View File

@@ -29,6 +29,12 @@ export type {
TouchInfo,
TouchEvent,
TouchHandler,
KeyboardEventInfo,
KeyboardHandler,
MouseEventInfo,
MouseHandler,
WheelEventInfo,
WheelHandler,
// 文件系统
IPlatformFileSubsystem,
FileInfo,
@@ -41,6 +47,9 @@ export type {
SystemInfo
} from './IPlatformSubsystems';
// 导出枚举值 | Export enum values
export { MouseButton } from './IPlatformSubsystems';
// WASM 库加载器
export {
PlatformType,

View File

@@ -1,22 +1,44 @@
/**
* Web 平台输入子系统
* Web platform input subsystem
*/
import type {
IPlatformInputSubsystem,
TouchHandler,
TouchEvent
TouchEvent,
KeyboardHandler,
KeyboardEventInfo,
MouseHandler,
MouseEventInfo,
WheelHandler,
WheelEventInfo
} from '@esengine/platform-common';
import { MouseButton } from '@esengine/platform-common';
/**
* Web 平台输入子系统实现
* Web platform input subsystem implementation
*/
export class WebInputSubsystem implements IPlatformInputSubsystem {
// ========== Touch handlers ==========
private _touchStartHandlers: Map<TouchHandler, (e: globalThis.TouchEvent) => void> = new Map();
private _touchMoveHandlers: Map<TouchHandler, (e: globalThis.TouchEvent) => void> = new Map();
private _touchEndHandlers: Map<TouchHandler, (e: globalThis.TouchEvent) => void> = new Map();
private _touchCancelHandlers: Map<TouchHandler, (e: globalThis.TouchEvent) => void> = new Map();
// ========== Keyboard handlers ==========
private _keyDownHandlers: Map<KeyboardHandler, (e: globalThis.KeyboardEvent) => void> = new Map();
private _keyUpHandlers: Map<KeyboardHandler, (e: globalThis.KeyboardEvent) => void> = new Map();
// ========== Mouse handlers ==========
private _mouseMoveHandlers: Map<MouseHandler, (e: globalThis.MouseEvent) => void> = new Map();
private _mouseDownHandlers: Map<MouseHandler, (e: globalThis.MouseEvent) => void> = new Map();
private _mouseUpHandlers: Map<MouseHandler, (e: globalThis.MouseEvent) => void> = new Map();
private _wheelHandlers: Map<WheelHandler, (e: globalThis.WheelEvent) => void> = new Map();
// ========== Touch events ==========
onTouchStart(handler: TouchHandler): void {
const nativeHandler = (e: globalThis.TouchEvent) => {
handler(this.convertTouchEvent(e));
@@ -82,9 +104,122 @@ export class WebInputSubsystem implements IPlatformInputSubsystem {
}
supportsPressure(): boolean {
return 'force' in Touch.prototype;
return typeof Touch !== 'undefined' && 'force' in Touch.prototype;
}
// ========== Keyboard events ==========
onKeyDown(handler: KeyboardHandler): void {
const nativeHandler = (e: globalThis.KeyboardEvent) => {
handler(this.convertKeyboardEvent(e));
};
this._keyDownHandlers.set(handler, nativeHandler);
window.addEventListener('keydown', nativeHandler);
}
onKeyUp(handler: KeyboardHandler): void {
const nativeHandler = (e: globalThis.KeyboardEvent) => {
handler(this.convertKeyboardEvent(e));
};
this._keyUpHandlers.set(handler, nativeHandler);
window.addEventListener('keyup', nativeHandler);
}
offKeyDown(handler: KeyboardHandler): void {
const nativeHandler = this._keyDownHandlers.get(handler);
if (nativeHandler) {
window.removeEventListener('keydown', nativeHandler);
this._keyDownHandlers.delete(handler);
}
}
offKeyUp(handler: KeyboardHandler): void {
const nativeHandler = this._keyUpHandlers.get(handler);
if (nativeHandler) {
window.removeEventListener('keyup', nativeHandler);
this._keyUpHandlers.delete(handler);
}
}
// ========== Mouse events ==========
onMouseMove(handler: MouseHandler): void {
const nativeHandler = (e: globalThis.MouseEvent) => {
handler(this.convertMouseEvent(e));
};
this._mouseMoveHandlers.set(handler, nativeHandler);
window.addEventListener('mousemove', nativeHandler);
}
onMouseDown(handler: MouseHandler): void {
const nativeHandler = (e: globalThis.MouseEvent) => {
handler(this.convertMouseEvent(e));
};
this._mouseDownHandlers.set(handler, nativeHandler);
window.addEventListener('mousedown', nativeHandler);
}
onMouseUp(handler: MouseHandler): void {
const nativeHandler = (e: globalThis.MouseEvent) => {
handler(this.convertMouseEvent(e));
};
this._mouseUpHandlers.set(handler, nativeHandler);
window.addEventListener('mouseup', nativeHandler);
}
onWheel(handler: WheelHandler): void {
const nativeHandler = (e: globalThis.WheelEvent) => {
handler(this.convertWheelEvent(e));
};
this._wheelHandlers.set(handler, nativeHandler);
window.addEventListener('wheel', nativeHandler);
}
offMouseMove(handler: MouseHandler): void {
const nativeHandler = this._mouseMoveHandlers.get(handler);
if (nativeHandler) {
window.removeEventListener('mousemove', nativeHandler);
this._mouseMoveHandlers.delete(handler);
}
}
offMouseDown(handler: MouseHandler): void {
const nativeHandler = this._mouseDownHandlers.get(handler);
if (nativeHandler) {
window.removeEventListener('mousedown', nativeHandler);
this._mouseDownHandlers.delete(handler);
}
}
offMouseUp(handler: MouseHandler): void {
const nativeHandler = this._mouseUpHandlers.get(handler);
if (nativeHandler) {
window.removeEventListener('mouseup', nativeHandler);
this._mouseUpHandlers.delete(handler);
}
}
offWheel(handler: WheelHandler): void {
const nativeHandler = this._wheelHandlers.get(handler);
if (nativeHandler) {
window.removeEventListener('wheel', nativeHandler);
this._wheelHandlers.delete(handler);
}
}
// ========== Capability queries ==========
supportsKeyboard(): boolean {
return true;
}
supportsMouse(): boolean {
// 检测是否有鼠标设备 | Check if mouse device exists
return window.matchMedia('(pointer: fine)').matches;
}
// ========== Event converters ==========
private convertTouchEvent(e: globalThis.TouchEvent): TouchEvent {
const convertTouch = (touch: globalThis.Touch) => ({
identifier: touch.identifier,
@@ -99,4 +234,103 @@ export class WebInputSubsystem implements IPlatformInputSubsystem {
timeStamp: e.timeStamp
};
}
private convertKeyboardEvent(e: globalThis.KeyboardEvent): KeyboardEventInfo {
return {
code: e.code,
key: e.key,
altKey: e.altKey,
ctrlKey: e.ctrlKey,
shiftKey: e.shiftKey,
metaKey: e.metaKey,
repeat: e.repeat,
timeStamp: e.timeStamp
};
}
private convertMouseEvent(e: globalThis.MouseEvent): MouseEventInfo {
return {
x: e.clientX,
y: e.clientY,
movementX: e.movementX,
movementY: e.movementY,
button: e.button as MouseButton,
buttons: e.buttons,
altKey: e.altKey,
ctrlKey: e.ctrlKey,
shiftKey: e.shiftKey,
metaKey: e.metaKey,
timeStamp: e.timeStamp
};
}
private convertWheelEvent(e: globalThis.WheelEvent): WheelEventInfo {
return {
x: e.clientX,
y: e.clientY,
deltaX: e.deltaX,
deltaY: e.deltaY,
deltaZ: e.deltaZ,
timeStamp: e.timeStamp
};
}
/**
* 销毁并移除所有事件监听器
* Dispose and remove all event listeners
*/
dispose(): void {
// 清理触摸事件 | Clean up touch events
this._touchStartHandlers.forEach((handler) => {
window.removeEventListener('touchstart', handler);
});
this._touchStartHandlers.clear();
this._touchMoveHandlers.forEach((handler) => {
window.removeEventListener('touchmove', handler);
});
this._touchMoveHandlers.clear();
this._touchEndHandlers.forEach((handler) => {
window.removeEventListener('touchend', handler);
});
this._touchEndHandlers.clear();
this._touchCancelHandlers.forEach((handler) => {
window.removeEventListener('touchcancel', handler);
});
this._touchCancelHandlers.clear();
// 清理键盘事件 | Clean up keyboard events
this._keyDownHandlers.forEach((handler) => {
window.removeEventListener('keydown', handler);
});
this._keyDownHandlers.clear();
this._keyUpHandlers.forEach((handler) => {
window.removeEventListener('keyup', handler);
});
this._keyUpHandlers.clear();
// 清理鼠标事件 | Clean up mouse events
this._mouseMoveHandlers.forEach((handler) => {
window.removeEventListener('mousemove', handler);
});
this._mouseMoveHandlers.clear();
this._mouseDownHandlers.forEach((handler) => {
window.removeEventListener('mousedown', handler);
});
this._mouseDownHandlers.clear();
this._mouseUpHandlers.forEach((handler) => {
window.removeEventListener('mouseup', handler);
});
this._mouseUpHandlers.clear();
this._wheelHandlers.forEach((handler) => {
window.removeEventListener('wheel', handler);
});
this._wheelHandlers.clear();
}
}

View File

@@ -21,11 +21,14 @@
"type-check": "tsc --noEmit",
"clean": "rimraf dist"
},
"devDependencies": {
"dependencies": {
"@esengine/ecs-framework": "workspace:*",
"@esengine/ecs-engine-bindgen": "workspace:*",
"@esengine/engine-core": "workspace:*",
"@esengine/asset-system": "workspace:*",
"@esengine/platform-common": "workspace:*"
},
"devDependencies": {
"@esengine/build-config": "workspace:*",
"rimraf": "^5.0.5",
"tsup": "^8.0.0",

View File

@@ -8,7 +8,7 @@
import { Core, Scene, SceneSerializer, HierarchySystem } from '@esengine/ecs-framework';
import { EngineBridge, EngineRenderSystem, CameraSystem } from '@esengine/ecs-engine-bindgen';
import { TransformComponent, TransformSystem } from '@esengine/engine-core';
import { TransformComponent, TransformSystem, InputSystem, Input } from '@esengine/engine-core';
import { AssetManager, EngineIntegration } from '@esengine/asset-system';
import {
runtimePluginManager,
@@ -77,6 +77,7 @@ export class GameRuntime {
private _scene: Scene | null = null;
private _renderSystem: EngineRenderSystem | null = null;
private _cameraSystem: CameraSystem | null = null;
private _inputSystem: InputSystem | null = null;
private _assetManager: AssetManager | null = null;
private _engineIntegration: EngineIntegration | null = null;
private _projectConfig: ProjectConfig;
@@ -228,6 +229,19 @@ export class GameRuntime {
this._scene.addSystem(new HierarchySystem());
this._scene.addSystem(new TransformSystem());
// 7. 添加输入系统(最先更新,以便其他系统可以读取输入状态)
// Add input system (updates first so other systems can read input state)
this._inputSystem = new InputSystem({
disableInEditor: true // 编辑器模式下禁用,避免与编辑器输入冲突
});
this._scene.addSystem(this._inputSystem);
// 设置平台输入子系统 | Set platform input subsystem
const inputSubsystem = this._platform.getInputSubsystem?.();
if (inputSubsystem) {
this._inputSystem.setInputSubsystem(inputSubsystem);
}
this._cameraSystem = new CameraSystem(this._bridge);
this._scene.addSystem(this._cameraSystem);
@@ -257,7 +271,9 @@ export class GameRuntime {
isEditor: this._platform.isEditorMode(),
engineBridge: this._bridge,
renderSystem: this._renderSystem,
assetManager: this._assetManager
assetManager: this._assetManager,
inputSystem: this._inputSystem,
inputManager: Input
};
// 11. 让插件创建系统(编辑器模式下跳过,由 EngineService.initializeModuleSystems 处理)
@@ -845,6 +861,7 @@ export class GameRuntime {
this._renderSystem = null;
this._cameraSystem = null;
this._inputSystem = null;
this._systemContext = null;
this._platform.dispose();

View File

@@ -6,6 +6,8 @@
* Defines the adapter interface that different platforms need to implement
*/
import type { IPlatformInputSubsystem } from '@esengine/platform-common';
/**
* 资源路径解析器
* Asset path resolver
@@ -127,6 +129,15 @@ export interface IPlatformAdapter {
*/
setShowGizmos?(show: boolean): void;
/**
* 获取输入子系统
* Get input subsystem
*
* 返回平台特定的输入子系统实现
* Returns platform-specific input subsystem implementation
*/
getInputSubsystem?(): IPlatformInputSubsystem | null;
/**
* 释放资源
* Dispose resources

View File

@@ -12,6 +12,7 @@ import type {
PlatformCapabilities,
PlatformAdapterConfig
} from '../IPlatformAdapter';
import type { IPlatformInputSubsystem } from '@esengine/platform-common';
/**
* 浏览器路径解析器
@@ -56,6 +57,11 @@ export interface BrowserPlatformConfig {
wasmModuleLoader?: () => Promise<any>;
/** 资产基础 URL */
assetBaseUrl?: string;
/**
* 输入子系统工厂函数
* Input subsystem factory function
*/
inputSubsystemFactory?: () => IPlatformInputSubsystem;
}
/**
@@ -77,6 +83,7 @@ export class BrowserPlatformAdapter implements IPlatformAdapter {
private _canvas: HTMLCanvasElement | null = null;
private _config: BrowserPlatformConfig;
private _viewportSize = { width: 0, height: 0 };
private _inputSubsystem: IPlatformInputSubsystem | null = null;
constructor(config: BrowserPlatformConfig = {}) {
this._config = config;
@@ -100,6 +107,10 @@ export class BrowserPlatformAdapter implements IPlatformAdapter {
this._canvas.width = width;
this._canvas.height = height;
this._viewportSize = { width, height };
if (this._config.inputSubsystemFactory) {
this._inputSubsystem = this._config.inputSubsystemFactory();
}
}
async getWasmModule(): Promise<any> {
@@ -137,7 +148,15 @@ export class BrowserPlatformAdapter implements IPlatformAdapter {
return false;
}
getInputSubsystem(): IPlatformInputSubsystem | null {
return this._inputSubsystem;
}
dispose(): void {
if (this._inputSubsystem) {
this._inputSubsystem.dispose?.();
this._inputSubsystem = null;
}
this._canvas = null;
}
}

View File

@@ -70,3 +70,20 @@ export {
type AssetCatalogEntry,
type BrowserFileSystemOptions
} from './services/BrowserFileSystemService';
// Re-export Input System from engine-core for convenience
export {
Input,
InputManager,
InputSystem,
MouseButton,
type InputSystemConfig,
type KeyState,
type MouseButtonState,
type Vector2,
type KeyboardEventInfo,
type MouseEventInfo,
type WheelEventInfo,
type TouchInfo,
type TouchEvent
} from '@esengine/engine-core';