xTibor / egui_extras_xt

MIT License
82 stars 9 forks source link

Customizable drag length for AudioKnob #12

Open andersforsgren opened 1 year ago

andersforsgren commented 1 year ago

Thanks for a great little lib. I think the drag is a bit coarse on the AudioKnob, e.g. when the knob is say 32px, then the drag is just divided into 32 steps

Existing impl: https://github.com/xTibor/egui_extras_xt/blob/d166b9a58c247809298fbdda099a56f912534904/egui_extras_xt/src/knobs/audio_knob.rs#L145

I have tried this with some success, simply introducing a scale factor so the user can drag N times the diameter (So you'd use 3 or 4). E.g.:

// self.drag_length: f32; a scale for how many diameters need to be dragged to cover the knob range
new_value += delta * (self.range.end() - self.range.start()) / (self.diameter * self.drag_length);

There are obviously other ways off accomplishing the same thing (E.g. using Some(100.0) as an optional fixed override might be better because it gives the user a way to provide the same drag-length for all knob sizes if they vary across the UI). E.g.:

// self.drag_length: Option<f32> an override to indicate how long to drag. 
new_value += delta * (self.range.end() - self.range.start()) / self.drag_length.unwrap_or(self.diameter);

I just use fixed 32px knobs so my use case was fine with a simple multiplier. I can submit a PR for this but I thought you might have some input on the api first.

Btw is there an issue with the shift-drag snapping for the AudioKnob? I can't quite get that working.

BillyDM commented 1 year ago

I'd reference how I do this in my code. My version also has the ability to fine-tune controls by scrolling the mouse wheel and/or holding down shift while dragging.

In fact, you can even use this logic for sliders to have fine-tune controls for those as well.

This is a concept called a "virtual slider" where every control has the same input behavior. The only difference between controls is how they are drawn.

https://github.com/MeadowlarkDAW/Meadowlark/blob/dev/src/ui/generic_views/virtual_slider.rs

xTibor commented 1 year ago

I stole that drag_length commit from your fork. Hope you don't mind it.

xTibor commented 1 year ago

Shift-snapping works for me, at least how it's used in the widget gallery:

https://user-images.githubusercontent.com/1627292/211760273-c50a1f82-73b9-431a-95ba-549d124857d6.mp4