diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 04:53:53 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 04:53:53 +0300 |
| commit | 8e11cede8106e36aa1b91400318cff353c157986 (patch) | |
| tree | fb905d43a5500af6934821228f05badecbf2b9bc /crates | |
| parent | 6c2545e8b548a8b892ef2a3adaba1a138ab623c9 (diff) | |
| download | fparkan-8e11cede8106e36aa1b91400318cff353c157986.tar.xz fparkan-8e11cede8106e36aa1b91400318cff353c157986.zip | |
feat(ui): open and transfer recent factory designs
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/designer_screen.rs | 251 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/factory_projects.rs | 33 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/ui_sprite.rs | 47 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 4 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/production.rs | 15 |
6 files changed, 320 insertions, 31 deletions
diff --git a/crates/fparkan-assets/src/designer_screen.rs b/crates/fparkan-assets/src/designer_screen.rs new file mode 100644 index 00000000..fea9eef9 --- /dev/null +++ b/crates/fparkan-assets/src/designer_screen.rs @@ -0,0 +1,251 @@ +//! Project selection and the installed parts of the selected Unit DAT. + +use crate::{ + factory_projects::PROJECT_SLOTS, + game_font::{load_game_font, BitmapFont}, + mission_pager::{copy_sprite, scale, UiFrame}, + object_descriptions::ObjectDescriptions, + ui_sprite::{atlas_region, load_ui_page}, + RgbaImage, +}; +use encoding_rs::WINDOWS_1251; +use fparkan_prototype::{unit_component_tree, UnitComponentRecord}; +use fparkan_vfs::Vfs; + +const SEND: [u32; 4] = [194, 437, 229, 480]; +const EXIT: [u32; 4] = [405, 437, 446, 480]; +/// Extent of the designer's original font profile. +pub const EXTENT: [u32; 2] = [800, 600]; +/// Number of part rows in the project viewport. +pub const PART_ROWS: usize = 18; + +/// Controls in logical 640×480 coordinates. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum DesignerAction { + /// Restore a recent project in the designer. + Project(usize), + /// Assign the currently open project to the factory. + Send, + /// Return to the factory panel. + Exit, +} + +/// Original strings that label the currently supported designer actions. +pub struct DesignerLabels { + /// Constructor title. + pub title: String, + /// Recent project list. + pub recent: String, + /// Transfer to the connected factory. + pub send: String, + /// Close the designer. + pub exit: String, +} + +/// Original UI resources required by the project view of the designer. +pub struct DesignerScreen { + font: BitmapFont, + frame: UiFrame, + descriptions: ObjectDescriptions, + recent: RgbaImage, + send: RgbaImage, + send_frame: RgbaImage, + exit: RgbaImage, + exit_frame: RgbaImage, + labels: DesignerLabels, +} + +/// Selection and scroll state to compose on the screen. +pub struct DesignerPicture<'a> { + /// Number of populated history slots. + pub count: usize, + /// Previewed project name and parts, when a slot has been opened. + pub project: Option<(usize, &'a str, &'a [UnitComponentRecord])>, + /// First component row in the pane. + pub first_part: usize, + /// Action whose original tooltip is shown. + pub hover: Option<DesignerAction>, +} + +impl DesignerScreen { + /// Reads the original sheets, display font and prototype descriptions. + pub fn load(vfs: &dyn Vfs, labels: DesignerLabels) -> Result<Self, String> { + let icons = load_ui_page(vfs, "icons")?; + let frames = load_ui_page(vfs, "page4")?; + let sprite = |page: &RgbaImage, [x, y, width, height]: [u32; 4]| { + atlas_region(page, [x, y, width, height]) + .ok_or_else(|| format!("designer sprite outside its page at {x},{y}")) + }; + Ok(Self { + font: load_game_font(vfs)?, + frame: UiFrame::load(vfs)?, + descriptions: ObjectDescriptions::load(vfs)?, + recent: sprite(&icons, [216, 96, 24, 24])?, + send: sprite(&icons, [176, 231, 24, 24])?, + send_frame: sprite(&frames, [84, 169, 35, 43])?, + exit: sprite(&icons, [192, 72, 24, 24])?, + exit_frame: sprite(&frames, [141, 213, 41, 43])?, + labels, + }) + } + + /// Paints the active project and every installed mount in Unit DAT order. + pub fn paint(&self, view: DesignerPicture<'_>) -> Result<RgbaImage, String> { + let [width, height] = EXTENT; + let mut image = RgbaImage { + width, + height, + rgba8: vec![0; (width * height * 4) as usize], + }; + for pixel in image.rgba8.chunks_exact_mut(4) { + pixel[3] = 255; + } + self.frame + .paint(&mut image, [185, 2, 455, 480], [0, 48, 0, 255]); + self.text(&mut image, &self.labels.title, [194, 10, 446, 26], [255; 3])?; + self.text( + &mut image, + &self.labels.recent, + [199, 250, 350, 268], + [100, 255, 100], + )?; + let selected = view.project.map(|(index, _, _)| index); + for index in 0..view.count.min(PROJECT_SLOTS) { + let tint = if selected == Some(index) { + [255; 3] + } else { + [160; 3] + }; + sprite(&mut image, &self.recent, recent_slot(index), tint); + } + if let Some((_, name, records)) = view.project { + self.text(&mut image, name, [194, 27, 446, 42], [255, 255, 100])?; + self.paint_parts(&mut image, records, view.first_part)?; + sprite(&mut image, &self.send_frame, SEND, [255; 3]); + sprite(&mut image, &self.send, [201, 450, 225, 474], [255; 3]); + } + sprite(&mut image, &self.exit_frame, EXIT, [255; 3]); + sprite(&mut image, &self.exit, [413, 450, 437, 474], [255; 3]); + let hint = match view.hover { + Some(DesignerAction::Send) => &self.labels.send, + Some(DesignerAction::Exit) => &self.labels.exit, + _ => return Ok(image), + }; + self.text(&mut image, hint, [194, 418, 446, 436], [255; 3])?; + Ok(image) + } + + fn paint_parts( + &self, + image: &mut RgbaImage, + records: &[UnitComponentRecord], + first: usize, + ) -> Result<(), String> { + let tree = unit_component_tree(records).map_err(|error| error.to_string())?; + let mut depth = Vec::with_capacity(tree.len()); + for node in tree { + depth.push(node.parent_index.map_or(0_u32, |parent| depth[parent] + 1)); + } + for (row, record) in records.iter().skip(first).take(PART_ROWS).enumerate() { + let key = field(&record.resource_raw); + let description = self + .descriptions + .short_name(key) + .filter(|name| !name.is_empty()) + .unwrap_or_else(|| field(&record.description_raw)); + let text = if description.is_empty() { + key + } else { + description + }; + let [width, glyph_height] = self.font.line_metrics_bytes(text)?; + if width == 0 { + continue; + } + let left = scale(196 + depth[first + row].min(8) * 7); + let top = scale(46 + 10 * row as u32); + let width = width.min((scale(446) - left) as u16); + let glyphs = self.font.centered_line_bytes(text, width, glyph_height)?; + copy_sprite( + image, + &glyphs, + [ + left, + top, + left + u32::from(width), + top + u32::from(glyph_height), + ], + [false; 2], + [255; 3], + ); + } + Ok(()) + } + + fn text( + &self, + image: &mut RgbaImage, + text: &str, + bounds: [u32; 4], + tint: [u8; 3], + ) -> Result<(), String> { + let [l, t, r, b] = bounds.map(scale); + let (bytes, _, _) = WINDOWS_1251.encode(text); + let labels = self + .font + .centered_line_bytes(&bytes, (r - l) as u16, (b - t) as u16)?; + copy_sprite(image, &labels, [l, t, r, b], [false; 2], tint); + Ok(()) + } +} + +/// Maps pointer input to a populated history slot or available command. +pub fn hit_test([x, y]: [f32; 2], count: usize, has_project: bool) -> Option<DesignerAction> { + 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 hit(EXIT) { + return Some(DesignerAction::Exit); + } + if has_project && hit(SEND) { + return Some(DesignerAction::Send); + } + (0..count.min(PROJECT_SLOTS)) + .find(|&index| hit(recent_slot(index))) + .map(DesignerAction::Project) +} + +fn recent_slot(index: usize) -> [u32; 4] { + let left = 200 + 30 * index as u32; + [left, 270, left + 24, 294] +} + +fn field(raw: &[u8; 32]) -> &[u8] { + raw.split(|&byte| byte == 0).next().unwrap_or_default() +} + +fn sprite(image: &mut RgbaImage, source: &RgbaImage, rect: [u32; 4], tint: [u8; 3]) { + copy_sprite(image, source, rect.map(scale), [false; 2], tint); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn project_picker_uses_populated_slots_and_needs_a_project_to_send() { + assert_eq!( + hit_test([240.0, 278.0], 2, false), + Some(DesignerAction::Project(1)) + ); + assert_eq!(hit_test([240.0, 278.0], 1, false), None); + assert_eq!(hit_test([201.0, 455.0], 2, false), None); + assert_eq!( + hit_test([201.0, 455.0], 2, true), + Some(DesignerAction::Send) + ); + assert_eq!( + hit_test([420.0, 455.0], 0, false), + Some(DesignerAction::Exit) + ); + } +} diff --git a/crates/fparkan-assets/src/factory_projects.rs b/crates/fparkan-assets/src/factory_projects.rs index 94973b22..ca77137a 100644 --- a/crates/fparkan-assets/src/factory_projects.rs +++ b/crates/fparkan-assets/src/factory_projects.rs @@ -16,6 +16,7 @@ pub const ORIGIN: [u32; 2] = [50, 122]; // 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)] @@ -24,6 +25,8 @@ pub enum FactoryProjectAction { 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. @@ -32,13 +35,14 @@ pub struct FactoryProjectImages { 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<Self, String> { - let [idle, pressed, project, build_icon] = load_ui_sprites( + let [idle, pressed, project, build_icon, constructor_icon] = load_ui_sprites( vfs, "ui/hq.cfg", [ @@ -46,6 +50,7 @@ impl FactoryProjectImages { "short_button_frame_on", "project_icon", "build_icon", + "constructor_icon", ], )?; let [build_frame] = load_ui_sprites(vfs, "ui/compaund.cfg", ["ccres_long_button_normal"])?; @@ -53,6 +58,7 @@ impl FactoryProjectImages { frames: [idle, pressed], project, build_icon, + constructor_icon, build_frame, font: load_layout_font(vfs)?, }) @@ -65,6 +71,7 @@ impl FactoryProjectImages { selected: usize, pressed: Option<usize>, name: &str, + designer_available: bool, ) -> Result<RgbaImage, String> { let mut image = RgbaImage { width: EXTENT[0], @@ -92,18 +99,25 @@ impl FactoryProjectImages { }, ); } - copy_sprite(&mut image, &self.build_frame, BUILD, [false; 2], [255; 3]); - let [left, top, _, _] = BUILD; + 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( - &mut image, - &self.build_icon, + image, + 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) } } @@ -118,6 +132,9 @@ pub fn hit_test(position: [f32; 2], count: usize) -> Option<FactoryProjectAction 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) diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 18da6ace..cb878857 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -34,6 +34,7 @@ mod behavior_profile_values; mod chassis_profile; mod control_drive; mod control_weapon_sound; +pub mod designer_screen; mod explosion_profile; pub mod factory_projects; pub mod game_font; diff --git a/crates/fparkan-assets/src/ui_sprite.rs b/crates/fparkan-assets/src/ui_sprite.rs index 812f1e6c..78843a25 100644 --- a/crates/fparkan-assets/src/ui_sprite.rs +++ b/crates/fparkan-assets/src/ui_sprite.rs @@ -42,27 +42,8 @@ pub fn load_ui_sprites<const N: usize>( let [x, y, width, height] = ["OFFSET_X", "OFFSET_Y", "WIDTH", "HEIGHT"].map(|key| number(®ion, key)); let [x, y, width, height] = [x?, y?, width?, height?]; - if width == 0 - || height == 0 - || x.checked_add(width).is_none_or(|right| right > image.width) - || y.checked_add(height) - .is_none_or(|bottom| bottom > image.height) - { - return Err(format!("UI sprite {name} is outside atlas page {page}")); - } - let rgba8 = (y..y + height) - .flat_map(|row| { - let start = (row as usize * image.width as usize + x as usize) * 4; - image.rgba8[start..start + width as usize * 4] - .iter() - .copied() - }) - .collect(); - let image = RgbaImage { - width, - height, - rgba8, - }; + let image = atlas_region(image, [x, y, width, height]) + .ok_or_else(|| format!("UI sprite {name} is outside atlas page {page}"))?; let angle = if region.contains_key("ROTATE") { number(®ion, "ROTATE")? } else { @@ -75,6 +56,30 @@ pub fn load_ui_sprites<const N: usize>( .expect("one rectangle per UI sprite name")) } +pub(crate) fn atlas_region( + image: &RgbaImage, + [x, y, width, height]: [u32; 4], +) -> Option<RgbaImage> { + let right = x.checked_add(width)?; + let bottom = y.checked_add(height)?; + if width == 0 || height == 0 || right > image.width || bottom > image.height { + return None; + } + let rgba8 = (y..bottom) + .flat_map(|row| { + let start = (row as usize * image.width as usize + x as usize) * 4; + image.rgba8[start..start + width as usize * 4] + .iter() + .copied() + }) + .collect(); + Some(RgbaImage { + width, + height, + rgba8, + }) +} + fn rotate(image: RgbaImage, angle: u32) -> Result<RgbaImage, String> { let (w, h) = (image.width, image.height); let (width, height) = match angle { diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index c5794e55..b5527a4a 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -197,8 +197,8 @@ pub use player_weapon::{ WeaponIndicator, WeaponLamp, }; pub use production::{ - clan_factory_schemes, factory_schemes, order_clan_factory_scheme, order_factory_robot, - order_factory_scheme, + clan_factory_schemes, factory_project_components, factory_schemes, order_clan_factory_scheme, + order_factory_robot, order_factory_scheme, }; pub use projectile::{ apply_projectile_contact, apply_projectile_hit, apply_projectile_impact, diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index 3d3eb159..9f6846f8 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -387,6 +387,21 @@ pub fn clan_factory_schemes( .collect()) } +/// Components of a prepared player project, in the original Unit DAT order. +pub fn factory_project_components( + engine: &Engine, + scheme: usize, +) -> Option<&[UnitComponentRecord]> { + let (mission, production) = loaded_production(engine).ok()?; + if scheme >= production.player_scheme_count { + return None; + } + mission + .object_drafts + .get(production.reserve_start + scheme * RESERVE_SLOTS) + .map(|draft| draft.unit_components.as_slice()) +} + fn schemes_for_plant<'a>( mission: &'a LoadedMissionState, production: &'a Production, |
