]> git.huck.website - cellseq.git/commitdiff
added: some keymaps and relevant structs
authorHuck Boles <huck@huck.website>
Mon, 29 May 2023 21:13:40 +0000 (16:13 -0500)
committerHuck Boles <huck@huck.website>
Mon, 29 May 2023 21:13:40 +0000 (16:13 -0500)
src/graphics.rs
src/graphics/keys.rs [new file with mode: 0644]
src/graphics/selector.rs
src/keys.rs [new file with mode: 0644]
src/lib.rs
src/main.rs
src/music/transport.rs

index a5de1b1cba7af2b7a755d64e584cd33d81b348f7..8e19bf6d5ce6b39c7e63bd210c3deb5c9747b191 100644 (file)
@@ -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<T>(map: &mut impl Map<T>, offset: Point) -> Result<()> {
             }
         }
     }
-    stdout().flush()?;
     Ok(())
 }
 
@@ -62,6 +63,8 @@ pub fn run_map<T>(map: &mut impl Map<T>, offset: Point, time: Duration) -> Resul
 
         draw_frame(map, offset)?;
 
+        stdout().flush()?;
+
         thread::sleep(time);
     }
 }
@@ -77,6 +80,7 @@ pub fn loop_map<T>(
         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 (file)
index 0000000..bfb8d04
--- /dev/null
@@ -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,
+    }
+}
index 51298d4b76e0cbe2d3100e918ab8bb4bafbc3bcd..7edf45b50961df6bcb0c646b661263c7aba5e908 100644 (file)
@@ -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 (file)
index 0000000..f110cd8
--- /dev/null
@@ -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,
+    }
+}
index f5e7525e6e21248207591c11dde31b603d59f845..131c0725f7843faeb3b7c55f6e0da90e6f6004e7 100644 (file)
@@ -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())
index 4a8445a923f2adb9e9206749d4115c1ef2fc9918..a1c450e67b7bb9c417be10bccccbd91e452061ee 100644 (file)
@@ -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<bool> = Mask::new(48, 24, false);
+    //
+    // let mut map = World::new(74, 32);
+    // map.randomize(0.75);
 
-    let mut mask: Mask<bool> = 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(())
index 0c147d1b9f3615b9ab5cd21a977e5b10b45a786f..ad19bdb6a4821857faee182ecb38e6a9176bd23a 100644 (file)
@@ -22,9 +22,3 @@ impl Song {
         }
     }
 }
-
-pub enum Transport {
-    Pause,
-    Play,
-    FastForward(usize),
-}