From 53b4ccdcc6cdac3b02480a97a2ec30439114ff6c Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 26 Jun 2019 05:50:34 -0500 Subject: now can parse tile properties --- src/xmlelements.rs | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 4 deletions(-) (limited to 'src/xmlelements.rs') diff --git a/src/xmlelements.rs b/src/xmlelements.rs index 86d7c77..bf9a213 100644 --- a/src/xmlelements.rs +++ b/src/xmlelements.rs @@ -2,9 +2,32 @@ use ggez::filesystem::File; use std::io::BufReader; use xml::reader::{ EventReader, - XmlEvent::{self, StartElement}, + XmlEvent::{self, EndElement, StartElement}, }; +#[derive(Debug)] +pub struct Property { + entity: String, + keyframe: usize, + delay: usize, +} + +impl Property { + pub fn new(property_elements: Vec) -> Property { + + let entity = XMLElements::get_attribute_value(&property_elements, "entity").unwrap().parse().unwrap(); + let keyframe = + XMLElements::get_attribute_value(&property_elements, "keyframe").unwrap().parse().unwrap(); + let delay = XMLElements::get_attribute_value(&property_elements, "delay").unwrap().parse().unwrap(); + + Property { + entity, + keyframe, + delay, + } + } +} + pub struct XMLElements { pub events: Vec, } @@ -33,20 +56,101 @@ impl XMLElements { .unwrap() } + pub fn get_children(&self, element: &XmlEvent, children_name: &str) -> Vec { + let start_index = self.events.iter().position(|e| e == element).unwrap(); + + let element_name = if let StartElement { name, .. } = element { + &name.local_name + } else { + "" + }; + + let end_index = self.events[start_index..] + .iter() + .position(|e| { + if let EndElement { name } = e { + element_name == name.local_name + } else { + false + } + }) + .unwrap() + + start_index; + + self.events[start_index..end_index] + .iter() + .cloned() + .filter(|e| { + if let StartElement { name, .. } = e { + name.local_name == children_name + } else { + false + } + }) + .collect() + } + + pub fn get_elements(&self, element_name: &str) -> Vec { + self.events + .clone() + .into_iter() + .filter(|e| { + if let StartElement { name, .. } = e { + name.local_name == element_name + } else { + false + } + }) + .collect() + } + pub fn get_element_attribute( &self, element_name: &str, attribute_name: &str, - ) -> Result { + ) -> Result { let element = self.get_element(element_name); + XMLElements::get_attribute(&element, attribute_name) + } + + pub fn get_attribute(element: &XmlEvent, attribute_name: &str) -> Result { if let StartElement { attributes, .. } = element { Ok(attributes .iter() .find(|a| a.name.local_name == attribute_name) .unwrap() .value - .parse() - .unwrap()) + .clone()) + } else { + Err(()) + } + } + + pub fn get_attribute_value( + elements: &Vec, + attribute_name: &str, + ) -> Result { + let element = elements + .iter() + .find(|e| { + if let StartElement { attributes, .. } = e { + attributes + .iter() + .find(|a| a.value == attribute_name) + .is_some() + } else { + false + } + }) + .unwrap(); + + if let StartElement { attributes, .. } = element { + Ok(attributes + .iter() + .find(|a| a.name.local_name == "value") + .unwrap() + .value + .clone()) } else { Err(()) } -- cgit v1.2.3 From f7bbf5646c8220bca2fbd9451d5c234049cf9225 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 26 Jun 2019 06:01:12 -0500 Subject: clippy and fmt --- src/xmlelements.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'src/xmlelements.rs') diff --git a/src/xmlelements.rs b/src/xmlelements.rs index bf9a213..d05e9ff 100644 --- a/src/xmlelements.rs +++ b/src/xmlelements.rs @@ -14,11 +14,18 @@ pub struct Property { impl Property { pub fn new(property_elements: Vec) -> Property { - - let entity = XMLElements::get_attribute_value(&property_elements, "entity").unwrap().parse().unwrap(); - let keyframe = - XMLElements::get_attribute_value(&property_elements, "keyframe").unwrap().parse().unwrap(); - let delay = XMLElements::get_attribute_value(&property_elements, "delay").unwrap().parse().unwrap(); + let entity = XMLElements::get_attribute_value(&property_elements, "entity") + .unwrap() + .parse() + .unwrap(); + let keyframe = XMLElements::get_attribute_value(&property_elements, "keyframe") + .unwrap() + .parse() + .unwrap(); + let delay = XMLElements::get_attribute_value(&property_elements, "delay") + .unwrap() + .parse() + .unwrap(); Property { entity, @@ -126,18 +133,12 @@ impl XMLElements { } } - pub fn get_attribute_value( - elements: &Vec, - attribute_name: &str, - ) -> Result { + pub fn get_attribute_value(elements: &[XmlEvent], attribute_name: &str) -> Result { let element = elements .iter() .find(|e| { if let StartElement { attributes, .. } = e { - attributes - .iter() - .find(|a| a.value == attribute_name) - .is_some() + attributes.iter().any(|a| a.value == attribute_name) } else { false } -- cgit v1.2.3 From ee8d055be8326eb1561900bcca6acd9e38071f4a Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 27 Jun 2019 03:31:33 -0500 Subject: tiles can now animate --- src/xmlelements.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/xmlelements.rs') diff --git a/src/xmlelements.rs b/src/xmlelements.rs index d05e9ff..ea880c8 100644 --- a/src/xmlelements.rs +++ b/src/xmlelements.rs @@ -5,11 +5,11 @@ use xml::reader::{ XmlEvent::{self, EndElement, StartElement}, }; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Property { - entity: String, - keyframe: usize, - delay: usize, + pub entity: String, + pub keyframe: usize, + pub delay: usize, } impl Property { -- cgit v1.2.3