use super::*;
use super::super::ds::{CellData, Direction, MazeData};
use super::super::io::flush;
fn cls() {
print!("{}", CLS);
}
fn newline() {
println!();
}
fn locate(x: usize, y: usize) {
print!("{}", format!("\x1B[{};{}H", x + 1, y + 1));
}
fn save_cursor_position() {
print!("{}", SAVE_CURSOR_POSITION);
}
fn restore_cursor_position() {
print!("{}", RESTORE_CURSOR_POSITION);
}
fn set_cursor_color(color: Color) {
print!("{}", color.as_str());
}
fn reset_color() {
print!("{}", RESET_COLOR);
}
fn pause(duration: u64) {
use std::{thread, time};
thread::sleep(time::Duration::from_millis(duration));
flush();
}
pub fn draw_maze(maze: &MazeData) {
cls();
locate(0, 0);
for i in 0..maze.get_height() {
for j in 0..maze.get_width() {
pause(MAZE_ANIMATION_SPEED);
draw_cell(maze.get_cell(i, j));
}
}
newline();
}
pub fn draw_cell(cell: &CellData) {
let (x, y) = (cell.get_location().get_x(), cell.get_location().get_y());
locate(x, y);
print!("{}", NORTH_SPRITE);
locate(x + 1, y);
print!("{}", WEST_SPRITE);
locate(x + 1, y + 4);
print!("{}", EAST_SPRITE);
locate(x + 2, y);
print!("{}", SOUTH_SPRITE);
}
pub fn delete_current_line() {
for _ in 0..3 {
print!("{}", CLEAR_CURRENT_LINE);
print!("{}", MOVE_BACK_ONE_LINE);
}
}
pub fn erase_wall(cell: &CellData, direction: &Direction) {
save_cursor_position();
let (x, y) = (cell.get_location().get_x(), cell.get_location().get_y());
match direction {
Direction::North => {
locate(x, y + 1);
for _ in 0..NORTH_SPRITE.len() - 2 {
print!("{}", ' ');
}
}
Direction::South => {
locate(x + 2, y + 1);
for _ in 0..SOUTH_SPRITE.len() - 2 {
print!("{}", ' ');
}
}
Direction::East => {
locate(x + 1, y + 4);
print!("{}", ' ');
}
Direction::West => {
locate(x + 1, y);
print!("{}", ' ');
}
}
restore_cursor_position();
}
pub fn fill_cell(cell: &CellData, c: char) {
save_cursor_position();
pause(PATH_ANIMATION_SPEED);
set_cursor_color(Color::RED);
let (x, y) = (cell.get_location().get_x(), cell.get_location().get_y());
locate(x + 1, y + 2);
print!("{}", c);
reset_color();
restore_cursor_position();
}
pub fn clear_cell(cell: &CellData) {
save_cursor_position();
let (x, y) = (cell.get_location().get_x(), cell.get_location().get_y());
locate(x + 1, y + 2);
print!("{}", ' ');
restore_cursor_position();
}