Skip to content

Commit

Permalink
Adds macro to implement RadiantTessellatable
Browse files Browse the repository at this point in the history
  • Loading branch information
codenikel committed Nov 3, 2023
1 parent afcd5c8 commit cc7ac8d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 63 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"crates/core",
"crates/image",
"crates/text",
"crates/macros",
"crates/path",
"crates/runtime",
"crates/winit",
Expand Down
13 changes: 13 additions & 0 deletions crates/macros/Cargo.toml
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"
63 changes: 63 additions & 0 deletions crates/macros/src/lib.rs
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()
}
1 change: 1 addition & 0 deletions crates/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ epaint = { version = "0.22.0", features = ["bytemuck", "serde"] }
pollster = "0.3"
futures-intrusive = "0.5"
radiant-core = { path = "../core" }
radiant-macros = { path = "../macros" }
radiant-image-node = { path = "../image" }
radiant-text-node = { path = "../text" }
radiant-path-node = { path = "../path" }
Expand Down
65 changes: 2 additions & 63 deletions crates/runtime/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use radiant_core::{
};
use radiant_core::{RadiantDocumentNode, RadiantGroupNode};
use radiant_image_node::RadiantImageNode;
use radiant_macros::RadiantTessellatable;
use radiant_path_node::RadiantPathNode;
use radiant_text_node::RadiantTextNode;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(RadiantTessellatable)]
pub enum RadiantNodeType {
Document(RadiantDocumentNode<RadiantNodeType>),
Artboard(RadiantGroupNode<RadiantNodeType>),
Expand All @@ -19,69 +21,6 @@ pub enum RadiantNodeType {
Text(RadiantTextNode),
}

impl RadiantTessellatable for RadiantNodeType {
fn attach(&mut self, screen_descriptor: &ScreenDescriptor) {
match self {
RadiantNodeType::Document(node) => node.attach(screen_descriptor),
RadiantNodeType::Artboard(node) => node.attach(screen_descriptor),
RadiantNodeType::Rectangle(node) => node.attach(screen_descriptor),
RadiantNodeType::Path(node) => node.attach(screen_descriptor),
RadiantNodeType::Image(node) => node.attach(screen_descriptor),
RadiantNodeType::Text(node) => node.attach(screen_descriptor),
}
}

fn detach(&mut self) {
match self {
RadiantNodeType::Document(node) => node.detach(),
RadiantNodeType::Artboard(node) => node.detach(),
RadiantNodeType::Rectangle(node) => node.detach(),
RadiantNodeType::Path(node) => node.detach(),
RadiantNodeType::Image(node) => node.detach(),
RadiantNodeType::Text(node) => node.detach(),
}
}

fn set_needs_tessellation(&mut self) {
match self {
RadiantNodeType::Document(node) => node.set_needs_tessellation(),
RadiantNodeType::Artboard(node) => node.set_needs_tessellation(),
RadiantNodeType::Rectangle(node) => node.set_needs_tessellation(),
RadiantNodeType::Path(node) => node.set_needs_tessellation(),
RadiantNodeType::Image(node) => node.set_needs_tessellation(),
RadiantNodeType::Text(node) => node.set_needs_tessellation(),
}
}

fn tessellate(
&mut self,
selection: bool,
screen_descriptor: &ScreenDescriptor,
fonts_manager: &epaint::text::Fonts,
) -> Vec<ClippedPrimitive> {
match self {
RadiantNodeType::Document(node) => {
node.tessellate(selection, screen_descriptor, fonts_manager)
}
RadiantNodeType::Artboard(node) => {
node.tessellate(selection, screen_descriptor, fonts_manager)
}
RadiantNodeType::Rectangle(node) => {
node.tessellate(selection, screen_descriptor, fonts_manager)
}
RadiantNodeType::Path(node) => {
node.tessellate(selection, screen_descriptor, fonts_manager)
}
RadiantNodeType::Image(node) => {
node.tessellate(selection, screen_descriptor, fonts_manager)
}
RadiantNodeType::Text(node) => {
node.tessellate(selection, screen_descriptor, fonts_manager)
}
}
}
}

impl RadiantNode for RadiantNodeType {
fn get_id(&self) -> u64 {
match self {
Expand Down

0 comments on commit cc7ac8d

Please sign in to comment.