From b68d33e4ae20a46919112ad09108d88f35616248 Mon Sep 17 00:00:00 2001 From: Adrien Burgun Date: Mon, 11 Jul 2022 10:28:37 +0200 Subject: [PATCH] :sparkles: impl Display for Pane and World --- stackline/Cargo.toml | 2 +- stackline/src/pane.rs | 6 +++--- stackline/src/text.rs | 1 - stackline/src/world.rs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/stackline/Cargo.toml b/stackline/Cargo.toml index b5558cc..69c685a 100755 --- a/stackline/Cargo.toml +++ b/stackline/Cargo.toml @@ -12,9 +12,9 @@ enum_dispatch = "0.3" contracts = { version = "0.6.3", features = ["override_debug"] } veccell = "0.3.0" pathfinding = "3.0" +colored = "2.0" [dev-dependencies] -colored = "2.0" criterion = { version = "0.3.5", features = ["html_reports"] } [build-dependencies] diff --git a/stackline/src/pane.rs b/stackline/src/pane.rs index f62a558..1e720cc 100644 --- a/stackline/src/pane.rs +++ b/stackline/src/pane.rs @@ -508,10 +508,10 @@ impl Pane { /// Draws the Pane at `(dx + self.position.0, dy + self.position.1)` on a [`TextSurface`]. /// Empty tiles will leave the `TextSurface` untouched, but tiles are free to modify the characters around them. - pub fn draw(&self, dx: isize, dy: isize, surface: &mut TextSurface) { + pub fn draw(&self, dx: i32, dy: i32, surface: &mut TextSurface) { for (x, y, tile) in self.tiles() { - let x = x as isize + dx + self.position.0 as isize; - let y = y as isize + dy + self.position.1 as isize; + let x = x as i32 + dx + self.position.0 as i32; + let y = y as i32 + dy + self.position.1 as i32; if x >= 0 && y >= 0 { tile.draw(x as usize, y as usize, surface); diff --git a/stackline/src/text.rs b/stackline/src/text.rs index c8397db..0698174 100644 --- a/stackline/src/text.rs +++ b/stackline/src/text.rs @@ -128,7 +128,6 @@ impl TextSurface { // TODO: resize } -#[cfg(test)] impl std::fmt::Display for TextSurface { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { use colored::Colorize; diff --git a/stackline/src/world.rs b/stackline/src/world.rs index 91b7d82..61eb725 100644 --- a/stackline/src/world.rs +++ b/stackline/src/world.rs @@ -53,6 +53,39 @@ impl World { false } + + pub fn draw(&self, dx: i32, dy: i32, surface: &mut TextSurface) { + for pane in self.panes.values() { + pane.draw(dx, dy, surface); + } + } + + pub fn get_bounds(&self) -> (i32, i32, i32, i32) { + self.panes.values().fold((0, 0, 0, 0), |acc, act| { + ( + acc.0.min(act.position().0), + acc.1.max(act.position().0), + acc.2.min(act.position().1), + acc.3.max(act.position().1), + ) + }) + } +} + +impl std::fmt::Display for World { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let bounds = self.get_bounds(); + let width = (bounds.1 - bounds.0) as usize; + let height = (bounds.3 - bounds.2) as usize; + + let mut surface = TextSurface::new(width, height); + + for pane in self.panes.values() { + pane.draw(bounds.0, bounds.2, &mut surface); + } + + ::fmt(&surface, f) + } } #[cfg(test)]