Skip to content

Commit

Permalink
impl Inspectable for the remaining number types
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobhellermann committed Jan 21, 2021
1 parent 28fa524 commit b072035
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
30 changes: 30 additions & 0 deletions src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,42 @@ macro_rules! impl_for_num {
};
}

macro_rules! impl_for_num_delegate_f64 {
($ty:ty) => {
impl Inspectable for $ty {
type FieldOptions = NumberAttributes<$ty>;

fn ui(&mut self, ui: &mut egui::Ui, options: Options<Self::FieldOptions>) {
let options_f64 = options.map(|custom| {
let mut custom = custom.map(|v| *v as f64);
if custom.speed == 0.0 {
custom.speed = 1.0;
}
custom
});

let mut value = *self as f64;
<f64 as Inspectable>::ui(&mut value, ui, options_f64);

*self = value as $ty;
}
}
};

( $($ty:ty),* ) => {
$( impl_for_num_delegate_f64!($ty); )*
}
}

impl_for_num!(f32 default_speed = 0.1);
impl_for_num!(f64 default_speed = 0.1);

impl_for_num!(u8);
impl_for_num!(i32);

impl_for_num_delegate_f64!(u16, u32, u64);
impl_for_num_delegate_f64!(i8, i16, i64);

#[derive(Clone, Debug, Default)]
pub struct StringAttributes {
pub multiline: bool,
Expand Down
12 changes: 6 additions & 6 deletions tests/number_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use bevy_inspector_egui::Inspectable;
struct IntegerAttribues {
#[inspectable(min = 2, max = 2, speed = 0.1)]
a: u8,
// b: u16,
// c: u32,
// d: u64,
// e: i8,
// f: i16,
b: u16,
c: u32,
d: u64,
e: i8,
f: i16,
#[inspectable(min = -2)]
g: i32,
// h: i64,
h: i64,
i: f32,
#[inspectable(min = 0.1, max = 0.2)]
j: f64,
Expand Down

0 comments on commit b072035

Please # to comment.