debug map
This commit is contained in:
parent
b94de7d4b0
commit
d655da0738
112
src/ui/mod.rs
112
src/ui/mod.rs
|
@ -4,7 +4,7 @@ mod controls;
|
||||||
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};
|
||||||
|
|
||||||
use crate::{game::Game, entity::EntityId};
|
use crate::{game::Game, entity::EntityId, world::Terrain};
|
||||||
|
|
||||||
use self::{chat::{Chat, ChatLine}, controls::Controls};
|
use self::{chat::{Chat, ChatLine}, controls::Controls};
|
||||||
|
|
||||||
|
@ -24,9 +24,11 @@ enum AppStatus {
|
||||||
Debug,
|
Debug,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
enum DebugView {
|
enum DebugView {
|
||||||
Creatures,
|
Creatures,
|
||||||
Items,
|
Items,
|
||||||
|
Map,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DebugData<'a> {
|
struct DebugData<'a> {
|
||||||
|
@ -37,6 +39,7 @@ struct DebugData<'a> {
|
||||||
item_list_state: tui::widgets::ListState,
|
item_list_state: tui::widgets::ListState,
|
||||||
|
|
||||||
selected: DebugView,
|
selected: DebugView,
|
||||||
|
map_scroll: (u16, u16),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct App<'a> {
|
pub struct App<'a> {
|
||||||
|
@ -160,30 +163,57 @@ impl<'a> App<'a> {
|
||||||
self.status = AppStatus::GuestSelection;
|
self.status = AppStatus::GuestSelection;
|
||||||
},
|
},
|
||||||
KeyCode::Up => {
|
KeyCode::Up => {
|
||||||
let ref mut state = match self.debug_data.as_ref().unwrap().selected {
|
if self.debug_data.as_ref().unwrap().selected == DebugView::Map {
|
||||||
DebugView::Creatures => &mut self.debug_data.as_mut().unwrap().list_state,
|
let (x, y) = self.debug_data.as_ref().unwrap().map_scroll;
|
||||||
DebugView::Items => &mut self.debug_data.as_mut().unwrap().item_list_state,
|
self.debug_data.as_mut().unwrap().map_scroll = (x.saturating_sub(1), y);
|
||||||
};
|
} else {
|
||||||
let selected = state.selected().unwrap();
|
let ref mut state = match self.debug_data.as_ref().unwrap().selected {
|
||||||
if selected > 0 {
|
DebugView::Creatures => &mut self.debug_data.as_mut().unwrap().list_state,
|
||||||
state.select(Some(selected - 1));
|
DebugView::Items => &mut self.debug_data.as_mut().unwrap().item_list_state,
|
||||||
|
_ => &mut self.debug_data.as_mut().unwrap().list_state,
|
||||||
|
};
|
||||||
|
let selected = state.selected().unwrap();
|
||||||
|
if selected > 0 {
|
||||||
|
state.select(Some(selected - 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
KeyCode::Down => {
|
KeyCode::Down => {
|
||||||
let mut max = 0;
|
if self.debug_data.as_ref().unwrap().selected == DebugView::Map {
|
||||||
let ref mut state = match self.debug_data.as_ref().unwrap().selected {
|
let (x, y) = self.debug_data.as_ref().unwrap().map_scroll;
|
||||||
DebugView::Creatures => {
|
self.debug_data.as_mut().unwrap().map_scroll = (x + 1, y);
|
||||||
max = self.debug_data.as_ref().unwrap().list.len() - 1;
|
} else {
|
||||||
&mut self.debug_data.as_mut().unwrap().list_state
|
let mut max = 0;
|
||||||
},
|
let ref mut state = match self.debug_data.as_ref().unwrap().selected {
|
||||||
DebugView::Items => {
|
DebugView::Creatures => {
|
||||||
max = self.debug_data.as_ref().unwrap().item_list.len() - 1;
|
max = self.debug_data.as_ref().unwrap().list.len() - 1;
|
||||||
&mut self.debug_data.as_mut().unwrap().item_list_state
|
&mut self.debug_data.as_mut().unwrap().list_state
|
||||||
},
|
},
|
||||||
};
|
DebugView::Items => {
|
||||||
let selected = state.selected().unwrap();
|
max = self.debug_data.as_ref().unwrap().item_list.len() - 1;
|
||||||
if selected < max {
|
&mut self.debug_data.as_mut().unwrap().item_list_state
|
||||||
state.select(Some(selected + 1));
|
},
|
||||||
|
_ => {
|
||||||
|
max = self.debug_data.as_ref().unwrap().list.len() - 1;
|
||||||
|
&mut self.debug_data.as_mut().unwrap().list_state
|
||||||
|
},
|
||||||
|
};
|
||||||
|
let selected = state.selected().unwrap();
|
||||||
|
if selected < max {
|
||||||
|
state.select(Some(selected + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
KeyCode::Left => {
|
||||||
|
if self.debug_data.as_ref().unwrap().selected == DebugView::Map {
|
||||||
|
let (x, y) = self.debug_data.as_ref().unwrap().map_scroll;
|
||||||
|
self.debug_data.as_mut().unwrap().map_scroll = (x, y.saturating_sub(1));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
KeyCode::Right => {
|
||||||
|
if self.debug_data.as_ref().unwrap().selected == DebugView::Map {
|
||||||
|
let (x, y) = self.debug_data.as_ref().unwrap().map_scroll;
|
||||||
|
self.debug_data.as_mut().unwrap().map_scroll = (x, y + 1);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
KeyCode::Char('i') => {
|
KeyCode::Char('i') => {
|
||||||
|
@ -192,6 +222,9 @@ impl<'a> App<'a> {
|
||||||
KeyCode::Char('c') => {
|
KeyCode::Char('c') => {
|
||||||
self.debug_data.as_mut().unwrap().selected = DebugView::Creatures;
|
self.debug_data.as_mut().unwrap().selected = DebugView::Creatures;
|
||||||
},
|
},
|
||||||
|
KeyCode::Char('m') => {
|
||||||
|
self.debug_data.as_mut().unwrap().selected = DebugView::Map;
|
||||||
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -225,7 +258,8 @@ impl<'a> App<'a> {
|
||||||
list_state: tui::widgets::ListState::default(),
|
list_state: tui::widgets::ListState::default(),
|
||||||
item_list,
|
item_list,
|
||||||
item_list_state: tui::widgets::ListState::default(),
|
item_list_state: tui::widgets::ListState::default(),
|
||||||
selected: DebugView::Creatures
|
selected: DebugView::Creatures,
|
||||||
|
map_scroll: (0, 0),
|
||||||
});
|
});
|
||||||
self.debug_data.as_mut().unwrap().list_state.select(Some(0));
|
self.debug_data.as_mut().unwrap().list_state.select(Some(0));
|
||||||
self.debug_data.as_mut().unwrap().item_list_state.select(Some(0));
|
self.debug_data.as_mut().unwrap().item_list_state.select(Some(0));
|
||||||
|
@ -353,13 +387,43 @@ impl<'a> App<'a> {
|
||||||
.highlight_style(tui::style::Style::default().add_modifier(tui::style::Modifier::ITALIC))
|
.highlight_style(tui::style::Style::default().add_modifier(tui::style::Modifier::ITALIC))
|
||||||
.highlight_symbol(">>");
|
.highlight_symbol(">>");
|
||||||
f.render_stateful_widget(main_window, chunks[0], &mut data.item_list_state);
|
f.render_stateful_widget(main_window, chunks[0], &mut data.item_list_state);
|
||||||
}
|
},
|
||||||
|
DebugView::Map => {
|
||||||
|
let mut rows = Vec::new();
|
||||||
|
for row in self.game.state.world.map.iter() {
|
||||||
|
rows.push(tui::text::Spans::from(
|
||||||
|
row.iter().map(|tile| {
|
||||||
|
match tile.terrain {
|
||||||
|
Terrain::Void => tui::text::Span::styled(" ", tui::style::Style::default().bg(tui::style::Color::Rgb(0, 0, 0)).fg(tui::style::Color::Rgb(0, 0, 0))),
|
||||||
|
Terrain::DeepOcean => tui::text::Span::styled("~", tui::style::Style::default().bg(tui::style::Color::Rgb(0, 0, 128)).fg(tui::style::Color::Rgb(32, 32, 128))),
|
||||||
|
Terrain::Ocean => tui::text::Span::styled("~", tui::style::Style::default().bg(tui::style::Color::Rgb(32, 32, 128)).fg(tui::style::Color::Rgb(128, 128, 128))),
|
||||||
|
Terrain::Beach => tui::text::Span::styled("-", tui::style::Style::default().bg(tui::style::Color::Rgb(200, 200, 10)).fg(tui::style::Color::Rgb(100, 100, 10))),
|
||||||
|
Terrain::Flats => tui::text::Span::styled("-", tui::style::Style::default().bg(tui::style::Color::Rgb(30, 150, 30)).fg(tui::style::Color::Rgb(30, 200, 30))),
|
||||||
|
Terrain::Hills => tui::text::Span::styled("~", tui::style::Style::default().bg(tui::style::Color::Rgb(120, 150, 30)).fg(tui::style::Color::Rgb(120, 150, 30))),
|
||||||
|
Terrain::Mountains => tui::text::Span::styled("A", tui::style::Style::default().bg(tui::style::Color::Rgb(120,120,120)).fg(tui::style::Color::Rgb(120,120,120))),
|
||||||
|
Terrain::HighMountains => tui::text::Span::styled("A", tui::style::Style::default().bg(tui::style::Color::Rgb(200,200,200)).fg(tui::style::Color::Rgb(200,200,200))),
|
||||||
|
}
|
||||||
|
}).collect::<Vec<tui::text::Span>>()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let main_window = tui::widgets::Paragraph::new(
|
||||||
|
tui::text::Text::from(rows)
|
||||||
|
)
|
||||||
|
.block(tui::widgets::Block::default().title("Map").borders(tui::widgets::Borders::ALL))
|
||||||
|
.style(tui::style::Style::default().fg(tui::style::Color::White))
|
||||||
|
.scroll(data.map_scroll);
|
||||||
|
|
||||||
|
f.render_widget(main_window, chunks[0]);
|
||||||
|
// f.render_stateful_widget(main_window, chunks[0], &mut data.map_list_state);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
let mut binding = Controls::new();
|
let mut binding = Controls::new();
|
||||||
let controls = binding
|
let controls = binding
|
||||||
.add("↑↓".to_owned(), "select guest".to_owned())
|
.add("↑↓".to_owned(), "select guest".to_owned())
|
||||||
.add("c".to_owned(), "Creatures".to_owned())
|
.add("c".to_owned(), "Creatures".to_owned())
|
||||||
.add("i".to_owned(), "Items".to_owned())
|
.add("i".to_owned(), "Items".to_owned())
|
||||||
|
.add("m".to_owned(), "Map".to_owned())
|
||||||
.add("Esc".to_owned(), "back".to_owned())
|
.add("Esc".to_owned(), "back".to_owned())
|
||||||
.render();
|
.render();
|
||||||
f.render_widget(controls, chunks[1]);
|
f.render_widget(controls, chunks[1]);
|
||||||
|
|
Loading…
Reference in New Issue