From 2fe4c28e42363d9ad1dc07adcbc1c08527f6bc09 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta Date: Sun, 5 Nov 2023 11:59:16 +0530 Subject: [PATCH] Adds generics for View and Runtime --- crates/core/src/lib.rs | 18 ++++++++++++++++++ crates/runtime/src/lib.rs | 1 - crates/runtime/src/runtime.rs | 14 ++++++++++---- crates/winit/src/lib.rs | 32 ++++++++++++++++++-------------- examples/basic/src/main.rs | 8 +++++--- 5 files changed, 51 insertions(+), 22 deletions(-) diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 1fedde4..e350c4d 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -32,3 +32,21 @@ impl ScreenDescriptor { ] } } + +pub trait View + TryInto, N: RadiantNode> { + fn scene(&self) -> &RadiantScene; + fn scene_mut(&mut self) -> &mut RadiantScene; +} + +pub trait Runtime<'a, M: From + TryInto, N: RadiantNode, R: 'a> { + type View: View; + + fn view(&self) -> &Self::View; + fn view_mut(&mut self) -> &mut Self::View; + + fn handle_message(&mut self, message: M) -> Option; + + fn scene(&'a self) -> &RadiantScene { self.view().scene() } + fn scene_mut(&'a mut self) -> &mut RadiantScene { self.view_mut().scene_mut() } + // fn add(&'a mut self, node: N) { self.scene_mut().add(node); } +} diff --git a/crates/runtime/src/lib.rs b/crates/runtime/src/lib.rs index e8df859..3888e4f 100644 --- a/crates/runtime/src/lib.rs +++ b/crates/runtime/src/lib.rs @@ -12,7 +12,6 @@ pub use radiant_path_node::RadiantPathNode; pub use radiant_text_node::RadiantTextNode; pub use radiant_winit::run_native; -pub use radiant_winit::Runtime; #[cfg(target_arch = "wasm32")] pub mod wasm; diff --git a/crates/runtime/src/runtime.rs b/crates/runtime/src/runtime.rs index a1bd3aa..f7f0f4f 100644 --- a/crates/runtime/src/runtime.rs +++ b/crates/runtime/src/runtime.rs @@ -1,10 +1,10 @@ use radiant_core::{ ColorComponent, RadiantComponentProvider, RadiantRectangleNode, RadiantTessellatable, - RadiantTransformable, SelectionTool, TransformComponent, + RadiantTransformable, SelectionTool, TransformComponent, Runtime, }; use radiant_image_node::RadiantImageNode; use radiant_text_node::RadiantTextNode; -use radiant_winit::{RadiantView, Runtime}; +use radiant_winit::RadiantView; use crate::{RadiantMessage, RadiantNodeType, RadiantResponse}; @@ -20,8 +20,14 @@ impl RadiantRuntime { } } -impl Runtime for RadiantRuntime { - fn view(&mut self) -> &mut RadiantView { +impl Runtime<'_, RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRuntime { + type View = RadiantView; + + fn view(&self) -> &RadiantView { + &self.view + } + + fn view_mut(&mut self) -> &mut RadiantView { &mut self.view } diff --git a/crates/winit/src/lib.rs b/crates/winit/src/lib.rs index ba7fa0f..cb4020e 100644 --- a/crates/winit/src/lib.rs +++ b/crates/winit/src/lib.rs @@ -1,4 +1,4 @@ -use radiant_core::{InteractionMessage, RadiantNode, RadiantScene, RadiantTool, ScreenDescriptor}; +use radiant_core::{InteractionMessage, RadiantNode, RadiantScene, RadiantTool, ScreenDescriptor, Runtime, View}; use winit::event_loop::EventLoop; use winit::window::{Window, WindowBuilder}; use winit::{event::*, event_loop::ControlFlow}; @@ -262,12 +262,16 @@ impl + TryInto, N: RadiantNode> } } -pub trait Runtime + TryInto, N: RadiantNode, R> { - fn view(&mut self) -> &mut RadiantView; - fn handle_message(&mut self, message: M) -> Option; +impl + TryInto, N: RadiantNode> View + for RadiantView +{ + fn scene(&self) -> &RadiantScene { + &self.scene + } - fn scene(&mut self) -> &mut RadiantScene { &mut self.view().scene } - fn add(&mut self, node: N) { self.scene().add(node); } + fn scene_mut(&mut self) -> &mut RadiantScene { + &mut self.scene + } } pub fn run_native< @@ -275,12 +279,12 @@ pub fn run_native< N: RadiantNode + 'static, R: 'static, >( - mut runtime: impl Runtime + 'static, + mut runtime: impl Runtime<'static, M, N, R, View = RadiantView> + 'static, handler: Box, ) { - if let Some(event_loop) = std::mem::replace(&mut runtime.view().event_loop, None) { + if let Some(event_loop) = std::mem::replace(&mut runtime.view_mut().event_loop, None) { event_loop.run(move |event, _, control_flow| { - if let Some(message) = runtime.view().handle_event(&event, control_flow) { + if let Some(message) = runtime.view_mut().handle_event(&event, control_flow) { if let Some(response) = runtime.handle_message(message) { handler(response); } @@ -289,7 +293,7 @@ pub fn run_native< match event { RedrawRequested(..) => { let output_frame = std::mem::replace( - &mut runtime.view().scene.render_manager.current_texture, + &mut runtime.view_mut().scene_mut().render_manager.current_texture, None, ); output_frame.unwrap().present(); @@ -304,15 +308,15 @@ pub fn run_native< pub fn run_wasm< M: From + TryInto + 'static, N: RadiantNode + 'static, - R: serde::ser::Serialize, + R: serde::ser::Serialize + 'static, >( - runtime: Arc + 'static>>, + runtime: Arc> + 'static>>, f: js_sys::Function, ) { let event_loop; { let Ok(mut runtime_) = runtime.write() else { return; }; - event_loop = std::mem::replace(&mut runtime_.view().event_loop, None); + event_loop = std::mem::replace(&mut runtime_.view_mut().event_loop, None); } let weak_runtime = Arc::downgrade(&runtime); @@ -321,7 +325,7 @@ pub fn run_wasm< event_loop.spawn(move |event, _, control_flow| { if let Some(runtime) = weak_runtime.upgrade() { if let Ok(mut runtime) = runtime.write() { - if let Some(message) = runtime.view().handle_event(&event, control_flow) { + if let Some(message) = runtime.view_mut().handle_event(&event, control_flow) { if let Some(response) = runtime.handle_message(message) { let this = JsValue::null(); let _ = diff --git a/examples/basic/src/main.rs b/examples/basic/src/main.rs index 382ebbd..f510f3e 100644 --- a/examples/basic/src/main.rs +++ b/examples/basic/src/main.rs @@ -1,6 +1,6 @@ use radiant_runtime::{ run_native, RadiantPathNode, RadiantRectangleNode, RadiantResponse, - RadiantRuntime, Runtime, + RadiantRuntime, }; async fn run() { @@ -16,14 +16,16 @@ async fn run() { let mut runtime = RadiantRuntime::new().await; runtime - .scene() + .view + .scene .add(RadiantRectangleNode::new( 1, [100.0, 100.0], [200.0, 200.0], ).into()); runtime - .scene() + .view + .scene .add(RadiantPathNode::new( 2, [400.0, 400.0],