2026-06-11 22:09:32 -05:00
|
|
|
use ratatui::crossterm::event::{Event, KeyCode, MouseEventKind};
|
|
|
|
|
use kiln_core::Direction;
|
|
|
|
|
use kiln_core::game::{GameState, ScrollLine};
|
2026-06-15 10:42:21 -05:00
|
|
|
use crate::menu::{MenuItem, MenuKey, pause_menu_items, ActionRc as MenuActionRc};
|
2026-06-11 22:09:32 -05:00
|
|
|
use crate::ui::Ui;
|
|
|
|
|
|
2026-06-13 18:43:29 -05:00
|
|
|
/// Moves the player and, if the move crosses a portal, captures both board Rcs
|
|
|
|
|
/// and stores them in [`Ui::pending_transition`] for the next draw call.
|
|
|
|
|
fn try_move_with_transition(game: &mut GameState, ui: &mut Ui, dir: Direction) {
|
|
|
|
|
let old_rc = game.board_rc();
|
|
|
|
|
let old_name = game.current_board_name().to_string();
|
|
|
|
|
game.try_move(dir);
|
|
|
|
|
if game.current_board_name() != old_name {
|
|
|
|
|
// Portal crossed: queue transition; consume the engine-side signal.
|
|
|
|
|
ui.pending_transition = Some((old_rc, game.board_rc()));
|
|
|
|
|
game.board_transition = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 22:09:32 -05:00
|
|
|
/// Which input mode the front-end is currently in.
|
|
|
|
|
pub enum InputMode {
|
|
|
|
|
/// Normal gameplay: arrow keys move the player, `l` toggles the log.
|
2026-06-13 18:43:29 -05:00
|
|
|
Board,
|
2026-06-11 22:09:32 -05:00
|
|
|
/// A scroll overlay is active; all input is captured for scroll navigation.
|
|
|
|
|
Scroll,
|
2026-06-15 10:42:21 -05:00
|
|
|
/// A menu overlay is active; all input is captured for menu navigation.
|
|
|
|
|
Menu,
|
2026-06-15 09:15:30 -05:00
|
|
|
/// An animation is running; all input is discarded.
|
|
|
|
|
Animating,
|
|
|
|
|
/// Reserved for a post-transition freeze; nothing currently produces this mode.
|
|
|
|
|
#[allow(dead_code)]
|
2026-06-13 18:43:29 -05:00
|
|
|
Frozen,
|
2026-06-11 22:09:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Determines the current input mode from game and UI state.
|
|
|
|
|
pub(crate) fn current_input_mode(game: &GameState, ui: &Ui) -> InputMode {
|
2026-06-15 09:15:30 -05:00
|
|
|
// Any active animation (or a pending transition waiting for its area) blocks input.
|
|
|
|
|
if ui.active_animation.is_some() || ui.pending_transition.is_some() {
|
|
|
|
|
return InputMode::Animating;
|
2026-06-13 18:43:29 -05:00
|
|
|
}
|
2026-06-15 10:42:21 -05:00
|
|
|
if ui.menu.is_some() { return InputMode::Menu; }
|
2026-06-15 09:15:30 -05:00
|
|
|
if game.active_scroll.is_some() { InputMode::Scroll } else { InputMode::Board }
|
2026-06-11 22:09:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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 {
|
2026-06-15 10:42:21 -05:00
|
|
|
KeyCode::Esc => ui.open_menu(pause_menu_items()),
|
2026-06-13 18:43:29 -05:00
|
|
|
KeyCode::Up => try_move_with_transition(game, ui, Direction::North),
|
|
|
|
|
KeyCode::Down => try_move_with_transition(game, ui, Direction::South),
|
|
|
|
|
KeyCode::Left => try_move_with_transition(game, ui, Direction::West),
|
|
|
|
|
KeyCode::Right => try_move_with_transition(game, ui, Direction::East),
|
2026-06-11 22:09:32 -05:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 22:17:46 -05:00
|
|
|
/// Returns the choice string for the given key character, scanning the scroll lines.
|
|
|
|
|
fn resolve_choice(lines: &[ScrollLine], c: char) -> Option<String> {
|
|
|
|
|
let mut letter = b'a';
|
|
|
|
|
for line in lines {
|
|
|
|
|
if let ScrollLine::Choice { choice, .. } = line {
|
|
|
|
|
if c == letter as char { return Some(choice.clone()); }
|
|
|
|
|
letter += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 22:09:32 -05:00
|
|
|
/// Handles an input event in [`InputMode::Scroll`].
|
|
|
|
|
pub(crate) fn handle_scroll_input(event: Event, game: &mut GameState, ui: &mut Ui) {
|
2026-06-15 09:15:30 -05:00
|
|
|
let lines: Vec<ScrollLine> = game.active_scroll.as_ref()
|
|
|
|
|
.map(|s| s.lines.clone())
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
let has_choices = lines.iter().any(|l| matches!(l, ScrollLine::Choice { .. }));
|
|
|
|
|
|
2026-06-11 22:09:32 -05:00
|
|
|
match event {
|
2026-06-15 09:15:30 -05:00
|
|
|
Event::Key(key) => match key.code {
|
|
|
|
|
// Esc dismisses only when there are no choices.
|
|
|
|
|
KeyCode::Esc if !has_choices => ui.begin_close(None, game),
|
|
|
|
|
KeyCode::Up => ui.scroll_lines(-1),
|
|
|
|
|
KeyCode::Down => ui.scroll_lines(1),
|
|
|
|
|
KeyCode::Char(c) => {
|
|
|
|
|
if let Some(ch) = resolve_choice(&lines, c) {
|
|
|
|
|
ui.begin_close(Some(ch), game);
|
2026-06-11 22:09:32 -05:00
|
|
|
}
|
|
|
|
|
}
|
2026-06-15 09:15:30 -05:00
|
|
|
_ => {}
|
|
|
|
|
},
|
2026-06-11 22:09:32 -05:00
|
|
|
Event::Mouse(m) => match m.kind {
|
|
|
|
|
MouseEventKind::ScrollUp => ui.scroll_lines(-1),
|
|
|
|
|
MouseEventKind::ScrollDown => ui.scroll_lines(1),
|
|
|
|
|
_ => {}
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-15 10:42:21 -05:00
|
|
|
|
|
|
|
|
/// Handles an input event in [`InputMode::Menu`].
|
|
|
|
|
///
|
|
|
|
|
/// Uses the clone-then-dispatch pattern: the action `Rc` is cloned to release
|
|
|
|
|
/// the immutable borrow on `ui.menu`, then the action is called with full `&mut Ui`.
|
|
|
|
|
pub(crate) fn handle_menu_input(event: Event, game: &mut GameState, ui: &mut Ui) {
|
|
|
|
|
match event {
|
|
|
|
|
Event::Key(key) => {
|
|
|
|
|
let pressed = match key.code {
|
|
|
|
|
KeyCode::Esc => Some(MenuKey::Esc),
|
|
|
|
|
KeyCode::Char(c) => Some(MenuKey::Char(c)),
|
|
|
|
|
KeyCode::Up => { ui.scroll_menu(-1); None }
|
|
|
|
|
KeyCode::Down => { ui.scroll_menu(1); None }
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
if let Some(mk) = pressed {
|
|
|
|
|
// Clone the Rc to release the borrow on ui.menu before calling the action.
|
|
|
|
|
let action: Option<MenuActionRc> = ui.menu.as_ref()
|
|
|
|
|
.and_then(|m| m.items.iter().find(|i| i.key == mk))
|
|
|
|
|
.map(|i| i.action_rc());
|
|
|
|
|
if let Some(action) = action {
|
|
|
|
|
MenuItem::call_action_rc(action, game, ui);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Event::Mouse(m) => match m.kind {
|
|
|
|
|
MouseEventKind::ScrollUp => ui.scroll_menu(-1),
|
|
|
|
|
MouseEventKind::ScrollDown => ui.scroll_menu(1),
|
|
|
|
|
_ => {}
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|