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 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 09:15:30 -05:00
|
|
|
/// View-only UI state for the log panel, scroll overlay, 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-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-11 21:45:50 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Ui {
|
|
|
|
|
/// Scrolls the overlay by `delta` lines, clamped to the valid range.
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
/// immediately calls [`handle_scroll`](GameState::handle_scroll) so the
|
|
|
|
|
/// closed overlay is cleared before the next draw rather than flickering open
|
|
|
|
|
/// for one extra 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;
|
|
|
|
|
// Returning to Board mode: eagerly close any scroll so it isn't
|
|
|
|
|
// rendered as open on the next frame before the game tick runs.
|
|
|
|
|
if matches!(mode, crate::input::InputMode::Board) {
|
|
|
|
|
game.handle_scroll();
|
|
|
|
|
}
|
2026-06-13 18:43:29 -05:00
|
|
|
}
|
2026-06-15 09:15:30 -05:00
|
|
|
return; // no game tick during any animation (even when animation 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.
|
|
|
|
|
///
|
|
|
|
|
/// `choice` is `None` when the player dismisses without selecting; it is
|
|
|
|
|
/// stored on the active scroll and dispatched by the engine on the next tick.
|
|
|
|
|
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
|
|
|
}
|