use crate::model::mdl;
use crate::model::mdl::is_zero;
use crate::model::mdx;
use crate::model::FixedText;
use crate::model::Model;
use crate::model::ModelVersion;
use crate::model::TexturesChunk;
use crate::model::ValueError;
use bitfield::bitfield;
bitfield! {
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, mdx::Read, mdx::Write, mdl::Read, mdl::Write)]
#[mdl(flags(WrapWidth = 1, WrapHeight = 2))]
pub struct TextureFlags(u32);
pub bits, _: 31, 0;
pub wrap_width, set_wrap_width: 0;
pub wrap_height, set_wrap_height: 1;
}
const PATH_SIZE: usize = 260;
#[derive(Clone, Debug, Eq, PartialEq, mdx::Read, mdx::Write, mdl::Read, mdl::Write)]
#[mdl(block = "Bitmap", write_order(path, replaceable_id, flags))]
pub struct Texture {
#[mdl(property = "ReplaceableId", default, skip_if = "is_zero")]
pub replaceable_id: u32,
#[mdl(property = "Image", default)]
pub path: FixedText<PATH_SIZE>,
#[mdl(flatten)]
pub flags: TextureFlags,
}
impl Texture {
pub fn new(path: &str) -> Result<Self, ValueError> {
let mut texture = Self {
replaceable_id: 0,
path: FixedText::default(),
flags: TextureFlags::default(),
};
texture.path.set_text(path)?;
Ok(texture)
}
}
impl<V: ModelVersion> Model<V> {
pub fn textures(&self) -> Vec<Texture> {
self.collect_chunk_records::<TexturesChunk>()
}
pub fn set_textures(&mut self, textures: &[Texture]) {
self.replace_chunk(TexturesChunk::new(textures.to_vec()));
}
}