diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 284ee53..81677e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,12 +40,21 @@ fn main() { // Line used to draw the square grid. let grid_line = Line::new([1., 1., 1., 0.3], 1.5); + let mut mouse_pos_px = [0., 0.]; let mut mouse_pos_m = [0., 0.]; let mut events = Events::new(EventSettings::new().lazy(true)); + let mut canvas_follows_mouse = false; while let Some(e) = events.next(&mut window) { - // Update the mouse cursor position - e.mouse_cursor(|pos| { - mouse_pos_m = transform.point_px_to_m(pos); + // Update the mouse cursor position and possibly the canvas position too, in case the user + // is currently dragging the canvas. + e.mouse_cursor(|new_pos| { + if canvas_follows_mouse { + let move_by = math::sub(new_pos, mouse_pos_px); + transform.move_by_px(move_by); + } + + mouse_pos_px = new_pos; + mouse_pos_m = transform.point_px_to_m(new_pos); }); // The zoom factor is changed with the mouse wheel. @@ -67,6 +76,16 @@ fn main() { } }); + // Handle the movement of the canvas when pressing the middle mouse button. + if let Some(Button::Mouse(MouseButton::Middle)) = e.press_args() { + canvas_follows_mouse = true; + println!("Canvas now follows the mouse"); + } + if let Some(Button::Mouse(MouseButton::Middle)) = e.release_args() { + canvas_follows_mouse = false; + println!("Canvas no longer follows the mouse"); + } + // Handle drawing a rectangle or finishing the rectangle when clicking with the mouse. if let Some(Button::Mouse(MouseButton::Left)) = e.press_args() { if let Some(first_point) = starting_rect_point { @@ -100,7 +119,12 @@ fn main() { window.draw_2d(&e, |c, g, _device| { clear([0.4, 0.2, 0., 1.], g); - grid.draw(&grid_line, &c.draw_state, c.transform, g); + grid.draw( + &grid_line, + &c.draw_state, + c.trans_pos(transform.translation_px()).transform, + g, + ); // Draw all rectangles that are part of the map for &rect in &rectangles { |
