8 Commits

Author SHA1 Message Date
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
5 changed files with 47 additions and 14 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}"
},
]
}

2
Cargo.lock generated
View File

@@ -896,7 +896,7 @@ dependencies = [
[[package]]
name = "egui_knob"
version = "0.1.3"
version = "0.1.6"
dependencies = [
"eframe",
"egui",

View File

@@ -1,12 +1,12 @@
[package]
name = "egui_knob"
version = "0.1.3"
version = "0.1.6"
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"]

View File

@@ -33,7 +33,6 @@ impl eframe::App for KnobExample {
.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::Dot)
.with_label("Gain", egui_knob::LabelPosition::Bottom)
@@ -44,7 +43,6 @@ impl eframe::App for KnobExample {
)
.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)

View File

@@ -81,7 +81,7 @@ impl<'a> Knob<'a> {
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);
@@ -97,8 +97,13 @@ impl Widget for Knob<'_> {
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 label_padding = 2.0;
let adjusted_size = Vec2::new(
knob_size.x + label_size.y + label_padding * 6.0,
knob_size.y + label_size.y + label_padding * 6.0,
);
let (rect, response) = ui.allocate_exact_size(adjusted_size, Sense::drag());
if response.dragged() {
@@ -110,7 +115,7 @@ impl Widget for Knob<'_> {
let painter = ui.painter();
let center = rect.center();
let radius = rect.width() / 2.0;
let radius = knob_size.x / 2.0;
let angle = (*self.value - self.min) / (self.max - self.min) * std::f32::consts::PI * 1.5
- std::f32::consts::PI;
@@ -147,19 +152,20 @@ impl Widget for Knob<'_> {
)
.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)
rect.center()
+ Vec2::new(-text_size.x / 2.0, -radius - label_padding - text_size.y)
}
LabelPosition::Bottom => {
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 => {
center + Vec2::new(-radius - label_offset - text_size.x, -text_size.y / 2.0)
rect.center()
+ Vec2::new(-radius - label_padding - text_size.x, -text_size.y / 2.0)
}
LabelPosition::Right => {
center + Vec2::new(radius + label_offset, -text_size.y / 2.0)
rect.center() + Vec2::new(radius + label_padding, -text_size.y / 2.0)
}
};
@@ -172,6 +178,9 @@ impl Widget for Knob<'_> {
);
}
// Draw the bounding rect
//painter.rect_stroke(rect, 0.0, Stroke::new(1.0, Color32::RED));
response
}
}