plate-tool/plate-tool-eframe/src/extra_widgets.rs

39 lines
1.1 KiB
Rust

use eframe::egui;
pub fn drag_value_with_scroll<T>(
ui: &mut egui::Ui,
value: &mut T,
range: std::ops::RangeInclusive<T>,
speed_drag: f64,
speed_scroll: f64,
) -> egui::Response
where
T: egui::emath::Numeric + std::ops::Add<Output = T> + std::fmt::Debug
{
let drag = egui::DragValue::new(value)
.speed(speed_drag)
.range(range.clone());
let response = ui.add(drag);
if response.hovered() {
let scroll_diff = ui.input(|i| i.smooth_scroll_delta.y);
const THRESHOLD: f32 = 10.0;
if scroll_diff > THRESHOLD {
*value = clamp_partial(*value + T::from_f64(speed_scroll), *range.start(), *range.end());
} else if scroll_diff < -THRESHOLD {
*value = clamp_partial(*value + T::from_f64(-speed_scroll), *range.start(), *range.end());
}
}
response
}
// literally why is this not given in the emath::Numeric definition
fn clamp_partial<T: PartialOrd + Copy + std::fmt::Debug>(val: T, min: T, max: T) -> T {
if val < min { min }
else if val > max { max }
else { val }
}