1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::egui::{self, widgets};
use crate::Context;
use crate::Inspectable;

#[derive(Debug, Clone)]
pub struct NumberAttributes<T> {
    pub min: Option<T>,
    pub max: Option<T>,
    /// How much the value changes when dragged one logical pixel.
    pub speed: f32,
    pub prefix: String,
    pub suffix: String,
}
impl<T> Default for NumberAttributes<T> {
    fn default() -> Self {
        NumberAttributes {
            min: None,
            max: None,
            speed: 0.0,
            prefix: "".to_string(),
            suffix: "".to_string(),
        }
    }
}

impl<T> NumberAttributes<T> {
    pub(crate) fn map<U>(&self, f: impl Fn(&T) -> U) -> NumberAttributes<U> {
        #[allow(clippy::redundant_closure)] // false positive
        NumberAttributes {
            min: self.min.as_ref().map(|v| f(v)),
            max: self.max.as_ref().map(f),
            speed: self.speed,
            prefix: self.prefix.clone(),
            suffix: self.suffix.clone(),
        }
    }

    pub fn min(min: T) -> Self {
        NumberAttributes {
            min: Some(min),
            max: None,
            ..Default::default()
        }
    }

    pub fn between(min: T, max: T) -> Self {
        NumberAttributes {
            min: Some(min),
            max: Some(max),
            ..Default::default()
        }
    }

    pub(crate) fn speed(self, speed: f32) -> Self {
        NumberAttributes { speed, ..self }
    }
}
impl NumberAttributes<f32> {
    pub(crate) fn positive() -> Self {
        NumberAttributes::min(0.0)
    }

    pub(crate) fn normalized() -> Self {
        NumberAttributes::between(0.0, 1.0).speed(0.1)
    }
}

macro_rules! impl_num {
    ($ty:ty $(| $default_speed:literal)?) => {
        impl Inspectable for $ty {
            type Attributes = NumberAttributes<$ty>;

            fn ui(
                &mut self,
                ui: &mut egui::Ui,
                options: Self::Attributes,
                _: &mut Context,
            ) -> bool {
                num_ui(self, options, ui, None)
            }
        }
    };
}

impl_num!(i8);
impl_num!(i16);
impl_num!(i32);
impl_num!(i64);
impl_num!(isize);
impl_num!(u8);
impl_num!(u16);
impl_num!(u32);
impl_num!(u64);
impl_num!(usize);

impl Inspectable for f32 {
    type Attributes = NumberAttributes<f32>;

    fn ui(&mut self, ui: &mut egui::Ui, options: Self::Attributes, _: &mut Context) -> bool {
        num_ui(self, options, ui, Some(0.1))
    }
}
impl Inspectable for f64 {
    type Attributes = NumberAttributes<f64>;

    fn ui(&mut self, ui: &mut egui::Ui, options: Self::Attributes, _: &mut Context) -> bool {
        num_ui(self, options, ui, Some(0.1))
    }
}

fn num_ui<T: egui::emath::Numeric>(
    value: &mut T,
    options: NumberAttributes<T>,
    ui: &mut egui::Ui,
    default_speed: Option<f32>,
) -> bool {
    let mut widget = widgets::DragValue::new(value);
    if !options.prefix.is_empty() {
        widget = widget.prefix(options.prefix);
    }
    if !options.suffix.is_empty() {
        widget = widget.suffix(options.suffix);
    }
    match (options.min, options.max) {
        (Some(min), Some(max)) => widget = widget.clamp_range(min.to_f64()..=max.to_f64()),
        (Some(min), None) => widget = widget.clamp_range(min.to_f64()..=f64::MAX),
        (None, Some(max)) => widget = widget.clamp_range(f64::MIN..=max.to_f64()),
        (None, None) => {}
    }
    if options.speed != 0.0 {
        widget = widget.speed(options.speed);
    } else if let Some(default_speed) = default_speed {
        widget = widget.speed(default_speed);
    }
    let mut changed = ui.add(widget).changed();
    if let Some(min) = options.min {
        let as_f64 = value.to_f64();
        let min = min.to_f64();
        if as_f64 < min {
            *value = T::from_f64(min);
            changed = true;
        }
    }
    if let Some(max) = options.max {
        let as_f64 = value.to_f64();
        let max = max.to_f64();
        if as_f64 > max {
            *value = T::from_f64(max);
            changed = true;
        }
    }
    changed
}