aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArne Dußin2020-10-29 03:13:10 +0100
committerArne Dußin2020-10-29 03:13:10 +0100
commitb12d0494f54d781a0b9f467a4fc3e4e255dd9839 (patch)
treebdad4cddca6c44564c67cf35feeed76658875ee4 /src
parent771f94fb2383176bcd3faced3edb7daa044065eb (diff)
downloadgraf_karto-b12d0494f54d781a0b9f467a4fc3e4e255dd9839.tar.gz
graf_karto-b12d0494f54d781a0b9f467a4fc3e4e255dd9839.zip
Make map draggable
The map can be dragged around, but since the grid is not yet infinitely big, it feels quite weird to zoom and drag
Diffstat (limited to 'src')
-rw-r--r--src/main.rs32
-rw-r--r--src/transform.rs5
2 files changed, 33 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 {
diff --git a/src/transform.rs b/src/transform.rs
index ef94029..a91d7da 100644
--- a/src/transform.rs
+++ b/src/transform.rs
@@ -121,6 +121,11 @@ impl Transform {
}
}
+ /// Move the canvas by the vector in pixels.
+ pub fn move_by_px(&mut self, by: [f64; 2]) {
+ self.translation_px = math::add(self.translation_px, by);
+ }
+
pub fn pixels_per_m(&self) -> f64 {
self.pixels_per_m
}