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
@@ -5,3 +5,4 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
crossterm = "0.29"
|
crossterm = "0.29"
|
||||||
|
rand = "0.8"
|
||||||
+52
-20
@@ -1,5 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
error,
|
error,
|
||||||
|
fmt::format,
|
||||||
io::{Stdout, Write, stdout},
|
io::{Stdout, Write, stdout},
|
||||||
process::Output,
|
process::Output,
|
||||||
};
|
};
|
||||||
@@ -8,13 +9,22 @@ use crossterm::{
|
|||||||
cursor::MoveTo,
|
cursor::MoveTo,
|
||||||
execute, queue,
|
execute, queue,
|
||||||
style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor},
|
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 {
|
pub struct Render {
|
||||||
out: Stdout,
|
out: Stdout,
|
||||||
rez: (usize, usize),
|
rez: (usize, usize),
|
||||||
pos: (usize, usize),
|
pos: (i32, i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render {
|
impl Render {
|
||||||
@@ -32,40 +42,62 @@ impl Render {
|
|||||||
self.rez = (a, b);
|
self.rez = (a, b);
|
||||||
Ok(())
|
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);
|
self.pos = (a, b);
|
||||||
}
|
}
|
||||||
pub fn init(&mut self) {
|
pub fn offset_pos(&mut self, a: PosCom) {
|
||||||
execute!(self.out, Clear(ClearType::All), MoveTo(0, 0));
|
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) {
|
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) {
|
pub fn print_err(&mut self, out_err: &str) {
|
||||||
execute!(
|
_ = execute!(
|
||||||
self.out,
|
self.out,
|
||||||
SetForegroundColor(Color::Red),
|
SetForegroundColor(Color::Red),
|
||||||
Print(out_err),
|
Print(out_err),
|
||||||
Print("\n"),
|
|
||||||
ResetColor
|
ResetColor
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
pub fn frame(&mut self) {
|
pub fn frame(&mut self, grid: &TorusGrid<i32>) {
|
||||||
for _i in 0..self.rez.0 {
|
let mut out = self.out.lock();
|
||||||
for _j in 0..self.rez.1 {
|
_ = queue!(out, MoveTo(0, 0));
|
||||||
queue!(
|
for i in 0..self.rez.0 {
|
||||||
self.out,
|
for j in 0..self.rez.1 {
|
||||||
SetBackgroundColor(Color::Rgb {
|
let a = grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1));
|
||||||
r: 100,
|
let b = *a as u8;
|
||||||
g: 30,
|
_ = queue!(
|
||||||
b: 90
|
out,
|
||||||
}),
|
SetBackgroundColor(Color::Rgb { r: b, g: b, b: b }),
|
||||||
Print(" "),
|
Print(" "),
|
||||||
ResetColor
|
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,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+23
-1
@@ -3,7 +3,7 @@ use crossterm::{
|
|||||||
event::{self, Event, KeyCode},
|
event::{self, Event, KeyCode},
|
||||||
terminal::{disable_raw_mode, enable_raw_mode},
|
terminal::{disable_raw_mode, enable_raw_mode},
|
||||||
};
|
};
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write, stdout};
|
||||||
|
|
||||||
use crate::input;
|
use crate::input;
|
||||||
|
|
||||||
@@ -29,6 +29,8 @@ pub fn Comand_pars() -> Result<ComandEvent, String> {
|
|||||||
}
|
}
|
||||||
"key" => {
|
"key" => {
|
||||||
_ = enable_raw_mode();
|
_ = enable_raw_mode();
|
||||||
|
let mut out = stdout();
|
||||||
|
_ = crossterm::execute!(out, crossterm::cursor::Hide);
|
||||||
return Ok(ComandEvent::Key);
|
return Ok(ComandEvent::Key);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
@@ -42,18 +44,38 @@ pub enum KeyEvent {
|
|||||||
None,
|
None,
|
||||||
Exit,
|
Exit,
|
||||||
Comand,
|
Comand,
|
||||||
|
A,
|
||||||
|
W,
|
||||||
|
S,
|
||||||
|
D,
|
||||||
}
|
}
|
||||||
pub fn Key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
|
pub fn Key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
|
||||||
if let Event::Key(key) = event::read()? {
|
if let Event::Key(key) = event::read()? {
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Char('q') => {
|
KeyCode::Char('q') => {
|
||||||
_ = disable_raw_mode();
|
_ = disable_raw_mode();
|
||||||
|
let mut out = stdout();
|
||||||
|
_ = crossterm::execute!(out, crossterm::cursor::Show);
|
||||||
return Ok(Some(KeyEvent::Exit));
|
return Ok(Some(KeyEvent::Exit));
|
||||||
}
|
}
|
||||||
KeyCode::Char('c') => {
|
KeyCode::Char('c') => {
|
||||||
_ = disable_raw_mode();
|
_ = disable_raw_mode();
|
||||||
|
let mut out = stdout();
|
||||||
|
_ = crossterm::execute!(out, crossterm::cursor::Show);
|
||||||
return Ok(Some(KeyEvent::Comand));
|
return Ok(Some(KeyEvent::Comand));
|
||||||
}
|
}
|
||||||
|
KeyCode::Char('a') => {
|
||||||
|
return Ok(Some(KeyEvent::A));
|
||||||
|
}
|
||||||
|
KeyCode::Char('w') => {
|
||||||
|
return Ok(Some(KeyEvent::W));
|
||||||
|
}
|
||||||
|
KeyCode::Char('s') => {
|
||||||
|
return Ok(Some(KeyEvent::S));
|
||||||
|
}
|
||||||
|
KeyCode::Char('d') => {
|
||||||
|
return Ok(Some(KeyEvent::D));
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+38
-8
@@ -1,5 +1,6 @@
|
|||||||
|
use rand::Rng;
|
||||||
mod world;
|
mod world;
|
||||||
use world::*;
|
pub use world::*;
|
||||||
|
|
||||||
mod input;
|
mod input;
|
||||||
use input::*;
|
use input::*;
|
||||||
@@ -13,22 +14,39 @@ enum Input_type {
|
|||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
let mut it = Input_type::comand;
|
let mut it = Input_type::comand;
|
||||||
|
let mut R = Render::new();
|
||||||
|
R.clear();
|
||||||
|
R.set_rez(40, 80);
|
||||||
|
|
||||||
|
let g_rez: (usize, usize) = (100, 50);
|
||||||
|
let mut grid: TorusGrid<i32> = TorusGrid::new(0, g_rez);
|
||||||
|
|
||||||
|
for i in 0..g_rez.0 {
|
||||||
|
for j in 0..g_rez.1 {
|
||||||
|
if let Some(a) = grid.get_mut((i as i32, j as i32)) {
|
||||||
|
*a = rng.gen_range(0..=255) as i32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
loop {
|
loop {
|
||||||
|
//R.clear();
|
||||||
|
R.frame(&grid);
|
||||||
match it {
|
match it {
|
||||||
Input_type::comand => {
|
Input_type::comand => {
|
||||||
println!("input your comand: ");
|
R.print("Input your comand: ");
|
||||||
let input = Comand_pars();
|
let input = Comand_pars();
|
||||||
if input.is_err() {
|
if input.is_err() {
|
||||||
println!("ERROR: {}", input.err().unwrap());
|
R.print_err(&format!("ERROR: {}\n", input.err().unwrap()));
|
||||||
} else {
|
} else {
|
||||||
match input.unwrap() {
|
match input.unwrap() {
|
||||||
ComandEvent::Exit => {
|
ComandEvent::Exit => {
|
||||||
println!("progarm is end");
|
R.print("progarm is end\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ComandEvent::Key => {
|
ComandEvent::Key => {
|
||||||
println!("jamp to keybard");
|
R.print("jamp to keybard\n");
|
||||||
it = Input_type::keybard
|
it = Input_type::keybard
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
@@ -38,18 +56,30 @@ fn main() {
|
|||||||
Input_type::keybard => {
|
Input_type::keybard => {
|
||||||
let input = Key_pars();
|
let input = Key_pars();
|
||||||
if input.is_err() {
|
if input.is_err() {
|
||||||
println!("ERROR: {}", input.err().unwrap())
|
R.print_err(&format!("ERROR: {}", input.err().unwrap()));
|
||||||
} else {
|
} else {
|
||||||
if let Some(k) = input.unwrap() {
|
if let Some(k) = input.unwrap() {
|
||||||
match k {
|
match k {
|
||||||
KeyEvent::Exit => {
|
KeyEvent::Exit => {
|
||||||
println!("progarm is end");
|
R.print("progarm is end\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
KeyEvent::Comand => {
|
KeyEvent::Comand => {
|
||||||
println!("jamp to comand");
|
R.print("jamp to comand");
|
||||||
it = Input_type::comand
|
it = Input_type::comand
|
||||||
}
|
}
|
||||||
|
KeyEvent::A => {
|
||||||
|
R.offset_pos(PosCom::LF);
|
||||||
|
}
|
||||||
|
KeyEvent::W => {
|
||||||
|
R.offset_pos(PosCom::UP);
|
||||||
|
}
|
||||||
|
KeyEvent::S => {
|
||||||
|
R.offset_pos(PosCom::DW);
|
||||||
|
}
|
||||||
|
KeyEvent::D => {
|
||||||
|
R.offset_pos(PosCom::RH);
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user