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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use log::error;
use std::sync::mpsc::{Receiver, Sender};
use n18game::Game;
use n18hex::{Colour, Hex};
use n18map::Map;
pub mod canvas;
pub mod control;
pub mod keymap;
pub mod state;
#[doc(inline)]
pub use canvas::Canvas;
#[doc(inline)]
pub use control::{Controller, PingSender, UiController};
#[doc(inline)]
pub use keymap::{ButtonPress, KeyPress, Keymap};
#[doc(inline)]
pub use state::{State, UiState};
pub enum PingDest {
TopLevel,
State,
}
pub struct Games {
games: Vec<Box<dyn Game>>,
game_ix: usize,
}
impl Games {
pub fn new<T>(games: T) -> Self
where
T: IntoIterator<Item = Box<dyn Game>>,
{
let games: Vec<_> = games.into_iter().collect();
Games { games, game_ix: 0 }
}
pub fn active(&self) -> &dyn Game {
&*self.games[self.game_ix]
}
pub fn active_mut(&mut self) -> &mut dyn Game {
&mut *self.games[self.game_ix]
}
pub fn names(&self) -> Vec<&str> {
self.games.iter().map(|g| g.name()).collect()
}
pub fn iter(&self) -> impl Iterator<Item = &dyn Game> {
self.games.iter().map(|g| &**g)
}
pub fn set_active_name(&mut self, name: &str) -> bool {
let ix_opt = self
.games
.iter()
.enumerate()
.find(|(_ix, game)| game.name() == name)
.map(|(ix, _game)| ix);
if let Some(ix) = ix_opt {
self.game_ix = ix;
true
} else {
false
}
}
pub fn set_active_index(&mut self, ix: usize) -> bool {
if ix < self.games.len() {
self.game_ix = ix;
true
} else {
false
}
}
}
pub struct Assets {
pub hex: Hex,
pub map: Map,
pub games: Games,
}
pub struct UiAction {
action: Action,
}
enum Action {
NewGame(usize),
LoadGame(std::path::PathBuf),
SaveGame(std::path::PathBuf),
SaveImage(std::path::PathBuf, cairo::ImageSurface),
}
impl From<UiAction> for Action {
fn from(action: UiAction) -> Self {
action.action
}
}
impl From<Action> for UiAction {
fn from(action: Action) -> Self {
UiAction { action }
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum UiResponse {
None,
Quit,
Redraw,
ResetGame,
ZoomIn,
ZoomOut,
}
pub struct UserInterface {
pub assets: Assets,
pub state: State,
pub controller: Controller,
pub keymap: Keymap,
pub canvas: Canvas,
sender: Sender<UiAction>,
receiver: Receiver<UiAction>,
min_hex_diameter: f64,
max_hex_diameter: f64,
}
impl UserInterface {
pub fn new<T, C>(games: T, controller: C, keymap: Keymap) -> Self
where
T: IntoIterator<Item = Box<dyn Game>>,
C: Into<Controller>,
{
let games = Games::new(games);
let start_state = state::start::Start::new();
let map = start_state.dummy_map();
let controller = controller.into();
let hex = Hex::default();
let min_hex_diameter: f64 = 56.0;
let max_hex_diameter: f64 = 164.0;
let mut assets = Assets { hex, map, games };
let state = State::Start(start_state);
let dims =
canvas::max_surface_dims(&state, &mut assets, max_hex_diameter);
let canvas = Canvas::new(dims.0, dims.1);
n18brush::clear_surface(canvas.context(), Colour::WHITE);
let (sender, receiver) = std::sync::mpsc::channel();
UserInterface {
assets,
state,
controller,
keymap,
canvas,
sender,
receiver,
min_hex_diameter,
max_hex_diameter,
}
}
pub fn draw(&self) {
self.state.draw(&self.assets, self.canvas.context())
}
pub fn min_hex_diameter(&self) -> f64 {
self.min_hex_diameter
}
pub fn max_hex_diameter(&self) -> f64 {
self.max_hex_diameter
}
#[must_use = "pass the response to UserInterface::respond"]
pub fn handle_key_press(&mut self, event: &KeyPress) -> UiResponse {
let response = self.keymap.handle_key_press(
&mut self.assets,
&mut self.controller,
&mut self.state,
&mut self.canvas,
&self.sender,
event,
);
if let Some((response, new_state_opt)) = response {
if let Some(new_state) = new_state_opt {
if new_state.is_default_state() {
self.set_default_title();
}
self.state = new_state
}
response
} else {
UiResponse::None
}
}
#[must_use = "pass the response to UserInterface::respond"]
pub fn handle_button_press(&mut self, event: &ButtonPress) -> UiResponse {
let response = self.keymap.handle_button_press(
&mut self.assets,
&mut self.controller,
&mut self.state,
&mut self.canvas,
&self.sender,
event,
);
if let Some((response, new_state_opt)) = response {
if let Some(new_state) = new_state_opt {
if new_state.is_default_state() {
self.set_default_title();
}
self.state = new_state
}
response
} else {
UiResponse::None
}
}
pub fn respond(&mut self, response: UiResponse) {
match response {
UiResponse::ZoomIn => {
let new_max_d = self.assets.hex.max_d + 10.0;
if new_max_d < self.max_hex_diameter() {
self.assets.hex.resize(new_max_d);
self.zoom_and_redraw();
}
}
UiResponse::ZoomOut => {
let new_max_d = self.assets.hex.max_d - 10.0;
if new_max_d > self.min_hex_diameter() {
self.assets.hex.resize(new_max_d);
self.zoom_and_redraw();
}
}
UiResponse::Redraw => {
self.draw();
self.controller.redraw();
}
UiResponse::ResetGame => {
self.reset_and_redraw();
}
UiResponse::Quit => self.controller.quit(),
UiResponse::None => {}
}
}
pub fn map_size(&self) -> (i32, i32) {
canvas::required_dims(&self.state, &self.assets)
}
pub fn zoom_and_redraw(&mut self) {
let (width, height) =
canvas::required_dims(&self.state, &self.assets);
self.controller.resize(width, height);
self.draw();
self.controller.redraw();
}
pub fn reset_and_redraw(&mut self) {
let hex_d = self.max_hex_diameter();
let (width, height) =
canvas::max_surface_dims(&self.state, &mut self.assets, hex_d);
let curr_width = self.canvas.width();
let curr_height = self.canvas.height();
let resize = (curr_width < width) || (curr_height < height);
if resize {
self.canvas.resize(width, height);
n18brush::clear_surface(self.canvas.context(), Colour::WHITE);
}
self.zoom_and_redraw()
}
pub fn set_default_title(&mut self) {
let title = self.assets.games.active().name();
self.controller.set_window_title(title);
}
#[must_use = "pass the response to UserInterface::respond"]
pub fn ping(&mut self, dest: PingDest) -> UiResponse {
match dest {
PingDest::State => {
let (response, new_state_opt) =
self.state.ping(&mut self.assets, &mut self.controller);
if let Some(new_state) = new_state_opt {
if new_state.is_default_state() {
self.set_default_title();
}
self.state = new_state;
}
response
}
PingDest::TopLevel => {
let msg = self.receiver.recv().unwrap();
let action = msg.into();
match action {
Action::NewGame(game_ix) => self.new_game(game_ix),
Action::LoadGame(path) => self.load_game(path),
Action::SaveGame(path) => self.save_game(path),
Action::SaveImage(path, image) => {
self.save_image(path, image)
}
}
}
}
}
#[must_use = "pass the response to UserInterface::respond"]
pub fn new_game(&mut self, game_ix: usize) -> UiResponse {
if self.assets.games.set_active_index(game_ix) {
self.assets.map =
self.assets.games.active().create_map(&self.assets.hex);
self.assets
.hex
.set_orientation(self.assets.map.orientation());
self.set_default_title();
let active_hex = self.assets.map.default_hex();
self.state = State::default_state(active_hex);
UiResponse::ResetGame
} else {
UiResponse::None
}
}
#[must_use = "pass the response to UserInterface::respond"]
pub fn load_game(&mut self, path: std::path::PathBuf) -> UiResponse {
let game_state = n18io::read_game_state(&path).unwrap_or_else(|_| {
panic!("Could not read '{}'", path.display())
});
if self.assets.games.set_active_name(&game_state.game) {
if let Some(new_map) = self
.assets
.games
.active_mut()
.load(&self.assets.hex, game_state)
{
self.assets.hex.set_orientation(new_map.orientation());
self.set_default_title();
self.assets.map = new_map;
let active_hex = self.assets.map.default_hex();
self.state = State::default_state(active_hex);
UiResponse::ResetGame
} else {
error!("game.load() returned None");
UiResponse::None
}
} else {
error!("no game called '{}'", game_state.game);
UiResponse::None
}
}
#[must_use = "pass the response to UserInterface::respond"]
pub fn save_game(&mut self, path: std::path::PathBuf) -> UiResponse {
let game_state = self.assets.games.active().save(&self.assets.map);
n18io::write_game_state(&path, game_state, true).unwrap_or_else(
|_| panic!("Could not write '{}'", path.display()),
);
UiResponse::None
}
#[must_use = "pass the response to UserInterface::respond"]
pub fn save_image(
&self,
path: std::path::PathBuf,
image: cairo::ImageSurface,
) -> UiResponse {
let mut file = std::fs::File::create(&path).unwrap_or_else(|_| {
panic!("Couldn't create '{}'", path.display())
});
image.write_to_png(&mut file).unwrap_or_else(|_| {
panic!("Couldn't write '{}'", path.display())
});
UiResponse::None
}
}