Skip to content

Commit

Permalink
cursor move and big improvements on rerender times
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharktheone committed Dec 26, 2024
1 parent c74f56d commit 4bfce15
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/gosub_instance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
27 changes: 18 additions & 9 deletions crates/gosub_instance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C: ModuleConfiguration> {
pub title: String,
Expand All @@ -37,7 +38,7 @@ impl<C: ModuleConfiguration> EngineInstance<C> {
id: InstanceId,
handles: Handles<C>,
) -> 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?;

Expand All @@ -56,7 +57,7 @@ impl<C: ModuleConfiguration> EngineInstance<C> {
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(),
Expand All @@ -77,7 +78,7 @@ impl<C: ModuleConfiguration> EngineInstance<C> {
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();
Expand Down Expand Up @@ -181,17 +182,25 @@ impl<C: ModuleConfiguration> EngineInstance<C> {
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);
}
Expand Down Expand Up @@ -235,13 +244,13 @@ impl<C: ModuleConfiguration> EventLoopHandle<C> for El<C> {
impl<C: ModuleConfiguration> El<C> {
fn send(&self, message: InternalInstanceMessage<C>) {
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);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_interface/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait TreeDrawer<C: HasDrawComponents> {
type ImgCache: ImgCache<C::RenderBackend>;

fn draw(&mut self, size: SizeU32, el: &impl EventLoopHandle<C>) -> <C::RenderBackend as RenderBackend>::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(
Expand Down
4 changes: 2 additions & 2 deletions crates/gosub_renderer/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
8 changes: 1 addition & 7 deletions examples/vello-renderer/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ impl<C: ModuleConfiguration> 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, .. } => {
Expand Down
1 change: 0 additions & 1 deletion examples/vello-renderer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ impl<C: HasRenderBackend> Clone for WinitEventLoopHandle<C> {
impl<C: HasRenderBackend> ChromeHandle<C> for WinitEventLoopHandle<C> {
type WindowId = WindowId;
fn draw_scene(&self, scene: <C::RenderBackend as RenderBackend>::Scene, size: SizeU32, instance: InstanceId) {

let _ = self.proxy.send_event(CustomEventInternal::DrawScene(scene, size, instance, self.window));
}

Expand Down

0 comments on commit 4bfce15

Please sign in to comment.