12 Commits

Author SHA1 Message Date
fa7dd9e181 chore: bump version 2026-01-23 11:00:51 +03:00
a637ff4bca Merge pull request #9 from bratorange/logarithmic. Fixes #8
Logarithmic Knobs
2026-01-23 10:55:36 +03:00
e3a378cc6e Updated README 2026-01-23 10:51:12 +03:00
Julius Schmitt
6decbfdca1 remove one compiler warning 2026-01-22 21:03:54 +01:00
Julius Schmitt
9facd0fdfd update README.md 2026-01-22 21:03:26 +01:00
Julius Schmitt
37d0792175 add example option, clean up 2026-01-22 20:56:48 +01:00
Julius Schmitt
969ba9bcb5 logarithmic scaling added; missing the step fn for now 2026-01-22 20:46:46 +01:00
fb3f9d75dc updated cargo.lock 2026-01-13 23:41:33 +03:00
45ce317879 bump version 2026-01-13 23:39:41 +03:00
15021b8cfa Merge pull request #7 from maor1993/master
Support for using mouse scroll wheel to change values
2026-01-13 23:36:37 +03:00
Maor Malka
ad468ae18a Merge branch 'feature/scroll' 2026-01-13 05:58:41 -05:00
Maor Malka
2c5b209571 added support for using scroll wheel to modify knob value 2026-01-13 05:57:30 -05:00
9 changed files with 710 additions and 559 deletions

View File

