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/satellite_map_chrome.rs | 242 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster.rs | 10 |
3 files changed, 250 insertions, 3 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 1f2cb172..72401c66 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 satellite_map_chrome; pub mod strategic_balance; pub mod strategic_roster; pub mod strategy_cursor; diff --git a/crates/fparkan-assets/src/satellite_map_chrome.rs b/crates/fparkan-assets/src/satellite_map_chrome.rs new file mode 100644 index 00000000..30b41e06 --- /dev/null +++ b/crates/fparkan-assets/src/satellite_map_chrome.rs @@ -0,0 +1,242 @@ +//! Compound header and contact footer from the strategic satellite map. + +use crate::{ + game_font::{load_layout_font, BitmapFont}, + mission_pager::copy_sprite, + strategic_roster::durability_fill, + ui_sprite::{atlas_region, load_ui_page, load_ui_sprites}, + RgbaImage, +}; +use fparkan_vfs::Vfs; + +/// Header and footer bounds in the original 640×480 strategic layout. +pub const HEADER: [u32; 4] = [374, 43, 640, 62]; +pub const FOOTER: [u32; 4] = [374, 330, 640, 349]; +const CLOSE: [u32; 4] = [605, 43, 640, 63]; +const WIDTH: u32 = 266; +const HEIGHT: u32 = 19; +const TEXT_LEFT: u32 = 72; +const TEXT_WIDTH: u32 = 183; + +/// Tests the original close rectangle, including the last logical row. +pub fn close_at(pointer: [f32; 2]) -> bool { + contains(CLOSE, pointer) +} + +/// Whether an event belongs to the header or footer instead of the map or scene. +pub fn covers_pointer(pointer: [f32; 2]) -> bool { + contains([HEADER[0], HEADER[1], HEADER[2], CLOSE[3]], pointer) || contains(FOOTER, pointer) +} + +fn contains([left, top, right, bottom]: [u32; 4], pointer: [f32; 2]) -> bool { + let [x, y] = pointer.map(f32::round_ties_even); + (left as f32..right as f32).contains(&x) && (top as f32..bottom as f32).contains(&y) +} + +/// Live text and art selectors for a single contact. +pub struct Contact<'a> { + pub label: &'a str, + pub class: u32, + pub size: u32, + pub chassis: u32, + pub clan: u16, + pub emblem_tint: [u8; 3], + pub durability_percent: Option<i32>, +} + +/// Loaded material for the two strips. +pub struct MapChrome { + font: BitmapFont, + cap: RgbaImage, + body: RgbaImage, + close: RgbaImage, + exit_icon: RgbaImage, + emitter: RgbaImage, + ray: RgbaImage, + end: RgbaImage, + unit_icons: RgbaImage, + extra_icons: RgbaImage, + contact_icons: RgbaImage, +} + +impl MapChrome { + /// Reads the compound controls and marker atlas from the installed game. + pub fn load(vfs: &dyn Vfs, contact_icons: RgbaImage) -> Result<Self, String> { + let [cap, body, close, emitter, ray, end] = load_ui_sprites( + vfs, + "ui/compaund.cfg", + [ + "ccres_ending_text", + "ccres_body_text", + "ccres_exit_button_normal", + "ccres_ray_emitter_off", + "ccres_ray_body", + "ccres_ray_ending", + ], + )?; + let [exit_icon] = load_ui_sprites(vfs, "ui/hq.cfg", ["exit_icon"])?; + Ok(Self { + font: load_layout_font(vfs)?, + cap, + body, + close, + exit_icon, + emitter, + ray, + end, + unit_icons: load_ui_page(vfs, "ui_menu")?, + extra_icons: load_ui_page(vfs, "ui_menu3")?, + contact_icons, + }) + } + + /// Renders the map title and its close button. + pub fn header(&self, title: &str) -> Result<RgbaImage, String> { + let mut image = empty_strip(); + let text = self.font.centered_line(title, 226, HEIGHT as u16)?; + stamp(&mut image, &self.cap, [0, 0, 5, HEIGHT], [255; 3]); + stamp(&mut image, &self.body, [5, 0, 231, HEIGHT], [255; 3]); + stamp(&mut image, &text, [5, 0, 231, HEIGHT], [128, 255, 128]); + stamp(&mut image, &self.close, [231, 0, WIDTH, HEIGHT], [255; 3]); + stamp(&mut image, &self.exit_icon, [246, 3, 259, 16], [255; 3]); + Ok(image) + } + + /// Renders the stationary footer and its optional hovered contact. + pub fn footer(&self, contact: Option<Contact<'_>>) -> Result<RgbaImage, String> { + let mut image = empty_strip(); + stamp(&mut image, &self.cap, [0, 0, 5, HEIGHT], [255; 3]); + stamp(&mut image, &self.body, [5, 0, 62, HEIGHT], [255; 3]); + copy_sprite( + &mut image, + &self.cap, + [61, 0, 67, HEIGHT], + [true, false], + [255; 3], + ); + stamp( + &mut image, + &self.emitter, + [62, 0, TEXT_LEFT + 1, HEIGHT], + [255; 3], + ); + let right = TEXT_LEFT + TEXT_WIDTH; + stamp( + &mut image, + &self.ray, + [TEXT_LEFT, 0, right + 1, HEIGHT], + [255; 3], + ); + stamp( + &mut image, + &self.end, + [right, 0, right + 6, HEIGHT], + [255; 3], + ); + let Some(contact) = contact else { + return Ok(image); + }; + if let Some(emblem) = atlas_region( + &self.contact_icons, + [u32::from(contact.clan) * 32, 224, 32, 32], + ) { + stamp(&mut image, &emblem, [7, 2, 22, 17], contact.emblem_tint); + } + self.paint_glyphs(&mut image, &contact); + if let Some((width, rgba)) = contact + .durability_percent + .and_then(|percent| durability_fill(percent, TEXT_WIDTH)) + { + stamp( + &mut image, + &RgbaImage { + width: 1, + height: 1, + rgba8: rgba.to_vec(), + }, + [TEXT_LEFT + 2, 3, TEXT_LEFT + width, HEIGHT - 3], + [255; 3], + ); + } + let label = self + .font + .centered_line(contact.label, TEXT_WIDTH as u16, HEIGHT as u16)?; + stamp(&mut image, &label, [TEXT_LEFT, 0, right, HEIGHT], [255; 3]); + Ok(image) + } + + fn paint_glyphs(&self, image: &mut RgbaImage, contact: &Contact<'_>) { + let building = contact.class & 0x8000_0000 != 0; + let tint = match (building, contact.size) { + (false, 1) => [255, 231, 255], + (false, 2) => [255, 128, 128], + (false, 3) => [128, 255, 128], + (false, 4) => [128, 128, 255], + (true, 2) => [255, 0, 0], + (true, 3) => [0, 255, 0], + (true, 4 | 5) => [0, 0, 255], + _ => [255; 3], + }; + let atlas = if contact.class == 0x8000_0200 { + &self.extra_icons + } else { + &self.unit_icons + }; + let class = match contact.class { + 0x0100_2000 => Some([81, 110]), + 0x0100_4000 => Some([65, 110]), + 0x0100_8000 | 0x0101_0000 | 0x0102_0000 => Some([49, 110]), + 0x8000_0002 => Some([113, 126]), + 0x8000_0004 => Some([129, 126]), + 0x8000_0008 => Some([81, 110]), + 0x8000_0010 => Some([129, 94]), + 0x8000_0040 => Some([97, 126]), + 0x8000_0200 => Some([148, 27]), + 0x8000_0400 => Some([49, 126]), + 0x8001_0000 | 0x8002_0000 | 0x8004_0000 => Some([65, 126]), + 0x8010_0000 | 0x8020_0000 => Some([81, 126]), + _ => None, + }; + if let Some(glyph) = class.and_then(|[x, y]| atlas_region(atlas, [x, y, 15, 15])) { + stamp(image, &glyph, [26, 2, 41, 17], tint); + } + if !building { + let x = match contact.chassis { + 1 => 97, + 2 => 49, + 3 => 65, + 4 => 81, + _ => return, + }; + if let Some(glyph) = atlas_region(&self.unit_icons, [x, 94, 15, 15]) { + stamp(image, &glyph, [45, 2, 60, 17], tint); + } + } + } +} + +fn empty_strip() -> RgbaImage { + RgbaImage { + width: WIDTH, + height: HEIGHT, + rgba8: vec![0; (WIDTH * HEIGHT * 4) as usize], + } +} + +fn stamp(image: &mut RgbaImage, sprite: &RgbaImage, bounds: [u32; 4], tint: [u8; 3]) { + copy_sprite(image, sprite, bounds, [false; 2], tint); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn close_button_is_separate_from_the_image_and_the_title() { + assert!(close_at([605.0, 43.0])); + assert!(close_at([639.0, 62.0])); + assert!(!close_at([604.0, 43.0])); + assert!(!covers_pointer([620.0, 63.0])); + assert!(covers_pointer([620.0, 335.0])); + } +} diff --git a/crates/fparkan-assets/src/strategic_roster.rs b/crates/fparkan-assets/src/strategic_roster.rs index 6c755300..b41e4102 100644 --- a/crates/fparkan-assets/src/strategic_roster.rs +++ b/crates/fparkan-assets/src/strategic_roster.rs @@ -203,7 +203,10 @@ impl RosterImages { ); } self.icons(&mut panel, row, [12, y + 2]); - if let Some((width, rgba)) = row.durability_percent.and_then(durability_fill) { + if let Some((width, rgba)) = row + .durability_percent + .and_then(|percent| durability_fill(percent, TEXT_WIDTH)) + { let fill = RgbaImage { width: 1, height: 1, @@ -262,8 +265,8 @@ fn stamp(panel: &mut RgbaImage, sprite: &RgbaImage, bounds: [u32; 4], tint: [u8; copy_sprite(panel, sprite, bounds, [false; 2], tint); } -fn durability_fill(percent: i32) -> Option<(u32, [u8; 4])> { - let width = (f64::from(percent.min(100)) * f64::from(TEXT_WIDTH) * f64::from(0.01_f32)) +pub(crate) fn durability_fill(percent: i32, span: u32) -> Option<(u32, [u8; 4])> { + let width = (f64::from(percent.min(100)) * f64::from(span) * f64::from(0.01_f32)) .round_ties_even() as u32; let rgba = match percent { ..=19 => [128, 0, 0, 128], @@ -289,6 +292,7 @@ mod tests { #[test] fn original_durability_colours_follow_the_rounded_health_percentage() { + let durability_fill = |percent| super::durability_fill(percent, TEXT_WIDTH); assert_eq!(durability_fill(0), None); assert_eq!(durability_fill(19), Some((47, [128, 0, 0, 128]))); assert_eq!(durability_fill(20), Some((50, [128, 128, 0, 128]))); |
