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
|
//! 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;
/// Bounds from iron3d+0x82930, in the original 640×480 screen space.
pub const EXTENT: [u32; 2] = [46, 480];
// Each row uses the ordinary enabled button and its own glyph.
// 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 GLYPH_OFFSET: [u32; 2] = [16, 3];
const LOCK_GLYPH_OFFSET: [u32; 2] = [18, 7];
// iron3d+0x9bf49 sets the ordinary enabled glyph to ARGB ff80ff80.
const ENABLED_GLYPH: [u8; 3] = [128, 255, 128];
/// 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)
})
}
/// Composes the fully expanded toolbar in its resting state.
pub fn load_expanded_toolbar(vfs: &dyn Vfs) -> Result<RgbaImage, String> {
let [head, separator, button, lock, lock_icon, glyphs @ ..] = load_ui_sprites(
vfs,
"ui/hq.cfg",
[
"objpanel_head",
"objpanel_separator",
"objpanel_button2",
"objpanel_lock2",
"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 toolbar = RgbaImage {
width,
height,
rgba8: vec![0; (width * height * 4) as usize],
};
stamp(&mut toolbar, &head, [0, 0], [255; 3]);
for top in SEPARATORS {
stamp(&mut toolbar, &separator, [0, top], [255; 3]);
}
for (top, glyph) in BUTTON_TOPS.into_iter().zip(glyphs) {
stamp(&mut toolbar, &button, [0, top], [255; 3]);
stamp(
&mut toolbar,
&glyph,
[GLYPH_OFFSET[0], top + GLYPH_OFFSET[1]],
ENABLED_GLYPH,
);
}
stamp(&mut toolbar, &lock, [0, LOCK_TOP], [255; 3]);
stamp(
&mut toolbar,
&lock_icon,
[LOCK_GLYPH_OFFSET[0], LOCK_TOP + LOCK_GLYPH_OFFSET[1]],
ENABLED_GLYPH,
);
Ok(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 toolbar = load_expanded_toolbar(&vfs).unwrap();
let [width, height] = EXTENT;
assert_eq!([toolbar.width, toolbar.height], EXTENT);
let [warbots, map] = load_ui_sprites(
&vfs,
"ui/hq.cfg",
["objpanel_icon_warbots", "objpanel_icon_map"],
)
.unwrap();
for (icon, top) in [(warbots, 65), (map, 399)] {
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],
&[128, 255, 128, 255]
);
}
assert_eq!(toolbar.rgba8.len(), (width * height * 4) as usize);
}
}
|