use crossterm::event::{KeyCode, KeyEvent}; #[derive(Debug, Clone, PartialEq)] pub enum Command { Quit, MoveUp, MoveDown, MoveLeft, MoveRight, EnterBuild, ExitBuild, CursorUp, CursorDown, CursorLeft, CursorRight, CycleCategory, CycleItemNext, CycleItemPrev, PlaceTile, DemolishTile, OpenResearch, ResearchUp, ResearchDown, ResearchBuy, ResearchClose, NextWave, Wait, // Defend phase controls SpeedUp, SpeedDown, TogglePause, // Title screen AnyKey, // World map OpenWorldMap, WorldMapUp, WorldMapDown, WorldMapSelect, WorldMapClose, // Minion hiring OpenHireMenu, HireUp, HireDown, HireSelect, HireClose, } pub fn handle_key( key: KeyEvent, build_mode: bool, research_menu: bool, defend_phase: bool, world_map: bool, hire_menu: bool, ) -> Option { // Research menu keys if research_menu { return match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::ResearchUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::ResearchDown), KeyCode::Enter => Some(Command::ResearchBuy), KeyCode::Esc => Some(Command::ResearchClose), _ => None, }; } // Hire menu keys if hire_menu { return match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::HireUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::HireDown), KeyCode::Enter => Some(Command::HireSelect), KeyCode::Esc | KeyCode::Char('f') => Some(Command::HireClose), KeyCode::Char('q') => Some(Command::Quit), _ => None, }; } // World map keys if world_map { return match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::WorldMapUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::WorldMapDown), KeyCode::Enter => Some(Command::WorldMapSelect), KeyCode::Esc | KeyCode::Char('m') => Some(Command::WorldMapClose), KeyCode::Char('q') => Some(Command::Quit), _ => None, }; } // Build mode keys if build_mode { return match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::CursorUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::CursorDown), KeyCode::Left => Some(Command::CursorLeft), KeyCode::Right | KeyCode::Char('l') => Some(Command::CursorRight), KeyCode::Tab => Some(Command::CycleCategory), KeyCode::Char('n') => Some(Command::CycleItemNext), KeyCode::Char('p') => Some(Command::CycleItemPrev), KeyCode::Char(' ') => Some(Command::PlaceTile), KeyCode::Char('x') => Some(Command::DemolishTile), KeyCode::Char('f') => Some(Command::OpenHireMenu), KeyCode::Esc => Some(Command::ExitBuild), KeyCode::Char('q') => Some(Command::Quit), _ => None, }; } // Defend phase keys (limited controls) if defend_phase { return match key.code { KeyCode::Char('+') | KeyCode::Char('=') => Some(Command::SpeedUp), KeyCode::Char('-') => Some(Command::SpeedDown), KeyCode::Char(' ') | KeyCode::Char('p') => Some(Command::TogglePause), KeyCode::Char('q') => Some(Command::Quit), _ => None, }; } // Normal (build) mode keys 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::Char('b') => Some(Command::EnterBuild), KeyCode::Char('r') => Some(Command::OpenResearch), KeyCode::Char('f') => Some(Command::OpenHireMenu), KeyCode::Char('m') => Some(Command::OpenWorldMap), KeyCode::Char(' ') => Some(Command::NextWave), KeyCode::Char('.') => Some(Command::Wait), KeyCode::Char('q') => Some(Command::Quit), _ => Some(Command::AnyKey), } }