From c510d4a406218c38e1146503473e9e10c282a56d Mon Sep 17 00:00:00 2001 From: tom barrett Date: Tue, 18 Jun 2019 07:23:10 -0500 Subject: much better way of receiving xml --- src/tileset.rs | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) (limited to 'src/tileset.rs') diff --git a/src/tileset.rs b/src/tileset.rs index 21e31d1..d4e652f 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -1,7 +1,7 @@ use ggez::filesystem::File; use ggez::graphics::Rect; -use std::io::BufReader; -use xml::reader::{EventReader, XmlEvent}; + +use crate::xmlelements::XMLElements; pub struct Tileset { pub tiles: Vec, @@ -11,29 +11,18 @@ pub struct Tileset { impl Tileset { pub fn new(file: File) -> Tileset { - let mut tile_width = None; - let mut tile_height = None; - let mut columns = None; - let mut height = None; - - for e in EventReader::new(BufReader::new(file)) { - if let Ok(XmlEvent::StartElement { attributes, .. }) = e { - for attribute in attributes { - match attribute.name.local_name.as_str() { - "columns" => columns = Some(attribute.value.parse::().unwrap()), - "tilewidth" => tile_width = Some(attribute.value.parse::().unwrap()), - "tileheight" => tile_height = Some(attribute.value.parse::().unwrap()), - "height" => height = Some(attribute.value.parse::().unwrap()), - _ => (), - } - } - } - } - - let columns = columns.unwrap(); - let tile_height = tile_height.unwrap(); - let tile_width = tile_width.unwrap(); - let height = height.unwrap(); + let elements = XMLElements::new(file); + + let columns = elements + .get_element_attribute("tileset", "columns") + .unwrap(); + let height = elements.get_element_attribute("image", "height").unwrap(); + let tile_width = elements + .get_element_attribute("tileset", "tilewidth") + .unwrap() as f32; + let tile_height = elements + .get_element_attribute("tileset", "tileheight") + .unwrap() as f32; let rows = height / (tile_height as usize); -- cgit v1.2.3