Skip to content

Commit

Permalink
Add a method to set the interact distance on the Plot
Browse files Browse the repository at this point in the history
Closes emilk#4519
  • Loading branch information
YgorSouza committed May 27, 2024
1 parent 1ae2d28 commit 5976cbd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions crates/egui_demo_lib/src/demo/plot_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ struct LineDemo {
show_axes: bool,
show_grid: bool,
line_style: LineStyle,
interact_distance: f32,
}

impl Default for LineDemo {
Expand All @@ -148,6 +149,7 @@ impl Default for LineDemo {
show_axes: true,
show_grid: true,
line_style: LineStyle::Solid,
interact_distance: 16.0,
}
}
}
Expand All @@ -165,6 +167,7 @@ impl LineDemo {
show_axes,
show_grid,
line_style,
interact_distance,
} = self;

ui.horizontal(|ui| {
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 17 additions & 1 deletion crates/egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AxisHints<'a>>, // default x axes
Expand Down Expand Up @@ -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)],
Expand Down Expand Up @@ -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
///
/// ```
Expand Down Expand Up @@ -751,6 +764,7 @@ impl<'a> Plot<'a> {
view_aspect,
mut show_x,
mut show_y,
interact_distance,
label_formatter,
coordinates_formatter,
x_axes,
Expand Down Expand Up @@ -1173,6 +1187,7 @@ impl<'a> Plot<'a> {
items,
show_x,
show_y,
interact_distance,
label_formatter,
coordinates_formatter,
show_grid,
Expand Down Expand Up @@ -1455,6 +1470,7 @@ 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 @@ -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()
Expand Down

0 comments on commit 5976cbd

Please sign in to comment.