tavern_keeper/src/ui/mod.rs

350 lines
13 KiB
Rust
Raw Normal View History

2023-01-04 18:41:31 +00:00
mod chat;
2023-01-05 14:28:58 +00:00
mod controls;
2023-01-04 18:41:31 +00:00
2022-12-31 10:44:42 +00:00
use crossterm::event::{read, Event, KeyCode};
2022-12-31 14:22:44 +00:00
use tui::{backend::Backend, Frame, layout::Layout};
2022-12-31 10:44:42 +00:00
2023-01-04 13:54:57 +00:00
use crate::{game::Game, entity::EntityId};
2022-12-31 10:44:42 +00:00
2023-01-05 14:28:58 +00:00
use self::{chat::{Chat, ChatLine}, controls::Controls};
2023-01-04 18:41:31 +00:00
2022-12-31 10:44:42 +00:00
/**
* |........................|
* | Conversations/Selection|
* | |
* |........................|
* | Available Options |
* |........................|
*/
enum AppStatus {
Initial,
GuestSelection,
2022-12-31 14:22:44 +00:00
TalkToGuest(Option<u32>),
2023-01-05 20:01:11 +00:00
Debug,
}
struct DebugData<'a> {
list: Vec<tui::widgets::ListItem<'a>>,
list_state: tui::widgets::ListState,
2022-12-31 10:44:42 +00:00
}
pub struct App<'a> {
2023-01-01 20:11:27 +00:00
game: Game,
2023-01-05 20:01:11 +00:00
status: AppStatus,
// Guest Selection
2022-12-31 10:44:42 +00:00
guest_list: Vec<tui::widgets::ListItem<'a>>,
2022-12-31 15:04:07 +00:00
guest_list_state: tui::widgets::ListState,
2023-01-05 20:01:11 +00:00
// Conversation
2023-01-04 18:41:31 +00:00
conversation: Chat,
2023-01-05 14:28:58 +00:00
conversation_scroll: u16,
2023-01-05 20:01:11 +00:00
// Debug
dabug_data: Option<DebugData<'a>>,
2022-12-31 10:44:42 +00:00
}
impl<'a> App<'a> {
2023-01-01 20:11:27 +00:00
pub fn new(state: Game) -> App<'a> {
2022-12-31 10:44:42 +00:00
App {
2023-01-01 20:11:27 +00:00
game: state,
2022-12-31 10:44:42 +00:00
guest_list: vec![],
2022-12-31 15:04:07 +00:00
guest_list_state: tui::widgets::ListState::default(),
2022-12-31 10:44:42 +00:00
status: AppStatus::Initial,
2023-01-04 18:41:31 +00:00
conversation: Chat::new(),
2023-01-05 14:28:58 +00:00
conversation_scroll: 0,
2023-01-05 20:01:11 +00:00
dabug_data: None,
2022-12-31 10:44:42 +00:00
}
}
pub fn step(&mut self) -> bool {
2022-12-31 10:57:55 +00:00
match &self.status {
2022-12-31 10:44:42 +00:00
AppStatus::Initial => {
// determine guests
2023-01-04 13:41:32 +00:00
self.guest_list = self.game.state.guests().iter().map(|creature_id| {
let creature = self.game.state.get_creature(*creature_id).unwrap();
tui::widgets::ListItem::new(creature.name.clone())
}).collect();
2022-12-31 15:04:07 +00:00
self.guest_list_state = tui::widgets::ListState::default();
self.guest_list_state.select(Some(0));
2022-12-31 10:44:42 +00:00
self.status = AppStatus::GuestSelection;
true
},
AppStatus::GuestSelection => {
match read() {
Ok(Event::Key(event)) => {
match event.code {
KeyCode::Up => {
2022-12-31 15:04:07 +00:00
let selected = self.guest_list_state.selected().unwrap();
2022-12-31 10:44:42 +00:00
if selected > 0 {
2022-12-31 15:04:07 +00:00
self.guest_list_state.select(Some(selected - 1));
2022-12-31 10:44:42 +00:00
}
},
KeyCode::Down => {
2022-12-31 15:04:07 +00:00
let selected = self.guest_list_state.selected().unwrap();
2022-12-31 10:44:42 +00:00
if selected < self.guest_list.len() - 1 {
2022-12-31 15:04:07 +00:00
self.guest_list_state.select(Some(selected + 1));
2022-12-31 10:44:42 +00:00
}
},
2022-12-31 10:57:55 +00:00
KeyCode::Enter => {
2023-01-05 14:28:58 +00:00
if self.game.state.guests().len() > 0 {
match self.guest_list_state.selected() {
Some(selected) => {
let guest_id = &self.game.state.guests()[selected];
self.conversation = Chat::new();
self.greet_guest(Some(*guest_id));
self.status = AppStatus::TalkToGuest(Some(*guest_id));
},
None => {}
}
}
2022-12-31 10:57:55 +00:00
},
2023-01-05 20:01:11 +00:00
KeyCode::Char('?') => {
self.open_debug();
},
2022-12-31 13:03:24 +00:00
KeyCode::Char('.') => {
2023-01-01 20:11:27 +00:00
self.game.step();
2022-12-31 13:03:24 +00:00
self.status = AppStatus::Initial;
},
2022-12-31 10:44:42 +00:00
KeyCode::Esc => {
return false;
},
_ => {}
}
},
_ => {}
}
true
2022-12-31 10:57:55 +00:00
},
AppStatus::TalkToGuest(guest) => {
match read() {
Ok(Event::Key(event)) => {
match event.code {
KeyCode::Esc => {
self.status = AppStatus::GuestSelection;
},
2022-12-31 15:04:07 +00:00
KeyCode::Char('a') => {
self.ask_business(*guest);
},
2023-01-05 14:28:58 +00:00
KeyCode::Up => {
self.conversation_scroll += 1;
},
KeyCode::Down => {
if self.conversation_scroll > 0 {
self.conversation_scroll -= 1;
}
},
2022-12-31 10:57:55 +00:00
_ => {}
}
},
_ => {}
}
true
2023-01-05 20:01:11 +00:00
},
AppStatus::Debug => {
match read() {
Ok(Event::Key(event)) => {
match event.code {
KeyCode::Esc => {
self.status = AppStatus::GuestSelection;
},
KeyCode::Up => {
let selected = self.dabug_data.as_mut().unwrap().list_state.selected().unwrap();
if selected > 0 {
self.dabug_data.as_mut().unwrap().list_state.select(Some(selected - 1));
}
},
KeyCode::Down => {
let selected = self.dabug_data.as_mut().unwrap().list_state.selected().unwrap();
if selected < self.dabug_data.as_ref().unwrap().list.len() - 1 {
self.dabug_data.as_mut().unwrap().list_state.select(Some(selected + 1));
}
},
_ => {}
}
},
_ => {}
}
true
2022-12-31 10:44:42 +00:00
}
}
}
2023-01-05 20:01:11 +00:00
fn open_debug(&mut self) {
let mut list = vec![];
for (id, creature) in self.game.state.creatures.iter() {
list.push(tui::widgets::ListItem::new(format!("{}: {} ({}) at {}",
id, creature.name,
creature.profession,
creature.entity.loc,
)));
}
self.dabug_data = Some(DebugData {
list,
list_state: tui::widgets::ListState::default(),
});
self.dabug_data.as_mut().unwrap().list_state.select(Some(0));
self.status = AppStatus::Debug;
}
2023-01-05 14:28:58 +00:00
fn default_layout(&self) -> Layout {
Layout::default()
.direction(tui::layout::Direction::Vertical)
.constraints(
[
tui::layout::Constraint::Min(3),
tui::layout::Constraint::Length(2)
]
.as_ref(),
)
}
2022-12-31 10:44:42 +00:00
pub fn draw<B: Backend>(&mut self, f: &mut Frame<B>) {
2022-12-31 10:57:55 +00:00
match &self.status {
AppStatus::Initial => {
self.draw_initial(f);
},
AppStatus::GuestSelection => {
self.draw_guest_selection(f);
},
2022-12-31 14:22:44 +00:00
AppStatus::TalkToGuest(guest_id) => {
self.draw_talk_to_guest(f, *guest_id);
2023-01-05 20:01:11 +00:00
},
AppStatus::Debug => {
self.draw_debug(f);
2022-12-31 10:57:55 +00:00
}
}
2022-12-31 10:44:42 +00:00
2022-12-31 10:57:55 +00:00
}
2023-01-04 18:02:44 +00:00
pub fn draw_initial<B: Backend>(&mut self, _f: &mut Frame<B>) {}
2022-12-31 10:57:55 +00:00
pub fn draw_guest_selection<B: Backend>(&mut self, f: &mut Frame<B>) {
2023-01-05 14:28:58 +00:00
let chunks = self.default_layout().split(f.size());
2022-12-31 11:14:48 +00:00
2022-12-31 10:44:42 +00:00
let main_window = tui::widgets::List::new(self.guest_list.clone())
.block(tui::widgets::Block::default().title("Guests").borders(tui::widgets::Borders::ALL))
.style(tui::style::Style::default().fg(tui::style::Color::White))
.highlight_style(tui::style::Style::default().add_modifier(tui::style::Modifier::ITALIC))
.highlight_symbol(">>");
2023-01-05 14:28:58 +00:00
let mut binding = Controls::new();
let controls = binding
.add("↑↓".to_owned(), "select guest".to_owned())
.add("".to_owned(), "talk to guest".to_owned())
.add(".".to_owned(), "pass one day".to_owned())
.add("Esc".to_owned(), "quit".to_owned())
.render();
2022-12-31 11:14:48 +00:00
2022-12-31 15:04:07 +00:00
f.render_stateful_widget(main_window, chunks[0], &mut self.guest_list_state);
2022-12-31 11:14:48 +00:00
f.render_widget(controls, chunks[1]);
2022-12-31 10:44:42 +00:00
}
2023-01-05 20:01:11 +00:00
2022-12-31 14:22:44 +00:00
fn draw_talk_to_guest<B: Backend>(&self, f: &mut Frame<B>, guest: Option<u32>) {
2023-01-05 14:28:58 +00:00
let chunks = self.default_layout().split(f.size());
2023-01-04 13:37:25 +00:00
let guest = self.game.state.get_creature(guest.unwrap()).unwrap();
2022-12-31 15:04:07 +00:00
let key_style = tui::style::Style::default()
.add_modifier(tui::style::Modifier::BOLD)
.fg(tui::style::Color::Green)
.bg(tui::style::Color::Black);
2023-01-04 18:41:31 +00:00
let mut full_text = self.conversation.to_spans();
2022-12-31 15:04:07 +00:00
full_text.push(tui::text::Spans::from(vec![
tui::text::Span::styled("(a) ", key_style),
2023-01-05 14:28:58 +00:00
tui::text::Span::raw("What's your business?\n\n"),
]));
full_text.push(tui::text::Spans::from(vec![
tui::text::Span::styled("(b) ", key_style),
tui::text::Span::raw("Let's trade?\n\n"),
]));
full_text.push(tui::text::Spans::from(vec![
tui::text::Span::styled("(c) ", key_style),
tui::text::Span::raw("Heard of anything interesting?\n\n"),
2022-12-31 15:04:07 +00:00
]));
let text = tui::text::Text::from(full_text);
2023-01-05 14:28:58 +00:00
let scroll: u16 = ((text.lines.len() as u16) )
.checked_sub(
(chunks[0].height as u16).checked_sub(2).unwrap_or(0) // 2 lines for the border
)
.unwrap_or(0)
.checked_sub(self.conversation_scroll)
.unwrap_or(0);
2022-12-31 15:04:07 +00:00
let main_window = tui::widgets::Paragraph::new(text)
.block(tui::widgets::Block::default().title(guest.name.clone()).borders(tui::widgets::Borders::ALL))
2023-01-05 14:28:58 +00:00
.style(tui::style::Style::default().fg(tui::style::Color::White))
.scroll((scroll, 0));
let mut binding = Controls::new();
let controls = binding
.add("a-z".to_owned(), "Talk".to_owned())
.add("Esc".to_owned(), "Back".to_owned())
.render();
f.render_widget(main_window, chunks[0]);
f.render_widget(controls, chunks[1]);
2022-12-31 10:57:55 +00:00
}
2022-12-31 15:04:07 +00:00
2023-01-05 20:01:11 +00:00
fn draw_debug<B: Backend>(&mut self, f: &mut Frame<B>) {
let chunks = self.default_layout().split(f.size());
let data = self.dabug_data.as_mut().unwrap();
let main_window = tui::widgets::List::new(data.list.clone())
.block(tui::widgets::Block::default().title("Guests").borders(tui::widgets::Borders::ALL))
.style(tui::style::Style::default().fg(tui::style::Color::White))
.highlight_style(tui::style::Style::default().add_modifier(tui::style::Modifier::ITALIC))
.highlight_symbol(">>");
let mut binding = Controls::new();
let controls = binding
.add("↑↓".to_owned(), "select guest".to_owned())
.add("Esc".to_owned(), "back".to_owned())
.render();
f.render_stateful_widget(main_window, chunks[0], &mut data.list_state);
f.render_widget(controls, chunks[1]);
}
2022-12-31 15:04:07 +00:00
/**
* Conversation
*/
2023-01-04 13:37:25 +00:00
fn greet_guest(&mut self, guest_id: Option<EntityId>) {
let guest = self.game.state.get_creature(guest_id.unwrap()).unwrap();
2023-01-04 18:41:31 +00:00
self.conversation.add_line(ChatLine::new(
0,
"You".to_owned(),
"Greetings traveller!".to_owned(),
false
));
self.conversation.add_line(ChatLine::new(
guest_id.unwrap(),
guest.name.clone(),
2023-01-05 19:14:27 +00:00
"Hello, I'm ".to_owned() + &guest.name + ", nice to meet you! "
+ "I'm a " + &guest.profession.to_string() + ".",
2023-01-04 18:41:31 +00:00
false
));
2023-01-05 14:28:58 +00:00
self.conversation_scroll = 0;
2022-12-31 15:04:07 +00:00
}
2023-01-04 13:37:25 +00:00
fn ask_business(&mut self, guest: Option<EntityId>) {
let guest = self.game.state.get_creature(guest.unwrap()).unwrap();
2023-01-04 18:41:31 +00:00
self.conversation.add_line(ChatLine::new(
0,
"You".to_owned(),
"What's your business?".to_owned(),
false
));
self.conversation.add_line(ChatLine::new(
guest.entity.id,
guest.name.clone(),
guest.say_agenda(& self.game.state),
false
));
2023-01-05 14:28:58 +00:00
self.conversation_scroll = 0;
2022-12-31 15:04:07 +00:00
}
2022-12-31 10:44:42 +00:00
}