1use super::{TiledError, HandleXml, helper::*, LayerData, Properties};
2
3use quick_xml::{events::{BytesStart, attributes::Attribute}, Reader};
4
5use std::path::PathBuf;
6
7pub struct ImageLayer {
8 pub image_path : PathBuf,
9 pub width: u32,
10 pub height: u32,
11 pub repeat_x: bool,
12 pub repeat_y: bool,
13 pub info: LayerData,
14 pub props: Properties,
15}
16
17impl ImageLayer {
18 fn blank() -> ImageLayer {
19 ImageLayer {
20 image_path: PathBuf::new(),
21 width: 0,
22 height: 0,
23 repeat_x: false,
24 repeat_y: false,
25 info: LayerData::new(),
26 props: Properties::blank(),
27 }
28 }
29 pub(crate) fn new(attribs : Vec<Attribute>, reader : &mut Reader<&[u8]>, layer_index: u32) -> Result<ImageLayer, TiledError> {
30 let mut img_layer = Self::blank();
31 img_layer.parse_base_attribs(attribs)?;
32 parse_xml(&mut img_layer, reader)?;
33 img_layer.info.layer_position = layer_index;
34 Ok(img_layer)
35 }
36
37 fn parse_base_attribs(&mut self, attribs : Vec<Attribute>) -> Result<(), TiledError> {
38 for a in attribs {
39 if let Some(()) = self.info.handle_attrib(&a)? {
40 match a.key.as_ref() {
41 b"repeatx" => self.repeat_x = get_value::<i32>(&a.value)? == 1,
42 b"repeaty" => self.repeat_y = get_value::<i32>(&a.value)? == 1,
43 _ => println!("warning: unrecognized atrribute {:?}", a.key),
44 }
45 }
46 }
47 Ok(())
48 }
49
50 fn parse_image_attributes(&mut self, attribs : Vec<Attribute>) -> Result<(), TiledError> {
51 for a in attribs {
52 match a.key.as_ref() {
53 b"source" => self.image_path.push(get_string(&a.value)?),
54 b"width" => self.width = get_value(&a.value)?,
55 b"height" => self.height = get_value(&a.value)?,
56 _ => println!("warning: unrecognized atrribute {:?}", a.key),
57 }
58 }
59 Ok(())
60 }
61
62}
63
64impl HandleXml for ImageLayer {
65 fn empty(&mut self, e : &BytesStart) -> Result<(), TiledError> {
66 match e.name().as_ref() {
67 b"image" => self.parse_image_attributes(collect_attribs(&e)?)?,
68 _ => println!("unrecognized empty tag {:?}", e.name()),
69 }
70 Ok(())
71 }
72 fn start(&mut self, e : &BytesStart, reader: &mut Reader<&[u8]>) -> Result<(), TiledError> {
73 match e.name().as_ref() {
74 b"properties" => parse_xml(&mut self.props, reader)?,
75 _ => println!("unrecognized tag {:?}", e.name()),
76 }
77 Ok(())
78 }
79 fn self_tag() -> &'static str {
80 "imagelayer"
81 }
82}