diff --git a/crates/core/src/interactions/interaction_manager.rs b/crates/core/src/interactions/interaction_manager.rs index 9e383a8..1f7c46e 100644 --- a/crates/core/src/interactions/interaction_manager.rs +++ b/crates/core/src/interactions/interaction_manager.rs @@ -1,7 +1,7 @@ use crate::{BoundingBoxInteraction, RadiantNode, ScreenDescriptor}; use epaint::ClippedPrimitive; -use serde::{Deserialize, Serialize}; use macro_magic::export_tokens; +use serde::{Deserialize, Serialize}; #[export_tokens] #[derive(Serialize, Deserialize, Debug, Clone)] diff --git a/crates/core/src/tools/rectangle_tool.rs b/crates/core/src/tools/rectangle_tool.rs index 7c6771c..c67554a 100644 --- a/crates/core/src/tools/rectangle_tool.rs +++ b/crates/core/src/tools/rectangle_tool.rs @@ -1,6 +1,6 @@ -use serde::{Deserialize, Serialize}; use crate::RadiantTool; use macro_magic::export_tokens; +use serde::{Deserialize, Serialize}; #[export_tokens] #[derive(Serialize, Deserialize, Debug, Clone)] diff --git a/crates/core/src/tools/selection_tool.rs b/crates/core/src/tools/selection_tool.rs index 8b936cd..e308662 100644 --- a/crates/core/src/tools/selection_tool.rs +++ b/crates/core/src/tools/selection_tool.rs @@ -1,11 +1,11 @@ use crate::RadiantTool; -use serde::{Deserialize, Serialize}; use macro_magic::export_tokens; +use serde::{Deserialize, Serialize}; #[export_tokens] #[derive(Serialize, Deserialize, Debug, Clone)] pub enum SelectionToolMessage { - SelectNode { + SelectNode { id: u64, }, TransformNode { diff --git a/crates/image/src/image_message.rs b/crates/image/src/image_message.rs index 1d2747a..9ed69dd 100644 --- a/crates/image/src/image_message.rs +++ b/crates/image/src/image_message.rs @@ -1,12 +1,8 @@ -use serde::{Deserialize, Serialize}; use macro_magic::export_tokens; +use serde::{Deserialize, Serialize}; #[export_tokens] #[derive(Serialize, Deserialize, Debug, Clone)] pub enum RadiantImageMessage { - AddImage { - name: String, - path: String, - }, + AddImage { name: String, path: String }, } - diff --git a/crates/image/src/lib.rs b/crates/image/src/lib.rs index e3177f7..b5a11e4 100644 --- a/crates/image/src/lib.rs +++ b/crates/image/src/lib.rs @@ -1,5 +1,5 @@ -pub mod image_node; pub mod image_message; +pub mod image_node; -pub use image_node::*; pub use image_message::*; +pub use image_node::*; diff --git a/crates/macros/src/lib.rs b/crates/macros/src/lib.rs index 49169c9..8401a31 100644 --- a/crates/macros/src/lib.rs +++ b/crates/macros/src/lib.rs @@ -1,15 +1,17 @@ +use macro_magic::import_tokens_attr; use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use quote::quote; -use macro_magic::import_tokens_attr; fn derive_tessellatable_internal(item: TokenStream2) -> syn::Result { let item = syn::parse2::(item)?; let name = item.ident.clone(); - let node_names = item.variants.iter().map(|variant| { - variant.ident.clone() - }).collect::>(); + let node_names = item + .variants + .iter() + .map(|variant| variant.ident.clone()) + .collect::>(); let res = quote! { impl RadiantTessellatable for #name { @@ -58,9 +60,11 @@ fn derive_node_internal(item: TokenStream2) -> syn::Result { let item = syn::parse2::(item)?; let name = item.ident.clone(); - let node_names = item.variants.iter().map(|variant| { - variant.ident.clone() - }).collect::>(); + let node_names = item + .variants + .iter() + .map(|variant| variant.ident.clone()) + .collect::>(); let res = quote! { impl RadiantNode for #name { @@ -96,9 +100,11 @@ fn derive_component_provider_internal(item: TokenStream2) -> syn::Result(item)?; let name = item.ident.clone(); - let node_names = item.variants.iter().map(|variant| { - variant.ident.clone() - }).collect::>(); + let node_names = item + .variants + .iter() + .map(|variant| variant.ident.clone()) + .collect::>(); let res = quote! { impl RadiantComponentProvider for #name { @@ -122,22 +128,38 @@ fn derive_component_provider_internal(item: TokenStream2) -> syn::Result syn::Result { +fn combine_enum_internal( + attr: TokenStream2, + item: TokenStream2, + foreign_path: syn::Path, +) -> syn::Result { let mut local_enum = syn::parse2::(item.clone())?; let local_name = local_enum.ident.clone(); let foreign_enum = syn::parse2::(attr)?; - let foreign_variants = foreign_enum.variants.iter().map(|variant| { - variant.ident.clone() - }).collect::>(); - let foreign_args = foreign_enum.variants.iter().map(|variant| { - variant.fields.iter().map(|field| { - field.ident.clone() - }).collect::>() - }).collect::>(); + let foreign_variants = foreign_enum + .variants + .iter() + .map(|variant| variant.ident.clone()) + .collect::>(); + let foreign_args = foreign_enum + .variants + .iter() + .map(|variant| { + variant + .fields + .iter() + .map(|field| field.ident.clone()) + .collect::>() + }) + .collect::>(); foreign_enum.variants.iter().for_each(|variant| { - if local_enum.variants.iter().any(|local_variant| local_variant.ident == variant.ident) { + if local_enum + .variants + .iter() + .any(|local_variant| local_variant.ident == variant.ident) + { return; } local_enum.variants.push(variant.clone()); @@ -158,7 +180,7 @@ fn combine_enum_internal(attr: TokenStream2, item: TokenStream2, foreign_path: s impl TryInto<#foreign_path> for #local_name { type Error = (); - + fn try_into(self) -> Result<#foreign_path, Self::Error> { match self { #( @@ -212,4 +234,4 @@ pub fn combine_enum(attr: TokenStream, item: TokenStream) -> TokenStream { Err(err) => err.to_compile_error(), }; res.into() -} \ No newline at end of file +} diff --git a/crates/path/src/path_tool.rs b/crates/path/src/path_tool.rs index 49b7aa0..d19c386 100644 --- a/crates/path/src/path_tool.rs +++ b/crates/path/src/path_tool.rs @@ -1,13 +1,13 @@ +use macro_magic::export_tokens; use radiant_core::RadiantTool; use serde::{Deserialize, Serialize}; -use macro_magic::export_tokens; // Todo: This is a stub. Implement the PathTool. #[export_tokens] #[derive(Serialize, Deserialize, Debug, Clone)] pub enum PathToolMessage { - SelectNode { - id: u64 + SelectNode { + id: u64, }, TransformNode { id: u64, diff --git a/crates/runtime/src/message.rs b/crates/runtime/src/message.rs index bc9b25f..fe93c32 100644 --- a/crates/runtime/src/message.rs +++ b/crates/runtime/src/message.rs @@ -1,8 +1,8 @@ use epaint::Color32; use radiant_macros::combine_enum; -use serde::{Deserialize, Serialize}; use crate::RadiantNodeType; +use serde::{Deserialize, Serialize}; #[combine_enum(radiant_core::RectangleToolMessage)] #[combine_enum(radiant_core::SelectionToolMessage)] @@ -12,7 +12,7 @@ use crate::RadiantNodeType; #[combine_enum(radiant_path_node::PathToolMessage)] #[derive(Serialize, Deserialize, Debug, Clone)] pub enum RadiantMessage { - AddArtboard { }, + AddArtboard {}, SelectArtboard { id: u64, }, diff --git a/crates/runtime/src/node.rs b/crates/runtime/src/node.rs index 849162b..9e923e6 100644 --- a/crates/runtime/src/node.rs +++ b/crates/runtime/src/node.rs @@ -10,8 +10,15 @@ use radiant_path_node::RadiantPathNode; use radiant_text_node::RadiantTextNode; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug, Clone)] -#[derive(RadiantComponentProvider, RadiantNode, RadiantTessellatable)] +#[derive( + Serialize, + Deserialize, + Debug, + Clone, + RadiantComponentProvider, + RadiantNode, + RadiantTessellatable, +)] pub enum RadiantNodeType { Document(RadiantDocumentNode), Artboard(RadiantGroupNode), diff --git a/crates/runtime/src/runtime.rs b/crates/runtime/src/runtime.rs index fbc9982..5eb4b11 100644 --- a/crates/runtime/src/runtime.rs +++ b/crates/runtime/src/runtime.rs @@ -10,26 +10,30 @@ use crate::{RadiantMessage, RadiantNodeType, RadiantResponse}; pub struct RadiantRuntime { pub app: RadiantApp, - pub handler: Box, } impl RadiantRuntime { - pub async fn new(handler: Box) -> Self { + pub async fn new() -> Self { Self { app: RadiantApp::new(SelectionTool::new()).await, - handler, } } +} + +impl Runtime for RadiantRuntime { + fn app(&mut self) -> &mut RadiantApp { + &mut self.app + } - pub fn handle_message(&mut self, message: RadiantMessage) -> Option { + fn handle_message(&mut self, message: RadiantMessage) -> Option { match message { - RadiantMessage::AddArtboard { }=> { + RadiantMessage::AddArtboard {} => { self.app.scene.document.add_artboard(); } - RadiantMessage::SelectArtboard{ id } => { + RadiantMessage::SelectArtboard { id } => { self.app.scene.document.set_active_artboard(id); } - RadiantMessage::SelectNode{ id } => { + RadiantMessage::SelectNode { id } => { if !self.app.scene.interaction_manager.is_interaction(id) { self.app.scene.document.select(id); if let Some(node) = self.app.scene.document.get_node(id) { @@ -161,26 +165,3 @@ impl RadiantRuntime { None } } - -impl RadiantRuntime { - pub fn process_message(&mut self, message: RadiantMessage) { - let response = self.handle_message(message); - self.handle_response(response); - } - - fn handle_response(&self, response: Option) { - if let Some(response) = response { - (self.handler)(response); - } - } -} - -impl Runtime for RadiantRuntime { - fn app(&mut self) -> &mut RadiantApp { - &mut self.app - } - - fn handle_runtime_message(&mut self, message: RadiantMessage) -> Option { - self.handle_message(message) - } -} diff --git a/crates/text/src/lib.rs b/crates/text/src/lib.rs index ad72dec..c78bbb4 100644 --- a/crates/text/src/lib.rs +++ b/crates/text/src/lib.rs @@ -1,5 +1,5 @@ -pub mod text_node; pub mod text_message; +pub mod text_node; -pub use text_node::*; pub use text_message::*; +pub use text_node::*; diff --git a/crates/text/src/text_message.rs b/crates/text/src/text_message.rs index b1ef494..eb21167 100644 --- a/crates/text/src/text_message.rs +++ b/crates/text/src/text_message.rs @@ -1,12 +1,8 @@ -use serde::{Deserialize, Serialize}; use macro_magic::export_tokens; +use serde::{Deserialize, Serialize}; #[export_tokens] #[derive(Serialize, Deserialize, Debug, Clone)] pub enum RadiantTextMessage { - AddText { - text: String, - position: [f32; 2], - }, + AddText { text: String, position: [f32; 2] }, } - diff --git a/crates/winit/src/lib.rs b/crates/winit/src/lib.rs index b486ed4..ca86dd3 100644 --- a/crates/winit/src/lib.rs +++ b/crates/winit/src/lib.rs @@ -264,20 +264,23 @@ impl + TryInto, N: RadiantNode> pub trait Runtime { fn app(&mut self) -> &mut RadiantApp; - fn handle_runtime_message(&mut self, message: M) -> Option; + fn handle_message(&mut self, message: M) -> Option; } pub fn run_native< M: From + TryInto + 'static, N: RadiantNode + 'static, - R, + R: 'static, >( mut runtime: impl Runtime + 'static, + handler: Box, ) { if let Some(event_loop) = std::mem::replace(&mut runtime.app().event_loop, None) { event_loop.run(move |event, _, control_flow| { if let Some(message) = runtime.app().handle_event(&event, control_flow) { - runtime.handle_runtime_message(message); + if let Some(response) = runtime.handle_message(message) { + handler(response); + } } match event { @@ -316,7 +319,7 @@ pub fn run_wasm< if let Some(runtime) = weak_runtime.upgrade() { if let Ok(mut runtime) = runtime.write() { if let Some(message) = runtime.app().handle_event(&event, control_flow) { - if let Some(response) = runtime.handle_runtime_message(message) { + if let Some(response) = runtime.handle_message(message) { let this = JsValue::null(); let _ = f.call1(&this, &serde_wasm_bindgen::to_value(&response).unwrap()); diff --git a/examples/basic/src/main.rs b/examples/basic/src/main.rs index 27354ba..098cfa0 100644 --- a/examples/basic/src/main.rs +++ b/examples/basic/src/main.rs @@ -14,7 +14,7 @@ async fn run() { println!("Response: {:?}", response); }); - let mut runtime = RadiantRuntime::new(handler).await; + let mut runtime = RadiantRuntime::new().await; runtime .app .scene @@ -31,7 +31,7 @@ async fn run() { [400.0, 400.0], ))); - run_native(runtime); + run_native(runtime, handler); } fn main() { diff --git a/examples/egui/src/main.rs b/examples/egui/src/main.rs index 474e66a..e7a6384 100644 --- a/examples/egui/src/main.rs +++ b/examples/egui/src/main.rs @@ -52,7 +52,7 @@ async fn run() { println!("Response: {:?}", response); }); - let mut runtime = RadiantRuntime::new(handler).await; + let mut runtime = RadiantRuntime::new().await; runtime .app .scene @@ -112,7 +112,9 @@ async fn run() { event_loop.run(move |event, _, control_flow| { if demo_app.pending_messages.len() > 0 { for message in demo_app.pending_messages.drain(..) { - runtime.handle_message(message); + if let Some(response) = runtime.handle_message(message) { + handler(response); + } } }