Skip to content

Commit

Permalink
Plot auto-bounds API improvement (part 2/2): add API (#3586)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
abey79 authored Nov 21, 2023
1 parent 49eecc4 commit 05a3c4c
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion crates/egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(),
Expand Down Expand Up @@ -989,6 +990,7 @@ impl Plot {
bounds.translate(delta);
auto_bounds = false.into();
}
BoundsModification::AutoBounds(new_auto_bounds) => auto_bounds = new_auto_bounds,
}
}

Expand Down Expand Up @@ -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
Expand All @@ -1335,6 +1338,7 @@ pub struct PlotUi {
items: Vec<Box<dyn PlotItem>>,
next_auto_color_idx: usize,
last_plot_transform: PlotTransform,
last_auto_bounds: Vec2b,
response: Response,
bounds_modifications: Vec<BoundsModification>,
ctx: Context,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 05a3c4c

Please sign in to comment.