diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 15:20:23 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 15:20:23 +0300 |
| commit | cc6bbdfa4aebf844ec7be2e8eb01b7fd1c69d24b (patch) | |
| tree | 4b69fecd2c3172724ad62cd2e75d1bb456ca0c65 /crates/fparkan-assets/src | |
| parent | 04e58559f76c89a284dfab820efed25a97db2461 (diff) | |
| download | fparkan-cc6bbdfa4aebf844ec7be2e8eb01b7fd1c69d24b.tar.xz fparkan-cc6bbdfa4aebf844ec7be2e8eb01b7fd1c69d24b.zip | |
feat(ui): show strategic factories and order a warbot
Diffstat (limited to 'crates/fparkan-assets/src')
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_factory.rs | 276 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategy_toolbar.rs | 3 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/warbot_card.rs | 18 |
4 files changed, 294 insertions, 4 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 800a8e64..f07e2af4 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -48,6 +48,7 @@ pub mod object_descriptions; pub mod satellite_map; pub mod satellite_map_chrome; pub mod strategic_balance; +pub mod strategic_factory; pub mod strategic_roster; pub mod strategy_cursor; pub mod strategy_toolbar; diff --git a/crates/fparkan-assets/src/strategic_factory.rs b/crates/fparkan-assets/src/strategic_factory.rs new file mode 100644 index 00000000..e9d1a387 --- /dev/null +++ b/crates/fparkan-assets/src/strategic_factory.rs @@ -0,0 +1,276 @@ +//! The strategic factory panel, composed from the installed compound controls. + +use crate::{ + game_font::{load_layout_font, BitmapFont}, + mission_pager::copy_sprite, + ui_sprite::{atlas_region, load_ui_page, load_ui_sprites}, + warbot_card::{self, CardLine}, + RgbaImage, +}; +use fparkan_vfs::Vfs; + +pub const ORIGIN: [u32; 2] = [51, 0]; +pub const EXTENT: [u32; 2] = [319, 480]; +/// iron3d+0x98240 places the rotating preview at card origin + (185, 2). +pub const MODEL_RECT: [f32; 4] = [236.0, 23.0, 351.0, 138.0]; +const ROW_TOP: u32 = 171; +const ROW_PITCH: u32 = 20; +const ROW_HEIGHT: u32 = 19; +pub const VISIBLE_PLANTS: usize = ((EXTENT[1] - ROW_TOP) / ROW_PITCH) as usize; +const BUILD: [u32; 4] = [263, 150, 313, 169]; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FactoryAction { + Close, + Project(usize), + Build, + Plant(usize), +} + +fn project_rect(index: usize) -> [u32; 4] { + let left = 10 + index as u32 * 23; + [left, 125, left + 19, 142] +} + +pub fn hit_test( + [x, y]: [f32; 2], + plants: usize, + projects: usize, + idle: bool, +) -> Option<FactoryAction> { + let [x, y] = [x.round_ties_even() - ORIGIN[0] as f32, y.round_ties_even()]; + let hit = |[left, top, right, bottom]: [u32; 4]| { + (left as f32..right as f32).contains(&x) && (top as f32..bottom as f32).contains(&y) + }; + if hit([283, 0, 318, 19]) { + return Some(FactoryAction::Close); + } + if idle && projects != 0 && hit(BUILD) { + return Some(FactoryAction::Build); + } + if let Some(index) = (0..projects).find(|&index| hit(project_rect(index))) { + return Some(FactoryAction::Project(index)); + } + (0..plants.min(VISIBLE_PLANTS)) + .find(|&index| { + let top = ROW_TOP + index as u32 * ROW_PITCH; + hit([0, top, 318, top + ROW_HEIGHT]) + }) + .map(FactoryAction::Plant) +} + +pub struct FactoryRow<'a> { + pub name: &'a str, + pub selected: bool, +} + +pub struct FactoryPicture<'a> { + pub rows: &'a [FactoryRow<'a>], + pub project_name: Option<&'a str>, + pub stats: &'a [Option<CardLine<'a>>], + pub projects: usize, + pub selected_project: Option<usize>, + pub progress: Option<u8>, + pub free_minds: u32, +} + +pub struct FactoryImages { + font: BitmapFont, + base: RgbaImage, + row_cap: [RgbaImage; 2], + row_body: RgbaImage, + ray: RgbaImage, + icons: [RgbaImage; 5], +} + +impl FactoryImages { + pub fn load(vfs: &dyn Vfs, title: &str) -> Result<Self, String> { + let [title_cap, body, close, row_off, row_on, ray, tl, tr, br, bl, h, v] = load_ui_sprites( + vfs, + "ui/compaund.cfg", + [ + "ccres_ending_text", + "ccres_body_text", + "ccres_exit_button_pressed", + "ccres_lamp_text_ending_off", + "ccres_lamp_text_ending_normal", + "ccres_ray_body", + "ccres_frame_corner_1", + "ccres_frame_corner_2", + "ccres_frame_corner_3", + "ccres_frame_corner_4", + "ccres_frame_edge_h", + "ccres_frame_edge_v", + ], + )?; + let [exit, project, project_frame, build, free] = load_ui_sprites( + vfs, + "ui/hq.cfg", + [ + "exit_icon", + "project_icon", + "short_button_frame_off", + "build_icon", + "brain_icon", + ], + )?; + let atlas = load_ui_page(vfs, "ui_menu")?; + let plant = + atlas_region(&atlas, [129, 94, 15, 15]).ok_or("factory glyph outside ui_menu")?; + let font = load_layout_font(vfs)?; + let mut base = RgbaImage { + width: EXTENT[0], + height: EXTENT[1], + rgba8: vec![0; (EXTENT[0] * EXTENT[1] * 4) as usize], + }; + let green = RgbaImage { + width: 1, + height: 1, + rgba8: vec![0, 128, 0, 128], + }; + for (sprite, rect) in [ + (&title_cap, [0, 0, 6, 19]), + (&body, [5, 0, 284, 19]), + (&close, [283, 0, 318, 19]), + (&exit, [298, 3, 311, 16]), + (&green, [5, 26, 313, 142]), + (&h, [8, 21, 310, 26]), + (&h, [8, 142, 310, 147]), + (&v, [0, 29, 5, 139]), + (&v, [313, 29, 318, 139]), + (&tl, [0, 21, 8, 29]), + (&tr, [310, 21, 318, 29]), + (&br, [310, 139, 318, 147]), + (&bl, [0, 139, 8, 147]), + ] { + stamp(&mut base, sprite, rect, [255; 3]); + } + text(&font, &mut base, title, [5, 0, 283, 19], [255; 3])?; + Ok(Self { + font, + base, + row_cap: [row_off, row_on], + row_body: body, + ray, + icons: [project, project_frame, plant, build, free], + }) + } + + pub fn paint(&self, picture: FactoryPicture<'_>) -> Result<RgbaImage, String> { + let mut image = self.base.clone(); + let [project, frame, plant, build, free] = &self.icons; + if let Some(name) = picture.project_name { + let width = u32::from(self.font.line_metrics(name)?[0]).min(170); + text( + &self.font, + &mut image, + name, + [10, 27, 10 + width, 43], + [255, 255, 0], + )?; + warbot_card::paint_at(&self.font, &mut image, picture.stats, [10, 144, 147], 43)?; + } + for index in 0..picture.projects { + let rect = project_rect(index); + stamp( + &mut image, + frame, + [rect[0] - 2, rect[1] - 2, rect[2] + 2, rect[3] + 2], + [255; 3], + ); + let tint = if picture.selected_project == Some(index) { + [255; 3] + } else { + [128, 255, 128] + }; + stamp(&mut image, project, rect, tint); + } + stamp(&mut image, free, [274, 127, 289, 142], [128, 255, 128]); + text( + &self.font, + &mut image, + &picture.free_minds.to_string(), + [289, 125, 306, 144], + [128, 255, 128], + )?; + stamp(&mut image, &self.row_body, [5, 150, 263, 169], [255; 3]); + if let Some(progress) = picture.progress { + text( + &self.font, + &mut image, + &format!("{progress}%"), + [5, 150, 263, 169], + [255; 3], + )?; + } else if picture.selected_project.is_some() && picture.free_minds > 0 { + stamp(&mut image, &self.row_body, BUILD, [255; 3]); + stamp( + &mut image, + build, + [BUILD[0] + 10, BUILD[1] + 2, BUILD[0] + 40, BUILD[1] + 17], + [255; 3], + ); + } + for (index, row) in picture.rows.iter().take(VISIBLE_PLANTS).enumerate() { + let y = ROW_TOP + index as u32 * ROW_PITCH; + let bottom = y + ROW_HEIGHT; + stamp( + &mut image, + &self.row_cap[usize::from(row.selected)], + [0, y, 11, bottom], + [255; 3], + ); + stamp(&mut image, &self.row_body, [10, y, 38, bottom], [255; 3]); + stamp(&mut image, plant, [13, y + 2, 28, y + 17], [255, 0, 0]); + stamp(&mut image, &self.ray, [38, y, 318, bottom], [255; 3]); + text( + &self.font, + &mut image, + row.name, + [38, y, 318, bottom], + if row.selected { [255; 3] } else { [128; 3] }, + )?; + } + Ok(image) + } +} + +fn stamp(image: &mut RgbaImage, sprite: &RgbaImage, rect: [u32; 4], tint: [u8; 3]) { + copy_sprite(image, sprite, rect, [false; 2], tint); +} + +fn text( + font: &BitmapFont, + image: &mut RgbaImage, + label: &str, + rect: [u32; 4], + tint: [u8; 3], +) -> Result<(), String> { + let [left, top, right, bottom] = rect; + let glyphs = font.centered_line(label, (right - left) as u16, (bottom - top) as u16)?; + stamp(image, &glyphs, rect, tint); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn factory_actions_follow_project_and_row_extents() { + assert_eq!( + hit_test([95.0, 132.0], 1, 2, true), + Some(FactoryAction::Project(1)) + ); + assert_eq!(hit_test([95.0, 132.0], 1, 1, true), None); + assert_eq!( + hit_test([340.0, 160.0], 1, 2, true), + Some(FactoryAction::Build) + ); + assert_eq!(hit_test([340.0, 160.0], 1, 2, false), None); + assert_eq!( + hit_test([200.0, 172.0], 1, 2, true), + Some(FactoryAction::Plant(0)) + ); + } +} diff --git a/crates/fparkan-assets/src/strategy_toolbar.rs b/crates/fparkan-assets/src/strategy_toolbar.rs index 11a3fcab..a50520c2 100644 --- a/crates/fparkan-assets/src/strategy_toolbar.rs +++ b/crates/fparkan-assets/src/strategy_toolbar.rs @@ -29,6 +29,8 @@ pub enum ToolbarAction { Hero, /// Toggle the clan's warbot list. Warbots, + /// Toggle the clan's factory panel. + Factories, /// Toggle the satellite map. Map, } @@ -39,6 +41,7 @@ pub fn command_button_at([x, y]: [f32; 2]) -> Option<ToolbarAction> { match button_index_at([x, y])? { 0 => Some(ToolbarAction::Hero), 1 => Some(ToolbarAction::Warbots), + 5 => Some(ToolbarAction::Factories), 10 => Some(ToolbarAction::Map), _ => None, } diff --git a/crates/fparkan-assets/src/warbot_card.rs b/crates/fparkan-assets/src/warbot_card.rs index 015bddce..c9608622 100644 --- a/crates/fparkan-assets/src/warbot_card.rs +++ b/crates/fparkan-assets/src/warbot_card.rs @@ -126,10 +126,20 @@ pub(super) fn paint( panel: &mut RgbaImage, lines: &[Option<CardLine<'_>>], ) -> Result<(), String> { + paint_at(font, panel, lines, [71, 203, 206], 40) +} + +pub(super) fn paint_at( + font: &BitmapFont, + panel: &mut RgbaImage, + lines: &[Option<CardLine<'_>>], + [label, number, unit]: [u32; 3], + top: u32, +) -> Result<(), String> { let height = u32::from(font.line_metrics("A")?[1]); for (index, line) in lines.iter().enumerate() { let Some(line) = line else { continue }; - let y = 40 + index as u32 * (height + 2); + let y = top + index as u32 * (height + 2); let green = [0, 255, 0]; let value = if line.warn { [255, 0, 0] @@ -137,9 +147,9 @@ pub(super) fn paint( [180, 180, 255] }; for (text, x, align_right, tint) in [ - (line.label, 71_u32, false, green), - (line.number.as_str(), 203, true, value), - (line.unit, 206, false, green), + (line.label, label, false, green), + (line.number.as_str(), number, true, value), + (line.unit, unit, false, green), ] { if text.is_empty() { continue; |
