From 7f41f704ef3c7ea29488881b12d65d621f6c5d32 Mon Sep 17 00:00:00 2001 From: Ross Andrews Date: Thu, 11 Jun 2026 22:17:46 -0500 Subject: [PATCH] final refactors --- kiln-tui/src/input.rs | 13 ++++++++++++- kiln-tui/src/log.rs | 9 +++++++-- kiln-tui/src/main.rs | 20 ++------------------ kiln-tui/src/scroll_overlay.rs | 7 +++++-- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/kiln-tui/src/input.rs b/kiln-tui/src/input.rs index 8ce9851..0ec4390 100644 --- a/kiln-tui/src/input.rs +++ b/kiln-tui/src/input.rs @@ -1,7 +1,6 @@ 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; @@ -50,6 +49,18 @@ pub(crate) fn handle_board_input(event: Event, log_open: bool, terminal_h: u16, false } +/// Returns the choice string for the given key character, scanning the scroll lines. +fn resolve_choice(lines: &[ScrollLine], c: char) -> Option { + 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 +} + /// Handles an input event in [`InputMode::Scroll`]. pub(crate) fn handle_scroll_input(event: Event, game: &mut GameState, ui: &mut Ui) { match event { diff --git a/kiln-tui/src/log.rs b/kiln-tui/src/log.rs index deca78d..2f02dc8 100644 --- a/kiln-tui/src/log.rs +++ b/kiln-tui/src/log.rs @@ -13,7 +13,6 @@ use ratatui::widgets::{Block, Paragraph, StatefulWidget, Widget, Wrap}; use crate::utils::rgba8_to_color; /// View state for the log panel. -#[derive(Default)] pub struct LogState { /// Whether the log panel is currently open. pub open: bool, @@ -23,10 +22,16 @@ pub struct LogState { pub scroll: u16, } +impl Default for LogState { + fn default() -> Self { + Self { open: false, height: 10, scroll: 0 } + } +} + impl LogState { /// Creates a `LogState` with sensible defaults (closed, 10 rows tall). pub fn new() -> Self { - Self { open: false, height: 10, scroll: 0 } + Self::default() } /// Toggles the panel open or closed, resetting scroll to the top on open. diff --git a/kiln-tui/src/main.rs b/kiln-tui/src/main.rs index edc9990..1e337eb 100644 --- a/kiln-tui/src/main.rs +++ b/kiln-tui/src/main.rs @@ -17,7 +17,7 @@ mod speech; mod ui; mod input; -use kiln_core::game::{GameState, ScrollLine}; +use kiln_core::game::GameState; use kiln_core::log::LogLine; use kiln_core::map_file; use ratatui::Frame; @@ -39,21 +39,6 @@ use crate::scroll_overlay::ScrollOverlayWidget; use crate::speech::SpeechBubblesWidget; use crate::ui::Ui; -/// Returns the choice string for the given key character, or `None` if it doesn't -/// match any choice in the scroll. Choices are assigned letters a, b, c… in order. -fn resolve_choice(lines: &[ScrollLine], c: char) -> Option { - 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 -} - /// Entry point: parse the map path, load the board, then run the play loop with /// the terminal in raw/alternate-screen mode (restored on every exit path). fn main() -> ExitCode { @@ -173,8 +158,7 @@ fn run( /// A scroll overlay is drawn on top of everything when active. fn draw(frame: &mut Frame, game: &GameState, ui: &mut Ui) { // Borrow the board for the duration of the frame (no script runs during draw). - let board = game.board(); - let board = &*board; + let board = &game.board(); if ui.log.open { // Split the screen: board on top, log panel of `height` at the bottom. diff --git a/kiln-tui/src/scroll_overlay.rs b/kiln-tui/src/scroll_overlay.rs index b66b591..805db3d 100644 --- a/kiln-tui/src/scroll_overlay.rs +++ b/kiln-tui/src/scroll_overlay.rs @@ -15,6 +15,9 @@ use ratatui::widgets::{ Widget, Wrap, }; +/// Duration of the open and close animations, in seconds. +const ANIM_SECS: f32 = 0.25; + /// Animation state for the scroll overlay window. #[derive(Default)] pub enum ScrollAnimState { @@ -54,11 +57,11 @@ impl ScrollOverlayState { pub fn advance_anim(&mut self, dt: f32) { match &mut self.anim { Some(ScrollAnimState::Opening(p)) => { - *p += dt / 0.5; + *p += dt / ANIM_SECS; if *p >= 1.0 { self.anim = Some(ScrollAnimState::Open); } } Some(ScrollAnimState::Closing(p)) => { - *p -= dt / 0.5; + *p -= dt / ANIM_SECS; if *p <= 0.0 { self.anim = Some(ScrollAnimState::Done); } } _ => {}