Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typename cleanup in SpaceViewClass framework #2321

Merged
merged 3 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions crates/re_space_view_spatial/src/space_view_class.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use nohash_hasher::IntSet;
use re_log_types::EntityPath;
use re_viewer_context::{SpaceViewClassImpl, SpaceViewId};
use re_viewer_context::{SpaceViewClass, SpaceViewId};

use crate::{
scene::{SpatialSceneContext, SpatialScenePartCollection, SpatialScenePartData},
Expand All @@ -10,10 +10,10 @@ use crate::{
#[derive(Default)]
pub struct SpatialSpaceView;

impl SpaceViewClassImpl for SpatialSpaceView {
type SpaceViewState = SpatialSpaceViewState;
type SceneContext = SpatialSceneContext;
type ScenePartCollection = SpatialScenePartCollection;
impl SpaceViewClass for SpatialSpaceView {
type State = SpatialSpaceViewState;
type Context = SpatialSceneContext;
type SceneParts = SpatialScenePartCollection;
type ScenePartData = SpatialScenePartData;

fn name(&self) -> re_viewer_context::SpaceViewClassName {
Expand All @@ -24,11 +24,11 @@ impl SpaceViewClassImpl for SpatialSpaceView {
&re_ui::icons::SPACE_VIEW_3D
}

fn help_text(&self, re_ui: &re_ui::ReUi, state: &Self::SpaceViewState) -> egui::WidgetText {
fn help_text(&self, re_ui: &re_ui::ReUi, state: &Self::State) -> egui::WidgetText {
state.help_text(re_ui)
}

fn preferred_tile_aspect_ratio(&self, state: &Self::SpaceViewState) -> Option<f32> {
fn preferred_tile_aspect_ratio(&self, state: &Self::State) -> Option<f32> {
match state.nav_mode.get() {
SpatialNavigationMode::TwoD => {
let size = state.scene_bbox_accum.size();
Expand All @@ -41,7 +41,7 @@ impl SpaceViewClassImpl for SpatialSpaceView {
fn prepare_populate(
&self,
ctx: &mut re_viewer_context::ViewerContext<'_>,
state: &Self::SpaceViewState,
state: &Self::State,
entity_paths: &IntSet<EntityPath>,
entity_properties: &mut re_data_store::EntityPropertyMap,
) {
Expand All @@ -52,7 +52,7 @@ impl SpaceViewClassImpl for SpatialSpaceView {
&self,
ctx: &mut re_viewer_context::ViewerContext<'_>,
ui: &mut egui::Ui,
state: &mut Self::SpaceViewState,
state: &mut Self::State,
space_origin: &EntityPath,
space_view_id: SpaceViewId,
) {
Expand All @@ -63,7 +63,7 @@ impl SpaceViewClassImpl for SpatialSpaceView {
&self,
ctx: &mut re_viewer_context::ViewerContext<'_>,
ui: &mut egui::Ui,
state: &mut Self::SpaceViewState,
state: &mut Self::State,
scene: &mut re_viewer_context::TypedScene<Self>,
space_origin: &EntityPath,
space_view_id: SpaceViewId,
Expand Down
6 changes: 3 additions & 3 deletions crates/re_space_view_text/src/scene_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use re_data_store::EntityPath;
use re_log_types::{Component as _, InstanceKey, RowId};
use re_query::{range_entity_with_primary, QueryError};
use re_viewer_context::{
ArchetypeDefinition, ScenePart, ScenePartCollection, SceneQuery, SpaceViewClassImpl,
ArchetypeDefinition, ScenePart, ScenePartCollection, SceneQuery, SpaceViewClass,
SpaceViewHighlights, ViewerContext,
};

Expand Down Expand Up @@ -51,8 +51,8 @@ impl ScenePart<TextSpaceView> for SceneText {
&mut self,
ctx: &mut ViewerContext<'_>,
query: &SceneQuery<'_>,
state: &<TextSpaceView as SpaceViewClassImpl>::SpaceViewState,
_scene_context: &<TextSpaceView as SpaceViewClassImpl>::SceneContext,
state: &<TextSpaceView as SpaceViewClass>::State,
_scene_context: &<TextSpaceView as SpaceViewClass>::Context,
_highlights: &SpaceViewHighlights,
) -> Vec<re_renderer::QueueableDrawData> {
let store = &ctx.store_db.entity_db.data_store;
Expand Down
18 changes: 9 additions & 9 deletions crates/re_space_view_text/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::BTreeMap;
use re_data_ui::item_ui;
use re_log_types::{EntityPath, TimePoint, Timeline};
use re_viewer_context::{
level_to_rich_text, SpaceViewClassImpl, SpaceViewClassName, SpaceViewId, SpaceViewState,
level_to_rich_text, SpaceViewClass, SpaceViewClassName, SpaceViewId, SpaceViewState,
TypedScene, ViewerContext,
};

Expand Down Expand Up @@ -36,10 +36,10 @@ impl SpaceViewState for TextSpaceViewState {
#[derive(Default)]
pub struct TextSpaceView;

impl SpaceViewClassImpl for TextSpaceView {
type SpaceViewState = TextSpaceViewState;
type SceneContext = re_space_view::EmptySceneContext;
type ScenePartCollection = SceneText;
impl SpaceViewClass for TextSpaceView {
type State = TextSpaceViewState;
type Context = re_space_view::EmptySceneContext;
type SceneParts = SceneText;
type ScenePartData = ();

fn name(&self) -> SpaceViewClassName {
Expand All @@ -50,19 +50,19 @@ impl SpaceViewClassImpl for TextSpaceView {
&re_ui::icons::SPACE_VIEW_TEXTBOX
}

fn help_text(&self, _re_ui: &re_ui::ReUi, _state: &Self::SpaceViewState) -> egui::WidgetText {
fn help_text(&self, _re_ui: &re_ui::ReUi, _state: &Self::State) -> egui::WidgetText {
"Shows text entries over time.\nSelect the Space View for filtering options.".into()
}

fn preferred_tile_aspect_ratio(&self, _state: &Self::SpaceViewState) -> Option<f32> {
fn preferred_tile_aspect_ratio(&self, _state: &Self::State) -> Option<f32> {
Some(2.0) // Make text logs wide
}

fn selection_ui(
&self,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
state: &mut Self::SpaceViewState,
state: &mut Self::State,
_space_origin: &EntityPath,
_space_view_id: SpaceViewId,
) {
Expand Down Expand Up @@ -114,7 +114,7 @@ impl SpaceViewClassImpl for TextSpaceView {
&self,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
state: &mut Self::SpaceViewState,
state: &mut Self::State,
scene: &mut TypedScene<Self>,
_space_origin: &EntityPath,
_space_view_id: SpaceViewId,
Expand Down
6 changes: 3 additions & 3 deletions crates/re_space_view_text_box/src/scene_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use re_arrow_store::LatestAtQuery;
use re_components::Component;
use re_query::{query_entity_with_primary, QueryError};
use re_viewer_context::{
ArchetypeDefinition, ScenePart, ScenePartCollection, SceneQuery, SpaceViewClassImpl,
ArchetypeDefinition, ScenePart, ScenePartCollection, SceneQuery, SpaceViewClass,
SpaceViewHighlights, ViewerContext,
};

Expand Down Expand Up @@ -40,8 +40,8 @@ impl ScenePart<TextBoxSpaceView> for SceneTextBox {
&mut self,
ctx: &mut ViewerContext<'_>,
query: &SceneQuery<'_>,
_space_view_state: &<TextBoxSpaceView as SpaceViewClassImpl>::SpaceViewState,
_scene_context: &<TextBoxSpaceView as SpaceViewClassImpl>::SceneContext,
_space_view_state: &<TextBoxSpaceView as SpaceViewClass>::State,
_scene_context: &<TextBoxSpaceView as SpaceViewClass>::Context,
_highlights: &SpaceViewHighlights,
) -> Vec<re_renderer::QueueableDrawData> {
let store = &ctx.store_db.entity_db.data_store;
Expand Down
16 changes: 8 additions & 8 deletions crates/re_space_view_text_box/src/space_view_class.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use egui::Label;
use re_viewer_context::{
external::re_log_types::EntityPath, SpaceViewClassImpl, SpaceViewClassName, SpaceViewId,
external::re_log_types::EntityPath, SpaceViewClass, SpaceViewClassName, SpaceViewId,
SpaceViewState, TypedScene, ViewerContext,
};

Expand Down Expand Up @@ -35,10 +35,10 @@ impl SpaceViewState for TextBoxSpaceViewState {
#[derive(Default)]
pub struct TextBoxSpaceView;

impl SpaceViewClassImpl for TextBoxSpaceView {
type SpaceViewState = TextBoxSpaceViewState;
type ScenePartCollection = SceneTextBox;
type SceneContext = re_space_view::EmptySceneContext;
impl SpaceViewClass for TextBoxSpaceView {
type State = TextBoxSpaceViewState;
type SceneParts = SceneTextBox;
type Context = re_space_view::EmptySceneContext;
type ScenePartData = ();

fn name(&self) -> SpaceViewClassName {
Expand All @@ -49,15 +49,15 @@ impl SpaceViewClassImpl for TextBoxSpaceView {
&re_ui::icons::SPACE_VIEW_TEXTBOX
}

fn help_text(&self, _re_ui: &re_ui::ReUi, _state: &Self::SpaceViewState) -> egui::WidgetText {
fn help_text(&self, _re_ui: &re_ui::ReUi, _state: &Self::State) -> egui::WidgetText {
"Displays text from a text entry components.".into()
}

fn selection_ui(
&self,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
state: &mut Self::SpaceViewState,
state: &mut Self::State,
_space_origin: &EntityPath,
_space_view_id: SpaceViewId,
) {
Expand All @@ -76,7 +76,7 @@ impl SpaceViewClassImpl for TextBoxSpaceView {
&self,
_ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
state: &mut Self::SpaceViewState,
state: &mut Self::State,
scene: &mut TypedScene<Self>,
_space_origin: &EntityPath,
_space_view_id: SpaceViewId,
Expand Down
4 changes: 2 additions & 2 deletions crates/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use re_renderer::WgpuResourcePoolStatistics;
use re_smart_channel::Receiver;
use re_ui::{toasts, Command};
use re_viewer_context::{
AppOptions, ComponentUiRegistry, PlayState, SpaceViewClass, SpaceViewClassRegistry,
AppOptions, ComponentUiRegistry, DynSpaceViewClass, PlayState, SpaceViewClassRegistry,
SpaceViewClassRegistryError,
};

Expand Down Expand Up @@ -231,7 +231,7 @@ impl App {
}

/// Adds a new space view class to the viewer.
pub fn add_space_view_class<T: SpaceViewClass + Default + 'static>(
pub fn add_space_view_class<T: DynSpaceViewClass + Default + 'static>(
&mut self,
) -> Result<(), SpaceViewClassRegistryError> {
self.space_view_class_registry.add::<T>()
Expand Down
4 changes: 2 additions & 2 deletions crates/re_viewer_context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub use selection_state::{
HoverHighlight, HoveredSpace, InteractionHighlight, SelectionHighlight, SelectionState,
};
pub use space_view::{
ArchetypeDefinition, Scene, SceneContext, SceneContextPart, ScenePart, ScenePartCollection,
SceneQuery, SpaceViewClass, SpaceViewClassImpl, SpaceViewClassName, SpaceViewClassRegistry,
ArchetypeDefinition, DynSpaceViewClass, Scene, SceneContext, SceneContextPart, ScenePart,
ScenePartCollection, SceneQuery, SpaceViewClass, SpaceViewClassName, SpaceViewClassRegistry,
SpaceViewClassRegistryError, SpaceViewEntityHighlight, SpaceViewHighlights,
SpaceViewOutlineMasks, SpaceViewState, TypedScene,
};
Expand Down
107 changes: 107 additions & 0 deletions crates/re_viewer_context/src/space_view/dyn_space_view_class.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use nohash_hasher::IntSet;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather impressive that git didn't see this move

use re_data_store::EntityPropertyMap;
use re_log_types::{ComponentName, EntityPath};

use crate::{Scene, SpaceViewId, ViewerContext};

/// First element is the primary component, all others are optional.
///
/// TODO(andreas/clement): More formal definition of an archetype.
pub type ArchetypeDefinition = vec1::Vec1<ComponentName>;

re_string_interner::declare_new_type!(
/// The unique name of a space view type.
#[derive(serde::Deserialize, serde::Serialize)]
pub struct SpaceViewClassName;
);

/// Defines a class of space view without any concrete types making it suitable for storage and interfacing.
///
/// Implemented by [`crate::SpaceViewClass`].
///
/// Each Space View in the viewer's viewport has a single class assigned immutable at its creation time.
/// The class defines all aspects of its behavior.
/// It determines which entities are queried, how they are rendered, and how the user can interact with them.
pub trait DynSpaceViewClass {
/// Name of this space view class.
///
/// Used for both ui display and identification.
/// Must be unique within a viewer session.
fn name(&self) -> SpaceViewClassName;

/// Icon used to identify this space view class.
fn icon(&self) -> &'static re_ui::Icon;

/// Help text describing how to interact with this space view in the ui.
fn help_text(&self, re_ui: &re_ui::ReUi, state: &dyn SpaceViewState) -> egui::WidgetText;

/// Called once for every new space view instance of this class.
///
/// The state is *not* persisted across viewer sessions, only shared frame-to-frame.
fn new_state(&self) -> Box<dyn SpaceViewState>;

/// Returns a new scene for this space view class.
///
/// Called both to determine the supported archetypes and
/// to populate a scene every frame.
fn new_scene(&self) -> Box<dyn Scene>;

/// Optional archetype of the Space View's blueprint properties.
///
/// Blueprint components that only apply to the space view itself, not to the entities it displays.
fn blueprint_archetype(&self) -> Option<ArchetypeDefinition>;

/// Preferred aspect ratio for the ui tiles of this space view.
fn preferred_tile_aspect_ratio(&self, state: &dyn SpaceViewState) -> Option<f32>;

/// Executed before the scene is populated, can be use for heuristic & state updates before populating the scene.
///
/// Is only allowed to access archetypes defined by [`Self::blueprint_archetype`]
/// Passed entity properties are individual properties without propagated values.
fn prepare_populate(
&self,
ctx: &mut ViewerContext<'_>,
state: &mut dyn SpaceViewState,
entity_paths: &IntSet<EntityPath>,
entity_properties: &mut EntityPropertyMap,
);

/// Ui shown when the user selects a space view of this class.
///
/// TODO(andreas): Should this be instead implemented via a registered `data_ui` of all blueprint relevant types?
fn selection_ui(
&self,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
state: &mut dyn SpaceViewState,
space_origin: &EntityPath,
space_view_id: SpaceViewId,
);

/// Draws the ui for this space view type and handles ui events.
///
/// The scene passed in was previously created by [`Self::new_scene`] and got populated by the time it is passed.
/// The state passed in was previously created by [`Self::new_state`] and is kept frame-to-frame.
fn ui(
&self,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
state: &mut dyn SpaceViewState,
scene: Box<dyn Scene>,
space_origin: &EntityPath,
space_view_id: SpaceViewId,
);
}

/// Unserialized frame to frame state of a space view.
///
/// For any state that should be persisted, use the Blueprint!
/// This state is used for transient state, such as animation or uncommitted ui state like dragging a camera.
/// (on mouse release, the camera would be committed to the blueprint).
pub trait SpaceViewState: std::any::Any {
/// Converts itself to a reference of [`std::any::Any`], which enables downcasting to concrete types.
fn as_any(&self) -> &dyn std::any::Any;

/// Converts itself to a reference of [`std::any::Any`], which enables downcasting to concrete types.
fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
}
10 changes: 5 additions & 5 deletions crates/re_viewer_context/src/space_view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
//! Does not implement any concrete space view.
// TODO(andreas): Can we move some of these to the `re_space_view` crate?
mod dyn_space_view_class;
mod highlights;
mod scene;
mod scene_context;
mod scene_part;
mod scene_query;
mod space_view_class;
mod space_view_class_impl;
mod space_view_class_registry;

pub use dyn_space_view_class::{
ArchetypeDefinition, DynSpaceViewClass, SpaceViewClassName, SpaceViewState,
};
pub use highlights::{SpaceViewEntityHighlight, SpaceViewHighlights, SpaceViewOutlineMasks};
pub use scene::{Scene, TypedScene};
pub use scene_context::{SceneContext, SceneContextPart};
pub use scene_part::{ScenePart, ScenePartCollection};
pub use scene_query::SceneQuery;
pub use space_view_class::{
ArchetypeDefinition, SpaceViewClass, SpaceViewClassName, SpaceViewState,
};
pub use space_view_class_impl::SpaceViewClassImpl;
pub use space_view_class::SpaceViewClass;
pub use space_view_class_registry::{SpaceViewClassRegistry, SpaceViewClassRegistryError};
Loading