use re_types::{
archetypes::Pinhole,
blueprint::{
archetypes::Background,
components::{BackgroundKind, VisualBounds2D},
},
components::Color,
Archetype,
};
use re_viewer_context::{SpaceViewStateExt, TypedComponentFallbackProvider};
use crate::{ui::SpatialSpaceViewState, SpatialSpaceView2D};
impl TypedComponentFallbackProvider<Color> for SpatialSpaceView2D {
fn fallback_for(&self, ctx: &re_viewer_context::QueryContext<'_>) -> Color {
if ctx.archetype_name == Some(Background::name()) {
Color::BLACK
} else {
Color::default()
}
}
}
impl TypedComponentFallbackProvider<BackgroundKind> for SpatialSpaceView2D {
fn fallback_for(&self, _ctx: &re_viewer_context::QueryContext<'_>) -> BackgroundKind {
BackgroundKind::SolidColor
}
}
fn valid_bound(rect: &egui::Rect) -> bool {
rect.is_finite() && rect.is_positive()
}
fn pinhole_resolution_rect(pinhole: &Pinhole) -> Option<egui::Rect> {
pinhole
.resolution()
.map(|res| egui::Rect::from_min_max(egui::Pos2::ZERO, egui::pos2(res.x, res.y)))
}
impl TypedComponentFallbackProvider<VisualBounds2D> for SpatialSpaceView2D {
fn fallback_for(&self, ctx: &re_viewer_context::QueryContext<'_>) -> VisualBounds2D {
let Ok(view_state) = ctx.view_state.downcast_ref::<SpatialSpaceViewState>() else {
return VisualBounds2D::default();
};
let default_scene_rect = view_state
.pinhole_at_origin
.as_ref()
.and_then(pinhole_resolution_rect)
.unwrap_or_else(|| {
let scene_rect_smoothed = view_state.bounding_boxes.smoothed;
egui::Rect::from_min_max(
scene_rect_smoothed.min.truncate().to_array().into(),
scene_rect_smoothed.max.truncate().to_array().into(),
)
});
if valid_bound(&default_scene_rect) {
default_scene_rect.into()
} else {
VisualBounds2D::default()
}
}
}
re_viewer_context::impl_component_fallback_provider!(SpatialSpaceView2D => [BackgroundKind, Color, VisualBounds2D]);