mirror of https://codeberg.org/topola/topola.git
egui: implement moving the canvas horizontally and vertically
This commit is contained in:
parent
b43111030e
commit
4959f3512b
|
|
@ -26,7 +26,7 @@ pub struct App {
|
|||
design: Option<DsnDesign>,
|
||||
|
||||
#[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));
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue