mirror of
https://github.com/obsqrbtz/egui_knob.git
synced 2026-04-09 12:37:52 +03:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c1db9a2e5b | |||
| 750d8afb25 | |||
| 4e8975c955 | |||
|
|
824b492b92 | ||
| a70b57e62b | |||
| a2a102cdb6 |
27
.github/workflows/publish.yml
vendored
Normal file
27
.github/workflows/publish.yml
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
name: Publish to crates.io
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*'
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Install Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
|
||||
- name: Check if version matches tag
|
||||
run: |
|
||||
TAG=${GITHUB_REF#refs/tags/v}
|
||||
CARGO_VERSION=$(grep '^version' Cargo.toml | head -1 | cut -d'"' -f2)
|
||||
if [ "$TAG" != "$CARGO_VERSION" ]; then
|
||||
echo "Tag version ($TAG) doesn't match Cargo.toml version ($CARGO_VERSION)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Publish to crates.io
|
||||
run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
|
||||
18
CHANGELOG.md
18
CHANGELOG.md
@@ -2,6 +2,24 @@
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## [0.3.8] - 2026-01-12
|
||||
|
||||
### 🚀 Features
|
||||
|
||||
- added `with_double_click_reset` option ([#PR6](https://github.com/obsqrbtz/egui_knob/pull/6) by [maor1993](https://github.com/maor1993))
|
||||
|
||||
## [0.3.7] - 2026-01-06
|
||||
|
||||
### 🛠 Maintenance
|
||||
|
||||
- expose egui types
|
||||
|
||||
## [0.3.6] - 2025-07-15
|
||||
|
||||
### 🛠 Maintenance
|
||||
|
||||
- cleaned up io a bit
|
||||
|
||||
## [0.3.5] - 2025-07-15
|
||||
|
||||
### 🛠 Maintenance
|
||||
|
||||
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -869,7 +869,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "egui_knob"
|
||||
version = "0.3.5"
|
||||
version = "0.3.8"
|
||||
dependencies = [
|
||||
"eframe",
|
||||
"egui",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "egui_knob"
|
||||
version = "0.3.5"
|
||||
version = "0.3.8"
|
||||
edition = "2024"
|
||||
authors = ["Daniel Dada"]
|
||||
description = "A simple knob widget for egui"
|
||||
|
||||
@@ -27,7 +27,7 @@ To use the Knob widget in your project, add the following to your `Cargo.toml`:
|
||||
[dependencies]
|
||||
egui = "0.33"
|
||||
eframe = "0.33"
|
||||
egui_knob = "0.3.5"
|
||||
egui_knob = "0.3.8"
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -4,13 +4,13 @@ use egui_knob::{Knob, KnobStyle, LabelPosition};
|
||||
fn main() -> eframe::Result<()> {
|
||||
let options = eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default()
|
||||
.with_inner_size([800.0, 600.0])
|
||||
.with_title("egui_knob demo"),
|
||||
.with_inner_size([600.0, 350.0])
|
||||
.with_title("Knob demo"),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
eframe::run_native(
|
||||
"egui_knob demo",
|
||||
"Knob demo",
|
||||
options,
|
||||
Box::new(|_cc| Ok(Box::new(KnobDemo::default()))),
|
||||
)
|
||||
@@ -43,7 +43,7 @@ impl Default for KnobDemo {
|
||||
impl eframe::App for KnobDemo {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("egui_knob demo");
|
||||
ui.heading("Knob demo");
|
||||
ui.separator();
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
@@ -82,13 +82,13 @@ impl eframe::App for KnobDemo {
|
||||
.enumerate()
|
||||
{
|
||||
ui.vertical(|ui| {
|
||||
ui.label(*label);
|
||||
let mut knob = Knob::new(&mut self.values[i], 0.0, 1.0, *config)
|
||||
.with_label(*label, LabelPosition::Bottom)
|
||||
.with_background_arc(self.show_bg_arc)
|
||||
.with_show_filled_segments(self.show_filled)
|
||||
.with_colors(self.knob_color, self.line_color, self.text_color)
|
||||
.with_step(self.use_step.then_some(0.02));
|
||||
.with_step(self.use_step.then_some(0.02))
|
||||
.with_double_click_reset(0.5);
|
||||
|
||||
if *label == "Wiper, Sweep" {
|
||||
knob = knob.with_sweep_range(0.25, 0.75).with_size(50.0);
|
||||
|
||||
BIN
scrot.png
BIN
scrot.png
Binary file not shown.
|
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 42 KiB |
@@ -16,6 +16,7 @@ pub struct KnobConfig {
|
||||
pub(crate) show_filled_segments: bool,
|
||||
pub(crate) min_angle: f32,
|
||||
pub(crate) max_angle: f32,
|
||||
pub(crate) reset_value: Option<f32>,
|
||||
}
|
||||
|
||||
impl KnobConfig {
|
||||
@@ -36,6 +37,7 @@ impl KnobConfig {
|
||||
drag_sensitivity: 0.005,
|
||||
show_background_arc: true,
|
||||
show_filled_segments: true,
|
||||
reset_value: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,5 +3,7 @@ mod render;
|
||||
mod style;
|
||||
mod widget;
|
||||
|
||||
pub use egui;
|
||||
|
||||
pub use style::{KnobStyle, LabelPosition};
|
||||
pub use widget::Knob;
|
||||
|
||||
@@ -32,11 +32,18 @@ impl<'a> KnobRenderer<'a> {
|
||||
|
||||
pub fn render_knob(&self, painter: &Painter, center: Pos2, radius: f32, hovered: bool) {
|
||||
let knob_color = if hovered {
|
||||
self.config.colors.knob_color.linear_multiply(1.2)
|
||||
self.config.colors.knob_color.linear_multiply(1.15)
|
||||
} else {
|
||||
self.config.colors.knob_color
|
||||
};
|
||||
|
||||
// TODO: make an option
|
||||
painter.circle_filled(
|
||||
center,
|
||||
radius - self.config.stroke_width / 2.0,
|
||||
self.config.colors.knob_color.gamma_multiply(0.15),
|
||||
);
|
||||
|
||||
painter.circle_stroke(
|
||||
center,
|
||||
radius,
|
||||
@@ -50,11 +57,11 @@ impl<'a> KnobRenderer<'a> {
|
||||
let angle = self.compute_angle();
|
||||
match self.config.style {
|
||||
KnobStyle::Wiper => {
|
||||
let pointer = center + Vec2::angled(angle) * (radius * 0.7);
|
||||
let pointer = center + Vec2::angled(angle) * (radius * 0.65);
|
||||
painter.line_segment(
|
||||
[center, pointer],
|
||||
Stroke::new(
|
||||
self.config.stroke_width * 1.5,
|
||||
self.config.stroke_width * 1.2,
|
||||
self.config.colors.line_color,
|
||||
),
|
||||
);
|
||||
@@ -63,7 +70,7 @@ impl<'a> KnobRenderer<'a> {
|
||||
let dot_pos = center + Vec2::angled(angle) * (radius * 0.7);
|
||||
painter.circle_filled(
|
||||
dot_pos,
|
||||
self.config.stroke_width * 1.5,
|
||||
self.config.stroke_width * 1.8,
|
||||
self.config.colors.line_color,
|
||||
);
|
||||
}
|
||||
@@ -73,9 +80,9 @@ impl<'a> KnobRenderer<'a> {
|
||||
fn render_background_arc(&self, painter: &Painter, center: Pos2, radius: f32) {
|
||||
let arc_start = self.config.min_angle;
|
||||
let arc_end = self.config.max_angle;
|
||||
let segments = 64;
|
||||
let arc_color = self.config.colors.knob_color.gamma_multiply(0.5);
|
||||
let arc_radius = radius * 0.8;
|
||||
let segments = 128;
|
||||
let arc_color = self.config.colors.knob_color.gamma_multiply(0.35);
|
||||
let arc_radius = radius * 0.85;
|
||||
|
||||
let mut points = Vec::with_capacity(segments + 1);
|
||||
for i in 0..=segments {
|
||||
@@ -95,6 +102,7 @@ impl<'a> KnobRenderer<'a> {
|
||||
* ((self.value - self.min) / (self.max - self.min)).clamp(0.0, 1.0))
|
||||
as usize;
|
||||
|
||||
if filled_segments > 0 {
|
||||
let mut fill_points = Vec::with_capacity(filled_segments + 1);
|
||||
for i in 0..=filled_segments {
|
||||
let t = i as f32 / segments as f32;
|
||||
@@ -105,35 +113,36 @@ impl<'a> KnobRenderer<'a> {
|
||||
|
||||
painter.add(egui::Shape::line(
|
||||
fill_points,
|
||||
Stroke::new(self.config.stroke_width, self.config.colors.line_color),
|
||||
Stroke::new(
|
||||
self.config.stroke_width * 1.2,
|
||||
self.config.colors.line_color,
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_label(&self, ui: &Ui, rect: Rect) {
|
||||
if let Some(label) = &self.config.label {
|
||||
let label_text = format!("{}: {}", label, (self.config.label_format)(self.value));
|
||||
let font_id = egui::FontId::proportional(self.config.font_size);
|
||||
let label_padding = 2.0;
|
||||
let label_padding = 4.0;
|
||||
|
||||
let (label_pos, alignment) = match self.config.label_position {
|
||||
LabelPosition::Top => (
|
||||
Vec2::new(
|
||||
rect.center().x,
|
||||
rect.min.y - self.config.label_offset + label_padding,
|
||||
),
|
||||
Vec2::new(rect.center().x, rect.min.y + label_padding),
|
||||
Align2::CENTER_TOP,
|
||||
),
|
||||
LabelPosition::Bottom => (
|
||||
Vec2::new(rect.center().x, rect.max.y + self.config.label_offset),
|
||||
Vec2::new(rect.center().x, rect.max.y - label_padding),
|
||||
Align2::CENTER_BOTTOM,
|
||||
),
|
||||
LabelPosition::Left => (
|
||||
Vec2::new(rect.min.x - self.config.label_offset, rect.center().y),
|
||||
Vec2::new(rect.min.x + label_padding, rect.center().y),
|
||||
Align2::LEFT_CENTER,
|
||||
),
|
||||
LabelPosition::Right => (
|
||||
Vec2::new(rect.max.x + self.config.label_offset, rect.center().y),
|
||||
Vec2::new(rect.max.x - label_padding, rect.center().y),
|
||||
Align2::RIGHT_CENTER,
|
||||
),
|
||||
};
|
||||
@@ -149,7 +158,7 @@ impl<'a> KnobRenderer<'a> {
|
||||
}
|
||||
|
||||
pub fn calculate_size(&self, ui: &Ui) -> Vec2 {
|
||||
let knob_size = Vec2::splat(self.config.size);
|
||||
let knob_size = Vec2::splat(self.config.size + self.config.stroke_width * 2.0);
|
||||
|
||||
let label_size = if let Some(label) = &self.config.label {
|
||||
let font_id = egui::FontId::proportional(self.config.font_size);
|
||||
@@ -161,16 +170,16 @@ impl<'a> KnobRenderer<'a> {
|
||||
Vec2::ZERO
|
||||
};
|
||||
|
||||
let label_padding = 2.0;
|
||||
let label_padding = 8.0;
|
||||
|
||||
match self.config.label_position {
|
||||
LabelPosition::Top | LabelPosition::Bottom => Vec2::new(
|
||||
knob_size.x.max(label_size.x + label_padding * 2.0),
|
||||
knob_size.y + label_size.y + label_padding * 2.0 + self.config.label_offset,
|
||||
knob_size.y + label_size.y + label_padding + self.config.label_offset,
|
||||
),
|
||||
LabelPosition::Left | LabelPosition::Right => Vec2::new(
|
||||
knob_size.x + label_size.x + label_padding * 2.0 + self.config.label_offset,
|
||||
knob_size.y.max(label_size.y + label_padding * 2.0),
|
||||
knob_size.x + label_size.x + label_padding + self.config.label_offset,
|
||||
knob_size.y.max(label_size.y + label_padding),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,6 +156,11 @@ impl<'a> Knob<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets a reset value to return to on doubleclick event.
|
||||
pub fn with_double_click_reset(mut self, reset_value: f32) -> Self {
|
||||
self.config.reset_value = Some(reset_value);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Knob<'_> {
|
||||
@@ -168,7 +173,7 @@ impl Widget for Knob<'_> {
|
||||
let renderer = KnobRenderer::new(&self.config, current_value, self.min, self.max);
|
||||
let adjusted_size = renderer.calculate_size(ui);
|
||||
|
||||
let (rect, response) = ui.allocate_exact_size(adjusted_size, Sense::drag());
|
||||
let (rect, response) = ui.allocate_exact_size(adjusted_size, Sense::click_and_drag());
|
||||
|
||||
let mut response = response;
|
||||
if response.dragged() {
|
||||
@@ -189,6 +194,10 @@ impl Widget for Knob<'_> {
|
||||
}
|
||||
|
||||
response.mark_changed();
|
||||
} else if response.double_clicked() {
|
||||
if let Some(reset_value) = self.config.reset_value {
|
||||
*self.value = reset_value
|
||||
}
|
||||
}
|
||||
|
||||
let knob_rect = renderer.calculate_knob_rect(rect);
|
||||
|
||||
Reference in New Issue
Block a user