//! Composition of the cockpit instruments from the original UI sheets. use crate::{ game_font::{load_layout_font, BitmapFont}, mission_pager::copy_sprite, strategic_factory::{stamp, text}, ui_sprite::{atlas_region, load_ui_page, load_ui_sprites}, RgbaImage, }; use fparkan_vfs::Vfs; const METER_HEIGHT: u32 = 94; #[derive(Clone, Copy, PartialEq, Eq)] pub enum Side { Left, Right, } impl Side { pub fn frame(self) -> [f32; 4] { match self { Self::Left => [0.0, 306.0, 150.0, 480.0], Self::Right => [490.0, 306.0, 640.0, 480.0], } } pub fn model_viewport(self) -> [f32; 4] { match self { Self::Left => [9.0, 315.0, 137.0, 443.0], Self::Right => [503.0, 315.0, 631.0, 443.0], } } } pub struct Readout<'a> { pub name: &'a str, pub status: Option<(&'a str, [u8; 3])>, pub distance: Option<&'a str>, pub durability: Option, pub energy: u8, pub shield: Option<[u8; 6]>, } pub struct Images { bezel: RgbaImage, life: RgbaImage, energy: RgbaImage, range: RgbaImage, shields: [RgbaImage; 5], page: RgbaImage, font: BitmapFont, } impl Images { pub fn load(vfs: &dyn Vfs) -> Result { let [bezel, life, energy, range, side, front, back, top, bottom] = load_ui_sprites( vfs, "ui/hq.cfg", [ "targeter_back", "targeter_life", "targeter_energy", "targeter_range", "left_shld", "frwd_shld", "back_shld", "top_shld", "bott_shld", ], )?; let page = load_ui_page(vfs, "ui_menu3")?; let font = load_layout_font(vfs)?; Ok(Self { bezel, life, energy, range, shields: [side, front, back, top, bottom], page, font, }) } pub fn paint(&self, side: Side, readout: Option>) -> Result { let (width, height) = (self.bezel.width, self.bezel.height); let mut image = RgbaImage { width, height, rgba8: vec![0; (width * height * 4) as usize], }; let right = side == Side::Right; let meter_rect = |[x0, y0, x1, y1]: [u32; 4]| { if right { [x0, y0, x1, y1] } else { [150 - x1, y0, 150 - x0, y1] } }; copy_sprite( &mut image, &self.bezel, [0, 0, width, height], [right, false], [255; 3], ); stamp( &mut image, &self.life, meter_rect([6, 105, 21, 120]), [255; 3], ); stamp( &mut image, &self.energy, meter_rect([130, 105, 145, 120]), [255; 3], ); let Some(readout) = readout else { return Ok(image); }; if let Some([front, back, left, right_shield, top, bottom]) = readout.shield { let [side_image, front_image, back_image, top_image, bottom_image] = &self.shields; for (sprite, [x0, y0, x1, y1], flip, value) in [ (side_image, [16, 28, 42, 107], false, left), (side_image, [107, 28, 133, 107], true, right_shield), (front_image, [44, 15, 105, 34], false, front), (back_image, [30, 103, 119, 132], false, back), (top_image, [40, 2, 109, 21], false, top), (bottom_image, [22, 114, 127, 145], false, bottom), ] { let shift = u32::from(right); copy_sprite( &mut image, sprite, [x0 + shift, y0, x1 + shift, y1], [flip, false], [255 - value, value, 0], ); } } for (percent, x, flip, tint) in [ (readout.durability.unwrap_or(0), 1, false, [25, 255, 175]), (i32::from(readout.energy), 110, true, [255, 180, 80]), ] { let cut = meter_cut(percent); if cut == METER_HEIGHT { continue; } let strip = atlas_region(&self.page, [151, 82 + cut, 40, METER_HEIGHT - cut]) .ok_or("cockpit meter outside ui_menu3")?; copy_sprite( &mut image, &strip, meter_rect([x, 8 + cut, x + 40, 102]), [flip == right, false], tint, ); } text( &self.font, &mut image, readout.name, [4, 153, 142, 164], [255; 3], )?; if let Some((status, tint)) = readout.status { text(&self.font, &mut image, status, [4, 163, 142, 174], tint)?; } if side == Side::Left { stamp(&mut image, &self.range, [108, 134, 147, 150], [255; 3]); } if let Some(distance) = readout.distance { text( &self.font, &mut image, distance, [112, 138, 143, 149], [0, 255, 0], )?; } Ok(image) } } fn meter_cut(percent: i32) -> u32 { (100 - percent.clamp(0, 100)) as u32 * METER_HEIGHT / 100 } #[cfg(test)] #[test] fn the_native_bar_fills_upward_in_integer_pixels() { assert_eq!(meter_cut(-1), 94); assert_eq!(meter_cut(25), 70); assert_eq!(meter_cut(99), 0); assert_eq!(meter_cut(120), 0); }