guest selection

This commit is contained in:
Niko Abeler 2022-12-31 11:57:55 +01:00
parent 5dee5f499c
commit 1471751231
1 changed files with 47 additions and 2 deletions

View File

@ -1,7 +1,9 @@
use std::rc::Rc;
use crossterm::event::{read, Event, KeyCode}; use crossterm::event::{read, Event, KeyCode};
use tui::{Terminal, backend::Backend, Frame}; use tui::{Terminal, backend::Backend, Frame};
use crate::state::{self, GameState}; use crate::{state::{self, GameState}, person::Person};
/** /**
* |........................| * |........................|
@ -15,6 +17,7 @@ use crate::state::{self, GameState};
enum AppStatus { enum AppStatus {
Initial, Initial,
GuestSelection, GuestSelection,
TalkToGuest(Rc<Person>),
} }
pub struct App<'a> { pub struct App<'a> {
@ -35,7 +38,7 @@ impl<'a> App<'a> {
} }
pub fn step(&mut self) -> bool { pub fn step(&mut self) -> bool {
match self.status { match &self.status {
AppStatus::Initial => { AppStatus::Initial => {
// determine guests // determine guests
self.guest_list = self.state.people.iter().map(|guest| { self.guest_list = self.state.people.iter().map(|guest| {
@ -62,6 +65,11 @@ impl<'a> App<'a> {
self.guest_state.select(Some(selected + 1)); self.guest_state.select(Some(selected + 1));
} }
}, },
KeyCode::Enter => {
let selected = self.guest_state.selected().unwrap();
let guest = &self.state.people[selected];
self.status = AppStatus::TalkToGuest(guest.clone());
},
KeyCode::Esc => { KeyCode::Esc => {
return false; return false;
}, },
@ -71,12 +79,42 @@ impl<'a> App<'a> {
_ => {} _ => {}
} }
true true
},
AppStatus::TalkToGuest(guest) => {
match read() {
Ok(Event::Key(event)) => {
match event.code {
KeyCode::Esc => {
self.status = AppStatus::GuestSelection;
},
_ => {}
}
},
_ => {}
}
true
} }
} }
} }
pub fn draw<B: Backend>(&mut self, f: &mut Frame<B>) { pub fn draw<B: Backend>(&mut self, f: &mut Frame<B>) {
match &self.status {
AppStatus::Initial => {
self.draw_initial(f);
},
AppStatus::GuestSelection => {
self.draw_guest_selection(f);
},
AppStatus::TalkToGuest(guest) => {
self.draw_talk_to_guest(f, guest.clone());
}
}
}
pub fn draw_initial<B: Backend>(&mut self, f: &mut Frame<B>) {}
pub fn draw_guest_selection<B: Backend>(&mut self, f: &mut Frame<B>) {
let main_window = tui::widgets::List::new(self.guest_list.clone()) let main_window = tui::widgets::List::new(self.guest_list.clone())
.block(tui::widgets::Block::default().title("Guests").borders(tui::widgets::Borders::ALL)) .block(tui::widgets::Block::default().title("Guests").borders(tui::widgets::Borders::ALL))
.style(tui::style::Style::default().fg(tui::style::Color::White)) .style(tui::style::Style::default().fg(tui::style::Color::White))
@ -85,4 +123,11 @@ impl<'a> App<'a> {
f.render_stateful_widget(main_window, f.size(), &mut self.guest_state); f.render_stateful_widget(main_window, f.size(), &mut self.guest_state);
} }
fn draw_talk_to_guest<B: Backend>(&self, f: &mut Frame<B>, guest: Rc<Person>) {
let main_window = tui::widgets::Paragraph::new(guest.name.clone())
.block(tui::widgets::Block::default().title("Guests").borders(tui::widgets::Borders::ALL))
.style(tui::style::Style::default().fg(tui::style::Color::White));
f.render_widget(main_window, f.size());
}
} }