From 39349dcd8102df48dca18c75103d23034e3b9ce9 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Mon, 29 May 2023 16:13:40 -0500 Subject: [PATCH] added: some keymaps and relevant structs --- src/graphics.rs | 6 +- src/graphics/keys.rs | 136 +++++++++++++++++++++++++++++++++++++++ src/graphics/selector.rs | 61 +----------------- src/keys.rs | 100 ++++++++++++++++++++++++++++ src/lib.rs | 6 ++ src/main.rs | 30 +++++++-- src/music/transport.rs | 6 -- 7 files changed, 272 insertions(+), 73 deletions(-) create mode 100644 src/graphics/keys.rs create mode 100644 src/keys.rs diff --git a/src/graphics.rs b/src/graphics.rs index a5de1b1..8e19bf6 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -1,7 +1,9 @@ +mod keys; mod map; mod point; mod selector; +pub use keys::*; pub use map::*; pub use point::*; pub use selector::*; @@ -50,7 +52,6 @@ pub fn draw_frame(map: &mut impl Map, offset: Point) -> Result<()> { } } } - stdout().flush()?; Ok(()) } @@ -62,6 +63,8 @@ pub fn run_map(map: &mut impl Map, offset: Point, time: Duration) -> Resul draw_frame(map, offset)?; + stdout().flush()?; + thread::sleep(time); } } @@ -77,6 +80,7 @@ pub fn loop_map( for _ in 0..steps { execute!(stdout(), terminal::Clear(terminal::ClearType::All))?; draw_frame(&mut tmp, offset)?; + stdout().flush()?; tmp.update(); thread::sleep(time); } diff --git a/src/graphics/keys.rs b/src/graphics/keys.rs new file mode 100644 index 0000000..bfb8d04 --- /dev/null +++ b/src/graphics/keys.rs @@ -0,0 +1,136 @@ +use crossterm::{ + event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers}, + execute, queue, terminal, +}; +use eyre::Result; +use std::time::Duration; + +pub fn action_loop(speed: usize) -> Result<()> { + loop { + if poll(Duration::from_millis(speed.try_into()?))? { + if let Event::Key(key) = read()? { + match key_event(key) { + Action::None => continue, + Action::Exit => crate::exit()?, + x => println!("{:?}", x), + } + } + } + } +} + +#[derive(Debug)] +pub enum Action { + None, + Move(Direction), + Resize(Direction), + Transport(Clock), + Channel(usize), + Select, + SelectArea, + Reload, + Randomize, + Exit, + Help, +} + +#[derive(Debug)] +pub enum Clock { + Stop, + Start, + Pause, + Faster(usize), + Slower(usize), +} + +#[derive(Debug)] +pub enum Direction { + Up, + Down, + Left, + Right, +} + +pub fn key_event(event: KeyEvent) -> Action { + match event.modifiers { + KeyModifiers::NONE => match_normal_key(event.code), + KeyModifiers::CONTROL => match_ctl_key(event.code), + KeyModifiers::ALT => match_alt_key(event.code), + KeyModifiers::SHIFT => match_shift_key(event.code), + _ => Action::None, + } +} + +pub fn match_normal_key(key: KeyCode) -> Action { + match key { + KeyCode::Enter => Action::Transport(Clock::Pause), + KeyCode::Esc => Action::Exit, + KeyCode::Up => Action::Move(Direction::Up), + KeyCode::Down => Action::Move(Direction::Down), + KeyCode::Right => Action::Move(Direction::Right), + KeyCode::Left => Action::Move(Direction::Left), + KeyCode::Char(c) => match c { + '?' => Action::Help, + ' ' => Action::Select, + 'q' => Action::Exit, + 'j' => Action::Move(Direction::Down), + 'k' => Action::Move(Direction::Up), + 'h' => Action::Move(Direction::Left), + 'l' => Action::Move(Direction::Right), + 'r' => Action::Reload, + '[' => Action::Transport(Clock::Slower(1)), + ']' => Action::Transport(Clock::Faster(1)), + '{' => Action::Transport(Clock::Slower(5)), + '}' => Action::Transport(Clock::Faster(5)), + '0' => Action::Channel(0), + '1' => Action::Channel(1), + '2' => Action::Channel(2), + '3' => Action::Channel(3), + '4' => Action::Channel(4), + '5' => Action::Channel(5), + '6' => Action::Channel(6), + '7' => Action::Channel(7), + '8' => Action::Channel(8), + '9' => Action::Channel(9), + _ => Action::None, + }, + _ => Action::None, + } +} + +pub fn match_ctl_key(key: KeyCode) -> Action { + match key { + KeyCode::Char(c) => match c { + 'c' => Action::Exit, + _ => Action::None, + }, + _ => Action::None, + } +} + +pub fn match_shift_key(key: KeyCode) -> Action { + match key { + KeyCode::Up => Action::Resize(Direction::Up), + KeyCode::Down => Action::Resize(Direction::Down), + KeyCode::Right => Action::Resize(Direction::Right), + KeyCode::Left => Action::Resize(Direction::Left), + KeyCode::Char(c) => match c { + 'J' => Action::Resize(Direction::Down), + 'K' => Action::Resize(Direction::Up), + 'H' => Action::Resize(Direction::Left), + 'L' => Action::Resize(Direction::Right), + 'R' => Action::Randomize, + _ => Action::None, + }, + _ => Action::None, + } +} + +pub fn match_alt_key(key: KeyCode) -> Action { + match key { + KeyCode::Char(c) => match c { + _ => Action::None, + }, + _ => Action::None, + } +} diff --git a/src/graphics/selector.rs b/src/graphics/selector.rs index 51298d4..7edf45b 100644 --- a/src/graphics/selector.rs +++ b/src/graphics/selector.rs @@ -1,16 +1,12 @@ use crate::Point; use crossterm::{ - cursor, - event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers}, - execute, queue, + cursor, queue, style::{self, Attribute, Color}, - terminal, }; use eyre::Result; use std::{ io::stdout, ops::{Deref, DerefMut}, - time::Duration, }; pub struct Cursor { @@ -57,59 +53,4 @@ impl Cursor { )?; Ok(()) } - - pub fn update(&mut self, event: KeyEvent) -> Result<()> { - let key = event.code; - match event.modifiers { - KeyModifiers::NONE => match key { - KeyCode::Char(c) => match c { - 'j' => self.y += 1, - 'k' => { - if self.y != 0 { - self.y -= 1 - } - } - 'h' => { - if self.x != 0 { - self.x -= 1 - } - } - 'l' => self.x += 1, - _ => {} - }, - _ => {} - }, - KeyModifiers::CONTROL => match key { - KeyCode::Char(c) => match c { - 'c' => std::process::exit(0), - _ => {} - }, - _ => {} - }, - KeyModifiers::ALT => match key { - _ => {} - }, - KeyModifiers::SHIFT => match key { - _ => {} - }, - _ => {} - }; - Ok(()) - } -} - -pub fn move_cursor() -> Result<()> { - let mut cursor = Cursor::new(); - loop { - if poll(Duration::from_millis(10))? { - execute!(stdout(), terminal::Clear(terminal::ClearType::All))?; - match read()? { - Event::Key(event) => { - cursor.update(event)?; - } - _ => continue, - } - } - cursor.render()?; - } } diff --git a/src/keys.rs b/src/keys.rs new file mode 100644 index 0000000..f110cd8 --- /dev/null +++ b/src/keys.rs @@ -0,0 +1,100 @@ +use crossterm::{ + event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers}, + execute, queue, terminal, +}; + +use super::*; + +pub enum Action { + None, + Move(Direction), + Resize(Direction), + Transport(Transport), + Select, + UnSelect, + Exit, +} + +pub enum Transport { + Stop, + Start, + Pause, + Faster(usize), + Slower(usize), +} + +pub enum Direction { + Up, + Down, + Left, + Right, +} + +pub fn key_event(event: KeyEvent) -> Action { + match event.modifiers { + KeyModifiers::NONE => match_normal_key(event.code), + KeyModifiers::CONTROL => match_ctl_key(event.code), + KeyModifiers::ALT => match_alt_key(event.code), + KeyModifiers::SHIFT => match_shift_key(event.code), + _ => Action::None, + } +} + +pub fn match_normal_key(key: KeyCode) -> Action { + match key { + KeyCode::Enter => Action::Transport(Transport::Pause), + KeyCode::Esc => Action::Exit, + KeyCode::Up => Action::Move(Direction::Up), + KeyCode::Down => Action::Move(Direction::Down), + KeyCode::Right => Action::Move(Direction::Right), + KeyCode::Left => Action::Move(Direction::Left), + KeyCode::Char(c) => match c { + ' ' => Action::Select, + 'q' => Action::Exit, + 'j' => Action::Move(Direction::Down), + 'k' => Action::Move(Direction::Up), + 'h' => Action::Move(Direction::Left), + 'l' => Action::Move(Direction::Right), + _ => Action::None, + }, + _ => Action::None, + } +} + +pub fn match_ctl_key(key: KeyCode) -> Action { + match key { + KeyCode::Char(c) => match c { + 'c' => Action::Exit, + _ => Action::None, + }, + _ => Action::None, + } +} + +pub fn match_shift_key(key: KeyCode) -> Action { + match key { + KeyCode::Enter => Action::Transport(Transport::Stop), + KeyCode::Up => Action::Resize(Direction::Up), + KeyCode::Down => Action::Resize(Direction::Down), + KeyCode::Right => Action::Resize(Direction::Right), + KeyCode::Left => Action::Resize(Direction::Left), + KeyCode::Char(c) => match c { + ' ' => Action::UnSelect, + 'j' => Action::Resize(Direction::Down), + 'k' => Action::Resize(Direction::Up), + 'h' => Action::Resize(Direction::Left), + 'l' => Action::Resize(Direction::Right), + _ => Action::None, + }, + _ => Action::None, + } +} + +pub fn match_alt_key(key: KeyCode) -> Action { + match key { + KeyCode::Char(c) => match c { + _ => Action::None, + }, + _ => Action::None, + } +} diff --git a/src/lib.rs b/src/lib.rs index f5e7525..131c072 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,8 +6,14 @@ pub use cells::*; pub use graphics::*; pub use music::*; +use eyre::Result; use std::time::Duration; +pub fn exit() -> Result<()> { + crossterm::terminal::disable_raw_mode()?; + std::process::exit(0); +} + pub fn bpm_to_ms(bpm: usize) -> Duration { let ms = 60000 / bpm; Duration::from_millis(ms.try_into().unwrap()) diff --git a/src/main.rs b/src/main.rs index 4a8445a..a1c450e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,34 @@ use cellseq::*; use eyre::Result; +use crossterm::{ + event::{poll, read, Event}, + terminal, +}; + fn main() -> Result<()> { - crossterm::terminal::enable_raw_mode()?; + terminal::enable_raw_mode()?; + + action_loop(10)?; + // let mut mask: Mask = Mask::new(48, 24, false); + // + // let mut map = World::new(74, 32); + // map.randomize(0.75); - let mut mask: Mask = Mask::new(48, 24, false); - let mut map = World::new(74, 32); - map.randomize(0.75); + // let time = bpm_to_ms(100); - let time = bpm_to_ms(100); + // loop { + // if poll(std::time::Duration::from_millis(50))? { + // // It's guaranteed that the `read()` won't block when the `poll()` + // // function returns `true` + // match read()? { + // Event::Key(event) => println!("\n{:?}", event), + // _ => continue, + // } + // } + // } - run_map(&mut map, Point::new(10, 2), time)?; + // run_map(&mut map, Point::new(10, 2), time)?; // edit_mask(&mask, (10, 5))?; // move_cursor()?; Ok(()) diff --git a/src/music/transport.rs b/src/music/transport.rs index 0c147d1..ad19bdb 100644 --- a/src/music/transport.rs +++ b/src/music/transport.rs @@ -22,9 +22,3 @@ impl Song { } } } - -pub enum Transport { - Pause, - Play, - FastForward(usize), -} -- 2.44.2