From 61d387c63baee7954abb381e8426dc98f1de4a32 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 15 Jul 2021 19:03:13 -0400 Subject: [PATCH] Rewrite RackRail and rename it to RailWidget. Add rail graphic revision from Pyer. --- include/app/RackWidget.hpp | 1 - include/app/{RackRail.hpp => RailWidget.hpp} | 8 +- include/app/common.hpp | 1 - include/math.hpp | 3 + include/rack.hpp | 2 +- res/ComponentLibrary/RackBusboard.svg | 501 ---------------- res/ComponentLibrary/RackRails.svg | 39 -- res/ComponentLibrary/Rail.svg | 566 +++++++++++++++++++ src/app/RackRail.cpp | 67 --- src/app/RackWidget.cpp | 50 +- src/app/RailWidget.cpp | 46 ++ 11 files changed, 648 insertions(+), 636 deletions(-) rename include/app/{RackRail.hpp => RailWidget.hpp} (61%) delete mode 100644 res/ComponentLibrary/RackBusboard.svg delete mode 100644 res/ComponentLibrary/RackRails.svg create mode 100644 res/ComponentLibrary/Rail.svg delete mode 100644 src/app/RackRail.cpp create mode 100644 src/app/RailWidget.cpp diff --git a/include/app/RackWidget.hpp b/include/app/RackWidget.hpp index d2782695..26a566f7 100644 --- a/include/app/RackWidget.hpp +++ b/include/app/RackWidget.hpp @@ -23,7 +23,6 @@ struct RackWidget : widget::OpaqueWidget { widget::Widget* moduleContainer; widget::Widget* cableContainer; CableWidget* incompleteCable = NULL; - widget::FramebufferWidget* railFb; /** The last mouse position in the RackWidget */ math::Vec mousePos; ParamWidget* touchedParam = NULL; diff --git a/include/app/RackRail.hpp b/include/app/RailWidget.hpp similarity index 61% rename from include/app/RackRail.hpp rename to include/app/RailWidget.hpp index f8e0a1e4..68959f25 100644 --- a/include/app/RackRail.hpp +++ b/include/app/RailWidget.hpp @@ -7,12 +7,12 @@ namespace rack { namespace app { -struct RackRail : widget::TransparentWidget { - std::shared_ptr busBoardSvg; - std::shared_ptr railsSvg; +struct RailWidget : widget::TransparentWidget { + std::shared_ptr svg; - RackRail(); + RailWidget(); void draw(const DrawArgs& args) override; + math::Vec getTileSize(); }; diff --git a/include/app/common.hpp b/include/app/common.hpp index 5350cad9..da0a683b 100644 --- a/include/app/common.hpp +++ b/include/app/common.hpp @@ -19,7 +19,6 @@ static const float RACK_GRID_WIDTH = 15; static const float RACK_GRID_HEIGHT = 380; static const math::Vec RACK_GRID_SIZE = math::Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT); static const math::Vec RACK_OFFSET = RACK_GRID_SIZE.mult(math::Vec(2000, 100)); -static const math::Vec BUS_BOARD_GRID_SIZE = math::Vec(RACK_GRID_WIDTH * 20, RACK_GRID_HEIGHT); } // namespace app diff --git a/include/math.hpp b/include/math.hpp index 1ca9a21f..1eee5630 100644 --- a/include/math.hpp +++ b/include/math.hpp @@ -236,6 +236,9 @@ struct Vec { float square() const { return x * x + y * y; } + float area() const { + return x * y; + } /** Rotates counterclockwise in radians. */ Vec rotate(float angle) { float sin = std::sin(angle); diff --git a/include/rack.hpp b/include/rack.hpp index 37b0f80f..16424230 100644 --- a/include/rack.hpp +++ b/include/rack.hpp @@ -57,7 +57,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/res/ComponentLibrary/RackBusboard.svg b/res/ComponentLibrary/RackBusboard.svg deleted file mode 100644 index 1bed970b..00000000 --- a/res/ComponentLibrary/RackBusboard.svg +++ /dev/null @@ -1,501 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/res/ComponentLibrary/RackRails.svg b/res/ComponentLibrary/RackRails.svg deleted file mode 100644 index c453e41f..00000000 --- a/res/ComponentLibrary/RackRails.svg +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/res/ComponentLibrary/Rail.svg b/res/ComponentLibrary/Rail.svg new file mode 100644 index 00000000..95c6593b --- /dev/null +++ b/res/ComponentLibrary/Rail.svg @@ -0,0 +1,566 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/app/RackRail.cpp b/src/app/RackRail.cpp deleted file mode 100644 index 9b5b363e..00000000 --- a/src/app/RackRail.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include -#include - - -namespace rack { -namespace app { - - -RackRail::RackRail() { - busBoardSvg = Svg::load(asset::system("res/ComponentLibrary/RackBusboard.svg")); - railsSvg = Svg::load(asset::system("res/ComponentLibrary/RackRails.svg")); - // DEBUG("%d %d %d", railsSvg->getNumShapes(), railsSvg->getNumPaths(), railsSvg->getNumPoints()); -} - - -void RackRail::draw(const DrawArgs& args) { - // Background color - nvgBeginPath(args.vg); - nvgRect(args.vg, 0.0, 0.0, box.size.x, box.size.y); - nvgFillColor(args.vg, nvgRGB(0x30, 0x30, 0x30)); - nvgFill(args.vg); - - // Rails - for (float y = 0; y < box.size.y; y += RACK_GRID_HEIGHT) { - const math::Vec busBoardSize = busBoardSvg->getSize(); - const float busBoardY = y + (RACK_GRID_HEIGHT - busBoardSize.y) / 2; - const NVGcolor shadowColor = nvgRGBA(0, 0, 0, 0x20); - - // Bus board shadow - nvgBeginPath(args.vg); - const float busBoardShadowY = busBoardY + busBoardSize.y; - const float busBoardShadowHeight = 10; - nvgRect(args.vg, 0, busBoardShadowY, box.size.x, busBoardShadowHeight); - nvgFillPaint(args.vg, nvgLinearGradient(args.vg, 0, busBoardShadowY, 0, busBoardShadowY + busBoardShadowHeight, shadowColor, color::BLACK_TRANSPARENT)); - nvgFill(args.vg); - - // Bus board - for (float x = 0; x < box.size.x; x += busBoardSize.x) { - nvgSave(args.vg); - nvgTranslate(args.vg, x, busBoardY); - busBoardSvg->draw(args.vg); - nvgRestore(args.vg); - } - - // Rails shadow - nvgBeginPath(args.vg); - const float railsShadowY = y + 15; - const float railsShadowHeight = 10; - nvgRect(args.vg, 0, railsShadowY, box.size.x, railsShadowHeight); - nvgFillPaint(args.vg, nvgLinearGradient(args.vg, 0, railsShadowY, 0, railsShadowY + railsShadowHeight, shadowColor, color::BLACK_TRANSPARENT)); - nvgFill(args.vg); - - // Rails - for (float x = 0; x < box.size.x; x += RACK_GRID_WIDTH) { - nvgSave(args.vg); - nvgTranslate(args.vg, x, y ); - railsSvg->draw(args.vg); - nvgRestore(args.vg); - } - } -} - - -} // namespace app -} // namespace rack diff --git a/src/app/RackWidget.cpp b/src/app/RackWidget.cpp index 3257e08a..16ff82f9 100644 --- a/src/app/RackWidget.cpp +++ b/src/app/RackWidget.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include @@ -72,18 +72,24 @@ struct CableContainer : widget::TransparentWidget { }; +struct RackWidget::Internal { + widget::FramebufferWidget* railFb; + app::RailWidget* rail; +}; + + RackWidget::RackWidget() { - // railFb = new widget::FramebufferWidget; - // railFb->box.size = math::Vec(); - // railFb->oversample = 1.0; - // // Don't redraw when the world offset of the rail FramebufferWidget changes its fractional value. - // railFb->dirtyOnSubpixelChange = false; - // { - // RackRail* rail = new RackRail; - // rail->box.size = math::Vec(); - // railFb->addChild(rail); - // } - // addChild(railFb); + internal = new Internal; + + internal->railFb = new widget::FramebufferWidget; + internal->railFb->box.size = math::Vec(); + internal->railFb->oversample = 1.0; + // Don't redraw when the world offset of the rail FramebufferWidget changes its fractional value. + internal->railFb->dirtyOnSubpixelChange = false; + addChild(internal->railFb); + + internal->rail = new RailWidget; + internal->railFb->addChild(internal->rail); moduleContainer = new ModuleContainer; addChild(moduleContainer); @@ -94,6 +100,7 @@ RackWidget::RackWidget() { RackWidget::~RackWidget() { clear(); + delete internal; } void RackWidget::step() { @@ -106,16 +113,15 @@ void RackWidget::draw(const DrawArgs& args) { nvgGlobalTint(args.vg, nvgRGBAf(b, b, b, 1)); // Resize and reposition the RackRail to align on the grid. - // math::Rect railBox; - // railBox.pos = args.clipBox.pos.div(BUS_BOARD_GRID_SIZE).floor().mult(BUS_BOARD_GRID_SIZE); - // railBox.size = args.clipBox.size.div(BUS_BOARD_GRID_SIZE).ceil().plus(math::Vec(1, 1)).mult(BUS_BOARD_GRID_SIZE); - // if (!railFb->box.size.equals(railBox.size)) { - // railFb->dirty = true; - // } - // railFb->box = railBox; - - // RackRail* rail = railFb->getFirstDescendantOfType(); - // rail->box.size = railFb->box.size; + math::Vec railSize = internal->rail->getTileSize(); + math::Rect railBox; + railBox.pos = args.clipBox.pos.div(railSize).floor().mult(railSize); + railBox.size = args.clipBox.size.div(railSize).ceil().plus(math::Vec(1, 1)).mult(railSize); + if (!internal->railFb->box.size.equals(railBox.size)) { + internal->railFb->setDirty(); + } + internal->railFb->box = railBox; + internal->rail->box.size = internal->railFb->box.size; Widget::draw(args); } diff --git a/src/app/RailWidget.cpp b/src/app/RailWidget.cpp new file mode 100644 index 00000000..c9cdeedd --- /dev/null +++ b/src/app/RailWidget.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include + + +namespace rack { +namespace app { + + +RailWidget::RailWidget() { + svg = Svg::load(asset::system("res/ComponentLibrary/Rail.svg")); + // DEBUG("%d %d %d", svg->getNumShapes(), svg->getNumPaths(), svg->getNumPoints()); +} + + +void RailWidget::draw(const DrawArgs& args) { + if (!svg) + return; + + math::Vec tileSize = getTileSize(); + if (tileSize.area() == 0.f) + return; + + for (float y = 0; y < box.size.y; y += tileSize.y) { + for (float x = 0; x < box.size.x; x += tileSize.x) { + nvgSave(args.vg); + nvgTranslate(args.vg, x, y); + svg->draw(args.vg); + nvgRestore(args.vg); + } + } + + Widget::draw(args); +} + + +math::Vec RailWidget::getTileSize() { + if (!svg) + return math::Vec(); + return svg->getSize().div(RACK_GRID_SIZE).round().mult(RACK_GRID_SIZE); +} + + +} // namespace app +} // namespace rack