1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10:06 -04:00

Plot items now have optional id which is returned in the plot's response when hovered (#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 <emil.ernerfeldt@gmail.com>
This commit is contained in:
Andreas Reich
2024-01-30 15:55:56 +01:00
committed by GitHub
parent 945a69d2f2
commit ca513ce241
4 changed files with 180 additions and 18 deletions

View File

@@ -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(),
@@ -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
}
}