WIP moving view into own structs
This commit is contained in:
parent
72a43400a0
commit
9e51daf566
|
@ -129,8 +129,7 @@ fn main() -> Result<(), io::Error> {
|
||||||
|
|
||||||
let mut app = App::new(game);
|
let mut app = App::new(game);
|
||||||
loop {
|
loop {
|
||||||
terminal.draw(|f| app.draw(f))?;
|
if !app.do_loop(&mut terminal) {
|
||||||
if !app.step() {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ use tui::{backend::Backend, Frame};
|
||||||
|
|
||||||
use crate::{world::Terrain, game::Game};
|
use crate::{world::Terrain, game::Game};
|
||||||
|
|
||||||
use super::{controls::Controls, DefaultLayout, status_line::StatusLine, AppStatus};
|
use super::{controls::Controls, DefaultLayout, status_line::StatusLine, AppStatus, guest_selection::GuestSelectionView};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
enum DebugTab {
|
enum DebugTab {
|
||||||
|
@ -56,12 +56,12 @@ impl<'a> DebugView<'a> {
|
||||||
view
|
view
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn control(&mut self, _game: &mut Game) -> (bool, Option<AppStatus>) {
|
pub fn control(&mut self, game: &mut Game) -> (bool, Option<AppStatus>) {
|
||||||
match read() {
|
match read() {
|
||||||
Ok(Event::Key(event)) => {
|
Ok(Event::Key(event)) => {
|
||||||
match event.code {
|
match event.code {
|
||||||
KeyCode::Esc => {
|
KeyCode::Esc => {
|
||||||
return (true, Some(AppStatus::GuestSelection))
|
return (true, Some(AppStatus::GuestSelection(GuestSelectionView::new(game))));
|
||||||
},
|
},
|
||||||
KeyCode::Up => {
|
KeyCode::Up => {
|
||||||
if self.selected == DebugTab::Map {
|
if self.selected == DebugTab::Map {
|
||||||
|
|
|
@ -3,7 +3,7 @@ use tui::{backend::Backend, Frame};
|
||||||
|
|
||||||
use crate::game::Game;
|
use crate::game::Game;
|
||||||
|
|
||||||
use super::{AppStatus, DefaultLayout, controls::Controls, status_line::StatusLine};
|
use super::{AppStatus, DefaultLayout, controls::Controls, status_line::StatusLine, talk_to_guest::TalkToGuestView, debug_view::DebugView};
|
||||||
|
|
||||||
pub struct GuestSelectionView<'a> {
|
pub struct GuestSelectionView<'a> {
|
||||||
guest_list: Vec<tui::widgets::ListItem<'a>>,
|
guest_list: Vec<tui::widgets::ListItem<'a>>,
|
||||||
|
@ -49,17 +49,14 @@ impl<'a> GuestSelectionView<'a> {
|
||||||
if game.state.guests().len() > 0 {
|
if game.state.guests().len() > 0 {
|
||||||
match self.guest_list_state.selected() {
|
match self.guest_list_state.selected() {
|
||||||
Some(selected) => {
|
Some(selected) => {
|
||||||
// let guest_id = &self.game.state.guests()[selected];
|
return (true, Some(AppStatus::TalkToGuest(TalkToGuestView::new(game, game.state.guests()[selected]))))
|
||||||
// self.conversation = Chat::new();
|
|
||||||
// self.greet_guest(Some(*guest_id));
|
|
||||||
// self.status = AppStatus::TalkToGuest(Some(*guest_id));
|
|
||||||
},
|
},
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
KeyCode::Char('?') => {
|
KeyCode::Char('?') => {
|
||||||
return (true, Some(AppStatus::Debug))
|
return (true, Some(AppStatus::Debug(DebugView::new(game))))
|
||||||
},
|
},
|
||||||
KeyCode::Char('.') => {
|
KeyCode::Char('.') => {
|
||||||
game.step();
|
game.step();
|
||||||
|
|
144
src/ui/mod.rs
144
src/ui/mod.rs
|
@ -2,14 +2,16 @@ mod chat;
|
||||||
mod controls;
|
mod controls;
|
||||||
mod debug_view;
|
mod debug_view;
|
||||||
mod guest_selection;
|
mod guest_selection;
|
||||||
|
mod talk_to_guest;
|
||||||
mod status_line;
|
mod status_line;
|
||||||
|
|
||||||
|
|
||||||
use crossterm::event::{read, Event, KeyCode};
|
use crossterm::event::{read, Event, KeyCode};
|
||||||
use tui::{backend::Backend, Frame, layout::Layout};
|
use tui::{backend::Backend, Frame, layout::Layout, Terminal};
|
||||||
|
|
||||||
use crate::{game::Game, entity::{EntityId, EntityType}, world::Terrain};
|
use crate::{game::Game, entity::{EntityId, EntityType}, world::Terrain};
|
||||||
|
|
||||||
use self::{chat::{Chat, ChatLine}, controls::Controls, debug_view::DebugView, guest_selection::GuestSelectionView};
|
use self::{chat::{Chat, ChatLine}, controls::Controls, debug_view::DebugView, guest_selection::GuestSelectionView, talk_to_guest::TalkToGuestView};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* |........................|
|
* |........................|
|
||||||
|
@ -20,27 +22,28 @@ use self::{chat::{Chat, ChatLine}, controls::Controls, debug_view::DebugView, gu
|
||||||
* |........................|
|
* |........................|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pub enum AppStatus {
|
pub enum AppStatus<'a> {
|
||||||
Initial,
|
Initial,
|
||||||
GuestSelection,
|
GuestSelection(GuestSelectionView<'a>),
|
||||||
TalkToGuest(Option<EntityId>),
|
TalkToGuest(TalkToGuestView),
|
||||||
BuyFromGuest(EntityId),
|
BuyFromGuest(EntityId),
|
||||||
Debug,
|
Debug(DebugView<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct App<'a> {
|
pub struct App<'a> {
|
||||||
game: Game,
|
game: Game,
|
||||||
status: AppStatus,
|
status: AppStatus<'a>,
|
||||||
|
next_status: Option<AppStatus<'a>>,
|
||||||
// Guest Selection
|
|
||||||
guest_selection: Option<GuestSelectionView<'a>>,
|
|
||||||
|
|
||||||
|
|
||||||
// Conversation
|
// Conversation
|
||||||
conversation: Chat,
|
conversation: Chat,
|
||||||
conversation_scroll: u16,
|
conversation_scroll: u16,
|
||||||
|
|
||||||
// Debug
|
// // Guest Selection
|
||||||
debug_view: Option<DebugView<'a>>,
|
// guest_selection: Option<GuestSelectionView<'a>>,
|
||||||
|
// // Debug
|
||||||
|
// debug_view: Option<DebugView<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DefaultLayout {
|
pub struct DefaultLayout {
|
||||||
|
@ -74,85 +77,60 @@ impl<'a> App<'a> {
|
||||||
pub fn new(state: Game) -> App<'a> {
|
pub fn new(state: Game) -> App<'a> {
|
||||||
App {
|
App {
|
||||||
game: state,
|
game: state,
|
||||||
guest_selection: None,
|
|
||||||
status: AppStatus::Initial,
|
status: AppStatus::Initial,
|
||||||
conversation: Chat::new(),
|
conversation: Chat::new(),
|
||||||
conversation_scroll: 0,
|
conversation_scroll: 0,
|
||||||
debug_view: None,
|
next_status: None,
|
||||||
|
// guest_selection: None,
|
||||||
|
// debug_view: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn step(&mut self) -> bool {
|
pub fn do_loop<B: Backend>(&'a mut self, terminal: &mut Terminal<B>) -> bool {
|
||||||
match &self.status {
|
terminal.draw(|f| self.draw(f)).unwrap();
|
||||||
|
self.step()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn step(&'a mut self) -> bool {
|
||||||
|
if let Some(next_status) = self.next_status.take() {
|
||||||
|
self.status = next_status;
|
||||||
|
self.next_status = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut ret = (true, None);
|
||||||
|
match &mut self.status {
|
||||||
AppStatus::Initial => {
|
AppStatus::Initial => {
|
||||||
self.guest_selection = Some(GuestSelectionView::new(&self.game));
|
ret.1 = Some(AppStatus::GuestSelection(GuestSelectionView::new(&self.game)));
|
||||||
self.status = AppStatus::GuestSelection;
|
|
||||||
true
|
|
||||||
},
|
},
|
||||||
AppStatus::GuestSelection => {
|
AppStatus::GuestSelection(ref mut guest_selection) => {
|
||||||
let ret = self.guest_selection.as_mut().unwrap().control(&mut self.game);
|
ret = guest_selection.control(&mut self.game);
|
||||||
if let Some(next) = ret.1 {
|
|
||||||
self.debug_view = None;
|
|
||||||
self.status = next;
|
|
||||||
}
|
|
||||||
ret.0
|
|
||||||
},
|
},
|
||||||
AppStatus::TalkToGuest(guest) => {
|
AppStatus::TalkToGuest(talk_view) => {
|
||||||
match read() {
|
ret = talk_view.control(&mut self.game);
|
||||||
Ok(Event::Key(event)) => {
|
|
||||||
match event.code {
|
|
||||||
KeyCode::Esc => {
|
|
||||||
self.status = AppStatus::GuestSelection;
|
|
||||||
},
|
|
||||||
KeyCode::Char('a') => {
|
|
||||||
self.ask_business(*guest);
|
|
||||||
},
|
|
||||||
KeyCode::Char('b') => {
|
|
||||||
self.status = AppStatus::BuyFromGuest(guest.unwrap());
|
|
||||||
},
|
|
||||||
KeyCode::Up => {
|
|
||||||
self.conversation_scroll += 1;
|
|
||||||
},
|
|
||||||
KeyCode::Down => {
|
|
||||||
if self.conversation_scroll > 0 {
|
|
||||||
self.conversation_scroll -= 1;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
true
|
|
||||||
},
|
},
|
||||||
AppStatus::BuyFromGuest(guest_id) => {
|
AppStatus::BuyFromGuest(guest_id) => {
|
||||||
match read() {
|
// match read() {
|
||||||
Ok(Event::Key(event)) => {
|
// Ok(Event::Key(event)) => {
|
||||||
match event.code {
|
// match event.code {
|
||||||
KeyCode::Esc => {
|
// KeyCode::Esc => {
|
||||||
self.status = AppStatus::TalkToGuest(Some(*guest_id));
|
// self.status = AppStatus::TalkToGuest(Some(*guest_id));
|
||||||
},
|
// },
|
||||||
_ => {}
|
// _ => {}
|
||||||
}
|
// }
|
||||||
},
|
// },
|
||||||
_ => {}
|
// _ => {}
|
||||||
}
|
// }
|
||||||
true
|
|
||||||
},
|
},
|
||||||
AppStatus::Debug => {
|
AppStatus::Debug(debug_view) => {
|
||||||
let ret = self.debug_view.as_mut().unwrap().control(&mut self.game);
|
ret = debug_view.control(&mut self.game);
|
||||||
if let Some(next) = ret.1 {
|
|
||||||
self.debug_view = None;
|
|
||||||
self.status = next;
|
|
||||||
}
|
|
||||||
ret.0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.next_status = ret.1;
|
||||||
|
ret.0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_debug(&mut self) {
|
fn open_debug(&mut self) {
|
||||||
self.debug_view = Some(DebugView::new(&self.game));
|
self.status = AppStatus::Debug(DebugView::new(&self.game));
|
||||||
self.status = AppStatus::Debug;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn default_layout(&self) -> Layout {
|
// fn default_layout(&self) -> Layout {
|
||||||
|
@ -160,21 +138,21 @@ impl<'a> App<'a> {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
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 {
|
match &mut self.status {
|
||||||
AppStatus::Initial => {
|
AppStatus::Initial => {
|
||||||
self.draw_initial(f);
|
self.draw_initial(f);
|
||||||
},
|
},
|
||||||
AppStatus::GuestSelection => {
|
AppStatus::GuestSelection(view) => {
|
||||||
self.guest_selection.as_mut().unwrap().draw(f, &self.game);
|
view.draw(f, &self.game);
|
||||||
},
|
},
|
||||||
AppStatus::TalkToGuest(guest_id) => {
|
AppStatus::TalkToGuest(view) => {
|
||||||
self.draw_talk_to_guest(f, *guest_id);
|
view.draw(f, &self.game);
|
||||||
},
|
},
|
||||||
AppStatus::BuyFromGuest(guest_id) => {
|
AppStatus::BuyFromGuest(guest_id) => {
|
||||||
self.draw_buy_from_guest(f, *guest_id);
|
// self.draw_buy_from_guest(f, *guest_id);
|
||||||
},
|
},
|
||||||
AppStatus::Debug => {
|
AppStatus::Debug(view) => {
|
||||||
self.debug_view.as_mut().unwrap().draw(f, &self.game);
|
view.draw(f, &self.game);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,138 @@
|
||||||
|
use crossterm::event::{read, Event, KeyCode};
|
||||||
|
use tui::{backend::Backend, Frame};
|
||||||
|
|
||||||
|
use crate::{game::Game, entity::{EntityId, EntityType}};
|
||||||
|
|
||||||
|
use super::{AppStatus, DefaultLayout, controls::Controls, status_line::StatusLine, chat::{Chat, ChatLine}, guest_selection::GuestSelectionView};
|
||||||
|
|
||||||
|
pub struct TalkToGuestView {
|
||||||
|
guest_id: EntityId,
|
||||||
|
conversation: Chat,
|
||||||
|
conversation_scroll: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TalkToGuestView {
|
||||||
|
pub fn new(game: &Game, guest_id: EntityId) -> TalkToGuestView {
|
||||||
|
let mut view = TalkToGuestView {
|
||||||
|
guest_id,
|
||||||
|
conversation: Chat::new(),
|
||||||
|
conversation_scroll: 0,
|
||||||
|
};
|
||||||
|
view.greet_guest(game);
|
||||||
|
view
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn control(&mut self, game: &mut Game) -> (bool, Option<AppStatus>) {
|
||||||
|
match read() {
|
||||||
|
Ok(Event::Key(event)) => {
|
||||||
|
match event.code {
|
||||||
|
KeyCode::Esc => {
|
||||||
|
return (true, Some(AppStatus::GuestSelection(GuestSelectionView::new(game))));
|
||||||
|
},
|
||||||
|
KeyCode::Char('a') => {
|
||||||
|
self.ask_business(game);
|
||||||
|
},
|
||||||
|
KeyCode::Char('b') => {
|
||||||
|
// self.status = AppStatus::BuyFromGuest(guest.unwrap());
|
||||||
|
},
|
||||||
|
KeyCode::Up => {
|
||||||
|
self.conversation_scroll += 1;
|
||||||
|
},
|
||||||
|
KeyCode::Down => {
|
||||||
|
if self.conversation_scroll > 0 {
|
||||||
|
self.conversation_scroll -= 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
return (true, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw<B: Backend>(&mut self, f: &mut Frame<B>, game: &Game) {
|
||||||
|
let chunks = DefaultLayout::default(f.size());
|
||||||
|
|
||||||
|
let guest = game.state.get_creature(self.guest_id).unwrap();
|
||||||
|
|
||||||
|
let key_style = tui::style::Style::default()
|
||||||
|
.add_modifier(tui::style::Modifier::BOLD)
|
||||||
|
.fg(tui::style::Color::Green)
|
||||||
|
.bg(tui::style::Color::Black);
|
||||||
|
|
||||||
|
let mut full_text = self.conversation.to_spans();
|
||||||
|
full_text.push(tui::text::Spans::from(vec![
|
||||||
|
tui::text::Span::styled("(a) ", key_style),
|
||||||
|
tui::text::Span::raw("What's your business?"),
|
||||||
|
]));
|
||||||
|
full_text.push(tui::text::Spans::from(vec![
|
||||||
|
tui::text::Span::styled("(b) ", key_style),
|
||||||
|
tui::text::Span::raw("What do you have to sell?"),
|
||||||
|
]));
|
||||||
|
full_text.push(tui::text::Spans::from(vec![
|
||||||
|
tui::text::Span::styled("(c) ", key_style),
|
||||||
|
tui::text::Span::raw("Heard of anything interesting?"),
|
||||||
|
]));
|
||||||
|
let text = tui::text::Text::from(full_text);
|
||||||
|
let scroll: u16 = ((text.lines.len() as u16) )
|
||||||
|
.saturating_sub(
|
||||||
|
(chunks.main.height as u16).saturating_sub(2) // 2 lines for the border
|
||||||
|
)
|
||||||
|
.saturating_sub(self.conversation_scroll);
|
||||||
|
|
||||||
|
let main_window = tui::widgets::Paragraph::new(text)
|
||||||
|
.block(tui::widgets::Block::default().title(guest.name.clone()).borders(tui::widgets::Borders::ALL))
|
||||||
|
.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();
|
||||||
|
|
||||||
|
StatusLine::draw(f, chunks.status, game);
|
||||||
|
f.render_widget(main_window, chunks.main);
|
||||||
|
f.render_widget(controls, chunks.controls);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Conversation
|
||||||
|
*/
|
||||||
|
fn greet_guest(&mut self, game: &Game) {
|
||||||
|
let guest = game.state.get_creature(self.guest_id).unwrap();
|
||||||
|
self.conversation.add_line(ChatLine::new(
|
||||||
|
(EntityType::Creature, 0),
|
||||||
|
"You".to_owned(),
|
||||||
|
"Greetings traveller!".to_owned(),
|
||||||
|
false
|
||||||
|
));
|
||||||
|
self.conversation.add_line(ChatLine::new(
|
||||||
|
self.guest_id,
|
||||||
|
guest.name.clone(),
|
||||||
|
"Hello, I'm ".to_owned() + &guest.name + ", nice to meet you! "
|
||||||
|
+ "I'm a " + &guest.profession.to_string() + ".",
|
||||||
|
false
|
||||||
|
));
|
||||||
|
self.conversation_scroll = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ask_business(&mut self, game: &mut Game) {
|
||||||
|
let guest = game.state.get_creature(self.guest_id).unwrap();
|
||||||
|
self.conversation.add_line(ChatLine::new(
|
||||||
|
(EntityType::Creature, 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(& game.state),
|
||||||
|
false
|
||||||
|
));
|
||||||
|
self.conversation_scroll = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue