# Slider LookAndFeel attachment

**URL:** <https://forum.juce.com/t/slider-lookandfeel-attachment/50529>\
**Category:** General JUCE discussion\
**Created:** [March 9, 2022, 12:57am UTC](https://forum.juce.com/t/slider-lookandfeel-attachment/50529 "2022-03-09T00:57:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Donovan](https://avatars.discourse-cdn.com/v4/letter/d/ea666f/32.png) [@Donovan](https://forum.juce.com/u/Donovan)\
**Post date:** [March 9, 2022, 12:57am UTC](https://forum.juce.com/t/slider-lookandfeel-attachment/50529/1 "2022-03-09T00:57:28Z")

</div>

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

```auto
	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

```auto
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!
```

---

<div class="post-metadata">

**Author:** ![Mrugalla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mrugalla/32/13092_2.png) [@Mrugalla](https://forum.juce.com/u/Mrugalla)\
**Post date:** [March 9, 2022, 8:33am UTC](https://forum.juce.com/t/slider-lookandfeel-attachment/50529/2 "2022-03-09T08:33:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kunz](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kunz](https://forum.juce.com/u/kunz)\
**Post date:** [March 9, 2022, 3:47pm UTC](https://forum.juce.com/t/slider-lookandfeel-attachment/50529/3 "2022-03-09T15:47:30Z")

</div>

Cool, marching cubes, can’t wait to see this 🙂

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.

---

<div class="post-metadata">

**Author:** ![Donovan](https://avatars.discourse-cdn.com/v4/letter/d/ea666f/32.png) [@Donovan](https://forum.juce.com/u/Donovan)\
**Post date:** [March 9, 2022, 7:42pm UTC](https://forum.juce.com/t/slider-lookandfeel-attachment/50529/4 "2022-03-09T19:42:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Donovan](https://avatars.discourse-cdn.com/v4/letter/d/ea666f/32.png) [@Donovan](https://forum.juce.com/u/Donovan)\
**Post date:** [March 9, 2022, 7:44pm UTC](https://forum.juce.com/t/slider-lookandfeel-attachment/50529/5 "2022-03-09T19:44:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kunz](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kunz](https://forum.juce.com/u/kunz)\
**Post date:** [March 9, 2022, 10:20pm UTC](https://forum.juce.com/t/slider-lookandfeel-attachment/50529/6 "2022-03-09T22:20:40Z")

</div>

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

> **[Marching cubes](https://en.wikipedia.org/wiki/Marching_cubes)**
>
> Marching cubes is a computer graphics algorithm, published in the 1987 SIGGRAPH proceedings by Lorensen and Cline, for extracting a polygonal mesh of an isosurface from a three-dimensional discrete scalar field (the elements of which are sometimes called voxels). The applications of this algorithm are mainly concerned with medical visualizations such as CT and MRI scan data images, and special effects or 3-D modelling with what is usually called metaballs or other metasurfaces. The marching cu Th...

---

<div class="post-metadata">

**Author:** ![Donovan](https://avatars.discourse-cdn.com/v4/letter/d/ea666f/32.png) [@Donovan](https://forum.juce.com/u/Donovan)\
**Post date:** [March 10, 2022, 9:34am UTC](https://forum.juce.com/t/slider-lookandfeel-attachment/50529/9 "2022-03-10T09:34:39Z")

</div>

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