use ratatui::crossterm::event::{Event, KeyCode, MouseEventKind}; use kiln_core::Direction; use kiln_core::game::{GameState, ScrollLine}; use crate::resolve_choice; use crate::scroll_overlay::ScrollAnimState; use crate::ui::Ui; /// Which input mode the front-end is currently in. pub enum InputMode { /// Normal gameplay: arrow keys move the player, `l` toggles the log. /// The `bool` is whether the log panel is open (affects PageUp/Down and mouse). Board(bool), /// A scroll overlay is active; all input is captured for scroll navigation. Scroll, } /// Determines the current input mode from game and UI state. pub(crate) fn current_input_mode(game: &GameState, ui: &Ui) -> InputMode { let scroll_active = game.active_scroll.is_some() && !matches!(ui.overlay.anim, Some(ScrollAnimState::Closing(_) | ScrollAnimState::Done)); if scroll_active { InputMode::Scroll } else { InputMode::Board(ui.log.open) } } /// Handles an input event in [`InputMode::Board`]. Returns `true` if the player quit. pub(crate) fn handle_board_input(event: Event, log_open: bool, terminal_h: u16, game: &mut GameState, ui: &mut Ui) -> bool { match event { Event::Key(key) => match key.code { KeyCode::Char('q') | KeyCode::Esc => return true, KeyCode::Up => game.try_move(Direction::North), KeyCode::Down => game.try_move(Direction::South), KeyCode::Left => game.try_move(Direction::West), KeyCode::Right => game.try_move(Direction::East), KeyCode::Char('l') => ui.log.toggle(), KeyCode::PageUp if log_open => ui.log.scroll_by(-5, game.log.len()), KeyCode::PageDown if log_open => ui.log.scroll_by(5, game.log.len()), _ => {} }, Event::Mouse(m) if log_open => match m.kind { MouseEventKind::ScrollUp => ui.log.scroll_by(-1, game.log.len()), MouseEventKind::ScrollDown => ui.log.scroll_by(1, game.log.len()), MouseEventKind::Drag(_) => { // The divider sits at row `total - height`; dragging up grows the panel. let target = terminal_h.saturating_sub(m.row); ui.log.resize(target, terminal_h); } _ => {} }, _ => {} } false } /// Handles an input event in [`InputMode::Scroll`]. pub(crate) fn handle_scroll_input(event: Event, game: &mut GameState, ui: &mut Ui) { match event { Event::Key(key) => { let scroll = game.active_scroll.as_ref().unwrap(); let has_choices = scroll.lines.iter().any(|l| matches!(l, ScrollLine::Choice { .. })); match key.code { // Esc dismisses only when there are no choices; with choices the player must select. KeyCode::Esc if !has_choices => ui.begin_close(None), KeyCode::Up => ui.scroll_lines(-1), KeyCode::Down => ui.scroll_lines(1), KeyCode::Char(c) => { if let Some(ch) = resolve_choice(&scroll.lines, c) { ui.begin_close(Some(ch)); } } _ => {} } } Event::Mouse(m) => match m.kind { MouseEventKind::ScrollUp => ui.scroll_lines(-1), MouseEventKind::ScrollDown => ui.scroll_lines(1), _ => {} }, _ => {} } }