mirror of
https://github.com/obsqrbtz/egui_knob.git
synced 2026-04-09 12:37:52 +03:00
Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a3c2db4d3 | |||
| 427434c607 | |||
| bcfcb55db3 | |||
| 23b2e3c792 | |||
| c2a1d57a60 | |||
| 05b9eb1df9 | |||
| 86d07f3593 | |||
| 617851102b | |||
| 530ba584bc | |||
| 135d70cfc8 | |||
|
|
c105722eb4 | ||
|
|
2a68f53a37 | ||
|
|
9968afbe48 | ||
| 06bca1c13a | |||
|
|
0a2915f31b | ||
| 39692f5011 | |||
| e79c4af600 | |||
| 7f24f4a468 | |||
| 64d072a1af | |||
| 19b0ff5405 | |||
| b141db8cd4 | |||
| 8a97e1ecd1 | |||
| b407b09853 | |||
| 9031d1c920 | |||
| 6aa6a231fa | |||
| 26ab08e7d5 | |||
| 421dd3ff60 | |||
| 268db4255e | |||
| 76670f416f | |||
| 0aec31e81d | |||
| 81bc2295fb | |||
| 6a49e6f961 | |||
| b20a393479 | |||
| e7f091a2f8 | |||
| f5b4bb7728 |
26
.vscode/launch.json
vendored
Normal file
26
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug example 'example_knob'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"build",
|
||||
"--example=example_knob",
|
||||
"--package=egui_knob"
|
||||
],
|
||||
"filter": {
|
||||
"name": "example_knob",
|
||||
"kind": "example"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
]
|
||||
}
|
||||
52
CHANGELOG.md
Normal file
52
CHANGELOG.md
Normal file
@@ -0,0 +1,52 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## [0.3.1] - 2025-07-15
|
||||
|
||||
### 🚀 Features
|
||||
|
||||
- Added background arc
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- Sanitize NaNs
|
||||
|
||||
## [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
|
||||
|
||||
### 🚀 Features
|
||||
|
||||
- Exposed label format
|
||||
- Add step size configuration
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- Add label offsets and position in relative to the widget rect
|
||||
|
||||
### 📚 Documentation
|
||||
|
||||
- Added doc comments
|
||||
|
||||
## [0.1.8] - 2025-02-03
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- Mark response changed when knob was dragged
|
||||
|
||||
<!-- 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]
|
||||
name = "egui_knob"
|
||||
version = "0.1.2"
|
||||
edition = "2021"
|
||||
version = "0.3.3"
|
||||
edition = "2024"
|
||||
description = "A simple knob widget for egui"
|
||||
homepage = "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"
|
||||
|
||||
[dependencies]
|
||||
egui = "0.30"
|
||||
egui = "0.32"
|
||||
|
||||
[dev-dependencies]
|
||||
eframe = { version = "0.30.0", features = ["default_fonts"] }
|
||||
eframe = { version = "0.32.0", features = ["default_fonts"] }
|
||||
|
||||
@@ -9,8 +9,9 @@ Simple knob widget for egui.
|
||||
## Features
|
||||
|
||||
- Adjustable size, font size, and stroke width.
|
||||
- Customizable colors for the knob and line.
|
||||
- Customizable colors for the knob, indicator and text.
|
||||
- Label positions (Top, Bottom, Left, Right).
|
||||
- Label formatting.
|
||||
- Two styles: Wiper and Dot.
|
||||
|
||||
## Installation
|
||||
@@ -19,8 +20,8 @@ To use the Knob widget in your project, add the following to your `Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
egui = "0.30"
|
||||
egui_knob = "0.1.1"
|
||||
egui = "0.32"
|
||||
egui_knob = "0.3.3"
|
||||
```
|
||||
|
||||
## Usage example
|
||||
|
||||
@@ -1,61 +1,112 @@
|
||||
use eframe::egui;
|
||||
use egui_knob::Knob;
|
||||
use egui_knob::{Knob, KnobStyle, LabelPosition};
|
||||
|
||||
fn main() -> eframe::Result<()> {
|
||||
eframe::run_native(
|
||||
"Knob Example",
|
||||
"egui_knob demo",
|
||||
eframe::NativeOptions::default(),
|
||||
Box::new(|_cc| Ok(Box::new(KnobExample::default()))),
|
||||
Box::new(|_cc| Ok(Box::new(KnobDemo::default()))),
|
||||
)
|
||||
}
|
||||
|
||||
struct KnobExample {
|
||||
value: f32,
|
||||
struct KnobDemo {
|
||||
values: [f32; 6],
|
||||
show_bg_arc: bool,
|
||||
show_filled: bool,
|
||||
use_step: bool,
|
||||
knob_color: egui::Color32,
|
||||
line_color: egui::Color32,
|
||||
text_color: egui::Color32,
|
||||
}
|
||||
|
||||
impl Default for KnobExample {
|
||||
impl Default for KnobDemo {
|
||||
fn default() -> Self {
|
||||
Self { value: 0.0 }
|
||||
Self {
|
||||
values: [f32::NAN; 6],
|
||||
show_bg_arc: true,
|
||||
show_filled: true,
|
||||
use_step: false,
|
||||
knob_color: egui::Color32::DARK_GRAY,
|
||||
line_color: egui::Color32::LIGHT_BLUE,
|
||||
text_color: egui::Color32::WHITE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for KnobExample {
|
||||
impl eframe::App for KnobDemo {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("🎛 egui-knob example");
|
||||
ui.separator();
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.add(
|
||||
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
|
||||
.with_label("Gain", egui_knob::LabelPosition::Bottom)
|
||||
.with_size(50.0),
|
||||
);
|
||||
ui.add(
|
||||
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Wiper)
|
||||
.with_label("Gain", egui_knob::LabelPosition::Bottom)
|
||||
.with_size(50.0),
|
||||
);
|
||||
ui.add_space(15.0);
|
||||
ui.add(
|
||||
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
|
||||
.with_label("Gain", egui_knob::LabelPosition::Bottom)
|
||||
.with_colors(
|
||||
egui::Color32::DARK_GRAY,
|
||||
egui::Color32::WHITE,
|
||||
egui::Color32::WHITE,
|
||||
)
|
||||
.with_size(50.0),
|
||||
);
|
||||
ui.add_space(15.0);
|
||||
ui.add(
|
||||
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Wiper)
|
||||
.with_label("Gain", egui_knob::LabelPosition::Bottom)
|
||||
.with_colors(
|
||||
egui::Color32::DARK_GRAY,
|
||||
egui::Color32::WHITE,
|
||||
egui::Color32::LIGHT_BLUE,
|
||||
)
|
||||
.with_size(50.0),
|
||||
);
|
||||
ui.checkbox(&mut self.show_bg_arc, "Show background arc");
|
||||
ui.checkbox(&mut self.show_filled, "Show filled segment");
|
||||
ui.checkbox(&mut self.use_step, "Enable step (0.1)");
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Knob Colors:");
|
||||
ui.color_edit_button_srgba(&mut self.knob_color);
|
||||
ui.color_edit_button_srgba(&mut self.line_color);
|
||||
ui.color_edit_button_srgba(&mut self.text_color);
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
ui.label("👇 Scroll or drag knobs to interact:");
|
||||
|
||||
ui.add_space(10.0);
|
||||
egui::Grid::new("knob_grid")
|
||||
.num_columns(3)
|
||||
.spacing([30.0, 20.0])
|
||||
.show(ui, |ui| {
|
||||
for (i, (label, config)) in [
|
||||
("Basic Dot", KnobStyle::Dot),
|
||||
("Wiper, Sweep", KnobStyle::Wiper),
|
||||
("Thick Stroke", KnobStyle::Wiper),
|
||||
("360° Sweep", KnobStyle::Wiper),
|
||||
("Multi-Turn", KnobStyle::Dot),
|
||||
("Large Font", KnobStyle::Wiper),
|
||||
]
|
||||
.iter()
|
||||
.enumerate()
|
||||
{
|
||||
ui.vertical(|ui| {
|
||||
ui.label(*label);
|
||||
let mut knob = Knob::new(&mut self.values[i], 0.0, 1.0, *config)
|
||||
.with_label(*label, LabelPosition::Bottom)
|
||||
.with_background_arc(self.show_bg_arc)
|
||||
.with_show_filled_segments(self.show_filled)
|
||||
.with_colors(self.knob_color, self.line_color, self.text_color)
|
||||
.with_step(self.use_step.then_some(0.1));
|
||||
|
||||
if *label == "Wiper, Sweep" {
|
||||
knob = knob.with_sweep_range(0.25, 0.75).with_size(50.0);
|
||||
}
|
||||
if *label == "Thick Stroke" {
|
||||
knob = knob.with_stroke_width(4.0).with_size(60.0);
|
||||
}
|
||||
if *label == "360° Sweep" {
|
||||
knob = knob.with_sweep_range(0.5, 1.0);
|
||||
}
|
||||
if *label == "Multi-Turn" {
|
||||
knob = knob.with_sweep_range(0.0, 2.5);
|
||||
}
|
||||
if *label == "Large Font" {
|
||||
knob = knob.with_size(70.0).with_font_size(18.0);
|
||||
}
|
||||
|
||||
ui.add(knob);
|
||||
});
|
||||
|
||||
if (i + 1) % 3 == 0 {
|
||||
ui.end_row();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ui.add_space(10.0);
|
||||
ui.separator();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
BIN
scrot.png
BIN
scrot.png
Binary file not shown.
|
Before Width: | Height: | Size: 9.4 KiB After Width: | Height: | Size: 46 KiB |
331
src/lib.rs
331
src/lib.rs
@@ -1,5 +1,8 @@
|
||||
use egui::{Align2, Color32, Response, Sense, Stroke, Ui, Vec2, Widget};
|
||||
use core::f32;
|
||||
|
||||
use egui::{Align2, Color32, Rect, Response, Sense, Stroke, Ui, Vec2, Widget};
|
||||
|
||||
/// Position of the label relative to the knob
|
||||
pub enum LabelPosition {
|
||||
Top,
|
||||
Bottom,
|
||||
@@ -7,11 +10,25 @@ pub enum LabelPosition {
|
||||
Right,
|
||||
}
|
||||
|
||||
/// Visual style of the knob indicator
|
||||
#[derive(Clone, Copy)] // <--- add this!
|
||||
pub enum KnobStyle {
|
||||
/// A line extending from the center to the edge
|
||||
Wiper,
|
||||
/// A dot on the edge of the knob
|
||||
Dot,
|
||||
}
|
||||
|
||||
/// A circular knob widget for egui that can be dragged to change a value
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// let mut value = 0.5;
|
||||
/// Knob::new(&mut value, 0.0, 1.0, KnobStyle::Wiper)
|
||||
/// .with_size(50.0)
|
||||
/// .with_label("Volume", LabelPosition::Bottom)
|
||||
/// .with_step(0.1);
|
||||
/// ```
|
||||
pub struct Knob<'a> {
|
||||
value: &'a mut f32,
|
||||
min: f32,
|
||||
@@ -25,9 +42,33 @@ pub struct Knob<'a> {
|
||||
label: Option<String>,
|
||||
label_position: LabelPosition,
|
||||
style: KnobStyle,
|
||||
label_offset: f32,
|
||||
label_format: Box<dyn Fn(f32) -> String>,
|
||||
step: Option<f32>,
|
||||
drag_sensitivity: f32,
|
||||
show_background_arc: bool,
|
||||
show_filled_segments: bool,
|
||||
|
||||
/// 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> {
|
||||
/// Creates a new knob widget
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `value` - Mutable reference to the value controlled by the knob
|
||||
/// * `min` - Minimum value
|
||||
/// * `max` - Maximum value
|
||||
/// * `style` - Visual style of the knob indicator
|
||||
pub fn new(value: &'a mut f32, min: f32, max: f32, style: KnobStyle) -> Self {
|
||||
Self {
|
||||
value,
|
||||
@@ -42,24 +83,76 @@ impl<'a> Knob<'a> {
|
||||
label: None,
|
||||
label_position: LabelPosition::Bottom,
|
||||
style,
|
||||
label_offset: 1.0,
|
||||
label_format: Box::new(|v| format!("{:.2}", v)),
|
||||
step: None,
|
||||
|
||||
// Hardcode those two angles to ENSURE backward compatibility
|
||||
min_angle: -std::f32::consts::PI,
|
||||
max_angle: std::f32::consts::PI * 0.5,
|
||||
drag_sensitivity: 0.005,
|
||||
show_background_arc: true,
|
||||
show_filled_segments: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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() {
|
||||
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
|
||||
pub fn with_size(mut self, size: f32) -> Self {
|
||||
self.size = size;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the font size for the label
|
||||
pub fn with_font_size(mut self, size: f32) -> Self {
|
||||
self.font_size = size;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the stroke width for the knob's outline and indicator
|
||||
pub fn with_stroke_width(mut self, width: f32) -> Self {
|
||||
self.stroke_width = width;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the colors for different parts of the knob
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `knob_color` - Color of the knob's outline
|
||||
/// * `line_color` - Color of the indicator
|
||||
/// * `text_color` - Color of the label text
|
||||
pub fn with_colors(
|
||||
mut self,
|
||||
knob_color: Color32,
|
||||
@@ -72,53 +165,187 @@ impl<'a> Knob<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Adds a label to the knob
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `label` - Text to display
|
||||
/// * `position` - Position of the label relative to the knob
|
||||
pub fn with_label(mut self, label: impl Into<String>, position: LabelPosition) -> Self {
|
||||
self.label = Some(label.into());
|
||||
self.label_position = position;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the spacing between the knob and its label
|
||||
pub fn with_label_offset(mut self, offset: f32) -> Self {
|
||||
self.label_offset = offset;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets a custom format function for displaying the value
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// # let mut value = 0.5;
|
||||
/// Knob::new(&mut value, 0.0, 1.0, KnobStyle::Wiper)
|
||||
/// .with_label_format(|v| format!("{:.1}%", v * 100.0));
|
||||
/// ```
|
||||
pub fn with_label_format(mut self, format: impl Fn(f32) -> String + 'static) -> Self {
|
||||
self.label_format = Box::new(format);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the step size for value changes
|
||||
///
|
||||
/// When set, the value will snap to discrete steps as the knob is dragged.
|
||||
pub fn with_step(mut self, step: Option<f32>) -> Self {
|
||||
self.step = step;
|
||||
self
|
||||
}
|
||||
/// Shows the background arc showing full range
|
||||
pub fn with_background_arc(mut self, enabled: bool) -> Self {
|
||||
self.show_background_arc = enabled;
|
||||
self
|
||||
}
|
||||
/// Shows filled range on the background arc
|
||||
pub fn with_show_filled_segments(mut self, enabled: bool) -> Self {
|
||||
self.show_filled_segments = enabled;
|
||||
self
|
||||
}
|
||||
// Private
|
||||
fn compute_angle(&self) -> f32 {
|
||||
if self.min == self.max || self.value.is_nan() {
|
||||
self.min_angle
|
||||
} else {
|
||||
self.min_angle
|
||||
+ (*self.value - self.min) / (self.max - self.min)
|
||||
* (self.max_angle - self.min_angle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Knob<'_> {
|
||||
fn ui(self, ui: &mut Ui) -> Response {
|
||||
let desired_size = Vec2::splat(self.size);
|
||||
if self.value.is_nan() {
|
||||
*self.value = self.min;
|
||||
}
|
||||
|
||||
let knob_size = Vec2::splat(self.size);
|
||||
|
||||
let label_size = if let Some(label) = &self.label {
|
||||
let font_id = egui::FontId::proportional(self.font_size);
|
||||
let max_text = format!("{}: {}", label, (self.label_format)(self.max));
|
||||
ui.painter()
|
||||
.layout(
|
||||
format!("{}: {:.2}", label, self.value),
|
||||
font_id,
|
||||
Color32::WHITE,
|
||||
f32::INFINITY,
|
||||
)
|
||||
.layout(max_text, font_id, Color32::WHITE, f32::INFINITY)
|
||||
.size()
|
||||
} else {
|
||||
Vec2::ZERO
|
||||
};
|
||||
|
||||
// Adjust desired size to include label space
|
||||
let adjusted_size = Vec2::new(desired_size.x, desired_size.y + label_size.y + 12.0); // 12.0 is an offset for spacing
|
||||
let (rect, response) = ui.allocate_exact_size(adjusted_size, Sense::drag());
|
||||
let label_padding = 2.0;
|
||||
|
||||
let adjusted_size = match self.label_position {
|
||||
LabelPosition::Top | LabelPosition::Bottom => Vec2::new(
|
||||
knob_size.x.max(label_size.x + label_padding * 2.0),
|
||||
knob_size.y + label_size.y + label_padding * 2.0 + self.label_offset,
|
||||
),
|
||||
LabelPosition::Left | LabelPosition::Right => Vec2::new(
|
||||
knob_size.x + label_size.x + label_padding * 2.0 + self.label_offset,
|
||||
knob_size.y.max(label_size.y + label_padding * 2.0),
|
||||
),
|
||||
};
|
||||
|
||||
let (rect, mut response) = ui.allocate_exact_size(adjusted_size, Sense::drag());
|
||||
|
||||
if response.dragged() {
|
||||
let delta = response.drag_delta().y;
|
||||
let range = self.max - self.min;
|
||||
let step = range * 0.005;
|
||||
*self.value = (*self.value - delta * step).clamp(self.min, self.max);
|
||||
let step = self.step.unwrap_or(range * self.drag_sensitivity);
|
||||
let new_value = (*self.value - delta * step).clamp(self.min, self.max);
|
||||
|
||||
*self.value = if let Some(step) = self.step {
|
||||
let steps = ((new_value - self.min) / step).round();
|
||||
(self.min + steps * step).clamp(self.min, self.max)
|
||||
} else {
|
||||
new_value
|
||||
};
|
||||
|
||||
if self.value.is_nan() {
|
||||
*self.value = self.min;
|
||||
}
|
||||
|
||||
response.mark_changed();
|
||||
}
|
||||
|
||||
let painter = ui.painter();
|
||||
let center = rect.center();
|
||||
let radius = rect.width() / 2.0;
|
||||
let angle = (*self.value - self.min) / (self.max - self.min) * std::f32::consts::PI * 1.5
|
||||
- std::f32::consts::PI;
|
||||
let knob_rect = match self.label_position {
|
||||
LabelPosition::Left => {
|
||||
Rect::from_min_size(rect.right_top() + Vec2::new(-knob_size.x, 0.0), knob_size)
|
||||
}
|
||||
LabelPosition::Right => Rect::from_min_size(rect.left_top(), knob_size),
|
||||
LabelPosition::Top => Rect::from_min_size(
|
||||
rect.left_bottom() + Vec2::new((rect.width() - knob_size.x) / 2.0, -knob_size.y),
|
||||
knob_size,
|
||||
),
|
||||
LabelPosition::Bottom => Rect::from_min_size(
|
||||
rect.left_top() + Vec2::new((rect.width() - knob_size.x) / 2.0, 0.0),
|
||||
knob_size,
|
||||
),
|
||||
};
|
||||
|
||||
painter.circle_stroke(
|
||||
center,
|
||||
radius,
|
||||
Stroke::new(self.stroke_width, self.knob_color),
|
||||
);
|
||||
let center = knob_rect.center();
|
||||
let radius = knob_size.x / 2.0;
|
||||
let angle = self.compute_angle();
|
||||
let knob_hovered = response.hovered();
|
||||
let knob_color = if knob_hovered {
|
||||
self.knob_color.linear_multiply(1.2)
|
||||
} else {
|
||||
self.knob_color
|
||||
};
|
||||
|
||||
painter.circle_stroke(center, radius, Stroke::new(self.stroke_width, knob_color));
|
||||
|
||||
// Background arc (indicating full range)
|
||||
if self.show_background_arc {
|
||||
let arc_start = self.min_angle;
|
||||
let arc_end = self.max_angle;
|
||||
let segments = 64;
|
||||
let arc_color = self.knob_color.gamma_multiply(0.5); // dimmed background arc
|
||||
let arc_radius = radius * 0.8;
|
||||
let mut points = Vec::with_capacity(segments + 1);
|
||||
|
||||
for i in 0..=segments {
|
||||
let t = i as f32 / segments as f32;
|
||||
let angle = arc_start + (arc_end - arc_start) * t;
|
||||
let pos = center + Vec2::angled(angle) * arc_radius;
|
||||
points.push(pos);
|
||||
}
|
||||
|
||||
painter.add(egui::Shape::line(
|
||||
points,
|
||||
Stroke::new(self.stroke_width, arc_color),
|
||||
));
|
||||
|
||||
// Filled part when background arc is enabled
|
||||
if self.show_filled_segments {
|
||||
let filled_segments = (segments as f32
|
||||
* ((*self.value - self.min) / (self.max - self.min)).clamp(0.0, 1.0))
|
||||
as usize;
|
||||
|
||||
let mut fill_points = Vec::with_capacity(filled_segments + 1);
|
||||
for i in 0..=filled_segments {
|
||||
let t = i as f32 / segments as f32;
|
||||
let angle = arc_start + (arc_end - arc_start) * t;
|
||||
let pos = center + Vec2::angled(angle) * arc_radius;
|
||||
fill_points.push(pos);
|
||||
}
|
||||
|
||||
painter.add(egui::Shape::line(
|
||||
fill_points,
|
||||
Stroke::new(self.stroke_width, self.line_color),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
match self.style {
|
||||
KnobStyle::Wiper => {
|
||||
@@ -135,43 +362,51 @@ impl Widget for Knob<'_> {
|
||||
}
|
||||
|
||||
if let Some(label) = self.label {
|
||||
let label_text = format!("{label}: {:.2}", self.value);
|
||||
let label_text = format!("{}: {}", label, (self.label_format)(*self.value));
|
||||
let font_id = egui::FontId::proportional(self.font_size);
|
||||
let text_size = ui
|
||||
.painter()
|
||||
.layout(
|
||||
label_text.clone(),
|
||||
font_id.clone(),
|
||||
Color32::WHITE,
|
||||
f32::INFINITY,
|
||||
)
|
||||
.size();
|
||||
|
||||
let label_offset = 12.0;
|
||||
let label_pos = match self.label_position {
|
||||
LabelPosition::Top => {
|
||||
center + Vec2::new(-text_size.x / 2.0, -radius - label_offset - text_size.y)
|
||||
}
|
||||
LabelPosition::Bottom => {
|
||||
center + Vec2::new(-text_size.x / 2.0, radius + label_offset)
|
||||
}
|
||||
LabelPosition::Left => {
|
||||
center + Vec2::new(-radius - label_offset - text_size.x, -text_size.y / 2.0)
|
||||
}
|
||||
LabelPosition::Right => {
|
||||
center + Vec2::new(radius + label_offset, -text_size.y / 2.0)
|
||||
}
|
||||
let label_padding = 2.0;
|
||||
|
||||
let (label_pos, alignment) = match self.label_position {
|
||||
LabelPosition::Top => (
|
||||
Vec2::new(
|
||||
rect.center().x,
|
||||
rect.min.y - self.label_offset + label_padding,
|
||||
),
|
||||
Align2::CENTER_TOP,
|
||||
),
|
||||
LabelPosition::Bottom => (
|
||||
Vec2::new(rect.center().x, rect.max.y + self.label_offset),
|
||||
Align2::CENTER_BOTTOM,
|
||||
),
|
||||
LabelPosition::Left => (
|
||||
Vec2::new(rect.min.x - self.label_offset, rect.center().y),
|
||||
Align2::LEFT_CENTER,
|
||||
),
|
||||
LabelPosition::Right => (
|
||||
Vec2::new(rect.max.x + self.label_offset, rect.center().y),
|
||||
Align2::RIGHT_CENTER,
|
||||
),
|
||||
};
|
||||
|
||||
ui.painter().text(
|
||||
label_pos,
|
||||
Align2::LEFT_TOP,
|
||||
label_pos.to_pos2(),
|
||||
alignment,
|
||||
label_text,
|
||||
font_id,
|
||||
self.text_color,
|
||||
);
|
||||
|
||||
if response.hovered() {
|
||||
response
|
||||
.clone()
|
||||
.on_hover_text((self.label_format)(*self.value));
|
||||
}
|
||||
}
|
||||
|
||||
// Draw the bounding rect
|
||||
//painter.rect_stroke(rect, 0.0, Stroke::new(1.0, Color32::RED));
|
||||
|
||||
response
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user