//! Resource balance rows assembled from the original compound controls. use crate::{game_font::load_layout_font, mission_pager::copy_sprite, ui_sprite::load_ui_sprites}; use fparkan_font::BitmapFont; use fparkan_texm::RgbaImage; use fparkan_vfs::Vfs; const ROW_HEIGHT: u16 = 19; const SEPARATION: u32 = 21; const LABEL_WIDTH: u16 = 205; const VALUE_WIDTH: u16 = 35; const GREEN: [u8; 3] = [128, 255, 128]; const RED: [u8; 3] = [255, 50, 50]; /// Panel size in the original 640×480 layout, including the overlapping edge pixel. pub const EXTENT: [u32; 2] = [267, SEPARATION + ROW_HEIGHT as u32]; /// Atlas pieces and the original font for the two strategic balance rows. pub struct BalancePanel { background: RgbaImage, labels: [RgbaImage; 2], font: BitmapFont, } impl BalancePanel { /// Loads the reusable atlas art and centers the localized labels in their fields. pub fn load(vfs: &dyn Vfs, labels: [&str; 2]) -> Result { let [end, value, separator, emitter, body, cap] = load_ui_sprites( vfs, "ui/compaund.cfg", [ "ccres_ending_text", "ccres_body_text", "ccres_separator_left_text", "ccres_ray_emitter_off", "ccres_ray_body", "ccres_ray_ending", ], )?; let cells = [ (&end, 5), (&value, u32::from(VALUE_WIDTH)), (&separator, 5), (&emitter, 10), (&body, u32::from(LABEL_WIDTH)), (&cap, 6), ]; let [width, height] = EXTENT; let mut background = RgbaImage { width, height, rgba8: vec![0; (width * height * 4) as usize], }; for y in [0, SEPARATION] { let mut right = width; for (sprite, width) in cells { copy_sprite( &mut background, sprite, [right - width - 1, y, right, y + u32::from(ROW_HEIGHT)], [false; 2], [255; 3], ); right -= width; } } let font = load_layout_font(vfs)?; let [ore, power] = labels.map(|text| font.centered_line(text, LABEL_WIDTH, ROW_HEIGHT)); Ok(Self { background, labels: [ore?, power?], font, }) } /// Composes the filled bars and their text. A hidden zero keeps the row frame. pub fn paint(&self, [ore, power]: [Option; 2]) -> Result { const LABEL_LEFT: u32 = 7; const LABEL_RIGHT: u32 = LABEL_LEFT + LABEL_WIDTH as u32; const VALUE_LEFT: u32 = EXTENT[0] - 5 - VALUE_WIDTH as u32; let mut image = self.background.clone(); for (row, percent) in [ore, power].into_iter().enumerate() { let Some(percent) = percent else { continue; }; let y = row as u32 * SEPARATION; let end = y + u32::from(ROW_HEIGHT); let filled = (f64::from(percent.min(100)) * f64::from(LABEL_WIDTH) * f64::from(0.01_f32)) .round_ties_even() as u32; if filled > 2 { let color = match percent { 0..=19 => [128, 0, 0, 128], 20..=79 => [128, 128, 0, 128], _ => [0, 128, 0, 128], }; copy_sprite( &mut image, &RgbaImage { width: 1, height: 1, rgba8: color.to_vec(), }, [LABEL_RIGHT - filled, y + 3, LABEL_RIGHT - 2, end - 3], [false; 2], [255; 3], ); } let tint = if percent == 0 { RED } else { GREEN }; copy_sprite( &mut image, &self.labels[row], [LABEL_LEFT, y, LABEL_RIGHT, end], [false; 2], tint, ); let number = self.font .centered_line(&format!("{percent}%"), VALUE_WIDTH, ROW_HEIGHT)?; copy_sprite( &mut image, &number, [VALUE_LEFT, y, VALUE_LEFT + u32::from(VALUE_WIDTH), end], [false; 2], tint, ); } Ok(image) } }