From ef6dc2ff675bfe55411f0b2c9f1c4bcb6da15a36 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 9 Feb 2017 04:27:41 -0500 Subject: [PATCH] Save draw state before each child is drawn, so widgets draw at (0, 0) instead of box.pos. --- src/gui.cpp | 3 +++ src/widgets/Button.cpp | 2 +- src/widgets/ChoiceButton.cpp | 2 +- src/widgets/Label.cpp | 2 +- src/widgets/Light.cpp | 3 +-- src/widgets/Menu.cpp | 2 +- src/widgets/MenuItem.cpp | 2 +- src/widgets/MenuLabel.cpp | 2 +- src/widgets/ModuleWidget.cpp | 8 ++++---- src/widgets/Panel.cpp | 4 ++-- src/widgets/RackScene.cpp | 13 ------------- src/widgets/RackWidget.cpp | 4 ++-- src/widgets/RadioButton.cpp | 2 +- src/widgets/ScrollBar.cpp | 2 +- src/widgets/Slider.cpp | 2 +- src/widgets/SpriteWidget.cpp | 6 ++---- src/widgets/Toolbar.cpp | 4 ++-- src/widgets/Widget.cpp | 4 ++-- src/widgets/WireWidget.cpp | 2 +- 19 files changed, 28 insertions(+), 41 deletions(-) diff --git a/src/gui.cpp b/src/gui.cpp index f4540ef8..5de14f51 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -165,7 +165,10 @@ void renderGui() { nvgBeginFrame(vg, width, height, 1.0); + nvgSave(vg); gScene->draw(vg); + nvgRestore(vg); + nvgEndFrame(vg); glfwSwapBuffers(window); } diff --git a/src/widgets/Button.cpp b/src/widgets/Button.cpp index dbfa440d..42c01873 100644 --- a/src/widgets/Button.cpp +++ b/src/widgets/Button.cpp @@ -4,7 +4,7 @@ namespace rack { void Button::draw(NVGcontext *vg) { - bndToolButton(vg, box.pos.x, box.pos.y, box.size.x, box.size.y, BND_CORNER_NONE, state, -1, text.c_str()); + bndToolButton(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE, state, -1, text.c_str()); } void Button::onMouseEnter() { diff --git a/src/widgets/ChoiceButton.cpp b/src/widgets/ChoiceButton.cpp index 15481eb6..e140dea4 100644 --- a/src/widgets/ChoiceButton.cpp +++ b/src/widgets/ChoiceButton.cpp @@ -4,7 +4,7 @@ namespace rack { void ChoiceButton::draw(NVGcontext *vg) { - bndChoiceButton(vg, box.pos.x, box.pos.y, box.size.x, box.size.y, BND_CORNER_NONE, state, -1, text.c_str()); + bndChoiceButton(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE, state, -1, text.c_str()); } diff --git a/src/widgets/Label.cpp b/src/widgets/Label.cpp index 3c810390..07b4d132 100644 --- a/src/widgets/Label.cpp +++ b/src/widgets/Label.cpp @@ -4,7 +4,7 @@ namespace rack { void Label::draw(NVGcontext *vg) { - bndLabel(vg, box.pos.x, box.pos.y, box.size.x, box.size.y, -1, text.c_str()); + bndLabel(vg, 0.0, 0.0, box.size.x, box.size.y, -1, text.c_str()); } diff --git a/src/widgets/Light.cpp b/src/widgets/Light.cpp index 19cc8a70..fe0bf619 100644 --- a/src/widgets/Light.cpp +++ b/src/widgets/Light.cpp @@ -6,11 +6,10 @@ namespace rack { void Light::draw(NVGcontext *vg) { NVGcolor colorOutline = nvgLerpRGBA(color, nvgRGBf(0.0, 0.0, 0.0), 0.5); - Vec c = box.getCenter(); Vec r = box.size.div(2.0); nvgBeginPath(vg); - nvgEllipse(vg, c.x, c.y, r.x - 1.0, r.y - 1.0); + nvgEllipse(vg, r.x, r.y, r.x - 1.0, r.y - 1.0); nvgFillColor(vg, color); nvgFill(vg); diff --git a/src/widgets/Menu.cpp b/src/widgets/Menu.cpp index 00915d81..6647b120 100644 --- a/src/widgets/Menu.cpp +++ b/src/widgets/Menu.cpp @@ -23,7 +23,7 @@ void Menu::draw(NVGcontext *vg) { child->box.size.x = box.size.x; } - bndMenuBackground(vg, box.pos.x, box.pos.y, box.size.x, box.size.y, BND_CORNER_NONE); + bndMenuBackground(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE); Widget::draw(vg); } diff --git a/src/widgets/MenuItem.cpp b/src/widgets/MenuItem.cpp index ccc92213..b0f351b3 100644 --- a/src/widgets/MenuItem.cpp +++ b/src/widgets/MenuItem.cpp @@ -4,7 +4,7 @@ namespace rack { void MenuItem::draw(NVGcontext *vg) { - bndMenuItem(vg, box.pos.x, box.pos.y, box.size.x, box.size.y, state, -1, text.c_str()); + bndMenuItem(vg, 0.0, 0.0, box.size.x, box.size.y, state, -1, text.c_str()); } void MenuItem::onMouseEnter() { diff --git a/src/widgets/MenuLabel.cpp b/src/widgets/MenuLabel.cpp index 464dd603..e8bafc6f 100644 --- a/src/widgets/MenuLabel.cpp +++ b/src/widgets/MenuLabel.cpp @@ -4,7 +4,7 @@ namespace rack { void MenuLabel::draw(NVGcontext *vg) { - bndMenuLabel(vg, box.pos.x, box.pos.y, box.size.x, box.size.y, -1, text.c_str()); + bndMenuLabel(vg, 0.0, 0.0, box.size.x, box.size.y, -1, text.c_str()); } diff --git a/src/widgets/ModuleWidget.cpp b/src/widgets/ModuleWidget.cpp index 6386d020..18b4c697 100644 --- a/src/widgets/ModuleWidget.cpp +++ b/src/widgets/ModuleWidget.cpp @@ -99,7 +99,7 @@ void ModuleWidget::cloneParams(ModuleWidget *source) { void ModuleWidget::draw(NVGcontext *vg) { Widget::draw(vg); - bndBevel(vg, box.pos.x, box.pos.y, box.size.x, box.size.y); + bndBevel(vg, 0.0, 0.0, box.size.x, box.size.y); // CPU usage text if (dynamic_cast(gScene)->toolbar->cpuUsageButton->value != 0.0) { @@ -108,17 +108,17 @@ void ModuleWidget::draw(NVGcontext *vg) { nvgSave(vg); nvgBeginPath(vg); - nvgRect(vg, box.pos.x, box.pos.y, box.size.x, BND_WIDGET_HEIGHT); + nvgRect(vg, 0.0, 0.0, box.size.x, BND_WIDGET_HEIGHT); nvgFillColor(vg, nvgRGBf(0.0, 0.0, 0.0)); nvgFill(vg); nvgBeginPath(vg); cpuTime = clampf(cpuTime, 0.0, 1.0); - nvgRect(vg, box.pos.x, box.pos.y, box.size.x * cpuTime, BND_WIDGET_HEIGHT); + nvgRect(vg, 0.0, 0.0, box.size.x * cpuTime, BND_WIDGET_HEIGHT); nvgFillColor(vg, nvgHSL(0.33 * cubic(1.0 - cpuTime), 1.0, 0.4)); nvgFill(vg); - bndMenuItem(vg, box.pos.x, box.pos.y, box.size.x, BND_WIDGET_HEIGHT, BND_DEFAULT, -1, text.c_str()); + bndMenuItem(vg, 0.0, 0.0, box.size.x, BND_WIDGET_HEIGHT, BND_DEFAULT, -1, text.c_str()); nvgRestore(vg); } } diff --git a/src/widgets/Panel.cpp b/src/widgets/Panel.cpp index 6fa111a0..131c26a9 100644 --- a/src/widgets/Panel.cpp +++ b/src/widgets/Panel.cpp @@ -5,7 +5,7 @@ namespace rack { void Panel::draw(NVGcontext *vg) { nvgBeginPath(vg); - nvgRect(vg, box.pos.x, box.pos.y, box.size.x, box.size.y); + nvgRect(vg, 0.0, 0.0, box.size.x, box.size.y); // Background color nvgFillColor(vg, backgroundColor); @@ -15,7 +15,7 @@ void Panel::draw(NVGcontext *vg) { if (backgroundImage) { int width, height; nvgImageSize(vg, backgroundImage->handle, &width, &height); - NVGpaint paint = nvgImagePattern(vg, box.pos.x, box.pos.y, width, height, 0.0, backgroundImage->handle, 1.0); + NVGpaint paint = nvgImagePattern(vg, 0.0, 0.0, width, height, 0.0, backgroundImage->handle, 1.0); nvgFillPaint(vg, paint); nvgFill(vg); } diff --git a/src/widgets/RackScene.cpp b/src/widgets/RackScene.cpp index 6ea612c1..7d5c3044 100644 --- a/src/widgets/RackScene.cpp +++ b/src/widgets/RackScene.cpp @@ -27,19 +27,6 @@ void RackScene::step() { void RackScene::draw(NVGcontext *vg) { Scene::draw(vg); - - // // Draw custom stuff here - // static std::shared_ptr svg; - // if (!svg) - // svg = SVG::load("res/ComponentLibrary/CL1362.svg"); - - // for (float y = 0.0; y < 1000.0; y += 200.0) - // for (float x = 0.0; x < 1000.0; x += 200.0) { - // nvgSave(vg); - // nvgTranslate(vg, x, y); - // drawSVG(vg, svg->handle); - // nvgRestore(vg); - // } } diff --git a/src/widgets/RackWidget.cpp b/src/widgets/RackWidget.cpp index 9e762600..55b6464e 100644 --- a/src/widgets/RackWidget.cpp +++ b/src/widgets/RackWidget.cpp @@ -289,7 +289,7 @@ void RackWidget::step() { void RackWidget::draw(NVGcontext *vg) { nvgBeginPath(vg); - nvgRect(vg, box.pos.x, box.pos.y, box.size.x, box.size.y); + nvgRect(vg, 0.0, 0.0, box.size.x, box.size.y); // Background color nvgFillColor(vg, nvgRGBf(0.2, 0.2, 0.2)); @@ -299,7 +299,7 @@ void RackWidget::draw(NVGcontext *vg) { { int imageWidth, imageHeight; nvgImageSize(vg, railsImage->handle, &imageWidth, &imageHeight); - NVGpaint paint = nvgImagePattern(vg, box.pos.x, box.pos.y, imageWidth, imageHeight, 0.0, railsImage->handle, 1.0); + NVGpaint paint = nvgImagePattern(vg, 0.0, 0.0, imageWidth, imageHeight, 0.0, railsImage->handle, 1.0); nvgFillPaint(vg, paint); nvgFill(vg); } diff --git a/src/widgets/RadioButton.cpp b/src/widgets/RadioButton.cpp index 8f5468eb..e1a75641 100644 --- a/src/widgets/RadioButton.cpp +++ b/src/widgets/RadioButton.cpp @@ -4,7 +4,7 @@ namespace rack { void RadioButton::draw(NVGcontext *vg) { - bndRadioButton(vg, box.pos.x, box.pos.y, box.size.x, box.size.y, BND_CORNER_NONE, value == 0.0 ? state : BND_ACTIVE, -1, label.c_str()); + bndRadioButton(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE, value == 0.0 ? state : BND_ACTIVE, -1, label.c_str()); } void RadioButton::onMouseEnter() { diff --git a/src/widgets/ScrollBar.cpp b/src/widgets/ScrollBar.cpp index ce0b049d..b60c196a 100644 --- a/src/widgets/ScrollBar.cpp +++ b/src/widgets/ScrollBar.cpp @@ -10,7 +10,7 @@ void ScrollBar::draw(NVGcontext *vg) { float offset = containerOffset / maxOffset; float size = boxSize / containerSize; size = clampf(size, 0.0, 1.0); - bndScrollBar(vg, box.pos.x, box.pos.y, box.size.x, box.size.y, state, offset, size); + bndScrollBar(vg, 0.0, 0.0, box.size.x, box.size.y, state, offset, size); } void ScrollBar::move(float delta) { diff --git a/src/widgets/Slider.cpp b/src/widgets/Slider.cpp index 9f4414cc..9f46f755 100644 --- a/src/widgets/Slider.cpp +++ b/src/widgets/Slider.cpp @@ -8,7 +8,7 @@ namespace rack { void Slider::draw(NVGcontext *vg) { float progress = mapf(value, minValue, maxValue, 0.0, 1.0); - bndSlider(vg, box.pos.x, box.pos.y, box.size.x, box.size.y, BND_CORNER_NONE, state, progress, getText().c_str(), NULL); + bndSlider(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE, state, progress, getText().c_str(), NULL); } void Slider::onDragStart() { diff --git a/src/widgets/SpriteWidget.cpp b/src/widgets/SpriteWidget.cpp index 09e2981a..9d1367b5 100644 --- a/src/widgets/SpriteWidget.cpp +++ b/src/widgets/SpriteWidget.cpp @@ -4,8 +4,6 @@ namespace rack { void SpriteWidget::draw(NVGcontext *vg) { - Vec pos = box.pos.plus(spriteOffset); - int width, height; nvgImageSize(vg, spriteImage->handle, &width, &height); int stride = width / spriteSize.x; @@ -14,10 +12,10 @@ void SpriteWidget::draw(NVGcontext *vg) { return; } Vec offset = Vec((index % stride) * spriteSize.x, (index / stride) * spriteSize.y); - NVGpaint paint = nvgImagePattern(vg, pos.x - offset.x, pos.y - offset.y, width, height, 0.0, spriteImage->handle, 1.0); + NVGpaint paint = nvgImagePattern(vg, spriteOffset.x - offset.x, spriteOffset.y - offset.y, width, height, 0.0, spriteImage->handle, 1.0); nvgFillPaint(vg, paint); nvgBeginPath(vg); - nvgRect(vg, pos.x, pos.y, spriteSize.x, spriteSize.y); + nvgRect(vg, spriteOffset.x, spriteOffset.y, spriteSize.x, spriteSize.y); nvgFill(vg); } diff --git a/src/widgets/Toolbar.cpp b/src/widgets/Toolbar.cpp index 1a9abd06..586391f4 100644 --- a/src/widgets/Toolbar.cpp +++ b/src/widgets/Toolbar.cpp @@ -165,8 +165,8 @@ Toolbar::Toolbar() { } void Toolbar::draw(NVGcontext *vg) { - bndBackground(vg, box.pos.x, box.pos.y, box.size.x, box.size.y); - bndBevel(vg, box.pos.x, box.pos.y, box.size.x, box.size.y); + bndBackground(vg, 0.0, 0.0, box.size.x, box.size.y); + bndBevel(vg, 0.0, 0.0, box.size.x, box.size.y); Widget::draw(vg); } diff --git a/src/widgets/Widget.cpp b/src/widgets/Widget.cpp index 78116dad..c075c2b5 100644 --- a/src/widgets/Widget.cpp +++ b/src/widgets/Widget.cpp @@ -69,12 +69,12 @@ void Widget::step() { } void Widget::draw(NVGcontext *vg) { - nvgTranslate(vg, box.pos.x, box.pos.y); for (Widget *child : children) { nvgSave(vg); + nvgTranslate(vg, child->box.pos.x, child->box.pos.y); child->draw(vg); + nvgRestore(vg); } - nvgRestore(vg); } Widget *Widget::onMouseDown(Vec pos, int button) { diff --git a/src/widgets/WireWidget.cpp b/src/widgets/WireWidget.cpp index 52e67f52..8c98c7e2 100644 --- a/src/widgets/WireWidget.cpp +++ b/src/widgets/WireWidget.cpp @@ -120,7 +120,7 @@ void WireWidget::updateWire() { } void WireWidget::draw(NVGcontext *vg) { - Vec absolutePos = getAbsolutePos(); + Vec absolutePos = getAbsolutePos().minus(box.pos); float opacity = dynamic_cast(gScene)->toolbar->wireOpacitySlider->value / 100.0; float tension = dynamic_cast(gScene)->toolbar->wireTensionSlider->value;