Skip to content

Commit

Permalink
Simplify canvas_view code
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Jan 2, 2025
1 parent de9a8f8 commit 79c8201
Show file tree
Hide file tree
Showing 11 changed files with 1,151 additions and 402 deletions.
1,120 changes: 1,024 additions & 96 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rust-version = "1.78"
crate-type = ["cdylib"]

[dependencies]
app-surface = { version = "1.3.3", features = ["web_rwh"] }
raw-window-handle = "0.6"
wgpu = "23"
bevy = { version = "0.15", features = [
Expand All @@ -22,11 +23,11 @@ bevy = { version = "0.15", features = [
uuid = { version = "1.7.0", features = ["v4"] }
rand = "0.8"
# Web only
wasm-bindgen = "0.2.97"
wasm-bindgen-futures = "0.4.47"
wasm-bindgen = "0.2.99"
wasm-bindgen-futures = "0.4.49"
console_error_panic_hook = "0.1.7"
console_log = "1"
web-sys = { version = "0.3.74", features = [
web-sys = { version = "0.3.76", features = [
"Window",
"Document",
"Element",
Expand All @@ -41,7 +42,7 @@ web-sys = { version = "0.3.74", features = [
"BlobPropertyBag",
"Url",
] }
js-sys = "0.3.74"
js-sys = "0.3.76"

[profile.wasm-release]
inherits = "release"
Expand Down
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Implemented `CanvasViewPlugin` to replace `bevy_winit`, allowing the Bevy engine
# Add Rust WebAssembly target
rustup target add wasm32-unknown-unknown
# Install wasm-bindgen command line tool
cargo install -f wasm-bindgen-cli --version 0.2.97
cargo install -f wasm-bindgen-cli --version 0.2.99

# Run:
# First install the http server
Expand Down
180 changes: 90 additions & 90 deletions public/bevy_in_main_thread.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/bevy_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ impl Deref for CurrentVolume {
}

/// 更新 aabb
#[allow(clippy::type_complexity)]
fn update_aabbes(
mut commands: Commands,
mut config_store: ResMut<GizmoConfigStore>,
Expand Down
203 changes: 0 additions & 203 deletions src/canvas_view/canvas.rs

This file was deleted.

6 changes: 4 additions & 2 deletions src/canvas_view/canvas_view_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{canvas::*, CanvasViews};
use super::*;
use bevy::app::{App, Plugin};
use bevy::ecs::{
entity::Entity,
Expand Down Expand Up @@ -54,7 +54,9 @@ pub fn create_canvas_window(app: &mut App) {

// Update resolution of bevy window
window.resolution.set_scale_factor(scale_factor);
window.resolution.set(logical_res.0, logical_res.1);
window
.resolution
.set(logical_res.0 as f32, logical_res.1 as f32);

let raw_window_wrapper = match app_view {
ViewObj::Canvas(window_wrapper) => RawHandleWrapper::new(window_wrapper),
Expand Down
2 changes: 1 addition & 1 deletion src/canvas_view/canvas_views.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::canvas::*;
use super::*;
use bevy::ecs::entity::Entity;
use bevy::utils::HashMap;

Expand Down
24 changes: 22 additions & 2 deletions src/canvas_view/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use uuid::Uuid;
use app_surface::{CanvasWrapper, OffscreenCanvasWrapper};
use bevy::window::WindowWrapper;

pub(crate) use app_surface::{Canvas, OffscreenCanvas};

mod canvas_view_plugin;
pub(crate) use canvas_view_plugin::*;

pub(crate) mod canvas;

mod canvas_views;
use canvas_views::CanvasViews;

Expand All @@ -16,3 +18,21 @@ impl WindowId {
WindowId(Uuid::new_v4())
}
}

// 封装 ViewObj 来同时支持 Canvas 与 Offscreen
#[derive(Debug)]
pub enum ViewObj {
Canvas(WindowWrapper<CanvasWrapper>),
Offscreen(WindowWrapper<OffscreenCanvasWrapper>),
}

impl ViewObj {
pub fn from_canvas(canvas: Canvas) -> Self {
ViewObj::Canvas(WindowWrapper::new(CanvasWrapper::new(canvas)))
}

pub fn from_offscreen_canvas(canvas: OffscreenCanvas) -> Self {
ViewObj::Offscreen(WindowWrapper::new(OffscreenCanvasWrapper::new(canvas)))
}
}

4 changes: 2 additions & 2 deletions src/web_ffi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::bevy_app::init_app;
use crate::{canvas::*, canvas_view, create_canvas_window, ActiveInfo, WorkerApp};
use crate::{canvas_view::*, create_canvas_window, ActiveInfo, WorkerApp};
use bevy::app::PluginsState;
use bevy::ecs::system::SystemState;
use bevy::prelude::*;
Expand Down Expand Up @@ -36,7 +36,7 @@ extern "C" {
pub fn init_bevy_app() -> u64 {
let mut app = init_app();
// 添加自定义的 canvas 窗口插件
app.add_plugins(canvas_view::CanvasViewPlugin);
app.add_plugins(CanvasViewPlugin);

info!("init_bevy_app");

Expand Down
2 changes: 1 addition & 1 deletion zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
# 添加 Rust WebAssembly target
rustup target add wasm32-unknown-unknown
# 安装 wasm-bindgen 命令行工具
cargo install -f wasm-bindgen-cli --version 0.2.97
cargo install -f wasm-bindgen-cli --version 0.2.99

# 运行:
# 先安装 http server
Expand Down

0 comments on commit 79c8201

Please sign in to comment.