use super::{ append_environment_gradient_material, append_environment_range, environment_vertex, mission_overlay::source_over, quad_indices, screen_tl_quad_vertices_pixels, world_pipeline_state, EnvironmentGpuRange, LegacyBlendMode, LegacyDepthMode, StaticPreviewScene, VulkanSmokeRenderer, VulkanStaticMesh, VulkanStaticTexture, }; use fparkan_assets::{game_font::BitmapFont, RgbaImage}; use std::sync::Arc; const TOP: u32 = 347; const WIDTH: u32 = 50; const HEIGHT: u32 = 110; const SCALE_HEIGHT: u32 = 105; const WHITE: [u8; 3] = [255; 3]; const UV: [[f32; 2]; 4] = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]; #[derive(Clone, Copy)] pub(super) struct GaugeLayout { left: u32, icon_src: u32, icon_left: u32, number_left: u32, strip_left: u32, flip_strip: bool, } impl GaugeLayout { pub(super) const ALTITUDE: Self = Self { left: 221, icon_src: 215, icon_left: 230, number_left: 226, strip_left: 240, flip_strip: true, }; pub(super) const SPEED: Self = Self { left: 370, icon_src: 199, icon_left: 396, number_left: 388, strip_left: 370, flip_strip: false, }; } pub(super) struct VerticalGaugeHud { range: EnvironmentGpuRange, backing: VulkanStaticTexture, layout: GaugeLayout, page: Arc, font: Arc, previous: Option<(i32, u32)>, } impl VerticalGaugeHud { pub(super) fn load( layout: GaugeLayout, page: Arc, icons: &RgbaImage, font: Arc, preview: &mut StaticPreviewScene, ) -> Result { let mut backing = VulkanStaticTexture { width: WIDTH, height: HEIGHT, rgba8: vec![0; (WIDTH * HEIGHT * 4) as usize], mip_levels: Vec::new(), }; // iron3d+0x3f6a0 and +0x3ff00 select and position the icons. blit( &mut backing, icons, [layout.icon_src, 0, 15, 28], [layout.icon_left - layout.left, 383 - TOP], false, WHITE, ); 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 = backing.clone(); 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, backing, layout, page, font, previous: None, }) } 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], state: Option<(i32, u32)>, ) -> Result<(), String> { renderer .set_draw_range_index_count(self.range_index(), if state.is_some() { 6 } else { 0 }) .map_err(|error| error.to_string())?; let Some(state) = state else { return Ok(()) }; if self.previous != Some(state) { let (value, scale_top) = state; let layout = self.layout; let mut sprite = self.backing.clone(); let text = self.font.centered_line(&value.to_string(), 28, 11)?; blit( &mut sprite, &text, [0, 0, text.width, text.height], [layout.number_left - layout.left, 445 - TOP], false, WHITE, ); // Both bars crop the same rows from the texture and screen. blit( &mut sprite, &self.page, [121, scale_top, 31, SCALE_HEIGHT - scale_top], [layout.strip_left - layout.left, scale_top], layout.flip_strip, [0xff, 0xb4, 0x50], ); renderer .update_material_texture(self.range.material_index, 0, &sprite) .map_err(|error| error.to_string())?; self.previous = Some(state); } let [sx, sy] = [viewport[0] / 640.0, viewport[1] / 480.0]; let half_width = WIDTH as f32 * 0.5; let half_height = HEIGHT as f32 * 0.5; let center = [ (self.layout.left as f32 + half_width) * sx, (TOP as f32 + half_height) * sy, ]; let start = self.range.vertex_start; mesh.vertices[start..start + 4].copy_from_slice(&screen_tl_quad_vertices_pixels( center, half_width * sx, half_height * sy, viewport, [1.0; 3], 1.0, UV, )); Ok(()) } } fn blit( target: &mut VulkanStaticTexture, source: &RgbaImage, [x, y, width, height]: [u32; 4], [left, top]: [u32; 2], flip_x: bool, tint: [u8; 3], ) { for row in 0..height { for col in 0..width { let sx = x + if flip_x { width - 1 - col } else { col }; let src = (((y + row) * source.width + sx) * 4) as usize; let mut pixel: [u8; 4] = source.rgba8[src..src + 4].try_into().expect("RGBA pixel"); for (channel, tint) in pixel.iter_mut().zip(tint) { *channel = ((u16::from(*channel) * u16::from(tint) + 127) / 255) as u8; } let dst = (((top + row) * WIDTH + left + col) * 4) as usize; source_over(&mut target.rgba8[dst..dst + 4], &pixel); } } }