aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/strategic_buildings.rs
blob: 0ed6d1407fbaa97b849f0b33ab22d4c1eaad2170 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! Shared compound controls of the strategic building sections.

use crate::{
    game_font::{load_layout_font, BitmapFont},
    satellite_map_chrome::{building_glyph, building_glyph_tint},
    strategic_factory::{stamp, text},
    strategic_roster::durability_fill,
    ui_sprite::{atlas_region, load_ui_page, load_ui_sprites},
    RgbaImage,
};
use fparkan_vfs::Vfs;

pub use crate::strategic_factory::{EXTENT, ORIGIN};
const ROW_PITCH: u32 = 20;
const ROW_HEIGHT: u32 = 19;
const TEXT_LEFT: u32 = 44;
const TEXT_RIGHT: u32 = 313;
const COMMAND_LEFT: u32 = 34;
const BUTTON_WIDTH: u32 = 35;
pub const COMMAND_TOOLTIP: u32 = 0x5ed;
pub const VISIBLE_ROWS: usize = (EXTENT[1] / ROW_PITCH - 1) as usize;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Hit {
    Close,
    Row(usize),
    Command(usize),
}

pub fn hit_test([x, y]: [f32; 2], rows: usize, command_buttons: bool) -> Option<Hit> {
    let x = x.round_ties_even() - ORIGIN[0] as f32;
    let y = y.round_ties_even();
    if (283.0..318.0).contains(&x) && (0.0..19.0).contains(&y) {
        return Some(Hit::Close);
    }
    if !(0.0..(EXTENT[0] - 1) as f32).contains(&x) || !(20.0..480.0).contains(&y) {
        return None;
    }
    let y = y as u32 - ROW_PITCH;
    let row = (y / ROW_PITCH) as usize;
    if row >= rows.min(VISIBLE_ROWS) || y % ROW_PITCH >= ROW_HEIGHT {
        return None;
    }
    let command = COMMAND_LEFT as f32..(COMMAND_LEFT + BUTTON_WIDTH) as f32;
    if command_buttons && command.contains(&x) {
        Some(Hit::Command(row))
    } else {
        Some(Hit::Row(row))
    }
}

pub struct Row<'a> {
    pub label: &'a str,
    pub class: u32,
    pub size: u32,
    pub selected: bool,
    pub durability_percent: Option<i32>,
}

pub struct Images {
    header: RgbaImage,
    font: BitmapFont,
    cap: [RgbaImage; 2],
    frame: RgbaImage,
    separator: RgbaImage,
    emitter: [RgbaImage; 2],
    ray: RgbaImage,
    end: RgbaImage,
    atlas: RgbaImage,
    command_frame: [RgbaImage; 2],
    command_icon: RgbaImage,
}

impl Images {
    pub fn load(vfs: &dyn Vfs) -> Result<Self, String> {
        let [title_cap, frame, close, cap_off, cap_on, separator, emitter_off, emitter_on, ray, end] =
            load_ui_sprites(
                vfs,
                "ui/compaund.cfg",
                [
                    "ccres_ending_text",
                    "ccres_body_text",
                    "ccres_exit_button_pressed",
                    "ccres_lamp_stub_ending_off",
                    "ccres_lamp_stub_ending_normal",
                    "ccres_separator_left_text",
                    "ccres_ray_emitter_off",
                    "ccres_ray_emitter_normal",
                    "ccres_ray_body",
                    "ccres_ray_ending",
                ],
            )?;
        let [exit, command_off, command_on, command_icon] = load_ui_sprites(
            vfs,
            "ui/hq.cfg",
            [
                "exit_icon",
                "buildscreen_button1",
                "buildscreen_button2",
                "buildscreen_direct_icon",
            ],
        )?;
        let font = load_layout_font(vfs)?;
        let [width, height] = EXTENT;
        let mut header = RgbaImage {
            width,
            height,
            rgba8: vec![0; (width * height * 4) as usize],
        };
        for (sprite, rect) in [
            (&title_cap, [0, 0, 6, 19]),
            (&frame, [5, 0, 284, 19]),
            (&close, [283, 0, 318, 19]),
            (&exit, [298, 3, 311, 16]),
        ] {
            stamp(&mut header, sprite, rect, [255; 3]);
        }
        Ok(Self {
            header,
            font,
            cap: [cap_off, cap_on],
            frame,
            separator,
            emitter: [emitter_off, emitter_on],
            ray,
            end,
            atlas: load_ui_page(vfs, "ui_menu")?,
            command_frame: [command_off, command_on],
            command_icon,
        })
    }

