Skip to content

Commit

Permalink
use generic ui & fallback providers for background color on 2d & 3d s…
Browse files Browse the repository at this point in the history
…pace views
  • Loading branch information
Wumpf committed Jun 3, 2024
1 parent ac12a93 commit 94c2c73
Show file tree
Hide file tree
Showing 18 changed files with 501 additions and 534 deletions.
41 changes: 25 additions & 16 deletions crates/re_space_view_spatial/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@ mod mesh_loader;
mod picking;
mod scene_bounding_boxes;
mod space_camera_3d;
mod space_view_2d;
mod space_view_3d;
mod spatial_topology;
mod ui;
mod ui_2d;
mod ui_3d;
mod view_2d;
mod view_2d_properties;
mod view_3d;
mod view_3d_properties;
mod visualizers;

use re_renderer::RenderContext;
use re_types::blueprint::components::BackgroundKind;
use re_types::components::{Resolution, TensorData};

pub use space_view_2d::SpatialSpaceView2D;
pub use space_view_3d::SpatialSpaceView3D;
pub use view_2d::SpatialSpaceView2D;
pub use view_3d::SpatialSpaceView3D;

// ---

use re_renderer::RenderContext;
use re_types::blueprint::components::BackgroundKind;
use re_types::components::{Color, Resolution, TensorData};
use re_viewport_blueprint::{ViewProperty, ViewPropertyQueryError};

mod view_kind {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpatialSpaceViewKind {
Expand Down Expand Up @@ -79,14 +82,17 @@ fn query_pinhole(
}

pub(crate) fn configure_background(
background: &ViewProperty<'_>,
render_ctx: &RenderContext,
kind: BackgroundKind,
color: re_types::components::Color,
) -> (Option<re_renderer::QueueableDrawData>, re_renderer::Rgba) {
view_system: &dyn re_viewer_context::ComponentFallbackProvider,
state: &dyn re_viewer_context::SpaceViewState,
) -> Result<(Option<re_renderer::QueueableDrawData>, re_renderer::Rgba), ViewPropertyQueryError> {
use re_renderer::renderer;

let kind: BackgroundKind = background.component_or_fallback(view_system, state)?;

match kind {
BackgroundKind::GradientDark => (
BackgroundKind::GradientDark => Ok((
Some(
renderer::GenericSkyboxDrawData::new(
render_ctx,
Expand All @@ -95,9 +101,9 @@ pub(crate) fn configure_background(
.into(),
),
re_renderer::Rgba::TRANSPARENT, // All zero is slightly faster to clear usually.
),
)),

BackgroundKind::GradientBright => (
BackgroundKind::GradientBright => Ok((
Some(
renderer::GenericSkyboxDrawData::new(
render_ctx,
Expand All @@ -106,8 +112,11 @@ pub(crate) fn configure_background(
.into(),
),
re_renderer::Rgba::TRANSPARENT, // All zero is slightly faster to clear usually.
),
)),

BackgroundKind::SolidColor => (None, color.into()),
BackgroundKind::SolidColor => {
let color: Color = background.component_or_fallback(view_system, state)?;
Ok((None, color.into()))
}
}
}
78 changes: 3 additions & 75 deletions crates/re_space_view_spatial/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ use re_format::format_f32;
use re_log_types::Instance;
use re_renderer::OutlineConfig;
use re_space_view::ScreenshotMode;
use re_types::{
blueprint::archetypes::Background,
components::{Color, DepthMeter, TensorData, ViewCoordinates},
};
use re_types::{blueprint::components::BackgroundKind, tensor_data::TensorDataMeaning};
use re_types::components::{DepthMeter, TensorData, ViewCoordinates};
use re_types::tensor_data::TensorDataMeaning;
use re_viewer_context::{
HoverHighlight, Item, ItemSpaceContext, SelectionHighlight, SpaceViewHighlights, SpaceViewId,
HoverHighlight, Item, ItemSpaceContext, SelectionHighlight, SpaceViewHighlights,
SpaceViewState, SpaceViewSystemExecutionError, TensorDecodeCache, TensorStatsCache, UiLayout,
ViewContextCollection, ViewQuery, ViewerContext, VisualizerCollection,
};
Expand Down Expand Up @@ -775,72 +772,3 @@ pub fn format_vector(v: glam::Vec3) -> String {
format!("[{:.02}, {:.02}, {:.02}]", v.x, v.y, v.z)
}
}

pub fn background_ui(
ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
space_view_id: SpaceViewId,
default_background: Background,
) {
let blueprint_db = ctx.store_context.blueprint;
let blueprint_query = ctx.blueprint_query;
let (archetype, blueprint_path) =
re_viewport_blueprint::query_view_property(space_view_id, blueprint_db, blueprint_query);

let Background { color, mut kind } = archetype.ok().flatten().unwrap_or(default_background);

ctx.re_ui.grid_left_hand_label(ui, "Background");

ui.vertical(|ui| {
let kind_before = kind;
egui::ComboBox::from_id_source("background")
.selected_text(background_color_text(kind))
.show_ui(ui, |ui| {
ui.selectable_value(
&mut kind,
BackgroundKind::GradientDark,
background_color_text(BackgroundKind::GradientDark),
);
ui.selectable_value(
&mut kind,
BackgroundKind::GradientBright,
background_color_text(BackgroundKind::GradientBright),
);
ui.selectable_value(
&mut kind,
BackgroundKind::SolidColor,
background_color_text(BackgroundKind::SolidColor),
);
});
if kind_before != kind {
ctx.save_blueprint_component(&blueprint_path, &kind);
}

if kind == BackgroundKind::SolidColor {
let current_color = color
.or(default_background.color)
.unwrap_or(Color::BLACK)
.into();
let mut edit_color = current_color;
egui::color_picker::color_edit_button_srgba(
ui,
&mut edit_color,
egui::color_picker::Alpha::Opaque,
);
if edit_color != current_color {
ctx.save_blueprint_component(&blueprint_path, &BackgroundKind::SolidColor);
ctx.save_blueprint_component(&blueprint_path, &Color::from(edit_color));
}
}
});

ui.end_row();
}

fn background_color_text(kind: BackgroundKind) -> &'static str {
match kind {
BackgroundKind::GradientDark => "Dark gradient",
BackgroundKind::GradientBright => "Bright gradient",
BackgroundKind::SolidColor => "Solid color",
}
}
Loading

0 comments on commit 94c2c73

Please sign in to comment.