aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/mission_help.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-02 01:44:35 +0300
committerValentin Popov <valentin@popov.link>2026-12-02 01:44:35 +0300
commitaaaa7aa0fd045de515079d21eeaf71e0815aa927 (patch)
treef666c08f6735c22ed46d4a403b4a174be46bee4e /apps/fparkan-game/src/mission_help.rs
parent905fea2fc8f35992769546aa15649200b3982c4d (diff)
downloadfparkan-aaaa7aa0fd045de515079d21eeaf71e0815aa927.tar.xz
fparkan-aaaa7aa0fd045de515079d21eeaf71e0815aa927.zip
feat(ui): open mission help and full message history with F1
Diffstat (limited to 'apps/fparkan-game/src/mission_help.rs')
-rw-r--r--apps/fparkan-game/src/mission_help.rs228
1 files changed, 228 insertions, 0 deletions
diff --git a/apps/fparkan-game/src/mission_help.rs b/apps/fparkan-game/src/mission_help.rs
new file mode 100644
index 00000000..78b0474e
--- /dev/null
+++ b/apps/fparkan-game/src/mission_help.rs
@@ -0,0 +1,228 @@
+//! Window and Vulkan integration for the in-mission help terminal.
+
+use super::{
+ append_environment_gradient_material, append_environment_range, environment_vertex,
+ quad_indices, screen_tl_quad_vertices_pixels, world_pipeline_state, DirectoryVfs,
+ EnvironmentGpuRange, Instant, LegacyBlendMode, LegacyDepthMode, StaticPreviewScene,
+ StaticVulkanApp, VulkanSmokeRenderer, VulkanStaticMesh, VulkanStaticTexture,
+};
+use fparkan_assets::help_screen::{HelpAction, HelpLabels, HelpScreen, EXTENT};
+use fparkan_platform_winit::text::StringResources;
+use std::path::Path;
+use winit::{
+ event::{ElementState, MouseButton, MouseScrollDelta, WindowEvent},
+ keyboard::{KeyCode, PhysicalKey},
+};
+
+pub(super) struct MissionHelp {
+ range: EnvironmentGpuRange,
+ screen: HelpScreen,
+ pause_before: Option<bool>,
+ dirty: bool,
+ cursor: [f32; 2],
+}
+
+impl MissionHelp {
+ pub(super) fn load(
+ root: &Path,
+ mission: &str,
+ preview: &mut StaticPreviewScene,
+ ) -> Result<Self, String> {
+ let strings = StringResources::open(&root.join("iron3d.dll"))?;
+ let read = |id| {
+ strings
+ .get(id)
+ .ok_or_else(|| format!("missing help string {id:#06x}"))
+ };
+ let labels = HelpLabels {
+ tabs: [read(0x13df)?, read(0x13e0)?, read(0x13e2)?],
+ controls: [read(0x1813)?, read(0x1811)?, read(0x1812)?, read(0x1814)?],
+ common_keys: read(0x1841)?,
+ game_menu_hint: read(0x1842)?,
+ empty_journal: read(0x1817)?,
+ message_number: read(0x1818)?,
+ };
+ let screen = HelpScreen::load(&DirectoryVfs::new(root), mission, labels)?;
+ 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 range = append_environment_range(
+ &mut preview.mesh,
+ material,
+ vec![environment_vertex([0.0; 3], [1.0; 3], [0.0; 2]); 4],
+ quad_indices(1)?,
+ world_pipeline_state(LegacyBlendMode::SourceAlpha, LegacyDepthMode::Disabled),
+ )?;
+ Ok(Self {
+ range,
+ screen,
+ pause_before: None,
+ dirty: true,
+ cursor: [f32::NAN; 2],
+ })
+ }
+
+ pub(super) fn post(&mut self, sender: &str, body: &str) -> Result<(), String> {
+ self.screen.post(sender, body)?;
+ self.dirty = true;
+ Ok(())
+ }
+
+ pub(super) fn range_index(&self) -> usize {
+ self.range.range_index
+ }
+
+ pub(super) fn draw(
+ &mut self,
+ renderer: &mut VulkanSmokeRenderer,
+ mesh: &mut VulkanStaticMesh,
+ viewport: [f32; 2],
+ ) -> Result<(), String> {
+ let visible = self.pause_before.is_some();
+ if visible {
+ if self.dirty {
+ let image = self.screen.paint()?;
+ renderer
+ .update_material_texture(self.range.material_index, 0, &texture(image.rgba8))
+ .map_err(|error| error.to_string())?;
+ self.dirty = false;
+ }
+ let half = viewport.map(|dimension| dimension * 0.5);
+ let vertices = screen_tl_quad_vertices_pixels(
+ half,
+ half[0],
+ half[1],
+ viewport,
+ [1.0; 3],
+ 1.0,
+ [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]],
+ );
+ mesh.vertices[self.range.vertex_start..self.range.vertex_start + vertices.len()]
+ .copy_from_slice(&vertices);
+ }
+ renderer
+ .set_draw_range_index_count(self.range.range_index, if visible { 6 } else { 0 })
+ .map_err(|error| error.to_string())
+ }
+}
+
+impl StaticVulkanApp {
+ pub(super) fn handle_mission_help_event(&mut self, event: &WindowEvent) -> bool {
+ if let WindowEvent::KeyboardInput { event, .. } = event {
+ if let PhysicalKey::Code(key) = event.physical_key {
+ return self.handle_help_key(key, event.state, event.repeat);
+ }
+ }
+ let Some(help) = &mut self.overlay.help else {
+ return false;
+ };
+ if let WindowEvent::CursorMoved { position, .. } = event {
+ if let Some(window) = &self.window {
+ let size = window.inner_size();
+ if size.width != 0 && size.height != 0 {
+ help.cursor = [
+ position.x as f32 * 640.0 / size.width as f32,
+ position.y as f32 * 480.0 / size.height as f32,
+ ];
+ }
+ }
+ }
+ if help.pause_before.is_none() {
+ return false;
+ }
+ let action = match event {
+ WindowEvent::MouseInput {
+ state: ElementState::Pressed,
+ button: MouseButton::Left,
+ ..
+ } => HelpScreen::hit_test(help.cursor),
+ WindowEvent::MouseWheel { delta, .. } => {
+ let y = match delta {
+ MouseScrollDelta::LineDelta(_, y) => *y,
+ MouseScrollDelta::PixelDelta(position) => position.y as f32,
+ };
+ (y != 0.0).then(|| HelpAction::Scroll(-2 * y.signum() as i32))
+ }
+ _ => None,
+ };
+ if let Some(action) = action {
+ self.navigate_mission_help(action);
+ }
+ matches!(
+ event,
+ WindowEvent::KeyboardInput { .. }
+ | WindowEvent::MouseInput { .. }
+ | WindowEvent::MouseWheel { .. }
+ | WindowEvent::CursorMoved { .. }
+ )
+ }
+
+ fn handle_help_key(&mut self, key: KeyCode, state: ElementState, repeat: bool) -> bool {
+ let Some(help) = &mut self.overlay.help else {
+ return false;
+ };
+ let open = help.pause_before.is_some();
+ if key == KeyCode::F1 {
+ if state == ElementState::Pressed && !repeat {
+ if open {
+ self.navigate_mission_help(HelpAction::Close);
+ } else if self.factory_menu.is_none() {
+ help.pause_before = Some(self.mission_paused);
+ self.player_fire_held = false;
+ self.player_fire_pressed = false;
+ self.pressed_keys.clear();
+ self.set_mission_paused_at(true, Instant::now());
+ self.schedule_next_redraw();
+ }
+ }
+ return true;
+ }
+ if !open {
+ return false;
+ }
+ if state == ElementState::Pressed {
+ let action = match key {
+ KeyCode::ArrowUp => Some(HelpAction::Scroll(-1)),
+ KeyCode::ArrowDown => Some(HelpAction::Scroll(1)),
+ KeyCode::Tab | KeyCode::ArrowRight | KeyCode::ArrowLeft => {
+ if help.screen.cycle(key == KeyCode::ArrowLeft) {
+ help.dirty = true;
+ self.schedule_next_redraw();
+ }
+ None
+ }
+ _ => None,
+ };
+ if let Some(action) = action {
+ self.navigate_mission_help(action);
+ }
+ }
+ true
+ }
+
+ fn navigate_mission_help(&mut self, action: HelpAction) {
+ let Some(help) = &mut self.overlay.help else {
+ return;
+ };
+ if action == HelpAction::Close {
+ if let Some(paused) = help.pause_before.take() {
+ self.set_mission_paused_at(paused, Instant::now());
+ self.schedule_next_redraw();
+ }
+ } else if help.screen.navigate(action) {
+ help.dirty = true;
+ self.schedule_next_redraw();
+ }
+ }
+}
+
+fn texture(rgba8: Vec<u8>) -> VulkanStaticTexture {
+ VulkanStaticTexture {
+ width: EXTENT[0],
+ height: EXTENT[1],
+ rgba8,
+ mip_levels: Vec::new(),
+ }
+}