aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/strategic_balance.rs
blob: eeebbce0eb92e807b2d41ba0dd496507662c88eb (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
//! Resource balance rows assembled from the original compound controls.

use crate::{game_font::load_layout_font, mission_pager::copy_sprite, ui_sprite::load_ui_sprites};
use fparkan_font::BitmapFont;
use fparkan_texm::RgbaImage;
use fparkan_vfs::Vfs;

const ROW_HEIGHT: u16 = 19;
const SEPARATION: u32 = 21;
const LABEL_WIDTH: u16 = 205;
const VALUE_WIDTH: u16 = 35;
const GREEN: [u8; 3] = [128, 255, 128];
const RED: [u8; 3] = [255, 50, 50];

/// Panel size in the original 640×480 layout, including the overlapping edge pixel.
pub const EXTENT: [u32; 2] = [267, SEPARATION + ROW_HEIGHT as u32];

/// Atlas pieces and the original font for the two strategic balance rows.
pub struct BalancePanel {
    background: RgbaImage,
    labels: [RgbaImage; 2],
    font: BitmapFont,
}

impl BalancePanel {
    /// Loads the reusable atlas art and centers the localized labels in their fields.
    pub fn load(vfs: &dyn Vfs, labels: [&str; 2]) -> Result<Self, String> {
        let [end, value, separator, emitter, body, cap] = load_ui_sprites(
            vfs,
            "ui/compaund.cfg",
            [
                "ccres_ending_text",
                "ccres_body_text",
                "ccres_separator_left_text",
                "ccres_ray_emitter_off",
                "ccres_ray_body",
                "ccres_ray_ending",
            ],
        )?;
        let cells = [
            (&end, 5),
            (&value, u32::from(VALUE_WIDTH)),
            (&separator, 5),
            (&emitter, 10),
            (&body, u32::from(LABEL_WIDTH)),
            (&cap, 6),
        ];
        let [width, height] = EXTENT;
        let mut background = RgbaImage {
            width,
            height,
            rgba8: vec![0; (width * height * 4) as usize],
        };
        for y in [0, SEPARATION] {
            let mut right = width;
            for (sprite, width) in cells {
                copy_sprite(
                    &mut background,
                    sprite,
                    [right - width - 1, y, right, y + u32::from(ROW_HEIGHT)],
                    [false; 2],
                    [255; 3],
                );
                right -= width;
            }
        }
        let font = load_layout_font(vfs)?;
        let [ore, power] = labels.map(|text| font.centered_line(text, LABEL_WIDTH, ROW_HEIGHT));
        Ok(Self {
            background,
            labels: [ore?, power?],
            font,
        })
    }

    /// Composes the filled bars and their text. A hidden zero keeps the row frame.
    pub fn paint(&self, [ore, power]: [Option<u8>; 2]) -> Result<RgbaImage, String> {
        const LABEL_LEFT: u32 = 7;
        const LABEL_RIGHT: u32 = LABEL_LEFT + LABEL_WIDTH as u32;
        const VALUE_LEFT: u32 = EXTENT[0] - 5 - VALUE_WIDTH as u32;
        let mut image = self.background.clone();
        for (row, percent) in [ore, power].into_iter().enumerate() {
            let Some(percent) = percent else {
                continue;
            };
            let y = row as u32 * SEPARATION;
            let end = y + u32::from(ROW_HEIGHT);
            let filled =
                (f64::from(percent.min(100)) * f64::from(LABEL_WIDTH) * f64::from(0.01_f32))
                    .round_ties_even() as u32;
            if filled > 2 {
                let color = match percent {
                    0..=19 => [128, 0, 0, 128],
                    20..=79 => [128, 128, 0, 128],
                    _ => [0, 128, 0, 128],
                };
                copy_sprite(
                    &mut image,
                    &RgbaImage {
                        width: 1,
                        height: 1,
                        rgba8: color.to_vec(),
                    },
                    [LABEL_RIGHT - filled, y + 3, LABEL_RIGHT - 2, end - 3],
                    [false; 2],
                    [255; 3],
                );
            }
            let tint = if percent == 0 { RED } else { GREEN };
            copy_sprite(
                &mut image,
                &self.labels[row],
                [LABEL_LEFT, y, LABEL_RIGHT, end],
                [false; 2],
                tint,
            );
            let number =
                self.font
                    .centered_line(&format!("{percent}%"), VALUE_WIDTH, ROW_HEIGHT)?;
            copy_sprite(
                &mut image,
                &number,
                [VALUE_LEFT, y, VALUE_LEFT + u32::from(VALUE_WIDTH), end],
                [false; 2],
                tint,
            );
        }
        Ok(image)
    }
}