Skip to content

Commit

Permalink
Use the interact_radius from the UI style on the plot
Browse files Browse the repository at this point in the history
  • Loading branch information
YgorSouza committed May 27, 2024
1 parent 5976cbd commit 0490cde
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
12 changes: 6 additions & 6 deletions crates/egui_demo_lib/src/demo/plot_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ struct LineDemo {
show_axes: bool,
show_grid: bool,
line_style: LineStyle,
interact_distance: f32,
interact_radius: f32,
}

impl Default for LineDemo {
Expand All @@ -149,7 +149,7 @@ impl Default for LineDemo {
show_axes: true,
show_grid: true,
line_style: LineStyle::Solid,
interact_distance: 16.0,
interact_radius: egui::style::Interaction::default().interact_radius,
}
}
}
Expand All @@ -167,7 +167,7 @@ impl LineDemo {
show_axes,
show_grid,
line_style,
interact_distance,
interact_radius,
} = self;

ui.horizontal(|ui| {
Expand Down Expand Up @@ -201,8 +201,8 @@ impl LineDemo {
ui.checkbox(coordinates, "Show coordinates on hover")
.on_hover_text("Can take a custom formatting function.");
ui.horizontal(|ui| {
ui.add(egui::DragValue::new(interact_distance).clamp_range(0.0..=100.0));
ui.label("Interact distance");
ui.add(egui::DragValue::new(interact_radius).clamp_range(0.0..=100.0));
ui.label("Interact radius");
});
});

Expand Down Expand Up @@ -285,7 +285,6 @@ impl LineDemo {
let mut plot = Plot::new("lines_demo")
.legend(Legend::default())
.y_axis_width(4)
.interact_distance(self.interact_distance)
.show_axes(self.show_axes)
.show_grid(self.show_grid);
if self.square {
Expand All @@ -297,6 +296,7 @@ impl LineDemo {
if self.coordinates {
plot = plot.coordinates_formatter(Corner::LeftBottom, CoordinatesFormatter::default());
}
ui.style_mut().interaction.interact_radius = self.interact_radius;
plot.show(ui, |plot_ui| {
plot_ui.line(self.circle());
plot_ui.line(self.sin());
Expand Down
18 changes: 1 addition & 17 deletions crates/egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ pub struct Plot<'a> {

show_x: bool,
show_y: bool,
interact_distance: f32,
label_formatter: LabelFormatter<'a>,
coordinates_formatter: Option<(Corner, CoordinatesFormatter<'a>)>,
x_axes: Vec<AxisHints<'a>>, // default x axes
Expand Down Expand Up @@ -218,7 +217,6 @@ impl<'a> Plot<'a> {

show_x: true,
show_y: true,
interact_distance: 16.0,
label_formatter: None,
coordinates_formatter: None,
x_axes: vec![AxisHints::new(Axis::X)],
Expand Down Expand Up @@ -384,17 +382,6 @@ impl<'a> Plot<'a> {
self
}

/// The snapping distance for hovering an item or plot point
///
/// The cursor will snap to the closest item to the pointer if it is closer than this value.
///
/// Default: `16.0`
#[inline]
pub fn interact_distance(mut self, distance: f32) -> Self {
self.interact_distance = distance;
self
}

/// Provide a function to customize the on-hover label for the x and y axis
///
/// ```
Expand Down Expand Up @@ -764,7 +751,6 @@ impl<'a> Plot<'a> {
view_aspect,
mut show_x,
mut show_y,
interact_distance,
label_formatter,
coordinates_formatter,
x_axes,
Expand Down Expand Up @@ -1187,7 +1173,6 @@ impl<'a> Plot<'a> {
items,
show_x,
show_y,
interact_distance,
label_formatter,
coordinates_formatter,
show_grid,
Expand Down Expand Up @@ -1470,7 +1455,6 @@ struct PreparedPlot<'a> {
items: Vec<Box<dyn PlotItem>>,
show_x: bool,
show_y: bool,
interact_distance: f32,
label_formatter: LabelFormatter<'a>,
coordinates_formatter: Option<(Corner, CoordinatesFormatter<'a>)>,
// axis_formatters: [AxisFormatter; 2],
Expand Down Expand Up @@ -1682,7 +1666,7 @@ impl<'a> PreparedPlot<'a> {
return (Vec::new(), None);
}

let interact_radius_sq = self.interact_distance.powi(2);
let interact_radius_sq = ui.style().interaction.interact_radius.powi(2);

let candidates = items
.iter()
Expand Down

0 comments on commit 0490cde

Please sign in to comment.