Slider LookAndFeel attachment

Hi there,
I’m having an hard time here trying to understand what i do wrong.

Using the default LookAndFeel class allows me to recall sliders values when i close and open again the plugin in my daw. ValueTree Attachment works fine.

But when my custom LookAndFeel gets called automations work perfectly fine, closing and opening again my plugin wouldn’t recall the last ValueState, and saving ValueState with the daw and recalling it would work only if reloaded twice.

Would anybody get an advice? I can’t figure out if i should create a specific function in my custom LookAndFell or if i simply do something wrong.

Here is what the LookAndFeel function looks like

	void setParameters(float cubes, float marginCubes, float marginHeight)
	{
		mcubes = cubes;
		mmarginCubes = marginCubes;
		mmarginHeight = marginHeight;
	}

	void drawRotarySlider(juce::Graphics& g, int x, int y, int width, int height, float sliderPos, float startAngle, float endAngle, juce::Slider& slider) override
	{
		auto* colour = &colour0;
		g.setColour(colour->withAlpha(0.0f));
		g.drawRect(x, y, width, height);
		g.setColour(*colour);
		auto widthF = float(width);
		auto heightF = float(height);

		auto textWidthRel = 0.25f;
		auto textWidth = widthF * textWidthRel;

		aleo.setHeight(textWidth / slider.getName().length());
		g.setFont(aleo);
		g.drawText(slider.getName(), juce::Rectangle<float>(0.0f, 0.0f, textWidth, heightF), juce::Justification::centredRight, true);

		widthF -= textWidth;

		auto marginHeightMirror = 1.0f - mmarginHeight;
		auto cubesInv = 1.0f / mcubes;
		auto cubePerSlider = mcubes * sliderPos;
		auto cubesMaxInv = 1.0f / (cubePerSlider - 1.0f);
		auto cubeWidth = widthF / (mcubes + mcubes * mmarginCubes * 2.0f);
		auto cubeX = cubeWidth * mmarginCubes;
		for (auto c = 0.0f; c < cubePerSlider; ++c)
		{
			auto cRel = c * cubesMaxInv;
			auto cubeHeight = heightF * marginHeightMirror;
			auto cubeY = heightF - cubeHeight;
			auto cubeArea = juce::Rectangle<float>(textWidth + cubeX, cubeY, cubeWidth, cubeHeight);
			auto alpha = std::tanh(2.0f * c * cubesInv);
			g.setColour(colour->withAlpha(alpha));
			g.fillRect(cubeArea);
			cubeX += cubeWidth + cubeWidth * mmarginCubes * 2.0f;
		}
	}

And how i call it

void EMFXAudioProcessorEditor::sliderVisible(juce::Slider& slider)
{
	slider.setSliderStyle(juce::Slider::SliderStyle::RotaryHorizontalVerticalDrag);
	addAndMakeVisible(slider);
	slider.setRange(0.0f, 1.0f, 0.1f);
	slider.setValue(0.5f);
	slider.setLookAndFeel(&olaf2);
	olaf2.setParameters(35.0, 0.1, 0.0);
}

Thanks a lot!

it probably hasn’t got anything to do with the way you are painting the slider but you can easily test that by DBGing the parameter value in the lookandfeel method to find out both what the value is and when it decides to repaint.

the problematic bit is the lower code fragment. you are manually setting the things for the slider, like range, value, and attaching a parameter. but in modern juce applications the parameter ranges and values and stuff are all defined in the constructor of the audioProcessor already and attaching them to the sliders automatically makes them work with those ranges etc… the easiest way to do that is by learning how to use AudioProcessorValueTreeState and SliderAttachment or ParameterAttachment, but there are also benefits of rolling your own parameter types and attachment implementations

1 Like

Cool, marching cubes, can’t wait to see this :slight_smile:

The problem is maybe related to the lifetime of your LookAndFeel class. The LookAndFeel class is a UI component and deleted when you close the UI.

I would use something that is attached to the processor to remember values when closing and reopening the UI.

1 Like

Thanks a lot for that i m going to check around then :wink:

haha they will be marching yeah, margin indeed :wink:
Ok thanks a lot it helps a lot i’m gonna have a look.

I really read marching cubes :sweat_smile: I hoped so much for something like this:

1 Like

Ok my bad, i was calling values with an other attachment thanks a lot for your help guys :wink:

1 Like