diff --git a/crates/viewer/re_component_ui/src/datatype_uis/mod.rs b/crates/viewer/re_component_ui/src/datatype_uis/mod.rs index 9a8db3c714f9..5147a63dac32 100644 --- a/crates/viewer/re_component_ui/src/datatype_uis/mod.rs +++ b/crates/viewer/re_component_ui/src/datatype_uis/mod.rs @@ -20,6 +20,6 @@ pub use range1d::edit_view_range1d; pub use singleline_string::{ display_name_ui, display_text_ui, edit_multiline_string, edit_singleline_string, }; -pub use vec::{edit_or_view_vec3d, edit_or_view_vec3d_raw}; +pub use vec::{edit_or_view_vec2d, edit_or_view_vec3d, edit_or_view_vec3d_raw}; pub use view_id::view_view_id; pub use view_uuid::view_uuid; diff --git a/crates/viewer/re_component_ui/src/datatype_uis/vec.rs b/crates/viewer/re_component_ui/src/datatype_uis/vec.rs index 2c06dd16c505..799c110bf008 100644 --- a/crates/viewer/re_component_ui/src/datatype_uis/vec.rs +++ b/crates/viewer/re_component_ui/src/datatype_uis/vec.rs @@ -1,7 +1,20 @@ +use std::ops::RangeInclusive; + +use egui::NumExt as _; use re_types::datatypes; use re_viewer_context::MaybeMutRef; -use super::float_drag::edit_f32_float_raw; +pub fn edit_or_view_vec2d( + _ctx: &re_viewer_context::ViewerContext<'_>, + ui: &mut egui::Ui, + value: &mut MaybeMutRef<'_, impl std::ops::DerefMut>, +) -> egui::Response { + let mut value: MaybeMutRef<'_, datatypes::Vec2D> = match value { + MaybeMutRef::Ref(value) => MaybeMutRef::Ref(value), + MaybeMutRef::MutRef(value) => MaybeMutRef::MutRef(value), + }; + edit_or_view_vec2d_raw(ui, &mut value) +} pub fn edit_or_view_vec3d( _ctx: &re_viewer_context::ViewerContext<'_>, @@ -12,50 +25,80 @@ pub fn edit_or_view_vec3d( MaybeMutRef::Ref(value) => MaybeMutRef::Ref(value), MaybeMutRef::MutRef(value) => MaybeMutRef::MutRef(value), }; - edit_or_view_vec3d_raw_immutable(ui, &mut value) + edit_or_view_vec3d_raw(ui, &mut value) } -// TODO(#6743): Since overrides are not yet taken into account, editing this value has no effect. -//MaybeMutRef::MutRef(value) => MaybeMutRef::MutRef(&mut value[i]), -fn edit_or_view_vec3d_raw_immutable( - ui: &mut egui::Ui, - value: &mut MaybeMutRef<'_, datatypes::Vec3D>, -) -> egui::Response { - edit_or_view_vector_component_immutable(ui, value, 0) - | edit_or_view_vector_component_immutable(ui, value, 1) - | edit_or_view_vector_component_immutable(ui, value, 2) +fn drag<'a>(value: &'a mut f32, range: RangeInclusive, suffix: &str) -> egui::DragValue<'a> { + let speed = (value.abs() * 0.01).at_least(0.001); + egui::DragValue::new(value) + .clamp_existing_to_range(false) + .range(range) + .speed(speed) + .suffix(suffix) } -fn edit_or_view_vector_component_immutable( +// TODO(#6743): Since overrides are not yet taken into account, editing this value has no effect. +pub fn edit_or_view_vec2d_raw( ui: &mut egui::Ui, - value: &mut MaybeMutRef<'_, datatypes::Vec3D>, - i: usize, + value: &mut MaybeMutRef<'_, datatypes::Vec2D>, ) -> egui::Response { - let mut value: MaybeMutRef<'_, f32> = match value { - MaybeMutRef::Ref(value) => MaybeMutRef::Ref(&value[i]), + let x = value.0[0]; + let y = value.0[1]; - MaybeMutRef::MutRef(value) => MaybeMutRef::Ref(&value[i]), - }; - edit_f32_float_raw(ui, &mut value, f32::MIN..=f32::MAX, "") + if let Some(value) = value.as_mut() { + let mut x_edit = x; + let mut y_edit = y; + + let response_x = ui.add(drag(&mut x_edit, f32::MIN..=f32::MAX, "")); + let response_y = ui.add(drag(&mut y_edit, f32::MIN..=f32::MAX, "")); + + let response = response_y | response_x; + + if response.changed() { + *value = datatypes::Vec2D([x_edit, y_edit]); + } + + response + } else { + ui.label(format!( + "[ {} , {} ]", + re_format::format_f32(x), + re_format::format_f32(y), + )) + } } +// TODO(#6743): Since overrides are not yet taken into account, editing this value has no effect. pub fn edit_or_view_vec3d_raw( ui: &mut egui::Ui, value: &mut MaybeMutRef<'_, datatypes::Vec3D>, ) -> egui::Response { - edit_or_view_vector_component(ui, value, 0) - | edit_or_view_vector_component(ui, value, 1) - | edit_or_view_vector_component(ui, value, 2) -} + let x = value.0[0]; + let y = value.0[1]; + let z = value.0[2]; -fn edit_or_view_vector_component( - ui: &mut egui::Ui, - value: &mut MaybeMutRef<'_, datatypes::Vec3D>, - i: usize, -) -> egui::Response { - let mut value: MaybeMutRef<'_, f32> = match value { - MaybeMutRef::Ref(value) => MaybeMutRef::Ref(&value[i]), - MaybeMutRef::MutRef(value) => MaybeMutRef::MutRef(&mut value[i]), - }; - edit_f32_float_raw(ui, &mut value, f32::MIN..=f32::MAX, "") + if let Some(value) = value.as_mut() { + let mut x_edit = x; + let mut y_edit = y; + let mut z_edit = z; + + let response_x = ui.add(drag(&mut x_edit, f32::MIN..=f32::MAX, "")); + let response_y = ui.add(drag(&mut y_edit, f32::MIN..=f32::MAX, "")); + let response_z = ui.add(drag(&mut z_edit, f32::MIN..=f32::MAX, "")); + + let response = response_y | response_x | response_z; + + if response.changed() { + *value = datatypes::Vec3D([x_edit, y_edit, z_edit]); + } + + response + } else { + ui.label(format!( + "[ {} , {} , {} ]", + re_format::format_f32(x), + re_format::format_f32(y), + re_format::format_f32(z), + )) + } } diff --git a/crates/viewer/re_component_ui/src/lib.rs b/crates/viewer/re_component_ui/src/lib.rs index 55b7da2d4730..7460795c9fc5 100644 --- a/crates/viewer/re_component_ui/src/lib.rs +++ b/crates/viewer/re_component_ui/src/lib.rs @@ -28,9 +28,9 @@ mod zoom_level; use datatype_uis::{ display_name_ui, display_text_ui, edit_bool, edit_f32_min_to_max_float, edit_f32_zero_to_max, - edit_f32_zero_to_one, edit_multiline_string, edit_or_view_vec3d, edit_singleline_string, - edit_ui_points, edit_view_enum, edit_view_enum_with_variant_available, edit_view_range1d, - view_uuid, view_view_id, + edit_f32_zero_to_one, edit_multiline_string, edit_or_view_vec2d, edit_or_view_vec3d, + edit_singleline_string, edit_ui_points, edit_view_enum, edit_view_enum_with_variant_available, + edit_view_range1d, view_uuid, view_view_id, }; use re_types::{ @@ -40,8 +40,8 @@ use re_types::{ components::{ AggregationPolicy, AlbedoFactor, AxisLength, Color, DepthMeter, DrawOrder, FillMode, FillRatio, GammaCorrection, GraphType, ImagePlaneDistance, MagnificationFilter, MarkerSize, - Name, Opacity, Range1D, Scale3D, ShowLabels, StrokeWidth, Text, TransformRelation, - Translation3D, ValueRange, + Name, Opacity, Position2D, Range1D, Scale3D, ShowLabels, StrokeWidth, Text, + TransformRelation, Translation3D, ValueRange, }, Component as _, }; @@ -114,6 +114,9 @@ pub fn create_component_ui_registry() -> re_viewer_context::ComponentUiRegistry registry.add_singleline_edit_or_view::(edit_view_enum); registry.add_singleline_edit_or_view::(edit_view_enum); + // Vec2 components: + registry.add_singleline_edit_or_view::(edit_or_view_vec2d); + // Vec3 components: registry.add_singleline_edit_or_view::(edit_or_view_vec3d); registry.add_singleline_edit_or_view::(edit_or_view_vec3d);