[noob] light with svg

hello,
I’m looking for an example on how to create custom widget : I want to be able to create something similar to a light, but using a custom svn image.
I.E : to be able to draw or not an image on top of the module.
what is the most simple way?

(I found answer for custom switch, but I don’t need the user interaction part)

Thanks

1 Like

Take a look at the SvgButton source code for reference: https://github.com/VCVRack/Rack/blob/v1/include/app/SvgButton.hpp
https://github.com/VCVRack/Rack/blob/v1/src/app/SvgButton.cpp

Maybe there is an easier way, but what I would do is make a new custom widget inheriting from TransparentWidget which basically copies SvgButton without the click functionality. I think this is what the widget should look like, but WARNING: I didn’t actually run this code so there are probably errors.

struct ChCustomWidget : TransparentWidget {
	ChModule *module;
	widget::FramebufferWidget *fb;
	widget::SvgWidget *sw;

	ChCustomWidget() {
		addFrame(APP->window->loadSvg(asset::plugin(chPluginInstance,"res/svg1.svg"));
		addFrame(APP->window->loadSvg(asset::plugin(chPluginInstance,"res/svg2.svg"));
	}

	void addFrame(std::shared_ptr<Svg> svg) {
		frames.push_back(svg);
		// If this is our first frame, automatically set SVG and size
		if (!sw->svg) {
			sw->setSvg(svg);
			box.size = sw->box.size;
			fb->box.size = sw->box.size;
		}
	};

	void draw(const DrawArgs &args) override {
		if (module) {
			sw->setSvg(frames[module->svgIndex]);
			fb->dirty = true;
		}
		else {

		}
	}

}

Your Module (of type ChModule in my example) must have a member svgIndex which tells the widget which svg to show. Instantiate the ChCustomWidget in your ModuleWidget constructor like this:

ChCustomWidget *chWidget = new ChCustomWidget();
			chWidget->module = module;
			chWidget->box.pos = Vec(20, 20);
			addChild(chWidget);