1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::io::{self, Read, Seek, SeekFrom};

use byteorder::{LittleEndian, ReadBytesExt};
use num_enum::CustomTryInto;

use crate::color::RGB256;
use crate::helpers::read_string;

#[derive(Eq, PartialEq, CustomTryInto)]
#[repr(u8)]
pub enum LoopAnimationDirection {
	Forward = 0,
	Reverse = 1,
	PingPong = 2,
}

pub struct Tag {
	pub from_tag: u16,
	pub to_tag: u16,
	pub loop_animation_direction: LoopAnimationDirection,
	pub tag_color: RGB256,
	pub tag_name: String,
}

pub struct FrameTagsChunk {
	pub number_of_tags: u16,
	pub tags: Vec<Tag>,
}

impl FrameTagsChunk {
	pub fn from_read<R>(read: &mut R) -> io::Result<Self>
	where
		R: Read + Seek,
	{
		let number_of_tags = read.read_u16::<LittleEndian>()?;
		let mut tags = Vec::with_capacity(number_of_tags as usize);

		for _ in 0..number_of_tags {
			let from_tag = read.read_u16::<LittleEndian>()?;
			let to_tag = read.read_u16::<LittleEndian>()?;
			let loop_animation_direction = read
				.read_u8()?
				.try_into_LoopAnimationDirection()
				.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
			read.seek(SeekFrom::Current(8))?;
			let tag_color = RGB256 {
				r: read.read_u8()?,
				g: read.read_u8()?,
				b: read.read_u8()?,
			};
			read.seek(SeekFrom::Current(1))?;
			let tag_name = read_string(read)?;

			tags.push(Tag {
				from_tag,
				to_tag,
				loop_animation_direction,
				tag_color,
				tag_name,
			});
		}

		Ok(Self {
			number_of_tags,
			tags,
		})
	}
}