Skip to content

Commit

Permalink
make bool edit_ui generic (there's more bools incoming soon!
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed May 31, 2024
1 parent a299f63 commit 2485882
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 37 deletions.
28 changes: 12 additions & 16 deletions crates/re_edit_ui/src/datatype_editors/bool_toggle.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
use std::ops::DerefMut;

use egui::NumExt as _;

/// Generic editor for a f32 value from zero to infinity.
pub fn edit_f32_zero_to_inf(
/// Generic editor for a boolean value.
pub fn edit_bool(
_ctx: &re_viewer_context::ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut impl DerefMut<Target = f32>,
value: &mut impl std::ops::DerefMut<Target = bool>,
) -> egui::Response {
edit_f32_zero_to_inf_impl(ui, value)
edit_bool_impl(ui, value)
}

/// Non monomorphized implementation of [`edit_f32_zero_to_inf`].
fn edit_f32_zero_to_inf_impl(ui: &mut egui::Ui, value: &mut f32) -> egui::Response {
let speed = (*value * 0.01).at_least(0.001);
ui.add(
egui::DragValue::new(value)
.clamp_range(0.0..=f32::INFINITY)
.speed(speed),
)
/// Non monomorphized implementation of [`edit_bool`].
fn edit_bool_impl(ui: &mut egui::Ui, value: &mut bool) -> egui::Response {
ui.scope(move |ui| {
ui.visuals_mut().widgets.hovered.expansion = 0.0;
ui.visuals_mut().widgets.active.expansion = 0.0;
ui.add(re_ui::toggle_switch(15.0, value))
})
.inner
}
4 changes: 1 addition & 3 deletions crates/re_edit_ui/src/datatype_editors/float_drag.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::ops::DerefMut;

use egui::NumExt as _;

/// Generic editor for a f32 value from zero to infinity.
pub fn edit_f32_zero_to_inf(
_ctx: &re_viewer_context::ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut impl DerefMut<Target = f32>,
value: &mut impl std::ops::DerefMut<Target = f32>,
) -> egui::Response {
edit_f32_zero_to_inf_impl(ui, value)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/re_edit_ui/src/datatype_editors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod bool_toggle;
mod float_drag;
mod singleline_string;

pub use bool_toggle::edit_bool;
pub use float_drag::edit_f32_zero_to_inf;
pub use singleline_string::edit_singleline_string;
9 changes: 6 additions & 3 deletions crates/re_edit_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ mod corner2d;
mod datatype_editors;
mod marker_shape;
mod response_utils;
mod visible;

use re_types::components::{Color, MarkerSize, Name, Radius, ScalarScattering, StrokeWidth, Text};
use re_types::{
blueprint::components::Visible,
components::{Color, MarkerSize, Name, Radius, ScalarScattering, StrokeWidth, Text},
};
use re_viewer_context::ViewerContext;

// ----
Expand Down Expand Up @@ -55,7 +57,8 @@ pub fn register_editors(registry: &mut re_viewer_context::ComponentUiRegistry) {
registry.add_editor(corner2d::edit_corner2d);
registry.add_editor(marker_shape::edit_marker_shape_ui);
registry.add_editor(edit_scatter_ui);
registry.add_editor(visible::edit_visible);

registry.add_editor::<Visible>(datatype_editors::edit_bool);

registry.add_editor::<Text>(datatype_editors::edit_singleline_string);
registry.add_editor::<Name>(datatype_editors::edit_singleline_string);
Expand Down
15 changes: 0 additions & 15 deletions crates/re_edit_ui/src/visible.rs

This file was deleted.

0 comments on commit 2485882

Please sign in to comment.