forked from CleanCut/invaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.rs
41 lines (36 loc) · 990 Bytes
/
menu.rs
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
use crate::frame::{Drawable, Frame};
pub struct Menu {
pub options: Vec<String>,
pub selection: usize,
}
impl Menu {
pub fn new() -> Self {
Self {
options: vec![String::from("New game"), String::from("Exit")],
selection: 0,
}
}
pub fn change_option(&mut self, upwards: bool) {
if upwards && self.selection > 0 {
self.selection -= 1;
} else if !upwards && self.selection < self.options.len() - 1 {
self.selection += 1;
}
}
}
impl Default for Menu {
fn default() -> Self {
Self::new()
}
}
// Reuse Frame grid to print the menu options
impl Drawable for Menu {
fn draw(&self, frame: &mut Frame) {
frame[0][self.selection] = '>';
for (index, option) in self.options.iter().enumerate() {
for i in 0..option.len() {
frame[i + 1][index] = self.options[index].chars().nth(i).unwrap();
}
}
}
}