Skip to content

Commit

Permalink
fixups for latest egui, allow selecting entities in bar chart plot
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Jan 30, 2024
1 parent a6c43a4 commit 1afc771
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 27 deletions.
31 changes: 27 additions & 4 deletions crates/re_space_view_bar_chart/src/space_view_class.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use egui::util::hash;
use egui::{ahash::HashMap, util::hash};
use re_entity_db::{EditableAutoValue, EntityProperties, LegendCorner};
use re_log_types::EntityPath;
use re_space_view::{controls, suggest_space_view_for_each_entity};
Expand Down Expand Up @@ -132,11 +132,11 @@ impl SpaceViewClass for BarChartSpaceView {

fn ui(
&self,
_ctx: &ViewerContext<'_>,
ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
_state: &mut Self::State,
root_entity_properties: &EntityProperties,
_query: &ViewQuery<'_>,
query: &ViewQuery<'_>,
system_output: re_viewer_context::SystemExecutionOutput,
) -> Result<(), SpaceViewSystemExecutionError> {
use egui_plot::{Bar, BarChart, Legend, Plot};
Expand Down Expand Up @@ -164,7 +164,13 @@ impl SpaceViewClass for BarChartSpaceView {
);
}

plot.show(ui, |plot_ui| {
let mut plot_item_id_to_entity_path = HashMap::default();

let egui_plot::PlotResponse {
response,
hovered_plot_item,
..
} = plot.show(ui, |plot_ui| {
fn create_bar_chart<N: Into<f64>>(
ent_path: &EntityPath,
values: impl Iterator<Item = N>,
Expand Down Expand Up @@ -251,9 +257,26 @@ impl SpaceViewClass for BarChartSpaceView {
}
};

let id = egui::Id::new(ent_path.hash());
plot_item_id_to_entity_path.insert(id, ent_path.clone());
let chart = chart.id(id);

plot_ui.bar_chart(chart);
}
});

// Interact with the plot items.
if let Some(entity_path) = hovered_plot_item
.and_then(|hovered_plot_item| plot_item_id_to_entity_path.get(&hovered_plot_item))
{
ctx.select_hovered_on_click(
&response,
re_viewer_context::Item::InstancePath(
Some(query.space_view_id),
entity_path.clone().into(),
),
);
}
});

Ok(())
Expand Down
14 changes: 7 additions & 7 deletions crates/re_space_view_spatial/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,13 @@ pub fn outline_config(gui_ctx: &egui::Context) -> OutlineConfig {

pub fn screenshot_context_menu(
_ctx: &ViewerContext<'_>,
response: egui::Response,
) -> (egui::Response, Option<ScreenshotMode>) {
response: &egui::Response,
) -> Option<ScreenshotMode> {
#[cfg(not(target_arch = "wasm32"))]
{
if _ctx.app_options.experimental_space_view_screenshots {
let mut take_screenshot = None;
let response = response.context_menu(|ui| {
response.context_menu(|ui| {
ui.style_mut().wrap = Some(false);
if ui.button("Save screenshot to disk").clicked() {
take_screenshot = Some(ScreenshotMode::SaveAndCopyToClipboard);
Expand All @@ -409,14 +409,14 @@ pub fn screenshot_context_menu(
ui.close_menu();
}
});
(response, take_screenshot)
take_screenshot
} else {
(response, None)
None
}
}
#[cfg(target_arch = "wasm32")]
{
(response, None)
None
}
}

Expand Down Expand Up @@ -661,7 +661,7 @@ pub fn picking(
});
};

ctx.select_hovered_on_click( &response, re_viewer_context::Selection(hovered_items));
ctx.select_hovered_on_click(&response, re_viewer_context::Selection(hovered_items));

Ok(response)
}
Expand Down
3 changes: 1 addition & 2 deletions crates/re_space_view_spatial/src/ui_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,7 @@ pub fn view_2d(
// ------------------------------------------------------------------------

// Screenshot context menu.
let (_, screenshot_mode) = screenshot_context_menu(ctx, response);
if let Some(mode) = screenshot_mode {
if let Some(mode) = screenshot_context_menu(ctx, &response) {
view_builder
.schedule_screenshot(ctx.render_ctx, query.space_view_id.gpu_readback_id(), mode)
.ok();
Expand Down
3 changes: 1 addition & 2 deletions crates/re_space_view_spatial/src/ui_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,7 @@ pub fn view_3d(
}

// Screenshot context menu.
let (_, screenshot_mode) = screenshot_context_menu(ctx, response);
if let Some(mode) = screenshot_mode {
if let Some(mode) = screenshot_context_menu(ctx, &response) {
view_builder
.schedule_screenshot(ctx.render_ctx, query.space_view_id.gpu_readback_id(), mode)
.ok();
Expand Down
20 changes: 10 additions & 10 deletions crates/re_space_view_time_series/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,16 @@ impl SpaceViewClass for TimeSeriesSpaceView {
});

// Interact with the plot items (lines, scatters, etc.)
if let Some(hovered_plot_item) = hovered_plot_item {
if let Some(entity_path) = plot_item_id_to_entity_path.get(&hovered_plot_item) {
ctx.select_hovered_on_click(
&response,
re_viewer_context::Item::InstancePath(
Some(query.space_view_id),
entity_path.clone().into(),
),
);
}
if let Some(entity_path) = hovered_plot_item
.and_then(|hovered_plot_item| plot_item_id_to_entity_path.get(&hovered_plot_item))
{
ctx.select_hovered_on_click(
&response,
re_viewer_context::Item::InstancePath(
Some(query.space_view_id),
entity_path.clone().into(),
),
);
}

if let Some(time_x) = time_x {
Expand Down
4 changes: 2 additions & 2 deletions crates/re_viewer/src/ui/selection_history_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl SelectionHistoryUi {
));

let mut return_current = false;
let response = response.context_menu(|ui| {
response.context_menu(|ui| {
// undo: newest on top, oldest on bottom
let cur = history.current;
for i in (0..history.current).rev() {
Expand Down Expand Up @@ -93,7 +93,7 @@ impl SelectionHistoryUi {
));

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

0 comments on commit 1afc771

Please sign in to comment.