diff options
Diffstat (limited to 'crates/fparkan-assets/src/strategy_cursor.rs')
| -rw-r--r-- | crates/fparkan-assets/src/strategy_cursor.rs | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/strategy_cursor.rs b/crates/fparkan-assets/src/strategy_cursor.rs new file mode 100644 index 00000000..392fa894 --- /dev/null +++ b/crates/fparkan-assets/src/strategy_cursor.rs @@ -0,0 +1,105 @@ +//! The original strategic mouse cursor strips in `ui/cursor.cfg`. + +use crate::{ui_configuration, ui_sprite, RgbaImage}; +use fparkan_vfs::Vfs; + +/// Number of adjacent frames loaded by iron3d+0x57d00. +pub const PHASES: u32 = 4; + +/// The cursor modes used for strategic routes. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum StrategyCursor { + /// Ordinary UI pointer. + Arrow, + /// A valid order waypoint. + Place, + /// An invalid order waypoint. + WrongPlace, +} + +impl StrategyCursor { + /// Modes in draw-range order. + pub const ALL: [Self; 3] = [Self::Arrow, Self::Place, Self::WrongPlace]; + + fn key(self) -> &'static str { + match self { + Self::Arrow => "ARROW", + Self::Place => "PLACE", + Self::WrongPlace => "WRONG_PLACE", + } + } +} + +/// Four adjacent frames and their anchoring and phase delay. +pub struct CursorStrip { + pub image: RgbaImage, + pub hotspot: [u32; 2], + pub phase_delay_ms: u32, +} + +/// Loads the route cursor strips from the installed UI archive. +pub fn load(vfs: &dyn Vfs) -> Result<[CursorStrip; 3], String> { + let config = ui_configuration::read(vfs, "ui/cursor.cfg")?; + let config = String::from_utf8_lossy(&config); + let region = |kind: StrategyCursor| { + ui_configuration::object(&config, kind.key()) + .ok_or_else(|| format!("missing cursor {}", kind.key())) + }; + let mut strips = Vec::with_capacity(StrategyCursor::ALL.len()); + let mut page = None; + for kind in StrategyCursor::ALL { + let fields = region(kind)?; + let texture = ui_configuration::field(&fields, "TEXTURE")?; + if page.as_ref().is_none_or(|(current, _)| *current != texture) { + page = Some((texture, ui_sprite::load_ui_page(vfs, texture)?)); + } + let Some((_, atlas)) = &page else { + unreachable!("the cursor atlas was loaded above"); + }; + let number = |key| ui_configuration::number(&fields, key); + let [x, y] = [number("OFFSET_X")?, number("OFFSET_Y")?]; + let [w, h] = [number("WIDTH")?, number("HEIGHT")?]; + let strip_width = w.checked_mul(PHASES).ok_or("cursor strip width overflow")?; + let image = ui_sprite::atlas_region(atlas, [x, y, strip_width, h]) + .ok_or_else(|| format!("cursor {} outside atlas", kind.key()))?; + strips.push(CursorStrip { + image, + hotspot: [number("EXTENT_X")?, number("EXTENT_Y")?], + phase_delay_ms: number("PHASE_DELAY")?, + }); + } + Ok(strips + .try_into() + .unwrap_or_else(|_| unreachable!("one strip per mode"))) +} + +#[cfg(test)] +mod tests { + use super::*; + use fparkan_vfs::DirectoryVfs; + + #[test] + #[ignore = "requires licensed corpus"] + fn original_strategy_cursor_frames_and_hotspots() { + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let strips = load(&DirectoryVfs::new(root)).unwrap(); + assert_eq!( + strips.each_ref().map(|strip| strip.hotspot), + [[0, 0], [8, 8], [8, 8]] + ); + for strip in strips { + assert_eq!([strip.image.width, strip.image.height], [64, 16]); + assert_eq!(strip.phase_delay_ms, 150); + for phase in 0..PHASES { + let offset = (phase * 16 * 4) as usize; + assert!(strip + .image + .rgba8 + .chunks_exact(64 * 4) + .any(|row| row[offset..offset + 16 * 4] + .chunks_exact(4) + .any(|pixel| pixel[3] != 0))); + } + } + } +} |
