Add visualization interface

This commit is contained in:
hal8174 2024-08-24 13:59:57 +02:00
parent 48419b4674
commit c572fcb0e2
4 changed files with 202 additions and 23 deletions

View file

@ -1,3 +1,4 @@
use crate::common::visualize::{Color, Symbol, Visualization, Visualize};
use crate::prelude::*;
use crate::{belt_finding::common::print_map, misc::Map};
@ -36,7 +37,7 @@ pub struct Problem {
#[derive(Debug, Clone, Copy)]
pub struct BlockHandle(usize);
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Layout<'a> {
problem: &'a Problem,
blocks: Vec<(Position, Direction)>,
@ -145,7 +146,9 @@ impl Layout<'_> {
/// Mutate existing layout, creating a valid layout
pub fn mutate<R: Rng + ?Sized>(&self, rng: &mut R) -> Self {
todo!()
let s = self.clone();
s
}
fn collision(
@ -208,10 +211,11 @@ impl Layout<'_> {
Direction::Left => pos + Position::new(offset.y, -offset.x),
}
}
}
pub fn print(&self) {
let mut m: Map<Option<(usize, &str)>> =
Map::new(self.problem.size.x as usize, self.problem.size.y as usize);
impl<'a> Visualize for Layout<'a> {
fn visualize(&self) -> Visualization {
let mut v = Visualization::new(self.problem.size);
for (i, ((p, d), b)) in self
.blocks
@ -219,39 +223,33 @@ impl Layout<'_> {
.zip(self.problem.blocks.iter())
.enumerate()
{
let c = Color::index(i);
let (npos, nsize) = Self::normalize_pos((b, *p, *d));
for x in npos.x..(npos.x + nsize.x) {
for y in npos.y..(npos.y + nsize.y) {
m.set(x as usize, y as usize, Some((i, "#")));
v.add_symbol(Position::new(x, y), Symbol::Block, Some(c), None);
}
}
let pos = Self::transform(*p, *d, Position::new(0, 0));
m.set(pos.x as usize, pos.y as usize, Some((i, "X")));
v.add_symbol(pos, Symbol::Char("X"), Some(c), None);
for input in &b.input {
let pos = Self::transform(*p, *d, input.offset);
m.set(pos.x as usize, pos.y as usize, Some((i, "i")));
v.add_symbol(pos, Symbol::Char("i"), Some(c), None);
}
for output in &b.output {
let pos = Self::transform(*p, *d, output.offset);
m.set(pos.x as usize, pos.y as usize, Some((i, "o")));
v.add_symbol(pos, Symbol::Char("o"), Some(c), None);
}
}
let _ = print_map(self.problem.size.x, self.problem.size.y, |x, y| {
if let Some(i) = m.get(x as usize, y as usize) {
let mut color = ColorSpec::new();
color.set_fg(Some(crate::common::color::COLORS[i.0]));
(color, i.1)
} else {
(ColorSpec::new(), " ")
}
});
v
}
}