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
|
use super::{stamp, RosterImages, ORIGIN, ROW_HEIGHT, ROW_PITCH, ROW_TOP};
use crate::{ui_sprite::load_ui_sprites, RgbaImage};
use fparkan_vfs::Vfs;
const LEFT: u32 = 100;
const RIGHT: u32 = 367;
/// Commands supported by the strategic warbot panel.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RosterOrder {
/// Stop in place.
Stand,
/// Draw a route and send it to the unit.
FollowRoute,
/// Find and engage hostile mobile units.
SearchAndDestroy,
/// Patrol a point or a live object.
Patrol,
/// Travel to a friendly facility for ammunition, charge, and repairs.
Recharge,
}
const ORDERS: [RosterOrder; 5] = [
RosterOrder::Stand,
RosterOrder::FollowRoute,
RosterOrder::SearchAndDestroy,
RosterOrder::Patrol,
RosterOrder::Recharge,
];
impl RosterOrder {
fn slot(self) -> u32 {
match self {
Self::Stand => 0,
Self::FollowRoute => 1,
Self::SearchAndDestroy => 2,
Self::Patrol => 3,
Self::Recharge => 4,
}
}
}
/// Returns the visible command at the pointer, excluding borders and row gaps.
pub fn order_at([x, y]: [f32; 2], count: usize) -> Option<RosterOrder> {
let [x, y] = [x.round_ties_even(), y.round_ties_even()];
if !(LEFT as f32..RIGHT as f32).contains(&x) || !(0.0..480.0).contains(&y) {
return None;
}
let y = (y as u32).checked_sub(panel_top(count) + ROW_PITCH)?;
ORDERS
.into_iter()
.find(|order| order.slot() == y / ROW_PITCH)
.filter(|_| y % ROW_PITCH < ROW_HEIGHT)
}
fn panel_top(count: usize) -> u32 {
ROW_TOP + count.min(19) as u32 * ROW_PITCH
}
pub(super) fn covers_pointer([x, y]: [f32; 2], count: usize) -> bool {
let y_top = panel_top(count);
let y_bottom = (y_top + (RosterOrder::Recharge.slot() + 2) * ROW_PITCH).min(480);
(LEFT as f32..RIGHT as f32).contains(&x) && (y_top as f32..y_bottom as f32).contains(&y)
}
pub(super) struct OrderImages {
stub: RgbaImage,
disabled: RgbaImage,
arrows: [RgbaImage; 2],
}
impl OrderImages {
pub(super) fn load(vfs: &dyn Vfs) -> Result<Self, String> {
let [stub, disabled] = load_ui_sprites(
vfs,
"ui/compaund.cfg",
["ccres_ending_stub", "ccres_long_button_off"],
)?;
let arrows = load_ui_sprites(vfs, "ui/hq.cfg", ["scroll_up_icon", "scroll_down_icon"])?;
Ok(Self {
stub,
disabled,
arrows,
})
}
pub(super) fn paint(
&self,
roster: &RosterImages,
panel: &mut RgbaImage,
count: usize,
[title, stand, follow, search, patrol, recharge]: [&str; 6],
hovered: Option<RosterOrder>,
) -> Result<(), String> {
let x = LEFT - ORIGIN[0];
let y = panel_top(count);
let title = roster.font.centered_line(title, 158, ROW_HEIGHT as u16)?;
for (sprite, left, right) in [
(&self.stub, 0, 9),
(&roster.icon_frame, 8, 167),
(&title, 8, 166),
] {
stamp(
panel,
sprite,
[x + left, y, x + right, y + ROW_HEIGHT],
[255; 3],
);
}
for (index, icon) in self.arrows.iter().enumerate() {
let left = 166 + 50 * index as u32;
stamp(
panel,
&self.disabled,
[x + left, y, x + left + 51, y + ROW_HEIGHT],
[255; 3],
);
let icon_left = x + left + 50_u32.saturating_sub(icon.width) / 2;
let icon_top = y + ROW_HEIGHT.saturating_sub(icon.height) / 2;
stamp(
panel,
icon,
[
icon_left,
icon_top,
icon_left + icon.width,
icon_top + icon.height,
],
[128; 3],
);
}
for (order, label) in ORDERS
.into_iter()
.zip([stand, follow, search, patrol, recharge])
{
let y = y + (order.slot() + 1) * ROW_PITCH;
let bottom = y + ROW_HEIGHT;
let label = roster.font.centered_line(label, 247, ROW_HEIGHT as u16)?;
for (sprite, left, right) in [
(&self.stub, 0, 9),
(&roster.emitter[1], 8, 15),
(&roster.ray, 14, 262),
(&roster.end, 261, 267),
] {
stamp(panel, sprite, [x + left, y, x + right, bottom], [255; 3]);
}
if hovered == Some(order) {
stamp(
panel,
&RgbaImage {
width: 1,
height: 1,
rgba8: vec![0, 128, 0, 128],
},
[x + 16, y + 3, x + 260, bottom - 3],
[255; 3],
);
}
stamp(panel, &label, [x + 14, y, x + 261, bottom], [255; 3]);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hit_test_follows_the_last_warbot_row() {
assert_eq!(order_at([250.0, 197.0], 3), None);
assert_eq!(order_at([250.0, 198.0], 3), Some(RosterOrder::Stand));
assert_eq!(order_at([250.0, 217.0], 3), None);
assert_eq!(order_at([250.0, 218.0], 3), Some(RosterOrder::FollowRoute));
assert_eq!(order_at([250.0, 218.0], 4), Some(RosterOrder::Stand));
assert_eq!(
order_at([250.0, 238.0], 3),
Some(RosterOrder::SearchAndDestroy)
);
assert_eq!(order_at([250.0, 258.0], 3), Some(RosterOrder::Patrol));
assert_eq!(order_at([250.0, 278.0], 3), Some(RosterOrder::Recharge));
}
}
|