From 2205944b1d3a0d3986820d5c204725e219cac884 Mon Sep 17 00:00:00 2001 From: Speykious Date: Thu, 15 Feb 2024 02:03:56 +0100 Subject: [PATCH] Merge example and wasm scene --- examples/common/Cargo.toml | 4 ++ examples/common/src/scene.rs | 4 ++ examples/render-webgl/Cargo.toml | 3 +- examples/render-webgl/src/main.rs | 11 +--- examples/render-webgl/src/scene.rs | 94 ------------------------------ 5 files changed, 12 insertions(+), 104 deletions(-) delete mode 100644 examples/render-webgl/src/scene.rs diff --git a/examples/common/Cargo.toml b/examples/common/Cargo.toml index 88b322f..0bb8ff7 100644 --- a/examples/common/Cargo.toml +++ b/examples/common/Cargo.toml @@ -8,4 +8,8 @@ edition = "2021" [dependencies] glam = "0.25.0" inox2d = { path = "../../inox2d" } +tracing = "0.1.40" winit = "0.29" + +[target.'cfg(target_arch = "wasm32")'.dependencies] +web-time = "1.0.0" diff --git a/examples/common/src/scene.rs b/examples/common/src/scene.rs index 0efcdae..ec173f9 100644 --- a/examples/common/src/scene.rs +++ b/examples/common/src/scene.rs @@ -1,7 +1,11 @@ //! A nice scene controller to smoothly move around in the window. +#[cfg(not(target_arch = "wasm32"))] use std::time::Instant; +#[cfg(target_arch = "wasm32")] +use web_time::Instant; + use glam::{vec2, Vec2}; use inox2d::math::camera::Camera; use winit::event::{ElementState, MouseScrollDelta, WindowEvent}; diff --git a/examples/render-webgl/Cargo.toml b/examples/render-webgl/Cargo.toml index 1ade467..4e9a352 100644 --- a/examples/render-webgl/Cargo.toml +++ b/examples/render-webgl/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] inox2d = { path = "../../inox2d" } inox2d-opengl = { path = "../../inox2d-opengl" } +common = { path = "../common" } console_error_panic_hook = "0.1.7" glam = "0.25.0" @@ -13,11 +14,9 @@ glow = "0.13.1" js-sys = "0.3.64" reqwest = "0.11.18" tracing = "0.1.37" -tracing-subscriber = "0.3.16" tracing-wasm = "0.2.1" wasm-bindgen = "0.2.87" wasm-bindgen-futures = "0.4.37" -web-time = "1.0.0" winit = "0.29" [dependencies.web-sys] diff --git a/examples/render-webgl/src/main.rs b/examples/render-webgl/src/main.rs index af8b65d..b62e17f 100644 --- a/examples/render-webgl/src/main.rs +++ b/examples/render-webgl/src/main.rs @@ -1,6 +1,3 @@ -#[cfg(target_arch = "wasm32")] -mod scene; - #[cfg(target_arch = "wasm32")] fn create_window(event: &winit::event_loop::EventLoop<()>) -> Result { use winit::dpi::PhysicalSize; @@ -54,7 +51,7 @@ async fn run() -> Result<(), Box> { use winit::event::{Event, WindowEvent}; use winit::platform::web::EventLoopExtWebSys; - use crate::scene::WasmSceneController; + use common::scene::ExampleSceneController; let events = winit::event_loop::EventLoop::new().unwrap(); let window = create_window(&events)?; @@ -99,7 +96,7 @@ async fn run() -> Result<(), Box> { renderer.camera.scale = Vec2::splat(0.15); info!("Inox2D renderer initialized"); - let scene_ctrl = WasmSceneController::new(&renderer.camera, 0.5); + let scene_ctrl = ExampleSceneController::new(&renderer.camera, 0.5); // Refcells because we need to make our own continuous animation loop. // Winit won't help us :( @@ -149,9 +146,7 @@ async fn run() -> Result<(), Box> { window.request_redraw(); } WindowEvent::CloseRequested => elwt.exit(), - _ => scene_ctrl - .borrow_mut() - .interact(&window, event, &renderer.borrow().camera), + _ => (scene_ctrl.borrow_mut()).interact(event, &renderer.borrow().camera), }, Event::AboutToWait => { window.request_redraw(); diff --git a/examples/render-webgl/src/scene.rs b/examples/render-webgl/src/scene.rs deleted file mode 100644 index 6888433..0000000 --- a/examples/render-webgl/src/scene.rs +++ /dev/null @@ -1,94 +0,0 @@ -//! A nice scene controller to smoothly move around in the window. - -use glam::{vec2, Vec2}; -use inox2d::math::camera::Camera; -use web_time::Instant; -use winit::event::{ElementState, MouseScrollDelta, WindowEvent}; -use winit::window::Window; - -pub struct WasmSceneController { - // for camera position and mouse interactions - camera_pos: Vec2, - mouse_pos: Vec2, - mouse_pos_held: Vec2, - mouse_state: ElementState, - - // for smooth scrolling - pub scroll_speed: f32, - hard_scale: Vec2, - - // for FPS-independent interactions - start: Instant, - prev_elapsed: f32, - current_elapsed: f32, -} - -impl WasmSceneController { - pub fn new(camera: &Camera, scroll_speed: f32) -> Self { - Self { - camera_pos: camera.position, - mouse_pos: Vec2::default(), - mouse_pos_held: Vec2::default(), - mouse_state: ElementState::Released, - scroll_speed, - hard_scale: camera.scale, - start: Instant::now(), - prev_elapsed: 0.0, - current_elapsed: 0.0, - } - } - - pub fn update(&mut self, camera: &mut Camera) { - // Smooth scrolling - let time_delta = self.current_elapsed - self.prev_elapsed; - camera.scale = camera.scale + time_delta.powf(0.6) * (self.hard_scale - camera.scale); - - // Mouse dragging - if self.mouse_state == ElementState::Pressed { - camera.position = self.camera_pos + (self.mouse_pos - self.mouse_pos_held) / camera.scale; - } - - // Frame interval - self.prev_elapsed = self.current_elapsed; - self.current_elapsed = self.start.elapsed().as_secs_f32(); - } - - pub fn interact(&mut self, window: &Window, event: &WindowEvent, camera: &Camera) { - match event { - WindowEvent::CursorMoved { position, .. } => { - self.mouse_pos = vec2(position.x as f32, position.y as f32); - - if self.mouse_state == ElementState::Pressed { - window.request_redraw(); - } - } - WindowEvent::MouseInput { state, .. } => { - self.mouse_state = *state; - if self.mouse_state == ElementState::Pressed { - self.mouse_pos_held = self.mouse_pos; - self.camera_pos = camera.position; - } - } - WindowEvent::MouseWheel { delta, .. } => { - // Handle mouse wheel (zoom) - let my = match delta { - MouseScrollDelta::LineDelta(_, y) => *y * 12., - MouseScrollDelta::PixelDelta(pos) => pos.y as f32 * 0.1, - }; - - self.hard_scale *= 2_f32.powf(self.scroll_speed * my * 0.1); - - window.request_redraw(); - } - _ => (), - } - } - - pub fn dt(&self) -> f32 { - self.current_elapsed - self.prev_elapsed - } - - pub fn current_elapsed(&self) -> f32 { - self.current_elapsed - } -}