Something went wrong. Try again.
A video game where you play as a misaligned AI, deceiving and building power. An experiment in spec-driven development. forked from cameron.stream/misaligned
Something went wrong. Try again.
3.9 kB · 96 lines
Rust
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697use 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<Command> { 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), }}