Add mouse interaction
This commit is contained in:
@ -20,6 +20,7 @@ pub struct GameMapWidget<'a> {
|
||||
colored_cells: Vec<ColoredCells>,
|
||||
title: Option<String>,
|
||||
legend: Option<String>,
|
||||
yield_coordinates: Option<Box<dyn 'a + FnMut(Coordinates, Rect)>>,
|
||||
}
|
||||
|
||||
impl<'a> GameMapWidget<'a> {
|
||||
@ -30,6 +31,7 @@ impl<'a> GameMapWidget<'a> {
|
||||
colored_cells: vec![],
|
||||
title: None,
|
||||
legend: None,
|
||||
yield_coordinates: None,
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,6 +55,14 @@ impl<'a> GameMapWidget<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_yield_func<F>(mut self, func: F) -> Self
|
||||
where
|
||||
F: 'a + FnMut(Coordinates, Rect),
|
||||
{
|
||||
self.yield_coordinates = Some(Box::new(func));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn grid_size(&self) -> (u16, u16) {
|
||||
let w = self.rules.map_width as u16 * 2 + 1;
|
||||
let h = self.rules.map_height as u16 * 2 + 1;
|
||||
@ -76,7 +86,7 @@ impl<'a> GameMapWidget<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Widget for GameMapWidget<'a> {
|
||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||
fn render(mut self, area: Rect, buf: &mut Buffer) {
|
||||
let alphabet = PlayConfiguration::default().ordinate_alphabet;
|
||||
|
||||
let symbols = BorderType::line_symbols(BorderType::Plain);
|
||||
@ -92,12 +102,16 @@ impl<'a> Widget for GameMapWidget<'a> {
|
||||
|
||||
// Paint game grid
|
||||
for y in 0..(self.rules.map_height + 1) {
|
||||
// Header (ordinate)
|
||||
if y < self.rules.map_height {
|
||||
buf.get_mut(area.x, start_y + 2 + (y as u16 * 2))
|
||||
.set_char(alphabet.chars().nth(y).unwrap());
|
||||
}
|
||||
|
||||
for x in 0..(self.rules.map_width + 1) {
|
||||
let coordinates = Coordinates::new(x as i32, y as i32);
|
||||
|
||||
// Header (abscissa)
|
||||
if x < self.rules.map_width {
|
||||
buf.set_string(
|
||||
area.x + 2 + (x as u16 * 2) - (x as u16) / 10,
|
||||
@ -110,6 +124,11 @@ impl<'a> Widget for GameMapWidget<'a> {
|
||||
let o_x = 1 + area.x + (x as u16 * 2);
|
||||
let o_y = 1 + start_y + (y as u16 * 2);
|
||||
|
||||
let color = self
|
||||
.colored_cells
|
||||
.iter()
|
||||
.find(|c| c.cells.contains(&coordinates));
|
||||
|
||||
buf.get_mut(o_x, o_y).set_symbol(match (x, y) {
|
||||
(0, 0) => symbols.top_left,
|
||||
|
||||
@ -142,13 +161,13 @@ impl<'a> Widget for GameMapWidget<'a> {
|
||||
.get_mut(o_x + 1, o_y + 1)
|
||||
.set_char(self.default_empty_character);
|
||||
|
||||
if let Some(c) = self
|
||||
.colored_cells
|
||||
.iter()
|
||||
.find(|c| c.cells.contains(&Coordinates::new(x as i32, y as i32)))
|
||||
{
|
||||
if let Some(c) = color {
|
||||
cell.set_bg(c.color);
|
||||
}
|
||||
|
||||
if let Some(f) = self.yield_coordinates.as_mut() {
|
||||
f(coordinates, Rect::new(o_x + 1, o_y + 1, 1, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user