Skip to content

Commit

Permalink
Plot items now have optional id which is returned in the plot's respo…
Browse files Browse the repository at this point in the history
…nse when hovered (emilk#3920)

This allows users to check which item the user interacts with in the
plot.



https://github.com/emilk/egui/assets/1220815/1a174b38-8414-49be-a802-d187cd93d154

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
2 people authored and hacknus committed Oct 30, 2024
1 parent 7ad7fb3 commit ed86635
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 18 deletions.
29 changes: 29 additions & 0 deletions crates/egui_demo_lib/src/demo/plot_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,28 @@ impl InteractionDemo {
let PlotResponse {
response,
inner: (screen_pos, pointer_coordinate, pointer_coordinate_drag_delta, bounds, hovered),
hovered_plot_item,
..
} = plot.show(ui, |plot_ui| {
plot_ui.line(
Line::new(PlotPoints::from_explicit_callback(
move |x| x.sin(),
..,
100,
))
.color(Color32::RED)
.id(egui::Id::new("sin")),
);
plot_ui.line(
Line::new(PlotPoints::from_explicit_callback(
move |x| x.cos(),
..,
100,
))
.color(Color32::BLUE)
.id(egui::Id::new("cos")),
);

(
plot_ui.screen_from_plot(PlotPoint::new(0.0, 0.0)),
plot_ui.pointer_coordinate(),
Expand Down Expand Up @@ -824,6 +844,15 @@ impl InteractionDemo {
);
ui.label(format!("pointer coordinate drag delta: {coordinate_text}"));

let hovered_item = if hovered_plot_item == Some(egui::Id::new("sin")) {
"red sin"
} else if hovered_plot_item == Some(egui::Id::new("cos")) {
"blue cos"
} else {
"none"
};
ui.label(format!("hovered plot item: {hovered_item}"));

response
}
}
Expand Down
Loading

0 comments on commit ed86635

Please sign in to comment.