From 4959f3512b361d5d44688db497f2ab9992a5cef7 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Tue, 5 Mar 2024 18:51:17 +0000 Subject: [PATCH] egui: implement moving the canvas horizontally and vertically --- src/bin/topola-egui/app.rs | 18 ++++++++---------- src/bin/topola-egui/painter.rs | 9 ++++++--- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/bin/topola-egui/app.rs b/src/bin/topola-egui/app.rs index ad9e700..52f82ef 100644 --- a/src/bin/topola-egui/app.rs +++ b/src/bin/topola-egui/app.rs @@ -26,7 +26,7 @@ pub struct App { design: Option, #[serde(skip)] - zoom_factor: f32, + from_rect: egui::emath::Rect, } impl Default for App { @@ -36,7 +36,7 @@ impl Default for App { label: "Hello World!".to_owned(), file_handle_channel: channel(), design: None, - zoom_factor: 1.0, + from_rect: egui::Rect::from_x_y_ranges(0.0..=1000.0, 0.0..=500.0), } } } @@ -111,25 +111,23 @@ impl eframe::App for App { let desired_size = ui.available_width() * egui::vec2(1.0, 0.5); let (_id, viewport_rect) = ui.allocate_space(desired_size); - self.zoom_factor = self.zoom_factor * ctx.input(|i| i.zoom_delta()); + self.from_rect = self.from_rect.translate(ctx.input(|i| -i.raw_scroll_delta)); + self.from_rect = self.from_rect / ctx.input(|i| i.zoom_delta()); - let transform = egui::emath::RectTransform::from_to( - egui::Rect::from_x_y_ranges(0.0..=1000.0, 0.0..=500.0) / self.zoom_factor, - viewport_rect, - ); + let transform = egui::emath::RectTransform::from_to(self.from_rect, viewport_rect); let mut painter = Painter::new(ui, transform); let dot_shape = Shape::Dot(DotShape { c: Circle { pos: [50.0, 100.0].into(), - r: 10.0 * self.zoom_factor as f64, + r: 10.0, }, }); let seg_shape = Shape::Seg(SegShape { from: [200.0, 25.0].into(), to: [300.0, 300.0].into(), - width: 5.0 * self.zoom_factor as f64, + width: 5.0, }); let bend_shape = Shape::Bend(BendShape { @@ -139,7 +137,7 @@ impl eframe::App for App { pos: [130.0, 130.0].into(), r: 30.0, }, - width: 12.0 * self.zoom_factor as f64, + width: 12.0, }); painter.paint_shape(&dot_shape, egui::Color32::from_rgb(255, 0, 0)); diff --git a/src/bin/topola-egui/painter.rs b/src/bin/topola-egui/painter.rs index 2f2a8e7..5824c8d 100644 --- a/src/bin/topola-egui/painter.rs +++ b/src/bin/topola-egui/painter.rs @@ -17,7 +17,7 @@ impl<'a> Painter<'a> { Shape::Dot(dot) => epaint::Shape::circle_filled( self.transform .transform_pos([dot.c.pos.x() as f32, dot.c.pos.y() as f32].into()), - dot.c.r as f32, + dot.c.r as f32 * self.transform.scale().x, color, ), Shape::Seg(seg) => epaint::Shape::line_segment( @@ -27,7 +27,7 @@ impl<'a> Painter<'a> { self.transform .transform_pos([seg.to.x() as f32, seg.to.y() as f32].into()), ], - egui::Stroke::new(seg.width as f32, color), + egui::Stroke::new(seg.width as f32 * self.transform.scale().x, color), ), Shape::Bend(bend) => { let delta_from = bend.from - bend.c.pos; @@ -45,7 +45,10 @@ impl<'a> Painter<'a> { points.push(self.transform.transform_pos([x as f32, y as f32].into())); } - epaint::Shape::line(points, egui::Stroke::new(bend.width as f32, color)) + epaint::Shape::line( + points, + egui::Stroke::new(bend.width as f32 * self.transform.scale().x, color), + ) } };