-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support modifying the plot style by introducing a generic framework f…
…or overriding components (#4914) ### What - Part of #4817 This tackles the problem of allowing SeriesStyle components to be overridden by building out a significant chunk of a general component override framework. The new UI is limited to TimeSeriesScalar view at the moment because it still requires special Visualizer-side handling of the component data. ### High level idea When selecting an entity within a space-view, we now have the ability to override any component that is both queried by the visualizer, and for which we have a registered component-editor. This override value is initialized based on looking for, in order: - The current value of the component for the entity - A value provided by the Visualizer (useful for things like matching the current auto-color) - A default value provided by a function registered along with the editor. ### Notable changes: - Adds a new mechanism to the Component UI Registry that allows us to register editors and default-value providers for any component. - Adds a mechanism to all visualizers to find out the full set of components they query. - During the `update_overrides` process, we now checks the appropriate location in the blueprint override tree and insert the path to any overridden components into the DataResult. - In the TimeSeriesSpaceView we now respect any overrides that come from the DataResult. - Introduce a new Override Components UI in the seleciton panel, which allows us to add an override using a default-provided, or visualizer-provided initial value. - Once the override has been added, the override UI dispatches to the registered editor for each overridden component. ### Screenshot ![image](https://github.com/rerun-io/rerun/assets/3312232/030f625b-462a-4b10-844a-5721492914a0) ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested the web demo (if applicable): * Using newly built examples: [app.rerun.io](https://app.rerun.io/pr/4914/index.html) * Using examples from latest `main` build: [app.rerun.io](https://app.rerun.io/pr/4914/index.html?manifest_url=https://app.rerun.io/version/main/examples_manifest.json) * Using full set of examples from `nightly` build: [app.rerun.io](https://app.rerun.io/pr/4914/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json) * [x] The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG - [PR Build Summary](https://build.rerun.io/pr/4914) - [Docs preview](https://rerun.io/preview/e5f379759f9e985910550ab0258abdec4d4ca1a1/docs) <!--DOCS-PREVIEW--> - [Examples preview](https://rerun.io/preview/e5f379759f9e985910550ab0258abdec4d4ca1a1/examples) <!--EXAMPLES-PREVIEW--> - [Recent benchmark results](https://build.rerun.io/graphs/crates.html) - [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
- Loading branch information
Showing
38 changed files
with
978 additions
and
281 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
// TODO(jleibs): Turn this into a trait | ||
|
||
use egui::NumExt as _; | ||
use re_data_store::{DataStore, LatestAtQuery}; | ||
use re_log_types::EntityPath; | ||
use re_query::ComponentWithInstances; | ||
use re_types::{ | ||
components::{Color, Radius, ScalarScattering, Text}, | ||
Component, Loggable, | ||
}; | ||
use re_viewer_context::{UiVerbosity, ViewerContext}; | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
fn edit_color_ui( | ||
ctx: &ViewerContext<'_>, | ||
ui: &mut egui::Ui, | ||
_verbosity: UiVerbosity, | ||
query: &LatestAtQuery, | ||
store: &DataStore, | ||
entity_path: &EntityPath, | ||
override_path: &EntityPath, | ||
component: &ComponentWithInstances, | ||
instance_key: &re_types::components::InstanceKey, | ||
) { | ||
let current_color = component | ||
.lookup::<Color>(instance_key) | ||
.ok() | ||
.unwrap_or_else(|| default_color(ctx, query, store, entity_path)); | ||
|
||
let [r, g, b, a] = current_color.to_array(); | ||
let current_color = egui::Color32::from_rgba_unmultiplied(r, g, b, a); | ||
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 { | ||
let [r, g, b, a] = edit_color.to_array(); | ||
let new_color = Color::from_unmultiplied_rgba(r, g, b, a); | ||
|
||
ctx.save_blueprint_component(override_path, new_color); | ||
} | ||
} | ||
|
||
#[inline] | ||
fn default_color( | ||
_ctx: &ViewerContext<'_>, | ||
_query: &LatestAtQuery, | ||
_store: &DataStore, | ||
_entity_path: &EntityPath, | ||
) -> Color { | ||
Color::from_rgb(255, 255, 255) | ||
} | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
fn edit_text_ui( | ||
ctx: &ViewerContext<'_>, | ||
ui: &mut egui::Ui, | ||
_verbosity: UiVerbosity, | ||
query: &LatestAtQuery, | ||
store: &DataStore, | ||
entity_path: &EntityPath, | ||
override_path: &EntityPath, | ||
component: &ComponentWithInstances, | ||
instance_key: &re_types::components::InstanceKey, | ||
) { | ||
let current_text = component | ||
.lookup::<Text>(instance_key) | ||
.ok() | ||
.unwrap_or_else(|| default_text(ctx, query, store, entity_path)); | ||
|
||
let current_text = current_text.to_string(); | ||
let mut edit_text = current_text.clone(); | ||
|
||
egui::TextEdit::singleline(&mut edit_text).show(ui); | ||
|
||
if edit_text != current_text { | ||
let new_text = Text::from(edit_text); | ||
|
||
ctx.save_blueprint_component(override_path, new_text); | ||
} | ||
} | ||
|
||
#[inline] | ||
fn default_text( | ||
_ctx: &ViewerContext<'_>, | ||
_query: &LatestAtQuery, | ||
_store: &DataStore, | ||
entity_path: &EntityPath, | ||
) -> Text { | ||
Text::from(entity_path.to_string()) | ||
} | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
fn edit_scatter_ui( | ||
ctx: &ViewerContext<'_>, | ||
ui: &mut egui::Ui, | ||
_verbosity: UiVerbosity, | ||
query: &LatestAtQuery, | ||
store: &DataStore, | ||
entity_path: &EntityPath, | ||
override_path: &EntityPath, | ||
component: &ComponentWithInstances, | ||
instance_key: &re_types::components::InstanceKey, | ||
) { | ||
let current_scatter = component | ||
.lookup::<ScalarScattering>(instance_key) | ||
.ok() | ||
.unwrap_or_else(|| default_scatter(ctx, query, store, entity_path)); | ||
|
||
let current_scatter = current_scatter.0; | ||
let mut edit_scatter = current_scatter; | ||
|
||
let scattered_text = if current_scatter { "Scattered" } else { "Line" }; | ||
|
||
egui::ComboBox::from_id_source("scatter") | ||
.selected_text(scattered_text) | ||
.show_ui(ui, |ui| { | ||
ui.style_mut().wrap = Some(false); | ||
ui.selectable_value(&mut edit_scatter, false, "Line"); | ||
ui.selectable_value(&mut edit_scatter, true, "Scattered"); | ||
}); | ||
|
||
if edit_scatter != current_scatter { | ||
let new_scatter = ScalarScattering::from(edit_scatter); | ||
|
||
ctx.save_blueprint_component(override_path, new_scatter); | ||
} | ||
} | ||
|
||
#[inline] | ||
fn default_scatter( | ||
_ctx: &ViewerContext<'_>, | ||
_query: &LatestAtQuery, | ||
_store: &DataStore, | ||
_entity_path: &EntityPath, | ||
) -> ScalarScattering { | ||
ScalarScattering::from(false) | ||
} | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
fn edit_radius_ui( | ||
ctx: &ViewerContext<'_>, | ||
ui: &mut egui::Ui, | ||
_verbosity: UiVerbosity, | ||
query: &LatestAtQuery, | ||
store: &DataStore, | ||
entity_path: &EntityPath, | ||
override_path: &EntityPath, | ||
component: &ComponentWithInstances, | ||
instance_key: &re_types::components::InstanceKey, | ||
) { | ||
let current_radius = component | ||
.lookup::<Radius>(instance_key) | ||
.ok() | ||
.unwrap_or_else(|| default_radius(ctx, query, store, entity_path)); | ||
|
||
let current_radius = current_radius.0; | ||
let mut edit_radius = current_radius; | ||
|
||
let speed = (current_radius * 0.01).at_least(0.001); | ||
|
||
ui.add( | ||
egui::DragValue::new(&mut edit_radius) | ||
.clamp_range(0.0..=f64::INFINITY) | ||
.speed(speed), | ||
); | ||
|
||
if edit_radius != current_radius { | ||
let new_radius = Radius::from(edit_radius); | ||
|
||
ctx.save_blueprint_component(override_path, new_radius); | ||
} | ||
} | ||
|
||
#[inline] | ||
fn default_radius( | ||
_ctx: &ViewerContext<'_>, | ||
_query: &LatestAtQuery, | ||
_store: &DataStore, | ||
_entity_path: &EntityPath, | ||
) -> Radius { | ||
Radius::from(1.0) | ||
} | ||
|
||
fn register_editor<'a, C: Component + Loggable + 'static>( | ||
registry: &mut re_viewer_context::ComponentUiRegistry, | ||
default: fn(&ViewerContext<'_>, &LatestAtQuery, &DataStore, &EntityPath) -> C, | ||
edit: fn( | ||
&ViewerContext<'_>, | ||
&mut egui::Ui, | ||
UiVerbosity, | ||
&LatestAtQuery, | ||
&DataStore, | ||
&EntityPath, | ||
&EntityPath, | ||
&ComponentWithInstances, | ||
&re_types::components::InstanceKey, | ||
), | ||
) where | ||
C: Into<::std::borrow::Cow<'a, C>>, | ||
{ | ||
registry.add_editor( | ||
C::name(), | ||
Box::new(move |ctx, query, store, entity_path| { | ||
let c = default(ctx, query, store, entity_path); | ||
[c].into() | ||
}), | ||
Box::new(edit), | ||
); | ||
} | ||
|
||
pub fn register_editors(registry: &mut re_viewer_context::ComponentUiRegistry) { | ||
register_editor::<Color>(registry, default_color, edit_color_ui); | ||
register_editor::<Text>(registry, default_text, edit_text_ui); | ||
register_editor::<ScalarScattering>(registry, default_scatter, edit_scatter_ui); | ||
register_editor::<Radius>(registry, default_radius, edit_radius_ui); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.