From f2ec0af4b0bb5314e07f238f7f9c68d6b4396a0b Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 26 Dec 2019 03:51:29 -0500 Subject: [PATCH] Restructure SvgPanel to handle dark mode. --- include/app/ModuleWidget.hpp | 12 ++++++---- include/app/SvgPanel.hpp | 11 +++++++-- include/helpers.hpp | 11 +++++++++ src/app/ModuleWidget.cpp | 28 +++++++++++++--------- src/app/SvgPanel.cpp | 45 +++++++++++++++++++++++++----------- 5 files changed, 77 insertions(+), 30 deletions(-) diff --git a/include/app/ModuleWidget.hpp b/include/app/ModuleWidget.hpp index ca0ea6a8..11051a7a 100644 --- a/include/app/ModuleWidget.hpp +++ b/include/app/ModuleWidget.hpp @@ -21,7 +21,6 @@ struct ModuleWidget : widget::OpaqueWidget { /** Owned. */ engine::Module* module = NULL; - widget::Widget* panel = NULL; /** Note that the indexes of these vectors do not necessarily correspond with the indexes of `Module::params` etc. */ std::vector params; @@ -43,11 +42,16 @@ struct ModuleWidget : widget::OpaqueWidget { void onDragEnd(const event::DragEnd& e) override; void onDragMove(const event::DragMove& e) override; - /** Associates this ModuleWidget with the Module - Transfers ownership + /** Associates this ModuleWidget with the Module. + Transfers ownership. */ void setModule(engine::Module* module); - void setPanel(std::shared_ptr svg); + /** Sets the panel and sets the size of the ModuleWidget from the panel. + Transfers ownership. + */ + void setPanel(widget::Widget* panel); + /** Use `setPanel(createPanel(svg))` instead. */ + DEPRECATED void setPanel(std::shared_ptr svg); /** Convenience functions for adding special widgets (calls addChild()) */ void addParam(ParamWidget* param); diff --git a/include/app/SvgPanel.hpp b/include/app/SvgPanel.hpp index 9044f008..80cfe820 100644 --- a/include/app/SvgPanel.hpp +++ b/include/app/SvgPanel.hpp @@ -15,9 +15,16 @@ struct PanelBorder : widget::TransparentWidget { }; -struct SvgPanel : widget::FramebufferWidget { +struct SvgPanel : widget::Widget { + widget::FramebufferWidget* fb; + widget::SvgWidget* sw; + PanelBorder* panelBorder; + std::shared_ptr svg; + std::shared_ptr darkSvg; + + SvgPanel(); void step() override; - void setBackground(std::shared_ptr svg); + void setBackground(std::shared_ptr svg, std::shared_ptr darkSvg = NULL); }; diff --git a/include/helpers.hpp b/include/helpers.hpp index 63c99599..aeddcd7b 100644 --- a/include/helpers.hpp +++ b/include/helpers.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -55,6 +56,16 @@ TWidget* createWidgetCentered(math::Vec pos) { return o; } +inline app::SvgPanel* createPanel(std::string svgPath, std::string darkSvgPath = "") { + app::SvgPanel* panel = new app::SvgPanel; + std::shared_ptr svg = APP->window->loadSvg(svgPath); + std::shared_ptr darkSvg; + if (darkSvgPath != "") + darkSvg = APP->window->loadSvg(darkSvgPath); + panel->setBackground(svg, darkSvg); + return panel; +} + template TParamWidget* createParam(math::Vec pos, engine::Module* module, int paramId) { TParamWidget* o = new TParamWidget; diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index 64667042..49b6f554 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -283,6 +283,8 @@ struct ModuleWidget::Internal { Set by RackWidget::updateModuleOldPositions() when *any* module begins dragging, since force-dragging can move other modules around. */ math::Vec oldPos; + + widget::Widget* panel = NULL; }; @@ -472,22 +474,26 @@ void ModuleWidget::setModule(engine::Module* module) { this->module = module; } -void ModuleWidget::setPanel(std::shared_ptr svg) { +void ModuleWidget::setPanel(widget::Widget* panel) { // Remove existing panel + if (internal->panel) { + removeChild(internal->panel); + delete internal->panel; + internal->panel = NULL; + } + if (panel) { - removeChild(panel); - delete panel; - panel = NULL; + addChildBottom(panel); + internal->panel = panel; + box.size.x = std::round(panel->box.size.x / RACK_GRID_WIDTH) * RACK_GRID_WIDTH; } +} +void ModuleWidget::setPanel(std::shared_ptr svg) { // Create SvgPanel - SvgPanel* svgPanel = new SvgPanel; - svgPanel->setBackground(svg); - panel = svgPanel; - addChildBottom(panel); - - // Set ModuleWidget size based on panel - box.size.x = std::round(panel->box.size.x / RACK_GRID_WIDTH) * RACK_GRID_WIDTH; + SvgPanel* panel = new SvgPanel; + panel->setBackground(svg); + setPanel(panel); } void ModuleWidget::addParam(ParamWidget* param) { diff --git a/src/app/SvgPanel.cpp b/src/app/SvgPanel.cpp index 7f5459a5..4a8e229e 100644 --- a/src/app/SvgPanel.cpp +++ b/src/app/SvgPanel.cpp @@ -1,4 +1,5 @@ #include +#include namespace rack { @@ -15,25 +16,43 @@ void PanelBorder::draw(const DrawArgs& args) { } +SvgPanel::SvgPanel() { + fb = new widget::FramebufferWidget; + addChild(fb); + + sw = new widget::SvgWidget; + fb->addChild(sw); + + panelBorder = new PanelBorder; + fb->addChild(panelBorder); +} + + void SvgPanel::step() { - if (math::isNear(APP->window->pixelRatio, 1.0)) { + if (APP->window->pixelRatio < 2.0) { // Small details draw poorly at low DPI, so oversample when drawing to the framebuffer - oversample = 2.0; + fb->oversample = 2.0; } - FramebufferWidget::step(); -} -void SvgPanel::setBackground(std::shared_ptr svg) { - widget::SvgWidget* sw = new widget::SvgWidget; - sw->setSvg(svg); - addChild(sw); + std::shared_ptr svg = this->svg; + if (settings::isDarkMode() && this->darkSvg) + svg = this->darkSvg; + if (sw->svg != svg) { + sw->setSvg(svg); + fb->dirty = true; + } - // Set size - box.size = sw->box.size.div(RACK_GRID_SIZE).round().mult(RACK_GRID_SIZE); + Widget::step(); +} - PanelBorder* pb = new PanelBorder; - pb->box.size = box.size; - addChild(pb); +void SvgPanel::setBackground(std::shared_ptr svg, std::shared_ptr darkSvg) { + this->svg = svg; + this->darkSvg = darkSvg; + + sw->setSvg(svg); + fb->box.size = sw->box.size.div(RACK_GRID_SIZE).round().mult(RACK_GRID_SIZE); + panelBorder->box.size = fb->box.size; + box.size = fb->box.size; }