Browse Source

Got interactivity working

master
Thomas Johnson 2 years ago
parent
commit
6d5e0c1cbc
  1. 3
      Cargo.toml
  2. 7
      src/grid.rs
  3. 14
      src/head.rs
  4. 52
      src/main.rs

3
Cargo.toml

@ -4,6 +4,5 @@ version = "0.1.0"
authors = ["thajohns <thajohns@clarkson.edu>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
termion = "1.5.5"

7
src/grid.rs

@ -30,6 +30,7 @@ impl Grid {
}
// TODO: use for resizing
// FIXME: probably broken
pub fn linear_idx_to_coords(linear_idx: usize, dims: (usize, usize), origin: (isize, isize)) -> Option<(isize, isize)> {
let unadjusted = ((linear_idx / dims.0), (linear_idx % dims.0));
if unadjusted.0 >= dims.0 || unadjusted.1 >= dims.1 {
@ -45,7 +46,8 @@ impl Grid {
if !(0..dims.0 as isize).contains(&adjusted.0) || !(0..dims.1 as isize).contains(&adjusted.1) {
None
} else {
Some(adjusted.0 as usize * dims.0 + adjusted.1 as usize)
let linidx = adjusted.1 as usize * dims.0 + adjusted.0 as usize;
Some(linidx)
}
}
@ -58,8 +60,9 @@ impl Grid {
let mut s = String::new();
for y in 0..self.dims.1 {
for x in 0..self.dims.0 {
s.push(self[(x as isize, y as isize)].to_char());
s.push(self[(x as isize - self.origin.0, y as isize - self.origin.1)].to_char());
}
s.push('\r');
s.push('\n');
}
s

14
src/head.rs

@ -32,6 +32,15 @@ G: Clone + Debug + Deref<Target=RefCell<Grid>> {
}
}
pub fn checked_go(&mut self, dir: Direction) -> bool {
if self.grid.borrow().in_bounds(add_v2(self.coords, dir.offset())) {
self.go(dir);
true
} else {
false
}
}
pub fn up(&mut self) {
self.down = false;
}
@ -40,6 +49,10 @@ G: Clone + Debug + Deref<Target=RefCell<Grid>> {
self.down = true;
}
pub fn toggle_down(&mut self) {
self.down = !self.down;
}
pub fn coords(&self) -> (isize, isize) {
self.coords
}
@ -48,4 +61,3 @@ G: Clone + Debug + Deref<Target=RefCell<Grid>> {
self.coords = coords;
}
}

52
src/main.rs

@ -9,27 +9,39 @@ use core::cell::RefCell;
use crate::grid::{Grid, Direction::*};
use crate::head::Head;
use termion::{raw::IntoRawMode, input::TermRead, event::Key, cursor::Goto};
fn main() {
let grid = Rc::new(RefCell::new(Grid::new((8, 8), (0, 0))));
if !termion::is_tty(&std::io::stdout()) {
panic!("this program must be used on a tty!");
}
let _stdout = std::io::stdout().into_raw_mode().unwrap();
let mut keys = std::io::stdin().keys();
let dims = termion::terminal_size().unwrap();
let grid = Rc::new(RefCell::new(Grid::new((dims.0 as usize, dims.1 as usize), (dims.0 as isize / 2, dims.1 as isize / 2))));
let mut head = Head::new_at(grid.clone(), (0, 0));
for dir in [
South,
East,
North,
East,
East,
South,
West,
South,
East,
South,
West,
West,
North,
West,
South,
].iter() {
head.go(*dir);
loop {
print!("{}{}", Goto(1, 1), grid.borrow().to_2d_string());
let key = keys.next();
if let Some(Ok(key)) = key {
match key.clone() {
Key::Up | Key::Down | Key::Left | Key::Right => {
let dir = match key {
Key::Up => North,
Key::Right => East,
Key::Down => South,
Key::Left => West,
_ => unreachable!(),
};
head.go(dir);
}
Key::Char('\n') => head.toggle_down(),
Key::Char('q') => break,
_ => {}
}
} else {
break
}
}
print!("{}", grid.borrow().to_2d_string());
print!("{}{}", termion::clear::All, Goto(1, 1));
}
Loading…
Cancel
Save