aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/factory_projects.rs
blob: ca77137a4a51e91659977960f235bb7ac8e40e5c (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
//! Factory project buttons in the factory panel's logical coordinate space.

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

/// Number of project slots on the factory panel.
pub const PROJECT_SLOTS: usize = 5;
/// Padded texture origin at the stock factory panel.
pub const ORIGIN: [u32; 2] = [50, 122];
/// Logical size of the project controls texture.
// A transparent texel on each side keeps repeat sampling from bleeding buttons across the quad.
pub const EXTENT: [u32; 2] = [328, 48];
const BUILD: [u32; 4] = [277, 28, 327, 47];
const CONSTRUCTOR: [u32; 4] = [20, 28, 70, 47];

/// A command selected with the pointer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FactoryProjectAction {
    /// Select a slot for inspection or production.
    Select(usize),
    /// Queue the selected project at this factory.
    Build,
    /// Open the active factory's model designer.
    Designer,
}

/// Original button frames, icons and game font for the project controls.
pub struct FactoryProjectImages {
    frames: [RgbaImage; 2],
    project: RgbaImage,
    build_frame: RgbaImage,
    build_icon: RgbaImage,
    constructor_icon: RgbaImage,
    font: BitmapFont,
}

impl FactoryProjectImages {
    /// Resolves factory sprites and text through the installed game's catalogs.
    pub fn load(vfs: &dyn Vfs) -> Result<Self, String> {
        let [idle, pressed, project, build_icon, constructor_icon] = load_ui_sprites(
            vfs,
            "ui/hq.cfg",
            [
                "short_button_frame_off",
                "short_button_frame_on",
                "project_icon",
                "build_icon",
                "constructor_icon",
            ],
        )?;
        let [build_frame] = load_ui_sprites(vfs, "ui/compaund.cfg", ["ccres_long_button_normal"])?;
        Ok(Self {
            frames: [idle, pressed],
            project,
            build_icon,
            constructor_icon,
            build_frame,
            font: load_layout_font(vfs)?,
        })
    }

    /// Composes just the factory project controls over the host view.
    pub fn paint(
        &self,
        count: usize,
        selected: usize,
        pressed: Option<usize>,
        name: &str,
        designer_available: bool,
    ) -> Result<RgbaImage, String> {
        let mut image = RgbaImage {
            width: EXTENT[0],
            height: EXTENT[1],
            rgba8: vec![0; (EXTENT[0] * EXTENT[1] * 4) as usize],
        };
        for index in 0..count.min(PROJECT_SLOTS) {
            let left = project_left(index);
            copy_sprite(
                &mut image,
                &self.frames[usize::from(pressed == Some(index))],
                [left, 1, left + 23, 22],
                [false; 2],
                [255; 3],
            );
            copy_sprite(
                &mut image,
                &self.project,
                project_hit(index),
                [false; 2],
                if selected == index {
                    [255; 3]
                } else {
                    [128, 255, 128]
                },
            );
        }
        self.paint_button(&mut image, BUILD, &self.build_icon);
        if designer_available {
            self.paint_button(&mut image, CONSTRUCTOR, &self.constructor_icon);
        }
        let label = self.font.centered_line(name, 150, 16)?;
        copy_sprite(&mut image, &label, [125, 17, 275, 33], [false; 2], [255; 3]);
        Ok(image)
    }

    fn paint_button(&self, image: &mut RgbaImage, rect: [u32; 4], icon: &RgbaImage) {
        copy_sprite(image, &self.build_frame, rect, [false; 2], [255; 3]);
        let [left, top, _, _] = rect;
        copy_sprite(
            image,
            icon,
            [left + 10, top + 2, left + 40, top + 17],
            [false; 2],
            [240; 3],
        );
    }
}

/// Resolves only populated slots and the order button in 640×480 coordinates.
pub fn hit_test(position: [f32; 2], count: usize) -> Option<FactoryProjectAction> {
    let [x, y] = [
        position[0] - ORIGIN[0] as f32,
        position[1] - ORIGIN[1] as f32,
    ];
    let hit =
        |[l, t, r, b]: [u32; 4]| x >= l as f32 && x < r as f32 && y >= t as f32 && y < b as f32;
    if count != 0 && hit(BUILD) {
        return Some(FactoryProjectAction::Build);
    }
    if hit(CONSTRUCTOR) {
        return Some(FactoryProjectAction::Designer);
    }
    (0..count.min(PROJECT_SLOTS))
        .find(|&index| hit(project_hit(index)))
        .map(FactoryProjectAction::Select)
}

fn project_left(index: usize) -> u32 {
    9 + 23 * index as u32
}

fn project_hit(index: usize) -> [u32; 4] {
    let left = project_left(index);
    [left + 2, 3, left + 21, 20]
}

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

    #[test]
    fn only_the_glyph_selects_a_populated_factory_slot() {
        assert_eq!(
            hit_test([61.0, 125.0], 2),
            Some(FactoryProjectAction::Select(0))
        );
        assert_eq!(
            hit_test([84.0, 125.0], 2),
            Some(FactoryProjectAction::Select(1))
        );
        assert_eq!(hit_test([83.0, 125.0], 2), None);
        assert_eq!(hit_test([84.0, 125.0], 1), None);
        assert_eq!(
            hit_test([350.0, 159.0], 2),
            Some(FactoryProjectAction::Build)
        );
        assert_eq!(hit_test([350.0, 159.0], 0), None);
    }
}