From 4bfce1581ef14bb7f6e47475af4eacdcbe6e5540 Mon Sep 17 00:00:00 2001 From: Shark Date: Thu, 26 Dec 2024 20:09:12 +0100 Subject: [PATCH] cursor move and big improvements on rerender times --- Cargo.lock | 1 + crates/gosub_instance/Cargo.toml | 1 + crates/gosub_instance/src/lib.rs | 27 ++++++++++++++++++--------- crates/gosub_interface/src/draw.rs | 2 +- crates/gosub_renderer/src/draw.rs | 4 ++-- examples/vello-renderer/event_loop.rs | 8 +------- examples/vello-renderer/main.rs | 1 - 7 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 44b35fab..21370727 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1838,6 +1838,7 @@ dependencies = [ "gosub_interface", "gosub_net", "gosub_shared", + "log", "tokio", "url", ] diff --git a/crates/gosub_instance/Cargo.toml b/crates/gosub_instance/Cargo.toml index ff3fe902..ce639726 100644 --- a/crates/gosub_instance/Cargo.toml +++ b/crates/gosub_instance/Cargo.toml @@ -9,3 +9,4 @@ gosub_interface = { path = "../gosub_interface" } gosub_net = { path = "../gosub_net" } tokio = { version = "1.42.0", features = ["sync", "rt"] } url = "2.5.4" +log = "0.4.22" diff --git a/crates/gosub_instance/src/lib.rs b/crates/gosub_instance/src/lib.rs index 4945531f..4109f085 100644 --- a/crates/gosub_instance/src/lib.rs +++ b/crates/gosub_instance/src/lib.rs @@ -16,6 +16,7 @@ use tokio::task::LocalSet; use url::Url; use gosub_interface::input::InputEvent; use gosub_interface::layout::LayoutTree; +use log::info; pub struct EngineInstance { pub title: String, @@ -37,7 +38,7 @@ impl EngineInstance { id: InstanceId, handles: Handles, ) -> Result<(Self, InstanceHandle)> { - let (tx, rx) = tokio::sync::mpsc::channel(64); + let (tx, rx) = tokio::sync::mpsc::channel(128); let instance = EngineInstance::with_chan(url.clone(), layouter, rx, id, handles).await?; @@ -56,7 +57,7 @@ impl EngineInstance { let fetcher = Arc::new(Fetcher::new(url.clone())); let data = C::TreeDrawer::with_fetcher(url.clone(), fetcher.clone(), layouter, false).await?; - let (itx, irx) = tokio::sync::mpsc::channel(16); + let (itx, irx) = tokio::sync::mpsc::channel(128); Ok(EngineInstance { title: "Gosub".to_string(), @@ -77,7 +78,7 @@ impl EngineInstance { where C::Layouter: Send + 'static, { - let (tx, rx) = tokio::sync::mpsc::channel(64); + let (tx, rx) = tokio::sync::mpsc::channel(128); std::thread::spawn(move || { let rt = Builder::new_current_thread().enable_all().build().unwrap(); @@ -181,17 +182,25 @@ impl EngineInstance { self.data.scroll(delta); self.redraw(); } + InputEvent::MouseMove(point) => { + if self.data.mouse_move(point.x, point.y) { + self.redraw(); + } + } _ => {} } - + } } - + Ok(()) } - + fn redraw(&mut self) { + let now = std::time::Instant::now(); let scene = self.data.draw(self.size, &self.el); + let elapsed = now.elapsed(); + info!("Redraw took {:?}", elapsed); self.handles.chrome.draw_scene(scene, self.size, self.id); } @@ -235,13 +244,13 @@ impl EventLoopHandle for El { impl El { fn send(&self, message: InternalInstanceMessage) { let send = self.0.clone(); - - + + if let Ok(handle) = Handle::try_current() { handle.spawn(async move { let _ = send.send(message).await; }); - + } else { let _ = send.blocking_send(message); } diff --git a/crates/gosub_interface/src/draw.rs b/crates/gosub_interface/src/draw.rs index 76d94ca8..d749260a 100644 --- a/crates/gosub_interface/src/draw.rs +++ b/crates/gosub_interface/src/draw.rs @@ -13,7 +13,7 @@ pub trait TreeDrawer { type ImgCache: ImgCache; fn draw(&mut self, size: SizeU32, el: &impl EventLoopHandle) -> ::Scene; - fn mouse_move(&mut self, backend: &mut C::RenderBackend, x: FP, y: FP) -> bool; + fn mouse_move(&mut self, x: FP, y: FP) -> bool; fn scroll(&mut self, point: Point); fn from_url( diff --git a/crates/gosub_renderer/src/draw.rs b/crates/gosub_renderer/src/draw.rs index d7c15aad..e1ea0ccf 100644 --- a/crates/gosub_renderer/src/draw.rs +++ b/crates/gosub_renderer/src/draw.rs @@ -165,13 +165,13 @@ where if self.dirty { self.dirty = false; - el.redraw(); + // el.redraw(); } root_scene } - fn mouse_move(&mut self, _backend: &mut C::RenderBackend, x: FP, y: FP) -> bool { + fn mouse_move(&mut self, x: FP, y: FP) -> bool { let x = x - self.scene_transform.clone().unwrap_or(Transform::IDENTITY).tx(); let y = y - self.scene_transform.clone().unwrap_or(Transform::IDENTITY).ty(); diff --git a/examples/vello-renderer/event_loop.rs b/examples/vello-renderer/event_loop.rs index d6f72eaa..4cd22431 100644 --- a/examples/vello-renderer/event_loop.rs +++ b/examples/vello-renderer/event_loop.rs @@ -51,13 +51,7 @@ impl Window<'_, C> { return Ok(()); }; - - - // tab.tx.blocking_send(InstanceMessage::MouseMove(Point::new(position.x as FP, position.y as FP)))?; - // - // if tab.data.mouse_move(backend, position.x as FP, position.y as FP) { - // self.window.request_redraw(); - // } + tab.tx.blocking_send(InstanceMessage::Input(InputEvent::MouseMove(Point::new(position.x as FP, position.y as FP))))?; } WindowEvent::MouseWheel { delta, .. } => { diff --git a/examples/vello-renderer/main.rs b/examples/vello-renderer/main.rs index 08516bdc..8f9d31ed 100644 --- a/examples/vello-renderer/main.rs +++ b/examples/vello-renderer/main.rs @@ -91,7 +91,6 @@ impl Clone for WinitEventLoopHandle { impl ChromeHandle for WinitEventLoopHandle { type WindowId = WindowId; fn draw_scene(&self, scene: ::Scene, size: SizeU32, instance: InstanceId) { - let _ = self.proxy.send_event(CustomEventInternal::DrawScene(scene, size, instance, self.window)); }