//! 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, } /// 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 { 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 { 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>) -> Result { 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, size) => building_glyph_tint(size), _ => [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]), class => building_glyph(class), }; 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); } } } } // The list and map readout both call iron3d+0x346a0 for building glyphs. pub(crate) fn building_glyph(class: u32) -> Option<[u32; 2]> { Some(match class { 0x8000_0002 => [113, 126], 0x8000_0004 => [129, 126], 0x8000_0008 => [81, 110], 0x8000_0010 => [129, 94], 0x8000_0040 => [97, 126], 0x8000_0200 => [148, 27], 0x8000_0400 => [49, 126], 0x8001_0000 | 0x8002_0000 | 0x8004_0000 => [65, 126], 0x8010_0000 | 0x8020_0000 => [81, 126], _ => return None, }) } pub(crate) fn building_glyph_tint(size: u32) -> [u8; 3] { match size { 2 => [255, 0, 0], 3 => [0, 255, 0], 4 | 5 => [0, 0, 255], _ => [255; 3], } } 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])); } }