From f8429aed0d12b1f1e36133ad975403dba8295069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20G=C3=B6rtler?= Date: Thu, 28 Nov 2024 09:02:33 +0100 Subject: [PATCH] Add entiy rects --- crates/viewer/re_space_view_graph/src/draw.rs | 46 ++++++++++++++++++- .../re_space_view_graph/src/layout/layout.rs | 1 + .../re_space_view_graph/src/ui/draw/entity.rs | 44 ------------------ .../re_space_view_graph/src/ui/draw/mod.rs | 2 - crates/viewer/re_space_view_graph/src/view.rs | 42 ++++++++++++----- crates/viewer/re_ui/src/zoom_pan_area.rs | 1 - 6 files changed, 76 insertions(+), 60 deletions(-) delete mode 100644 crates/viewer/re_space_view_graph/src/ui/draw/entity.rs diff --git a/crates/viewer/re_space_view_graph/src/draw.rs b/crates/viewer/re_space_view_graph/src/draw.rs index cb6ae4c58d9b..1694be55ba32 100644 --- a/crates/viewer/re_space_view_graph/src/draw.rs +++ b/crates/viewer/re_space_view_graph/src/draw.rs @@ -1,5 +1,9 @@ -use egui::{Color32, Painter, Pos2, Rect, Response, Sense, Shape, Stroke, Ui, UiBuilder, Vec2}; -use re_viewer_context::InteractionHighlight; +use egui::{ + Align2, Color32, FontId, Painter, Pos2, Rect, Response, Sense, Shape, Stroke, Ui, UiBuilder, + Vec2, +}; +use re_chunk::EntityPath; +use re_viewer_context::{InteractionHighlight, SpaceViewHighlights}; use crate::ui::draw::DrawableLabel; @@ -81,3 +85,41 @@ pub fn draw_edge(ui: &mut Ui, points: [Pos2; 2], show_arrow: bool) -> Response { // rect, so that bounding boxes are computed correctly. ui.allocate_rect(rect, NON_SENSE) } + +pub fn draw_entity_rect( + ui: &mut Ui, + rect: Rect, + entity_path: &EntityPath, + highlights: &SpaceViewHighlights, +) -> Response { + let color = if highlights + .entity_outline_mask(entity_path.hash()) + .overall + .is_some() + { + ui.ctx().style().visuals.text_color() + } else { + ui.ctx() + .style() + .visuals + .gray_out(ui.ctx().style().visuals.text_color()) + }; + + let padded = rect.expand(10.0); + + ui.painter() + .rect(padded, 0.0, Color32::TRANSPARENT, Stroke::new(1.0, color)); + + ui.painter().text( + padded.left_top(), + Align2::LEFT_BOTTOM, + entity_path.to_string(), + FontId { + size: 12.0, + family: Default::default(), + }, + color, + ); + + ui.allocate_rect(rect, Sense::click()) +} diff --git a/crates/viewer/re_space_view_graph/src/layout/layout.rs b/crates/viewer/re_space_view_graph/src/layout/layout.rs index 71073078906c..34b1725707e6 100644 --- a/crates/viewer/re_space_view_graph/src/layout/layout.rs +++ b/crates/viewer/re_space_view_graph/src/layout/layout.rs @@ -8,6 +8,7 @@ pub type LineSegment = [Pos2; 2]; pub struct Layout { pub(super) nodes: ahash::HashMap, pub(super) edges: ahash::HashMap<(NodeIndex, NodeIndex), LineSegment>, + // TODO(grtlr): Consider adding the entity rects here too. } impl Layout { diff --git a/crates/viewer/re_space_view_graph/src/ui/draw/entity.rs b/crates/viewer/re_space_view_graph/src/ui/draw/entity.rs deleted file mode 100644 index 91a97e4b5972..000000000000 --- a/crates/viewer/re_space_view_graph/src/ui/draw/entity.rs +++ /dev/null @@ -1,44 +0,0 @@ -use egui::{Align2, Color32, FontId, Rect, Response, Sense, Stroke, Ui}; - -use re_log_types::EntityPath; -use re_viewer_context::SpaceViewHighlights; - -pub fn draw_entity( - ui: &mut Ui, - rect: Rect, - entity_path: &EntityPath, - highlights: &SpaceViewHighlights, -) -> Response { - let (rect, response) = ui.allocate_at_least(rect.size(), Sense::drag()); - - let color = if highlights - .entity_outline_mask(entity_path.hash()) - .overall - .is_some() - { - ui.ctx().style().visuals.text_color() - } else { - ui.ctx() - .style() - .visuals - .gray_out(ui.ctx().style().visuals.text_color()) - }; - - let padded = rect.expand(10.0); - - ui.painter() - .rect(padded, 0.0, Color32::TRANSPARENT, Stroke::new(1.0, color)); - - ui.painter().text( - padded.left_top(), - Align2::LEFT_BOTTOM, - entity_path.to_string(), - FontId { - size: 12.0, - family: Default::default(), - }, - color, - ); - - response -} diff --git a/crates/viewer/re_space_view_graph/src/ui/draw/mod.rs b/crates/viewer/re_space_view_graph/src/ui/draw/mod.rs index d9d5243df73c..33c49a30caa0 100644 --- a/crates/viewer/re_space_view_graph/src/ui/draw/mod.rs +++ b/crates/viewer/re_space_view_graph/src/ui/draw/mod.rs @@ -1,5 +1,3 @@ -mod entity; mod node; -pub use entity::draw_entity; pub use node::DrawableLabel; diff --git a/crates/viewer/re_space_view_graph/src/view.rs b/crates/viewer/re_space_view_graph/src/view.rs index 1cc312d94384..63e722f0210c 100644 --- a/crates/viewer/re_space_view_graph/src/view.rs +++ b/crates/viewer/re_space_view_graph/src/view.rs @@ -11,15 +11,16 @@ use re_ui::{ self, zoom_pan_area::zoom_pan_area, ModifiersMarkdown, MouseButtonMarkdown, UiExt as _, }; use re_viewer_context::{ - IdentifiedViewSystem as _, RecommendedSpaceView, SpaceViewClass, SpaceViewClassLayoutPriority, - SpaceViewClassRegistryError, SpaceViewId, SpaceViewSpawnHeuristics, SpaceViewState, - SpaceViewStateExt as _, SpaceViewSystemExecutionError, SpaceViewSystemRegistrator, - SystemExecutionOutput, ViewQuery, ViewerContext, + external::re_entity_db::InstancePath, IdentifiedViewSystem as _, Item, RecommendedSpaceView, + SpaceViewClass, SpaceViewClassLayoutPriority, SpaceViewClassRegistryError, SpaceViewId, + SpaceViewSpawnHeuristics, SpaceViewState, SpaceViewStateExt as _, + SpaceViewSystemExecutionError, SpaceViewSystemRegistrator, SystemExecutionOutput, ViewQuery, + ViewerContext, }; use re_viewport_blueprint::ViewProperty; use crate::{ - draw::{draw_debug, draw_edge, draw_node}, + draw::{draw_debug, draw_edge, draw_entity_rect, draw_node}, graph::Graph, layout::LayoutRequest, ui::GraphSpaceViewState, @@ -174,21 +175,40 @@ Display a graph of nodes and edges. |ui| { let mut world_bounding_rect = egui::Rect::NOTHING; - for graph in graphs { + for graph in &graphs { + // For now we compute the entity rectangles on the fly. + let mut current_rect = egui::Rect::NOTHING; + for node in graph.nodes() { let center = layout.get_node(&node.id()).center(); // TODO(grtlr): Add proper highlights here: - let resp = - draw_node(ui, center, node.label(), Default::default()); - world_bounding_rect = world_bounding_rect.union(resp.rect); + let resp = draw_node(ui, center, node.label(), Default::default()); + current_rect = current_rect.union(resp.rect); } for edge in graph.edges() { let points = layout.get_edge(edge.from, edge.to); - let resp = draw_edge(ui, points, edge.arrow); - world_bounding_rect = world_bounding_rect.union(resp.rect); + let resp = draw_edge(ui, points, edge.arrow); + current_rect = current_rect.union(resp.rect); + } + + // We only show entity rects if there are multiple entities. + if graphs.len() > 1 { + let entity_path = graph.entity(); + let resp = + draw_entity_rect(ui, current_rect, entity_path, &query.highlights); + + let instance_path = InstancePath::entity_all(entity_path.clone()); + ctx.select_hovered_on_click( + &resp, + vec![(Item::DataResult(query.space_view_id, instance_path), None)] + .into_iter(), + ); + current_rect = current_rect.union(resp.rect); } + + world_bounding_rect = world_bounding_rect.union(current_rect); } // We need to draw the debug information after the rest to ensure that we have the correct bounding box. diff --git a/crates/viewer/re_ui/src/zoom_pan_area.rs b/crates/viewer/re_ui/src/zoom_pan_area.rs index c7850c70ade2..7d81262da7a1 100644 --- a/crates/viewer/re_ui/src/zoom_pan_area.rs +++ b/crates/viewer/re_ui/src/zoom_pan_area.rs @@ -40,7 +40,6 @@ pub fn fit_to_world_rect(available_size: Vec2, world_rect: Rect) -> TSTransform let center_world = world_rect.center().to_vec2(); // Set the transformation to scale and then translate to center. - TSTransform::from_translation(center_screen.to_vec2() - center_world * scale) * TSTransform::from_scaling(scale) }