23 Commits

Author SHA1 Message Date
135d70cfc8 Merge pull request #2 from sacha-renault/feat/configurable-sweep-range
Allow customizable sweep range for knob widgets
2025-07-07 16:34:48 +03:00
sacha-renault
c105722eb4 fix: Address PR feedback
Prevent division by zero when min equals max value

Also improves angle calculations using TAU constant and adds NaN validation.
Also fix doc
2025-07-06 09:36:29 +02:00
sacha-renault
2a68f53a37 docs: update changelog for sweep range feature 2025-07-05 21:52:05 +02:00
sacha-renault
9968afbe48 feat: Add with_sweep_range() method for
control over knob start position and rotation range. Maintains backward compatibility
2025-07-05 21:45:57 +02:00
06bca1c13a Merge pull request #1 from chloebrett/master
chore: bump deps, fix typos.
2025-05-12 08:42:37 +03:00
Chloe Brett
0a2915f31b Bump egui and eframe versions. Therefore bump minor version (as this is a breaking change to the same extent as the egui bump). 2025-04-06 21:48:53 +10:00
39692f5011 chore: Release egui_knob version 0.1.9 2025-02-04 10:04:18 +03:00
e79c4af600 added changelog 2025-02-04 10:03:50 +03:00
7f24f4a468 docs: added doc comments 2025-02-04 09:49:12 +03:00
64d072a1af feat: add step size configuration 2025-02-04 09:23:53 +03:00
19b0ff5405 feat: exposed label format 2025-02-04 09:17:23 +03:00
b141db8cd4 fix: add label offsets and position in relative to the widget rect 2025-02-04 09:09:20 +03:00
8a97e1ecd1 chore: Release egui_knob version 0.1.8 2025-02-03 23:09:19 +03:00
b407b09853 chore: Release egui_knob version 0.1.7 2025-02-03 23:02:32 +03:00
9031d1c920 fix: mark reponse changed when knob was dragged 2025-02-03 22:47:48 +03:00
6aa6a231fa chore: Release egui_knob version 0.1.6 2025-02-03 16:25:52 +03:00
26ab08e7d5 do not display bounds 2025-02-03 16:25:41 +03:00
421dd3ff60 chore: Release egui_knob version 0.1.5 2025-02-03 16:23:41 +03:00
268db4255e another sizing fix 2025-02-03 16:23:23 +03:00
76670f416f chore: Release egui_knob version 0.1.4 2025-02-03 15:55:43 +03:00
0aec31e81d set MIT license in cargo.toml 2025-02-03 15:53:54 +03:00
81bc2295fb set MIT license in cargo.toml 2025-02-03 15:53:37 +03:00
6a49e6f961 fix: knob size 2025-02-03 15:50:55 +03:00
7 changed files with 521 additions and 144 deletions

26
.vscode/launch.json vendored Normal file
View 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}"
},
]
}

32
CHANGELOG.md Normal file
View File

@@ -0,0 +1,32 @@
# Changelog
All notable changes to this project will be documented in this file.
## [Unreleased]
### 🚀 Features
- Add configurable sweep range with `with_sweep_range()` method
## [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 -->

228
Cargo.lock generated
View File

