Skip to content

Commit

Permalink
Make Position2D components editable in selection panel (#8357)
Browse files Browse the repository at this point in the history
### Related

<!--
Include links to any related issues/PRs in a bulleted list, for example:
* Closes #1234
* Part of #1337
-->

* Part of #8299 

### What

This adds a simple single line edit for position 2D. There is no
`speed_fn` because I think in most applications UI coordinates are
probably good unit (for example in graphs).



https://github.com/user-attachments/assets/2fad00c4-8e68-40fe-a474-32fa5806814e



<!--
Make sure the PR title and labels are set to maximize their usefulness
for the CHANGELOG,
and our `git log`.

If you have noticed any breaking changes, include them in the migration
guide.

We track various metrics at <https://build.rerun.io>.

For maintainers:
* To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
* To deploy documentation changes immediately after merging this PR, add
the `deploy docs` label.
-->

---------

Co-authored-by: Andreas Reich <andreas@rerun.io>
  • Loading branch information
grtlr and Wumpf authored Dec 9, 2024
1 parent 1aa3427 commit 067c637
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 39 deletions.
2 changes: 1 addition & 1 deletion crates/viewer/re_component_ui/src/datatype_uis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
109 changes: 76 additions & 33 deletions crates/viewer/re_component_ui/src/datatype_uis/vec.rs
Original file line number Diff line number Diff line change
@@ -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<Target = datatypes::Vec2D>>,
) -> 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<'_>,
Expand All @@ -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<f32>, 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),
))
}
}
13 changes: 8 additions & 5 deletions crates/viewer/re_component_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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 _,
};
Expand Down Expand Up @@ -114,6 +114,9 @@ pub fn create_component_ui_registry() -> re_viewer_context::ComponentUiRegistry
registry.add_singleline_edit_or_view::<TransformRelation>(edit_view_enum);
registry.add_singleline_edit_or_view::<ViewFit>(edit_view_enum);

// Vec2 components:
registry.add_singleline_edit_or_view::<Position2D>(edit_or_view_vec2d);

// Vec3 components:
registry.add_singleline_edit_or_view::<Translation3D>(edit_or_view_vec3d);
registry.add_singleline_edit_or_view::<Scale3D>(edit_or_view_vec3d);
Expand Down

0 comments on commit 067c637

Please sign in to comment.