//! Classic strategic toolbar assembled from the original HQ sprites. use crate::{mission_pager::copy_sprite, ui_sprite::load_ui_sprites, RgbaImage}; use fparkan_vfs::Vfs; /// Bounds from iron3d+0x82930, in the original 640×480 screen space. pub const EXTENT: [u32; 2] = [46, 480]; // Each row uses the ordinary enabled button and its own glyph. // The constructor at iron3d+0x82930 also determines the sections' order. const BUTTON_TOPS: [u32; 12] = [12, 65, 94, 123, 176, 229, 258, 287, 316, 369, 399, 428]; const SEPARATORS: [u32; 4] = [41, 152, 205, 345]; const LOCK_TOP: u32 = 457; const GLYPH_OFFSET: [u32; 2] = [16, 3]; const LOCK_GLYPH_OFFSET: [u32; 2] = [18, 7]; // iron3d+0x9bf49 sets the ordinary enabled glyph to ARGB ff80ff80. const ENABLED_GLYPH: [u8; 3] = [128, 255, 128]; /// Actions available immediately upon entering strategic mode. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ToolbarAction { /// Return to the hero in the suit. Hero, /// Toggle the clan's warbot list. Warbots, /// Toggle the satellite map. Map, } /// Tests the native button bounds in the original 640×480 screen space. #[must_use] pub fn command_button_at([x, y]: [f32; 2]) -> Option { if !(0.0..EXTENT[0] as f32).contains(&x.round_ties_even()) { return None; } let y = y.round_ties_even(); [ (0, ToolbarAction::Hero), (1, ToolbarAction::Warbots), (10, ToolbarAction::Map), ] .into_iter() .find_map(|(index, action)| { let top = BUTTON_TOPS[index] as f32; (top..top + 29.0).contains(&y).then_some(action) }) } /// Composes the fully expanded toolbar in its resting state. pub fn load_expanded_toolbar(vfs: &dyn Vfs) -> Result { let [head, separator, button, lock, lock_icon, glyphs @ ..] = load_ui_sprites( vfs, "ui/hq.cfg", [ "objpanel_head", "objpanel_separator", "objpanel_button2", "objpanel_lock2", "objpanel_lock_icon", "objpanel_icon_hero", "objpanel_icon_warbots", "objpanel_icon_builder", "objpanel_icon_cargo", "objpanel_icon_restree", "objpanel_icon_factory", "objpanel_icon_tower", "objpanel_icon_bunker", "objpanel_icon_build", "objpanel_icon_chat", "objpanel_icon_map", "objpanel_icon_system", ], )?; let [width, height] = EXTENT; let mut toolbar = RgbaImage { width, height, rgba8: vec![0; (width * height * 4) as usize], }; stamp(&mut toolbar, &head, [0, 0], [255; 3]); for top in SEPARATORS { stamp(&mut toolbar, &separator, [0, top], [255; 3]); } for (top, glyph) in BUTTON_TOPS.into_iter().zip(glyphs) { stamp(&mut toolbar, &button, [0, top], [255; 3]); stamp( &mut toolbar, &glyph, [GLYPH_OFFSET[0], top + GLYPH_OFFSET[1]], ENABLED_GLYPH, ); } stamp(&mut toolbar, &lock, [0, LOCK_TOP], [255; 3]); stamp( &mut toolbar, &lock_icon, [LOCK_GLYPH_OFFSET[0], LOCK_TOP + LOCK_GLYPH_OFFSET[1]], ENABLED_GLYPH, ); Ok(toolbar) } fn stamp(toolbar: &mut RgbaImage, sprite: &RgbaImage, [x, y]: [u32; 2], tint: [u8; 3]) { copy_sprite( toolbar, sprite, [x, y, x + sprite.width, y + sprite.height], [false; 2], tint, ); } #[cfg(test)] mod tests { use super::*; use fparkan_vfs::DirectoryVfs; #[test] fn command_buttons_use_their_original_row_bounds() { assert_eq!(command_button_at([16.0, 12.0]), Some(ToolbarAction::Hero)); assert_eq!( command_button_at([16.0, 65.0]), Some(ToolbarAction::Warbots) ); assert_eq!(command_button_at([45.0, 427.0]), Some(ToolbarAction::Map)); assert_eq!(command_button_at([46.0, 400.0]), None); assert_eq!(command_button_at([25.0, 428.0]), None); } #[test] #[ignore = "requires licensed corpus"] fn expanded_toolbar_loads_the_native_buttons_in_order() { let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap(); let vfs = DirectoryVfs::new(root); let toolbar = load_expanded_toolbar(&vfs).unwrap(); let [width, height] = EXTENT; assert_eq!([toolbar.width, toolbar.height], EXTENT); let [warbots, map] = load_ui_sprites( &vfs, "ui/hq.cfg", ["objpanel_icon_warbots", "objpanel_icon_map"], ) .unwrap(); for (icon, top) in [(warbots, 65), (map, 399)] { let pixel = icon .rgba8 .chunks_exact(4) .position(|p| p == [255; 4]) .unwrap(); let [x, y] = GLYPH_OFFSET; let index = (top + y) as usize * width as usize + x as usize + pixel / icon.width as usize * width as usize + pixel % icon.width as usize; assert_eq!( &toolbar.rgba8[index * 4..index * 4 + 4], &[128, 255, 128, 255] ); } assert_eq!(toolbar.rgba8.len(), (width * height * 4) as usize); } }