Skip to content

Commit

Permalink
Adds ability to combine messages
Browse files Browse the repository at this point in the history
  • Loading branch information
codenikel committed Nov 3, 2023
1 parent 25464b3 commit 492dcc7
Show file tree
Hide file tree
Showing 19 changed files with 265 additions and 126 deletions.
133 changes: 133 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ cgmath = "0.18"
serde = { version = "1.0", features = ["derive"] }
epaint = { version = "0.22.0", features = ["bytemuck", "serde"] }
pollster = "0.3"
futures-intrusive = "0.5"
futures-intrusive = "0.5"
macro_magic = "0.5.0"
2 changes: 2 additions & 0 deletions crates/core/src/interactions/interaction_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{BoundingBoxInteraction, RadiantNode, ScreenDescriptor};
use epaint::ClippedPrimitive;
use serde::{Deserialize, Serialize};
use macro_magic::export_tokens;

#[export_tokens]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum InteractionMessage {
TransformNode {
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/tools/rectangle_tool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use serde::{Deserialize, Serialize};

use crate::RadiantTool;
use macro_magic::export_tokens;

#[export_tokens]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RectangleToolMessage {
AddNode {
Expand Down
8 changes: 6 additions & 2 deletions crates/core/src/tools/selection_tool.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use crate::RadiantTool;
use serde::{Deserialize, Serialize};
use macro_magic::export_tokens;

#[export_tokens]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum SelectionToolMessage {
SelectNode(u64),
SelectNode {
id: u64,
},
TransformNode {
id: u64,
position: [f32; 2],
Expand All @@ -29,7 +33,7 @@ impl<M: From<SelectionToolMessage>> RadiantTool<M> for SelectionTool {
fn on_mouse_down(&mut self, node_id: u64, _position: [f32; 2]) -> Option<M> {
if node_id > 0 {
self.active_node_id = Some(node_id - 1);
let message = SelectionToolMessage::SelectNode(node_id - 1);
let message = SelectionToolMessage::SelectNode { id: node_id - 1 };
Some(message.into())
} else {
None
Expand Down
1 change: 1 addition & 0 deletions crates/image/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ epaint = { version = "0.22.0", features = ["bytemuck", "serde"] }
pollster = "0.3"
futures-intrusive = "0.5"
radiant-core = { path = "../core" }
macro_magic = "0.5.0"
2 changes: 2 additions & 0 deletions crates/image/src/image_message.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};
use macro_magic::export_tokens;

#[export_tokens]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RadiantImageMessage {
AddImage {
Expand Down
2 changes: 2 additions & 0 deletions crates/image/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod image_node;
pub mod image_message;

pub use image_node::*;
pub use image_message::*;
2 changes: 2 additions & 0 deletions crates/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ proc-macro = true
proc-macro2 = "1.0.69"
quote = "1.0.33"
syn = "2.0.38"
macro_magic = { version = "0.5.0", features = ["proc_support"] }
proc-utils = "0"
66 changes: 66 additions & 0 deletions crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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)?;
Expand Down Expand Up @@ -121,6 +122,60 @@ 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> {
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<_>>();

foreign_enum.variants.iter().for_each(|variant| {
if local_enum.variants.iter().any(|local_variant| local_variant.ident == variant.ident) {
return;
}
local_enum.variants.push(variant.clone());
});

let res = quote! {
#local_enum

impl From<#foreign_path> for #local_name {
fn from(foreign: #foreign_path) -> Self {
match foreign {
#(
#foreign_path::#foreign_variants { #(#foreign_args,)* } => Self::#foreign_variants { #(#foreign_args,)* },
)*
}
}
}

impl TryInto<#foreign_path> for #local_name {
type Error = ();

fn try_into(self) -> Result<#foreign_path, Self::Error> {
match self {
#(
Self::#foreign_variants { #(#foreign_args,)* } => Ok(#foreign_path::#foreign_variants { #(#foreign_args,)* }),
)*
_ => Err(()),
}
}
}
};

// use proc_utils::*;
// res.pretty_print();

Ok(res)
}

#[proc_macro_derive(RadiantTessellatable)]
pub fn derive_tessellatable(item: TokenStream) -> TokenStream {
let res = match derive_tessellatable_internal(item.into()) {
Expand All @@ -146,4 +201,15 @@ pub fn derive_component_provider(item: TokenStream) -> TokenStream {
Err(err) => err.to_compile_error(),
};
res.into()
}

#[import_tokens_attr]
#[proc_macro_attribute]
pub fn combine_enum(attr: TokenStream, item: TokenStream) -> TokenStream {
let foreign_path = syn::parse::<syn::Path>(__source_path).unwrap();
let res = match combine_enum_internal(attr.into(), item.into(), foreign_path) {
Ok(res) => res,
Err(err) => err.to_compile_error(),
};
res.into()
}
1 change: 1 addition & 0 deletions crates/path/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ epaint = { version = "0.22.0", features = ["bytemuck", "serde"] }
pollster = "0.3"
futures-intrusive = "0.5"
radiant-core = { path = "../core" }
macro_magic = "0.5.0"
Loading

0 comments on commit 492dcc7

Please sign in to comment.