Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract scene into separate crate #798

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 0 additions & 103 deletions macroquad_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,106 +229,3 @@ pub fn test(_attr: TokenStream, item: TokenStream) -> TokenStream {

prelude
}

/// Very experimental thing for macroquad::experimantal::scene
/// Maybe will go away in future versions
#[doc(hidden)]
#[proc_macro_derive(CapabilityTrait)]
pub fn capability_trait_macro(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let mut source = input.into_iter().peekable();

while let Some(TokenTree::Punct(_)) = source.peek() {
let _ = source.next();
let _ = next_group(&mut source);
}
assert_eq!("pub", &format!("{}", source.next().unwrap()));
assert_eq!("struct", &format!("{}", source.next().unwrap()));
let struct_name = format!("{}", source.next().unwrap());

let mut group = next_group(&mut source)
.unwrap()
.stream()
.into_iter()
.peekable();

let mut trait_decl = format!("pub trait {}Trait {{", struct_name);
let mut trait_impl = format!("impl {}Trait for NodeWith<{}> {{", struct_name, struct_name);

fn next_str(group: &mut Peekable<impl Iterator<Item = TokenTree>>) -> Option<String> {
group.next().map(|tok| format!("{}", tok))
}

loop {
// skip doc comments
while let Some(TokenTree::Punct(_)) = group.peek() {
let _ = group.next();
let _ = next_group(&mut group);
}

let _pub = next_str(&mut group);
if _pub.is_none() {
break;
}
assert_eq!("pub", _pub.unwrap());
let fn_name = next_str(&mut group).unwrap();
let mut fn_res = "()".to_string();
assert_eq!(":", &next_str(&mut group).unwrap());
assert_eq!("fn", &next_str(&mut group).unwrap());
let fn_args_decl = next_str(&mut group).unwrap();
let mut fn_args_impl = String::new();

let args = fn_args_decl.split(":").collect::<Vec<&str>>();
for arg in &args[1..args.len() - 1] {
fn_args_impl.push_str(&format!(", {}", arg.split(", ").last().unwrap()));
}
let p = next_str(&mut group);
match p.as_deref() {
Some("-") => {
assert_eq!(">", next_str(&mut group).unwrap());
fn_res = next_str(&mut group).unwrap();
let _ = next_str(&mut group);
}
Some(",") => {}
None => break,
_ => panic!(),
};

trait_decl.push_str(&format!(
"fn {} {} -> {};",
fn_name,
fn_args_decl
.replace("node : HandleUntyped", "&self")
.replace("node: HandleUntyped", "&self"),
fn_res
));

let args = fn_args_impl
.replace("node : HandleUntyped", "")
.replace("node: HandleUntyped", "")
.replace("(", "")
.replace(")", "");

trait_impl.push_str(&format!(
"fn {} {} -> {} {{",
fn_name,
fn_args_decl
.replace("node : HandleUntyped", "&self")
.replace("node: HandleUntyped", "&self"),
fn_res
));
trait_impl.push_str(&format!(
"(self.capability.{})(self.node {})",
fn_name, args
));
trait_impl.push_str("}");
}
trait_decl.push_str("}");
trait_impl.push_str("}");

let res = format!(
"{}
{}",
trait_decl, trait_impl
);
res.parse().unwrap()
}
1 change: 0 additions & 1 deletion src/experimental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ pub mod camera;
pub mod collections;
pub mod coroutines;
pub mod scene;
pub mod state_machine;
94 changes: 0 additions & 94 deletions src/experimental/coroutines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,97 +261,3 @@ pub fn wait_seconds(time: f32) -> TimerDelayFuture {
remaining_time: time,
}
}

/// Special built-in coroutines for modifying values over time.
pub mod tweens {
use crate::experimental::scene::{Handle, Lens, Node};
use std::future::Future;
use std::pin::Pin;
use std::{
ops::{Add, Mul, Sub},
task::{Context, Poll},
};

pub struct LinearTweenFuture<T>
where
T: Copy + Add<Output = T> + Sub<Output = T> + Mul<f32, Output = T>,
{
from: T,
to: T,
lens: Lens<T>,
start_time: f64,
time: f32,
}
impl<T> Unpin for LinearTweenFuture<T> where
T: Copy + Add<Output = T> + Sub<Output = T> + Mul<f32, Output = T>
{
}

impl<T> Future for LinearTweenFuture<T>
where
T: Copy + Add<Output = T> + Sub<Output = T> + Mul<f32, Output = T>,
{
type Output = ();

fn poll(self: Pin<&mut Self>, _: &mut Context) -> Poll<Self::Output> {
let t = (miniquad::date::now() - self.start_time) / self.time as f64;
let this = self.get_mut();
let var = this.lens.get();

// node with value was deleted
if var.is_none() {
return Poll::Ready(());
}
let var = var.unwrap();

if t <= 1. {
*var = this.from + (this.to - this.from) * t as f32;

Poll::Pending
} else {
*var = this.to;

Poll::Ready(())
}
}
}

pub fn linear<T, T1, F>(
handle: Handle<T1>,
lens: F,
from: T,
to: T,
time: f32,
) -> LinearTweenFuture<T>
where
T: Copy + Add<Output = T> + Sub<Output = T> + Mul<f32, Output = T>,
T1: Node,
F: for<'r> FnMut(&'r mut T1) -> &'r mut T,
{
LinearTweenFuture {
to,
from,
lens: handle.lens(lens),
time,
start_time: miniquad::date::now(),
}
}

pub async fn follow_path<T, T1, F>(handle: Handle<T1>, mut lens: F, path: Vec<T>, time: f32)
where
T: Copy + Add<Output = T> + Sub<Output = T> + Mul<f32, Output = T>,
T1: Node,
F: for<'r> FnMut(&'r mut T1) -> &'r mut T,
{
for point in path.windows(2) {
linear(
handle,
&mut lens,
point[0],
point[1],
time / path.len() as f32,
)
.await
}
}
}
Loading
Loading