use crate::model::Animatable;
use crate::model::Color;
use crate::model::GeosetAnimationsChunk;
use crate::model::KnownChunk;
use crate::model::Model;
use crate::model::ModelVersion;
use crate::model::{mdl, mdx};
use bitfield::bitfield;
bitfield! {
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, mdx::Read, mdx::Write, mdl::Read, mdl::Write)]
#[mdl(flags(DropShadow = 1), allow_bits = 2)]
pub struct GeosetAnimationFlags(u32);
pub bits, _: 31, 0;
pub drop_shadow, set_drop_shadow: 0;
pub color, set_color: 1;
}
#[derive(Clone, Debug, PartialEq, mdx::Read, mdx::Write, mdl::Read, mdl::Write)]
#[mdx(sized(tag = GeosetAnimationsChunk::TAG))]
#[mdl(
block = "GeosetAnim",
default,
write_order(alpha, flags, geoset_id, color)
)]
pub struct GeosetAnimation {
#[mdx(tag = *b"KGAO")]
#[mdl(property = "Alpha")]
pub alpha: Animatable<f32>,
#[mdl(flatten)]
pub flags: GeosetAnimationFlags,
#[mdx(tag = *b"KGAC")]
#[mdl(
property = "Color",
enabled_if = "Self::uses_color",
enable_with = "Self::enable_color"
)]
pub color: Animatable<Color>,
#[mdl(property = "GeosetId", required)]
pub geoset_id: u32,
}
impl Default for GeosetAnimation {
fn default() -> Self {
Self {
alpha: Animatable::Static(1.0),
flags: GeosetAnimationFlags::default(),
color: Animatable::Static([1.0; 3]),
geoset_id: 0,
}
}
}
impl GeosetAnimation {
pub fn new(geoset_id: u32) -> Self {
Self {
geoset_id,
..Self::default()
}
}
}
impl<V: ModelVersion> Model<V> {
pub fn geoset_animations(&self) -> Vec<GeosetAnimation> {
self.collect_chunk_records::<GeosetAnimationsChunk>()
}
pub fn set_geoset_animations(&mut self, animations: &[GeosetAnimation]) {
self.replace_chunk(GeosetAnimationsChunk::new(animations.to_vec()));
}
}
impl GeosetAnimation {
fn uses_color(&self) -> bool {
self.flags.color()
}
fn enable_color(&mut self) {
self.flags.set_color(true);
}
}