aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/strategic_buildings_hud.rs
blob: b846ae53d322312690f7c9a90a90e9d87def05a0 (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
use super::{
    mission_overlay::{add_overlay_sprite, overlay_texture},
    DirectoryVfs, EnvironmentGpuRange, StaticPreviewScene, VulkanSmokeRenderer,
};
use fparkan_assets::{
    strategic_buildings::{
        hit_test, Hit, Images, Row, COMMAND_TOOLTIP, EXTENT, MANUAL_TOOLTIP, ORIGIN,
    },
    RgbaImage,
};
use fparkan_platform_winit::text::StringResources;
use fparkan_runtime::strategic_buildings::{BuildingRow, BuildingSection};
use fparkan_world::ObjectHandle;
use std::path::Path;

#[derive(PartialEq, Eq)]
pub(super) struct BuildingsFrame {
    pub section: BuildingSection,
    pub rows: Vec<BuildingRow>,
    pub selected: Option<ObjectHandle>,
}

impl BuildingsFrame {
    pub(super) fn tooltip(&self, pointer: [f32; 2]) -> Option<u32> {
        match hit_test(
            pointer,
            self.rows.len(),
            self.section == BuildingSection::Bunkers,
        )? {
            Hit::Command(_) => Some(COMMAND_TOOLTIP),
            Hit::Manual(index) if self.rows[index].manual_control => Some(MANUAL_TOOLTIP),
            _ => None,
        }
    }
}

pub(super) struct StrategicBuildingsHud {
    range: EnvironmentGpuRange,
    images: Images,
    titles: [String; 2],
    strings: StringResources,
    displayed: Option<BuildingsFrame>,
}

impl StrategicBuildingsHud {
    pub(super) fn load(root: &Path, preview: &mut StaticPreviewScene) -> Result<Self, String> {
        let strings = StringResources::open(&root.join("iron3d.dll"))?;
        let titles = [
            strings
                .get(0x64a)
                .ok_or("missing strategic buildings title")?,
            strings
                .get(0x648)
                .ok_or("missing strategic bunkers title")?,
        ];
        let images = Images::load(&DirectoryVfs::new(root))?;
        let [width, height] = EXTENT;
        let [x, y] = ORIGIN.map(|axis| axis as f32);
        let range = add_overlay_sprite(
            preview,
            RgbaImage {
                width,
                height,
                rgba8: vec![0; (width * height * 4) as usize],
            },
            [x, y, x + width as f32, y + height as f32],
            [1.0; 3],
            1.0,
        )?;
        Ok(Self {
            range,
            images,
            titles,
            strings,
            displayed: None,
        })
    }

    pub(super) fn range_index(&self) -> usize {
        self.range.range_index
    }

    pub(super) fn draw(
        &mut self,
        renderer: &mut VulkanSmokeRenderer,
        frame: Option<BuildingsFrame>,
    ) -> Result<(), String> {
        renderer
            .set_draw_range_index_count(self.range_index(), if frame.is_some() { 6 } else { 0 })
            .map_err(|error| error.to_string())?;
        if frame == self.displayed {
            return Ok(());
        }
        if let Some(frame) = &frame {
            let labels: Vec<_> = frame
                .rows
                .iter()
                .map(|row| self.strings.get(row.label).unwrap_or_default())
                .collect();
            let command = frame.section == BuildingSection::Bunkers;
            let rows = frame.rows.iter().zip(&labels).map(|(row, label)| Row {
                label,
                class: row.class,
                size: row.size,
                selected: frame.selected == Some(row.handle),
                durability_percent: row.durability_percent,
                manual_control: row.manual_control,
            });
            let image = self
                .images
                .paint(&self.titles[usize::from(command)], command, rows)?;
            renderer
                .update_material_texture(self.range.material_index, 0, &overlay_texture(image))
                .map_err(|error| error.to_string())?;
        }
        self.displayed = frame;
        Ok(())
    }
}