tavern_keeper/src/ui/mod.rs

190 lines
5.9 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-10 18:24:38 +00:00
mod debug_view;
2023-01-10 18:46:44 +00:00
mod guest_selection;
2023-01-11 19:56:47 +00:00
mod talk_to_guest;
2023-01-10 18:24:38 +00:00
mod status_line;
2023-01-04 18:41:31 +00:00
2023-01-11 19:56:47 +00:00
2022-12-31 10:44:42 +00:00
use crossterm::event::{read, Event, KeyCode};
2023-01-11 19:56:47 +00:00
use tui::{backend::Backend, Frame, layout::Layout, Terminal};
2022-12-31 10:44:42 +00:00
2023-01-07 09:08:42 +00:00
use crate::{game::Game, entity::{EntityId, EntityType}, world::Terrain};
2022-12-31 10:44:42 +00:00
2023-01-11 19:56:47 +00:00
use self::{chat::{Chat, ChatLine}, controls::Controls, debug_view::DebugView, guest_selection::GuestSelectionView, talk_to_guest::TalkToGuestView};
2023-01-04 18:41:31 +00:00
2022-12-31 10:44:42 +00:00
/**
* |........................|
* | Conversations/Selection|
* | |
* |........................|
* | Available Options |
* |........................|
*/
2023-01-11 20:49:16 +00:00
pub enum AppStatus {
2022-12-31 10:44:42 +00:00
Initial,
2023-01-11 20:49:16 +00:00
GuestSelection(GuestSelectionView),
2023-01-11 19:56:47 +00:00
TalkToGuest(TalkToGuestView),
2023-01-09 19:14:19 +00:00
BuyFromGuest(EntityId),
2023-01-11 20:49:16 +00:00
Debug(DebugView),
2023-01-05 20:01:11 +00:00
}
2023-01-11 20:49:16 +00:00
pub struct App {
2023-01-01 20:11:27 +00:00
game: Game,
2023-01-11 20:49:16 +00:00
status: AppStatus,
2022-12-31 10:44:42 +00:00
}
2023-01-07 13:42:42 +00:00
pub struct DefaultLayout {
pub status: tui::layout::Rect,
pub main: tui::layout::Rect,
pub controls: tui::layout::Rect,
}
impl DefaultLayout {
pub fn default(rect: tui::layout::Rect) -> DefaultLayout {
let chunks = Layout::default()
.direction(tui::layout::Direction::Vertical)
.constraints(
[
tui::layout::Constraint::Length(1),
tui::layout::Constraint::Min(3),
tui::layout::Constraint::Length(2)
]
.as_ref(),
).split(rect);
DefaultLayout {
status: chunks[0],
main: chunks[1],
controls: chunks[2],
}
}
}
2023-01-11 20:49:16 +00:00
impl App {
pub fn new(state: Game) -> App {
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
status: AppStatus::Initial,
}
}
2023-01-11 20:49:16 +00:00
pub fn step(&mut self) -> bool {
2023-01-11 19:56:47 +00:00
let mut ret = (true, None);
match &mut self.status {
2022-12-31 10:44:42 +00:00
AppStatus::Initial => {
2023-01-11 19:56:47 +00:00
ret.1 = Some(AppStatus::GuestSelection(GuestSelectionView::new(&self.game)));
2022-12-31 10:44:42 +00:00
},
2023-01-11 19:56:47 +00:00
AppStatus::GuestSelection(ref mut guest_selection) => {
ret = guest_selection.control(&mut self.game);
2022-12-31 10:57:55 +00:00
},
2023-01-11 19:56:47 +00:00
AppStatus::TalkToGuest(talk_view) => {
ret = talk_view.control(&mut self.game);
2023-01-05 20:01:11 +00:00
},
2023-01-09 19:14:19 +00:00
AppStatus::BuyFromGuest(guest_id) => {
2023-01-11 19:56:47 +00:00
// match read() {
// Ok(Event::Key(event)) => {
// match event.code {
// KeyCode::Esc => {
// self.status = AppStatus::TalkToGuest(Some(*guest_id));
// },
// _ => {}
// }
// },
// _ => {}
// }
2023-01-09 19:14:19 +00:00
},
2023-01-11 19:56:47 +00:00
AppStatus::Debug(debug_view) => {
ret = debug_view.control(&mut self.game);
2022-12-31 10:44:42 +00:00
}
}
2023-01-11 20:49:16 +00:00
if let Some(next_status) = ret.1 {
self.status = next_status;
}
2023-01-11 19:56:47 +00:00
ret.0
2022-12-31 10:44:42 +00:00
}
2023-01-05 20:01:11 +00:00
fn open_debug(&mut self) {
2023-01-11 19:56:47 +00:00
self.status = AppStatus::Debug(DebugView::new(&self.game));
2023-01-05 20:01:11 +00:00
}
2023-01-07 13:42:42 +00:00
// fn default_layout(&self) -> Layout {
// DefaultLayout::default()
// }
2023-01-05 14:28:58 +00:00
2022-12-31 10:44:42 +00:00
pub fn draw<B: Backend>(&mut self, f: &mut Frame<B>) {
2023-01-11 19:56:47 +00:00
match &mut self.status {
2022-12-31 10:57:55 +00:00
AppStatus::Initial => {
self.draw_initial(f);
},
2023-01-11 19:56:47 +00:00
AppStatus::GuestSelection(view) => {
view.draw(f, &self.game);
2022-12-31 10:57:55 +00:00
},
2023-01-11 19:56:47 +00:00
AppStatus::TalkToGuest(view) => {
view.draw(f, &self.game);
2023-01-05 20:01:11 +00:00
},
2023-01-09 19:14:19 +00:00
AppStatus::BuyFromGuest(guest_id) => {
2023-01-11 19:56:47 +00:00
// self.draw_buy_from_guest(f, *guest_id);
2023-01-09 19:14:19 +00:00
},
2023-01-11 19:56:47 +00:00
AppStatus::Debug(view) => {
view.draw(f, &self.game);
2023-01-09 19:14:19 +00:00
},
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-07 13:42:42 +00:00
fn draw_status<B: Backend>(&self, f: &mut Frame<B>, rect: tui::layout::Rect) {
2023-01-08 10:57:13 +00:00
let tavern = self.game.state.world.get_site(
self.game.state.tavern.unwrap()
).unwrap();
2023-01-07 13:42:42 +00:00
let spans = tui::text::Spans::from(vec![
tui::text::Span::raw("Date: "),
tui::text::Span::raw(format!("{}", self.game.state.time)),
2023-01-08 10:57:13 +00:00
tui::text::Span::raw(" "),
tui::text::Span::raw("Funds: "),
2023-01-08 11:11:33 +00:00
tui::text::Span::raw(format!("{} gold coins", tavern.coins)),
2023-01-07 13:42:42 +00:00
]);
let status_text = tui::widgets::Paragraph::new(spans)
.block(tui::widgets::Block::default().borders(tui::widgets::Borders::LEFT | tui::widgets::Borders::RIGHT))
.style(tui::style::Style::default().fg(tui::style::Color::White));
f.render_widget(status_text, rect);
}
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
2022-12-31 15:04:07 +00:00
2023-01-09 19:14:19 +00:00
fn draw_buy_from_guest<B: Backend>(&self, f: &mut Frame<B>, guest_id: EntityId) {
let chunks = DefaultLayout::default(f.size());
let guest = self.game.state.get_creature(guest_id).unwrap();
let inventory = self.game.state.get_inventory(guest_id);
let mut list_items = vec![];
for item in inventory {
let item = self.game.state.get_item(item).unwrap();
list_items.push(tui::widgets::ListItem::new(format!("{} ({} gold)", item.name, item.value())));
}
let main_window = tui::widgets::List::new(list_items)
.block(tui::widgets::Block::default().title(guest.name.clone()).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("a-z".to_owned(), "Select".to_owned())
.add("Esc".to_owned(), "Back".to_owned())
.render();
self.draw_status(f, chunks.status);
f.render_widget(main_window, chunks.main);
2023-01-10 18:24:38 +00:00
f.render_widget(controls, chunks.controls);
2023-01-05 20:01:11 +00:00
}
2022-12-31 10:44:42 +00:00
}