Getting state of custom SvgSwitch within the draw method?

Hello! I’m making some custom switches.

struct roundToggle : app::SvgSwitch {

  roundToggle() {
    momentary = false;
    addFrame(APP->window->loadSvg(asset::plugin(pluginInstance, "res/components/round_light_off.svg")));
    addFrame(APP->window->loadSvg(asset::plugin(pluginInstance, "res/components/round_light_on.svg")));
    box.size = Vec(15.5,15.5);
  }

  void draw(const DrawArgs& args) override
  {

    // How can I find the switch value here? 

I have a custom glow that I wish to draw only when the switch is ON.

Thanks!!

I think if(latch)

I had to look it up. I usually search the code on git hub. Here I searched for SvgSwitch and found this file: Rack/SvgSwitch.cpp at 05fa24a72bccf4023f5fb1b0fa7f1c26855c0926 · VCVRack/Rack · GitHub

I’m not sure what ‘latch’ does, but it doesn’t seem to be what I’m looking for.

Ah HA! I figured it out:

struct roundToggle : VoxglitchSwitch {

  roundToggle() {
    momentary = false;
    addFrame(APP->window->loadSvg(asset::plugin(pluginInstance, "res/components/round_light_off.svg")));
    addFrame(APP->window->loadSvg(asset::plugin(pluginInstance, "res/components/round_light6.svg")));
    box.size = Vec(15.5,15.5);
  }

  void draw(const DrawArgs& args) override
  {
        ParamQuantity *test = getParamQuantity();

        if(! (test->getValue() == test->getMinValue()))  // THIS WORKS!
        {

I have NO IDEA what this is doing. The name “ParamQuantity” really throws me off. My best guess is that this code is fetching the value of the switch that’s stored in the parameter. Credit to @CountModula for leading me to the answer:

VCVRackPlugins/CountModulaPushButtons.hpp at dd03920f3d386baffa0b0a3e637b7cc088d0c032 · countmodula/VCVRackPlugins · GitHub

Cheers,
Bret

1 Like

Yes, a ParamWidget will edit the float value in the ParamQuantity.

For safety, I would check “test” for null. I’m not sure of the behaviour of getParamQuantity() when draw is called from within the browser.

1 Like

I ran into this before your posting and wrapped everything in an “if(module)”. :smile:

Hi! I’m not quite following,

will edit the float value

Could you explain how ParamWidget edits the float value in ParamQuantity? If you have insights into all this works, I’d love to hear it.

If you look around in the rack SDK source you will see that ParamQuantity is derived from Quantity, and the comment in Quantity says:

/** A controller for manipulating a float value (which subclasses must store somehow) with limits and labels.

If you poke around in any of the widgets you will soon see that they are all eventually manipulating a ParamQuantity.

I don’t know how you prefer to poke around inside rack. I often use the not very smart method of sprinkling printf around in the code to see what comes out. I think smarter people just look at the code, or maybe set breakpoints in a debugger.

btw, the ParamQuantity is of course where you set custom tooltips and such. I think custom tooltips are actually documented.