ncblair / NTHN_TEMPLATE_PLUGIN

a starting point for real-time safe JUCE plugins
MIT License
76 stars 2 forks source link

ParameterSlider: Parameter snapping during mouse drag causing issues #1

Closed JamesWenlock closed 11 months ago

JamesWenlock commented 11 months ago

Knobs weren't responding smoothly when a parameter range was set at (0 -> 3) and the snap was at 1. Fixed by having an additional member variable holding the last value created by the slider then using this variable instead of the normalized value in the mouse drag function.

Here's the constructor

ParameterSlider::ParameterSlider(StateManager* s, size_t p_id) : juce::SettableTooltipClient(), juce::Component(), param_id(p_id + 1), state(s) { // param_id is initialized to p_id + 1 so that update_param_id runs on the first call update_param_id(p_id); setOpaque(true); setColour(ColourIds::sliderColourId, juce::Colour(0xff000000)); // to change the colour of the slider, set colour id 1 last_value = state->param_value(param_id); last_value = PARAMETER_RANGES[param_id].convertTo0to1(last_value); }

And here's the updated function

void ParameterSlider::mouseDrag(const juce::MouseEvent& e) { // change parameter value auto cur_position = e.getPosition(); auto change = cur_position - last_mouse_position; auto speed = (e.mods.isShiftDown() ? 0.05f : 1.0f) * pixels_per_percent; auto slider_change = 1.0f * float(change.getX() - change.getY()) / speed; auto new_val = last_value + slider_change; state->set_parameter_normalized(param_id, new_val); last_mouse_position = cur_position; last_value = new_val; }

Great work on the template by the way! Using it to update a filter plugin I made a few years ago.

ncblair commented 11 months ago

Thanks. Will take a look later today and push something!

ncblair commented 11 months ago

Should be fixed now. Thanks again!