Skip to content

Commit

Permalink
Add entiy rects
Browse files Browse the repository at this point in the history
  • Loading branch information
grtlr committed Nov 28, 2024
1 parent 226804c commit f8429ae
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 60 deletions.
46 changes: 44 additions & 2 deletions crates/viewer/re_space_view_graph/src/draw.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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())
}
1 change: 1 addition & 0 deletions crates/viewer/re_space_view_graph/src/layout/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub type LineSegment = [Pos2; 2];
pub struct Layout {
pub(super) nodes: ahash::HashMap<NodeIndex, Rect>,
pub(super) edges: ahash::HashMap<(NodeIndex, NodeIndex), LineSegment>,
// TODO(grtlr): Consider adding the entity rects here too.
}

impl Layout {
Expand Down
44 changes: 0 additions & 44 deletions crates/viewer/re_space_view_graph/src/ui/draw/entity.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/viewer/re_space_view_graph/src/ui/draw/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod entity;
mod node;

pub use entity::draw_entity;
pub use node::DrawableLabel;
42 changes: 31 additions & 11 deletions crates/viewer/re_space_view_graph/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion crates/viewer/re_ui/src/zoom_pan_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit f8429ae

Please sign in to comment.