Skip to content

Commit

Permalink
organize component agnostic editors neatly
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed May 31, 2024
1 parent 3b3a669 commit 2580249
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 48 deletions.
22 changes: 22 additions & 0 deletions crates/re_edit_ui/src/datatype_editors/bool_toggle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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>,
) -> egui::Response {
edit_f32_zero_to_inf_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),
)
}
22 changes: 22 additions & 0 deletions crates/re_edit_ui/src/datatype_editors/float_drag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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>,
) -> egui::Response {
edit_f32_zero_to_inf_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),
)
}
5 changes: 5 additions & 0 deletions crates/re_edit_ui/src/datatype_editors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod float_drag;
mod singleline_string;

pub use float_drag::edit_f32_zero_to_inf;
pub use singleline_string::edit_singleline_string;
File renamed without changes.
56 changes: 8 additions & 48 deletions crates/re_edit_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
//! This should be called by `re_viewer` on startup.
mod corner2d;
mod datatype_editors;
mod marker_shape;
mod response_utils;
mod singleline_string;
mod visible;

use egui::NumExt as _;
use re_types::components::{Color, MarkerSize, Name, Radius, StrokeWidth, Text};
use re_viewer_context::ViewerContext;
use singleline_string::edit_singleline_string;

// ----

Expand All @@ -29,46 +27,6 @@ fn edit_color_ui(_ctx: &ViewerContext<'_>, ui: &mut egui::Ui, value: &mut Color)
response
}

fn edit_radius_ui(
_ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut Radius,
) -> egui::Response {
let speed = (value.0 * 0.01).at_least(0.001);

ui.add(
egui::DragValue::new(&mut value.0)
.clamp_range(0.0..=f64::INFINITY)
.speed(speed),
)
}

fn edit_stroke_width_ui(
_ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut StrokeWidth,
) -> egui::Response {
let speed = (value.0 * 0.01).at_least(0.001);
ui.add(
egui::DragValue::new(&mut value.0)
.clamp_range(0.0..=f64::INFINITY)
.speed(speed),
)
}

fn edit_marker_size_ui(
_ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut MarkerSize,
) -> egui::Response {
let speed = (value.0 * 0.01).at_least(0.001);
ui.add(
egui::DragValue::new(&mut value.0)
.clamp_range(0.0..=f64::INFINITY)
.speed(speed),
)
}

// ----

/// Registers all editors of this crate in the component UI registry.
Expand All @@ -79,11 +37,13 @@ pub fn register_editors(registry: &mut re_viewer_context::ComponentUiRegistry) {
registry.add_editor(edit_color_ui);
registry.add_editor(corner2d::edit_corner2d);
registry.add_editor(marker_shape::edit_marker_shape_ui);
registry.add_editor(edit_marker_size_ui);
registry.add_editor(edit_radius_ui);
registry.add_editor(edit_stroke_width_ui);

registry.add_editor(visible::edit_visible);

registry.add_editor::<Text>(edit_singleline_string);
registry.add_editor::<Name>(edit_singleline_string);
registry.add_editor::<Text>(datatype_editors::edit_singleline_string);
registry.add_editor::<Name>(datatype_editors::edit_singleline_string);

registry.add_editor::<Radius>(datatype_editors::edit_f32_zero_to_inf);
registry.add_editor::<MarkerSize>(datatype_editors::edit_f32_zero_to_inf);
registry.add_editor::<StrokeWidth>(datatype_editors::edit_f32_zero_to_inf);
}

0 comments on commit 2580249

Please sign in to comment.