Using a widget base class to draw a background rectangle?

Hello! I’m stuck on a simple problem and could use a second set of eyes. This is for my Groovebox module. I’m refactoring how some of the code is organized. I’ve created a class called “LCDWidget”, which is a TransparentWidget, which all the other LCDWidget displays inherit from:

Both the RatchetDisplay and SampleDisplay widgets have a drawLayer method that looks something like this:

  void drawLayer(const DrawArgs &args, int layer) override
  {
    if (layer == 1)
    {
      TransparentWidget::draw(args);
      const auto vg = args.vg;

      if (module)
      {
        nvgSave(vg);
        // draw stuff here

The TrackDisplay widget is a little bit different. It doesn’t have a draw method. Instead, it’s essentially a container for other widgets and adds them in the constructor, using code like:

    for (unsigned int i = 0; i < NUMBER_OF_TRACKS; i++)
    {
        // x and y are computed here
        // then...

        TrackLabelDisplay *track_label_display = new TrackLabelDisplay(i, track_label_width, track_label_height);
        track_label_display->setPosition(Vec(x, y));
        track_label_display->module = module;
        addChild(track_label_display);

What I’m trying to do next is have the base class, LCDWidget, draw the background of the display so that I don’t repeat that code in all of the child widgets. I don’t wish to add the background directly to the module’s SVG panel because I’m adding the ability to change the color of the background, so it must be drawn separately.

Any suggestions? I tried adding a draw() method in the base class, but it didn’t work and would sometimes crash rack. Maybe I was doing it wrong?

Thanks!

Never mind! I got it to work. My strategy was correct, but I was calling the parent class’s draw method in the wrong place, and also calling draw when I should have been calling drawLayer, like:

Widget::drawLayer(args, layer);

Once I cleaned up my code, it stopped crashing and started working. :man_dancing:

2 Likes