From 05a3c4c0f13a1eed11170250c55f993e7fb78d85 Mon Sep 17 00:00:00 2001 From: Antoine Beyeler <49431240+abey79@users.noreply.github.com> Date: Tue, 21 Nov 2023 11:56:39 +0100 Subject: [PATCH] Plot auto-bounds API improvement (part 2/2): add API (#3586) Part 2 of 2 of adding a better API for egui_plot's auto-bounds feature. In this PR: add `auto_bounds()`/`set_auto_bounds()` APIs to PlotUI --- crates/egui_plot/src/lib.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/egui_plot/src/lib.rs b/crates/egui_plot/src/lib.rs index 912d64d9d31..f7eef12b0fd 100644 --- a/crates/egui_plot/src/lib.rs +++ b/crates/egui_plot/src/lib.rs @@ -854,7 +854,7 @@ impl Plot { PlotMemory::load(ui.ctx(), plot_id) } .unwrap_or_else(|| PlotMemory { - auto_bounds: true.into(), + auto_bounds: default_auto_bounds, hovered_entry: None, hidden_items: Default::default(), last_plot_transform: PlotTransform::new( @@ -879,6 +879,7 @@ impl Plot { items: Vec::new(), next_auto_color_idx: 0, last_plot_transform, + last_auto_bounds: auto_bounds, response, bounds_modifications: Vec::new(), ctx: ui.ctx().clone(), @@ -989,6 +990,7 @@ impl Plot { bounds.translate(delta); auto_bounds = false.into(); } + BoundsModification::AutoBounds(new_auto_bounds) => auto_bounds = new_auto_bounds, } } @@ -1327,6 +1329,7 @@ fn axis_widgets( enum BoundsModification { Set(PlotBounds), Translate(Vec2), + AutoBounds(Vec2b), } /// Provides methods to interact with a plot while building it. It is the single argument of the closure @@ -1335,6 +1338,7 @@ pub struct PlotUi { items: Vec>, next_auto_color_idx: usize, last_plot_transform: PlotTransform, + last_auto_bounds: Vec2b, response: Response, bounds_modifications: Vec, ctx: Context, @@ -1372,6 +1376,18 @@ impl PlotUi { .push(BoundsModification::Translate(delta_pos)); } + /// Whether the plot axes were in auto-bounds mode in the last frame. If called on the first + /// frame, this is the [`Plot`]'s default auto-bounds mode. + pub fn auto_bounds(&self) -> Vec2b { + self.last_auto_bounds + } + + /// Set the auto-bounds mode for the plot axes. + pub fn set_auto_bounds(&mut self, auto_bounds: Vec2b) { + self.bounds_modifications + .push(BoundsModification::AutoBounds(auto_bounds)); + } + /// Can be used to check if the plot was hovered or clicked. pub fn response(&self) -> &Response { &self.response