Preview App
use ranim::{
    anims::transform::TransformAnim,
    color,
    color::palettes::manim,
    glam::DVec3,
    items::{
        Group,
        vitem::{VItem, geometry::Square},
    },
    prelude::*,
    utils::rate_functions::linear,
};
use ranim_core::animation::StaticAnim;

#[scene]
#[output(dir = "perspective_blend")]
fn perspective_blend(r: &mut RanimScene) {
    let mut cam = CameraFrame::default();
    let r_cam = r.insert(cam.clone());

    // Update camera's state and show the new state
    cam.pos = DVec3::Z * 5.0;
    r.timeline_mut(r_cam).play(cam.show());

    // Create a cube
    let side_length = 2.0;

    let square_with_color = |color: color::AlphaColor<color::Srgb>| {
        VItem::from(Square::new(side_length).with(|square| {
            square.set_color(color).set_fill_opacity(0.5);
        }))
    };

    // bottom, right, back, top, front, left
    let mut square_faces = [
        manim::TEAL_C,
        manim::GREEN_C,
        manim::BLUE_C,
        manim::PURPLE_C,
        manim::RED_C,
        manim::YELLOW_C,
    ]
    .map(|color| {
        let face = square_with_color(color);
        (r.insert(face.clone()), face)
    });

    let transform_fns: [&dyn Fn(&mut VItem); 6] = [
        &(|data| {
            data.shift(DVec3::NEG_Y * side_length / 2.0)
                .rotate(std::f64::consts::PI / 2.0, DVec3::X);
        }),
        &(|data| {
            data.shift(DVec3::X * side_length / 2.0)
                .rotate(std::f64::consts::PI / 2.0, DVec3::Y);
        }),
        &(|data| {
            data.shift(DVec3::NEG_Z * side_length / 2.0);
        }),
        &(|data| {
            data.shift(DVec3::Y * side_length / 2.0)
                .rotate(-std::f64::consts::PI / 2.0, DVec3::X);
        }),
        &(|data| {
            data.shift(DVec3::Z * side_length / 2.0);
        }),
        &(|data| {
            data.shift(DVec3::NEG_X * side_length / 2.0)
                .rotate(-std::f64::consts::PI / 2.0, DVec3::Y);
        }),
    ];

    square_faces
        .iter_mut()
        .zip(transform_fns)
        .for_each(|((r_face, face), transform_fn)| {
            r.timeline_mut(*r_face)
                .play(face.transform(transform_fn).with_rate_func(linear))
                .hide();
        });

    let faces = square_faces.map(|(_, face)| face);
    let mut faces = Group(faces.to_vec());

    let r_faces = r.new_timeline();
    r.timelines_mut().sync(); // TODO: make this better
    r.timeline_mut(r_faces).play(
        faces
            .transform(|data| {
                data.rotate(std::f64::consts::PI / 6.0, DVec3::Y)
                    .rotate(std::f64::consts::PI / 6.0, DVec3::X);
            })
            .with_duration(4.0),
    );

    r.timeline_mut(r_cam).forward(2.0).play(
        cam.transform(|data| {
            data.perspective_blend = 1.0;
        })
        .with_duration(2.0),
    );
    r.insert_time_mark(
        r.timelines().max_total_secs(),
        TimeMark::Capture("preview.png".to_string()),
    );
}