feat: 集成Rust WASM渲染引擎与TypeScript ECS框架 (#228)
* feat: 集成Rust WASM渲染引擎与TypeScript ECS框架 * feat: 增强编辑器UI功能与跨平台支持 * fix: 修复CI测试和类型检查问题 * fix: 修复CI问题并提高测试覆盖率 * fix: 修复CI问题并提高测试覆盖率
This commit is contained in:
61
packages/engine/src/input/input_manager.rs
Normal file
61
packages/engine/src/input/input_manager.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
//! Unified input manager.
|
||||
//! 统一输入管理器。
|
||||
|
||||
use super::{KeyboardState, MouseState, TouchState};
|
||||
|
||||
/// Unified input manager handling keyboard, mouse, and touch.
|
||||
/// 处理键盘、鼠标和触摸的统一输入管理器。
|
||||
///
|
||||
/// Provides a single interface for all input types.
|
||||
/// 为所有输入类型提供单一接口。
|
||||
#[derive(Debug, Default)]
|
||||
pub struct InputManager {
|
||||
/// Keyboard state.
|
||||
/// 键盘状态。
|
||||
pub keyboard: KeyboardState,
|
||||
|
||||
/// Mouse state.
|
||||
/// 鼠标状态。
|
||||
pub mouse: MouseState,
|
||||
|
||||
/// Touch state.
|
||||
/// 触摸状态。
|
||||
pub touch: TouchState,
|
||||
}
|
||||
|
||||
impl InputManager {
|
||||
/// Create a new input manager.
|
||||
/// 创建新的输入管理器。
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Update all input states for a new frame.
|
||||
/// 为新帧更新所有输入状态。
|
||||
pub fn update(&mut self) {
|
||||
self.keyboard.update();
|
||||
self.mouse.update();
|
||||
self.touch.update();
|
||||
}
|
||||
|
||||
/// Check if a key is currently pressed.
|
||||
/// 检查某个键是否当前被按下。
|
||||
#[inline]
|
||||
pub fn is_key_down(&self, key: &str) -> bool {
|
||||
self.keyboard.is_key_down(key)
|
||||
}
|
||||
|
||||
/// Check if a key was just pressed this frame.
|
||||
/// 检查某个键是否在本帧刚被按下。
|
||||
#[inline]
|
||||
pub fn is_key_just_pressed(&self, key: &str) -> bool {
|
||||
self.keyboard.is_key_just_pressed(key)
|
||||
}
|
||||
|
||||
/// Clear all input states.
|
||||
/// 清除所有输入状态。
|
||||
pub fn clear(&mut self) {
|
||||
self.keyboard.clear();
|
||||
self.touch.clear();
|
||||
}
|
||||
}
|
||||
82
packages/engine/src/input/keyboard.rs
Normal file
82
packages/engine/src/input/keyboard.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
//! Keyboard input handling.
|
||||
//! 键盘输入处理。
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
/// Keyboard input state.
|
||||
/// 键盘输入状态。
|
||||
#[derive(Debug, Default)]
|
||||
pub struct KeyboardState {
|
||||
/// Currently pressed keys.
|
||||
/// 当前按下的键。
|
||||
pressed: HashSet<String>,
|
||||
|
||||
/// Keys pressed this frame.
|
||||
/// 本帧按下的键。
|
||||
just_pressed: HashSet<String>,
|
||||
|
||||
/// Keys released this frame.
|
||||
/// 本帧释放的键。
|
||||
just_released: HashSet<String>,
|
||||
}
|
||||
|
||||
impl KeyboardState {
|
||||
/// Create new keyboard state.
|
||||
/// 创建新的键盘状态。
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Handle key down event.
|
||||
/// 处理按键按下事件。
|
||||
pub fn key_down(&mut self, key: String) {
|
||||
if !self.pressed.contains(&key) {
|
||||
self.just_pressed.insert(key.clone());
|
||||
}
|
||||
self.pressed.insert(key);
|
||||
}
|
||||
|
||||
/// Handle key up event.
|
||||
/// 处理按键释放事件。
|
||||
pub fn key_up(&mut self, key: String) {
|
||||
if self.pressed.remove(&key) {
|
||||
self.just_released.insert(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a key is currently pressed.
|
||||
/// 检查某个键是否当前被按下。
|
||||
#[inline]
|
||||
pub fn is_key_down(&self, key: &str) -> bool {
|
||||
self.pressed.contains(key)
|
||||
}
|
||||
|
||||
/// Check if a key was just pressed this frame.
|
||||
/// 检查某个键是否在本帧刚被按下。
|
||||
#[inline]
|
||||
pub fn is_key_just_pressed(&self, key: &str) -> bool {
|
||||
self.just_pressed.contains(key)
|
||||
}
|
||||
|
||||
/// Check if a key was just released this frame.
|
||||
/// 检查某个键是否在本帧刚被释放。
|
||||
#[inline]
|
||||
pub fn is_key_just_released(&self, key: &str) -> bool {
|
||||
self.just_released.contains(key)
|
||||
}
|
||||
|
||||
/// Update state for new frame.
|
||||
/// 为新帧更新状态。
|
||||
pub fn update(&mut self) {
|
||||
self.just_pressed.clear();
|
||||
self.just_released.clear();
|
||||
}
|
||||
|
||||
/// Clear all input state.
|
||||
/// 清除所有输入状态。
|
||||
pub fn clear(&mut self) {
|
||||
self.pressed.clear();
|
||||
self.just_pressed.clear();
|
||||
self.just_released.clear();
|
||||
}
|
||||
}
|
||||
12
packages/engine/src/input/mod.rs
Normal file
12
packages/engine/src/input/mod.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
//! Input handling system.
|
||||
//! 输入处理系统。
|
||||
|
||||
mod keyboard;
|
||||
mod mouse;
|
||||
mod touch;
|
||||
mod input_manager;
|
||||
|
||||
pub use input_manager::InputManager;
|
||||
pub use keyboard::KeyboardState;
|
||||
pub use mouse::{MouseState, MouseButton};
|
||||
pub use touch::{TouchState, TouchPoint};
|
||||
136
packages/engine/src/input/mouse.rs
Normal file
136
packages/engine/src/input/mouse.rs
Normal file
@@ -0,0 +1,136 @@
|
||||
//! Mouse input handling.
|
||||
//! 鼠标输入处理。
|
||||
|
||||
use crate::math::Vec2;
|
||||
|
||||
/// Mouse button identifiers.
|
||||
/// 鼠标按钮标识符。
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum MouseButton {
|
||||
/// Left mouse button.
|
||||
/// 鼠标左键。
|
||||
Left,
|
||||
/// Middle mouse button (scroll wheel).
|
||||
/// 鼠标中键(滚轮)。
|
||||
Middle,
|
||||
/// Right mouse button.
|
||||
/// 鼠标右键。
|
||||
Right,
|
||||
}
|
||||
|
||||
impl MouseButton {
|
||||
/// Convert from button index.
|
||||
/// 从按钮索引转换。
|
||||
pub fn from_index(index: i16) -> Option<Self> {
|
||||
match index {
|
||||
0 => Some(MouseButton::Left),
|
||||
1 => Some(MouseButton::Middle),
|
||||
2 => Some(MouseButton::Right),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Mouse input state.
|
||||
/// 鼠标输入状态。
|
||||
#[derive(Debug, Default)]
|
||||
pub struct MouseState {
|
||||
/// Current mouse position.
|
||||
/// 当前鼠标位置。
|
||||
pub position: Vec2,
|
||||
|
||||
/// Mouse movement delta since last frame.
|
||||
/// 自上一帧以来的鼠标移动增量。
|
||||
pub delta: Vec2,
|
||||
|
||||
/// Scroll wheel delta.
|
||||
/// 滚轮增量。
|
||||
pub scroll_delta: f32,
|
||||
|
||||
/// Button states (left, middle, right).
|
||||
/// 按钮状态(左、中、右)。
|
||||
buttons: [bool; 3],
|
||||
|
||||
/// Buttons just pressed this frame.
|
||||
/// 本帧刚按下的按钮。
|
||||
just_pressed: [bool; 3],
|
||||
|
||||
/// Buttons just released this frame.
|
||||
/// 本帧刚释放的按钮。
|
||||
just_released: [bool; 3],
|
||||
|
||||
/// Previous position for delta calculation.
|
||||
/// 用于计算增量的上一位置。
|
||||
prev_position: Vec2,
|
||||
}
|
||||
|
||||
impl MouseState {
|
||||
/// Create new mouse state.
|
||||
/// 创建新的鼠标状态。
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Handle mouse move event.
|
||||
/// 处理鼠标移动事件。
|
||||
pub fn mouse_move(&mut self, x: f32, y: f32) {
|
||||
self.position = Vec2::new(x, y);
|
||||
}
|
||||
|
||||
/// Handle mouse button down event.
|
||||
/// 处理鼠标按钮按下事件。
|
||||
pub fn button_down(&mut self, button: MouseButton) {
|
||||
let index = button as usize;
|
||||
if !self.buttons[index] {
|
||||
self.just_pressed[index] = true;
|
||||
}
|
||||
self.buttons[index] = true;
|
||||
}
|
||||
|
||||
/// Handle mouse button up event.
|
||||
/// 处理鼠标按钮释放事件。
|
||||
pub fn button_up(&mut self, button: MouseButton) {
|
||||
let index = button as usize;
|
||||
if self.buttons[index] {
|
||||
self.just_released[index] = true;
|
||||
}
|
||||
self.buttons[index] = false;
|
||||
}
|
||||
|
||||
/// Handle scroll wheel event.
|
||||
/// 处理滚轮事件。
|
||||
pub fn scroll(&mut self, delta: f32) {
|
||||
self.scroll_delta = delta;
|
||||
}
|
||||
|
||||
/// Check if a button is currently pressed.
|
||||
/// 检查某个按钮是否当前被按下。
|
||||
#[inline]
|
||||
pub fn is_button_down(&self, button: MouseButton) -> bool {
|
||||
self.buttons[button as usize]
|
||||
}
|
||||
|
||||
/// Check if a button was just pressed this frame.
|
||||
/// 检查某个按钮是否在本帧刚被按下。
|
||||
#[inline]
|
||||
pub fn is_button_just_pressed(&self, button: MouseButton) -> bool {
|
||||
self.just_pressed[button as usize]
|
||||
}
|
||||
|
||||
/// Check if a button was just released this frame.
|
||||
/// 检查某个按钮是否在本帧刚被释放。
|
||||
#[inline]
|
||||
pub fn is_button_just_released(&self, button: MouseButton) -> bool {
|
||||
self.just_released[button as usize]
|
||||
}
|
||||
|
||||
/// Update state for new frame.
|
||||
/// 为新帧更新状态。
|
||||
pub fn update(&mut self) {
|
||||
self.delta = self.position - self.prev_position;
|
||||
self.prev_position = self.position;
|
||||
self.scroll_delta = 0.0;
|
||||
self.just_pressed = [false; 3];
|
||||
self.just_released = [false; 3];
|
||||
}
|
||||
}
|
||||
164
packages/engine/src/input/touch.rs
Normal file
164
packages/engine/src/input/touch.rs
Normal file
@@ -0,0 +1,164 @@
|
||||
//! Touch input handling.
|
||||
//! 触摸输入处理。
|
||||
|
||||
use crate::math::Vec2;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Single touch point.
|
||||
/// 单个触摸点。
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct TouchPoint {
|
||||
/// Touch identifier.
|
||||
/// 触摸标识符。
|
||||
pub id: i32,
|
||||
|
||||
/// Current position.
|
||||
/// 当前位置。
|
||||
pub position: Vec2,
|
||||
|
||||
/// Starting position.
|
||||
/// 起始位置。
|
||||
pub start_position: Vec2,
|
||||
|
||||
/// Movement delta since last frame.
|
||||
/// 自上一帧以来的移动增量。
|
||||
pub delta: Vec2,
|
||||
|
||||
/// Previous position.
|
||||
/// 上一位置。
|
||||
prev_position: Vec2,
|
||||
}
|
||||
|
||||
impl TouchPoint {
|
||||
/// Create a new touch point.
|
||||
/// 创建新的触摸点。
|
||||
pub fn new(id: i32, x: f32, y: f32) -> Self {
|
||||
let pos = Vec2::new(x, y);
|
||||
Self {
|
||||
id,
|
||||
position: pos,
|
||||
start_position: pos,
|
||||
delta: Vec2::ZERO,
|
||||
prev_position: pos,
|
||||
}
|
||||
}
|
||||
|
||||
/// Update touch position.
|
||||
/// 更新触摸位置。
|
||||
pub fn update_position(&mut self, x: f32, y: f32) {
|
||||
self.prev_position = self.position;
|
||||
self.position = Vec2::new(x, y);
|
||||
self.delta = self.position - self.prev_position;
|
||||
}
|
||||
}
|
||||
|
||||
/// Touch input state.
|
||||
/// 触摸输入状态。
|
||||
#[derive(Debug, Default)]
|
||||
pub struct TouchState {
|
||||
/// Active touch points.
|
||||
/// 活动的触摸点。
|
||||
touches: HashMap<i32, TouchPoint>,
|
||||
|
||||
/// Touch IDs that started this frame.
|
||||
/// 本帧开始的触摸ID。
|
||||
just_started: Vec<i32>,
|
||||
|
||||
/// Touch IDs that ended this frame.
|
||||
/// 本帧结束的触摸ID。
|
||||
just_ended: Vec<i32>,
|
||||
}
|
||||
|
||||
impl TouchState {
|
||||
/// Create new touch state.
|
||||
/// 创建新的触摸状态。
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Handle touch start event.
|
||||
/// 处理触摸开始事件。
|
||||
pub fn touch_start(&mut self, id: i32, x: f32, y: f32) {
|
||||
let touch = TouchPoint::new(id, x, y);
|
||||
self.touches.insert(id, touch);
|
||||
self.just_started.push(id);
|
||||
}
|
||||
|
||||
/// Handle touch move event.
|
||||
/// 处理触摸移动事件。
|
||||
pub fn touch_move(&mut self, id: i32, x: f32, y: f32) {
|
||||
if let Some(touch) = self.touches.get_mut(&id) {
|
||||
touch.update_position(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle touch end event.
|
||||
/// 处理触摸结束事件。
|
||||
pub fn touch_end(&mut self, id: i32) {
|
||||
if self.touches.remove(&id).is_some() {
|
||||
self.just_ended.push(id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a touch point by ID.
|
||||
/// 按ID获取触摸点。
|
||||
#[inline]
|
||||
pub fn get_touch(&self, id: i32) -> Option<&TouchPoint> {
|
||||
self.touches.get(&id)
|
||||
}
|
||||
|
||||
/// Get all active touch points.
|
||||
/// 获取所有活动的触摸点。
|
||||
#[inline]
|
||||
pub fn get_touches(&self) -> impl Iterator<Item = &TouchPoint> {
|
||||
self.touches.values()
|
||||
}
|
||||
|
||||
/// Get number of active touches.
|
||||
/// 获取活动触摸数量。
|
||||
#[inline]
|
||||
pub fn touch_count(&self) -> usize {
|
||||
self.touches.len()
|
||||
}
|
||||
|
||||
/// Check if any touch is active.
|
||||
/// 检查是否有任何触摸活动。
|
||||
#[inline]
|
||||
pub fn is_touching(&self) -> bool {
|
||||
!self.touches.is_empty()
|
||||
}
|
||||
|
||||
/// Get touches that started this frame.
|
||||
/// 获取本帧开始的触摸。
|
||||
#[inline]
|
||||
pub fn just_started(&self) -> &[i32] {
|
||||
&self.just_started
|
||||
}
|
||||
|
||||
/// Get touches that ended this frame.
|
||||
/// 获取本帧结束的触摸。
|
||||
#[inline]
|
||||
pub fn just_ended(&self) -> &[i32] {
|
||||
&self.just_ended
|
||||
}
|
||||
|
||||
/// Update state for new frame.
|
||||
/// 为新帧更新状态。
|
||||
pub fn update(&mut self) {
|
||||
self.just_started.clear();
|
||||
self.just_ended.clear();
|
||||
|
||||
// Reset deltas | 重置增量
|
||||
for touch in self.touches.values_mut() {
|
||||
touch.delta = Vec2::ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear all touch state.
|
||||
/// 清除所有触摸状态。
|
||||
pub fn clear(&mut self) {
|
||||
self.touches.clear();
|
||||
self.just_started.clear();
|
||||
self.just_ended.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user