final refactors
This commit is contained in:
+12
-1
@@ -1,7 +1,6 @@
|
|||||||
use ratatui::crossterm::event::{Event, KeyCode, MouseEventKind};
|
use ratatui::crossterm::event::{Event, KeyCode, MouseEventKind};
|
||||||
use kiln_core::Direction;
|
use kiln_core::Direction;
|
||||||
use kiln_core::game::{GameState, ScrollLine};
|
use kiln_core::game::{GameState, ScrollLine};
|
||||||
use crate::resolve_choice;
|
|
||||||
use crate::scroll_overlay::ScrollAnimState;
|
use crate::scroll_overlay::ScrollAnimState;
|
||||||
use crate::ui::Ui;
|
use crate::ui::Ui;
|
||||||
|
|
||||||
@@ -50,6 +49,18 @@ pub(crate) fn handle_board_input(event: Event, log_open: bool, terminal_h: u16,
|
|||||||
false
|
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`].
|
/// Handles an input event in [`InputMode::Scroll`].
|
||||||
pub(crate) fn handle_scroll_input(event: Event, game: &mut GameState, ui: &mut Ui) {
|
pub(crate) fn handle_scroll_input(event: Event, game: &mut GameState, ui: &mut Ui) {
|
||||||
match event {
|
match event {
|
||||||
|
|||||||
+7
-2
@@ -13,7 +13,6 @@ use ratatui::widgets::{Block, Paragraph, StatefulWidget, Widget, Wrap};
|
|||||||
use crate::utils::rgba8_to_color;
|
use crate::utils::rgba8_to_color;
|
||||||
|
|
||||||
/// View state for the log panel.
|
/// View state for the log panel.
|
||||||
#[derive(Default)]
|
|
||||||
pub struct LogState {
|
pub struct LogState {
|
||||||
/// Whether the log panel is currently open.
|
/// Whether the log panel is currently open.
|
||||||
pub open: bool,
|
pub open: bool,
|
||||||
@@ -23,10 +22,16 @@ pub struct LogState {
|
|||||||
pub scroll: u16,
|
pub scroll: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for LogState {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self { open: false, height: 10, scroll: 0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl LogState {
|
impl LogState {
|
||||||
/// Creates a `LogState` with sensible defaults (closed, 10 rows tall).
|
/// Creates a `LogState` with sensible defaults (closed, 10 rows tall).
|
||||||
pub fn new() -> Self {
|
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.
|
/// Toggles the panel open or closed, resetting scroll to the top on open.
|
||||||
|
|||||||
+2
-18
@@ -17,7 +17,7 @@ mod speech;
|
|||||||
mod ui;
|
mod ui;
|
||||||
mod input;
|
mod input;
|
||||||
|
|
||||||
use kiln_core::game::{GameState, ScrollLine};
|
use kiln_core::game::GameState;
|
||||||
use kiln_core::log::LogLine;
|
use kiln_core::log::LogLine;
|
||||||
use kiln_core::map_file;
|
use kiln_core::map_file;
|
||||||
use ratatui::Frame;
|
use ratatui::Frame;
|
||||||
@@ -39,21 +39,6 @@ use crate::scroll_overlay::ScrollOverlayWidget;
|
|||||||
use crate::speech::SpeechBubblesWidget;
|
use crate::speech::SpeechBubblesWidget;
|
||||||
use crate::ui::Ui;
|
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
|
/// 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).
|
/// the terminal in raw/alternate-screen mode (restored on every exit path).
|
||||||
fn main() -> ExitCode {
|
fn main() -> ExitCode {
|
||||||
@@ -173,8 +158,7 @@ fn run(
|
|||||||
/// A scroll overlay is drawn on top of everything when active.
|
/// A scroll overlay is drawn on top of everything when active.
|
||||||
fn draw(frame: &mut Frame, game: &GameState, ui: &mut Ui) {
|
fn draw(frame: &mut Frame, game: &GameState, ui: &mut Ui) {
|
||||||
// Borrow the board for the duration of the frame (no script runs during draw).
|
// Borrow the board for the duration of the frame (no script runs during draw).
|
||||||
let board = game.board();
|
let board = &game.board();
|
||||||
let board = &*board;
|
|
||||||
|
|
||||||
if ui.log.open {
|
if ui.log.open {
|
||||||
// Split the screen: board on top, log panel of `height` at the bottom.
|
// Split the screen: board on top, log panel of `height` at the bottom.
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ use ratatui::widgets::{
|
|||||||
Widget, Wrap,
|
Widget, Wrap,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Duration of the open and close animations, in seconds.
|
||||||
|
const ANIM_SECS: f32 = 0.25;
|
||||||
|
|
||||||
/// Animation state for the scroll overlay window.
|
/// Animation state for the scroll overlay window.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub enum ScrollAnimState {
|
pub enum ScrollAnimState {
|
||||||
@@ -54,11 +57,11 @@ impl ScrollOverlayState {
|
|||||||
pub fn advance_anim(&mut self, dt: f32) {
|
pub fn advance_anim(&mut self, dt: f32) {
|
||||||
match &mut self.anim {
|
match &mut self.anim {
|
||||||
Some(ScrollAnimState::Opening(p)) => {
|
Some(ScrollAnimState::Opening(p)) => {
|
||||||
*p += dt / 0.5;
|
*p += dt / ANIM_SECS;
|
||||||
if *p >= 1.0 { self.anim = Some(ScrollAnimState::Open); }
|
if *p >= 1.0 { self.anim = Some(ScrollAnimState::Open); }
|
||||||
}
|
}
|
||||||
Some(ScrollAnimState::Closing(p)) => {
|
Some(ScrollAnimState::Closing(p)) => {
|
||||||
*p -= dt / 0.5;
|
*p -= dt / ANIM_SECS;
|
||||||
if *p <= 0.0 { self.anim = Some(ScrollAnimState::Done); }
|
if *p <= 0.0 { self.anim = Some(ScrollAnimState::Done); }
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|||||||
Reference in New Issue
Block a user