final refactors
This commit is contained in:
+12
-1
@@ -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<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
|
||||
}
|
||||
|
||||
/// Handles an input event in [`InputMode::Scroll`].
|
||||
pub(crate) fn handle_scroll_input(event: Event, game: &mut GameState, ui: &mut Ui) {
|
||||
match event {
|
||||
|
||||
+7
-2
@@ -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.
|
||||
|
||||
+2
-18
@@ -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<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
|
||||
}
|
||||
|
||||
/// 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.
|
||||
|
||||
@@ -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); }
|
||||
}
|
||||
_ => {}
|
||||
|
||||
Reference in New Issue
Block a user