aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/warbot_tooltip_hud.rs
blob: cd54b7d451f3347495e66da8863004249b9492ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use super::{
    mission_overlay::add_overlay_sprite, screen_tl_quad_vertices_pixels, DirectoryVfs,
    EnvironmentGpuRange, StaticPreviewScene, VulkanSmokeRenderer, VulkanStaticMesh,
};
use fparkan_assets::{
    game_font::{load_tool_font, TOOL_FONT_EXTENTS},
    strategic_roster::{hit_test, CockpitControl, ListHit},
    ui_tooltip::{self, TooltipHover},
    RgbaImage,
};
use fparkan_platform_winit::text::StringResources;
use std::{path::Path, time::Instant};

// In the order in which iron3d+0x82930 assigns the original texts to the buttons.
const LABELS: [u32; 4] = [0x05de, 0x05ef, 0x05ee, 0x1864];

#[derive(Clone, Copy, Default)]
struct Frame {
    top: u32,
    size: [u32; 2],
}

pub(super) struct WarbotTooltipHud {
    range: EnvironmentGpuRange,
    frames: [[Frame; LABELS.len()]; TOOL_FONT_EXTENTS.len()],
    atlas_size: [u32; 2],
    hover: TooltipHover,
}

impl WarbotTooltipHud {
    pub(super) fn load(root: &Path, preview: &mut StaticPreviewScene) -> Result<Self, String> {
        let strings = StringResources::open(&root.join("iron3d.dll"))?;
        let labels = LABELS.map(|id| {
            strings
                .get(id)
                .ok_or_else(|| format!("missing warbot tooltip {id:#x}"))
        });
        let [manual, auto_gunner, autopilot, self_destruct] = labels;
        let labels = [manual?, auto_gunner?, autopilot?, self_destruct?];
        let vfs = DirectoryVfs::new(root);
        let mut images = Vec::new();
        let mut frames = [[Frame::default(); LABELS.len()]; TOOL_FONT_EXTENTS.len()];
        let [mut width, mut height] = [0, 0];
        for (profile, frames) in frames.iter_mut().enumerate() {
            let font = load_tool_font(&vfs, profile)?;
            for (frame, label) in frames.iter_mut().zip(&labels) {
                let image = ui_tooltip::paint(&font, label)?;
                *frame = Frame {
                    top: height,
                    size: [image.width, image.height],
                };
                width = width.max(image.width);
                height += image.height;
                images.push(image);
            }
        }
        let mut rgba8 = vec![0; (width * height * 4) as usize];
        for (frame, image) in frames.iter().flatten().zip(images) {
            let rows = rgba8
                .chunks_exact_mut((width * 4) as usize)
                .skip(frame.top as usize);
            for (dest, source) in rows.zip(image.rgba8.chunks_exact((image.width * 4) as usize)) {
                dest[..source.len()].copy_from_slice(source);
            }
        }
        let range = add_overlay_sprite(
            preview,
            RgbaImage {
                width,
                height,
                rgba8,
            },
            [0.0; 4],
            [1.0; 3],
            1.0,
        )?;
        Ok(Self {
            range,
            frames,
            atlas_size: [width, height],
            hover: TooltipHover::default(),
        })
    }

    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],
        pointer: Option<[f32; 2]>,
        rows: usize,
    ) -> Result<(), String> {
        let tooltip = pointer.and_then(|xy| {
            let label = match hit_test(xy, rows)? {
                ListHit::Cockpit(CockpitControl::Manual) => 0,
                ListHit::Cockpit(CockpitControl::AutoGunner) => 1,
                ListHit::Cockpit(CockpitControl::Autopilot) => 2,
                ListHit::SelfDestruct => 3,
                _ => return None,
            };
            let pointer = std::array::from_fn(|i| {
                (xy[i] * viewport[i] / [640.0, 480.0][i]).round_ties_even() as i32
            });
            self.hover
                .visible(pointer, Instant::now())
                .then_some((pointer, label))
        });
        renderer
            .set_draw_range_index_count(self.range_index(), if tooltip.is_some() { 6 } else { 0 })
            .map_err(|error| error.to_string())?;
        let Some((pointer, label)) = tooltip else {
            return Ok(());
        };
        let extent = viewport.map(|value| value as u32);
        let profile = TOOL_FONT_EXTENTS
            .iter()
            .rposition(|&[width, height]| extent[0] >= width && extent[1] >= height)
            .unwrap_or(0);
        let frame = self.frames[profile][label];
        let [x, y] = ui_tooltip::origin(
            pointer,
            frame.size.map(|value| value as i32),
            extent.map(|value| value as i32),
            (16.0 * viewport[1] / 480.0).round_ties_even() as i32,
        );
        let [half_width, half_height] = frame.size.map(|value| value as f32 * 0.5);
        let right = frame.size[0] as f32 / self.atlas_size[0] as f32;
        let top = frame.top as f32 / self.atlas_size[1] as f32;
        let bottom = (frame.top + frame.size[1]) as f32 / self.atlas_size[1] as f32;
        let vertices = screen_tl_quad_vertices_pixels(
            [x as f32 + half_width, y as f32 + half_height],
            half_width,
            half_height,
            viewport,
            [1.0; 3],
            1.0,
            [[0.0, top], [0.0, bottom], [right, bottom], [right, top]],
        );
        let start = self.range.vertex_start;
        mesh.vertices[start..start + vertices.len()].copy_from_slice(&vertices);
        Ok(())
    }
}