debug menu WIP

This commit is contained in:
Niko Abeler 2023-01-05 21:01:11 +01:00
parent ed8a048a22
commit a479774b3e
2 changed files with 95 additions and 1 deletions

View File

@ -1,3 +1,5 @@
use std::fmt;
pub type EntityId = u32;
@ -11,4 +13,10 @@ pub struct Location {
pub struct Entity {
pub id: EntityId,
pub loc: Location
}
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}

View File

@ -21,15 +21,28 @@ enum AppStatus {
Initial,
GuestSelection,
TalkToGuest(Option<u32>),
Debug,
}
struct DebugData<'a> {
list: Vec<tui::widgets::ListItem<'a>>,
list_state: tui::widgets::ListState,
}
pub struct App<'a> {
game: Game,
status: AppStatus,
// Guest Selection
guest_list: Vec<tui::widgets::ListItem<'a>>,
guest_list_state: tui::widgets::ListState,
status: AppStatus,
// Conversation
conversation: Chat,
conversation_scroll: u16,
// Debug
dabug_data: Option<DebugData<'a>>,
}
impl<'a> App<'a> {
@ -41,6 +54,7 @@ impl<'a> App<'a> {
status: AppStatus::Initial,
conversation: Chat::new(),
conversation_scroll: 0,
dabug_data: None,
}
}
@ -86,6 +100,9 @@ impl<'a> App<'a> {
}
}
},
KeyCode::Char('?') => {
self.open_debug();
},
KeyCode::Char('.') => {
self.game.step();
self.status = AppStatus::Initial;
@ -124,10 +141,53 @@ impl<'a> App<'a> {
_ => {}
}
true
},
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
}
}
}
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;
}
fn default_layout(&self) -> Layout {
Layout::default()
.direction(tui::layout::Direction::Vertical)
@ -150,6 +210,9 @@ impl<'a> App<'a> {
},
AppStatus::TalkToGuest(guest_id) => {
self.draw_talk_to_guest(f, *guest_id);
},
AppStatus::Debug => {
self.draw_debug(f);
}
}
@ -177,6 +240,7 @@ impl<'a> App<'a> {
f.render_stateful_widget(main_window, chunks[0], &mut self.guest_list_state);
f.render_widget(controls, chunks[1]);
}
fn draw_talk_to_guest<B: Backend>(&self, f: &mut Frame<B>, guest: Option<u32>) {
let chunks = self.default_layout().split(f.size());
@ -224,6 +288,28 @@ impl<'a> App<'a> {
f.render_widget(controls, chunks[1]);
}
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]);
}
/**
* Conversation
*/