diff options
Diffstat (limited to 'crates/fparkan-assets/src/designer_screen.rs')
| -rw-r--r-- | crates/fparkan-assets/src/designer_screen.rs | 100 |
1 files changed, 82 insertions, 18 deletions
diff --git a/crates/fparkan-assets/src/designer_screen.rs b/crates/fparkan-assets/src/designer_screen.rs index bdbdeda2..2aa09e32 100644 --- a/crates/fparkan-assets/src/designer_screen.rs +++ b/crates/fparkan-assets/src/designer_screen.rs @@ -14,6 +14,7 @@ use fparkan_prototype::UnitComponentRecord; use fparkan_vfs::Vfs; pub use crate::designer_categories::{DesignerCategory, DesignerPartsPane}; +pub use crate::designer_sockets::{designer_sockets, DesignerSocket}; const SEND: [u32; 4] = [194, 437, 229, 480]; const EXIT: [u32; 4] = [405, 437, 446, 480]; @@ -38,6 +39,8 @@ pub enum DesignerAction { Category(DesignerCategory), /// Choose the installed mount to inspect. Part(usize), + /// Choose a named mount, including a socket without an installed part. + Socket(usize), /// Install a researched variant at the inspected mount. Variant(usize), /// Move a list by one row. @@ -116,7 +119,9 @@ pub struct DesignerPicture<'a> { /// Installed part category displayed in the model pane. pub category: DesignerCategory, /// Selected record and the available equipment for it. - pub variants: Option<(usize, &'a [String])>, + pub variants: Option<(DesignerAction, &'a [String])>, + /// External model mounts, including empty ones. + pub sockets: &'a [DesignerSocket], /// Action whose original tooltip is shown. pub hover: Option<DesignerAction>, /// Active project file control. @@ -308,7 +313,7 @@ impl DesignerScreen { for (index, category) in DesignerCategory::ALL.into_iter().enumerate() { let enabled = match pane { Parts => category == view.category && !variants.is_empty(), - Model => category.mounts(records).next().is_some(), + Model => model_rows(category, records, view.sockets).next().is_some(), }; let selected = enabled && category == view.category; let (frame, icon, _) = category.tab(); @@ -333,27 +338,29 @@ impl DesignerScreen { ); } } - let mounts = view.category.mounts(records).collect::<Vec<_>>(); - for (row, index) in mounts + let mounts = model_rows(view.category, records, view.sockets).collect::<Vec<_>>(); + for (row, &mount) in mounts .iter() .skip(view.first_part) .take(PART_ROWS) .enumerate() { - let record = &records[*index]; - let key = field(&record.resource_raw); - let label = self.part_label(key, field(&record.description_raw)); + let label = mounted_record(mount, view.sockets) + .and_then(|index| records.get(index)) + .map_or(b"".as_slice(), |record| { + self.part_label(field(&record.resource_raw), field(&record.description_raw)) + }); self.paint_row( image, label, Model, row, - view.variants.is_some_and(|(mount, _)| mount == *index), + view.variants.is_some_and(|(selected, _)| selected == mount), )?; } let installed = view .variants - .and_then(|(mount, _)| records.get(mount)) + .and_then(|(mount, _)| records.get(mounted_record(mount, view.sockets)?)) .map(|part| field(&part.resource_raw)); for (row, name) in variants .iter() @@ -515,15 +522,16 @@ pub fn equipment_hit( records: &[UnitComponentRecord], first_variant: usize, variants: usize, + sockets: &[DesignerSocket], ) -> Option<DesignerAction> { use DesignerPartsPane::{Model, Parts}; - if let Some(category) = category_hit(cursor, records) { + if let Some(category) = category_hit(cursor, records, sockets) { return Some(DesignerAction::Category(category)); } let pane = DesignerPartsPane::at(cursor)?; let count = match pane { Parts => variants, - Model => category.mounts(records).count(), + Model => model_rows(category, records, sockets).count(), }; if count > PART_ROWS { for forward in [false, true] { @@ -537,15 +545,16 @@ pub fn equipment_hit( Parts => { (first_variant + row < variants).then_some(DesignerAction::Variant(first_variant + row)) } - Model => category - .mounts(records) - .nth(first_part + row) - .map(DesignerAction::Part), + Model => model_rows(category, records, sockets).nth(first_part + row), } } /// Finds an enabled category tab in the model panel. -pub fn category_hit(cursor: [f32; 2], records: &[UnitComponentRecord]) -> Option<DesignerCategory> { +pub fn category_hit( + cursor: [f32; 2], + records: &[UnitComponentRecord], + sockets: &[DesignerSocket], +) -> Option<DesignerCategory> { DesignerCategory::ALL.into_iter().find(|&category| { let (left, _, _) = category.tab(); let left = left + DesignerPartsPane::Model.left(); @@ -555,10 +564,36 @@ pub fn category_hit(cursor: [f32; 2], records: &[UnitComponentRecord]) -> Option DesignerCategory::Chassis | DesignerCategory::Armour )); contains([left, 16, left + hit_width, 53], cursor) - && category.mounts(records).next().is_some() + && model_rows(category, records, sockets).next().is_some() }) } +/// Rows of one equipment category in native mount order. +pub fn model_rows<'a>( + category: DesignerCategory, + records: &'a [UnitComponentRecord], + sockets: &'a [DesignerSocket], +) -> impl Iterator<Item = DesignerAction> + 'a { + sockets + .iter() + .enumerate() + .filter_map(move |(index, socket)| { + (socket.category == category).then_some(DesignerAction::Socket(index)) + }) + .chain(category.mounts(records).filter_map(|part| { + (!sockets.iter().any(|socket| socket.part == Some(part))) + .then_some(DesignerAction::Part(part)) + })) +} + +fn mounted_record(mount: DesignerAction, sockets: &[DesignerSocket]) -> Option<usize> { + match mount { + DesignerAction::Part(part) => Some(part), + DesignerAction::Socket(socket) => sockets.get(socket)?.part, + _ => None, + } +} + fn recent_slot(index: usize) -> [u32; 4] { let left = 200 + 30 * index as u32; [left, 270, left + 24, 294] @@ -606,7 +641,8 @@ mod tests { immediate_child_count: 0, }; let records = [0, 3, 4, 3, 4, 3, 3, 3, 3, 3].map(make_part); - let action = |cursor| equipment_hit(cursor, DesignerCategory::Systems, 1, &records, 2, 8); + let action = + |cursor| equipment_hit(cursor, DesignerCategory::Systems, 1, &records, 2, 8, &[]); assert_eq!( action([491.0, 24.0]), Some(DesignerAction::Category(DesignerCategory::Chassis)) @@ -627,4 +663,32 @@ mod tests { [2, 4] ); } + + #[test] + fn an_empty_socket_enables_its_category_and_has_its_own_row() { + let sockets = [ + DesignerSocket { + category: DesignerCategory::Weapon, + parent: 1, + node: 6, + family: "universal_lr".into(), + part: None, + }, + DesignerSocket { + category: DesignerCategory::Weapon, + parent: 1, + node: 7, + family: "universal_lr".into(), + part: None, + }, + ]; + let action = + |cursor| equipment_hit(cursor, DesignerCategory::Weapon, 0, &[], 0, 0, &sockets); + assert_eq!( + action([526.0, 28.0]), + Some(DesignerAction::Category(DesignerCategory::Weapon)) + ); + assert_eq!(action([521.0, 73.0]), Some(DesignerAction::Socket(0))); + assert_eq!(action([521.0, 96.0]), Some(DesignerAction::Socket(1))); + } } |
