mirror of
https://github.com/obsqrbtz/egui_knob.git
synced 2026-04-08 20:19:17 +03:00
fix: knob rect
This commit is contained in:
3467
Cargo.lock
generated
3467
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -14,5 +14,12 @@ categories = ["gui"]
|
||||
name = "egui_knob"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[example]]
|
||||
name = "example_knob"
|
||||
path = "examples/example_knob.rs"
|
||||
|
||||
[dependencies]
|
||||
egui = "0.30"
|
||||
|
||||
[dev-dependencies]
|
||||
eframe = { version = "0.30.0", features = ["default_fonts"] }
|
||||
3781
examples/Cargo.lock
generated
3781
examples/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,13 +0,0 @@
|
||||
[package]
|
||||
name = "egui_knob_demo"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
|
||||
[[bin]]
|
||||
name = "egui_knob_demo"
|
||||
path = "demo.rs"
|
||||
|
||||
[dependencies]
|
||||
egui = "0.30"
|
||||
egui_knob = "0.1.1"
|
||||
eframe = { version = "0.30.0", features = ["default_fonts"] }
|
||||
@@ -23,13 +23,11 @@ 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| {
|
||||
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_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)
|
||||
19
src/lib.rs
19
src/lib.rs
@@ -82,7 +82,24 @@ impl<'a> Knob<'a> {
|
||||
impl Widget for Knob<'_> {
|
||||
fn ui(self, ui: &mut Ui) -> Response {
|
||||
let desired_size = Vec2::splat(self.size);
|
||||
let (rect, response) = ui.allocate_exact_size(desired_size, Sense::drag());
|
||||
|
||||
let label_size = if let Some(label) = &self.label {
|
||||
let font_id = egui::FontId::proportional(self.font_size);
|
||||
ui.painter()
|
||||
.layout(
|
||||
format!("{}: {:.2}", label, self.value),
|
||||
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());
|
||||
|
||||
if response.dragged() {
|
||||
let delta = response.drag_delta().y;
|
||||
|
||||
Reference in New Issue
Block a user