Skip to content

Commit

Permalink
Restructures response handler
Browse files Browse the repository at this point in the history
  • Loading branch information
codenikel committed Nov 5, 2023
1 parent 492dcc7 commit 2c18565
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 87 deletions.
2 changes: 1 addition & 1 deletion crates/core/src/interactions/interaction_manager.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/tools/rectangle_tool.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/tools/selection_tool.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions crates/image/src/image_message.rs
Original file line number Diff line number Diff line change
@@ -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 },
}

4 changes: 2 additions & 2 deletions crates/image/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::*;
66 changes: 44 additions & 22 deletions crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<TokenStream2> {
let item = syn::parse2::<syn::ItemEnum>(item)?;

let name = item.ident.clone();
let node_names = item.variants.iter().map(|variant| {
variant.ident.clone()
}).collect::<Vec<_>>();
let node_names = item
.variants
.iter()
.map(|variant| variant.ident.clone())
.collect::<Vec<_>>();

let res = quote! {
impl RadiantTessellatable for #name {
Expand Down Expand Up @@ -58,9 +60,11 @@ fn derive_node_internal(item: TokenStream2) -> syn::Result<TokenStream2> {
let item = syn::parse2::<syn::ItemEnum>(item)?;

let name = item.ident.clone();
let node_names = item.variants.iter().map(|variant| {
variant.ident.clone()
}).collect::<Vec<_>>();
let node_names = item
.variants
.iter()
.map(|variant| variant.ident.clone())
.collect::<Vec<_>>();

let res = quote! {
impl RadiantNode for #name {
Expand Down Expand Up @@ -96,9 +100,11 @@ fn derive_component_provider_internal(item: TokenStream2) -> syn::Result<TokenSt
let item = syn::parse2::<syn::ItemEnum>(item)?;

let name = item.ident.clone();
let node_names = item.variants.iter().map(|variant| {
variant.ident.clone()
}).collect::<Vec<_>>();
let node_names = item
.variants
.iter()
.map(|variant| variant.ident.clone())
.collect::<Vec<_>>();

let res = quote! {
impl RadiantComponentProvider for #name {
Expand All @@ -122,22 +128,38 @@ fn derive_component_provider_internal(item: TokenStream2) -> syn::Result<TokenSt
Ok(res)
}

fn combine_enum_internal(attr: TokenStream2, item: TokenStream2, foreign_path: syn::Path) -> syn::Result<TokenStream2> {
fn combine_enum_internal(
attr: TokenStream2,
item: TokenStream2,
foreign_path: syn::Path,
) -> syn::Result<TokenStream2> {
let mut local_enum = syn::parse2::<syn::ItemEnum>(item.clone())?;
let local_name = local_enum.ident.clone();

let foreign_enum = syn::parse2::<syn::ItemEnum>(attr)?;
let foreign_variants = foreign_enum.variants.iter().map(|variant| {
variant.ident.clone()
}).collect::<Vec<_>>();
let foreign_args = foreign_enum.variants.iter().map(|variant| {
variant.fields.iter().map(|field| {
field.ident.clone()
}).collect::<Vec<_>>()
}).collect::<Vec<_>>();
let foreign_variants = foreign_enum
.variants
.iter()
.map(|variant| variant.ident.clone())
.collect::<Vec<_>>();
let foreign_args = foreign_enum
.variants
.iter()
.map(|variant| {
variant
.fields
.iter()
.map(|field| field.ident.clone())
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();

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());
Expand All @@ -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 {
#(
Expand Down Expand Up @@ -212,4 +234,4 @@ pub fn combine_enum(attr: TokenStream, item: TokenStream) -> TokenStream {
Err(err) => err.to_compile_error(),
};
res.into()
}
}
6 changes: 3 additions & 3 deletions crates/path/src/path_tool.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 2 additions & 2 deletions crates/runtime/src/message.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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,
},
Expand Down
11 changes: 9 additions & 2 deletions crates/runtime/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RadiantNodeType>),
Artboard(RadiantGroupNode<RadiantNodeType>),
Expand Down
41 changes: 11 additions & 30 deletions crates/runtime/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@ use crate::{RadiantMessage, RadiantNodeType, RadiantResponse};

pub struct RadiantRuntime {
pub app: RadiantApp<RadiantMessage, RadiantNodeType>,
pub handler: Box<dyn Fn(RadiantResponse)>,
}

impl RadiantRuntime {
pub async fn new(handler: Box<dyn Fn(RadiantResponse)>) -> Self {
pub async fn new() -> Self {
Self {
app: RadiantApp::new(SelectionTool::new()).await,
handler,
}
}
}

impl Runtime<RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRuntime {
fn app(&mut self) -> &mut RadiantApp<RadiantMessage, RadiantNodeType> {
&mut self.app
}

pub fn handle_message(&mut self, message: RadiantMessage) -> Option<RadiantResponse> {
fn handle_message(&mut self, message: RadiantMessage) -> Option<RadiantResponse> {
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) {
Expand Down Expand Up @@ -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<RadiantResponse>) {
if let Some(response) = response {
(self.handler)(response);
}
}
}

impl Runtime<RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRuntime {
fn app(&mut self) -> &mut RadiantApp<RadiantMessage, RadiantNodeType> {
&mut self.app
}

fn handle_runtime_message(&mut self, message: RadiantMessage) -> Option<RadiantResponse> {
self.handle_message(message)
}
}
4 changes: 2 additions & 2 deletions crates/text/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::*;
8 changes: 2 additions & 6 deletions crates/text/src/text_message.rs
Original file line number Diff line number Diff line change
@@ -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] },
}

11 changes: 7 additions & 4 deletions crates/winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,23 @@ impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode>

pub trait Runtime<M, N: RadiantNode, R> {
fn app(&mut self) -> &mut RadiantApp<M, N>;
fn handle_runtime_message(&mut self, message: M) -> Option<R>;
fn handle_message(&mut self, message: M) -> Option<R>;
}

pub fn run_native<
M: From<InteractionMessage> + TryInto<InteractionMessage> + 'static,
N: RadiantNode + 'static,
R,
R: 'static,
>(
mut runtime: impl Runtime<M, N, R> + 'static,
handler: Box<dyn Fn(R)>,
) {
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 {
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +31,7 @@ async fn run() {
[400.0, 400.0],
)));

run_native(runtime);
run_native(runtime, handler);
}

fn main() {
Expand Down
6 changes: 4 additions & 2 deletions examples/egui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 2c18565

Please sign in to comment.