aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/cockpit_instrument.rs
blob: 5114d0b1f4b02e4286a008d16eac291b719bd399 (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
//! Composition of the right cockpit instrument from the original UI sheets.

use crate::{
    game_font::{load_layout_font, BitmapFont},
    mission_pager::copy_sprite,
    strategic_factory::{stamp, text},
    ui_sprite::{atlas_region, load_ui_page, load_ui_sprites},
    RgbaImage,
};
use fparkan_vfs::Vfs;

pub const FRAME: [f32; 4] = [490.0, 306.0, 640.0, 480.0];
pub const MODEL: [f32; 4] = [503.0, 315.0, 631.0, 443.0];
const METER_HEIGHT: u32 = 94;

pub struct Images {
    bezel: RgbaImage,
    life: RgbaImage,
    energy: RgbaImage,
    shields: [RgbaImage; 5],
    page: RgbaImage,
    font: BitmapFont,
}

impl Images {
    pub fn load(vfs: &dyn Vfs) -> Result<Self, String> {
        let [bezel, life, energy, side, front, back, top, bottom] = load_ui_sprites(
            vfs,
            "ui/hq.cfg",
            [
                "targeter_back",
                "targeter_life",
                "targeter_energy",
                "left_shld",
                "frwd_shld",
                "back_shld",
                "top_shld",
                "bott_shld",
            ],
        )?;
        let page = load_ui_page(vfs, "ui_menu3")?;
        let font = load_layout_font(vfs)?;
        Ok(Self {
            bezel,
            life,
            energy,
            shields: [side, front, back, top, bottom],
            page,
            font,
        })
    }

    pub fn paint(
        &self,
        name: &str,
        durability: Option<i32>,
        energy: u8,
        shield: Option<[u8; 6]>,
    ) -> Result<RgbaImage, String> {
        let (width, height) = (self.bezel.width, self.bezel.height);
        let mut image = RgbaImage {
            width,
            height,
            rgba8: vec![0; (width * height * 4) as usize],
        };
        copy_sprite(
            &mut image,
            &self.bezel,
            [0, 0, width, height],
            [true, false],
            [255; 3],
        );
        stamp(&mut image, &self.life, [6, 105, 21, 120], [255; 3]);
        stamp(&mut image, &self.energy, [130, 105, 145, 120], [255; 3]);
        if let Some([front, back, left, right, top, bottom]) = shield {
            let [side_image, front_image, back_image, top_image, bottom_image] = &self.shields;
            for (sprite, rect, flip, value) in [
                (side_image, [17, 28, 43, 107], false, left),
                (side_image, [108, 28, 134, 107], true, right),
                (front_image, [45, 15, 106, 34], false, front),
                (back_image, [31, 103, 120, 132], false, back),
                (top_image, [41, 2, 110, 21], false, top),
                (bottom_image, [23, 114, 128, 145], false, bottom),
            ] {
                copy_sprite(
                    &mut image,
                    sprite,
                    rect,
                    [flip, false],
                    [255 - value, value, 0],
                );
            }
        }
        for (percent, x, flip, tint) in [
            (durability.unwrap_or(0), 1, false, [25, 255, 175]),
            (i32::from(energy), 110, true, [255, 180, 80]),
        ] {
            let cut = meter_cut(percent);
            if cut == METER_HEIGHT {
                continue;
            }
            let strip = atlas_region(&self.page, [151, 82 + cut, 40, METER_HEIGHT - cut])
                .ok_or("cockpit meter outside ui_menu3")?;
            copy_sprite(
                &mut image,
                &strip,
                [x, 8 + cut, x + 40, 102],
                [flip, false],
                tint,
            );
        }
        text(&self.font, &mut image, name, [4, 153, 142, 164], [255; 3])?;
        Ok(image)
    }
}

fn meter_cut(percent: i32) -> u32 {
    (100 - percent.clamp(0, 100)) as u32 * METER_HEIGHT / 100
}

#[cfg(test)]
#[test]
fn the_native_bar_fills_upward_in_integer_pixels() {
    assert_eq!(meter_cut(-1), 94);
    assert_eq!(meter_cut(25), 70);
    assert_eq!(meter_cut(99), 0);
    assert_eq!(meter_cut(120), 0);
}