Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text objects #230

Merged
merged 3 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Template support!
Templates are loaded automatically when they are encountered, and are treated as intermediate
objects. As such, `ResourceCache` has now methods for both getting and inserting them (#170).
- Text object support (#230).
- VFS support (#199).
- `cache_mut` loader property (#207).

Expand Down
2 changes: 1 addition & 1 deletion examples/ggez/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl MapHandler {
)?;
graphics::draw(ctx, &shape, draw_param)?;
}
tiled::ObjectShape::Point(_, _) => {
tiled::ObjectShape::Point(_, _) | tiled::ObjectShape::Text { .. } => {
// Left as an exercise for the reader
}
}
Expand Down
145 changes: 140 additions & 5 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
properties::{parse_properties, Properties},
template::Template,
util::{get_attrs, map_wrapper, parse_tag, XmlEventResult},
Gid, MapTilesetGid, ResourceCache, ResourceReader, Tile, TileId, Tileset,
Color, Gid, MapTilesetGid, ResourceCache, ResourceReader, Tile, TileId, Tileset,
};

/// The location of the tileset this tile is in
Expand Down Expand Up @@ -120,11 +120,55 @@ impl<'map> ObjectTile<'map> {
#[derive(Debug, PartialEq, Clone)]
#[allow(missing_docs)]
pub enum ObjectShape {
Rect { width: f32, height: f32 },
Ellipse { width: f32, height: f32 },
Polyline { points: Vec<(f32, f32)> },
Polygon { points: Vec<(f32, f32)> },
Rect {
width: f32,
height: f32,
},
Ellipse {
width: f32,
height: f32,
},
Polyline {
points: Vec<(f32, f32)>,
},
Polygon {
points: Vec<(f32, f32)>,
},
Point(f32, f32),
Text {
font_family: String,
pixel_size: usize,
wrap: bool,
color: Color,
bold: bool,
italic: bool,
underline: bool,
strikeout: bool,
kerning: bool,
halign: HorizontalAlignment,
valign: VerticalAlignment,
},
}

/// The horizontal alignment of an [`ObjectShape::Text`].
#[derive(Debug, PartialEq, Clone, Copy, Default)]
#[allow(missing_docs)]
pub enum HorizontalAlignment {
#[default]
Left,
Center,
Right,
Justify,
}

/// The vertical alignment of an [`ObjectShape::Text`].
#[derive(Debug, PartialEq, Clone, Copy, Default)]
#[allow(missing_docs)]
pub enum VerticalAlignment {
#[default]
Top,
Center,
Bottom,
}

/// Raw data belonging to an object. Used internally and for tile collisions.
Expand Down Expand Up @@ -274,6 +318,10 @@ impl ObjectData {
shape = Some(ObjectShape::Point(x, y));
Ok(())
},
"text" => |attrs| {
shape = Some(ObjectData::new_text(attrs)?);
Ok(())
},
"properties" => |_| {
properties = parse_properties(parser)?;
Ok(())
Expand Down Expand Up @@ -333,6 +381,93 @@ impl ObjectData {
Ok(ObjectShape::Polygon { points })
}

fn new_text(attrs: Vec<OwnedAttribute>) -> Result<ObjectShape> {
let (
font_family,
pixel_size,
wrap,
color,
bold,
italic,
underline,
strikeout,
kerning,
halign,
valign,
) = get_attrs!(
for v in attrs {
Some("fontfamily") => font_family = v,
Some("pixelsize") => pixel_size ?= v.parse(),
Some("wrap") => wrap ?= v.parse(),
Some("color") => color ?= v.parse(),
Some("bold") => bold ?= v.parse(),
Some("italic") => italic ?= v.parse(),
Some("underline") => underline ?= v.parse(),
Some("strikeout") => strikeout ?= v.parse(),
Some("kerning") => kerning ?= v.parse(),
Some("halign") => halign = match v.as_str() {
"left" => HorizontalAlignment::Left,
"center" => HorizontalAlignment::Center,
"right" => HorizontalAlignment::Right,
"justify" => HorizontalAlignment::Justify,
_ => return Err(Error::MalformedAttributes("`halign` property did not contain a valid value of 'left', 'center', 'right' or 'justify'".to_string()))
},
Some("valign") => valign = match v.as_str() {
"top" => VerticalAlignment::Top,
"center" => VerticalAlignment::Center,
"bottom" => VerticalAlignment::Bottom,
_ => return Err(Error::MalformedAttributes(
"`halign` property did not contain a valid value of 'top', 'center' or 'bottom'"
.to_string(),
)),
},
}
(
font_family,
pixel_size,
wrap,
color,
bold,
italic,
underline,
strikeout,
kerning,
halign,
valign,
)
);
let font_family = font_family.unwrap_or_else(|| "sans-serif".to_string());
let pixel_size = pixel_size.unwrap_or(16);
let color = color.unwrap_or(Color {
red: 0,
green: 0,
blue: 0,
alpha: 255,
});
let wrap = wrap == Some(1);
let bold = bold == Some(1);
let italic = italic == Some(1);
let underline = underline == Some(1);
let strikeout = strikeout == Some(1);
let kerning = kerning == Some(1);
let halign = halign.unwrap_or_default();
let valign = valign.unwrap_or_default();

Ok(ObjectShape::Text {
font_family,
pixel_size,
wrap,
color,
bold,
italic,
underline,
strikeout,
kerning,
halign,
valign,
})
}

fn parse_points(s: String) -> Result<Vec<(f32, f32)>> {
let pairs = s.split(' ');
pairs
Expand Down