Skip to content

Commit 1afc771

Browse files
committed
fixups for latest egui, allow selecting entities in bar chart plot
1 parent a6c43a4 commit 1afc771

6 files changed

Lines changed: 48 additions & 27 deletions

File tree

crates/re_space_view_bar_chart/src/space_view_class.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use egui::util::hash;
1+
use egui::{ahash::HashMap, util::hash};
22
use re_entity_db::{EditableAutoValue, EntityProperties, LegendCorner};
33
use re_log_types::EntityPath;
44
use re_space_view::{controls, suggest_space_view_for_each_entity};
@@ -132,11 +132,11 @@ impl SpaceViewClass for BarChartSpaceView {
132132

133133
fn ui(
134134
&self,
135-
_ctx: &ViewerContext<'_>,
135+
ctx: &ViewerContext<'_>,
136136
ui: &mut egui::Ui,
137137
_state: &mut Self::State,
138138
root_entity_properties: &EntityProperties,
139-
_query: &ViewQuery<'_>,
139+
query: &ViewQuery<'_>,
140140
system_output: re_viewer_context::SystemExecutionOutput,
141141
) -> Result<(), SpaceViewSystemExecutionError> {
142142
use egui_plot::{Bar, BarChart, Legend, Plot};
@@ -164,7 +164,13 @@ impl SpaceViewClass for BarChartSpaceView {
164164
);
165165
}
166166

167-
plot.show(ui, |plot_ui| {
167+
let mut plot_item_id_to_entity_path = HashMap::default();
168+
169+
let egui_plot::PlotResponse {
170+
response,
171+
hovered_plot_item,
172+
..
173+
} = plot.show(ui, |plot_ui| {
168174
fn create_bar_chart<N: Into<f64>>(
169175
ent_path: &EntityPath,
170176
values: impl Iterator<Item = N>,
@@ -251,9 +257,26 @@ impl SpaceViewClass for BarChartSpaceView {
251257
}
252258
};
253259

260+
let id = egui::Id::new(ent_path.hash());
261+
plot_item_id_to_entity_path.insert(id, ent_path.clone());
262+
let chart = chart.id(id);
263+
254264
plot_ui.bar_chart(chart);
255265
}
256266
});
267+
268+
// Interact with the plot items.
269+
if let Some(entity_path) = hovered_plot_item
270+
.and_then(|hovered_plot_item| plot_item_id_to_entity_path.get(&hovered_plot_item))
271+
{
272+
ctx.select_hovered_on_click(
273+
&response,
274+
re_viewer_context::Item::InstancePath(
275+
Some(query.space_view_id),
276+
entity_path.clone().into(),
277+
),
278+
);
279+
}
257280
});
258281

259282
Ok(())

crates/re_space_view_spatial/src/ui.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,13 @@ pub fn outline_config(gui_ctx: &egui::Context) -> OutlineConfig {
393393

394394
pub fn screenshot_context_menu(
395395
_ctx: &ViewerContext<'_>,
396-
response: egui::Response,
397-
) -> (egui::Response, Option<ScreenshotMode>) {
396+
response: &egui::Response,
397+
) -> Option<ScreenshotMode> {
398398
#[cfg(not(target_arch = "wasm32"))]
399399
{
400400
if _ctx.app_options.experimental_space_view_screenshots {
401401
let mut take_screenshot = None;
402-
let response = response.context_menu(|ui| {
402+
response.context_menu(|ui| {
403403
ui.style_mut().wrap = Some(false);
404404
if ui.button("Save screenshot to disk").clicked() {
405405
take_screenshot = Some(ScreenshotMode::SaveAndCopyToClipboard);
@@ -409,14 +409,14 @@ pub fn screenshot_context_menu(
409409
ui.close_menu();
410410
}
411411
});
412-
(response, take_screenshot)
412+
take_screenshot
413413
} else {
414-
(response, None)
414+
None
415415
}
416416
}
417417
#[cfg(target_arch = "wasm32")]
418418
{
419-
(response, None)
419+
None
420420
}
421421
}
422422

@@ -661,7 +661,7 @@ pub fn picking(
661661
});
662662
};
663663

664-
ctx.select_hovered_on_click( &response, re_viewer_context::Selection(hovered_items));
664+
ctx.select_hovered_on_click(&response, re_viewer_context::Selection(hovered_items));
665665

666666
Ok(response)
667667
}

crates/re_space_view_spatial/src/ui_2d.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,7 @@ pub fn view_2d(
355355
// ------------------------------------------------------------------------
356356

357357
// Screenshot context menu.
358-
let (_, screenshot_mode) = screenshot_context_menu(ctx, response);
359-
if let Some(mode) = screenshot_mode {
358+
if let Some(mode) = screenshot_context_menu(ctx, &response) {
360359
view_builder
361360
.schedule_screenshot(ctx.render_ctx, query.space_view_id.gpu_readback_id(), mode)
362361
.ok();

crates/re_space_view_spatial/src/ui_3d.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,7 @@ pub fn view_3d(
581581
}
582582

583583
// Screenshot context menu.
584-
let (_, screenshot_mode) = screenshot_context_menu(ctx, response);
585-
if let Some(mode) = screenshot_mode {
584+
if let Some(mode) = screenshot_context_menu(ctx, &response) {
586585
view_builder
587586
.schedule_screenshot(ctx.render_ctx, query.space_view_id.gpu_readback_id(), mode)
588587
.ok();

crates/re_space_view_time_series/src/space_view_class.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -426,16 +426,16 @@ impl SpaceViewClass for TimeSeriesSpaceView {
426426
});
427427

428428
// Interact with the plot items (lines, scatters, etc.)
429-
if let Some(hovered_plot_item) = hovered_plot_item {
430-
if let Some(entity_path) = plot_item_id_to_entity_path.get(&hovered_plot_item) {
431-
ctx.select_hovered_on_click(
432-
&response,
433-
re_viewer_context::Item::InstancePath(
434-
Some(query.space_view_id),
435-
entity_path.clone().into(),
436-
),
437-
);
438-
}
429+
if let Some(entity_path) = hovered_plot_item
430+
.and_then(|hovered_plot_item| plot_item_id_to_entity_path.get(&hovered_plot_item))
431+
{
432+
ctx.select_hovered_on_click(
433+
&response,
434+
re_viewer_context::Item::InstancePath(
435+
Some(query.space_view_id),
436+
entity_path.clone().into(),
437+
),
438+
);
439439
}
440440

441441
if let Some(time_x) = time_x {

crates/re_viewer/src/ui/selection_history_ui.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl SelectionHistoryUi {
4343
));
4444

4545
let mut return_current = false;
46-
let response = response.context_menu(|ui| {
46+
response.context_menu(|ui| {
4747
// undo: newest on top, oldest on bottom
4848
let cur = history.current;
4949
for i in (0..history.current).rev() {
@@ -93,7 +93,7 @@ impl SelectionHistoryUi {
9393
));
9494

9595
let mut return_current = false;
96-
let response = response.context_menu(|ui| {
96+
response.context_menu(|ui| {
9797
// redo: oldest on top, most recent on bottom
9898
let cur = history.current;
9999
for i in (history.current + 1)..history.stack.len() {

0 commit comments

Comments
 (0)