From d26da0a21ce2e0e08257a5c545d935acd0ef3bd7 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 05:15:22 +0400 Subject: fix(ui): animate native F1 controls --- crates/fparkan-assets/src/help_screen.rs | 237 ++++++++++++++++++++++++++----- 1 file changed, 205 insertions(+), 32 deletions(-) (limited to 'crates/fparkan-assets/src') diff --git a/crates/fparkan-assets/src/help_screen.rs b/crates/fparkan-assets/src/help_screen.rs index 7b97f6f0..068b43c1 100644 --- a/crates/fparkan-assets/src/help_screen.rs +++ b/crates/fparkan-assets/src/help_screen.rs @@ -9,6 +9,7 @@ use crate::{ }; use encoding_rs::WINDOWS_1251; use fparkan_vfs::Vfs; +use std::time::{Duration, Instant}; /// Drawable extent used to compose the original font profile. pub const EXTENT: [u32; 2] = [800, 600]; @@ -17,8 +18,11 @@ const TAB_X: [u32; 3] = [2, 199, 396]; const KEY_COLUMN: u32 = PAGE[0] + 250; const CLOSE: [u32; 4] = [593, 2, 640, 23]; const SCROLL: [[u32; 4]; 2] = [[526, 458, 576, 477], [576, 458, 626, 477]]; +const HOVER_STEP_MS: u64 = 251; +const SCROLL_STEP: Duration = Duration::from_millis(51); const WHITE: [u8; 3] = [255; 3]; const GREEN: [u8; 3] = [0, 255, 0]; +const PALE_GREEN: [u8; 3] = [100, 255, 100]; const YELLOW: [u8; 3] = [255, 255, 0]; /// A hit in the native 640×480 coordinate space. @@ -52,14 +56,25 @@ pub struct HelpLabels { pub struct HelpScreen { font: BitmapFont, frame: UiFrame, - tab: [RgbaImage; 3], - buttons: [RgbaImage; 3], + tab_body: RgbaImage, + emitter: [RgbaImage; 2], + close: [RgbaImage; 2], + scroll_button: [RgbaImage; 3], icons: [RgbaImage; 3], labels: HelpLabels, pages: [Vec; 3], tab_index: usize, scroll: [usize; 3], message_count: usize, + pointer: HelpPointer, +} + +struct HelpPointer { + hover: Option, + blink_start: Instant, + bright: bool, + blink_at: Option, + scroll_at: Option, } #[derive(Default)] @@ -74,23 +89,28 @@ impl HelpScreen { /// Prepares original mission and keyboard help pages and their shared frame. pub fn load(vfs: &dyn Vfs, mission: &str, labels: HelpLabels) -> Result { let data = HelpResources::load(vfs, mission, false)?; - let mut screen = Self { - font: load_game_font(vfs)?, - frame: UiFrame::load(vfs)?, - tab: load_ui_sprites( - vfs, - "ui/compaund.cfg", - ["ccres_ending_text", "ccres_body_text", "ccres_ending_stub"], - )?, - buttons: load_ui_sprites( + let [tab_body, ray_normal, ray_pressed, close_normal, close_pressed, off, normal, pressed] = + load_ui_sprites( vfs, "ui/compaund.cfg", [ + "ccres_body_text", + "ccres_ray_emitter_normal", + "ccres_ray_emitter_pressed", "ccres_exit_button_normal", + "ccres_exit_button_pressed", "ccres_long_button_off", "ccres_long_button_normal", + "ccres_long_button_pressed", ], - )?, + )?; + let mut screen = Self { + font: load_game_font(vfs)?, + frame: UiFrame::load(vfs)?, + tab_body, + emitter: [ray_normal, ray_pressed], + close: [close_normal, close_pressed], + scroll_button: [off, normal, pressed], icons: load_ui_sprites( vfs, "ui/hq.cfg", @@ -101,6 +121,13 @@ impl HelpScreen { tab_index: 0, scroll: [0; 3], message_count: 0, + pointer: HelpPointer { + hover: None, + blink_start: Instant::now(), + bright: false, + blink_at: None, + scroll_at: None, + }, }; screen.pages = [ screen.mission_rows(data.mission)?, @@ -170,7 +197,7 @@ impl HelpScreen { let hit = |[l, t, r, b]: [u32; 4]| x >= l as f32 && x < r as f32 && y >= t as f32 && y < b as f32; for (index, l) in TAB_X.into_iter().enumerate() { - if hit([l, 2, l + 197, 23]) { + if hit([l, 2, l + 184, 23]) { return Some(HelpAction::Tab(index)); } } @@ -183,6 +210,64 @@ impl HelpScreen { .map(|index| HelpAction::Scroll(if index == 0 { 1 } else { -1 })) } + /// Changes the hovered control and releases scrolling when leaving the arrows. + pub fn point(&mut self, position: [f32; 2]) -> bool { + let hover = Self::hit_test(position); + if self.pointer.hover == hover { + return false; + } + self.pointer.hover = hover; + if !matches!(hover, Some(HelpAction::Scroll(_))) { + self.pointer.scroll_at = None; + } + true + } + + /// Selects a tab or latches the hovered scroll arrow for the next update. + pub fn press_pointer(&mut self, now: Instant) -> Option { + let action = self.pointer.hover?; + if matches!(action, HelpAction::Scroll(_)) { + self.pointer.scroll_at = Some(now); + None + } else { + Some(action) + } + } + + /// Releases a held arrow and reports whether its appearance might change. + pub fn release_pointer(&mut self) -> bool { + self.pointer.scroll_at.take().is_some() + } + + /// Updates the hovered control using real time while mission time is paused. + pub fn update_pointer(&mut self, now: Instant) -> bool { + let elapsed = now.duration_since(self.pointer.blink_start).as_millis(); + let step = u128::from(HOVER_STEP_MS); + let bright = elapsed / step % 2 != 0; + let mut redraw = self.pointer.bright != bright + && self.pointer.scroll_at.is_none() + && self.next_pointer_update().is_some(); + self.pointer.bright = bright; + self.pointer.blink_at = + Some(now + Duration::from_millis(HOVER_STEP_MS - (elapsed % step) as u64)); + if self.pointer.scroll_at.is_some_and(|at| now >= at) { + if let Some(action @ HelpAction::Scroll(_)) = self.pointer.hover { + redraw |= self.navigate(action); + } + self.pointer.scroll_at = Some(now + SCROLL_STEP); + } + redraw + } + + /// Next visible pointer update. Idle help pages need no timer. + pub fn next_pointer_update(&self) -> Option { + match self.pointer.hover? { + HelpAction::Tab(index) if index == self.tab_index => None, + HelpAction::Scroll(lines) if !self.scroll_enabled(lines) => None, + _ => self.pointer.scroll_at.or(self.pointer.blink_at), + } + } + /// Paints the selected page from the original UI parts and game font. pub fn paint(&self) -> Result { let [width, height] = EXTENT; @@ -197,18 +282,31 @@ impl HelpScreen { self.frame .paint(&mut image, [2, 24, 637, 454], [0, 64, 0, 255]); for (index, left) in TAB_X.into_iter().enumerate() { - let tint = if index == self.tab_index { - WHITE - } else { - GREEN + let hovered = self.pointer.hover == Some(HelpAction::Tab(index)); + let lit = index == self.tab_index || (hovered && self.pointer.bright); + let tint = match (lit, hovered) { + (true, _) => WHITE, + (false, true) => PALE_GREEN, + _ => GREEN, }; - let [leading, body, ending] = &self.tab; - sprite(&mut image, leading, [left, 2, left + 5, 21], WHITE); - sprite(&mut image, body, [left + 5, 2, left + 184, 21], WHITE); - sprite(&mut image, ending, [left + 184, 2, left + 189, 21], WHITE); + let emitter = &self.emitter[usize::from(lit)]; + sprite(&mut image, emitter, [left, 2, left + 10, 21], WHITE); + sprite( + &mut image, + &self.tab_body, + [left + 10, 2, left + 189, 21], + WHITE, + ); + copy_sprite( + &mut image, + emitter, + [left + 183, 2, left + 194, 21].map(scale), + [true, false], + WHITE, + ); let text = encode(&self.labels.tabs[index])?; let width = u32::from(self.font.line_metrics_bytes(&text)?[0]); - let center = scale(left + 94); + let center = scale(left + 100); self.text( &mut image, &text, @@ -216,23 +314,36 @@ impl HelpScreen { tint, )?; } - sprite(&mut image, &self.buttons[0], [593, 2, 640, 21], WHITE); - sprite(&mut image, &self.icons[0], [610, 5, 623, 18], WHITE); + let close_lit = self.pointer.hover == Some(HelpAction::Close) && self.pointer.bright; + sprite( + &mut image, + &self.emitter[usize::from(close_lit)], + [593, 2, 603, 21], + WHITE, + ); + sprite( + &mut image, + &self.close[usize::from(close_lit)], + [603, 2, 638, 21], + WHITE, + ); + sprite( + &mut image, + &self.icons[0], + [618, 5, 631, 18], + [if close_lit { 255 } else { 240 }; 3], + ); let start = self.scroll[self.tab_index]; - let enabled = [start < self.last_scroll(), start > 0]; for (index, bounds) in SCROLL.into_iter().enumerate() { let [l, t, _, _] = bounds; - sprite( - &mut image, - &self.buttons[1 + usize::from(enabled[index])], - bounds, - WHITE, - ); + let lines = if index == 0 { 1 } else { -1 }; + let state = self.scroll_state(lines); + sprite(&mut image, &self.scroll_button[state], bounds, WHITE); sprite( &mut image, &self.icons[index + 1], [l + 10, t + 2, l + 40, t + 17], - WHITE, + [[128; 3], [240; 3], WHITE][state], ); } let mut y = PAGE[1]; @@ -259,6 +370,24 @@ impl HelpScreen { Ok(image) } + fn scroll_enabled(&self, lines: i32) -> bool { + let start = self.scroll[self.tab_index]; + if lines > 0 { + start < self.last_scroll() + } else { + start > 0 + } + } + + fn scroll_state(&self, lines: i32) -> usize { + if !self.scroll_enabled(lines) { + return 0; + } + let pressed = self.pointer.hover == Some(HelpAction::Scroll(lines)) + && (self.pointer.bright || self.pointer.scroll_at.is_some()); + 1 + usize::from(pressed) + } + fn last_scroll(&self) -> usize { let rows = &self.pages[self.tab_index]; let visible = PAGE[3] - PAGE[1]; @@ -458,4 +587,48 @@ mod tests { ); assert_eq!(HelpScreen::hit_test([300.0, 400.0]), None); } + + #[test] + #[ignore = "requires licensed corpus"] + fn help_pointer_keeps_the_blink_phase_and_repeats_a_held_arrow() { + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let mut help = HelpScreen::load( + &fparkan_vfs::DirectoryVfs::new(root), + "MISSIONS/CAMPAIGN/CAMPAIGN.00/Mission.01/data.tma", + HelpLabels { + tabs: Default::default(), + controls: Default::default(), + common_keys: String::new(), + game_menu_hint: String::new(), + empty_journal: String::new(), + message_number: String::new(), + }, + ) + .unwrap(); + let start = help.pointer.blink_start; + let at = |ms| start + Duration::from_millis(ms); + assert!(help.navigate(HelpAction::Tab(1))); + assert_eq!([help.scroll_state(1), help.scroll_state(-1)], [1, 0]); + assert!(help.point([60.0, 10.0])); + assert!(!help.update_pointer(at(250))); + assert!(help.update_pointer(at(251))); + assert!(help.pointer.bright); + assert!(help.point([620.0, 10.0])); + assert!(help.pointer.bright); + + assert!(help.point([540.0, 469.0])); + help.update_pointer(at(502)); + assert_eq!(help.scroll_state(1), 1); + assert_eq!(help.press_pointer(at(502)), None); + assert_eq!(help.scroll_state(1), 2); + assert!(help.update_pointer(at(502))); + assert_eq!(help.scroll[1], 1); + assert!(!help.update_pointer(at(552))); + assert!(help.update_pointer(at(553))); + assert_eq!(help.scroll[1], 2); + assert!(help.point([300.0, 400.0])); + assert_eq!(help.next_pointer_update(), None); + assert!(!help.update_pointer(at(604))); + assert_eq!(help.scroll[1], 2); + } } -- cgit v1.2.3