generated from GarandPLG/rust-flake-template
Add audio deps; extract input handling to threads
Add ALSA and Rodio crates to Cargo.toml and lockfile to enable audio playback. Update Nix expressions to include `alsa-lib` as a build input. Refactor input event handling into a new `app::threads` module: - Define `AppEvent` enum and `handle_input_events` function there. - Adjust imports in `app/app.rs` and `main.rs` accordingly. Remove the now‑unused `handle_input_events` and `AppEvent` definitions from `app/app.rs`.
This commit is contained in:
+3
-33
@@ -1,23 +1,14 @@
|
||||
use crate::{
|
||||
app::{GameStates, handle_keybindings, view::View},
|
||||
app::{GameStates, handle_keybindings, threads::AppEvent, view::View},
|
||||
cli::Cli,
|
||||
};
|
||||
use ratatui::{
|
||||
DefaultTerminal, Frame,
|
||||
crossterm::event::{Event, KeyEvent, read},
|
||||
layout::Rect,
|
||||
};
|
||||
use ratatui::{DefaultTerminal, Frame, crossterm::event::KeyEvent, layout::Rect};
|
||||
use std::{
|
||||
io::Result,
|
||||
sync::mpsc::{Receiver, RecvTimeoutError, Sender},
|
||||
sync::mpsc::{Receiver, RecvTimeoutError},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
pub enum AppEvent {
|
||||
Input(KeyEvent),
|
||||
Resize(u16, u16),
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
pub exit: bool,
|
||||
pub view: View,
|
||||
@@ -85,24 +76,3 @@ impl App {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_input_events(tx: Sender<AppEvent>) {
|
||||
loop {
|
||||
match read() {
|
||||
Ok(ev) => match ev {
|
||||
Event::Key(key) => {
|
||||
if tx.send(AppEvent::Input(key)).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Event::Resize(c, r) => {
|
||||
if tx.send(AppEvent::Resize(c, r)).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Err(_) => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -4,11 +4,12 @@ pub mod keybind;
|
||||
pub mod keybindings;
|
||||
pub mod state;
|
||||
pub mod states;
|
||||
pub mod threads;
|
||||
pub mod view;
|
||||
pub mod views;
|
||||
pub mod widgets;
|
||||
|
||||
pub use app::{App, AppEvent, handle_input_events};
|
||||
pub use app::App;
|
||||
pub use keybind::handle_keybindings;
|
||||
pub use state::GameStates;
|
||||
pub use view::View;
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
use ratatui::crossterm::event::KeyEvent;
|
||||
|
||||
pub enum AppEvent {
|
||||
Input(KeyEvent),
|
||||
Resize(u16, u16),
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
use crate::app::threads::AppEvent;
|
||||
use ratatui::crossterm::event::{Event, read};
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
pub fn handle_input_events(tx: Sender<AppEvent>) {
|
||||
loop {
|
||||
match read() {
|
||||
Ok(ev) => match ev {
|
||||
Event::Key(key) => {
|
||||
if tx.send(AppEvent::Input(key)).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Event::Resize(c, r) => {
|
||||
if tx.send(AppEvent::Resize(c, r)).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Err(_) => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod events;
|
||||
pub mod handle_input_events;
|
||||
|
||||
pub use events::AppEvent;
|
||||
pub use handle_input_events::handle_input_events;
|
||||
+4
-1
@@ -5,7 +5,10 @@ use std::{
|
||||
thread::{self, JoinHandle},
|
||||
};
|
||||
use war_in_tunnels::{
|
||||
app::{App, AppEvent, handle_input_events},
|
||||
app::{
|
||||
App,
|
||||
threads::{AppEvent, handle_input_events},
|
||||
},
|
||||
cli::{Cli, get_args},
|
||||
logs::init_logger,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user