mirror of
https://github.com/obsqrbtz/egui_knob.git
synced 2026-04-09 12:37:52 +03:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c31936f3b0 | |||
| d20180729a | |||
|
|
353dcd8cc4 | ||
| fa7dd9e181 | |||
| a637ff4bca | |||
| e3a378cc6e | |||
|
|
6decbfdca1 | ||
|
|
9facd0fdfd | ||
|
|
37d0792175 | ||
|
|
969ba9bcb5 | ||
| fb3f9d75dc | |||
| 45ce317879 | |||
| 15021b8cfa | |||
|
|
ad468ae18a | ||
|
|
2c5b209571 | ||
| c1db9a2e5b | |||
| 750d8afb25 | |||
| 4e8975c955 | |||
|
|
824b492b92 | ||
| a70b57e62b | |||
| a2a102cdb6 |
27
.github/workflows/publish.yml
vendored
Normal file
27
.github/workflows/publish.yml
vendored
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
name: Publish to crates.io
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v6
|
||||||
|
|
||||||
|
- name: Install Rust
|
||||||
|
uses: dtolnay/rust-toolchain@stable
|
||||||
|
|
||||||
|
- name: Check if version matches tag
|
||||||
|
run: |
|
||||||
|
TAG=${GITHUB_REF#refs/tags/v}
|
||||||
|
CARGO_VERSION=$(grep '^version' Cargo.toml | head -1 | cut -d'"' -f2)
|
||||||
|
if [ "$TAG" != "$CARGO_VERSION" ]; then
|
||||||
|
echo "Tag version ($TAG) doesn't match Cargo.toml version ($CARGO_VERSION)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Publish to crates.io
|
||||||
|
run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
|
||||||
36
CHANGELOG.md
36
CHANGELOG.md
@@ -2,6 +2,42 @@
|
|||||||
|
|
||||||
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.11] - 2026-03-30
|
||||||
|
|
||||||
|
- bumped egui version ([#PR10](https://github.com/obsqrbtz/egui_knob/pull/10) by [elwerene](https://github.com/elwerene)
|
||||||
|
|
||||||
|
### 🛠 Maintenance
|
||||||
|
|
||||||
|
## [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
|
||||||
|
|
||||||
|
### 🚀 Features
|
||||||
|
|
||||||
|
- added `with_double_click_reset` option ([#PR6](https://github.com/obsqrbtz/egui_knob/pull/6) by [maor1993](https://github.com/maor1993))
|
||||||
|
|
||||||
|
## [0.3.7] - 2026-01-06
|
||||||
|
|
||||||
|
### 🛠 Maintenance
|
||||||
|
|
||||||
|
- expose egui types
|
||||||
|
|
||||||
|
## [0.3.6] - 2025-07-15
|
||||||
|
|
||||||
|
### 🛠 Maintenance
|
||||||
|
|
||||||
|
- cleaned up io a bit
|
||||||
|
|
||||||
## [0.3.5] - 2025-07-15
|
## [0.3.5] - 2025-07-15
|
||||||
|
|
||||||
### 🛠 Maintenance
|
### 🛠 Maintenance
|
||||||
|
|||||||
2438
Cargo.lock
generated
2438
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "egui_knob"
|
name = "egui_knob"
|
||||||
version = "0.3.5"
|
version = "0.3.11"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors = ["Daniel Dada"]
|
authors = ["Daniel Dada"]
|
||||||
description = "A simple knob widget for egui"
|
description = "A simple knob widget for egui"
|
||||||
@@ -21,7 +21,7 @@ name = "example_knob"
|
|||||||
path = "examples/example_knob.rs"
|
path = "examples/example_knob.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
egui = "0.33"
|
egui = "0.34"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
eframe = { version = "0.33", features = ["default_fonts"] }
|
eframe = { version = "0.34", features = ["default_fonts"] }
|
||||||
|
|||||||
10
README.md
10
README.md
@@ -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.5"
|
egui_knob = "0.3.11"
|
||||||
```
|
```
|
||||||
|
|
||||||
## 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
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ use egui_knob::{Knob, KnobStyle, LabelPosition};
|
|||||||
fn main() -> eframe::Result<()> {
|
fn main() -> eframe::Result<()> {
|
||||||
let options = eframe::NativeOptions {
|
let options = eframe::NativeOptions {
|
||||||
viewport: egui::ViewportBuilder::default()
|
viewport: egui::ViewportBuilder::default()
|
||||||
.with_inner_size([800.0, 600.0])
|
.with_inner_size([600.0, 350.0])
|
||||||
.with_title("egui_knob demo"),
|
.with_title("Knob demo"),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
eframe::run_native(
|
eframe::run_native(
|
||||||
"egui_knob demo",
|
"Knob demo",
|
||||||
options,
|
options,
|
||||||
Box::new(|_cc| Ok(Box::new(KnobDemo::default()))),
|
Box::new(|_cc| Ok(Box::new(KnobDemo::default()))),
|
||||||
)
|
)
|
||||||
@@ -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,
|
||||||
@@ -41,82 +43,86 @@ impl Default for KnobDemo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl eframe::App for KnobDemo {
|
impl eframe::App for KnobDemo {
|
||||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
ui.heading("Knob demo");
|
||||||
ui.heading("egui_knob demo");
|
ui.separator();
|
||||||
ui.separator();
|
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.label("Global Settings:");
|
ui.label("Global Settings:");
|
||||||
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.label("Color Theme:");
|
|
||||||
ui.color_edit_button_srgba(&mut self.knob_color);
|
|
||||||
ui.label("Knob");
|
|
||||||
ui.color_edit_button_srgba(&mut self.line_color);
|
|
||||||
ui.label("Indicator");
|
|
||||||
ui.color_edit_button_srgba(&mut self.text_color);
|
|
||||||
ui.label("Text");
|
|
||||||
});
|
|
||||||
|
|
||||||
ui.separator();
|
|
||||||
|
|
||||||
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.02));
|
|
||||||
|
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
ui.label("Color Theme:");
|
||||||
|
ui.color_edit_button_srgba(&mut self.knob_color);
|
||||||
|
ui.label("Knob");
|
||||||
|
ui.color_edit_button_srgba(&mut self.line_color);
|
||||||
|
ui.label("Indicator");
|
||||||
|
ui.color_edit_button_srgba(&mut self.text_color);
|
||||||
|
ui.label("Text");
|
||||||
|
});
|
||||||
|
|
||||||
|
ui.separator();
|
||||||
|
|
||||||
|
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| {
|
||||||
|
let mut knob = Knob::new(&mut self.values[i], 0., 1., *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.02))
|
||||||
|
.with_double_click_reset(0.5)
|
||||||
|
.with_middle_scroll();
|
||||||
|
|
||||||
|
if self.logarithmic_scaling {
|
||||||
|
knob = knob.with_logarithmic_scaling();
|
||||||
|
}
|
||||||
|
|
||||||
|
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: 50 KiB After Width: | Height: | Size: 112 KiB |
@@ -16,6 +16,9 @@ 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>,
|
||||||
|
pub(crate) allow_scroll: bool,
|
||||||
|
pub(crate) logarithmic_scaling: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KnobConfig {
|
impl KnobConfig {
|
||||||
@@ -36,6 +39,9 @@ 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,
|
||||||
|
allow_scroll:false,
|
||||||
|
logarithmic_scaling: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,5 +3,7 @@ mod render;
|
|||||||
mod style;
|
mod style;
|
||||||
mod widget;
|
mod widget;
|
||||||
|
|
||||||
|
pub use egui;
|
||||||
|
|
||||||
pub use style::{KnobStyle, LabelPosition};
|
pub use style::{KnobStyle, LabelPosition};
|
||||||
pub use widget::Knob;
|
pub use widget::Knob;
|
||||||
|
|||||||
@@ -6,37 +6,45 @@ 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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_knob(&self, painter: &Painter, center: Pos2, radius: f32, hovered: bool) {
|
pub fn render_knob(&self, painter: &Painter, center: Pos2, radius: f32, hovered: bool) {
|
||||||
let knob_color = if hovered {
|
let knob_color = if hovered {
|
||||||
self.config.colors.knob_color.linear_multiply(1.2)
|
self.config.colors.knob_color.linear_multiply(1.15)
|
||||||
} else {
|
} else {
|
||||||
self.config.colors.knob_color
|
self.config.colors.knob_color
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: make an option
|
||||||
|
painter.circle_filled(
|
||||||
|
center,
|
||||||
|
radius - self.config.stroke_width / 2.0,
|
||||||
|
self.config.colors.knob_color.gamma_multiply(0.15),
|
||||||
|
);
|
||||||
|
|
||||||
painter.circle_stroke(
|
painter.circle_stroke(
|
||||||
center,
|
center,
|
||||||
radius,
|
radius,
|
||||||
@@ -50,11 +58,11 @@ impl<'a> KnobRenderer<'a> {
|
|||||||
let angle = self.compute_angle();
|
let angle = self.compute_angle();
|
||||||
match self.config.style {
|
match self.config.style {
|
||||||
KnobStyle::Wiper => {
|
KnobStyle::Wiper => {
|
||||||
let pointer = center + Vec2::angled(angle) * (radius * 0.7);
|
let pointer = center + Vec2::angled(angle) * (radius * 0.65);
|
||||||
painter.line_segment(
|
painter.line_segment(
|
||||||
[center, pointer],
|
[center, pointer],
|
||||||
Stroke::new(
|
Stroke::new(
|
||||||
self.config.stroke_width * 1.5,
|
self.config.stroke_width * 1.2,
|
||||||
self.config.colors.line_color,
|
self.config.colors.line_color,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -63,7 +71,7 @@ impl<'a> KnobRenderer<'a> {
|
|||||||
let dot_pos = center + Vec2::angled(angle) * (radius * 0.7);
|
let dot_pos = center + Vec2::angled(angle) * (radius * 0.7);
|
||||||
painter.circle_filled(
|
painter.circle_filled(
|
||||||
dot_pos,
|
dot_pos,
|
||||||
self.config.stroke_width * 1.5,
|
self.config.stroke_width * 1.8,
|
||||||
self.config.colors.line_color,
|
self.config.colors.line_color,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -73,9 +81,9 @@ impl<'a> KnobRenderer<'a> {
|
|||||||
fn render_background_arc(&self, painter: &Painter, center: Pos2, radius: f32) {
|
fn render_background_arc(&self, painter: &Painter, center: Pos2, radius: f32) {
|
||||||
let arc_start = self.config.min_angle;
|
let arc_start = self.config.min_angle;
|
||||||
let arc_end = self.config.max_angle;
|
let arc_end = self.config.max_angle;
|
||||||
let segments = 64;
|
let segments = 128;
|
||||||
let arc_color = self.config.colors.knob_color.gamma_multiply(0.5);
|
let arc_color = self.config.colors.knob_color.gamma_multiply(0.35);
|
||||||
let arc_radius = radius * 0.8;
|
let arc_radius = radius * 0.85;
|
||||||
|
|
||||||
let mut points = Vec::with_capacity(segments + 1);
|
let mut points = Vec::with_capacity(segments + 1);
|
||||||
for i in 0..=segments {
|
for i in 0..=segments {
|
||||||
@@ -92,21 +100,26 @@ 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;
|
||||||
|
|
||||||
let mut fill_points = Vec::with_capacity(filled_segments + 1);
|
if filled_segments > 0 {
|
||||||
for i in 0..=filled_segments {
|
let mut fill_points = Vec::with_capacity(filled_segments + 1);
|
||||||
let t = i as f32 / segments as f32;
|
for i in 0..=filled_segments {
|
||||||
let angle = arc_start + (arc_end - arc_start) * t;
|
let t = i as f32 / segments as f32;
|
||||||
let pos = center + Vec2::angled(angle) * arc_radius;
|
let angle = arc_start + (arc_end - arc_start) * t;
|
||||||
fill_points.push(pos);
|
let pos = center + Vec2::angled(angle) * arc_radius;
|
||||||
}
|
fill_points.push(pos);
|
||||||
|
}
|
||||||
|
|
||||||
painter.add(egui::Shape::line(
|
painter.add(egui::Shape::line(
|
||||||
fill_points,
|
fill_points,
|
||||||
Stroke::new(self.config.stroke_width, self.config.colors.line_color),
|
Stroke::new(
|
||||||
));
|
self.config.stroke_width * 1.2,
|
||||||
|
self.config.colors.line_color,
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,26 +127,23 @@ impl<'a> KnobRenderer<'a> {
|
|||||||
if let Some(label) = &self.config.label {
|
if let Some(label) = &self.config.label {
|
||||||
let label_text = format!("{}: {}", label, (self.config.label_format)(self.value));
|
let label_text = format!("{}: {}", label, (self.config.label_format)(self.value));
|
||||||
let font_id = egui::FontId::proportional(self.config.font_size);
|
let font_id = egui::FontId::proportional(self.config.font_size);
|
||||||
let label_padding = 2.0;
|
let label_padding = 4.0;
|
||||||
|
|
||||||
let (label_pos, alignment) = match self.config.label_position {
|
let (label_pos, alignment) = match self.config.label_position {
|
||||||
LabelPosition::Top => (
|
LabelPosition::Top => (
|
||||||
Vec2::new(
|
Vec2::new(rect.center().x, rect.min.y + label_padding),
|
||||||
rect.center().x,
|
|
||||||
rect.min.y - self.config.label_offset + label_padding,
|
|
||||||
),
|
|
||||||
Align2::CENTER_TOP,
|
Align2::CENTER_TOP,
|
||||||
),
|
),
|
||||||
LabelPosition::Bottom => (
|
LabelPosition::Bottom => (
|
||||||
Vec2::new(rect.center().x, rect.max.y + self.config.label_offset),
|
Vec2::new(rect.center().x, rect.max.y - label_padding),
|
||||||
Align2::CENTER_BOTTOM,
|
Align2::CENTER_BOTTOM,
|
||||||
),
|
),
|
||||||
LabelPosition::Left => (
|
LabelPosition::Left => (
|
||||||
Vec2::new(rect.min.x - self.config.label_offset, rect.center().y),
|
Vec2::new(rect.min.x + label_padding, rect.center().y),
|
||||||
Align2::LEFT_CENTER,
|
Align2::LEFT_CENTER,
|
||||||
),
|
),
|
||||||
LabelPosition::Right => (
|
LabelPosition::Right => (
|
||||||
Vec2::new(rect.max.x + self.config.label_offset, rect.center().y),
|
Vec2::new(rect.max.x - label_padding, rect.center().y),
|
||||||
Align2::RIGHT_CENTER,
|
Align2::RIGHT_CENTER,
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
@@ -149,7 +159,7 @@ impl<'a> KnobRenderer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn calculate_size(&self, ui: &Ui) -> Vec2 {
|
pub fn calculate_size(&self, ui: &Ui) -> Vec2 {
|
||||||
let knob_size = Vec2::splat(self.config.size);
|
let knob_size = Vec2::splat(self.config.size + self.config.stroke_width * 2.0);
|
||||||
|
|
||||||
let label_size = if let Some(label) = &self.config.label {
|
let label_size = if let Some(label) = &self.config.label {
|
||||||
let font_id = egui::FontId::proportional(self.config.font_size);
|
let font_id = egui::FontId::proportional(self.config.font_size);
|
||||||
@@ -161,16 +171,16 @@ impl<'a> KnobRenderer<'a> {
|
|||||||
Vec2::ZERO
|
Vec2::ZERO
|
||||||
};
|
};
|
||||||
|
|
||||||
let label_padding = 2.0;
|
let label_padding = 8.0;
|
||||||
|
|
||||||
match self.config.label_position {
|
match self.config.label_position {
|
||||||
LabelPosition::Top | LabelPosition::Bottom => Vec2::new(
|
LabelPosition::Top | LabelPosition::Bottom => Vec2::new(
|
||||||
knob_size.x.max(label_size.x + label_padding * 2.0),
|
knob_size.x.max(label_size.x + label_padding * 2.0),
|
||||||
knob_size.y + label_size.y + label_padding * 2.0 + self.config.label_offset,
|
knob_size.y + label_size.y + label_padding + self.config.label_offset,
|
||||||
),
|
),
|
||||||
LabelPosition::Left | LabelPosition::Right => Vec2::new(
|
LabelPosition::Left | LabelPosition::Right => Vec2::new(
|
||||||
knob_size.x + label_size.x + label_padding * 2.0 + self.config.label_offset,
|
knob_size.x + label_size.x + label_padding + self.config.label_offset,
|
||||||
knob_size.y.max(label_size.y + label_padding * 2.0),
|
knob_size.y.max(label_size.y + label_padding),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -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,22 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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<'_> {
|
||||||
@@ -164,39 +180,62 @@ 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::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() {
|
||||||
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.hovered() & self.config.allow_scroll && 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()
|
||||||
|
&& 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);
|
||||||
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);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user