use std::rc::Rc;
use crate::{
animation::{AnimSchedule, Animation},
context::WgpuContext,
render::primitives::{RenderInstance, RenderInstances},
RanimTimeline,
};
pub mod camera_frame;
pub mod svg_item;
pub mod vitem;
pub struct Group<T> {
pub items: Vec<T>,
}
pub struct RabjectGroup<'t, T> {
pub rabjects: Vec<Rabject<'t, T>>,
}
impl<'r, 't: 'r, T> RabjectGroup<'t, T> {
pub fn lagged_anim(
&'r mut self,
lag_ratio: f32,
anim_builder: impl FnOnce(&'r mut Rabject<'t, T>) -> AnimSchedule<'r, 't, T> + Clone,
) -> Vec<AnimSchedule<'r, 't, T>> {
let n = self.rabjects.len();
let mut anim_schedules = self
.rabjects
.iter_mut()
.map(|rabject| (anim_builder.clone())(rabject))
.collect::<Vec<_>>();
let duration = anim_schedules[0].anim.duration_secs;
let lag_time = duration * lag_ratio;
anim_schedules
.iter_mut()
.enumerate()
.for_each(|(i, schedule)| {
schedule.anim.padding = (i as f32 * lag_time, (n - i - 1) as f32 * lag_time);
});
anim_schedules
}
}
pub struct Rabject<'t, T> {
pub timeline: &'t RanimTimeline,
pub id: usize,
pub data: T,
}
impl<T> Drop for Rabject<'_, T> {
fn drop(&mut self) {
self.timeline.hide(self);
}
}
impl<'t, T: 'static> Rabject<'t, T> {
pub fn schedule<'r>(
&'r mut self,
anim_builder: impl FnOnce(&mut Self) -> Animation<T>,
) -> AnimSchedule<'r, 't, T> {
let animation = (anim_builder)(self);
AnimSchedule::new(self, animation)
}
}
pub trait Entity {
fn get_render_instance_for_entity<'a>(
&self,
render_instances: &'a RenderInstances,
entity_id: usize,
) -> Option<&'a dyn RenderInstance>;
fn prepare_render_instance_for_entity(
&self,
ctx: &WgpuContext,
render_instances: &mut RenderInstances,
entity_id: usize,
);
}
impl<T: Entity + 'static> Entity for Rc<T> {
fn get_render_instance_for_entity<'a>(
&self,
render_instances: &'a RenderInstances,
entity_id: usize,
) -> Option<&'a dyn RenderInstance> {
self.as_ref()
.get_render_instance_for_entity(render_instances, entity_id)
}
fn prepare_render_instance_for_entity(
&self,
ctx: &WgpuContext,
render_instances: &mut RenderInstances,
entity_id: usize,
) {
self.as_ref()
.prepare_render_instance_for_entity(ctx, render_instances, entity_id);
}
}
pub trait Blueprint<T> {
fn build(self) -> T;
}
pub trait Updatable {
fn update_from(&mut self, other: &Self);
}
impl<T: Clone> Updatable for T {
fn update_from(&mut self, other: &Self) {
*self = other.clone();
}
}