Skip to content
Snippets Groups Projects
Commit ef6bbfda authored by Matthew Hall's avatar Matthew Hall
Browse files

Add colour parsing

parent ed774951
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="100" height="100" tilewidth="32" tileheight="32"> <map version="1.0" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="32" tileheight="32" backgroundcolor="#ff00ff">
<tileset firstgid="1" name="tilesheet" tilewidth="32" tileheight="32"> <tileset firstgid="1" name="tilesheet" tilewidth="32" tileheight="32">
<image source="tilesheet.png" width="448" height="192"/> <image source="tilesheet.png" width="448" height="192"/>
<tile id="1"> <tile id="1">
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
eJzt1zEKwzAMBVBdIScIJXPS+9+uDY1BuHbpULvLeyDIkEkfIXmLiK2qW6cYb7lqv+p41r1TcpmjZFLPyamVEXOcGSzpu66SixkZb41Xz9dO7fE+O4yR+703KmdyhExGK32v+1zv+rxLZDJO7vO5G3KvSyY1eYxT31Dlrm3t9AhZzNC6a+v3RsmhNzP83qdc8ttdHuO07thWLlt4E87w7S6wM+bo7e3ef8z1KRt5/N+388N88gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIHsArPIXTA== eJzt1zEKwzAMBVBdIScIJXPS+9+uDY1BuHbpULvLeyDIkEkfIXmLiK2qW6cYb7lqv+p41r1TcpmjZFLPyamVEXOcGSzpu66SixkZb41Xz9dO7fE+O4yR+703KmdyhExGK32v+1zv+rxLZDJO7vO5G3KvSyY1eYxT31Dlrm3t9AhZzNC6a+v3RsmhNzP83qdc8ttdHuO07thWLlt4E87w7S6wM+bo7e3ef8z1KRt5/N+388N88gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIHsArPIXTA==
</data> </data>
</layer> </layer>
<objectgroup name="Object group" width="100" height="100"> <objectgroup name="Object group">
<object x="14" y="9" width="285" height="135"/> <object x="14" y="9" width="285" height="135"/>
<object x="329" y="217" width="102" height="109"> <object x="329" y="217" width="102" height="109">
<ellipse/> <ellipse/>
......
...@@ -11,6 +11,7 @@ use xml::common::Attribute; ...@@ -11,6 +11,7 @@ use xml::common::Attribute;
use xml::reader::events::*; use xml::reader::events::*;
use serialize::base64::{FromBase64, FromBase64Error}; use serialize::base64::{FromBase64, FromBase64Error};
use flate2::reader::ZlibDecoder; use flate2::reader::ZlibDecoder;
use std::num::from_str_radix;
// Loops through the attributes once and pulls out the ones we ask it to. It // Loops through the attributes once and pulls out the ones we ask it to. It
// will check that the required ones are there. This could have been done with // will check that the required ones are there. This could have been done with
...@@ -68,6 +69,33 @@ macro_rules! parse_tag { ...@@ -68,6 +69,33 @@ macro_rules! parse_tag {
} }
} }
#[deriving(Show)]
pub struct Colour {
pub red: u8,
pub green: u8,
pub blue: u8
}
impl FromStr for Colour {
fn from_str(s: &str) -> Option<Colour> {
let s = if s.starts_with("#") {
s[1..]
} else {
s
};
if s.len() != 6 {
return None;
}
let r = from_str_radix(s[0..2], 16);
let g = from_str_radix(s[2..4], 16);
let b = from_str_radix(s[4..6], 16);
if r.is_some() && g.is_some() && b.is_some() {
return Some(Colour {red: r.unwrap(), green: g.unwrap(), blue: b.unwrap()})
}
None
}
}
/// Errors which occured when parsing the file /// Errors which occured when parsing the file
#[deriving(Show)] #[deriving(Show)]
pub enum TiledError { pub enum TiledError {
...@@ -112,14 +140,15 @@ pub struct Map { ...@@ -112,14 +140,15 @@ pub struct Map {
tilesets: Vec<Tileset>, tilesets: Vec<Tileset>,
layers: Vec<Layer>, layers: Vec<Layer>,
object_groups: Vec<ObjectGroup>, object_groups: Vec<ObjectGroup>,
properties: Properties properties: Properties,
background_colour: Option<Colour>,
} }
impl Map { impl Map {
fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<Attribute>) -> Result<Map, TiledError> { fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<Attribute>) -> Result<Map, TiledError> {
let ((), (v, o, w, h, tw, th)) = get_attrs!( let (c, (v, o, w, h, tw, th)) = get_attrs!(
attrs, attrs,
optionals: [], optionals: [("backgroundcolor", colour, |v:String| from_str(v[]))],
required: [("version", version, |v| Some(v)), required: [("version", version, |v| Some(v)),
("orientation", orientation, |v:String| from_str(v[])), ("orientation", orientation, |v:String| from_str(v[])),
("width", width, |v:String| from_str::<int>(v[])), ("width", width, |v:String| from_str::<int>(v[])),
...@@ -153,7 +182,8 @@ impl Map { ...@@ -153,7 +182,8 @@ impl Map {
width: w, height: h, width: w, height: h,
tile_width: tw, tile_height: th, tile_width: tw, tile_height: th,
tilesets: tilesets, layers: layers, object_groups: object_groups, tilesets: tilesets, layers: layers, object_groups: object_groups,
properties: properties}) properties: properties,
background_colour: c,})
} }
/// This function will return the correct Tileset given a GID. /// This function will return the correct Tileset given a GID.
...@@ -235,21 +265,22 @@ pub struct Image { ...@@ -235,21 +265,22 @@ pub struct Image {
/// The filepath of the image /// The filepath of the image
pub source: String, pub source: String,
pub width: int, pub width: int,
pub height: int pub height: int,
pub transparent_colour: Option<Colour>,
} }
impl Image { impl Image {
fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<Attribute>) -> Result<Image, TiledError> { fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<Attribute>) -> Result<Image, TiledError> {
let ((), (s, w, h)) = get_attrs!( let (c, (s, w, h)) = get_attrs!(
attrs, attrs,
optionals: [], optionals: [("trans", trans, |v:String| from_str(v[]))],
required: [("source", source, |v| Some(v)), required: [("source", source, |v| Some(v)),
("width", width, |v:String| from_str(v[])), ("width", width, |v:String| from_str(v[])),
("height", height, |v:String| from_str(v[]))], ("height", height, |v:String| from_str(v[]))],
MalformedAttributes("image must have a source, width and height with correct types".to_string())); MalformedAttributes("image must have a source, width and height with correct types".to_string()));
parse_tag!(parser, "image", "" => |_| Ok(())); parse_tag!(parser, "image", "" => |_| Ok(()));
Ok(Image {source: s, width: w, height: h}) Ok(Image {source: s, width: w, height: h, transparent_colour: c})
} }
} }
...@@ -293,15 +324,17 @@ pub struct ObjectGroup { ...@@ -293,15 +324,17 @@ pub struct ObjectGroup {
pub name: String, pub name: String,
pub opacity: f32, pub opacity: f32,
pub visible: bool, pub visible: bool,
pub objects: Vec<Object> pub objects: Vec<Object>,
pub colour: Option<Colour>,
} }
impl ObjectGroup { impl ObjectGroup {
fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<Attribute>) -> Result<ObjectGroup, TiledError> { fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<Attribute>) -> Result<ObjectGroup, TiledError> {
let ((o, v), n) = get_attrs!( let ((o, v, c), n) = get_attrs!(
attrs, attrs,
optionals: [("opacity", opacity, |v:String| from_str(v[])), optionals: [("opacity", opacity, |v:String| from_str(v[])),
("visible", visible, |v:String| from_str(v[]).map(|x:int| x == 1))], ("visible", visible, |v:String| from_str(v[]).map(|x:int| x == 1)),
("color", colour, |v:String| from_str(v[]))],
required: [("name", name, |v| Some(v))], required: [("name", name, |v| Some(v))],
MalformedAttributes("object groups must have a name".to_string())); MalformedAttributes("object groups must have a name".to_string()));
let mut objects = Vec::new(); let mut objects = Vec::new();
...@@ -312,7 +345,8 @@ impl ObjectGroup { ...@@ -312,7 +345,8 @@ impl ObjectGroup {
}); });
Ok(ObjectGroup {name: n, Ok(ObjectGroup {name: n,
opacity: o.unwrap_or(1.0), visible: v.unwrap_or(true), opacity: o.unwrap_or(1.0), visible: v.unwrap_or(true),
objects: objects}) objects: objects,
colour: c})
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment