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
use std::io::{self, Write};
use crossterm::{
cursor::MoveTo,
event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
style::{Color, Print, PrintStyledContent, Stylize},
terminal::{Clear, ClearType},
QueueableCommand,
};
use crate::application::{Application, Menu, MenuUpdate};
impl<T: Write> Application<T> {
pub(in crate::application) fn run_menu_title(&mut self) -> io::Result<MenuUpdate> {
let selection = vec![
Menu::NewGame,
Menu::Settings,
Menu::ScoresAndReplays,
Menu::About,
Menu::Quit,
];
let mut selected = 0usize;
loop {
let w_main: usize = Self::W_MAIN.into();
let (x_main, y_main) = Self::fetch_main_xy();
let y_selection = Self::H_MAIN / 5;
let title = [
"▄▄▄▄▄▄▄ ▄▄▄▄ ▄▄▄▄▄▄▄ ▄▄▄▄ ▄▄▄▄ ",
" ▄▀ █▄▄ ▄▀ ▄█▄▄▀ ▄█ ▄█",
" █▀ █▄▄▄▄▄▄ █▀ █▀ ▀█ ▀▄▄▄▄▀ ",
];
let title_colors = [
"1111555 1111 1111555 5666 1111 ",
" 35 666 35 35526 33 33",
" 33 6661111 33 33 22 311113 ",
];
self.term.queue(Clear(ClearType::All))?;
let dx_title = w_main.saturating_sub(36) / 2;
for (dy, (bline, cline)) in title.iter().zip(title_colors).enumerate() {
for (dx, (bchar, cchar)) in bline.chars().zip(cline.chars()).enumerate() {
self.term.queue(MoveTo(
x_main + u16::try_from(dx_title + dx).unwrap(),
y_main + y_selection + u16::try_from(dy).unwrap(),
))?;
self.term.queue(PrintStyledContent(bchar.to_string().with(
if cchar == ' ' {
Color::Reset
} else {
*self
.settings
.palette()
.get(
&falling_tetromino_engine::Tetromino::VARIANTS
[cchar.to_string().parse::<usize>().unwrap()]
.tiletypeid()
.get(),
)
.unwrap_or(&Color::Reset)
},
)))?;
}
}
let names = selection
.iter()
.map(|menu| menu.to_string())
.collect::<Vec<_>>();
let n_names = names.len();
for (i, name) in names.into_iter().enumerate() {
self.term
.queue(MoveTo(
x_main,
y_main + y_selection + 5 + u16::try_from(i).unwrap(),
))?
.queue(Print(format!(
"{:^w_main$}",
if i == selected {
format!(">> {name} <<")
} else {
name
}
)))?;
}
self.term
.queue(MoveTo(
x_main,
y_main + y_selection + 5 + u16::try_from(n_names).unwrap() + 2,
))?
.queue(PrintStyledContent(
format!(
"{:^w_main$}",
"(Controls: [←|↓|↑|→] [Esc|Enter|Del] / hjklqed)",
)
.italic(),
))?;
self.term.flush()?;
// Wait for new input.
match event::read()? {
// Quit menu.
Event::Key(KeyEvent {
code: KeyCode::Char('c' | 'C'),
modifiers: KeyModifiers::CONTROL,
kind: KeyEventKind::Press | KeyEventKind::Repeat,
state: _,
}) => break Ok(MenuUpdate::Push(Menu::Quit)),
Event::Key(KeyEvent {
code:
KeyCode::Esc
| KeyCode::Char('q' | 'Q')
| KeyCode::Backspace
| KeyCode::Char('b' | 'B'),
kind: KeyEventKind::Press,
..
}) => {
selected = 4;
}
// Select next menu.
Event::Key(KeyEvent {
code: KeyCode::Enter | KeyCode::Char('e' | 'E'),
kind: KeyEventKind::Press,
..
}) => {
if !selection.is_empty() {
let menu = selection.into_iter().nth(selected).unwrap();
break Ok(MenuUpdate::Push(menu));
}
}
// Move selector up.
Event::Key(KeyEvent {
code: KeyCode::Up | KeyCode::Char('k' | 'K'),
kind: KeyEventKind::Press | KeyEventKind::Repeat,
..
}) => {
if !selection.is_empty() {
selected += selection.len() - 1;
}
}
// Move selector down.
Event::Key(KeyEvent {
code: KeyCode::Down | KeyCode::Char('j' | 'J'),
kind: KeyEventKind::Press | KeyEventKind::Repeat,
..
}) => {
if !selection.is_empty() {
selected += 1;
}
}
// Other event: don't care.
_ => {}
}
if !selection.is_empty() {
selected = selected.rem_euclid(selection.len());
}
}
}
}