From 8e11cede8106e36aa1b91400318cff353c157986 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 05:53:53 +0400 Subject: feat(ui): open and transfer recent factory designs --- apps/fparkan-game/src/factory_menu.rs | 254 +++++++++++++++++++++++++++++----- 1 file changed, 222 insertions(+), 32 deletions(-) (limited to 'apps/fparkan-game/src/factory_menu.rs') diff --git a/apps/fparkan-game/src/factory_menu.rs b/apps/fparkan-game/src/factory_menu.rs index 8f307cfc..743a438a 100644 --- a/apps/fparkan-game/src/factory_menu.rs +++ b/apps/fparkan-game/src/factory_menu.rs @@ -1,16 +1,20 @@ +#[cfg(windows)] +use super::robot_designer::RobotDesignerPanel; use super::{ append_environment_gradient_material, append_environment_range, quad_indices, screen_tl_quad_vertices_pixels, world_pipeline_state, DirectoryVfs, EnvironmentGpuRange, LegacyBlendMode, LegacyDepthMode, StaticPreviewScene, StaticVulkanApp, VulkanSmokeRenderer, VulkanStaticTexture, }; +use fparkan_assets::designer_screen::{self, DesignerAction, PART_ROWS}; use fparkan_assets::factory_projects::{ hit_test, FactoryProjectAction, FactoryProjectImages, EXTENT, ORIGIN, PROJECT_SLOTS, }; +use fparkan_runtime::{factory_project_components, Engine}; use fparkan_world::ObjectHandle; use std::{ops::Range, path::Path}; use winit::{ - event::{ElementState, MouseButton, WindowEvent}, + event::{ElementState, MouseButton, MouseScrollDelta, WindowEvent}, keyboard::KeyCode, }; @@ -22,10 +26,27 @@ pub(super) struct FactoryChoice { } pub(super) struct FactoryMenu { - choices: Vec, + pub(super) choices: Vec, selected: usize, cursor: [f32; 2], pressed: Option, + pub(super) designer: Option, +} + +pub(super) struct DesignerSession { + pub(super) slots: Range, + pub(super) selected: Option, + pub(super) first_part: usize, + pub(super) hover: Option, + pub(super) dirty: bool, +} + +impl DesignerSession { + fn point(&mut self, cursor: [f32; 2]) { + let next = designer_screen::hit_test(cursor, self.slots.len(), self.selected.is_some()); + self.dirty |= next != self.hover; + self.hover = next; + } } impl FactoryMenu { @@ -36,6 +57,7 @@ impl FactoryMenu { selected: 0, cursor: [f32::NAN; 2], pressed: None, + designer: None, } } @@ -64,6 +86,39 @@ impl FactoryMenu { start..start + count } + fn open_designer(&mut self) { + let mut designer = DesignerSession { + slots: self.visible_slots(), + selected: None, + first_part: 0, + hover: None, + dirty: true, + }; + designer.point(self.cursor); + self.designer = Some(designer); + } + + fn designer_action(&mut self, action: DesignerAction) { + let Some(designer) = &mut self.designer else { + return; + }; + match action { + DesignerAction::Project(slot) if slot < designer.slots.len() => { + designer.selected = Some(slot); + designer.first_part = 0; + designer.dirty = true; + } + DesignerAction::Send => { + if let Some(slot) = designer.selected { + self.selected = designer.slots.start + slot; + self.designer = None; + } + } + DesignerAction::Exit => self.designer = None, + _ => {} + } + } + pub(super) fn title(&self) -> String { let choice = self.selected(); let factory = choice.factory.map_or_else( @@ -83,6 +138,8 @@ pub(super) struct FactoryProjectPanel { images: FactoryProjectImages, range: EnvironmentGpuRange, displayed: Option, + #[cfg(windows)] + designer: Option, } #[derive(PartialEq, Eq)] @@ -96,45 +153,64 @@ struct ProjectPicture { impl FactoryProjectPanel { pub(super) fn load(root: &Path, preview: &mut StaticPreviewScene) -> Result { let images = FactoryProjectImages::load(&DirectoryVfs::new(root))?; - let material = append_environment_gradient_material(&mut preview.materials)?; - preview.materials[material].screen_tl = true; - preview.materials[material].sky_far_depth = false; - preview.materials[material].texture = - texture(vec![0; (EXTENT[0] * EXTENT[1] * 4) as usize]); - let half = EXTENT.map(|dimension| dimension as f32 * 0.5); - let vertices = screen_tl_quad_vertices_pixels( - [ORIGIN[0] as f32 + half[0], ORIGIN[1] as f32 + half[1]], - half[0], - half[1], - [640.0, 480.0], - [1.0; 3], - 1.0, - [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]], - ); - let range = append_environment_range( - &mut preview.mesh, - material, - vertices.to_vec(), - quad_indices(1)?, - world_pipeline_state(LegacyBlendMode::SourceAlpha, LegacyDepthMode::Disabled), + let range = append_picture( + preview, + EXTENT, + [ + ORIGIN[0] as f32, + ORIGIN[1] as f32, + EXTENT[0] as f32, + EXTENT[1] as f32, + ], )?; + #[cfg(windows)] + let designer = RobotDesignerPanel::load(root, preview) + .map(Some) + .unwrap_or_else(|error| { + eprintln!("robot_designer_unavailable reason={error}"); + None + }); Ok(Self { images, range, displayed: None, + #[cfg(windows)] + designer, }) } - pub(super) fn range_index(&self) -> usize { - self.range.range_index + pub(super) fn designer_available(&self) -> bool { + #[cfg(windows)] + { + self.designer.is_some() + } + #[cfg(not(windows))] + { + false + } + } + + pub(super) fn ranges(&self) -> impl Iterator { + #[cfg(windows)] + let designer = self.designer.as_ref().map(RobotDesignerPanel::range_index); + #[cfg(not(windows))] + let designer = None; + std::iter::once(self.range.range_index).chain(designer) } pub(super) fn draw( &mut self, renderer: &mut VulkanSmokeRenderer, - menu: Option<&mut FactoryMenu>, + mut menu: Option<&mut FactoryMenu>, + engine: Option<&Engine>, ) -> Result<(), String> { - if let Some(menu) = menu { + #[cfg(windows)] + if let Some(designer) = &mut self.designer { + designer.draw(renderer, menu.as_deref_mut(), engine)?; + } + #[cfg(not(windows))] + let _ = (&mut menu, engine); + if let Some(menu) = menu.filter(|menu| menu.designer.is_none()) { let slots = menu.visible_slots(); let count = slots.len(); let selected = menu.selected - slots.start; @@ -147,9 +223,15 @@ impl FactoryProjectPanel { && picture.name == *name }); if !unchanged { - let image = self.images.paint(count, selected, pressed, name)?; + let image = + self.images + .paint(count, selected, pressed, name, self.designer_available())?; renderer - .update_material_texture(self.range.material_index, 0, &texture(image.rgba8)) + .update_material_texture( + self.range.material_index, + 0, + &picture_texture(EXTENT, image.rgba8), + ) .map_err(|error| error.to_string())?; self.displayed = Some(ProjectPicture { count, @@ -180,6 +262,17 @@ impl StaticVulkanApp { return false; }; if state == ElementState::Pressed && !repeat && !self.mission_paused { + if menu.designer.is_some() { + match code { + KeyCode::Escape => menu.designer_action(DesignerAction::Exit), + KeyCode::Enter | KeyCode::NumpadEnter => { + menu.designer_action(DesignerAction::Send) + } + _ => {} + } + self.update_mission_title(); + return true; + } match code { KeyCode::ArrowUp => menu.navigate(true), KeyCode::ArrowDown => menu.navigate(false), @@ -198,6 +291,11 @@ impl StaticVulkanApp { } pub(super) fn handle_factory_pointer(&mut self, event: &WindowEvent) -> bool { + let designer_available = self + .overlay + .factory_projects + .as_ref() + .is_some_and(FactoryProjectPanel::designer_available); let Some(menu) = &mut self.factory_menu else { return false; }; @@ -210,19 +308,54 @@ impl StaticVulkanApp { position.x as f32 * 640.0 / size.width as f32, position.y as f32 * 480.0 / size.height as f32, ]; + if let Some(designer) = &mut menu.designer { + designer.point(menu.cursor); + } } } true } WindowEvent::CursorLeft { .. } | WindowEvent::Focused(false) => { menu.cursor = [f32::NAN; 2]; + if let Some(designer) = &mut menu.designer { + designer.point(menu.cursor); + } false } + WindowEvent::MouseWheel { delta, .. } if menu.designer.is_some() => { + let designer = menu.designer.as_mut().expect("the designer is open"); + if let Some(parts) = designer.selected.and_then(|slot| { + let choice = &menu.choices[designer.slots.start + slot]; + factory_project_components(self.mission_engine.as_ref()?, choice.scheme) + }) { + let y = match delta { + MouseScrollDelta::LineDelta(_, y) => *y, + MouseScrollDelta::PixelDelta(point) => point.y as f32, + }; + if y == 0.0 { + return true; + } + let first_part = designer + .first_part + .saturating_add_signed(-y.signum() as isize) + .min(parts.len().saturating_sub(PART_ROWS)); + designer.dirty |= designer.first_part != first_part; + designer.first_part = first_part; + } + true + } WindowEvent::MouseInput { state, button, .. } => { if *state == ElementState::Pressed && *button == MouseButton::Left && !self.mission_paused { + if let Some(designer) = &menu.designer { + if let Some(action) = designer.hover { + menu.designer_action(action); + self.update_mission_title(); + } + return true; + } let slots = menu.visible_slots(); match hit_test(menu.cursor, slots.len()) { Some(FactoryProjectAction::Select(index)) => { @@ -231,6 +364,10 @@ impl StaticVulkanApp { self.update_mission_title(); } Some(FactoryProjectAction::Build) => self.player_build_pressed = true, + Some(FactoryProjectAction::Designer) if designer_available => { + menu.open_designer() + } + Some(FactoryProjectAction::Designer) => {} None => {} } } @@ -241,10 +378,38 @@ impl StaticVulkanApp { } } -fn texture(rgba8: Vec) -> VulkanStaticTexture { +pub(super) fn append_picture( + preview: &mut StaticPreviewScene, + extent: [u32; 2], + [x, y, width, height]: [f32; 4], +) -> Result { + let material = append_environment_gradient_material(&mut preview.materials)?; + preview.materials[material].screen_tl = true; + preview.materials[material].sky_far_depth = false; + preview.materials[material].texture = + picture_texture(extent, vec![0; (extent[0] * extent[1] * 4) as usize]); + let vertices = screen_tl_quad_vertices_pixels( + [x + width * 0.5, y + height * 0.5], + width * 0.5, + height * 0.5, + [640.0, 480.0], + [1.0; 3], + 1.0, + [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]], + ); + append_environment_range( + &mut preview.mesh, + material, + vertices.to_vec(), + quad_indices(1)?, + world_pipeline_state(LegacyBlendMode::SourceAlpha, LegacyDepthMode::Disabled), + ) +} + +pub(super) fn picture_texture([width, height]: [u32; 2], rgba8: Vec) -> VulkanStaticTexture { VulkanStaticTexture { - width: EXTENT[0], - height: EXTENT[1], + width, + height, rgba8, mip_levels: Vec::new(), } @@ -273,4 +438,29 @@ mod tests { menu.navigate(false); assert_eq!(menu.visible_slots(), 6..8); } + + #[test] + fn previewing_a_project_changes_the_factory_selection_only_on_transfer() { + let mut menu = FactoryMenu::new( + (0..2) + .map(|scheme| FactoryChoice { + factory: None, + scheme, + name: format!("{scheme}.dat"), + }) + .collect(), + ); + menu.open_designer(); + menu.designer_action(DesignerAction::Send); + assert!(menu.designer.is_some()); + menu.designer_action(DesignerAction::Project(1)); + menu.designer_action(DesignerAction::Exit); + assert_eq!(menu.selected().scheme, 0); + + menu.open_designer(); + menu.designer_action(DesignerAction::Project(1)); + menu.designer_action(DesignerAction::Send); + assert!(menu.designer.is_none()); + assert_eq!(menu.selected().scheme, 1); + } } -- cgit v1.2.3