use crate::common::M2Parse;
use crate::io_ext::{ReadExt, WriteExt};
use std::io::{Read, Seek, Write};
use crate::chunks::animation::M2AnimationBlock;
use crate::error::Result;
use crate::version::M2Version;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct M2Color {
pub r: f32,
pub g: f32,
pub b: f32,
}
impl Default for M2Color {
fn default() -> Self {
Self::white()
}
}
impl M2Parse for M2Color {
fn parse<R: Read + Seek>(reader: &mut R) -> Result<Self> {
M2Color::parse(reader)
}
fn write<W: Write>(&self, writer: &mut W) -> Result<()> {
self.write(writer)
}
}
impl M2Color {
pub fn parse<R: Read>(reader: &mut R) -> Result<Self> {
let r = reader.read_f32_le()?;
let g = reader.read_f32_le()?;
let b = reader.read_f32_le()?;
Ok(Self { r, g, b })
}
pub fn write<W: Write>(&self, writer: &mut W) -> Result<()> {
writer.write_f32_le(self.r)?;
writer.write_f32_le(self.g)?;
writer.write_f32_le(self.b)?;
Ok(())
}
pub fn new(r: f32, g: f32, b: f32) -> Self {
Self { r, g, b }
}
pub fn white() -> Self {
Self::new(1.0, 1.0, 1.0)
}
pub fn black() -> Self {
Self::new(0.0, 0.0, 0.0)
}
pub fn transparent() -> Self {
Self::new(0.0, 0.0, 0.0)
}
}
#[derive(Debug, Clone)]
pub struct M2ColorAnimation {
pub color: M2AnimationBlock<M2Color>,
pub alpha: M2AnimationBlock<u16>,
}
impl M2ColorAnimation {
pub fn parse<R: Read + Seek>(reader: &mut R) -> Result<Self> {
let color = M2AnimationBlock::parse(reader)?;
let alpha = M2AnimationBlock::parse(reader)?;
Ok(Self { color, alpha })
}
pub fn write<W: Write>(&self, writer: &mut W) -> Result<()> {
self.color.write(writer)?;
self.alpha.write(writer)?;
Ok(())
}
pub fn convert(&self, _target_version: M2Version) -> Self {
self.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn test_color_parse_write() {
let color = M2Color::new(0.5, 0.6, 0.7);
let mut data = Vec::new();
color.write(&mut data).unwrap();
let mut cursor = Cursor::new(data);
let parsed_color = M2Color::parse(&mut cursor).unwrap();
assert_eq!(parsed_color.r, 0.5);
assert_eq!(parsed_color.g, 0.6);
assert_eq!(parsed_color.b, 0.7);
}
}