Complete mouse interaction
This commit is contained in:
		@@ -35,6 +35,7 @@ pub fn set_boat_layout<B: Backend>(
 | 
			
		||||
    let mut coordinates_mapper = CoordinatesMapper::default();
 | 
			
		||||
 | 
			
		||||
    let mut last_tick = Instant::now();
 | 
			
		||||
    let mut is_moving_boat = false;
 | 
			
		||||
    loop {
 | 
			
		||||
        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();
 | 
			
		||||
            } else if let Event::Mouse(mouse) = event {
 | 
			
		||||
                if MouseEventKind::Up(MouseButton::Left) == mouse.kind {
 | 
			
		||||
                    let src_pos = Coordinates::new(mouse.column, mouse.row);
 | 
			
		||||
                    if let Some(pos) = coordinates_mapper.get(&src_pos) {
 | 
			
		||||
            }
 | 
			
		||||
            // Mouse event
 | 
			
		||||
            else if let Event::Mouse(mouse) = event {
 | 
			
		||||
                let src_pos = Coordinates::new(mouse.column, mouse.row);
 | 
			
		||||
 | 
			
		||||
                // 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() {
 | 
			
		||||
                            // Change of selected boat
 | 
			
		||||
                            Some(b) if b != model.layout.0[model.curr_boat] => {
 | 
			
		||||
                                model.curr_boat =
 | 
			
		||||
                                    model.layout.0.iter().position(|s| s == &b).unwrap();
 | 
			
		||||
                                false
 | 
			
		||||
                            }
 | 
			
		||||
 | 
			
		||||
                            // 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;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user