//! Shared compound controls of the strategic building sections. use crate::{ game_font::{load_layout_font, BitmapFont}, satellite_map_chrome::{building_glyph, building_glyph_tint}, strategic_factory::{stamp, text}, strategic_roster::durability_fill, ui_sprite::{atlas_region, load_ui_page, load_ui_sprites}, RgbaImage, }; use fparkan_vfs::Vfs; pub use crate::strategic_factory::{EXTENT, ORIGIN}; const ROW_PITCH: u32 = 20; const ROW_HEIGHT: u32 = 19; const TEXT_LEFT: u32 = 44; const TEXT_RIGHT: u32 = 313; const COMMAND_LEFT: u32 = 34; const BUTTON_WIDTH: u32 = 35; pub const COMMAND_TOOLTIP: u32 = 0x5ed; pub const MANUAL_TOOLTIP: u32 = 0x5de; pub const VISIBLE_ROWS: usize = (EXTENT[1] / ROW_PITCH - 1) as usize; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Hit { Close, Row(usize), Command(usize), Manual(usize), } pub fn hit_test([x, y]: [f32; 2], rows: usize, command_buttons: bool) -> Option { let x = x.round_ties_even() - ORIGIN[0] as f32; let y = y.round_ties_even(); if (283.0..318.0).contains(&x) && (0.0..19.0).contains(&y) { return Some(Hit::Close); } if !(0.0..(EXTENT[0] - 1) as f32).contains(&x) || !(20.0..480.0).contains(&y) { return None; } let y = y as u32 - ROW_PITCH; let row = (y / ROW_PITCH) as usize; if row >= rows.min(VISIBLE_ROWS) || y % ROW_PITCH >= ROW_HEIGHT { return None; } let command = COMMAND_LEFT as f32..(COMMAND_LEFT + BUTTON_WIDTH) as f32; let manual = command.end..command.end + BUTTON_WIDTH as f32; if command_buttons && command.contains(&x) { Some(Hit::Command(row)) } else if command_buttons && manual.contains(&x) { Some(Hit::Manual(row)) } else { Some(Hit::Row(row)) } } pub struct Row<'a> { pub label: &'a str, pub class: u32, pub size: u32, pub selected: bool, pub durability_percent: Option, pub manual_control: bool, } pub struct Images { header: RgbaImage, font: BitmapFont, cap: [RgbaImage; 2], frame: RgbaImage, separator: RgbaImage, emitter: [RgbaImage; 2], ray: RgbaImage, end: RgbaImage, atlas: RgbaImage, command_frame: [RgbaImage; 2], command_icon: RgbaImage, manual_icon: RgbaImage, } impl Images { pub fn load(vfs: &dyn Vfs) -> Result { let [title_cap, frame, close, cap_off, cap_on, separator, emitter_off, emitter_on, ray, end] = load_ui_sprites( vfs, "ui/compaund.cfg", [ "ccres_ending_text", "ccres_body_text", "ccres_exit_button_pressed", "ccres_lamp_stub_ending_off", "ccres_lamp_stub_ending_normal", "ccres_separator_left_text", "ccres_ray_emitter_off", "ccres_ray_emitter_normal", "ccres_ray_body", "ccres_ray_ending", ], )?; let [exit, command_off, command_on, command_icon, manual_icon] = load_ui_sprites( vfs, "ui/hq.cfg", [ "exit_icon", "buildscreen_button1", "buildscreen_button2", "buildscreen_direct_icon", "buildscreen_hq_icon", ], )?; let font = load_layout_font(vfs)?; let [width, height] = EXTENT; let mut header = RgbaImage { width, height, rgba8: vec![0; (width * height * 4) as usize], }; for (sprite, rect) in [ (&title_cap, [0, 0, 6, 19]), (&frame, [5, 0, 284, 19]), (&close, [283, 0, 318, 19]), (&exit, [298, 3, 311, 16]), ] { stamp(&mut header, sprite, rect, [255; 3]); } Ok(Self { header, font, cap: [cap_off, cap_on], frame, separator, emitter: [emitter_off, emitter_on], ray, end, atlas: load_ui_page(vfs, "ui_menu")?, command_frame: [command_off, command_on], command_icon, manual_icon, }) } pub fn paint<'a>( &self, title: &str, command_buttons: bool, rows: impl Iterator>, ) -> Result { let mut image = self.header.clone(); text(&self.font, &mut image, title, [5, 0, 283, 19], [255; 3])?; for (index, row) in rows.take(VISIBLE_ROWS).enumerate() { let y = (index as u32 + 1) * ROW_PITCH; let bottom = y + ROW_HEIGHT; let selected = usize::from(row.selected); // The original bunker layout measures the label after two 35-unit controls. let emitter = COMMAND_LEFT + if command_buttons { 2 * BUTTON_WIDTH } else { 0 }; let text_left = emitter + (TEXT_LEFT - COMMAND_LEFT); for (sprite, left, right) in [ (&self.cap[selected], 0, 11), (&self.frame, 10, 30), (&self.separator, 29, 35), (&self.emitter[selected], emitter, emitter + 11), (&self.ray, text_left, TEXT_RIGHT + 1), (&self.end, TEXT_RIGHT, EXTENT[0]), ] { stamp(&mut image, sprite, [left, y, right, bottom], [255; 3]); } if command_buttons { for (offset, icon, enabled) in [ (COMMAND_LEFT, &self.command_icon, true), ( COMMAND_LEFT + BUTTON_WIDTH, &self.manual_icon, row.manual_control, ), ] { stamp( &mut image, &self.command_frame[selected], [offset, y, offset + BUTTON_WIDTH + 1, bottom], [255; 3], ); stamp( &mut image, icon, [offset + 10, y + 2, offset + 25, y + 17], if enabled { [255; 3] } else { [128; 3] }, ); } } if let Some([x, atlas_y]) = building_glyph(row.class) { let glyph = atlas_region(&self.atlas, [x, atlas_y, 15, 15]) .ok_or("building glyph outside ui_menu")?; stamp( &mut image, &glyph, [12, y + 2, 27, y + 17], building_glyph_tint(row.size), ); } if let Some((width, rgba)) = row .durability_percent .and_then(|percent| durability_fill(percent, TEXT_RIGHT - text_left)) { let fill = RgbaImage { width: 1, height: 1, rgba8: rgba.to_vec(), }; stamp( &mut image, &fill, [text_left + 2, y + 3, text_left + width, bottom - 3], [255; 3], ); } text( &self.font, &mut image, row.label, [text_left, y, TEXT_RIGHT, bottom], if row.selected { [255; 3] } else { [128; 3] }, )?; } Ok(image) } } #[cfg(test)] mod tests { use super::*; #[test] fn building_list_hits_labels_icons_and_close_with_native_row_gaps() { assert_eq!(hit_test([350.0, 9.0], 4, false), Some(Hit::Close)); assert_eq!(hit_test([68.0, 39.0], 4, false), None); assert_eq!(hit_test([68.0, 40.0], 4, false), Some(Hit::Row(1))); assert_eq!(hit_test([340.0, 49.0], 4, false), Some(Hit::Row(1))); assert_eq!(hit_test([340.0, 49.0], 1, false), None); assert_eq!(hit_test([102.0, 25.0], 1, true), Some(Hit::Command(0))); assert_eq!(hit_test([102.0, 25.0], 1, false), Some(Hit::Row(0))); assert_eq!(hit_test([119.0, 25.0], 1, true), Some(Hit::Command(0))); assert_eq!(hit_test([120.0, 25.0], 1, true), Some(Hit::Manual(0))); assert_eq!(hit_test([155.0, 25.0], 1, true), Some(Hit::Row(0))); } }