diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategy_toolbar.rs | 120 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/autodemo.rs | 10 |
3 files changed, 130 insertions, 1 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 6900953c..5bdd7579 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -46,6 +46,7 @@ pub mod mission_loader; pub mod mission_pager; pub mod object_descriptions; pub mod satellite_map; +pub mod strategy_toolbar; mod turret_elevation; mod ui_configuration; pub mod ui_sprite; diff --git a/crates/fparkan-assets/src/strategy_toolbar.rs b/crates/fparkan-assets/src/strategy_toolbar.rs new file mode 100644 index 00000000..5e4d04a8 --- /dev/null +++ b/crates/fparkan-assets/src/strategy_toolbar.rs @@ -0,0 +1,120 @@ +//! 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]; + +/// Composes the fully expanded toolbar in its resting state. +pub fn load_expanded_toolbar(vfs: &dyn Vfs) -> Result<RgbaImage, String> { + 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] + #[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); + } +} diff --git a/crates/fparkan-runtime/src/autodemo.rs b/crates/fparkan-runtime/src/autodemo.rs index ab793fb2..d7b53438 100644 --- a/crates/fparkan-runtime/src/autodemo.rs +++ b/crates/fparkan-runtime/src/autodemo.rs @@ -431,9 +431,14 @@ fn read_setting(bytes: &[u8], key: &str) -> Option<u32> { } impl AutoDemoCamera { + /// The original draws the command toolbar throughout the strategic interval. + pub fn shows_strategy_toolbar(&self) -> bool { + self.phase == 0 && self.last_clock_ms.is_some() + } + /// Unit selected while the automatic camera shows the strategic map. pub fn strategy_map_target(&self, snapshot: &WorldSnapshot) -> Option<StartingCockpit> { - if self.phase != 0 || self.last_clock_ms.is_none() { + if !self.shows_strategy_toolbar() { return None; } self.targets @@ -607,10 +612,12 @@ mod tests { ], }; let terrain = |_| Ok(Some(0.0)); + assert!(!demo.shows_strategy_toolbar()); assert!(matches!( demo.sample_with_height(0, &snapshot, terrain).unwrap(), AutoDemoView::Exterior { bird: false, .. } )); + assert!(demo.shows_strategy_toolbar()); assert_eq!( demo.strategy_map_target(&snapshot) .map(|target| target.handle), @@ -624,6 +631,7 @@ mod tests { demo.sample_with_height(5_001, &snapshot, terrain).unwrap(), AutoDemoView::Exterior { bird: true, .. } )); + assert!(!demo.shows_strategy_toolbar()); assert!(demo.strategy_map_target(&snapshot).is_none()); assert!(matches!( demo.sample_with_height(15_001, &snapshot, terrain).unwrap(), |
