mirror of
https://github.com/obsqrbtz/egui_knob.git
synced 2026-04-09 12:37:52 +03:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 617851102b | |||
| 530ba584bc | |||
| 135d70cfc8 | |||
|
|
c105722eb4 | ||
|
|
2a68f53a37 | ||
|
|
9968afbe48 | ||
| 06bca1c13a | |||
|
|
0a2915f31b |
18
CHANGELOG.md
18
CHANGELOG.md
@@ -2,6 +2,22 @@
|
|||||||
|
|
||||||
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.1] - 2025-07-15
|
||||||
|
|
||||||
|
### 🛠 Maintenance
|
||||||
|
|
||||||
|
- Bump egui version to 0.32
|
||||||
|
|
||||||
|
## [0.3.0] - 2025-07-07
|
||||||
|
|
||||||
|
### 🚀 Features
|
||||||
|
|
||||||
|
- Add configurable sweep range with `with_sweep_range()` method ([PR#2](https://github.com/obsqrbtz/egui_knob/pull/2) by [sacha-renault](https://github.com/sacha-renault))
|
||||||
|
|
||||||
|
## [0.2.0] - 2025-05-12
|
||||||
|
|
||||||
|
- Bump egui and eframe versions ([PR#1](https://github.com/obsqrbtz/egui_knob/pull/1) by [chloebrett](https://github.com/chloebrett))
|
||||||
|
|
||||||
## [0.1.9] - 2025-02-04
|
## [0.1.9] - 2025-02-04
|
||||||
|
|
||||||
### 🚀 Features
|
### 🚀 Features
|
||||||
@@ -21,6 +37,6 @@ All notable changes to this project will be documented in this file.
|
|||||||
|
|
||||||
### 🐛 Bug Fixes
|
### 🐛 Bug Fixes
|
||||||
|
|
||||||
- Mark reponse changed when knob was dragged
|
- Mark response changed when knob was dragged
|
||||||
|
|
||||||
<!-- generated by git-cliff -->
|
<!-- generated by git-cliff -->
|
||||||
|
|||||||
837
Cargo.lock
generated
837
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "egui_knob"
|
name = "egui_knob"
|
||||||
version = "0.1.9"
|
version = "0.3.1"
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
description = "A simple knob widget for egui"
|
description = "A simple knob widget for egui"
|
||||||
homepage = "https://github.com/obsqrbtz/egui_knob"
|
homepage = "https://github.com/obsqrbtz/egui_knob"
|
||||||
repository = "https://github.com/obsqrbtz/egui_knob"
|
repository = "https://github.com/obsqrbtz/egui_knob"
|
||||||
@@ -19,7 +19,7 @@ name = "example_knob"
|
|||||||
path = "examples/example_knob.rs"
|
path = "examples/example_knob.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
egui = "0.30"
|
egui = "0.32"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
eframe = { version = "0.30.0", features = ["default_fonts"] }
|
eframe = { version = "0.32.0", features = ["default_fonts"] }
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ Simple knob widget for egui.
|
|||||||
- Adjustable size, font size, and stroke width.
|
- Adjustable size, font size, and stroke width.
|
||||||
- Customizable colors for the knob, indicator and text.
|
- Customizable colors for the knob, indicator and text.
|
||||||
- Label positions (Top, Bottom, Left, Right).
|
- Label positions (Top, Bottom, Left, Right).
|
||||||
- Label formating.
|
- Label formatting.
|
||||||
- Two styles: Wiper and Dot.
|
- Two styles: Wiper and Dot.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
@@ -20,8 +20,8 @@ To use the Knob widget in your project, add the following to your `Cargo.toml`:
|
|||||||
|
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
egui = "0.30"
|
egui = "0.32"
|
||||||
egui_knob = "*"
|
egui_knob = "0.3.1"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage example
|
## Usage example
|
||||||
|
|||||||
@@ -117,18 +117,20 @@ impl eframe::App for KnobExample {
|
|||||||
egui::Color32::from_rgb(100, 255, 100),
|
egui::Color32::from_rgb(100, 255, 100),
|
||||||
)
|
)
|
||||||
.with_size(50.0)
|
.with_size(50.0)
|
||||||
.with_label_format(|v| format!("{:.2}%", v)),
|
.with_label_format(|v| format!("{:.2}%", v))
|
||||||
|
.with_sweep_range(1. / 8., 0.75),
|
||||||
);
|
);
|
||||||
|
|
||||||
ui.add(
|
ui.add(
|
||||||
Knob::new(&mut self.blue_value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
|
Knob::new(&mut self.blue_value, 0.0, 100., egui_knob::KnobStyle::Dot)
|
||||||
.with_label("Top", egui_knob::LabelPosition::Top)
|
.with_label("Top", egui_knob::LabelPosition::Top)
|
||||||
.with_colors(
|
.with_colors(
|
||||||
egui::Color32::from_rgb(30, 30, 80),
|
egui::Color32::from_rgb(30, 30, 80),
|
||||||
egui::Color32::from_rgb(50, 50, 220),
|
egui::Color32::from_rgb(50, 50, 220),
|
||||||
egui::Color32::from_rgb(100, 100, 255),
|
egui::Color32::from_rgb(100, 100, 255),
|
||||||
)
|
)
|
||||||
.with_size(50.0),
|
.with_size(50.0)
|
||||||
|
.with_sweep_range(0., 2.),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
62
src/lib.rs
62
src/lib.rs
@@ -1,3 +1,5 @@
|
|||||||
|
use core::f32;
|
||||||
|
|
||||||
use egui::{Align2, Color32, Rect, Response, Sense, Stroke, Ui, Vec2, Widget};
|
use egui::{Align2, Color32, Rect, Response, Sense, Stroke, Ui, Vec2, Widget};
|
||||||
|
|
||||||
/// Position of the label relative to the knob
|
/// Position of the label relative to the knob
|
||||||
@@ -42,6 +44,17 @@ pub struct Knob<'a> {
|
|||||||
label_offset: f32,
|
label_offset: f32,
|
||||||
label_format: Box<dyn Fn(f32) -> String>,
|
label_format: Box<dyn Fn(f32) -> String>,
|
||||||
step: Option<f32>,
|
step: Option<f32>,
|
||||||
|
|
||||||
|
/// Minimum angle in radians.
|
||||||
|
/// Specifies the lower bound of the knob's rotation.
|
||||||
|
/// Expected range: 0.0 to TAU. Values outside this range are allowed if your use case requires it.
|
||||||
|
min_angle: f32,
|
||||||
|
|
||||||
|
/// Maximum angle in radians.
|
||||||
|
/// Specifies the upper bound of the knob's rotation.
|
||||||
|
/// Can be any value > `min_angle`
|
||||||
|
/// range `max_angle` - `min_angle` > TAU is allowed but will induce multi-turn
|
||||||
|
max_angle: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Knob<'a> {
|
impl<'a> Knob<'a> {
|
||||||
@@ -69,9 +82,47 @@ impl<'a> Knob<'a> {
|
|||||||
label_offset: 1.0,
|
label_offset: 1.0,
|
||||||
label_format: Box::new(|v| format!("{:.2}", v)),
|
label_format: Box::new(|v| format!("{:.2}", v)),
|
||||||
step: None,
|
step: None,
|
||||||
|
|
||||||
|
// Hardcode those two angles to ENSURE backward compatibility
|
||||||
|
min_angle: -std::f32::consts::PI,
|
||||||
|
max_angle: std::f32::consts::PI * 0.5,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the angular sweep range of the knob
|
||||||
|
///
|
||||||
|
/// This controls where the knob starts and how far it can rotate. By default,
|
||||||
|
/// knobs start at the left (180°) and sweep 270° clockwise to bottom.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
/// * `start_angle_normalized` - Starting position as fraction of full circle:
|
||||||
|
/// - `0.0` = bottom (6 o'clock)
|
||||||
|
/// - `0.25` = left (9 o'clock)
|
||||||
|
/// - `0.5` = top (12 o'clock)
|
||||||
|
/// - `0.75` = right (3 o'clock)
|
||||||
|
/// * `range` - How far the knob can sweep as fraction of full circle:
|
||||||
|
/// - `0.25` = quarter turn (90°)
|
||||||
|
/// - `0.5` = half turn (180°)
|
||||||
|
/// - `0.75` = three-quarter turn (270°)
|
||||||
|
/// - `1.0` = full turn (360°)
|
||||||
|
/// - Values > 1.0 create multi-turn knobs
|
||||||
|
/// - Negative values are clamped to 0.0
|
||||||
|
///
|
||||||
|
/// Note: the start angle is offset by PI/2 so that `0.0` is at the bottom (6 o'clock)
|
||||||
|
pub fn with_sweep_range(mut self, start_angle_normalized: f32, range: f32) -> Self {
|
||||||
|
if start_angle_normalized.is_nan() || range.is_nan() {
|
||||||
|
// Invalid input, return unchanged
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.min_angle =
|
||||||
|
start_angle_normalized.rem_euclid(1.) * f32::consts::TAU + f32::consts::PI / 2.;
|
||||||
|
|
||||||
|
// A range of 1. represent a full turn
|
||||||
|
self.max_angle = self.min_angle + range.max(0.) * f32::consts::TAU;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the size of the knob
|
/// Sets the size of the knob
|
||||||
pub fn with_size(mut self, size: f32) -> Self {
|
pub fn with_size(mut self, size: f32) -> Self {
|
||||||
self.size = size;
|
self.size = size;
|
||||||
@@ -210,8 +261,15 @@ impl Widget for Knob<'_> {
|
|||||||
|
|
||||||
let center = knob_rect.center();
|
let center = knob_rect.center();
|
||||||
let radius = knob_size.x / 2.0;
|
let radius = knob_size.x / 2.0;
|
||||||
let angle = (*self.value - self.min) / (self.max - self.min) * std::f32::consts::PI * 1.5
|
let angle = if self.min == self.max {
|
||||||
- std::f32::consts::PI;
|
// If min == max, just return min angle
|
||||||
|
// That's a edge case, using a 0 range knob is pretty useless
|
||||||
|
self.min_angle
|
||||||
|
} else {
|
||||||
|
self.min_angle
|
||||||
|
+ (*self.value - self.min) / (self.max - self.min)
|
||||||
|
* (self.max_angle - self.min_angle)
|
||||||
|
};
|
||||||
|
|
||||||
painter.circle_stroke(
|
painter.circle_stroke(
|
||||||
center,
|
center,
|
||||||
|
|||||||
Reference in New Issue
Block a user