Skip to content

Commit

Permalink
refactor: add back scene macro
Browse files Browse the repository at this point in the history
  • Loading branch information
YishiMichael committed Jan 26, 2025
1 parent 88157f3 commit e87b07e
Show file tree
Hide file tree
Showing 5 changed files with 475 additions and 288 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resolver = "2"
members = [
"morphing",
"morphing-macros",
"examples",
"winit-egui-wgpu-template",
]
13 changes: 13 additions & 0 deletions morphing-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "morphing-macros"
version = "0.1.0"
authors = ["YishiMichael"]
edition = "2021"

[lib]
proc-macro = true

[dependencies]
darling = "0.20.10"
quote = "1.0.38"
syn = "2.0.96"
40 changes: 40 additions & 0 deletions morphing-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use darling::FromMeta;
use proc_macro::TokenStream;

#[derive(FromMeta)]
struct SceneArgs {
override_settings: Option<syn::Path>,
}

#[proc_macro_attribute]
pub fn scene(input: TokenStream, tokens: TokenStream) -> TokenStream {
let args = match darling::ast::NestedMeta::parse_meta_list(input.into()) {
Ok(args) => match SceneArgs::from_list(&args) {
Ok(args) => args,
Err(error) => return TokenStream::from(error.write_errors()),
},
Err(error) => return TokenStream::from(darling::Error::from(error).write_errors()),
};
let scene_fn = syn::parse_macro_input!(tokens as syn::ItemFn);

let scene_name = scene_fn.sig.ident.clone();
let scene_settings = quote::format_ident!("__scene_settings");

let override_settings_stmt = if let Some(override_settings_path) = args.override_settings {
quote::quote! {
let #scene_settings = #override_settings_path(#scene_settings);
}
} else {
quote::quote! {}
};
let block = quote::quote! {
pub fn #scene_name(
#scene_settings: ::morphing::toplevel::settings::SceneSettings,
) -> ::morphing::toplevel::app::scene::SceneTimelines {
#scene_fn
#override_settings_stmt
::morphing::toplevel::app::scene::SceneTimelines::new(stringify!(#scene_name), #scene_settings, #scene_name)
}
};
block.into()
}
27 changes: 12 additions & 15 deletions morphing/src/timelines/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::Range;
pub trait Timeline:
'static + Debug + Send + Sync + serde_traitobject::Deserialize + serde_traitobject::Serialize
{
fn presentation(&self, device: &wgpu::Device) -> anyhow::Result<Box<dyn Presentation>>;
fn precut(&self, device: &wgpu::Device) -> anyhow::Result<Box<dyn Presentation>>;
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
Expand Down Expand Up @@ -32,16 +32,13 @@ impl TimelineEntries {
});
}

pub(crate) fn presentation(
&self,
device: &wgpu::Device,
) -> anyhow::Result<PresentationEntries> {
pub(crate) fn precut(&self, device: &wgpu::Device) -> anyhow::Result<PresentationEntries> {
self.0
.iter()
.map(|timeline_entry| {
Ok(PresentationEntry {
time_interval: timeline_entry.time_interval.clone(),
presentation: timeline_entry.timeline.presentation(device)?,
presentation: timeline_entry.timeline.precut(device)?,
})
})
.collect::<anyhow::Result<_>>()
Expand Down Expand Up @@ -107,7 +104,7 @@ pub mod steady {
where
M: Mobject,
{
fn presentation(&self, device: &wgpu::Device) -> anyhow::Result<Box<dyn Presentation>> {
fn precut(&self, device: &wgpu::Device) -> anyhow::Result<Box<dyn Presentation>> {
Ok(Box::new(SteadyTimelinePresentation {
realization: self.mobject.realize(device)?,
}))
Expand Down Expand Up @@ -174,7 +171,7 @@ pub mod dynamic {
{
type ContentPresentation: ContentPresentation;

fn content_presentation(
fn content_precut(
&self,
device: &wgpu::Device,
) -> anyhow::Result<Self::ContentPresentation>;
Expand Down Expand Up @@ -217,9 +214,9 @@ pub mod dynamic {
ME: DynamicTimelineMetric,
R: Rate,
{
fn presentation(&self, device: &wgpu::Device) -> anyhow::Result<Box<dyn Presentation>> {
fn precut(&self, device: &wgpu::Device) -> anyhow::Result<Box<dyn Presentation>> {
Ok(Box::new(DynamicTimelinePresentation {
content_presentation: self.content.content_presentation(device)?,
content_presentation: self.content.content_precut(device)?,
metric: self.metric.clone(),
rate: self.rate.clone(),
}))
Expand Down Expand Up @@ -278,7 +275,7 @@ pub mod dynamic {
{
type ContentPresentation = IndeterminedTimelineContentPresentation<M::Realization>;

fn content_presentation(
fn content_precut(
&self,
device: &wgpu::Device,
) -> anyhow::Result<Self::ContentPresentation> {
Expand Down Expand Up @@ -344,7 +341,7 @@ pub mod action {
{
type ContentPresentation = ActionTimelineContentPresentation<M::Realization, M, MD>;

fn content_presentation(
fn content_precut(
&self,
device: &wgpu::Device,
) -> anyhow::Result<Self::ContentPresentation> {
Expand Down Expand Up @@ -421,7 +418,7 @@ pub mod continuous {
{
type ContentPresentation = ContinuousTimelineContentPresentation<M::Realization, M, U>;

fn content_presentation(
fn content_precut(
&self,
device: &wgpu::Device,
) -> anyhow::Result<Self::ContentPresentation> {
Expand Down Expand Up @@ -498,12 +495,12 @@ pub mod discrete {
{
type ContentPresentation = DiscreteTimelineContentPresentation;

fn content_presentation(
fn content_precut(
&self,
device: &wgpu::Device,
) -> anyhow::Result<Self::ContentPresentation> {
Ok(DiscreteTimelineContentPresentation {
presentation_entries: self.timeline_entries.presentation(device)?,
presentation_entries: self.timeline_entries.precut(device)?,
})
}
}
Expand Down
Loading

0 comments on commit e87b07e

Please sign in to comment.