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
use std::io::{self, Write};
use crossterm::{
cursor::{self, MoveTo},
event::{
self, Event, KeyCode, KeyEvent,
KeyEventKind::{Press, Repeat},
KeyModifiers,
},
style::{Print, PrintStyledContent, Stylize},
terminal::{Clear, ClearType},
ExecutableCommand, QueueableCommand,
};
use falling_tetromino_engine::Button;
use crate::{
fmt_helpers::fmt_button_keybinds,
tui_menus::{title_bar, Menu, MenuUpdate},
tui_settings::GameKeybinds,
Application, Settings,
};
impl<T: Write> Application<T> {
pub fn run_menu_adjust_keybinds(&mut self) -> io::Result<MenuUpdate> {
let if_unmodifiable_clone_and_switch = |s: &mut Settings| {
if let Some(cloned_slot_idx) = s
.keybinds_slotmachine
.clone_slot_if_unmodifiable(s.keybinds_selected)
{
s.keybinds_selected = cloned_slot_idx;
}
};
let buttons_available = Button::VARIANTS;
// +1 for available slot selection.
let selection_len = 1 + buttons_available.len();
// Go to actual keybind selection on menu entry.
let mut selected = 1usize;
loop {
let w_main = Self::W_MAIN.into();
let (x_main, y_main) = Self::viewport_offset();
let y_selection = (Self::H_MAIN / 5).saturating_sub(2);
// Draw menu title.
self.term
.queue(Clear(ClearType::All))?
.queue(MoveTo(x_main, y_main + y_selection))?
.queue(PrintStyledContent(
format!("{:^w_main$}", "@ Keybinds @").bold(),
))?
.queue(MoveTo(x_main, y_main + y_selection + 2))?
.queue(Print(format!("{:^w_main$}", title_bar(&self.settings))))?;
// Draw slot label.
let slot_label = format!(
"Slot {}/{}: '{}'{}",
self.settings.keybinds_selected + 1,
self.settings.keybinds_slotmachine.slots.len(),
self.settings
.keybinds_slotmachine
.grab(self.settings.keybinds_selected)
.0,
if self.settings.keybinds_slotmachine.slots.len() < 2 {
"".to_owned()
} else {
format!(
" [←|{}→] ",
if self.settings.keybinds_selected
< self.settings.keybinds_slotmachine.unmodifiable_slots
{
""
} else {
"Del|"
}
)
}
);
self.term
.queue(MoveTo(x_main, y_main + y_selection + 3))?
.queue(Print(format!(
"{:^w_main$}",
if selected == 0 {
format!(">> {slot_label} <<")
} else {
slot_label
}
)))?
.queue(MoveTo(x_main, y_main + y_selection + 4))?
.queue(Print(format!("{:^w_main$}", title_bar(&self.settings))))?;
// Draw keybinds selection.
let button_names = buttons_available.iter().map(|&button| {
format!(
"{button:?}: {}",
fmt_button_keybinds(button, self.settings.keybinds())
)
});
for (i, name) in button_names.enumerate() {
self.term
.queue(MoveTo(
x_main,
y_main + y_selection + 6 + u16::try_from(i).unwrap(),
))?
.queue(Print(format!(
"{:^w_main$}",
// +1 because the first button is Slot selection.
if i + 1 == selected {
format!(">> {name} <<")
} else {
name
}
)))?;
}
// Draw footer legend.
self.term
.queue(MoveTo(
x_main,
y_main + y_selection + 6 + u16::try_from(buttons_available.len()).unwrap() + 1,
))?
.queue(PrintStyledContent(
format!(
"{:^w_main$}",
"(Controls: [Enter]=add [Esc]=cancel [Del]=clear)",
)
.italic(),
))?;
self.term.flush()?;
// Wait for new input.
match event::read()? {
// Exit program.
Event::Key(KeyEvent {
code: KeyCode::Char('c' | 'C'),
modifiers: KeyModifiers::CONTROL,
kind: Press | Repeat,
state: _,
}) => break Ok(MenuUpdate::Push(Menu::Quit)),
// Quit menu.
Event::Key(KeyEvent {
code: KeyCode::Esc | KeyCode::Char('q' | 'Q') | KeyCode::Backspace,
kind: Press,
..
}) => break Ok(MenuUpdate::Pop),
// Modify keybind.
Event::Key(KeyEvent {
code: KeyCode::Enter | KeyCode::Char('e' | 'E'),
kind: Press,
..
}) => {
// `> 0` because 0 is slot selection.
if selected > 0 {
let current_button = buttons_available[selected - 1];
self.term
.execute(MoveTo(
x_main,
y_main
+ y_selection
+ 4
+ u16::try_from(selection_len).unwrap()
+ 2,
))?
.execute(PrintStyledContent(
format!(
"{:^w_main$}",
format!("Press a key for {current_button:?}..."),
)
.italic(),
))?
.execute(cursor::MoveToNextLine(1))?
.execute(Clear(ClearType::CurrentLine))?;
// Wait until appropriate keypress detected.
if self.temp_data.kitty_assumed {
let f = Self::GAME_KEYBOARD_ENHANCEMENT_FLAGS;
// NOTE: Explicitly ignore an error when pushing flags. This is so we can still try even if Crossterm minds if we do this on Windows.
let _v = self.term.execute(event::PushKeyboardEnhancementFlags(f));
}
loop {
if let Event::Key(KeyEvent {
code,
modifiers,
kind: Press,
..
}) = event::read()?
{
// Add key pressed unless it's [Esc] or [Ctrl+C].
if matches!(
(code, modifiers),
(KeyCode::Char('c' | 'C'), KeyModifiers::CONTROL)
) {
return Ok(MenuUpdate::Push(Menu::Quit));
} else if !matches!(code, KeyCode::Esc) {
if_unmodifiable_clone_and_switch(&mut self.settings);
self.settings.keybinds_mut().unstable_access().insert(
GameKeybinds::normalize((code, modifiers)),
current_button,
);
}
break;
}
}
// Console epilogue: De-initialization.
if self.temp_data.kitty_assumed {
// NOTE: Explicitly ignore an error when pushing flags. This is so we can still try even if Crossterm minds if we do this on Windows.
let _v = self.term.execute(event::PopKeyboardEnhancementFlags);
}
}
}
// Delete keybind, or entire slot.
Event::Key(KeyEvent {
code: KeyCode::Delete | KeyCode::Char('d' | 'D'),
kind: Press,
..
}) => {
if selected == 0 {
// If a custom slot, then remove it (and return to the 'default' 0th slot).
if self.settings.keybinds_selected
>= self.settings.keybinds_slotmachine.unmodifiable_slots
{
self.settings
.keybinds_slotmachine
.slots
.remove(self.settings.keybinds_selected);
self.settings.keybinds_selected = 0;
}
} else {
// Trying to modify a default slot: create copy of slot to allow safely modifying that.
if let Some(cloned_slot_idx) = self
.settings
.keybinds_slotmachine
.clone_slot_if_unmodifiable(self.settings.keybinds_selected)
{
self.settings.keybinds_selected = cloned_slot_idx;
}
// Remove all keys bound to the selected action button.
let button_selected = buttons_available[selected - 1];
self.settings
.keybinds_mut()
.unstable_access()
.retain(|_code, button| *button != button_selected);
}
}
// Move selector up.
Event::Key(KeyEvent {
code: KeyCode::Up | KeyCode::Char('k' | 'K'),
kind: Press | Repeat,
..
}) => {
selected += selection_len - 1;
}
// Move selector down.
Event::Key(KeyEvent {
code: KeyCode::Down | KeyCode::Char('j' | 'J'),
kind: Press | Repeat,
..
}) => {
selected += 1;
}
// Reload from savefile.
Event::Key(KeyEvent {
code: KeyCode::Char('l' | 'L'),
modifiers,
kind: Press | Repeat,
..
}) if { modifiers.contains(KeyModifiers::CONTROL.union(KeyModifiers::ALT)) } => {
self.temp_data.loadfile_result = self.savefile_load();
}
// Store to savefile.
Event::Key(KeyEvent {
code: KeyCode::Char('s' | 'S'),
modifiers,
kind: Press | Repeat,
..
}) if { modifiers.contains(KeyModifiers::CONTROL.union(KeyModifiers::ALT)) } => {
self.temp_data.storefile_result = self.savefile_store();
}
// Cycle slot to right.
Event::Key(KeyEvent {
code: KeyCode::Right | KeyCode::Char('l' | 'L'),
kind: Press | Repeat,
..
}) => {
if selected == 0 {
self.settings.keybinds_selected += 1;
self.settings.keybinds_selected %=
self.settings.keybinds_slotmachine.slots.len();
}
}
// Cycle slot to right.
Event::Key(KeyEvent {
code: KeyCode::Left | KeyCode::Char('h' | 'H'),
kind: Press | Repeat,
..
}) => {
if selected == 0 {
self.settings.keybinds_selected +=
self.settings.keybinds_slotmachine.slots.len() - 1;
self.settings.keybinds_selected %=
self.settings.keybinds_slotmachine.slots.len();
}
}
// Other IO event: no action.
_ => {}
}
selected %= selection_len;
}
}
}