Skip to content

Commit

Permalink
[#66] Add HandleStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Orchaldir committed May 23, 2021
1 parent a31f2d6 commit bbb2f6d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
7 changes: 6 additions & 1 deletion texture_generation/src/generation/component/rendering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ pub mod depth_factory;
/// Renders the texture.
pub enum RenderingComponent {
/// Fills the area with a color.
FillArea { color: Color, depth: u8 },
FillArea {
color: Color,
depth: u8,
},
Mock,
/// Renders a [`Shape`].
Shape {
shape_factory: ShapeFactory,
Expand Down Expand Up @@ -78,6 +82,7 @@ impl RenderingComponent {
point.y += 1;
}
}
RenderingComponent::Mock => {}
RenderingComponent::Shape {
shape_factory,
color_selector,
Expand Down
11 changes: 11 additions & 0 deletions texture_generation/src/math/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ impl Point {
Point { x, y }
}

/// Returns a new point with switched x & <height>.
///
/// ```
///# use texture_generation::math::point::Point;
/// let point = Point::new(-10, 30);
/// assert_eq!(point.flip(), Point::new(30, -10));
/// ```
pub fn flip(&self) -> Point {
Point::new(self.y, self.x)
}

/// Calculates the euclidean distance to another point.
///
/// ```
Expand Down
68 changes: 68 additions & 0 deletions tilemap/src/rendering/style/handle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use anyhow::{bail, Result};
use texture_generation::generation::component::rendering::RenderingComponent;
use texture_generation::math::point::Point;
use texture_generation::math::size::Size;

#[derive(Clone, Debug, PartialEq)]
pub struct HandleStyle {
on_both_sides: bool,
horizontal_pos: Point,
vertical_pos: Point,
horizontal_size: Size,
vertical_size: Size,
component: RenderingComponent,
}

impl HandleStyle {
pub fn new(
on_both_sides: bool,
pos: Point,
size: Size,
component: RenderingComponent,
) -> Result<HandleStyle> {
if size.width() == 0 {
bail!("Argument 'size.width' needs to be greater than 0");
} else if size.height() == 0 {
bail!("Argument 'size.height' needs to be greater than 0");
}

Ok(HandleStyle {
on_both_sides,
horizontal_pos: pos,
vertical_pos: pos.flip(),
horizontal_size: size,
vertical_size: size.flip(),
component,
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use texture_generation::generation::component::rendering::RenderingComponent;

#[test]
#[should_panic]
fn test_new_with_0_width() {
HandleStyle::new(
true,
Point::default(),
Size::new(0, 10),
RenderingComponent::Mock,
)
.unwrap();
}

#[test]
#[should_panic]
fn test_new_with_0_height() {
HandleStyle::new(
true,
Point::default(),
Size::new(20, 0),
RenderingComponent::Mock,
)
.unwrap();
}
}
1 change: 1 addition & 0 deletions tilemap/src/rendering/style/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod door;
pub mod edge;
pub mod handle;
pub mod node;
pub mod wall;
pub mod window;

0 comments on commit bbb2f6d

Please sign in to comment.