Skip to content

Commit

Permalink
[#66] Improve BrickPattern & EdgeStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Orchaldir committed May 14, 2021
1 parent a0be622 commit 01914cf
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 21 deletions.
34 changes: 29 additions & 5 deletions texture_generation/src/generation/component/layout/brick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ pub struct BrickPattern {

impl BrickPattern {
pub fn new(brick: Size, offset: u32, component: Component) -> Result<BrickPattern> {
if brick.width() < 1 {
bail!("Argument 'brick.width' needs to be greater than 1");
} else if brick.height() < 1 {
bail!("Argument 'brick.height' needs to be greater than 1");
if brick.width() == 0 {
bail!("Argument 'brick.width' needs to be greater than 0");
} else if brick.height() == 0 {
bail!("Argument 'brick.height' needs to be greater than 0");
} else if offset >= brick.width() {
bail!("Argument 'offset' needs to be greater than or equal to 'brick.width'");
}
Expand All @@ -56,7 +56,7 @@ impl BrickPattern {

pub fn new_square(side: u32, component: Component) -> Result<BrickPattern> {
if side < 1 {
bail!("Argument 'side' needs to be greater than 1");
bail!("Argument 'side' needs to be greater than 0");
}

Ok(BrickPattern {
Expand Down Expand Up @@ -141,6 +141,30 @@ mod tests {
use crate::math::color::{RED, WHITE};
use crate::math::size::Size;

#[test]
#[should_panic]
fn test_new_with_width_too_small() {
BrickPattern::new(Size::new(0, 5), 2, Component::Mock(2)).unwrap();
}

#[test]
#[should_panic]
fn test_new_with_height_too_small() {
BrickPattern::new(Size::new(4, 0), 2, Component::Mock(2)).unwrap();
}

#[test]
#[should_panic]
fn test_new_with_offset_too_large() {
BrickPattern::new(Size::new(4, 5), 6, Component::Mock(2)).unwrap();
}

#[test]
#[should_panic]
fn test_new_square_with_side_too_small() {
BrickPattern::new_square(0, Component::Mock(2)).unwrap();
}

#[test]
fn test_brick_wall() {
let size = Size::new(10, 15);
Expand Down
1 change: 1 addition & 0 deletions tilemap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0"
chrono = "0.4"
env_logger = "0.8"
log = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion tilemap/src/rendering/style/door.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct DoorStyle {

impl DoorStyle {
pub fn default(thickness: u32) -> DoorStyle {
Self::new("default", EdgeStyle::default(thickness), true)
Self::new("default", EdgeStyle::default(thickness).unwrap(), true)
}

pub fn new<S: Into<String>>(name: S, edge_style: EdgeStyle, is_centered: bool) -> DoorStyle {
Expand Down
39 changes: 30 additions & 9 deletions tilemap/src/rendering/style/edge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::rendering::style::node::NodeStyle;
use anyhow::{bail, Result};
use texture_generation::generation::component::layout::LayoutComponent;
use texture_generation::generation::component::rendering::RenderingComponent;
use texture_generation::generation::data::texture::Texture;
Expand All @@ -24,26 +25,34 @@ pub enum EdgeStyle {
}

impl EdgeStyle {
pub fn default(thickness: u32) -> EdgeStyle {
pub fn default(thickness: u32) -> Result<EdgeStyle> {
Self::new_solid(thickness, RenderingComponent::default())
}

pub fn new_layout(thickness: u32, component: LayoutComponent) -> EdgeStyle {
pub fn new_layout(thickness: u32, component: LayoutComponent) -> Result<EdgeStyle> {
if thickness == 0 {
bail!("Argument 'thickness' needs to be greater than 0");
}

let vertical = component.flip();
EdgeStyle::Layout {
Ok(EdgeStyle::Layout {
thickness,
half_thickness: (thickness / 2) as i32,
horizontal: component,
vertical,
}
})
}

pub fn new_solid(thickness: u32, component: RenderingComponent) -> EdgeStyle {
EdgeStyle::Solid {
pub fn new_solid(thickness: u32, component: RenderingComponent) -> Result<EdgeStyle> {
if thickness == 0 {
bail!("Argument 'thickness' needs to be greater than 0");
}

Ok(EdgeStyle::Solid {
thickness,
half_thickness: (thickness / 2) as i32,
component,
}
})
}

pub fn get_thickness(&self) -> u32 {
Expand Down Expand Up @@ -185,7 +194,7 @@ impl EdgeStyle {

impl Default for EdgeStyle {
fn default() -> Self {
EdgeStyle::default(1)
EdgeStyle::default(1).unwrap()
}
}

Expand All @@ -196,13 +205,25 @@ mod tests {
use texture_generation::generation::data::texture::Texture;
use texture_generation::math::color::{BLACK, GREEN, RED};

#[test]
#[should_panic]
fn test_new_layout_with_thickness_too_small() {
EdgeStyle::new_layout(0, LayoutComponent::Mock(8)).unwrap();
}

#[test]
#[should_panic]
fn test_new_solid_with_thickness_too_small() {
EdgeStyle::new_solid(0, RenderingComponent::default()).unwrap();
}

#[test]
fn test_render_horizontal() {
let component = RenderingComponent::new_fill_area(RED, 9);
let edge_component = RenderingComponent::new_fill_area(GREEN, 4);
let node_style0 = NodeStyle::new(4, component.clone());
let node_style1 = NodeStyle::new(2, component);
let edge_style = EdgeStyle::new_solid(2, edge_component);
let edge_style = EdgeStyle::new_solid(2, edge_component).unwrap();
let mut texture = Texture::new(Size::new(11, 6), BLACK);

edge_style.render_horizontal(
Expand Down
2 changes: 1 addition & 1 deletion tilemap/src/rendering/style/wall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct WallStyle {

impl WallStyle {
pub fn default(thickness: u32) -> WallStyle {
Self::new("default", EdgeStyle::default(thickness), None, 0)
Self::new("default", EdgeStyle::default(thickness).unwrap(), None, 0)
}

pub fn new<S: Into<String>>(
Expand Down
4 changes: 2 additions & 2 deletions tilemap/src/rendering/style/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ impl WindowStyle {
pub fn default(thickness: u32) -> WindowStyle {
Self::new(
"default",
EdgeStyle::default(thickness),
EdgeStyle::default(thickness),
EdgeStyle::default(thickness).unwrap(),
EdgeStyle::default(thickness).unwrap(),
)
}

Expand Down
10 changes: 7 additions & 3 deletions tilemap_io/src/rendering/style/edge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use texture_generation::definition::convert;
use texture_generation::definition::generation::component::layout::LayoutDefinition;
Expand All @@ -23,7 +23,9 @@ impl EdgeDefinition {
match self {
EdgeDefinition::Layout { thickness, layout } => {
let layout = layout.convert(&format!("{}.Layout.layout", parent), factor)?;
Ok(EdgeStyle::new_layout(convert(*thickness, factor), layout))
let edge_style = EdgeStyle::new_layout(convert(*thickness, factor), layout)
.context(format!("Failed to create '{}.Layout'", parent))?;
Ok(edge_style)
}
EdgeDefinition::Mock(value) => Ok(EdgeStyle::Mock(convert(*value, factor))),
EdgeDefinition::Solid {
Expand All @@ -32,7 +34,9 @@ impl EdgeDefinition {
} => {
let component =
component.convert(&format!("{}.Solid.component", parent), factor)?;
Ok(EdgeStyle::new_solid(convert(*thickness, factor), component))
let edge_style = EdgeStyle::new_solid(convert(*thickness, factor), component)
.context(format!("Failed to create '{}.Solid'", parent))?;
Ok(edge_style)
}
}
}
Expand Down

0 comments on commit 01914cf

Please sign in to comment.