Hello fellow Jucers ![]()
i am trying to replace the juce slider (knob) with a .png image
here is what i have done so far with not great success…
CustimGraphic.cpp file:
#include "CustomGraphic.h"
#include "JuceHeader.h"
void customSlider::drawRotarySlider(Graphics& g, int x, int y, int width, int height, float sliderPos,
float rotaryStartAngle, float rotaryEndAngle, Slider& slider)
{
float angleInRadians;
AffineTransform rotator;
verticalSliderKnob = ImageCache::getFromMemory(BinaryData::images_png,
BinaryData::images_pngSize);
g.setOpacity(1.0);
if (slider.getName() == "HPFSlider")
{
angleInRadians = pow((slider.getValue() - 20.f) / (20000 - 20), 0.228) + 6.166;
g.drawImageTransformed(verticalSliderKnob, AffineTransform::rotation(angleInRadians * 4.7123889804 , verticalSliderKnob.getWidth() / 2, verticalSliderKnob.getHeight() / 2), false);
}
else
{
angleInRadians = pow((slider.getValue() - 20.f) / (20000 - 20), 0.228) + 6.166;
g.drawImageTransformed(verticalSliderKnob, AffineTransform::rotation(angleInRadians * 4.7123889804 , verticalSliderKnob.getWidth() / 2, verticalSliderKnob.getHeight() / 2), false);
}
}
and here is my CustomGraphic.h file:
#include "JuceHeader.h"
#ifndef CUSTOMGRAPHIC_H_INCLUDED
#define CUSTOMGRAPHIC_H_INCLUDED
class customSlider : public LookAndFeel_V3
{
void drawRotarySlider(Graphics& g, int x, int y, int width, int height, float sliderPos,
float rotaryStartAngle, float rotaryEndAngle, Slider& slider);
private: Image verticalSliderKnob;
};
#endif // CUSTOMGRAPHIC_H_INCLUDED
this is the Editor.h
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
#include "PluginProcessor.h"
#include "CustomGraphic.h"
//==============================================================================
/** This is the editor component that our filter will display.
*/
class DaBorderAudioProcessorEditor : public AudioProcessorEditor,
public Slider::Listener,
public Label::Listener
{
public:
DaBorderAudioProcessorEditor(DaBorderAudioProcessor&);
~DaBorderAudioProcessorEditor();
typedef AudioProcessorValueTreeState::SliderAttachment SliderAttachment;
//==============================================================================
void paint(Graphics&) override;
void resized() override;
//void sliderValueChanged(Slider* sliderThatWasMoved, Graphics g);
void sliderValueChanged(Slider* sliderThatWasMoved) override;
void labelTextChanged(Label* labelThatHasChanged) override;
//void drawRotarySlider(Graphics& g, int x, int y, int width, int height, float sliderPos,
// float rotaryStartAngle, float rotaryEndAngle, Slider& slider);
static const char* images_png;
static const int images_pngSize;
static const char* Background_png;
static const int Background_pngSize;
private:
class ParameterSlider;
Image Background,verticalSliderKnob;
//Sliders
ScopedPointer<ParameterSlider> LPFSlider, HPFSlider;
//Slider HPSlider, LPSlider;
//Labels
ScopedPointer<Label> label;
ScopedPointer<Label> label2;
DaBorderAudioProcessor& processor;
//==============================================================================
DaBorderAudioProcessor& getProcessor() const
{
return static_cast<DaBorderAudioProcessor&> (processor);
}
};
and this is the Editor.cpp :
#include "PluginProcessor.h"
#include "PluginEditor.h"
//==============================================================================
// This is a handy slider subclass that controls an AudioProcessorParameter
// (may move this class into the library itself at some point in the future..)
class DaBorderAudioProcessorEditor::ParameterSlider : public Slider,
private Timer
{
public:
ParameterSlider(AudioProcessorParameter& p)
: Slider(p.getName(256)), param(p)
{
setRange(0.0, 1.0, 0.0);
startTimerHz(30);
updateSliderPos();
}
void valueChanged() override { param.setValueNotifyingHost((float)Slider::getValue()); }
void timerCallback() override { updateSliderPos(); }
void startedDragging() override { param.beginChangeGesture(); }
void stoppedDragging() override { param.endChangeGesture(); }
double getValueFromText(const String& text) override { return param.getValueForText(text); }
String getTextFromValue(double value) override { return param.getText((float)value, 1024); }
void updateSliderPos()
{
const float newValue = param.getValue();
if (newValue != (float)Slider::getValue() && !isMouseButtonDown())
Slider::setValue(newValue, NotificationType::dontSendNotification);
}
AudioProcessorParameter& param;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ParameterSlider)
};
//==============================================================================
DaBorderAudioProcessorEditor::DaBorderAudioProcessorEditor(DaBorderAudioProcessor& owner)
: AudioProcessorEditor(owner), processor(owner)
{
//Sliders
//1
addAndMakeVisible(LPFSlider = new ParameterSlider(*owner.LPFREQParam));
LPFSlider->setSliderStyle(Slider::RotaryHorizontalVerticalDrag);
LPFSlider->setTextBoxStyle(Slider::NoTextBox, false, 60, 20);
LPFSlider->setName("LPFSlider");
LPFSlider->addListener(this);
//2
addAndMakeVisible(HPFSlider = new ParameterSlider(*owner.HPFREQParam));
HPFSlider->setSliderStyle(Slider::RotaryHorizontalVerticalDrag);
HPFSlider->setTextBoxStyle(Slider::NoTextBox, false, 60, 20);
HPFSlider->setName("HPFSlider");
HPFSlider->addListener(this);
//Labels
addAndMakeVisible(label = new Label("hpf",
TRANS("20")));
label->setFont(Font(15.00f, Font::plain).withTypefaceStyle("Regular"));
label->setJustificationType(Justification::centredRight);
label->setEditable(false, true, false);
label->setColour(Label::textColourId, Colours::white);
label->setColour(TextEditor::textColourId, Colours::white);
label->setColour(TextEditor::backgroundColourId, Colours::grey);
label->addListener(this);
addAndMakeVisible(label2 = new Label("lpf",
TRANS("20")));
label2->setFont(Font(15.00f, Font::plain).withTypefaceStyle("Regular"));
label2->setJustificationType(Justification::centredRight);
label2->setEditable(false, true, false);
label2->setColour(Label::textColourId, Colours::white);
label2->setColour(TextEditor::textColourId, Colours::white);
label2->setColour(TextEditor::backgroundColourId, Colours::grey);
label2->addListener(this);
Background = ImageCache::getFromMemory(BinaryData::Background_png,
BinaryData::Background_pngSize);
//
verticalSliderKnob = ImageCache::getFromMemory(BinaryData::images_png,
BinaryData::images_pngSize);
//
// set resize limits for this plug-in
setResizeLimits(619, 303, 619, 303);
// set our component's initial size to be the last one that was stored in the filter's settings
setSize(owner.lastUIWidth, owner.lastUIHeight);
//LookAndFeel::setDefaultLookAndFeel(&pluginLookAndFeel);
customSlider* lafobjct = new customSlider();
setLookAndFeel(lafobjct);
}
DaBorderAudioProcessorEditor::~DaBorderAudioProcessorEditor()
{
LPFSlider = nullptr;
HPFSlider = nullptr;
label = nullptr;
label2 = nullptr;
}
//==============================================================================
void DaBorderAudioProcessorEditor::paint(Graphics& g)
{
g.fillAll();
int x = 0, y = 0, width = 619, height = 303;
//g.drawImage(Background,
// x, y, width, height,
// 0, 0, Background.getWidth(), Background.getHeight());
//g.drawImage(verticalSliderKnob,
// x, y, width, height,
// 0, 0, verticalSliderKnob.getWidth(), verticalSliderKnob.getHeight());
}
void DaBorderAudioProcessorEditor::resized()
{
LPFSlider->setBounds(121, 110, 120, 120);
HPFSlider->setBounds(378, 110, 120, 120);
label->setBounds(156, 260, 50, 24);
label2->setBounds(413, 260, 50, 24);
getProcessor().lastUIWidth = getWidth();
getProcessor().lastUIHeight = getHeight();
}
//Slider listener for updating custom labels
void DaBorderAudioProcessorEditor::sliderValueChanged(Slider* sliderThatWasMoved)
{
if (sliderThatWasMoved == LPFSlider)
{
String temp = String((int)*processor.LPFREQParam);
label->setText(temp, dontSendNotification);
}
else if (sliderThatWasMoved == HPFSlider)
{
String temp = String((int)*processor.HPFREQParam);
label2->setText(temp, dontSendNotification);
}
}
//Labels listener to write value to slider
void DaBorderAudioProcessorEditor::labelTextChanged(Label* labelThatHasChanged)
{
if (labelThatHasChanged == label)
{
float value = (float)label->getText().getIntValue();
//Bottom OutOf range
if (value < 20)
label->setText("20", dontSendNotification);
//Top outOf range
else if (value > 20000)
label->setText("20000", dontSendNotification);
//Parameter's range 0-1 so, normalise it
float normalisedValue = value / (2000 - 20);
processor.LPFREQParam->setValueNotifyingHost(normalisedValue);
//in case slider was already in that position
if (HPFSlider->getValue() == value)
label->setText(String(value), dontSendNotification);
}
else if (labelThatHasChanged == label2)
{
float value = (float)label2->getText().getIntValue();
//Bottom OutOf range
if (value < 20)
label2->setText("20", dontSendNotification);
//Top outOf range
else if (value > 20000)
label2->setText("20000", dontSendNotification);
//Parameter's range 0-1 so, normalise it
float normalisedValue = value / (2000 - 20);
processor.HPFREQParam->setValueNotifyingHost(normalisedValue);
//in case slider was already in that position
if (HPFSlider->getValue() == value)
label2->setText(String(value), dontSendNotification);
}
}
any ideas what is going wrong?
