aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/strategy_cursor_hud.rs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/fparkan-game/src/strategy_cursor_hud.rs')
-rw-r--r--apps/fparkan-game/src/strategy_cursor_hud.rs107
1 files changed, 107 insertions, 0 deletions
diff --git a/apps/fparkan-game/src/strategy_cursor_hud.rs b/apps/fparkan-game/src/strategy_cursor_hud.rs
new file mode 100644
index 00000000..d64217a5
--- /dev/null
+++ b/apps/fparkan-game/src/strategy_cursor_hud.rs
@@ -0,0 +1,107 @@
+use super::{
+ mission_overlay::add_overlay_sprite, screen_tl_quad_vertices_pixels, DirectoryVfs,
+ EnvironmentGpuRange, StaticPreviewScene, VulkanSmokeRenderer, VulkanStaticMesh,
+};
+use fparkan_assets::strategy_cursor::{self, CursorStrip, StrategyCursor, PHASES};
+use std::path::Path;
+use winit::window::Window;
+
+pub(super) struct StrategyCursorHud {
+ strips: [CursorDraw; 3],
+ visible: bool,
+}
+
+struct CursorDraw {
+ range: EnvironmentGpuRange,
+ size: [f32; 2],
+ hotspot: [f32; 2],
+ delay_ms: u32,
+ phase: u32,
+ last_change_ms: u32,
+}
+
+impl StrategyCursorHud {
+ pub(super) fn load(root: &Path, preview: &mut StaticPreviewScene) -> Result<Self, String> {
+ let [arrow, place, wrong_place] = strategy_cursor::load(&DirectoryVfs::new(root))?;
+ let mut load = |strip: CursorStrip| {
+ let size = [
+ (strip.image.width / PHASES) as f32,
+ strip.image.height as f32,
+ ];
+ Ok::<_, String>(CursorDraw {
+ range: add_overlay_sprite(
+ preview,
+ strip.image,
+ [0.0, 0.0, size[0], size[1]],
+ [1.0; 3],
+ 1.0,
+ )?,
+ size,
+ hotspot: strip.hotspot.map(|axis| axis as f32),
+ delay_ms: strip.phase_delay_ms,
+ phase: 0,
+ last_change_ms: 0,
+ })
+ };
+ Ok(Self {
+ strips: [load(arrow)?, load(place)?, load(wrong_place)?],
+ visible: false,
+ })
+ }
+
+ pub(super) fn ranges(&self) -> [usize; 3] {
+ std::array::from_fn(|i| self.strips[i].range.range_index)
+ }
+
+ pub(super) fn sync_visibility(&mut self, window: &Window, visible: bool, mouse_look: bool) {
+ if self.visible != visible {
+ self.visible = visible;
+ window.set_cursor_visible(!visible && !mouse_look);
+ }
+ }
+
+ pub(super) fn draw(
+ &mut self,
+ renderer: &mut VulkanSmokeRenderer,
+ mesh: &mut VulkanStaticMesh,
+ now_ms: u32,
+ cursor: Option<([f32; 2], StrategyCursor)>,
+ ) -> Result<(), String> {
+ for (kind, strip) in StrategyCursor::ALL.into_iter().zip(&mut self.strips) {
+ let position = cursor
+ .filter(|(_, current)| kind == *current)
+ .map(|(xy, _)| xy);
+ renderer
+ .set_draw_range_index_count(
+ strip.range.range_index,
+ if position.is_some() { 6 } else { 0 },
+ )
+ .map_err(|error| error.to_string())?;
+ let Some(position) = position else {
+ continue;
+ };
+ let half = strip.size.map(|axis| axis * 0.5);
+ let center =
+ std::array::from_fn(|i| position[i].round_ties_even() - strip.hotspot[i] + half[i]);
+ let left = strip.phase as f32 / PHASES as f32;
+ let right = (strip.phase + 1) as f32 / PHASES as f32;
+ let vertices = screen_tl_quad_vertices_pixels(
+ center,
+ half[0],
+ half[1],
+ [640.0, 480.0],
+ [1.0; 3],
+ 1.0,
+ [[left, 0.0], [left, 1.0], [right, 1.0], [right, 0.0]],
+ );
+ let start = strip.range.vertex_start;
+ mesh.vertices[start..start + vertices.len()].copy_from_slice(&vertices);
+ // iron3d+0x57500 draws the current phase, then advances one frame if due.
+ if now_ms.saturating_sub(strip.last_change_ms) > strip.delay_ms {
+ strip.phase = (strip.phase + 1) % PHASES;
+ strip.last_change_ms = now_ms;
+ }
+ }
+ Ok(())
+ }
+}