bevy-inspector-egui icon indicating copy to clipboard operation
bevy-inspector-egui copied to clipboard

Is there a way to add a slider?

Open andreesteve opened this issue 4 years ago • 1 comments

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!

andreesteve avatar Jun 27 '21 20:06 andreesteve

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 avatar Jun 28 '21 11:06 jakobhellermann

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

jakobhellermann avatar Jan 05 '23 20:01 jakobhellermann