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, mdl::Read, mdl::Write)]
#[mdl(
block = "GeosetAnim",
default,
write_order(alpha, flags, geoset_id, color)
)]
pub struct GeosetAnimation {
#[mdl(property = "Alpha")]
pub alpha: Animatable<f32>,
#[mdl(flatten)]
pub flags: GeosetAnimationFlags,
#[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);
}
}
#[derive(mdx::Read, mdx::Write)]
#[mdx(sized(tag = GeosetAnimationsChunk::TAG))]
struct BinaryGeosetAnimation {
#[mdx(tag = *b"KGAO")]
alpha: Animatable<f32>,
flags: GeosetAnimationFlags,
#[mdx(tag = *b"KGAC")]
color: Animatable<Color>,
geoset_id: u32,
}
fn swap_base_color(color: &mut Animatable<Color>) {
if let Some(mut value) = color.value().copied() {
value.swap(0, 2);
color.set_value(value);
}
}
impl mdx::Read for GeosetAnimation {
fn read_mdx(cursor: &mut mdx::Cursor<'_>) -> Result<Self, mdx::ReadError> {
let mut value: BinaryGeosetAnimation = cursor.read()?;
swap_base_color(&mut value.color);
Ok(Self {
alpha: value.alpha,
flags: value.flags,
color: value.color,
geoset_id: value.geoset_id,
})
}
}
impl mdx::Write for GeosetAnimation {
fn write_mdx(&self, encoder: &mut mdx::Encoder<'_>) -> Result<(), mdx::WriteError> {
let mut color = self.color.clone();
swap_base_color(&mut color);
encoder.write(&BinaryGeosetAnimation {
alpha: self.alpha.clone(),
flags: self.flags,
color,
geoset_id: self.geoset_id,
})
}
}