//! 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]; const BATCH: [u32; 4] = [211, 150, 261, 169]; const CONSTRUCTOR: [u32; 4] = [5, 150, 55, 169]; const ACTIVE: [u32; 4] = [135, 125, 165, 142]; const FREE_BUILD: [u32; 4] = [228, 127, 258, 142]; const MINDS: [u32; 4] = [274, 125, 307, 144]; pub const TOOLTIP_IDS: [u32; 10] = [ 0x611, 0xbfe, 0x185e, 0x185f, 0x5e7, 0xbfc, 0xbfd, 0xbfb, 0x1868, 0x1869, ]; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum FactoryAction { Close, Project(usize), ActiveProject, Build, Batch, Cancel, Designer, Plant(usize), } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum FactoryOrders { Ready, Unavailable, Single, Batch, } impl FactoryOrders { pub fn for_plant(batch: bool, active: bool, project_selected: bool, free_minds: u32) -> Self { if batch { Self::Batch } else if active { Self::Single } else if project_selected && free_minds > 0 { Self::Ready } else { Self::Unavailable } } } fn project_rect(index: usize) -> [u32; 4] { let left = 10 + index as u32 * 23; [left, 125, left + 19, 142] } #[derive(Clone, Copy)] pub struct FactoryControls { pub plants: usize, pub projects: usize, pub orders: FactoryOrders, pub designer_available: bool, pub active_available: bool, pub free_build: bool, } impl FactoryControls { pub fn hit_test(self, at: [f32; 2]) -> Option { let hit = |rect| contains(at, rect); if hit([283, 0, 318, 19]) { return Some(FactoryAction::Close); } if hit(BUILD) { return match self.orders { FactoryOrders::Ready => Some(FactoryAction::Build), FactoryOrders::Single => Some(FactoryAction::Cancel), _ => None, }; } if hit(BATCH) { return match self.orders { FactoryOrders::Ready => Some(FactoryAction::Batch), FactoryOrders::Batch => Some(FactoryAction::Cancel), _ => None, }; } if self.designer_available && hit(CONSTRUCTOR) { return Some(FactoryAction::Designer); } if self.active_available && hit(ACTIVE) { return Some(FactoryAction::ActiveProject); } if let Some(index) = (0..self.projects).find(|&index| hit(project_rect(index))) { return Some(FactoryAction::Project(index)); } (0..self.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 fn tooltip(self, at: [f32; 2]) -> Option { let hit = |rect| contains(at, rect); let busy = matches!(self.orders, FactoryOrders::Single | FactoryOrders::Batch); if hit(BUILD) { Some(if busy { 0xbfe } else { 0x611 }) } else if hit(BATCH) { Some(if busy { 0x185f } else { 0x185e }) } else if hit(CONSTRUCTOR) { Some(0x5e7) } else if self.active_available && hit(ACTIVE) { Some(0xbfd) } else if (0..self.projects).any(|index| hit(project_rect(index))) { Some(0xbfc) } else if hit(FREE_BUILD) { Some(if self.free_build { 0x1868 } else { 0x1869 }) } else if hit(MINDS) { Some(0xbfb) } else { None } } } fn contains([x, y]: [f32; 2], [left, top, right, bottom]: [u32; 4]) -> bool { let x = x.round_ties_even() - ORIGIN[0] as f32; let y = y.round_ties_even(); (left as f32..right as f32).contains(&x) && (top as f32..bottom as f32).contains(&y) } 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>], pub controls: FactoryControls, pub selected_project: Option, pub viewing_active: bool, pub progress: Option, pub free_minds: u32, } pub struct FactoryImages { font: BitmapFont, base: RgbaImage, row_cap: [RgbaImage; 2], row_body: RgbaImage, ray: RgbaImage, icons: [RgbaImage; 7], batch_icons: [RgbaImage; 2], active_project: RgbaImage, free_bots_icons: [RgbaImage; 2], } impl FactoryImages { pub fn load(vfs: &dyn Vfs, title: &str) -> Result { 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, stop, constructor, free, batch, stop_batch, active_project, free_bots, no_free_bots] = load_ui_sprites( vfs, "ui/hq.cfg", [ "exit_icon", "project_icon", "short_button_frame_off", "build_icon", "stop_build_icon", "constructor_icon", "brain_icon", "batch_build_icon", "batch_stop_build_icon", "active_project", "free_bots_icon", "no_free_bots_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, stop, constructor, free, ], batch_icons: [batch, stop_batch], active_project, free_bots_icons: [no_free_bots, free_bots], }) } pub fn paint(&self, picture: FactoryPicture<'_>) -> Result { let mut image = self.base.clone(); let [project, frame, plant, build, stop, constructor, 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.controls.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.viewing_active && picture.selected_project == Some(index) { [255; 3] } else { [128, 255, 128] }; stamp(&mut image, project, rect, tint); } if picture.controls.active_available { stamp(&mut image, frame, [133, 123, 167, 144], [255; 3]); let tint = if picture.viewing_active { [255; 3] } else { [128, 255, 128] }; stamp(&mut image, &self.active_project, ACTIVE, tint); } stamp( &mut image, &self.free_bots_icons[usize::from(picture.controls.free_build)], FREE_BUILD, [255; 3], ); 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], )?; let progress_rect = [55, 150, BATCH[0], 169]; stamp(&mut image, &self.row_body, progress_rect, [255; 3]); if let Some(progress) = picture.progress { text( &self.font, &mut image, &format!("{progress}%"), progress_rect, [255; 3], )?; } let orders = picture.controls.orders; let busy = matches!(orders, FactoryOrders::Single | FactoryOrders::Batch); for (rect, icon, enabled) in [ ( CONSTRUCTOR, constructor, picture.controls.designer_available, ), ( BATCH, &self.batch_icons[usize::from(busy)], matches!(orders, FactoryOrders::Ready | FactoryOrders::Batch), ), ( BUILD, if busy { stop } else { build }, matches!(orders, FactoryOrders::Ready | FactoryOrders::Single), ), ] { let tint = if enabled { [255; 3] } else { [104; 3] }; let [x, y, _, _] = rect; stamp(&mut image, &self.row_body, rect, [255; 3]); stamp(&mut image, icon, [x + 10, y + 2, x + 40, y + 17], tint); } 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) } } pub(crate) fn stamp(image: &mut RgbaImage, sprite: &RgbaImage, rect: [u32; 4], tint: [u8; 3]) { copy_sprite(image, sprite, rect, [false; 2], tint); } pub(crate) 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() { use FactoryOrders::{Batch, Ready, Single, Unavailable}; let mut controls = FactoryControls { plants: 1, projects: 2, orders: Ready, designer_available: true, active_available: false, free_build: true, }; assert_eq!( controls.hit_test([95.0, 132.0]), Some(FactoryAction::Project(1)) ); assert_eq!( controls.hit_test([340.0, 160.0]), Some(FactoryAction::Build) ); assert_eq!(controls.tooltip([340.0, 160.0]), Some(0x611)); assert_eq!( controls.hit_test([289.0, 160.0]), Some(FactoryAction::Batch) ); assert_eq!( controls.hit_test([200.0, 172.0]), Some(FactoryAction::Plant(0)) ); assert_eq!( controls.hit_test([80.0, 160.0]), Some(FactoryAction::Designer) ); assert_eq!(controls.hit_test([201.0, 132.0]), None); assert_eq!(controls.tooltip([201.0, 132.0]), None); assert_eq!(controls.tooltip([95.0, 132.0]), Some(0xbfc)); assert_eq!(controls.tooltip([287.0, 132.0]), Some(0x1868)); controls.orders = Single; controls.active_available = true; assert_eq!(controls.hit_test([289.0, 160.0]), None); assert_eq!( controls.hit_test([340.0, 160.0]), Some(FactoryAction::Cancel) ); assert_eq!( controls.hit_test([201.0, 132.0]), Some(FactoryAction::ActiveProject) ); assert_eq!(controls.tooltip([201.0, 132.0]), Some(0xbfd)); controls.orders = Batch; assert_eq!(controls.hit_test([340.0, 160.0]), None); assert_eq!( controls.hit_test([289.0, 160.0]), Some(FactoryAction::Cancel) ); assert_eq!(controls.tooltip([289.0, 160.0]), Some(0x185f)); controls.orders = Unavailable; controls.projects = 1; controls.designer_available = false; controls.free_build = false; assert_eq!(controls.hit_test([95.0, 132.0]), None); assert_eq!(controls.hit_test([340.0, 160.0]), None); assert_eq!(controls.hit_test([80.0, 160.0]), None); assert_eq!(controls.tooltip([287.0, 132.0]), Some(0x1869)); } }