mirror of
https://github.com/obsqrbtz/egui_knob.git
synced 2026-04-09 12:37:52 +03:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8a97e1ecd1 | |||
| b407b09853 | |||
| 9031d1c920 | |||
| 6aa6a231fa | |||
| 26ab08e7d5 | |||
| 421dd3ff60 | |||
| 268db4255e |
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}"
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -896,7 +896,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "egui_knob"
|
name = "egui_knob"
|
||||||
version = "0.1.4"
|
version = "0.1.8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"eframe",
|
"eframe",
|
||||||
"egui",
|
"egui",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "egui_knob"
|
name = "egui_knob"
|
||||||
version = "0.1.4"
|
version = "0.1.8"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
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"
|
||||||
|
|||||||
@@ -23,17 +23,19 @@ impl eframe::App for KnobExample {
|
|||||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.add(
|
if ui.add(
|
||||||
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
|
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),
|
||||||
);
|
).changed(){
|
||||||
|
println!("Value changed");
|
||||||
|
}
|
||||||
|
|
||||||
ui.add(
|
ui.add(
|
||||||
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Wiper)
|
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Wiper)
|
||||||
.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(
|
ui.add(
|
||||||
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
|
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)
|
||||||
@@ -44,7 +46,6 @@ impl eframe::App for KnobExample {
|
|||||||
)
|
)
|
||||||
.with_size(50.0),
|
.with_size(50.0),
|
||||||
);
|
);
|
||||||
ui.add_space(15.0);
|
|
||||||
ui.add(
|
ui.add(
|
||||||
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Wiper)
|
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Wiper)
|
||||||
.with_label("Gain", egui_knob::LabelPosition::Bottom)
|
.with_label("Gain", egui_knob::LabelPosition::Bottom)
|
||||||
|
|||||||
29
src/lib.rs
29
src/lib.rs
@@ -97,24 +97,21 @@ impl Widget for Knob<'_> {
|
|||||||
Vec2::ZERO
|
Vec2::ZERO
|
||||||
};
|
};
|
||||||
|
|
||||||
let label_padding = 12.0;
|
let label_padding = 2.0;
|
||||||
|
|
||||||
let adjusted_size = match self.label_position {
|
let adjusted_size = Vec2::new(
|
||||||
LabelPosition::Top | LabelPosition::Bottom => {
|
knob_size.x + label_size.y + label_padding * 6.0,
|
||||||
Vec2::new(knob_size.x, knob_size.y + label_size.y + label_padding)
|
knob_size.y + label_size.y + label_padding * 6.0,
|
||||||
}
|
);
|
||||||
LabelPosition::Left | LabelPosition::Right => {
|
|
||||||
Vec2::new(knob_size.x + label_size.x + label_padding, knob_size.y)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let (rect, response) = ui.allocate_exact_size(adjusted_size, Sense::drag());
|
let (rect, mut response) = ui.allocate_exact_size(adjusted_size, Sense::drag());
|
||||||
|
|
||||||
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 range = self.max - self.min;
|
||||||
let step = range * 0.005;
|
let step = range * 0.005;
|
||||||
*self.value = (*self.value - delta * step).clamp(self.min, self.max);
|
*self.value = (*self.value - delta * step).clamp(self.min, self.max);
|
||||||
|
response.mark_changed();
|
||||||
}
|
}
|
||||||
|
|
||||||
let painter = ui.painter();
|
let painter = ui.painter();
|
||||||
@@ -156,21 +153,20 @@ impl Widget for Knob<'_> {
|
|||||||
)
|
)
|
||||||
.size();
|
.size();
|
||||||
|
|
||||||
let label_offset = label_padding;
|
|
||||||
let label_pos = match self.label_position {
|
let label_pos = match self.label_position {
|
||||||
LabelPosition::Top => {
|
LabelPosition::Top => {
|
||||||
rect.center()
|
rect.center()
|
||||||
+ Vec2::new(-text_size.x / 2.0, -radius - label_offset - text_size.y)
|
+ Vec2::new(-text_size.x / 2.0, -radius - label_padding - text_size.y)
|
||||||
}
|
}
|
||||||
LabelPosition::Bottom => {
|
LabelPosition::Bottom => {
|
||||||
rect.center() + Vec2::new(-text_size.x / 2.0, radius + label_offset)
|
rect.center() + Vec2::new(-text_size.x / 2.0, radius + label_padding)
|
||||||
}
|
}
|
||||||
LabelPosition::Left => {
|
LabelPosition::Left => {
|
||||||
rect.center()
|
rect.center()
|
||||||
+ Vec2::new(-radius - label_offset - text_size.x, -text_size.y / 2.0)
|
+ Vec2::new(-radius - label_padding - text_size.x, -text_size.y / 2.0)
|
||||||
}
|
}
|
||||||
LabelPosition::Right => {
|
LabelPosition::Right => {
|
||||||
rect.center() + Vec2::new(radius + label_offset, -text_size.y / 2.0)
|
rect.center() + Vec2::new(radius + label_padding, -text_size.y / 2.0)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -183,6 +179,9 @@ impl Widget for Knob<'_> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw the bounding rect
|
||||||
|
//painter.rect_stroke(rect, 0.0, Stroke::new(1.0, Color32::RED));
|
||||||
|
|
||||||
response
|
response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user