diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 04:39:32 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 04:39:32 +0300 |
| commit | 6c2545e8b548a8b892ef2a3adaba1a138ab623c9 (patch) | |
| tree | 4e1a9926db1fbb566db5e4a84065b5570a951aaa /crates/fparkan-assets | |
| parent | cd45231dd2a4fd9f970d9c7e06004cedd5c9add6 (diff) | |
| download | fparkan-6c2545e8b548a8b892ef2a3adaba1a138ab623c9.tar.xz fparkan-6c2545e8b548a8b892ef2a3adaba1a138ab623c9.zip | |
feat(ui): select and order factory projects in the mission
Diffstat (limited to 'crates/fparkan-assets')
| -rw-r--r-- | crates/fparkan-assets/src/factory_projects.rs | 157 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 1 |
2 files changed, 158 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/factory_projects.rs b/crates/fparkan-assets/src/factory_projects.rs new file mode 100644 index 00000000..94973b22 --- /dev/null +++ b/crates/fparkan-assets/src/factory_projects.rs @@ -0,0 +1,157 @@ +//! Factory project buttons in the factory panel's logical coordinate space. + +use crate::{ + game_font::{load_layout_font, BitmapFont}, + mission_pager::copy_sprite, + ui_sprite::load_ui_sprites, + RgbaImage, +}; +use fparkan_vfs::Vfs; + +/// Number of project slots on the factory panel. +pub const PROJECT_SLOTS: usize = 5; +/// Padded texture origin at the stock factory panel. +pub const ORIGIN: [u32; 2] = [50, 122]; +/// Logical size of the project controls texture. +// A transparent texel on each side keeps repeat sampling from bleeding buttons across the quad. +pub const EXTENT: [u32; 2] = [328, 48]; +const BUILD: [u32; 4] = [277, 28, 327, 47]; + +/// A command selected with the pointer. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FactoryProjectAction { + /// Select a slot for inspection or production. + Select(usize), + /// Queue the selected project at this factory. + Build, +} + +/// Original button frames, icons and game font for the project controls. +pub struct FactoryProjectImages { + frames: [RgbaImage; 2], + project: RgbaImage, + build_frame: RgbaImage, + build_icon: RgbaImage, + font: BitmapFont, +} + +impl FactoryProjectImages { + /// Resolves factory sprites and text through the installed game's catalogs. + pub fn load(vfs: &dyn Vfs) -> Result<Self, String> { + let [idle, pressed, project, build_icon] = load_ui_sprites( + vfs, + "ui/hq.cfg", + [ + "short_button_frame_off", + "short_button_frame_on", + "project_icon", + "build_icon", + ], + )?; + let [build_frame] = load_ui_sprites(vfs, "ui/compaund.cfg", ["ccres_long_button_normal"])?; + Ok(Self { + frames: [idle, pressed], + project, + build_icon, + build_frame, + font: load_layout_font(vfs)?, + }) + } + + /// Composes just the factory project controls over the host view. + pub fn paint( + &self, + count: usize, + selected: usize, + pressed: Option<usize>, + name: &str, + ) -> Result<RgbaImage, String> { + let mut image = RgbaImage { + width: EXTENT[0], + height: EXTENT[1], + rgba8: vec![0; (EXTENT[0] * EXTENT[1] * 4) as usize], + }; + for index in 0..count.min(PROJECT_SLOTS) { + let left = project_left(index); + copy_sprite( + &mut image, + &self.frames[usize::from(pressed == Some(index))], + [left, 1, left + 23, 22], + [false; 2], + [255; 3], + ); + copy_sprite( + &mut image, + &self.project, + project_hit(index), + [false; 2], + if selected == index { + [255; 3] + } else { + [128, 255, 128] + }, + ); + } + copy_sprite(&mut image, &self.build_frame, BUILD, [false; 2], [255; 3]); + let [left, top, _, _] = BUILD; + copy_sprite( + &mut image, + &self.build_icon, + [left + 10, top + 2, left + 40, top + 17], + [false; 2], + [240; 3], + ); + let label = self.font.centered_line(name, 150, 16)?; + copy_sprite(&mut image, &label, [1, 22, 151, 38], [false; 2], [255; 3]); + Ok(image) + } +} + +/// Resolves only populated slots and the order button in 640×480 coordinates. +pub fn hit_test(position: [f32; 2], count: usize) -> Option<FactoryProjectAction> { + let [x, y] = [ + position[0] - ORIGIN[0] as f32, + position[1] - ORIGIN[1] as f32, + ]; + let hit = + |[l, t, r, b]: [u32; 4]| x >= l as f32 && x < r as f32 && y >= t as f32 && y < b as f32; + if count != 0 && hit(BUILD) { + return Some(FactoryProjectAction::Build); + } + (0..count.min(PROJECT_SLOTS)) + .find(|&index| hit(project_hit(index))) + .map(FactoryProjectAction::Select) +} + +fn project_left(index: usize) -> u32 { + 9 + 23 * index as u32 +} + +fn project_hit(index: usize) -> [u32; 4] { + let left = project_left(index); + [left + 2, 3, left + 21, 20] +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_the_glyph_selects_a_populated_factory_slot() { + assert_eq!( + hit_test([61.0, 125.0], 2), + Some(FactoryProjectAction::Select(0)) + ); + assert_eq!( + hit_test([84.0, 125.0], 2), + Some(FactoryProjectAction::Select(1)) + ); + assert_eq!(hit_test([83.0, 125.0], 2), None); + assert_eq!(hit_test([84.0, 125.0], 1), None); + assert_eq!( + hit_test([350.0, 159.0], 2), + Some(FactoryProjectAction::Build) + ); + assert_eq!(hit_test([350.0, 159.0], 0), None); + } +} diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 70e0de6d..18da6ace 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -35,6 +35,7 @@ mod chassis_profile; mod control_drive; mod control_weapon_sound; mod explosion_profile; +pub mod factory_projects; pub mod game_font; pub mod help_resources; pub mod help_screen; |
