jakobhellermann / bevy-inspector-egui

Inspector plugin for the bevy game engine
Apache License 2.0
1.19k stars 173 forks source link

Is there a way to add a slider? #23

Closed andreesteve closed 1 year ago

andreesteve commented 3 years ago

Hi, first of all, amazing crate!

I was wondering if there is a way to add a slider. Similar to the Vec2 visual, but in a single dimension. My scenario is that I have lots of floats [0, 1] that a gradual increase on a slider would work very well.

Thanks!

jakobhellermann commented 3 years ago

You could write a wrapper for numeric types which you want to display as a slider like this:

#[derive(Clone)]
struct SliderAttrs<T> {
    range: RangeInclusive<T>,
}
impl<T: emath::Numeric> Default for SliderAttrs<T> {
    fn default() -> Self {
        SliderAttrs {
            range: T::MIN..=T::MAX,
        }
    }
}
#[derive(Default)]
struct Slider<T: emath::Numeric>(T);
impl<T: emath::Numeric> Inspectable for Slider<T> {
    type Attributes = SliderAttrs<T>;

    fn ui(&mut self, egui::Ui, options: Self::Attributes, _: &bevy_inspector_egui::Context) -> bool {
        let widget = egui::widgets::Slider::new(&mut self.0, options.range);
        ui.add(widget).changed()
    }
}

#[derive(Inspectable)]
struct Data {
    #[inspectable(range = 10.0..=20.0)]
    value: Slider<f32>,
    ...
}

Alternatively the NumberAttributes could be extended with an enum NumericDisplay { Slider, Field } and display a slider depending on the enum value.

jakobhellermann commented 1 year ago

I just released 0.16 which lets you display numbers as a slider: https://github.com/jakobhellermann/bevy-inspector-egui/blob/863191c2fa7898dd451f64b09e77012b7778a26f/crates/bevy-inspector-egui/examples/basic/inspector_options.rs#L9