Browse Source

Draw ModuleWidget shadow with drawLayer() instead of a custom drawShadow() method.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
0839a7c041
5 changed files with 29 additions and 23 deletions
  1. +1
    -1
      include/app/ModuleWidget.hpp
  2. +5
    -1
      include/widget/Widget.hpp
  3. +2
    -0
      src/app/CableWidget.cpp
  4. +16
    -11
      src/app/ModuleWidget.cpp
  5. +5
    -10
      src/app/RackWidget.cpp

+ 1
- 1
include/app/ModuleWidget.hpp View File

@@ -62,7 +62,7 @@ struct ModuleWidget : widget::OpaqueWidget {
std::vector<PortWidget*> getOutputs();

void draw(const DrawArgs& args) override;
void drawShadow(const DrawArgs& args);
void drawLayer(const DrawArgs& args, int layer) override;

/** Override to add context menu entries to your subclass.
It is recommended to add a blank `ui::MenuSeparator` first for spacing.


+ 5
- 1
include/widget/Widget.hpp View File

@@ -128,7 +128,10 @@ struct Widget : WeakBase {
NVGLUframebuffer* fb = NULL;
};

/** Draws the widget to the NanoVG context */
/** Draws the widget to the NanoVG context.

When overriding, call the superclass draw() to recurse to children.
*/
virtual void draw(const DrawArgs& args);
/** Override draw(const DrawArgs &args) instead */
DEPRECATED virtual void draw(NVGcontext* vg) {}
@@ -138,6 +141,7 @@ struct Widget : WeakBase {
Custom widgets may draw its children multiple times on different layers, passing an arbitrary layer number each time.
Layer 0 calls children's draw().
When overriding, always wrap draw commands in `if (layer == ...) {}` to avoid drawing on all layers.
When overriding, call the superclass drawLayer() to recurse to children.
*/
virtual void drawLayer(const DrawArgs& args, int layer);



+ 2
- 0
src/app/CableWidget.cpp View File

@@ -350,6 +350,8 @@ void CableWidget::drawLayer(const DrawArgs& args, int layer) {
nvgStrokeWidth(args.vg, thickness - 1.0);
nvgStroke(args.vg);
}

Widget::drawLayer(args, layer);
}




+ 16
- 11
src/app/ModuleWidget.cpp View File

@@ -284,17 +284,22 @@ void ModuleWidget::draw(const DrawArgs& args) {
nvgResetScissor(args.vg);
}

void ModuleWidget::drawShadow(const DrawArgs& args) {
nvgBeginPath(args.vg);
float r = 20; // Blur radius
float c = 20; // Corner radius
math::Rect shadowBox = box.zeroPos().grow(math::Vec(10, -30));
math::Rect shadowOutsideBox = shadowBox.grow(math::Vec(r, r));
nvgRect(args.vg, RECT_ARGS(shadowOutsideBox));
NVGcolor shadowColor = nvgRGBAf(0, 0, 0, 0.2);
NVGcolor transparentColor = nvgRGBAf(0, 0, 0, 0);
nvgFillPaint(args.vg, nvgBoxGradient(args.vg, RECT_ARGS(shadowBox), c, r, shadowColor, transparentColor));
nvgFill(args.vg);
void ModuleWidget::drawLayer(const DrawArgs& args, int layer) {
if (layer == -1) {
nvgBeginPath(args.vg);
float r = 20; // Blur radius
float c = 20; // Corner radius
math::Rect shadowBox = box.zeroPos().grow(math::Vec(10, -30));
math::Rect shadowOutsideBox = shadowBox.grow(math::Vec(r, r));
nvgRect(args.vg, RECT_ARGS(shadowOutsideBox));
NVGcolor shadowColor = nvgRGBAf(0, 0, 0, 0.2);
NVGcolor transparentColor = nvgRGBAf(0, 0, 0, 0);
nvgFillPaint(args.vg, nvgBoxGradient(args.vg, RECT_ARGS(shadowBox), c, r, shadowColor, transparentColor));
nvgFill(args.vg);
}
else {
Widget::drawLayer(args, layer);
}
}

void ModuleWidget::onHover(const HoverEvent& e) {


+ 5
- 10
src/app/RackWidget.cpp View File

@@ -40,18 +40,13 @@ static ModuleWidget* moduleWidgetFromJson(json_t* moduleJ) {

struct ModuleContainer : widget::Widget {
void draw(const DrawArgs& args) override {
// Draw shadows behind each ModuleWidget first, so the shadow doesn't overlap the front of other ModuleWidgets.
for (widget::Widget* child : children) {
ModuleWidget* w = dynamic_cast<ModuleWidget*>(child);
assert(w);

nvgSave(args.vg);
nvgTranslate(args.vg, child->box.pos.x, child->box.pos.y);
w->drawShadow(args);
nvgRestore(args.vg);
}
// Draw ModuleWidget shadows
Widget::drawLayer(args, -1);

Widget::draw(args);

// Draw lights and light halos
Widget::drawLayer(args, 1);
}
};



Loading…
Cancel
Save