[widgets] new widget: DragValue
This commit is contained in:
parent
a90dda6162
commit
acdc2b004f
2 changed files with 55 additions and 5 deletions
|
@ -277,7 +277,7 @@ struct Widgets {
|
||||||
checked: bool,
|
checked: bool,
|
||||||
count: usize,
|
count: usize,
|
||||||
radio: usize,
|
radio: usize,
|
||||||
slider_value: usize,
|
slider_value: f32,
|
||||||
single_line_text_input: String,
|
single_line_text_input: String,
|
||||||
multiline_text_input: String,
|
multiline_text_input: String,
|
||||||
}
|
}
|
||||||
|
@ -288,7 +288,7 @@ impl Default for Widgets {
|
||||||
checked: true,
|
checked: true,
|
||||||
radio: 0,
|
radio: 0,
|
||||||
count: 0,
|
count: 0,
|
||||||
slider_value: 100,
|
slider_value: 3.14,
|
||||||
single_line_text_input: "Hello World!".to_owned(),
|
single_line_text_input: "Hello World!".to_owned(),
|
||||||
multiline_text_input: "Text can both be so wide that it needs a linebreak, but you can also add manual linebreak by pressing enter, creating new paragraphs.\nThis is the start of the next paragraph.\n\nClick me to edit me!".to_owned(),
|
multiline_text_input: "Text can both be so wide that it needs a linebreak, but you can also add manual linebreak by pressing enter, creating new paragraphs.\nThis is the start of the next paragraph.\n\nClick me to edit me!".to_owned(),
|
||||||
}
|
}
|
||||||
|
@ -330,10 +330,18 @@ impl Widgets {
|
||||||
ui.add(label!("The button has been clicked {} times", self.count));
|
ui.add(label!("The button has been clicked {} times", self.count));
|
||||||
});
|
});
|
||||||
|
|
||||||
ui.add(Slider::usize(&mut self.slider_value, 1..=1000).text("value"));
|
ui.add(
|
||||||
|
Slider::f32(&mut self.slider_value, -10.0..=10.0)
|
||||||
|
.text("value")
|
||||||
|
.precision(2),
|
||||||
|
);
|
||||||
if ui.add(Button::new("Double it")).clicked {
|
if ui.add(Button::new("Double it")).clicked {
|
||||||
self.slider_value *= 2;
|
self.slider_value *= 2.0;
|
||||||
}
|
}
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
ui.label("drag this number:");
|
||||||
|
ui.add(DragValue::f32(&mut self.slider_value).speed(0.01));
|
||||||
|
});
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.add(label!("Single line text input:"));
|
ui.add(label!("Single line text input:"));
|
||||||
|
|
|
@ -231,7 +231,7 @@ impl Button {
|
||||||
pub fn sense(mut self, sense: Sense) -> Self {
|
pub fn sense(mut self, sense: Sense) -> Self {
|
||||||
self.sense = sense;
|
self.sense = sense;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Widget for Button {
|
impl Widget for Button {
|
||||||
|
@ -506,3 +506,45 @@ impl Widget for Separator {
|
||||||
ui.interact_hover(rect)
|
ui.interact_hover(rect)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
pub struct DragValue<'a> {
|
||||||
|
value: &'a mut f32,
|
||||||
|
speed: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> DragValue<'a> {
|
||||||
|
pub fn f32(value: &'a mut f32) -> Self {
|
||||||
|
DragValue { value, speed: 1.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// How much the value changes when dragged one point (logical pixel).
|
||||||
|
pub fn speed(mut self, speed: f32) -> Self {
|
||||||
|
self.speed = speed;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Widget for DragValue<'a> {
|
||||||
|
fn ui(self, ui: &mut Ui) -> InteractInfo {
|
||||||
|
let Self { value, speed } = self;
|
||||||
|
let speed_in_physical_pixels = speed / ui.input().pixels_per_point;
|
||||||
|
let precision = (1.0 / speed_in_physical_pixels.abs())
|
||||||
|
.log10()
|
||||||
|
.ceil()
|
||||||
|
.max(0.0) as usize;
|
||||||
|
let button = Button::new(format!("{:.*}", precision, *value)).sense(Sense::drag());
|
||||||
|
let interact = ui.add(button);
|
||||||
|
if interact.active {
|
||||||
|
let mdelta = ui.input().mouse.delta;
|
||||||
|
let delta_points = mdelta.x - mdelta.y; // Increase to the right and up
|
||||||
|
let delta_value = speed * delta_points;
|
||||||
|
if delta_value != 0.0 {
|
||||||
|
*value += delta_value;
|
||||||
|
*value = round_to_precision(*value, precision);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
interact.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue