Skip to content

Commit

Permalink
Move SpaceViewState out of Blueprint
Browse files Browse the repository at this point in the history
  • Loading branch information
jleibs committed May 23, 2023
1 parent ac1c386 commit 604c16c
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 65 deletions.
26 changes: 17 additions & 9 deletions crates/re_viewer/src/ui/auto_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use itertools::Itertools as _;
use re_data_store::{EntityPath, EntityPathPart};
use re_viewer_context::SpaceViewId;

use super::{space_view::SpaceViewBlueprint, view_category::ViewCategory};
use super::{
space_view::{SpaceViewBlueprint, SpaceViewState},
view_category::ViewCategory,
};

#[derive(Clone, Debug)]
pub struct SpaceMakeInfo {
Expand Down Expand Up @@ -48,6 +51,7 @@ pub(crate) fn tree_from_space_views(
viewport_size: egui::Vec2,
visible: &std::collections::BTreeSet<SpaceViewId>,
space_views: &HashMap<SpaceViewId, SpaceViewBlueprint>,
space_view_states: &HashMap<SpaceViewId, SpaceViewState>,
) -> egui_tiles::Tree<SpaceViewId> {
let mut space_make_infos = space_views
.iter()
Expand All @@ -63,15 +67,19 @@ pub(crate) fn tree_from_space_views(
.map(|(space_view_id, space_view)| {
let aspect_ratio = match space_view.category {
ViewCategory::Spatial => {
let state_spatial = &space_view.view_state.state_spatial;
match *state_spatial.nav_mode.get() {
// This is the only thing where the aspect ratio makes complete sense.
super::view_spatial::SpatialNavigationMode::TwoD => {
let size = state_spatial.scene_bbox_accum.size();
Some(size.x / size.y)
if let Some(space_view_state) = space_view_states.get(space_view_id) {
let state_spatial = &space_view_state.state_spatial;
match *state_spatial.nav_mode.get() {
// This is the only thing where the aspect ratio makes complete sense.
super::view_spatial::SpatialNavigationMode::TwoD => {
let size = state_spatial.scene_bbox_accum.size();
Some(size.x / size.y)
}
// 3D scenes can be pretty flexible
super::view_spatial::SpatialNavigationMode::ThreeD => None,
}
// 3D scenes can be pretty flexible
super::view_spatial::SpatialNavigationMode::ThreeD => None,
} else {
None
}
}
ViewCategory::TextBox | ViewCategory::Tensor | ViewCategory::TimeSeries => {
Expand Down
66 changes: 41 additions & 25 deletions crates/re_viewer/src/ui/selection_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,37 +259,48 @@ fn blueprint_ui(
ui.add_space(ui.spacing().item_spacing.y);

if let Some(space_view) = blueprint.viewport.space_view_mut(space_view_id) {
space_view.selection_ui(ctx, ui);
// TODO(jleibs): Is this the right place to create default state?
let space_view_state = viewport_state
.space_view_states
.entry(*space_view_id)
.or_default();

space_view.selection_ui(space_view_state, ctx, ui);
}
}

Item::InstancePath(space_view_id, instance_path) => {
if let Some(space_view) = space_view_id
.and_then(|space_view_id| blueprint.viewport.space_view_mut(&space_view_id))
{
if instance_path.instance_key.is_specific() {
ui.horizontal(|ui| {
ui.label("Part of");
item_ui::entity_path_button(
if let Some(space_view_id) = space_view_id {
if let Some(space_view) = blueprint.viewport.space_view_mut(space_view_id) {
if instance_path.instance_key.is_specific() {
ui.horizontal(|ui| {
ui.label("Part of");
item_ui::entity_path_button(
ctx,
ui,
Some(*space_view_id),
&instance_path.entity_path,
);
});
// TODO(emilk): show the values of this specific instance (e.g. point in the point cloud)!
} else {
let space_view_state = viewport_state
.space_view_states
.entry(*space_view_id)
.or_default();

// splat - the whole entity
let data_blueprint = space_view.data_blueprint.data_blueprints_individual();
let mut props = data_blueprint.get(&instance_path.entity_path);
entity_props_ui(
ctx,
ui,
*space_view_id,
&instance_path.entity_path,
Some(&instance_path.entity_path),
&mut props,
space_view_state,
);
});
// TODO(emilk): show the values of this specific instance (e.g. point in the point cloud)!
} else {
// splat - the whole entity
let data_blueprint = space_view.data_blueprint.data_blueprints_individual();
let mut props = data_blueprint.get(&instance_path.entity_path);
entity_props_ui(
ctx,
ui,
Some(&instance_path.entity_path),
&mut props,
&space_view.view_state,
);
data_blueprint.set(instance_path.entity_path.clone(), props);
data_blueprint.set(instance_path.entity_path.clone(), props);
}
}
} else {
list_existing_data_blueprints(ui, ctx, &instance_path.entity_path, blueprint);
Expand All @@ -302,12 +313,17 @@ fn blueprint_ui(
.data_blueprint
.group_mut(*data_blueprint_group_handle)
{
let space_view_state = viewport_state
.space_view_states
.entry(*space_view_id)
.or_default();

entity_props_ui(
ctx,
ui,
None,
&mut group.properties_individual,
&space_view.view_state,
space_view_state,
);
} else {
ctx.selection_state_mut().clear_current();
Expand Down
39 changes: 20 additions & 19 deletions crates/re_viewer/src/ui/space_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ pub struct SpaceViewBlueprint {
/// It determines which entities are part of the spaceview.
pub data_blueprint: DataBlueprintTree,

pub view_state: SpaceViewState,

/// We only show data that match this category.
pub category: ViewCategory,

Expand Down Expand Up @@ -77,7 +75,6 @@ impl SpaceViewBlueprint {
id: SpaceViewId::random(),
space_path: space_path.clone(),
data_blueprint: data_blueprint_tree,
view_state: SpaceViewState::default(),
category,
entities_determined_by_user: false,
}
Expand Down Expand Up @@ -148,19 +145,24 @@ impl SpaceViewBlueprint {
}
}

pub fn selection_ui(&mut self, ctx: &mut ViewerContext<'_>, ui: &mut egui::Ui) {
pub fn selection_ui(
&mut self,
view_state: &mut SpaceViewState,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
) {
#[allow(clippy::match_same_arms)]
match self.category {
ViewCategory::Text => {
self.view_state.state_text.selection_ui(ctx.re_ui, ui);
view_state.state_text.selection_ui(ctx.re_ui, ui);
}
ViewCategory::TextBox => {
self.view_state.state_textbox.selection_ui(ctx.re_ui, ui);
view_state.state_textbox.selection_ui(ctx.re_ui, ui);
}
ViewCategory::TimeSeries => {}
ViewCategory::BarChart => {}
ViewCategory::Spatial => {
self.view_state.state_spatial.selection_ui(
view_state.state_spatial.selection_ui(
ctx,
ui,
&self.data_blueprint,
Expand All @@ -169,10 +171,8 @@ impl SpaceViewBlueprint {
);
}
ViewCategory::Tensor => {
if let Some(selected_tensor) = &self.view_state.selected_tensor {
if let Some(state_tensor) =
self.view_state.state_tensors.get_mut(selected_tensor)
{
if let Some(selected_tensor) = &view_state.selected_tensor {
if let Some(state_tensor) = view_state.state_tensors.get_mut(selected_tensor) {
state_tensor.ui(ctx, ui);
}
}
Expand All @@ -182,6 +182,7 @@ impl SpaceViewBlueprint {

pub(crate) fn scene_ui(
&mut self,
view_state: &mut SpaceViewState,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
latest_at: TimeInt,
Expand All @@ -204,26 +205,26 @@ impl SpaceViewBlueprint {
match self.category {
ViewCategory::Text => {
let mut scene = view_text::SceneText::default();
scene.load(ctx, &query, &self.view_state.state_text.filters);
self.view_state.ui_text(ctx, ui, &scene);
scene.load(ctx, &query, &view_state.state_text.filters);
view_state.ui_text(ctx, ui, &scene);
}

ViewCategory::TextBox => {
let mut scene = view_text_box::SceneTextBox::default();
scene.load(ctx, &query);
self.view_state.ui_textbox(ctx, ui, &scene);
view_state.ui_textbox(ctx, ui, &scene);
}

ViewCategory::TimeSeries => {
let mut scene = view_time_series::SceneTimeSeries::default();
scene.load(ctx, &query);
self.view_state.ui_time_series(ctx, ui, &scene);
view_state.ui_time_series(ctx, ui, &scene);
}

ViewCategory::BarChart => {
let mut scene = view_bar_chart::SceneBarChart::default();
scene.load(ctx, &query);
self.view_state.ui_bar_chart(ctx, ui, &scene);
view_state.ui_bar_chart(ctx, ui, &scene);
}

ViewCategory::Spatial => {
Expand All @@ -235,10 +236,10 @@ impl SpaceViewBlueprint {
);
let mut scene = view_spatial::SceneSpatial::new(ctx.render_ctx);
scene.load(ctx, &query, &transforms, highlights);
self.view_state
view_state
.state_spatial
.update_object_property_heuristics(ctx, &mut self.data_blueprint);
self.view_state.ui_spatial(
view_state.ui_spatial(
ctx,
ui,
&self.space_path,
Expand All @@ -252,7 +253,7 @@ impl SpaceViewBlueprint {
ViewCategory::Tensor => {
let mut scene = view_tensor::SceneTensor::default();
scene.load(ctx, &query);
self.view_state.ui_tensor(ctx, ui, &scene);
view_state.ui_tensor(ctx, ui, &scene);
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions crates/re_viewer/src/ui/view_spatial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ mod scene;
mod space_camera_3d;

mod ui;
mod ui_2d;
mod ui_3d;
pub mod ui_2d;
pub mod ui_3d;
pub mod ui_renderer_bridge;

pub use self::scene::{Image, MeshSource, SceneSpatial, UiLabel, UiLabelTarget};
Expand Down
43 changes: 33 additions & 10 deletions crates/re_viewer/src/ui/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use crate::{
};

use super::{
data_blueprint::DataBlueprintGroup, space_view_entity_picker::SpaceViewEntityPicker,
data_blueprint::DataBlueprintGroup, space_view::SpaceViewState,
space_view_entity_picker::SpaceViewEntityPicker,
space_view_heuristics::all_possible_space_views, view_category::ViewCategory,
SpaceViewBlueprint,
};
Expand Down Expand Up @@ -446,12 +447,14 @@ impl Viewport {
ui.available_size(),
&visible_space_views,
&self.space_views,
&state.space_view_states,
)
});

ui.scope(|ui| {
let mut tab_viewer = TabViewer {
ctx,
viewport_state: state,
space_views: &mut self.space_views,
maximized: &mut self.maximized,
};
Expand Down Expand Up @@ -520,6 +523,7 @@ impl Viewport {
#[derive(Clone, Default)]
pub struct ViewportState {
pub(crate) space_view_entity_window: Option<SpaceViewEntityPicker>,
pub(crate) space_view_states: HashMap<SpaceViewId, SpaceViewState>,
}

impl ViewportState {
Expand Down Expand Up @@ -630,6 +634,7 @@ fn visibility_button_ui(
// ----------------------------------------------------------------------------

struct TabViewer<'a, 'b> {
viewport_state: &'a mut ViewportState,
ctx: &'a mut ViewerContext<'b>,
space_views: &'a mut HashMap<SpaceViewId, SpaceViewBlueprint>,
maximized: &'a mut Option<SpaceViewId>,
Expand All @@ -646,12 +651,23 @@ impl<'a, 'b> egui_tiles::Behavior<SpaceViewId> for TabViewer<'a, 'b> {

let highlights =
highlights_for_space_view(self.ctx.selection_state(), *space_view_id, self.space_views);
let space_view = self
let space_view_blueprint = self
.space_views
.get_mut(space_view_id)
.expect("Should have been populated beforehand");

space_view_ui(self.ctx, ui, space_view, &highlights);
let space_view_state = self
.viewport_state
.space_view_states
.entry(*space_view_id)
.or_default();

space_view_ui(
self.ctx,
ui,
space_view_blueprint,
space_view_state,
&highlights,
);

Default::default()
}
Expand Down Expand Up @@ -705,6 +721,7 @@ impl<'a, 'b> egui_tiles::Behavior<SpaceViewId> for TabViewer<'a, 'b> {
let space_view_id = *space_view_id;

let Some(space_view) = self.space_views.get(&space_view_id) else { return; };
let Some(space_view_state) = self.viewport_state.space_view_states.get(&space_view_id) else { return; };

let num_space_views = tiles.tiles.values().filter(|tile| tile.is_pane()).count();

Expand Down Expand Up @@ -737,7 +754,7 @@ impl<'a, 'b> egui_tiles::Behavior<SpaceViewId> for TabViewer<'a, 'b> {
}

// Show help last, since not all space views have help text
help_text_ui(ui, self.ctx.re_ui, space_view);
help_text_ui(ui, self.ctx.re_ui, space_view, space_view_state);
}

// Styling:
Expand Down Expand Up @@ -765,11 +782,16 @@ impl<'a, 'b> egui_tiles::Behavior<SpaceViewId> for TabViewer<'a, 'b> {
}
}

fn help_text_ui(ui: &mut egui::Ui, re_ui: &re_ui::ReUi, space_view: &SpaceViewBlueprint) {
let help_text = match space_view.category {
fn help_text_ui(
ui: &mut egui::Ui,
re_ui: &re_ui::ReUi,
space_view_blueprint: &SpaceViewBlueprint,
space_view_state: &SpaceViewState,
) {
let help_text = match space_view_blueprint.category {
ViewCategory::TimeSeries => Some(crate::ui::view_time_series::help_text(re_ui)),
ViewCategory::BarChart => Some(crate::ui::view_bar_chart::help_text(re_ui)),
ViewCategory::Spatial => Some(space_view.view_state.state_spatial.help_text(re_ui)),
ViewCategory::Spatial => Some(space_view_state.state_spatial.help_text(re_ui)),
ViewCategory::TextBox | ViewCategory::Text | ViewCategory::Tensor => None,
};

Expand All @@ -781,7 +803,8 @@ fn help_text_ui(ui: &mut egui::Ui, re_ui: &re_ui::ReUi, space_view: &SpaceViewBl
fn space_view_ui(
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
space_view: &mut SpaceViewBlueprint,
space_view_blueprint: &mut SpaceViewBlueprint,
space_view_state: &mut SpaceViewState,
space_view_highlights: &SpaceViewHighlights,
) {
let Some(latest_at) = ctx.rec_cfg.time_ctrl.time_int() else {
Expand All @@ -791,7 +814,7 @@ fn space_view_ui(
return
};

space_view.scene_ui(ctx, ui, latest_at, space_view_highlights);
space_view_blueprint.scene_ui(space_view_state, ctx, ui, latest_at, space_view_highlights);
}

// ----------------------------------------------------------------------------
Expand Down

0 comments on commit 604c16c

Please sign in to comment.