@@ -2,6 +2,18 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## [0.3.10] - 2026-01-23
### 🚀 Features
- added support for logarithmic scaling ([#PR9](https://github.com/obsqrbtz/egui_knob/pull/9) by [bratorange](https://github.com/bratorange))
## [0.3.9] - 2026-01-13
### 🚀 Features
- added support for using mouse scroll wheel to change values ([#PR7](https://github.com/obsqrbtz/egui_knob/pull/7) by [maor1993](https://github.com/maor1993))
## [0.3.8] - 2026-01-12 ## [0.3.8] - 2026-01-12
### 🚀 Features ### 🚀 Features

1156
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "egui_knob" name = "egui_knob"
version = "0.3.8" version = "0.3.10"
edition = "2024" edition = "2024"
authors = ["Daniel Dada"] authors = ["Daniel Dada"]
description = "A simple knob widget for egui" description = "A simple knob widget for egui"

View File

@@ -18,6 +18,7 @@ A simple, customizable knob widget for egui.
- Configurable sweep range - Configurable sweep range
- Background arc with filled segments - Background arc with filled segments
- Adjustable drag sensitivity - Adjustable drag sensitivity
- Logarithmic scaling
## Installation ## Installation
@@ -27,7 +28,7 @@ To use the Knob widget in your project, add the following to your `Cargo.toml`:
[dependencies] [dependencies]
egui = "0.33" egui = "0.33"
eframe = "0.33" eframe = "0.33"
egui_knob = "0.3.8" egui_knob = "0.3.10"
``` ```
## Usage ## Usage
@@ -104,6 +105,13 @@ Knob::new(&mut value, 0.0, 1.0, KnobStyle::Wiper)
.with_label_format(|v| format!("{:.0}%", v * 100.0)); .with_label_format(|v| format!("{:.0}%", v * 100.0));
``` ```
#### Logarithmic Knobs
```rust
// Enable logarithmic scaling
Knob::new(&mut value, 0.0, 1.0, KnobStyle::Wiper)
.with_logarithmic_scaling();
```
## Running demo app ## Running demo app
```bash ```bash

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,13 +86,18 @@ impl eframe::App for KnobDemo {
.enumerate() .enumerate()
{ {
ui.vertical(|ui| { ui.vertical(|ui| {
let mut knob = Knob::new(&mut self.values[i], 0.0, 1.0, *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)
.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); .with_double_click_reset(0.5)
.with_middle_scroll();
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);

BIN
scrot.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 112 KiB

View File

@@ -17,6 +17,8 @@ pub struct KnobConfig {
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>, pub(crate) reset_value: Option<f32>,
pub(crate) allow_scroll: bool,
pub(crate) logarithmic_scaling: bool,
} }
impl KnobConfig { impl KnobConfig {
@@ -38,6 +40,8 @@ impl KnobConfig {
show_background_arc: true, show_background_arc: true,
show_filled_segments: true, show_filled_segments: true,
reset_value: None, reset_value: None,
allow_scroll:false,
logarithmic_scaling: false,
} }
} }
} }

View File

@@ -6,27 +6,28 @@ use crate::style::{KnobStyle, LabelPosition};
pub(crate) struct KnobRenderer<'a> { pub(crate) struct KnobRenderer<'a> {
config: &'a KnobConfig, config: &'a KnobConfig,
value: f32, value: f32,
raw: f32,
min: f32, min: f32,
max: f32, max: f32,
} }
impl<'a> KnobRenderer<'a> { impl<'a> KnobRenderer<'a> {
pub fn new(config: &'a KnobConfig, value: f32, min: f32, max: f32) -> Self { pub fn new(config: &'a KnobConfig, value: f32, raw: f32, min: f32, max: f32) -> Self {
Self { Self {
config, config,
value, value,
raw,
min, min,
max, max,
} }
} }
pub fn compute_angle(&self) -> f32 { pub fn compute_angle(&self) -> f32 {
if self.min == self.max || self.value.is_nan() { if self.min == self.max || self.raw.is_nan() {
self.config.min_angle self.config.min_angle
} else { } else {
self.config.min_angle self.config.min_angle
+ (self.value - self.min) / (self.max - self.min) + self.raw * (self.config.max_angle - self.config.min_angle)
* (self.config.max_angle - self.config.min_angle)
} }
} }
@@ -99,7 +100,7 @@ impl<'a> KnobRenderer<'a> {
if self.config.show_filled_segments { if self.config.show_filled_segments {
let filled_segments = (segments as f32 let filled_segments = (segments as f32
* ((self.value - self.min) / (self.max - self.min)).clamp(0.0, 1.0)) * self.raw.clamp(0.0, 1.0))
as usize; as usize;
if filled_segments > 0 { if filled_segments > 0 {

View File

@@ -1,4 +1,4 @@
use egui::{Color32, Response, Sense, Ui, Widget}; use egui::{remap, Color32, Response, Sense, Ui, Widget};
use crate::config::KnobConfig; use crate::config::KnobConfig;
use crate::render::KnobRenderer; use crate::render::KnobRenderer;
@@ -161,6 +161,17 @@ impl<'a> Knob<'a> {
self.config.reset_value = Some(reset_value); self.config.reset_value = Some(reset_value);
self self
} }
/// Allows user to use scroll wheel to change knob value
/// Uses config.step for the increment value
pub fn with_middle_scroll(mut self) -> Self {
self.config.allow_scroll = true;
self
}
pub fn with_logarithmic_scaling(mut self) -> Self {
self.config.logarithmic_scaling = true;
self
}
} }
impl Widget for Knob<'_> { impl Widget for Knob<'_> {
@@ -169,8 +180,13 @@ impl Widget for Knob<'_> {
*self.value = self.min; *self.value = self.min;
} }
let current_value = *self.value; let mut raw = if self.config.logarithmic_scaling {
let renderer = KnobRenderer::new(&self.config, current_value, self.min, self.max); remap(*self.value, self.min..=self.max, 1.0..=10.0).log(10.0)
} else {
remap(*self.value, self.min..=self.max, 0.0..=1.0)
};
let renderer = KnobRenderer::new(&self.config, *self.value, raw, 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::click_and_drag()); let (rect, response) = ui.allocate_exact_size(adjusted_size, Sense::click_and_drag());
@@ -178,23 +194,41 @@ 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 = self.max - self.min; 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);
let new_value = (*self.value - delta * step).clamp(self.min, self.max);
*self.value = if let Some(step) = self.config.step { raw = if let Some(step) = self.config.step {
let steps = ((new_value - self.min) / step).round(); let steps = (raw / step).round();
(self.min + steps * step).clamp(self.min, self.max) (steps * step).clamp(0.0, 1.0)
} else { } else {
new_value raw
}; };
if self.value.is_nan() { if self.value.is_nan() {
*self.value = self.min; *self.value = 0.0;
} }
response.mark_changed(); response.mark_changed();
} else if response.double_clicked() { } else if response.hovered() & self.config.allow_scroll {
if let Some(scoll) = ui.input(|input| {
input.events.iter().find_map(|e| match e {
egui::Event::MouseWheel { delta, .. } => Some(*delta),
_ => None,
})
}) {
raw = (raw
+ scoll.y * self.config.step.unwrap_or(self.config.drag_sensitivity))
.clamp(0.0, 1.0);
}
}
*self.value = if self.config.logarithmic_scaling {
remap(10f32.powf(raw), 1.0..=10.0, self.min..=self.max)
}else {
remap(raw, 0.0..=1.0, self.min..=self.max)
};
if response.double_clicked() {
if let Some(reset_value) = self.config.reset_value { if let Some(reset_value) = self.config.reset_value {
*self.value = reset_value *self.value = reset_value
} }
@@ -204,8 +238,7 @@ impl Widget for Knob<'_> {
let center = knob_rect.center(); let center = knob_rect.center();
let radius = self.config.size / 2.0; let radius = self.config.size / 2.0;
let updated_value = *self.value; let updated_renderer = KnobRenderer::new(&self.config, *self.value, raw, self.min, self.max);
let updated_renderer = KnobRenderer::new(&self.config, updated_value, self.min, self.max);
updated_renderer.render_knob(ui.painter(), center, radius, response.hovered()); updated_renderer.render_knob(ui.painter(), center, radius, response.hovered());
updated_renderer.render_label(ui, rect); updated_renderer.render_label(ui, rect);