    pub fn paint<'a>(
        &self,
        title: &str,
        command_buttons: bool,
        rows: impl Iterator<Item = Row<'a>>,
    ) -> Result<RgbaImage, String> {
        let mut image = self.header.clone();
        text(&self.font, &mut image, title, [5, 0, 283, 19], [255; 3])?;
        for (index, row) in rows.take(VISIBLE_ROWS).enumerate() {
            let y = (index as u32 + 1) * ROW_PITCH;
            let bottom = y + ROW_HEIGHT;
            let selected = usize::from(row.selected);
            // The original bunker layout measures the label after two 35-unit controls.
            let emitter = COMMAND_LEFT + if command_buttons { 2 * BUTTON_WIDTH } else { 0 };
            let text_left = emitter + (TEXT_LEFT - COMMAND_LEFT);
            for (sprite, left, right) in [
                (&self.cap[selected], 0, 11),
                (&self.frame, 10, 30),
                (&self.separator, 29, 35),
                (&self.emitter[selected], emitter, emitter + 11),
                (&self.ray, text_left, TEXT_RIGHT + 1),
                (&self.end, TEXT_RIGHT, EXTENT[0]),
            ] {
                stamp(&mut image, sprite, [left, y, right, bottom], [255; 3]);
            }
            if command_buttons {
                stamp(
                    &mut image,
                    &self.command_frame[selected],
                    [COMMAND_LEFT, y, COMMAND_LEFT + BUTTON_WIDTH + 1, bottom],
                    [255; 3],
                );
                stamp(
                    &mut image,
                    &self.command_icon,
                    [COMMAND_LEFT + 10, y + 2, COMMAND_LEFT + 25, y + 17],
                    [255; 3],
                );
            }
            if let Some([x, atlas_y]) = building_glyph(row.class) {
                let glyph = atlas_region(&self.atlas, [x, atlas_y, 15, 15])
                    .ok_or("building glyph outside ui_menu")?;
                stamp(
                    &mut image,
                    &glyph,
                    [12, y + 2, 27, y + 17],
                    building_glyph_tint(row.size),
                );
            }
            if let Some((width, rgba)) = row
                .durability_percent
                .and_then(|percent| durability_fill(percent, TEXT_RIGHT - text_left))
            {
                let fill = RgbaImage {
                    width: 1,
                    height: 1,
                    rgba8: rgba.to_vec(),
                };
                stamp(
                    &mut image,
                    &fill,
                    [text_left + 2, y + 3, text_left + width, bottom - 3],
                    [255; 3],
                );
            }
            text(
                &self.font,
                &mut image,
                row.label,
                [text_left, y, TEXT_RIGHT, bottom],
                if row.selected { [255; 3] } else { [128; 3] },
            )?;
        }
        Ok(image)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn building_list_hits_labels_icons_and_close_with_native_row_gaps() {
        assert_eq!(hit_test([350.0, 9.0], 4, false), Some(Hit::Close));
        assert_eq!(hit_test([68.0, 39.0], 4, false), None);
        assert_eq!(hit_test([68.0, 40.0], 4, false), Some(Hit::Row(1)));
        assert_eq!(hit_test([340.0, 49.0], 4, false), Some(Hit::Row(1)));
        assert_eq!(hit_test([340.0, 49.0], 1, false), None);
        assert_eq!(hit_test([102.0, 25.0], 1, true), Some(Hit::Command(0)));
        assert_eq!(hit_test([102.0, 25.0], 1, false), Some(Hit::Row(0)));
        assert_eq!(hit_test([119.0, 25.0], 1, true), Some(Hit::Command(0)));
        assert_eq!(hit_test([120.0, 25.0], 1, true), Some(Hit::Row(0)));
    }
}