mirror of
https://github.com/obsqrbtz/egui_knob.git
synced 2026-04-08 20:19:17 +03:00
Merge pull request #6 from maor1993/master
added support for double click reset to a known value
This commit is contained in:
@@ -87,7 +87,8 @@ impl eframe::App for KnobDemo {
|
|||||||
.with_background_arc(self.show_bg_arc)
|
.with_background_arc(self.show_bg_arc)
|
||||||
.with_show_filled_segments(self.show_filled)
|
.with_show_filled_segments(self.show_filled)
|
||||||
.with_colors(self.knob_color, self.line_color, self.text_color)
|
.with_colors(self.knob_color, self.line_color, self.text_color)
|
||||||
.with_step(self.use_step.then_some(0.02));
|
.with_step(self.use_step.then_some(0.02))
|
||||||
|
.with_double_click_reset(0.5);
|
||||||
|
|
||||||
if *label == "Wiper, Sweep" {
|
if *label == "Wiper, Sweep" {
|
||||||
knob = knob.with_sweep_range(0.25, 0.75).with_size(50.0);
|
knob = knob.with_sweep_range(0.25, 0.75).with_size(50.0);
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ pub struct KnobConfig {
|
|||||||
pub(crate) show_filled_segments: bool,
|
pub(crate) show_filled_segments: bool,
|
||||||
pub(crate) min_angle: f32,
|
pub(crate) min_angle: f32,
|
||||||
pub(crate) max_angle: f32,
|
pub(crate) max_angle: f32,
|
||||||
|
pub(crate) reset_value: Option<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KnobConfig {
|
impl KnobConfig {
|
||||||
@@ -36,6 +37,7 @@ impl KnobConfig {
|
|||||||
drag_sensitivity: 0.005,
|
drag_sensitivity: 0.005,
|
||||||
show_background_arc: true,
|
show_background_arc: true,
|
||||||
show_filled_segments: true,
|
show_filled_segments: true,
|
||||||
|
reset_value: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ impl<'a> Knob<'a> {
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.config.min_angle =
|
self.config.min_angle =
|
||||||
start_angle_normalized.rem_euclid(1.0) * std::f32::consts::TAU + std::f32::consts::PI / 2.0;
|
start_angle_normalized.rem_euclid(1.0) * std::f32::consts::TAU + std::f32::consts::PI / 2.0;
|
||||||
self.config.max_angle = self.config.min_angle + range.max(0.0) * std::f32::consts::TAU;
|
self.config.max_angle = self.config.min_angle + range.max(0.0) * std::f32::consts::TAU;
|
||||||
self
|
self
|
||||||
@@ -156,6 +156,11 @@ impl<'a> Knob<'a> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets a reset value to return to on doubleclick event.
|
||||||
|
pub fn with_double_click_reset(mut self, reset_value: f32) -> Self {
|
||||||
|
self.config.reset_value = Some(reset_value);
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Widget for Knob<'_> {
|
impl Widget for Knob<'_> {
|
||||||
@@ -168,7 +173,7 @@ impl Widget for Knob<'_> {
|
|||||||
let renderer = KnobRenderer::new(&self.config, current_value, self.min, self.max);
|
let renderer = KnobRenderer::new(&self.config, current_value, self.min, self.max);
|
||||||
let adjusted_size = renderer.calculate_size(ui);
|
let adjusted_size = renderer.calculate_size(ui);
|
||||||
|
|
||||||
let (rect, response) = ui.allocate_exact_size(adjusted_size, Sense::drag());
|
let (rect, response) = ui.allocate_exact_size(adjusted_size, Sense::click_and_drag());
|
||||||
|
|
||||||
let mut response = response;
|
let mut response = response;
|
||||||
if response.dragged() {
|
if response.dragged() {
|
||||||
@@ -189,6 +194,10 @@ impl Widget for Knob<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
response.mark_changed();
|
response.mark_changed();
|
||||||
|
} else if response.double_clicked() {
|
||||||
|
if let Some(reset_value) = self.config.reset_value {
|
||||||
|
*self.value = reset_value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let knob_rect = renderer.calculate_knob_rect(rect);
|
let knob_rect = renderer.calculate_knob_rect(rect);
|
||||||
|
|||||||
Reference in New Issue
Block a user