aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/strategy_toolbar.rs
blob: 11a3fcab5843a5639f6a3aa5946f5637c88bc407 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! Classic strategic toolbar assembled from the original HQ sprites.

use crate::{mission_pager::copy_sprite, ui_sprite::load_ui_sprites, RgbaImage};
use fparkan_vfs::Vfs;
mod fold;
pub use fold::{ToolbarFold, ToolbarLayout};

/// Bounds from iron3d+0x82930, in the original 640×480 screen space.
pub const EXTENT: [u32; 2] = [46, 480];

// The constructor at iron3d+0x82930 also determines the sections' order.
const BUTTON_TOPS: [u32; 12] = [12, 65, 94, 123, 176, 229, 258, 287, 316, 369, 399, 428];
/// String resources assigned to the buttons by iron3d+0x82930, top to bottom.
pub const TOOLTIP_IDS: [u32; BUTTON_TOPS.len()] = [
    0x64c, 0x640, 0x642, 0x641, 0x13d8, 0x647, 0x64e, 0x648, 0x64a, 0x13ae, 0x5dd, 0x516,
];
const SEPARATORS: [u32; 4] = [41, 152, 205, 345];
const LOCK_TOP: u32 = 457;
const LOCK_INDEX: usize = BUTTON_TOPS.len();
/// Number of drawable controls, including the bottom latch.
pub const CONTROL_COUNT: usize = LOCK_INDEX + 1;
const GLYPH_OFFSET: [u32; 2] = [16, 3];
const LOCK_GLYPH_OFFSET: [u32; 2] = [18, 7];

/// Actions available immediately upon entering strategic mode.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ToolbarAction {
    /// Return to the hero in the suit.
    Hero,
    /// Toggle the clan's warbot list.
    Warbots,
    /// Toggle the satellite map.
    Map,
}

/// Tests the native button bounds in the original 640×480 screen space.
#[must_use]
pub fn command_button_at([x, y]: [f32; 2]) -> Option<ToolbarAction> {
    match button_index_at([x, y])? {
        0 => Some(ToolbarAction::Hero),
        1 => Some(ToolbarAction::Warbots),
        10 => Some(ToolbarAction::Map),
        _ => None,
    }
}

/// Index of the button under the pointer, including disabled buttons.
#[must_use]
pub fn button_index_at([x, y]: [f32; 2]) -> Option<usize> {
    if !(0.0..EXTENT[0] as f32).contains(&x.round_ties_even()) {
        return None;
    }
    let y = y.round_ties_even();
    BUTTON_TOPS.into_iter().position(|top| {
        let top = top as f32;
        (top..top + 29.0).contains(&y)
    })
}

/// The original disabled, resting and lit frame and glyph variants.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ButtonVisual {
    Disabled,
    Idle,
    Lit,
}

impl ButtonVisual {
    fn tint(self) -> [u8; 3] {
        match self {
            Self::Disabled => [100; 3],
            Self::Idle => [128, 255, 128],
            Self::Lit => [255; 3],
        }
    }
}

/// Hover phase and timestamp belong to each button in iron3d+0x9be40.
#[derive(Default)]
pub struct ToolbarHover {
    lit: [bool; CONTROL_COUNT],
    last_change: [u32; CONTROL_COUNT],
}

impl ToolbarHover {
    pub fn apply(
        &mut self,
        visuals: &mut [ButtonVisual; CONTROL_COUNT],
        pointer: [f32; 2],
        now_ms: u32,
        layout: ToolbarLayout,
    ) {
        let button = if layout.over_lock(pointer) {
            Some(LOCK_INDEX)
        } else {
            button_index_at(pointer).filter(|_| layout.button_visible(pointer[1]))
        };
        let Some(index) = button.filter(|&index| visuals[index] == ButtonVisual::Idle) else {
            return;
        };
        if self.lit[index] {
            visuals[index] = ButtonVisual::Lit;
        }
        if now_ms.wrapping_sub(self.last_change[index]) > 100 {
            self.last_change[index] = now_ms;
            self.lit[index] = !self.lit[index];
        }
    }
}

/// Original frame and glyph images for the expanded toolbar.
pub struct ToolbarImages {
    base: RgbaImage,
    buttons: [RgbaImage; 3],
    locks: [RgbaImage; 3],
    lock_icon: RgbaImage,
    glyphs: [RgbaImage; BUTTON_TOPS.len()],
}

impl ToolbarImages {
    pub fn load(vfs: &dyn Vfs) -> Result<Self, String> {
        let [head, separator, button1, button2, button3, lock1, lock2, lock3, lock_icon, glyphs @ ..] =
            load_ui_sprites(
                vfs,
                "ui/hq.cfg",
                [
                    "objpanel_head",
                    "objpanel_separator",
                    "objpanel_button1",
                    "objpanel_button2",
                    "objpanel_button3",
                    "objpanel_lock1",
                    "objpanel_lock2",
                    "objpanel_lock3",
                    "objpanel_lock_icon",
                    "objpanel_icon_hero",
                    "objpanel_icon_warbots",
                    "objpanel_icon_builder",
                    "objpanel_icon_cargo",
                    "objpanel_icon_restree",
                    "objpanel_icon_factory",
                    "objpanel_icon_tower",
                    "objpanel_icon_bunker",
                    "objpanel_icon_build",
                    "objpanel_icon_chat",
                    "objpanel_icon_map",
                    "objpanel_icon_system",
                ],
            )?;
        let [width, height] = EXTENT;
        let mut base = RgbaImage {
            width,
            height,
            rgba8: vec![0; (width * height * 4) as usize],
        };
        stamp(&mut base, &head, [0, 0], [255; 3]);
        for top in SEPARATORS {
            stamp(&mut base, &separator, [0, top], [255; 3]);
        }
        Ok(Self {
            base,
            buttons: [button1, button2, button3],
            locks: [lock1, lock2, lock3],
            lock_icon,
            glyphs,
        })
    }

