test functional for render, grid, input. Add some function. Add colot print in render ' pub fn print_c()', add offset for pos in render 'pub fn offset_pos()'. Create generete grid in main.rs as random value and test render this.
This commit is contained in:
1 parent
d9b301ea04
commit
9c61078414
4 files changed
+114
-29
No files matched your search
+52
-20
@@ -1,5 +1,6 @@
|
||||
use std::{
|
||||
error,
|
||||
fmt::format,
|
||||
io::{Stdout, Write, stdout},
|
||||
process::Output,
|
||||
};
|
||||
@@ -8,13 +9,22 @@ use crossterm::{
|
||||
cursor::MoveTo,
|
||||
execute, queue,
|
||||
style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor},
|
||||
terminal::{Clear, ClearType},
|
||||
terminal::{Clear, ClearType, enable_raw_mode},
|
||||
};
|
||||
|
||||
use super::TorusGrid;
|
||||
|
||||
pub enum PosCom {
|
||||
UP,
|
||||
DW,
|
||||
LF,
|
||||
RH,
|
||||
}
|
||||
|
||||
pub struct Render {
|
||||
out: Stdout,
|
||||
rez: (usize, usize),
|
||||
pos: (usize, usize),
|
||||
pos: (i32, i32),
|
||||
}
|
||||
|
||||
impl Render {
|
||||
@@ -32,40 +42,62 @@ impl Render {
|
||||
self.rez = (a, b);
|
||||
Ok(())
|
||||
}
|
||||
pub fn set_pos(&mut self, a: usize, b: usize) {
|
||||
pub fn set_pos(&mut self, a: i32, b: i32) {
|
||||
self.pos = (a, b);
|
||||
}
|
||||
pub fn init(&mut self) {
|
||||
execute!(self.out, Clear(ClearType::All), MoveTo(0, 0));
|
||||
pub fn offset_pos(&mut self, a: PosCom) {
|
||||
match a {
|
||||
PosCom::LF => {
|
||||
self.pos.1 -= 1;
|
||||
}
|
||||
PosCom::UP => {
|
||||
self.pos.0 -= 1;
|
||||
}
|
||||
PosCom::DW => {
|
||||
self.pos.0 += 1;
|
||||
}
|
||||
PosCom::RH => {
|
||||
self.pos.1 += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn clear(&mut self) {
|
||||
_ = execute!(self.out, Clear(ClearType::All), MoveTo(0, 0));
|
||||
}
|
||||
pub fn print(&mut self, out_str: &str) {
|
||||
execute!(self.out, Print(out_str));
|
||||
_ = execute!(self.out, Print(out_str));
|
||||
}
|
||||
pub fn print_c(&mut self, out_str: &str, c: Color) {
|
||||
_ = execute!(self.out, SetForegroundColor(c), Print(out_str), ResetColor);
|
||||
}
|
||||
pub fn print_err(&mut self, out_err: &str) {
|
||||
execute!(
|
||||
_ = execute!(
|
||||
self.out,
|
||||
SetForegroundColor(Color::Red),
|
||||
Print(out_err),
|
||||
Print("\n"),
|
||||
ResetColor
|
||||
);
|
||||
}
|
||||
pub fn frame(&mut self) {
|
||||
for _i in 0..self.rez.0 {
|
||||
for _j in 0..self.rez.1 {
|
||||
queue!(
|
||||
self.out,
|
||||
SetBackgroundColor(Color::Rgb {
|
||||
r: 100,
|
||||
g: 30,
|
||||
b: 90
|
||||
}),
|
||||
pub fn frame(&mut self, grid: &TorusGrid<i32>) {
|
||||
let mut out = self.out.lock();
|
||||
_ = queue!(out, MoveTo(0, 0));
|
||||
for i in 0..self.rez.0 {
|
||||
for j in 0..self.rez.1 {
|
||||
let a = grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1));
|
||||
let b = *a as u8;
|
||||
_ = queue!(
|
||||
out,
|
||||
SetBackgroundColor(Color::Rgb { r: b, g: b, b: b }),
|
||||
Print(" "),
|
||||
ResetColor
|
||||
);
|
||||
}
|
||||
queue!(self.out, Print("\r\n"));
|
||||
_ = queue!(out, Print("\r\n"));
|
||||
}
|
||||
self.out.flush();
|
||||
_ = out.flush();
|
||||
self.print_c(
|
||||
&format!("pos is: {} {} \n", self.pos.1, self.pos.0),
|
||||
Color::Yellow,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user