From 5976cbd3c864576fde4df6a05052f6fa0559751d Mon Sep 17 00:00:00 2001 From: Ygor Souza Date: Tue, 21 May 2024 20:03:18 +0200 Subject: [PATCH 1/3] Add a method to set the interact distance on the Plot Closes #4519 --- crates/egui_demo_lib/src/demo/plot_demo.rs | 8 ++++++++ crates/egui_plot/src/lib.rs | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) 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() From 0490cde55ab8d44f3ae2e9e7e134933a902cb1d1 Mon Sep 17 00:00:00 2001 From: Ygor Souza Date: Mon, 27 May 2024 17:18:00 +0200 Subject: [PATCH 2/3] Use the interact_radius from the UI style on the plot --- crates/egui_demo_lib/src/demo/plot_demo.rs | 12 ++++++------ crates/egui_plot/src/lib.rs | 18 +----------------- 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/crates/egui_demo_lib/src/demo/plot_demo.rs b/crates/egui_demo_lib/src/demo/plot_demo.rs index 326fd18edce..e8644ef3ab0 100644 --- a/crates/egui_demo_lib/src/demo/plot_demo.rs +++ b/crates/egui_demo_lib/src/demo/plot_demo.rs @@ -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 { @@ -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, } } } @@ -167,7 +167,7 @@ impl LineDemo { show_axes, show_grid, line_style, - interact_distance, + interact_radius, } = self; ui.horizontal(|ui| { @@ -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"); }); }); @@ -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 { @@ -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()); diff --git a/crates/egui_plot/src/lib.rs b/crates/egui_plot/src/lib.rs index 75fdc7a2d4e..6dec9d7276d 100644 --- a/crates/egui_plot/src/lib.rs +++ b/crates/egui_plot/src/lib.rs @@ -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>, // default x axes @@ -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)], @@ -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 /// /// ``` @@ -764,7 +751,6 @@ impl<'a> Plot<'a> { view_aspect, mut show_x, mut show_y, - interact_distance, label_formatter, coordinates_formatter, x_axes, @@ -1187,7 +1173,6 @@ impl<'a> Plot<'a> { items, show_x, show_y, - interact_distance, label_formatter, coordinates_formatter, show_grid, @@ -1470,7 +1455,6 @@ 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], @@ -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() From 641ca9b7b417a1e193e7054736adec9ba981a8fd Mon Sep 17 00:00:00 2001 From: Ygor Souza Date: Mon, 27 May 2024 19:01:27 +0200 Subject: [PATCH 3/3] Revert changes to demo app --- crates/egui_demo_lib/src/demo/plot_demo.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/crates/egui_demo_lib/src/demo/plot_demo.rs b/crates/egui_demo_lib/src/demo/plot_demo.rs index e8644ef3ab0..12e45aa0f09 100644 --- a/crates/egui_demo_lib/src/demo/plot_demo.rs +++ b/crates/egui_demo_lib/src/demo/plot_demo.rs @@ -133,7 +133,6 @@ struct LineDemo { show_axes: bool, show_grid: bool, line_style: LineStyle, - interact_radius: f32, } impl Default for LineDemo { @@ -149,7 +148,6 @@ impl Default for LineDemo { show_axes: true, show_grid: true, line_style: LineStyle::Solid, - interact_radius: egui::style::Interaction::default().interact_radius, } } } @@ -167,7 +165,6 @@ impl LineDemo { show_axes, show_grid, line_style, - interact_radius, } = self; ui.horizontal(|ui| { @@ -200,10 +197,6 @@ 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_radius).clamp_range(0.0..=100.0)); - ui.label("Interact radius"); - }); }); ui.vertical(|ui| { @@ -296,7 +289,6 @@ 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());