Add Plot::allow_scroll (#1382)

This commit is contained in:
Lucas Kent 2022-04-03 18:43:55 +10:00 committed by GitHub
parent 95efbbc03e
commit c2039920de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View file

@ -10,6 +10,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
* Added `Shape::Callback` for backend-specific painting, [with an example](https://github.com/emilk/egui/blob/master/eframe/examples/custom_3d_three-d.rs) ([#1351](https://github.com/emilk/egui/pull/1351)). * Added `Shape::Callback` for backend-specific painting, [with an example](https://github.com/emilk/egui/blob/master/eframe/examples/custom_3d_three-d.rs) ([#1351](https://github.com/emilk/egui/pull/1351)).
* Added `Frame::canvas` ([#1362](https://github.com/emilk/egui/pull/1362)). * Added `Frame::canvas` ([#1362](https://github.com/emilk/egui/pull/1362)).
* `Context::request_repaint` will wake up UI thread, if integrations has called `Context::set_request_repaint_callback` ([#1366](https://github.com/emilk/egui/pull/1366)). * `Context::request_repaint` will wake up UI thread, if integrations has called `Context::set_request_repaint_callback` ([#1366](https://github.com/emilk/egui/pull/1366)).
* Added `Plot::allow_scroll`, `Plot::allow_zoom` no longer affects scrolling ([#1382](https://github.com/emilk/egui/pull/1382)).
* Added `Ui::push_id` ([#1374](https://github.com/emilk/egui/pull/1374)). * Added `Ui::push_id` ([#1374](https://github.com/emilk/egui/pull/1374)).
* Added `Frame::outer_margin`. * Added `Frame::outer_margin`.

View file

@ -165,6 +165,7 @@ pub struct Plot {
center_y_axis: bool, center_y_axis: bool,
allow_zoom: bool, allow_zoom: bool,
allow_drag: bool, allow_drag: bool,
allow_scroll: bool,
min_auto_bounds: PlotBounds, min_auto_bounds: PlotBounds,
margin_fraction: Vec2, margin_fraction: Vec2,
allow_boxed_zoom: bool, allow_boxed_zoom: bool,
@ -197,6 +198,7 @@ impl Plot {
center_y_axis: false, center_y_axis: false,
allow_zoom: true, allow_zoom: true,
allow_drag: true, allow_drag: true,
allow_scroll: true,
min_auto_bounds: PlotBounds::NOTHING, min_auto_bounds: PlotBounds::NOTHING,
margin_fraction: Vec2::splat(0.05), margin_fraction: Vec2::splat(0.05),
allow_boxed_zoom: true, allow_boxed_zoom: true,
@ -288,6 +290,12 @@ impl Plot {
self self
} }
/// Whether to allow scrolling in the plot. Default: `true`.
pub fn allow_scroll(mut self, on: bool) -> Self {
self.allow_scroll = on;
self
}
/// Set the side margin as a fraction of the plot size. /// Set the side margin as a fraction of the plot size.
/// ///
/// For instance, a value of `0.1` will add 10% space on both sides. /// For instance, a value of `0.1` will add 10% space on both sides.
@ -435,6 +443,7 @@ impl Plot {
center_x_axis, center_x_axis,
center_y_axis, center_y_axis,
allow_zoom, allow_zoom,
allow_scroll,
allow_drag, allow_drag,
allow_boxed_zoom, allow_boxed_zoom,
boxed_zoom_pointer_button: boxed_zoom_pointer, boxed_zoom_pointer_button: boxed_zoom_pointer,
@ -661,8 +670,8 @@ impl Plot {
} }
} }
if allow_zoom {
if let Some(hover_pos) = response.hover_pos() { if let Some(hover_pos) = response.hover_pos() {
if allow_zoom {
let zoom_factor = if data_aspect.is_some() { let zoom_factor = if data_aspect.is_some() {
Vec2::splat(ui.input().zoom_delta()) Vec2::splat(ui.input().zoom_delta())
} else { } else {
@ -672,7 +681,8 @@ impl Plot {
transform.zoom(zoom_factor, hover_pos); transform.zoom(zoom_factor, hover_pos);
auto_bounds = false; auto_bounds = false;
} }
}
if allow_scroll {
let scroll_delta = ui.input().scroll_delta; let scroll_delta = ui.input().scroll_delta;
if scroll_delta != Vec2::ZERO { if scroll_delta != Vec2::ZERO {
transform.translate_bounds(-scroll_delta); transform.translate_bounds(-scroll_delta);

View file

@ -21,6 +21,7 @@ pub struct ContextMenus {
show_axes: [bool; 2], show_axes: [bool; 2],
allow_drag: bool, allow_drag: bool,
allow_zoom: bool, allow_zoom: bool,
allow_scroll: bool,
center_x_axis: bool, center_x_axis: bool,
center_y_axis: bool, center_y_axis: bool,
width: f32, width: f32,
@ -34,6 +35,7 @@ impl Default for ContextMenus {
show_axes: [true, true], show_axes: [true, true],
allow_drag: true, allow_drag: true,
allow_zoom: true, allow_zoom: true,
allow_scroll: true,
center_x_axis: false, center_x_axis: false,
center_y_axis: false, center_y_axis: false,
width: 400.0, width: 400.0,
@ -99,6 +101,7 @@ impl super::View for ContextMenus {
ui.end_row(); ui.end_row();
if ui.checkbox(&mut self.allow_drag, "Drag").changed() if ui.checkbox(&mut self.allow_drag, "Drag").changed()
|| ui.checkbox(&mut self.allow_zoom, "Zoom").changed() || ui.checkbox(&mut self.allow_zoom, "Zoom").changed()
|| ui.checkbox(&mut self.allow_scroll, "Scroll").changed()
{ {
ui.close_menu(); ui.close_menu();
} }
@ -128,6 +131,7 @@ impl ContextMenus {
.show_axes(self.show_axes) .show_axes(self.show_axes)
.allow_drag(self.allow_drag) .allow_drag(self.allow_drag)
.allow_zoom(self.allow_zoom) .allow_zoom(self.allow_zoom)
.allow_scroll(self.allow_scroll)
.center_x_axis(self.center_x_axis) .center_x_axis(self.center_x_axis)
.center_x_axis(self.center_y_axis) .center_x_axis(self.center_y_axis)
.width(self.width) .width(self.width)