mirror of
https://github.com/obsqrbtz/egui_knob.git
synced 2026-04-08 20:19:17 +03:00
add readme and knob styles
This commit is contained in:
9
LICENSE
Normal file
9
LICENSE
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2025 Daniel Dada
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
2. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
40
README.md
Normal file
40
README.md
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# Knob Widget
|
||||||
|
|
||||||
|
Simple knob widget for egui.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Adjustable size, font size, and stroke width.
|
||||||
|
- Customizable colors for the knob and line.
|
||||||
|
- Label positions (Top, Bottom, Left, Right).
|
||||||
|
- Two styles: Wiper and Dot.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
To use the Knob widget in your project, add the following to your `Cargo.toml`:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[dependencies]
|
||||||
|
egui = "0.30"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage example
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use egui::{Color32, Context};
|
||||||
|
use egui_knob::Knob;
|
||||||
|
|
||||||
|
// ..
|
||||||
|
|
||||||
|
let mut value: f32 = 0.5;
|
||||||
|
let knob = Knob::new(&mut value, 0.0, 1.0, KnobStyle::Wiper)
|
||||||
|
.with_size(50.0)
|
||||||
|
.with_font_size(14.0)
|
||||||
|
.with_stroke_width(3.0)
|
||||||
|
.with_colors(Color32::GRAY, Color32::WHITE)
|
||||||
|
.with_label("Volume", LabelPosition::Top);
|
||||||
|
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
ui.add(knob);
|
||||||
|
});
|
||||||
|
```
|
||||||
@@ -25,25 +25,13 @@ impl eframe::App for KnobExample {
|
|||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.add_space(15.0);
|
ui.add_space(15.0);
|
||||||
ui.add(
|
ui.add(
|
||||||
Knob::new(&mut self.value, 0.0, 100.0)
|
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
|
||||||
.with_label("Gain", egui_knob::LabelPosition::Bottom)
|
.with_label("Gain", egui_knob::LabelPosition::Bottom)
|
||||||
.with_size(50.0),
|
.with_size(50.0),
|
||||||
);
|
);
|
||||||
ui.add_space(15.0);
|
ui.add_space(15.0);
|
||||||
ui.add(
|
ui.add(
|
||||||
Knob::new(&mut self.value, 0.0, 100.0)
|
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)
|
|
||||||
.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)
|
|
||||||
.with_label("Gain", egui_knob::LabelPosition::Bottom)
|
.with_label("Gain", egui_knob::LabelPosition::Bottom)
|
||||||
.with_size(50.0),
|
.with_size(50.0),
|
||||||
);
|
);
|
||||||
|
|||||||
30
src/lib.rs
30
src/lib.rs
@@ -7,6 +7,11 @@ pub enum LabelPosition {
|
|||||||
Right,
|
Right,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum KnobStyle {
|
||||||
|
Wiper,
|
||||||
|
Dot,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Knob<'a> {
|
pub struct Knob<'a> {
|
||||||
value: &'a mut f32,
|
value: &'a mut f32,
|
||||||
min: f32,
|
min: f32,
|
||||||
@@ -18,10 +23,11 @@ pub struct Knob<'a> {
|
|||||||
line_color: Color32,
|
line_color: Color32,
|
||||||
label: Option<String>,
|
label: Option<String>,
|
||||||
label_position: LabelPosition,
|
label_position: LabelPosition,
|
||||||
|
style: KnobStyle,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Knob<'a> {
|
impl<'a> Knob<'a> {
|
||||||
pub fn new(value: &'a mut f32, min: f32, max: f32) -> Self {
|
pub fn new(value: &'a mut f32, min: f32, max: f32, style: KnobStyle) -> Self {
|
||||||
Self {
|
Self {
|
||||||
value,
|
value,
|
||||||
min,
|
min,
|
||||||
@@ -33,6 +39,7 @@ impl<'a> Knob<'a> {
|
|||||||
line_color: Color32::GRAY,
|
line_color: Color32::GRAY,
|
||||||
label: None,
|
label: None,
|
||||||
label_position: LabelPosition::Bottom,
|
label_position: LabelPosition::Bottom,
|
||||||
|
style,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,23 +89,28 @@ impl Widget for Knob<'_> {
|
|||||||
let angle = (*self.value - self.min) / (self.max - self.min) * std::f32::consts::PI * 1.5
|
let angle = (*self.value - self.min) / (self.max - self.min) * std::f32::consts::PI * 1.5
|
||||||
- std::f32::consts::PI;
|
- std::f32::consts::PI;
|
||||||
|
|
||||||
// Draw knob circle
|
|
||||||
painter.circle_stroke(
|
painter.circle_stroke(
|
||||||
center,
|
center,
|
||||||
radius,
|
radius,
|
||||||
Stroke::new(self.stroke_width, self.knob_color),
|
Stroke::new(self.stroke_width, self.knob_color),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Draw pointer line
|
match self.style {
|
||||||
let pointer = center + Vec2::angled(angle) * (radius * 0.7);
|
KnobStyle::Wiper => {
|
||||||
painter.line_segment(
|
let pointer = center + Vec2::angled(angle) * (radius * 0.7);
|
||||||
[center, pointer],
|
painter.line_segment(
|
||||||
Stroke::new(self.stroke_width * 1.5, self.line_color),
|
[center, pointer],
|
||||||
);
|
Stroke::new(self.stroke_width * 1.5, self.line_color),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
KnobStyle::Dot => {
|
||||||
|
let dot_pos = center + Vec2::angled(angle) * (radius * 0.7);
|
||||||
|
painter.circle_filled(dot_pos, self.stroke_width * 1.5, self.line_color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(label) = self.label {
|
if let Some(label) = self.label {
|
||||||
let label_text = format!("{label}: {:.2}", self.value);
|
let label_text = format!("{label}: {:.2}", self.value);
|
||||||
|
|
||||||
let font_id = egui::FontId::proportional(self.font_size);
|
let font_id = egui::FontId::proportional(self.font_size);
|
||||||
let text_size = ui
|
let text_size = ui
|
||||||
.painter()
|
.painter()
|
||||||
|
|||||||
Reference in New Issue
Block a user