From 413363370dbe50d83daa4c14f02327f90c175c96 Mon Sep 17 00:00:00 2001 From: Daniel Dada Date: Mon, 3 Feb 2025 12:31:20 +0300 Subject: [PATCH] add readme and knob styles --- LICENSE | 9 +++++++++ README.md | 40 ++++++++++++++++++++++++++++++++++++++++ examples/demo.rs | 16 ++-------------- src/lib.rs | 30 +++++++++++++++++++++--------- 4 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..950c0f7 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..bcd6f6f --- /dev/null +++ b/README.md @@ -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); +}); +``` \ No newline at end of file diff --git a/examples/demo.rs b/examples/demo.rs index e4ea424..1b93cf4 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -25,25 +25,13 @@ impl eframe::App for KnobExample { ui.horizontal(|ui| { ui.add_space(15.0); 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_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_size(50.0), - ); - ui.add_space(15.0); - 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), ); diff --git a/src/lib.rs b/src/lib.rs index 552264f..f77e5cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,11 @@ pub enum LabelPosition { Right, } +pub enum KnobStyle { + Wiper, + Dot, +} + pub struct Knob<'a> { value: &'a mut f32, min: f32, @@ -18,10 +23,11 @@ pub struct Knob<'a> { line_color: Color32, label: Option, label_position: LabelPosition, + style: KnobStyle, } 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 { value, min, @@ -33,6 +39,7 @@ impl<'a> Knob<'a> { line_color: Color32::GRAY, label: None, 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 - std::f32::consts::PI; - // Draw knob circle painter.circle_stroke( center, radius, Stroke::new(self.stroke_width, self.knob_color), ); - // Draw pointer line - let pointer = center + Vec2::angled(angle) * (radius * 0.7); - painter.line_segment( - [center, pointer], - Stroke::new(self.stroke_width * 1.5, self.line_color), - ); + match self.style { + KnobStyle::Wiper => { + let pointer = center + Vec2::angled(angle) * (radius * 0.7); + painter.line_segment( + [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 { let label_text = format!("{label}: {:.2}", self.value); - let font_id = egui::FontId::proportional(self.font_size); let text_size = ui .painter()