Files
kiln/kiln-tui/src/ui.rs
T

128 lines
5.7 KiB
Rust
Raw Normal View History

2026-06-13 18:43:29 -05:00
use std::cell::RefCell;
use std::rc::Rc;
2026-06-11 21:45:50 -05:00
use std::time::Duration;
2026-06-13 18:43:29 -05:00
use kiln_core::Board;
2026-06-11 21:45:50 -05:00
use kiln_core::game::GameState;
2026-06-15 09:15:30 -05:00
use crate::animation::Animation;
2026-06-11 21:55:53 -05:00
use crate::log::LogState;
2026-06-15 10:42:21 -05:00
use crate::menu::{MenuItem, MenuCloseAnimation, MenuOpenAnimation, MenuState};
2026-06-15 09:15:30 -05:00
use crate::scroll_overlay::{ScrollCloseAnimation, ScrollOpenAnimation, ScrollOverlayState};
2026-06-13 18:43:29 -05:00
use crate::transition::TransitionAnimation;
2026-06-11 21:45:50 -05:00
2026-06-13 18:43:29 -05:00
/// A pair of board `Rc`s (old board, new board) queued for transition pre-rendering.
type PendingTransition = Option<(Rc<RefCell<Board>>, Rc<RefCell<Board>>)>;
2026-06-15 10:42:21 -05:00
/// View-only UI state for the log panel, scroll overlay, menu, and board animations.
2026-06-13 18:43:29 -05:00
/// Presentation-only state; lives in the front-end, not on [`GameState`].
2026-06-11 21:45:50 -05:00
pub(crate) struct Ui {
2026-06-11 21:55:53 -05:00
/// State for the log panel (open/closed, height, scroll position).
pub(crate) log: LogState,
2026-06-15 09:15:30 -05:00
/// Scroll offset and bounds for the fully-open scroll overlay.
2026-06-11 21:45:50 -05:00
pub(crate) overlay: ScrollOverlayState,
2026-06-13 18:43:29 -05:00
/// Old and new board Rcs waiting to be pre-rendered on the next `draw()` call.
/// Populated when a portal transition is detected; consumed in `draw()`.
pub(crate) pending_transition: PendingTransition,
2026-06-15 09:15:30 -05:00
/// The currently running animation, if any. All input is suppressed while `Some`.
pub(crate) active_animation: Option<Box<dyn Animation>>,
2026-06-15 10:42:21 -05:00
/// State for the active menu overlay. `Some` while the menu is open or closing.
pub(crate) menu: Option<MenuState>,
/// Set by a menu action to signal that the game loop should exit after the
/// close animation finishes.
pub(crate) should_quit: bool,
2026-06-11 21:45:50 -05:00
}
impl Default for Ui {
fn default() -> Self {
Self {
2026-06-11 21:55:53 -05:00
log: LogState::new(),
2026-06-11 21:45:50 -05:00
overlay: ScrollOverlayState::default(),
2026-06-13 18:43:29 -05:00
pending_transition: None,
2026-06-15 09:15:30 -05:00
active_animation: None,
2026-06-15 10:42:21 -05:00
menu: None,
should_quit: false,
2026-06-11 21:45:50 -05:00
}
}
}
impl Ui {
2026-06-15 10:42:21 -05:00
/// Scrolls the scroll overlay by `delta` lines, clamped to the valid range.
2026-06-11 21:45:50 -05:00
pub(crate) fn scroll_lines(&mut self, delta: i32) {
self.overlay.offset = (self.overlay.offset as i32 + delta)
.clamp(0, self.overlay.max_scroll as i32) as u16;
}
2026-06-15 10:42:21 -05:00
/// Scrolls the menu overlay by `delta` lines, clamped to the valid range.
pub(crate) fn scroll_menu(&mut self, delta: i32) {
if let Some(m) = self.menu.as_mut() {
m.offset = (m.offset as i32 + delta).clamp(0, m.max_scroll as i32) as u16;
}
}
/// Opens a menu: stores the items as [`MenuState`] and starts the open animation.
pub(crate) fn open_menu(&mut self, items: Vec<MenuItem>) {
self.active_animation = Some(Box::new(MenuOpenAnimation::new(items.clone())));
self.menu = Some(MenuState { items, ..Default::default() });
}
/// Starts the close animation using a snapshot of the current menu items.
///
/// Called from within a menu item's action closure (e.g. `ui.begin_close_menu()`).
pub(crate) fn begin_close_menu(&mut self) {
let items = self.menu.as_ref().map(|m| m.items.clone()).unwrap_or_default();
self.active_animation = Some(Box::new(MenuCloseAnimation::new(items)));
}
2026-06-11 21:45:50 -05:00
/// Advances all time-driven UI state by `dt` and drives the game tick.
///
2026-06-15 09:15:30 -05:00
/// While any animation is running, `game.tick` is suppressed. When an
/// animation finishes, [`input_mode_after`](Animation::input_mode_after) is
/// consulted: returning to [`Board`](crate::input::InputMode::Board) mode
2026-06-15 10:42:21 -05:00
/// eagerly calls [`handle_scroll`](GameState::handle_scroll) to clear any
/// pending scroll, and clears [`menu`](Ui::menu) so the closed overlay is not
/// rendered on the next frame.
2026-06-11 21:45:50 -05:00
pub(crate) fn tick(&mut self, dt: Duration, game: &mut GameState) {
2026-06-15 09:15:30 -05:00
// Advance the active animation; on completion, dispatch any mode-change effect.
if let Some(ref mut anim) = self.active_animation {
if anim.update(dt.as_secs_f32()) {
let mode = anim.input_mode_after();
self.active_animation = None;
2026-06-15 10:42:21 -05:00
// Returning to Board mode: eagerly clear scroll and menu state so
// neither renders as open for one extra frame before the game tick.
2026-06-15 09:15:30 -05:00
if matches!(mode, crate::input::InputMode::Board) {
game.handle_scroll();
2026-06-15 10:42:21 -05:00
self.menu = None;
2026-06-15 09:15:30 -05:00
}
2026-06-13 18:43:29 -05:00
}
2026-06-15 10:42:21 -05:00
return; // no game tick during any animation (even when just ended)
2026-06-13 18:43:29 -05:00
}
2026-06-15 09:15:30 -05:00
// Normal path: tick the game, then check if a scroll appeared.
game.tick(dt);
if let Some(scroll) = &game.active_scroll {
let lines = scroll.lines.clone();
self.active_animation = Some(Box::new(ScrollOpenAnimation::new(lines)));
2026-06-11 21:45:50 -05:00
}
2026-06-15 09:15:30 -05:00
}
2026-06-11 21:45:50 -05:00
2026-06-15 09:15:30 -05:00
/// Sets the player's scroll choice and starts the close animation.
///
2026-06-15 10:42:21 -05:00
/// `choice` is `None` when the player dismisses without selecting.
2026-06-15 09:15:30 -05:00
pub(crate) fn begin_close(&mut self, choice: Option<String>, game: &mut GameState) {
if let Some(scroll) = game.active_scroll.as_mut() {
scroll.choice = choice;
2026-06-11 21:45:50 -05:00
}
2026-06-15 09:15:30 -05:00
let lines = game.active_scroll.as_ref()
.map(|s| s.lines.clone())
.unwrap_or_default();
self.active_animation = Some(Box::new(ScrollCloseAnimation::new(lines)));
2026-06-11 21:45:50 -05:00
}
2026-06-15 09:15:30 -05:00
/// Initialises the board-wipe transition animation once the draw area is known.
///
/// Called from `draw_board_area` when [`pending_transition`](Ui::pending_transition)
/// is set, so the [`TransitionAnimation`] is sized to the exact inner board area.
pub(crate) fn start_transition(&mut self, old: &Board, new: &Board, area: ratatui::layout::Rect) {
self.active_animation = Some(Box::new(TransitionAnimation::new(old, new, area)));
2026-06-11 21:45:50 -05:00
}
2026-06-11 21:55:53 -05:00
}