    pub fn paint(
        &self,
        visuals: [ButtonVisual; CONTROL_COUNT],
        layout: ToolbarLayout,
    ) -> RgbaImage {
        let mut toolbar = self.base.clone();
        let body_bottom = layout.body_bottom();
        toolbar.rgba8[(body_bottom * EXTENT[0] * 4) as usize..].fill(0);
        for (index, (&top, glyph)) in BUTTON_TOPS.iter().zip(&self.glyphs).enumerate() {
            if top >= body_bottom {
                break;
            }
            let visual = visuals[index];
            stamp(
                &mut toolbar,
                &self.buttons[visual as usize],
                [0, top],
                [255; 3],
            );
            stamp(
                &mut toolbar,
                glyph,
                [GLYPH_OFFSET[0], top + GLYPH_OFFSET[1]],
                visual.tint(),
            );
        }
        if let Some(top) = layout.lock_top() {
            let lock = visuals[LOCK_INDEX];
            stamp(&mut toolbar, &self.locks[lock as usize], [0, top], [255; 3]);
            stamp(
                &mut toolbar,
                &self.lock_icon,
                [LOCK_GLYPH_OFFSET[0], top + LOCK_GLYPH_OFFSET[1]],
                lock.tint(),
            );
        }
        toolbar
    }
}

fn stamp(toolbar: &mut RgbaImage, sprite: &RgbaImage, [x, y]: [u32; 2], tint: [u8; 3]) {
    copy_sprite(
        toolbar,
        sprite,
        [x, y, x + sprite.width, y + sprite.height],
        [false; 2],
        tint,
    );
}

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

    #[test]
    fn command_buttons_use_their_original_row_bounds() {
        assert_eq!(command_button_at([16.0, 12.0]), Some(ToolbarAction::Hero));
        assert_eq!(
            command_button_at([16.0, 65.0]),
            Some(ToolbarAction::Warbots)
        );
        assert_eq!(command_button_at([45.0, 427.0]), Some(ToolbarAction::Map));
        assert_eq!(command_button_at([46.0, 400.0]), None);
        assert_eq!(command_button_at([25.0, 428.0]), None);
    }

    #[test]
    fn tooltip_targets_include_disabled_buttons_but_exclude_separators_and_lock() {
        let resource = |x, y| button_index_at([x, y]).map(|index| TOOLTIP_IDS[index]);
        assert_eq!(resource(45.0, 258.0), Some(0x64e));
        assert_eq!(resource(0.0, 456.0), Some(0x516));
        for gap in [11.0, 41.0, 152.0, 205.0, 345.0, 457.0, 479.0] {
            assert_eq!(resource(24.0, gap), None);
        }
        assert_eq!(resource(46.0, 258.0), None);
    }

    #[test]
    #[ignore = "requires licensed corpus"]
    fn expanded_toolbar_loads_the_native_buttons_in_order() {
        let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap();
        let vfs = DirectoryVfs::new(root);
        let assets = ToolbarImages::load(&vfs).unwrap();
        let mut visuals = [ButtonVisual::Idle; CONTROL_COUNT];
        visuals[10] = ButtonVisual::Lit;
        visuals[6] = ButtonVisual::Disabled;
        let toolbar = assets.paint(visuals, ToolbarLayout::default());
        let [width, height] = EXTENT;
        assert_eq!([toolbar.width, toolbar.height], EXTENT);
        let [warbots, map, tower] = load_ui_sprites(
            &vfs,
            "ui/hq.cfg",
            [
                "objpanel_icon_warbots",
                "objpanel_icon_map",
                "objpanel_icon_tower",
            ],
        )
        .unwrap();
        for (icon, top, pixel_color) in [
            (warbots, 65, [128, 255, 128, 255]),
            (map, 399, [255; 4]),
            (tower, 258, [100, 100, 100, 255]),
        ] {
            let pixel = icon
                .rgba8
                .chunks_exact(4)
                .position(|p| p == [255; 4])
                .unwrap();
            let [x, y] = GLYPH_OFFSET;
            let index = (top + y) as usize * width as usize
                + x as usize
                + pixel / icon.width as usize * width as usize
                + pixel % icon.width as usize;
            assert_eq!(&toolbar.rgba8[index * 4..index * 4 + 4], &pixel_color);
        }
        assert_eq!(toolbar.rgba8.len(), (width * height * 4) as usize);
    }

    #[test]
    fn hover_blinks_per_button_and_keeps_disabled_and_selected_variants() {
        use ButtonVisual::*;
        let mut hover = ToolbarHover::default();
        let mut sample = |position, now| {
            let mut visuals = [Idle; CONTROL_COUNT];
            visuals[1] = Lit;
            visuals[6] = Disabled;
            hover.apply(&mut visuals, position, now, ToolbarLayout::default());
            visuals
        };
        assert_eq!(sample([20.0, 12.0], 101)[0], Idle);
        assert_eq!(sample([20.0, 12.0], 201)[0], Lit);
        assert_eq!(sample([20.0, 12.0], 202)[0], Lit);
        assert_eq!(sample([20.0, 12.0], 203)[0], Idle);
        assert_eq!(sample([20.0, 65.0], 240)[1], Lit);
        assert_eq!(sample([20.0, 258.0], 280)[6], Disabled);
        assert_eq!(sample([20.0, 457.0], 300)[LOCK_INDEX], Idle);
        assert_eq!(sample([20.0, 457.0], 301)[LOCK_INDEX], Lit);
    }
}