Skip to content

Instantly share code, notes, and snippets.

@Xenakios
Created July 31, 2019 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xenakios/e6db592d1a54dec0402d7f6b3f00e9b9 to your computer and use it in GitHub Desktop.
Save Xenakios/e6db592d1a54dec0402d7f6b3f00e9b9 to your computer and use it in GitHub Desktop.
// .h
class XYPadComponent : public Component, public Value::Listener
{
public:
XYPadComponent(BinauralSpatAudioProcessor& p);
void valueChanged(Value& value) override;
void mouseDrag(const MouseEvent& ev) override;
void mouseWheelMove(const MouseEvent& ev, const MouseWheelDetails& det) override;
void paint(Graphics& g) override;
private:
BinauralSpatAudioProcessor& m_processor;
Value m_xpos0;
Value m_ypos0;
Value m_zpos0;
};
// .cpp
XYPadComponent::XYPadComponent(BinauralSpatAudioProcessor & p) : m_processor(p)
{
m_xpos0 = m_processor.m_state.getParameterAsValue("pos0x");
m_xpos0.addListener(this);
m_ypos0 = m_processor.m_state.getParameterAsValue("pos0y");
m_ypos0.addListener(this);
m_zpos0 = m_processor.m_state.getParameterAsValue("pos0z");
m_zpos0.addListener(this);
}
void XYPadComponent::valueChanged(Value & value)
{
repaint();
}
void XYPadComponent::mouseDrag(const MouseEvent & ev)
{
m_xpos0 = jlimit<float>(-1.0, 1.0, jmap<float>(ev.x, 0.0, getWidth(), -1.0, 1.0));
m_ypos0 = jlimit<float>(-1.0, 1.0, jmap<float>(ev.y, 0.0, getHeight(), -1.0, 1.0));
}
void XYPadComponent::mouseWheelMove(const MouseEvent & ev, const MouseWheelDetails & det)
{
float curval = m_zpos0.getValue();
if (det.deltaY < 0.0)
curval -= 0.05f;
if (det.deltaY > 0.0)
curval += 0.05f;
m_zpos0 = jlimit(-1.0f, 1.0f, curval);
}
void XYPadComponent::paint(Graphics & g)
{
g.fillAll(Colours::black);
g.setColour(Colours::white);
float xcor = jmap<float>(m_xpos0.getValue(), -1.0, 1.0, 0.0, getWidth());
float ycor = jmap<float>(m_ypos0.getValue(), -1.0, 1.0, 0.0, getHeight());
float zsize = jmap<float>(m_zpos0.getValue(), -1.0, 1.0, 2.0, 50.0);
g.fillEllipse(xcor - zsize / 2.0, ycor - zsize / 2.0, zsize, zsize);
g.drawText(String(m_valuechangecount), 10, 10, 100, 50, Justification::centred);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment