Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto_bounds/set_auto_bounds to PlotUi #3586

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Collaborator

@Wumpf Wumpf Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was wondering about this one on the previous PR that added default_auto_bounds. Got me, I should have commented 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the default_auto_bounds were just renamed, so that was a pre-existing bug I believe.

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