1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use re_types::{
    blueprint::{
        archetypes::{Background, LineGrid3D},
        components::BackgroundKind,
    },
    components::{Color, Plane3D, StrokeWidth},
    Archetype as _,
};
use re_viewer_context::{TypedComponentFallbackProvider, ViewStateExt as _};

use crate::{ui::SpatialViewState, SpatialView3D};

impl TypedComponentFallbackProvider<Color> for SpatialView3D {
    fn fallback_for(&self, ctx: &re_viewer_context::QueryContext<'_>) -> Color {
        // Color is a fairly common component, make sure this is the right context.
        if ctx.archetype_name == Some(Background::name()) {
            Color::WHITE
        } else if ctx.archetype_name == Some(LineGrid3D::name()) {
            Color::from_unmultiplied_rgba(128, 128, 128, 60)
        } else {
            Color::default()
        }
    }
}

impl TypedComponentFallbackProvider<BackgroundKind> for SpatialView3D {
    fn fallback_for(&self, _ctx: &re_viewer_context::QueryContext<'_>) -> BackgroundKind {
        BackgroundKind::GradientDark
    }
}

impl TypedComponentFallbackProvider<StrokeWidth> for SpatialView3D {
    fn fallback_for(&self, _ctx: &re_viewer_context::QueryContext<'_>) -> StrokeWidth {
        1.0.into()
    }
}

impl TypedComponentFallbackProvider<Plane3D> for SpatialView3D {
    fn fallback_for(&self, ctx: &re_viewer_context::QueryContext<'_>) -> Plane3D {
        const DEFAULT_PLANE: Plane3D = Plane3D::XY;

        let Ok(view_state) = ctx.view_state.downcast_ref::<SpatialViewState>() else {
            return DEFAULT_PLANE;
        };

        view_state
            .state_3d
            .scene_view_coordinates
            .and_then(|view_coordinates| view_coordinates.up())
            .map_or(DEFAULT_PLANE, |up| Plane3D::new(up.as_vec3(), 0.0))
    }
}

re_viewer_context::impl_component_fallback_provider!(SpatialView3D => [BackgroundKind, Color, StrokeWidth, Plane3D]);