Complete mouse interaction

This commit is contained in:
Pierre HUBERT 2022-10-07 20:07:18 +02:00
parent a2c9e4e257
commit b1f60894ed

View File

@ -35,6 +35,7 @@ pub fn set_boat_layout<B: Backend>(
let mut coordinates_mapper = CoordinatesMapper::default(); let mut coordinates_mapper = CoordinatesMapper::default();
let mut last_tick = Instant::now(); let mut last_tick = Instant::now();
let mut is_moving_boat = false;
loop { loop {
terminal.draw(|f| coordinates_mapper = ui(f, &mut model, rules))?; terminal.draw(|f| coordinates_mapper = ui(f, &mut model, rules))?;
@ -57,20 +58,37 @@ pub fn set_boat_layout<B: Backend>(
} }
model.curr_boat %= model.layout.number_of_boats(); model.curr_boat %= model.layout.number_of_boats();
} else if let Event::Mouse(mouse) = event { }
if MouseEventKind::Up(MouseButton::Left) == mouse.kind { // Mouse event
else if let Event::Mouse(mouse) = event {
let src_pos = Coordinates::new(mouse.column, mouse.row); let src_pos = Coordinates::new(mouse.column, mouse.row);
if let Some(pos) = coordinates_mapper.get(&src_pos) {
// Start mouse action
if MouseEventKind::Down(MouseButton::Left) == mouse.kind {
is_moving_boat = if let Some(pos) = coordinates_mapper.get(&src_pos) {
match model.layout.find_boat_at_position(*pos).cloned() { match model.layout.find_boat_at_position(*pos).cloned() {
// Change of selected boat // Change of selected boat
Some(b) if b != model.layout.0[model.curr_boat] => { Some(b) if b != model.layout.0[model.curr_boat] => {
model.curr_boat = model.curr_boat =
model.layout.0.iter().position(|s| s == &b).unwrap(); model.layout.0.iter().position(|s| s == &b).unwrap();
false
} }
// Move current boat // Move current boat
_ => model.layout.0[model.curr_boat].start = *pos, _ => true,
} }
} else {
false
}
}
// Handle continue mouse action
else if is_moving_boat {
if let Some(pos) = coordinates_mapper.get(&src_pos) {
model.layout.0[model.curr_boat].start = *pos;
}
if let MouseEventKind::Up(_) = mouse.kind {
is_moving_boat = false;
} }
} }
} }