impl Display for Pane and World

main
Shad Amethyst 2 years ago
parent fa4d18569d
commit b68d33e4ae
Signed by: amethyst
GPG Key ID: D970C8DD1D6DEE36

@ -12,9 +12,9 @@ enum_dispatch = "0.3"
contracts = { version = "0.6.3", features = ["override_debug"] } contracts = { version = "0.6.3", features = ["override_debug"] }
veccell = "0.3.0" veccell = "0.3.0"
pathfinding = "3.0" pathfinding = "3.0"
colored = "2.0"
[dev-dependencies] [dev-dependencies]
colored = "2.0"
criterion = { version = "0.3.5", features = ["html_reports"] } criterion = { version = "0.3.5", features = ["html_reports"] }
[build-dependencies] [build-dependencies]

@ -508,10 +508,10 @@ impl Pane {
/// Draws the Pane at `(dx + self.position.0, dy + self.position.1)` on a [`TextSurface`]. /// 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. /// 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() { for (x, y, tile) in self.tiles() {
let x = x as isize + dx + self.position.0 as isize; let x = x as i32 + dx + self.position.0 as i32;
let y = y as isize + dy + self.position.1 as isize; let y = y as i32 + dy + self.position.1 as i32;
if x >= 0 && y >= 0 { if x >= 0 && y >= 0 {
tile.draw(x as usize, y as usize, surface); tile.draw(x as usize, y as usize, surface);

@ -128,7 +128,6 @@ impl TextSurface {
// TODO: resize // TODO: resize
} }
#[cfg(test)]
impl std::fmt::Display for TextSurface { impl std::fmt::Display for TextSurface {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use colored::Colorize; use colored::Colorize;

@ -53,6 +53,39 @@ impl World {
false 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);
}
<TextSurface as std::fmt::Display>::fmt(&surface, f)
}
} }
#[cfg(test)] #[cfg(test)]

Loading…
Cancel
Save