diff options
Diffstat (limited to 'crates/fparkan-assets/src')
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster.rs | 10 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster/orders.rs | 6 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategy_cursor.rs | 105 |
4 files changed, 122 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 253920fa..1f2cb172 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -48,6 +48,7 @@ pub mod object_descriptions; pub mod satellite_map; pub mod strategic_balance; pub mod strategic_roster; +pub mod strategy_cursor; pub mod strategy_toolbar; mod turret_elevation; mod ui_configuration; diff --git a/crates/fparkan-assets/src/strategic_roster.rs b/crates/fparkan-assets/src/strategic_roster.rs index 440ead26..64c819a1 100644 --- a/crates/fparkan-assets/src/strategic_roster.rs +++ b/crates/fparkan-assets/src/strategic_roster.rs @@ -46,6 +46,16 @@ pub fn hit_test([x, y]: [f32; 2], row_count: usize) -> Option<ListHit> { (row < row_count && y % ROW_PITCH < ROW_HEIGHT).then_some(ListHit::Row(row)) } +/// Whether the roster or its supported order panel covers the pointer. +pub fn covers_pointer([x, y]: [f32; 2], rows: usize) -> bool { + let row_end = ROW_TOP + ROW_PITCH * rows.min(19) as u32; + let [x, y] = [x.round_ties_even(), y.round_ties_even()]; + rows != 0 + && ((ORIGIN[0] as f32..(ORIGIN[0] + EXTENT[0] - 1) as f32).contains(&x) + && (0.0..row_end as f32).contains(&y) + || orders::covers_pointer([x, y], rows)) +} + /// A localized row with the native icon selectors. pub struct Row<'a> { pub label: &'a str, diff --git a/crates/fparkan-assets/src/strategic_roster/orders.rs b/crates/fparkan-assets/src/strategic_roster/orders.rs index 743b4801..4ea7d8d8 100644 --- a/crates/fparkan-assets/src/strategic_roster/orders.rs +++ b/crates/fparkan-assets/src/strategic_roster/orders.rs @@ -33,6 +33,12 @@ fn panel_top(count: usize) -> u32 { ROW_TOP + count.min(19) as u32 * ROW_PITCH } +pub(super) fn covers_pointer([x, y]: [f32; 2], count: usize) -> bool { + let y_top = panel_top(count); + let y_bottom = (y_top + (ORDERS.len() as u32 + 1) * ROW_PITCH).min(480); + (LEFT as f32..RIGHT as f32).contains(&x) && (y_top as f32..y_bottom as f32).contains(&y) +} + pub(super) struct OrderImages { stub: RgbaImage, disabled: RgbaImage, 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))); + } + } + } +} |
