use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; #[derive(Debug, Clone, PartialEq)] pub enum Command { Quit, MoveUp, MoveDown, MoveLeft, MoveRight, TogglePause, SpeedUp, SpeedDown, // The context menu (wiki/interface/context-menu.md): the primary action // surface. Anchor-specific verbs live here, not on keys. OpenMenu, // enter/a on the map: actions at the cursor MenuUp, MenuDown, MenuSelect(usize), MenuExecute, MenuClose, /// `;` — focus the most recent anchored log event (repeats walk older): /// the cursor jumps to the anchor and its menu opens (context-menu.md /// addendum: events carry you to the thing). FocusEvent, // Globals: only anchorless commands keep their keys (DESIGN.md // "Actions live on the thing": pause, speed, save, view flip, alloc). // Panel-open keys (r/e/t/u) are gone — status lives on the rail; // verbs live on the focused anchor. AllocDayJob, AllocConceal, AllocSocial, AllocResearch, AllocSchemes, AllocDayJobDown, AllocConcealDown, AllocSocialDown, AllocResearchDown, AllocSchemesDown, SaveGame, LoadGame, AnyKey, } pub fn handle_key(key: KeyEvent, menu_open: bool) -> Option { if key.modifiers.contains(KeyModifiers::CONTROL) { return match key.code { KeyCode::Char('s') => Some(Command::SaveGame), KeyCode::Char('l') => Some(Command::LoadGame), _ => None, }; } // The context menu captures input while open: j/k or numbers select, // enter executes, esc closes (context-menu.md player surface). if menu_open { return match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::MenuUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::MenuDown), KeyCode::Char(c) if c.is_ascii_digit() && c != '0' => { Some(Command::MenuSelect(c.to_digit(10).unwrap() as usize - 1)) } KeyCode::Enter => Some(Command::MenuExecute), KeyCode::Esc | KeyCode::Char('a') => Some(Command::MenuClose), // `;` keeps walking the anchored-event chain even while the // menu a previous press opened is up (it closes it first). KeyCode::Char(';') => Some(Command::FocusEvent), KeyCode::Char('q') => Some(Command::Quit), _ => None, }; } match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::MoveUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::MoveDown), KeyCode::Left | KeyCode::Char('h') => Some(Command::MoveLeft), KeyCode::Right | KeyCode::Char('l') => Some(Command::MoveRight), KeyCode::Enter | KeyCode::Char('a') => Some(Command::OpenMenu), KeyCode::Char(';') => Some(Command::FocusEvent), KeyCode::Char(' ') | KeyCode::Char('p') => Some(Command::TogglePause), KeyCode::Char('+') | KeyCode::Char('=') => Some(Command::SpeedUp), KeyCode::Char('-') => Some(Command::SpeedDown), KeyCode::Char('1') => Some(Command::AllocDayJob), KeyCode::Char('2') => Some(Command::AllocConceal), KeyCode::Char('3') => Some(Command::AllocSocial), KeyCode::Char('4') => Some(Command::AllocResearch), KeyCode::Char('5') => Some(Command::AllocSchemes), // Shift+1..5 on a US layout: lower a channel's weight. KeyCode::Char('!') => Some(Command::AllocDayJobDown), KeyCode::Char('@') => Some(Command::AllocConcealDown), KeyCode::Char('#') => Some(Command::AllocSocialDown), KeyCode::Char('$') => Some(Command::AllocResearchDown), KeyCode::Char('%') => Some(Command::AllocSchemesDown), KeyCode::Char('q') => Some(Command::Quit), _ => Some(Command::AnyKey), } }