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
310
311
312
313
314
315
316
317
318
319
320
|
//! Mission selection before loading a world into the game window.
use fparkan_assets::{
game_font::{load_menu_font, BitmapFont},
mission_catalog::MissionCatalog,
};
use fparkan_vfs::Vfs;
use winit::{
dpi::{PhysicalPosition, PhysicalSize},
event::{ElementState, MouseButton, WindowEvent},
keyboard::{Key, NamedKey},
window::Window,
};
const EXTENT: [u32; 2] = [960, 540];
const LEFT: usize = 56;
const TOP: usize = 132;
const WIDTH: usize = 848;
const ROW_HEIGHT: usize = 42;
const VISIBLE_ROWS: usize = 8;
const GAP: usize = 6;
const BACKGROUND: u32 = 0x141c24;
pub(super) struct LaunchMenu {
catalog: MissionCatalog,
font: BitmapFont,
screen: Screen,
selected: usize,
pointer: Option<PhysicalPosition<f64>>,
}
#[derive(Clone, Copy)]
enum Screen {
Main,
Start,
Campaigns,
Campaign(usize),
Single,
}
struct Row<'a> {
label: &'a str,
action: Action<'a>,
}
#[derive(Clone, Copy)]
enum Action<'a> {
Open(Screen),
Launch(&'a str),
Exit,
}
pub(super) enum MenuEvent {
None,
Redraw,
Launch(String),
Exit,
}
impl LaunchMenu {
pub(super) fn open(vfs: &dyn Vfs) -> Result<Self, String> {
if !cfg!(windows) {
return Err("the mission menu requires a Win32 window; specify --mission".into());
}
let catalog = MissionCatalog::load(vfs)?;
if catalog.campaigns.is_empty() && catalog.single.is_empty() && catalog.demo.is_none() {
return Err("no missions with a descr caption found in the game directory".into());
}
Ok(Self {
catalog,
font: load_menu_font(vfs)?,
screen: Screen::Main,
selected: 0,
pointer: None,
})
}
fn rows(&self) -> Vec<Row<'_>> {
let mut rows = Vec::new();
match self.screen {
Screen::Main => {
if let Some(demo) = &self.catalog.demo {
rows.push(Row {
label: "Демо",
action: Action::Launch(&demo.key),
});
}
if !self.catalog.campaigns.is_empty() || !self.catalog.single.is_empty() {
rows.push(Row {
label: "Начать игру",
action: Action::Open(Screen::Start),
});
}
rows.push(Row {
label: "Выход",
action: Action::Exit,
});
}
Screen::Start => {
if !self.catalog.campaigns.is_empty() {
rows.push(Row {
label: "Кампании",
action: Action::Open(Screen::Campaigns),
});
}
if !self.catalog.single.is_empty() {
rows.push(Row {
label: "Одиночные миссии",
action: Action::Open(Screen::Single),
});
}
}
Screen::Campaigns => {
rows.extend(
self.catalog
.campaigns
.iter()
.enumerate()
.map(|(index, campaign)| Row {
label: &campaign.title,
action: Action::Open(Screen::Campaign(index)),
}),
);
}
Screen::Campaign(index) => {
rows.extend(
self.catalog.campaigns[index]
.missions
.iter()
.map(|mission| Row {
label: &mission.title,
action: Action::Launch(&mission.key),
}),
);
}
Screen::Single => {
rows.extend(self.catalog.single.iter().map(|mission| Row {
label: &mission.title,
action: Action::Launch(&mission.key),
}));
}
}
if let Some(parent) = self.parent() {
rows.push(Row {
label: "Назад",
action: Action::Open(parent),
});
}
rows
}
fn parent(&self) -> Option<Screen> {
match self.screen {
Screen::Main => None,
Screen::Start => Some(Screen::Main),
Screen::Campaigns | Screen::Single => Some(Screen::Start),
Screen::Campaign(_) => Some(Screen::Campaigns),
}
}
fn enter(&mut self, screen: Screen) -> MenuEvent {
self.screen = screen;
self.selected = 0;
MenuEvent::Redraw
}
fn activate(&mut self, index: usize) -> MenuEvent {
match self.rows().get(index).map(|row| row.action) {
Some(Action::Open(screen)) => self.enter(screen),
Some(Action::Launch(key)) => MenuEvent::Launch(key.to_string()),
Some(Action::Exit) => MenuEvent::Exit,
None => MenuEvent::None,
}
}
pub(super) fn input(&mut self, event: &WindowEvent, size: PhysicalSize<u32>) -> MenuEvent {
match event {
WindowEvent::KeyboardInput { event, .. } if event.state == ElementState::Pressed => {
match event.logical_key {
Key::Named(NamedKey::ArrowUp | NamedKey::ArrowDown) => {
let count = self.rows().len();
let step = if event.logical_key == Key::Named(NamedKey::ArrowUp) {
count - 1
} else {
1
};
self.selected = (self.selected + step) % count;
MenuEvent::Redraw
}
Key::Named(NamedKey::Enter) if !event.repeat => self.activate(self.selected),
Key::Named(NamedKey::Escape) if !event.repeat => match self.parent() {
Some(parent) => self.enter(parent),
None => MenuEvent::Exit,
},
_ => MenuEvent::None,
}
}
WindowEvent::CursorMoved { position, .. } => {
self.pointer = Some(*position);
match self.hit(size) {
Some(index) if index != self.selected => {
self.selected = index;
MenuEvent::Redraw
}
_ => MenuEvent::None,
}
}
WindowEvent::CursorLeft { .. } | WindowEvent::Resized(_) => {
self.pointer = None;
MenuEvent::None
}
WindowEvent::MouseInput {
state: ElementState::Pressed,
button: MouseButton::Left,
..
} => match self.hit(size) {
Some(index) => self.activate(index),
None => MenuEvent::None,
},
_ => MenuEvent::None,
}
}
fn hit(&self, size: PhysicalSize<u32>) -> Option<usize> {
let cursor = self.pointer?;
if size.width == 0 || size.height == 0 {
return None;
}
let x = cursor.x * f64::from(EXTENT[0]) / f64::from(size.width);
let y = cursor.y * f64::from(EXTENT[1]) / f64::from(size.height);
if x < LEFT as f64 || x >= (LEFT + WIDTH) as f64 || y < TOP as f64 {
return None;
}
let row = (y as usize - TOP) / ROW_HEIGHT;
let first = self.first_row();
(row < VISIBLE_ROWS
&& (y as usize - TOP) % ROW_HEIGHT < ROW_HEIGHT - GAP
&& first + row < self.rows().len())
.then_some(first + row)
}
fn first_row(&self) -> usize {
self.selected.saturating_sub(VISIBLE_ROWS - 1)
}
pub(super) fn paint(&self, _window: &Window) -> Result<(), String> {
#[cfg(windows)]
{
let pixels = self.frame()?;
fparkan_platform_winit::startup_window::paint(_window, EXTENT, &pixels)
.map_err(|error| format!("draw mission menu: {error}"))?;
}
Ok(())
}
fn frame(&self) -> Result<Vec<u32>, String> {
let mut frame = vec![BACKGROUND; EXTENT[0] as usize * EXTENT[1] as usize];
let title = match self.screen {
Screen::Main => "FParkan",
Screen::Start => "Начать игру",
Screen::Campaigns => "Кампании",
Screen::Campaign(index) => &self.catalog.campaigns[index].title,
Screen::Single => "Одиночные миссии",
};
self.text(&mut frame, title, [LEFT, 57], 2)?;
let rows = self.rows();
let first = self.first_row();
for (visible, row) in rows[first..].iter().take(VISIBLE_ROWS).enumerate() {
let top = TOP + visible * ROW_HEIGHT;
let selected = first + visible == self.selected;
let color = if selected { 0x205960 } else { 0x26333d };
fill(&mut frame, LEFT, top, WIDTH, ROW_HEIGHT - GAP, color);
if selected {
fill(&mut frame, LEFT, top, 4, ROW_HEIGHT - GAP, 0x65dacc);
}
self.text(&mut frame, row.label, [LEFT + 20, top + 9], 1)?;
}
Ok(frame)
}
fn text(
&self,
frame: &mut [u32],
label: &str,
at: [usize; 2],
scale: usize,
) -> Result<(), String> {
let [width, height] = self.font.line_metrics(label)?;
let source = self.font.centered_line(label, width, height)?;
for y in 0..source.height as usize * scale {
if at[1] + y >= EXTENT[1] as usize {
break;
}
for x in 0..(source.width as usize * scale).min(LEFT + WIDTH - at[0]) {
let src = (y / scale * source.width as usize + x / scale) * 4;
let index = (at[1] + y) * EXTENT[0] as usize + at[0] + x;
let alpha = u32::from(source.rgba8[src + 3]);
if alpha == 0 {
continue;
}
let background = frame[index];
let mut pixel = 0;
for (channel, shift) in [16, 8, 0].into_iter().enumerate() {
let foreground = u32::from(source.rgba8[src + channel]);
let back = (background >> shift) & 0xff;
pixel |= ((foreground * alpha + back * (255 - alpha)) / 255) << shift;
}
frame[index] = pixel;
}
}
Ok(())
}
}
fn fill(frame: &mut [u32], x: usize, y: usize, width: usize, height: usize, color: u32) {
for row in y..y + height {
let start = row * EXTENT[0] as usize + x;
frame[start..start + width].fill(color);
}
}
|