diff --git a/crates/egui_demo_lib/src/demo/plot_demo.rs b/crates/egui_demo_lib/src/demo/plot_demo.rs index 12e45aa0f09..326fd18edce 100644 --- a/crates/egui_demo_lib/src/demo/plot_demo.rs +++ b/crates/egui_demo_lib/src/demo/plot_demo.rs @@ -133,6 +133,7 @@ struct LineDemo { show_axes: bool, show_grid: bool, line_style: LineStyle, + interact_distance: f32, } impl Default for LineDemo { @@ -148,6 +149,7 @@ impl Default for LineDemo { show_axes: true, show_grid: true, line_style: LineStyle::Solid, + interact_distance: 16.0, } } } @@ -165,6 +167,7 @@ impl LineDemo { show_axes, show_grid, line_style, + interact_distance, } = self; ui.horizontal(|ui| { @@ -197,6 +200,10 @@ impl LineDemo { ui.checkbox(show_grid, "Show grid"); 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.vertical(|ui| { @@ -278,6 +285,7 @@ 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 { diff --git a/crates/egui_plot/src/lib.rs b/crates/egui_plot/src/lib.rs index a46b7a225a5..75fdc7a2d4e 100644 --- a/crates/egui_plot/src/lib.rs +++ b/crates/egui_plot/src/lib.rs @@ -170,6 +170,7 @@ 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>, // default x axes @@ -217,6 +218,7 @@ 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)], @@ -382,6 +384,17 @@ 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 /// /// ``` @@ -751,6 +764,7 @@ impl<'a> Plot<'a> { view_aspect, mut show_x, mut show_y, + interact_distance, label_formatter, coordinates_formatter, x_axes, @@ -1173,6 +1187,7 @@ impl<'a> Plot<'a> { items, show_x, show_y, + interact_distance, label_formatter, coordinates_formatter, show_grid, @@ -1455,6 +1470,7 @@ struct PreparedPlot<'a> { items: Vec>, show_x: bool, show_y: bool, + interact_distance: f32, label_formatter: LabelFormatter<'a>, coordinates_formatter: Option<(Corner, CoordinatesFormatter<'a>)>, // axis_formatters: [AxisFormatter; 2], @@ -1666,7 +1682,7 @@ impl<'a> PreparedPlot<'a> { return (Vec::new(), None); } - let interact_radius_sq = (16.0_f32).powi(2); + let interact_radius_sq = self.interact_distance.powi(2); let candidates = items .iter()