From 7dfbf534267011ad629c11c7140d0f9c0fe9cd3b Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 10:44:41 +0400 Subject: feat(ui): show the original strategic resource balances --- crates/fparkan-assets/src/lib.rs | 1 + crates/fparkan-assets/src/strategic_balance.rs | 130 +++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 crates/fparkan-assets/src/strategic_balance.rs (limited to 'crates/fparkan-assets/src') diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 5bdd7579..d64425ef 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 strategic_balance; pub mod strategy_toolbar; mod turret_elevation; mod ui_configuration; diff --git a/crates/fparkan-assets/src/strategic_balance.rs b/crates/fparkan-assets/src/strategic_balance.rs new file mode 100644 index 00000000..eeebbce0 --- /dev/null +++ b/crates/fparkan-assets/src/strategic_balance.rs @@ -0,0 +1,130 @@ +//! 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) + } +} -- cgit v1.2.3