-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds macro to implement RadiantTessellatable
- Loading branch information
Showing
6 changed files
with
90 additions
and
63 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "radiant-macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0.69" | ||
quote = "1.0.33" | ||
syn = "2.0.38" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use proc_macro::TokenStream; | ||
use proc_macro2::TokenStream as TokenStream2; | ||
use quote::quote; | ||
|
||
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 res = quote! { | ||
impl RadiantTessellatable for #name { | ||
fn attach(&mut self, screen_descriptor: &ScreenDescriptor) { | ||
match self { | ||
#( | ||
#name::#node_names(node) => node.attach(screen_descriptor), | ||
)* | ||
} | ||
} | ||
|
||
fn detach(&mut self) { | ||
match self { | ||
#( | ||
#name::#node_names(node) => node.detach(), | ||
)* | ||
} | ||
} | ||
|
||
fn set_needs_tessellation(&mut self) { | ||
match self { | ||
#( | ||
#name::#node_names(node) => node.set_needs_tessellation(), | ||
)* | ||
} | ||
} | ||
|
||
fn tessellate( | ||
&mut self, | ||
selection: bool, | ||
screen_descriptor: &ScreenDescriptor, | ||
fonts_manager: &epaint::text::Fonts, | ||
) -> Vec<ClippedPrimitive> { | ||
match self { | ||
#( | ||
#name::#node_names(node) => node.tessellate(selection, screen_descriptor, fonts_manager), | ||
)* | ||
} | ||
} | ||
} | ||
}; | ||
Ok(res) | ||
} | ||
|
||
#[proc_macro_derive(RadiantTessellatable)] | ||
pub fn derive_tessellatable(item: TokenStream) -> TokenStream { | ||
let res = match derive_tessellatable_internal(item.into()) { | ||
Ok(res) => res, | ||
Err(err) => err.to_compile_error(), | ||
}; | ||
res.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters