add example option, clean up

This commit is contained in:
Julius Schmitt
2026-01-22 20:56:48 +01:00
parent 969ba9bcb5
commit 37d0792175
2 changed files with 16 additions and 10 deletions

View File

@@ -21,6 +21,7 @@ struct KnobDemo {
show_bg_arc: bool, show_bg_arc: bool,
show_filled: bool, show_filled: bool,
use_step: bool, use_step: bool,
logarithmic_scaling: bool,
knob_color: egui::Color32, knob_color: egui::Color32,
line_color: egui::Color32, line_color: egui::Color32,
text_color: egui::Color32, text_color: egui::Color32,
@@ -33,6 +34,7 @@ impl Default for KnobDemo {
show_bg_arc: true, show_bg_arc: true,
show_filled: true, show_filled: true,
use_step: false, use_step: false,
logarithmic_scaling: false,
knob_color: egui::Color32::DARK_GRAY, knob_color: egui::Color32::DARK_GRAY,
line_color: egui::Color32::LIGHT_BLUE, line_color: egui::Color32::LIGHT_BLUE,
text_color: egui::Color32::WHITE, text_color: egui::Color32::WHITE,
@@ -51,6 +53,8 @@ impl eframe::App for KnobDemo {
ui.checkbox(&mut self.show_bg_arc, "Background arc"); ui.checkbox(&mut self.show_bg_arc, "Background arc");
ui.checkbox(&mut self.show_filled, "Filled segment"); ui.checkbox(&mut self.show_filled, "Filled segment");
ui.checkbox(&mut self.use_step, "Step (0.02)"); ui.checkbox(&mut self.use_step, "Step (0.02)");
ui.checkbox(&mut self.logarithmic_scaling, "Logarithmic");
}); });
ui.horizontal(|ui| { ui.horizontal(|ui| {
@@ -82,7 +86,7 @@ impl eframe::App for KnobDemo {
.enumerate() .enumerate()
{ {
ui.vertical(|ui| { ui.vertical(|ui| {
let mut knob = Knob::new(&mut self.values[i], 0., 3., *config) let mut knob = Knob::new(&mut self.values[i], 0., 1., *config)
.with_label(*label, LabelPosition::Bottom) .with_label(*label, LabelPosition::Bottom)
.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)
@@ -90,7 +94,10 @@ impl eframe::App for KnobDemo {
.with_step(self.use_step.then_some(0.02)) .with_step(self.use_step.then_some(0.02))
.with_double_click_reset(0.5) .with_double_click_reset(0.5)
.with_middle_scroll(); .with_middle_scroll();
//.with_logarithmic_scaling();
if self.logarithmic_scaling {
knob = knob.with_logarithmic_scaling();
}
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);

View File

@@ -194,20 +194,19 @@ impl Widget for Knob<'_> {
let mut response = response; let mut response = response;
if response.dragged() { if response.dragged() {
let delta = response.drag_delta().y; let delta = response.drag_delta().y;
let range = 1.0; let step = self.config.step.unwrap_or(self.config.drag_sensitivity);
let step = self.config.step.unwrap_or(range * self.config.drag_sensitivity);
raw = (raw - delta * step).clamp(0.0,1.0); raw = (raw - delta * step).clamp(0.0,1.0);
/*raw = if let Some(step) = self.config.step { raw = if let Some(step) = self.config.step {
let steps = ((raw - 1.0) / step).round(); let steps = (raw / step).round();
(steps * step).clamp(1.0, 2.0) (steps * step).clamp(0.0, 1.0)
} else { } else {
raw raw
};*/ };
/*if self.value.is_nan() { if self.value.is_nan() {
*self.value = 0.0; *self.value = 0.0;
}*/ }
response.mark_changed(); response.mark_changed();
} else if response.hovered() & self.config.allow_scroll { } else if response.hovered() & self.config.allow_scroll {