1use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
4use std::time::Duration;
5
6use super::state::MapState;
7
8pub enum MapAction {
10 None,
12 Resized(u16, u16),
14}
15
16pub fn handle_input(state: &mut MapState) -> std::io::Result<MapAction> {
21 if !event::poll(Duration::from_millis(100))? {
22 return Ok(MapAction::None);
23 }
24
25 match event::read()? {
26 Event::Key(key) => {
27 handle_key(key, state);
28 Ok(MapAction::None)
29 }
30 Event::Resize(w, h) => {
31 state.resize(w, h);
32 Ok(MapAction::Resized(w, h))
33 }
34 _ => Ok(MapAction::None),
35 }
36}
37
38fn handle_key(key: KeyEvent, state: &mut MapState) {
40 if state.show_help {
42 state.show_help = false;
43 return;
44 }
45
46 match key.code {
47 KeyCode::Up => state.move_cursor(0, -1),
49 KeyCode::Down => state.move_cursor(0, 1),
50 KeyCode::Left => state.move_cursor(-1, 0),
51 KeyCode::Right => state.move_cursor(1, 0),
52
53 KeyCode::Enter | KeyCode::Char('+') => state.zoom_in(),
55 KeyCode::Esc | KeyCode::Char('-') if !state.zoom_out() => {
56 state.should_quit = true;
57 }
58
59 KeyCode::Char('q') => state.should_quit = true,
61 KeyCode::Char('c') if key.modifiers == KeyModifiers::NONE => {
62 state.center_on_player();
63 }
64 KeyCode::Char('?') => state.show_help = !state.show_help,
65
66 KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
68 state.should_quit = true;
69 }
70
71 _ => {}
72 }
73}