//! 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]; const CONSTRUCTOR: [u32; 4] = [20, 28, 70, 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, /// Open the active factory's model designer. Designer, } /// 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, constructor_icon: RgbaImage, font: BitmapFont, } impl FactoryProjectImages { /// Resolves factory sprites and text through the installed game's catalogs. pub fn load(vfs: &dyn Vfs) -> Result { let [idle, pressed, project, build_icon, constructor_icon] = load_ui_sprites( vfs, "ui/hq.cfg", [ "short_button_frame_off", "short_button_frame_on", "project_icon", "build_icon", "constructor_icon", ], )?; let [build_frame] = load_ui_sprites(vfs, "ui/compaund.cfg", ["ccres_long_button_normal"])?; Ok(Self { frames: [idle, pressed], project, build_icon, constructor_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, name: &str, designer_available: bool, ) -> Result { 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] }, ); } self.paint_button(&mut image, BUILD, &self.build_icon); if designer_available { self.paint_button(&mut image, CONSTRUCTOR, &self.constructor_icon); } let label = self.font.centered_line(name, 150, 16)?; copy_sprite(&mut image, &label, [125, 17, 275, 33], [false; 2], [255; 3]); Ok(image) } fn paint_button(&self, image: &mut RgbaImage, rect: [u32; 4], icon: &RgbaImage) { copy_sprite(image, &self.build_frame, rect, [false; 2], [255; 3]); let [left, top, _, _] = rect; copy_sprite( image, icon, [left + 10, top + 2, left + 40, top + 17], [false; 2], [240; 3], ); } } /// Resolves only populated slots and the order button in 640×480 coordinates. pub fn hit_test(position: [f32; 2], count: usize) -> Option { 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); } if hit(CONSTRUCTOR) { return Some(FactoryProjectAction::Designer); } (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); } }