Skip to content

Commit

Permalink
implement handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Dec 28, 2024
1 parent 3717885 commit 8bbf86e
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/screen/tui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::gameboy::Gameboy;
use crate::input::KeypadKey;
use std::env;
use std::{
error::Error,
Expand Down Expand Up @@ -87,14 +88,25 @@ fn run_app<B: Backend>(
if let Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Press {
if let KeyCode::Char(c) = key.code {
app.on_key(c);
app.on_key(c, gameboy);
} else if let KeyCode::Up = key.code {
gameboy.keydown(KeypadKey::Up);
} else if let KeyCode::Down = key.code {
gameboy.keydown(KeypadKey::Down);
} else if let KeyCode::Left = key.code {
gameboy.keydown(KeypadKey::Left);
} else if let KeyCode::Right = key.code {
gameboy.keydown(KeypadKey::Right);
}
}
}
}
if last_tick.elapsed() >= app.tick_rate {
app.on_tick(gameboy);
gameboy.frame();
if let Some(key) = app.last_key.take() {
gameboy.keyup(key);
}
last_tick = Instant::now();
}
if app.should_quit {
Expand All @@ -105,6 +117,7 @@ fn run_app<B: Backend>(

struct App {
should_quit: bool,
last_key: Option<KeypadKey>,
tick_rate: Duration,
split_percent: u16,

Expand Down Expand Up @@ -162,6 +175,7 @@ impl App {
tick_rate: Duration::from_millis(5),
split_percent: 40,
picker,
last_key: None,
image_source,

image_static,
Expand All @@ -170,7 +184,7 @@ impl App {
image_static_offset: (0, 0),
}
}
pub fn on_key(&mut self, c: char) {
pub fn on_key(&mut self, c: char, gameboy: &mut Gameboy) {
match c {
'q' => {
self.should_quit = true;
Expand Down Expand Up @@ -206,6 +220,22 @@ impl App {
'l' => {
self.image_static_offset.0 += 1;
}
'a' | 'A' => {
gameboy.keydown(KeypadKey::A);
self.last_key = Some(KeypadKey::A);
}
'b' | 'B' => {
gameboy.keydown(KeypadKey::B);
self.last_key = Some(KeypadKey::B);
}
'z' | 'Z' => {
gameboy.keydown(KeypadKey::Select);
self.last_key = Some(KeypadKey::Select);
}
'x' | 'X' => {
gameboy.keydown(KeypadKey::Start);
self.last_key = Some(KeypadKey::Start);
}
_ => {}
}
}
Expand Down

0 comments on commit 8bbf86e

Please sign in to comment.