@@ -34,7 +34,7 @@ dependencies = [
"accesskit_consumer",
"atspi-common",
"serde",
"thiserror",
"thiserror 1.0.69",
"zvariant",
]
@@ -147,7 +147,7 @@ dependencies = [
"ndk-context",
"ndk-sys 0.6.0+11769913",
"num_enum",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -172,11 +172,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df099ccb16cd014ff054ac1bf392c67feeef57164b05c42f037cd40f5d4357f4"
dependencies = [
"clipboard-win",
"core-graphics",
"image",
"log",
"objc2",
"objc2-app-kit",
"objc2-foundation",
"parking_lot",
"windows-sys 0.48.0",
"x11rb",
]
@@ -439,6 +442,9 @@ name = "bitflags"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
dependencies = [
"serde",
]
[[package]]
name = "block"
@@ -532,7 +538,7 @@ dependencies = [
"polling",
"rustix",
"slab",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -570,12 +576,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
@@ -778,9 +778,9 @@ checksum = "f25c0e292a7ca6d6498557ff1df68f32c99850012b6ea401cf8daf771f22ff53"
[[package]]
name = "ecolor"
version = "0.30.0"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d72e9c39f6e11a2e922d04a34ec5e7ef522ea3f5a1acfca7a19d16ad5fe50f5"
checksum = "bc4feb366740ded31a004a0e4452fbf84e80ef432ecf8314c485210229672fd1"
dependencies = [
"bytemuck",
"emath",
@@ -788,9 +788,9 @@ dependencies = [
[[package]]
name = "eframe"
version = "0.30.0"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2f2d9e7ea2d11ec9e98a8683b6eb99f9d7d0448394ef6e0d6d91bd4eb817220"
checksum = "d0dfe0859f3fb1bc6424c57d41e10e9093fe938f426b691e42272c2f336d915c"
dependencies = [
"ahash",
"bytemuck",
@@ -799,7 +799,7 @@ dependencies = [
"egui-wgpu",
"egui-winit",
"egui_glow",
"glow 0.16.0",
"glow",
"glutin",
"glutin-winit",
"image",
@@ -824,12 +824,13 @@ dependencies = [
[[package]]
name = "egui"
version = "0.30.0"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "252d52224d35be1535d7fd1d6139ce071fb42c9097773e79f7665604f5596b5e"
checksum = "25dd34cec49ab55d85ebf70139cb1ccd29c977ef6b6ba4fe85489d6877ee9ef3"
dependencies = [
"accesskit",
"ahash",
"bitflags 2.8.0",
"emath",
"epaint",
"log",
@@ -839,9 +840,9 @@ dependencies = [
[[package]]
name = "egui-wgpu"
version = "0.30.0"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26c1e821d2d8921ef6ce98b258c7e24d9d6aab2ca1f9cdf374eca997e7f67f59"
checksum = "d319dfef570f699b6e9114e235e862a2ddcf75f0d1a061de9e1328d92146d820"
dependencies = [
"ahash",
"bytemuck",
@@ -850,7 +851,7 @@ dependencies = [
"epaint",
"log",
"profiling",
"thiserror",
"thiserror 1.0.69",
"type-map",
"web-time",
"wgpu",
@@ -859,13 +860,14 @@ dependencies = [
[[package]]
name = "egui-winit"
version = "0.30.0"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e84c2919cd9f3a38a91e8f84ac6a245c19251fd95226ed9fae61d5ea564fce3"
checksum = "7d9dfbb78fe4eb9c3a39ad528b90ee5915c252e77bbab9d4ebc576541ab67e13"
dependencies = [
"accesskit_winit",
"ahash",
"arboard",
"bytemuck",
"egui",
"log",
"profiling",
@@ -878,14 +880,14 @@ dependencies = [
[[package]]
name = "egui_glow"
version = "0.30.0"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf6264cc7608e3e69a7d57a6175f438275f1b3889c1a551b418277721c95e6"
checksum = "910906e3f042ea6d2378ec12a6fd07698e14ddae68aed2d819ffe944a73aab9e"
dependencies = [
"ahash",
"bytemuck",
"egui",
"glow 0.16.0",
"glow",
"log",
"memoffset",
"profiling",
@@ -896,7 +898,7 @@ dependencies = [
[[package]]
name = "egui_knob"
version = "0.1.3"
version = "0.2.0"
dependencies = [
"eframe",
"egui",
@@ -904,9 +906,9 @@ dependencies = [
[[package]]
name = "emath"
version = "0.30.0"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4fe73c1207b864ee40aa0b0c038d6092af1030744678c60188a05c28553515d"
checksum = "9e4cadcff7a5353ba72b7fea76bf2122b5ebdbc68e8155aa56dfdea90083fe1b"
dependencies = [
"bytemuck",
]
@@ -940,9 +942,9 @@ dependencies = [
[[package]]
name = "epaint"
version = "0.30.0"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5666f8d25236293c966fbb3635eac18b04ad1914e3bab55bc7d44b9980cafcac"
checksum = "41fcc0f5a7c613afd2dee5e4b30c3e6acafb8ad6f0edb06068811f708a67c562"
dependencies = [
"ab_glyph",
"ahash",
@@ -958,9 +960,9 @@ dependencies = [
[[package]]
name = "epaint_default_fonts"
version = "0.30.0"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66f6ddac3e6ac6fd4c3d48bb8b1943472f8da0f43a4303bcd8a18aa594401c80"
checksum = "fc7e7a64c02cf7a5b51e745a9e45f60660a286f151c238b9d397b3e923f5082f"
[[package]]
name = "equivalent"
@@ -1191,18 +1193,6 @@ dependencies = [
"xml-rs",
]
[[package]]
name = "glow"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51fa363f025f5c111e03f13eda21162faeacb6911fe8caa0c0349f9cf0c4483"
dependencies = [
"js-sys",
"slotmap",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "glow"
version = "0.16.0"
@@ -1222,7 +1212,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03642b8b0cce622392deb0ee3e88511f75df2daac806102597905c3ea1974848"
dependencies = [
"bitflags 2.8.0",
"cfg_aliases 0.2.1",
"cfg_aliases",
"cgl",
"core-foundation 0.9.4",
"dispatch",
@@ -1246,7 +1236,7 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85edca7075f8fc728f28cb8fbb111a96c3b89e930574369e3e9c27eb75d3788f"
dependencies = [
"cfg_aliases 0.2.1",
"cfg_aliases",
"glutin",
"raw-window-handle",
"winit",
@@ -1329,6 +1319,12 @@ dependencies = [
"foldhash",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.4.0"
@@ -1505,6 +1501,7 @@ dependencies = [
"byteorder-lite",
"num-traits",
"png",
"tiff",
]
[[package]]
@@ -1537,7 +1534,7 @@ dependencies = [
"combine",
"jni-sys",
"log",
"thiserror",
"thiserror 1.0.69",
"walkdir",
"windows-sys 0.45.0",
]
@@ -1557,6 +1554,12 @@ dependencies = [
"libc",
]
[[package]]
name = "jpeg-decoder"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0"
[[package]]
name = "js-sys"
version = "0.3.77"
@@ -1680,9 +1683,9 @@ dependencies = [
[[package]]
name = "metal"
version = "0.29.0"
version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ecfd3296f8c56b7c1f6fbac3c71cefa9d78ce009850c45000015f206dc7fa21"
checksum = "f569fb946490b5743ad69813cb19629130ce9374034abe31614a36402d18f99e"
dependencies = [
"bitflags 2.8.0",
"block",
@@ -1705,22 +1708,23 @@ dependencies = [
[[package]]
name = "naga"
version = "23.1.0"
version = "24.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "364f94bc34f61332abebe8cad6f6cd82a5b65cff22c828d05d0968911462ca4f"
checksum = "e380993072e52eef724eddfcde0ed013b0c023c3f0417336ed041aa9f076994e"
dependencies = [
"arrayvec",
"bit-set",
"bitflags 2.8.0",
"cfg_aliases 0.1.1",
"cfg_aliases",
"codespan-reporting",
"hexf-parse",
"indexmap",
"log",
"rustc-hash",
"spirv",
"strum",
"termcolor",
"thiserror",
"thiserror 2.0.12",
"unicode-xid",
]
@@ -1736,7 +1740,7 @@ dependencies = [
"ndk-sys 0.6.0+11769913",
"num_enum",
"raw-window-handle",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -1771,7 +1775,7 @@ checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags 2.8.0",
"cfg-if",
"cfg_aliases 0.2.1",
"cfg_aliases",
"libc",
"memoffset",
]
@@ -2039,6 +2043,15 @@ dependencies = [
"libredox",
]
[[package]]
name = "ordered-float"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bb71e1b3fa6ca1c61f383464aaf2bb0e2f8e772a1f01d486832464de363b951"
dependencies = [
"num-traits",
]
[[package]]
name = "ordered-stream"
version = "0.2.0"
@@ -2457,7 +2470,7 @@ dependencies = [
"log",
"memmap2",
"rustix",
"thiserror",
"thiserror 1.0.69",
"wayland-backend",
"wayland-client",
"wayland-csd-frame",
@@ -2515,6 +2528,28 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
[[package]]
name = "strum"
version = "0.26.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn",
]
[[package]]
name = "syn"
version = "2.0.96"
@@ -2566,7 +2601,16 @@ version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [
"thiserror-impl",
"thiserror-impl 1.0.69",
]
[[package]]
name = "thiserror"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [
"thiserror-impl 2.0.12",
]
[[package]]
@@ -2580,6 +2624,28 @@ dependencies = [
"syn",
]
[[package]]
name = "thiserror-impl"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tiff"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba1310fcea54c6a9a4fd1aad794ecc02c31682f6bfbecdf460bf19533eed1e3e"
dependencies = [
"flate2",
"jpeg-decoder",
"weezl",
]
[[package]]
name = "tiny-skia"
version = "0.11.4"
@@ -2992,13 +3058,20 @@ dependencies = [
]
[[package]]
name = "wgpu"
version = "23.0.1"
name = "weezl"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80f70000db37c469ea9d67defdc13024ddf9a5f1b89cb2941b812ad7cde1735a"
checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082"
[[package]]
name = "wgpu"
version = "24.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35904fb00ba2d2e0a4d002fcbbb6e1b89b574d272a50e5fc95f6e81cf281c245"
dependencies = [
"arrayvec",
"cfg_aliases 0.1.1",
"bitflags 2.8.0",
"cfg_aliases",
"document-features",
"js-sys",
"log",
@@ -3017,14 +3090,14 @@ dependencies = [
[[package]]
name = "wgpu-core"
version = "23.0.1"
version = "24.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d63c3c478de8e7e01786479919c8769f62a22eec16788d8c2ac77ce2c132778a"
checksum = "671c25545d479b47d3f0a8e373aceb2060b67c6eb841b24ac8c32348151c7a0c"
dependencies = [
"arrayvec",
"bit-vec",
"bitflags 2.8.0",
"cfg_aliases 0.1.1",
"cfg_aliases",
"document-features",
"indexmap",
"log",
@@ -3035,25 +3108,25 @@ dependencies = [
"raw-window-handle",
"rustc-hash",
"smallvec",
"thiserror",
"thiserror 2.0.12",
"wgpu-hal",
"wgpu-types",
]
[[package]]
name = "wgpu-hal"
version = "23.0.1"
version = "24.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89364b8a0b211adc7b16aeaf1bd5ad4a919c1154b44c9ce27838213ba05fd821"
checksum = "f112f464674ca69f3533248508ee30cb84c67cf06c25ff6800685f5e0294e259"
dependencies = [
"android_system_properties",
"arrayvec",
"ash",
"bitflags 2.8.0",
"bytemuck",
"cfg_aliases 0.1.1",
"cfg_aliases",
"core-graphics-types",
"glow 0.14.2",
"glow",
"glutin_wgl_sys",
"gpu-alloc",
"gpu-descriptor",
@@ -3067,13 +3140,14 @@ dependencies = [
"ndk-sys 0.5.0+25.2.9519653",
"objc",
"once_cell",
"ordered-float",
"parking_lot",
"profiling",
"raw-window-handle",
"renderdoc-sys",
"rustc-hash",
"smallvec",
"thiserror",
"thiserror 2.0.12",
"wasm-bindgen",
"web-sys",
"wgpu-types",
@@ -3082,12 +3156,13 @@ dependencies = [
[[package]]
name = "wgpu-types"
version = "23.0.0"
version = "24.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "610f6ff27778148c31093f3b03abc4840f9636d58d597ca2f5977433acfe0068"
checksum = "50ac044c0e76c03a0378e7786ac505d010a873665e2d51383dcff8dd227dc69c"
dependencies = [
"bitflags 2.8.0",
"js-sys",
"log",
"web-sys",
]
@@ -3195,6 +3270,15 @@ dependencies = [
"windows-targets 0.42.2",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
@@ -3404,7 +3488,7 @@ dependencies = [
"block2",
"bytemuck",
"calloop",
"cfg_aliases 0.2.1",
"cfg_aliases",
"concurrent-queue",
"core-foundation 0.9.4",
"core-graphics",

View File

@@ -1,12 +1,12 @@
[package]
name = "egui_knob"
version = "0.1.3"
version = "0.2.0"
edition = "2021"
description = "A simple knob widget for egui"
homepage = "https://github.com/obsqrbtz/egui_knob"
repository = "https://github.com/obsqrbtz/egui_knob"
readme = "README.md"
license-file = "LICENSE"
license = "MIT"
keywords = ["egui", "ui", "widget", "knob", "range"]
categories = ["gui"]
@@ -19,7 +19,7 @@ name = "example_knob"
path = "examples/example_knob.rs"
[dependencies]
egui = "0.30"
egui = "0.31"
[dev-dependencies]
eframe = { version = "0.30.0", features = ["default_fonts"] }
eframe = { version = "0.31.0", features = ["default_fonts"] }

View File

@@ -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 = "*"
egui = "0.31"
egui_knob = "0.2.0"
```
## Usage example
@@ -42,4 +43,4 @@ let knob = Knob::new(&mut value, 0.0, 1.0, KnobStyle::Wiper)
egui::CentralPanel::default().show(ctx, |ui| {
ui.add(knob);
});
```
```

View File

@@ -10,12 +10,26 @@ fn main() -> eframe::Result<()> {
}
struct KnobExample {
value: f32,
basic_value: f32,
purple_value: f32,
large_value: f32,
thick_value: f32,
red_value: f32,
green_value: f32,
blue_value: f32,
}
impl Default for KnobExample {
fn default() -> Self {
Self { value: 0.0 }
Self {
basic_value: 0.0,
purple_value: 0.0,
large_value: 0.0,
thick_value: 0.0,
red_value: 0.0,
green_value: 0.0,
blue_value: 0.0,
}
}
}
@@ -23,37 +37,100 @@ impl eframe::App for KnobExample {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| {
if ui
.add(
Knob::new(&mut self.basic_value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
.with_label("Basic", egui_knob::LabelPosition::Right)
.with_size(40.0)
.with_font_size(10.0)
.with_colors(
egui::Color32::from_rgb(60, 60, 60),
egui::Color32::from_rgb(150, 150, 150),
egui::Color32::from_rgb(200, 200, 200),
),
)
.changed()
{
println!("Basic value changed: {}", self.basic_value);
}
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),
Knob::new(
&mut self.purple_value,
0.0,
100.0,
egui_knob::KnobStyle::Wiper,
)
.with_label("Purple", egui_knob::LabelPosition::Bottom)
.with_colors(
egui::Color32::from_rgb(60, 30, 80),
egui::Color32::from_rgb(200, 100, 255),
egui::Color32::from_rgb(230, 150, 255),
)
.with_size(50.0)
.with_font_size(14.0)
.with_stroke_width(3.0)
.with_step(0.1),
);
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),
Knob::new(&mut self.large_value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
.with_label("Large", egui_knob::LabelPosition::Bottom)
.with_size(60.0)
.with_font_size(16.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)
Knob::new(
&mut self.thick_value,
0.0,
100.0,
egui_knob::KnobStyle::Wiper,
)
.with_label("Thick", egui_knob::LabelPosition::Bottom)
.with_size(50.0)
.with_stroke_width(4.0),
);
ui.add(
Knob::new(&mut self.red_value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
.with_label("Red", egui_knob::LabelPosition::Bottom)
.with_colors(
egui::Color32::DARK_GRAY,
egui::Color32::WHITE,
egui::Color32::WHITE,
egui::Color32::from_rgb(80, 30, 30),
egui::Color32::from_rgb(220, 50, 50),
egui::Color32::from_rgb(255, 100, 100),
)
.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)
Knob::new(
&mut self.green_value,
0.0,
100.0,
egui_knob::KnobStyle::Wiper,
)
.with_label("Leftandlongtext", egui_knob::LabelPosition::Left)
.with_colors(
egui::Color32::from_rgb(30, 80, 30),
egui::Color32::from_rgb(50, 220, 50),
egui::Color32::from_rgb(100, 255, 100),
)
.with_size(50.0)
.with_label_format(|v| format!("{:.2}%", v))
.with_sweep_range(1. / 8., 0.75),
);
ui.add(
Knob::new(&mut self.blue_value, 0.0, 100., egui_knob::KnobStyle::Dot)
.with_label("Top", egui_knob::LabelPosition::Top)
.with_colors(
egui::Color32::DARK_GRAY,
egui::Color32::WHITE,
egui::Color32::LIGHT_BLUE,
egui::Color32::from_rgb(30, 30, 80),
egui::Color32::from_rgb(50, 50, 220),
egui::Color32::from_rgb(100, 100, 255),
)
.with_size(50.0),
.with_size(50.0)
.with_sweep_range(0., 2.),
);
});
});

View File

@@ -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,24 @@ pub enum LabelPosition {
Right,
}
/// Visual style of the knob indicator
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 +41,30 @@ 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>,
/// 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 +79,74 @@ 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,
}
}
/// 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
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,47 +159,117 @@ 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: f32) -> Self {
self.step = Some(step);
self
}
}
impl Widget for Knob<'_> {
fn ui(self, ui: &mut Ui) -> Response {
let desired_size = Vec2::splat(self.size);
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 * 0.005);
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
};
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,
),
};
let center = knob_rect.center();
let radius = knob_size.x / 2.0;
let angle = if self.min == self.max {
// 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(
center,
@@ -135,43 +292,43 @@ 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_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,
);
}
// Draw the bounding rect
//painter.rect_stroke(rect, 0.0, Stroke::new(1.0, Color32::RED));
response
}
}