You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.8KB

  1. /*
  2. * DISTRHO Cardinal Plugin
  3. * Copyright (C) 2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 3 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the LICENSE file.
  16. */
  17. /**
  18. * This file is an edited version of VCVRack's helpers.hpp
  19. * Copyright (C) 2016-2021 VCV.
  20. *
  21. * This program is free software: you can redistribute it and/or
  22. * modify it under the terms of the GNU General Public License as
  23. * published by the Free Software Foundation; either version 3 of
  24. * the License, or (at your option) any later version.
  25. */
  26. #pragma once
  27. #include <app/ModuleWidget.hpp>
  28. #include <engine/Module.hpp>
  29. #include <unordered_map>
  30. #include "DistrhoUtils.hpp"
  31. namespace rack {
  32. namespace asset {
  33. void updateForcingBlackSilverScrewMode(std::string slug);
  34. }
  35. struct CardinalPluginModelHelper : plugin::Model {
  36. virtual app::ModuleWidget* createModuleWidgetFromEngineLoad(engine::Module* m) = 0;
  37. virtual void removeCachedModuleWidget(engine::Module* m) = 0;
  38. };
  39. template <class TModule, class TModuleWidget>
  40. struct CardinalPluginModel : CardinalPluginModelHelper
  41. {
  42. std::unordered_map<engine::Module*, TModuleWidget*> widgets;
  43. std::unordered_map<engine::Module*, bool> widgetNeedsDeletion;
  44. CardinalPluginModel(const std::string slug)
  45. {
  46. this->slug = slug;
  47. }
  48. engine::Module* createModule() override
  49. {
  50. engine::Module* const m = new TModule;
  51. m->model = this;
  52. return m;
  53. }
  54. app::ModuleWidget* createModuleWidget(engine::Module* const m) override
  55. {
  56. TModule* tm = nullptr;
  57. if (m)
  58. {
  59. DISTRHO_SAFE_ASSERT_RETURN(m->model == this, nullptr);
  60. if (widgets.find(m) != widgets.end())
  61. {
  62. widgetNeedsDeletion[m] = false;
  63. return widgets[m];
  64. }
  65. tm = dynamic_cast<TModule*>(m);
  66. }
  67. asset::updateForcingBlackSilverScrewMode(slug);
  68. app::ModuleWidget* const tmw = new TModuleWidget(tm);
  69. DISTRHO_CUSTOM_SAFE_ASSERT_RETURN(m != nullptr ? m->model->name.c_str() : "null", tmw->module == m, nullptr);
  70. tmw->setModel(this);
  71. return tmw;
  72. }
  73. app::ModuleWidget* createModuleWidgetFromEngineLoad(engine::Module* const m) override
  74. {
  75. DISTRHO_SAFE_ASSERT_RETURN(m != nullptr, nullptr);
  76. DISTRHO_SAFE_ASSERT_RETURN(m->model == this, nullptr);
  77. TModule* const tm = dynamic_cast<TModule*>(m);
  78. DISTRHO_SAFE_ASSERT_RETURN(tm != nullptr, nullptr);
  79. asset::updateForcingBlackSilverScrewMode(slug);
  80. TModuleWidget* const tmw = new TModuleWidget(tm);
  81. DISTRHO_SAFE_ASSERT_RETURN(tmw->module == m, nullptr);
  82. tmw->setModel(this);
  83. widgets[m] = tmw;
  84. widgetNeedsDeletion[m] = true;
  85. return tmw;
  86. }
  87. void removeCachedModuleWidget(engine::Module* const m) override
  88. {
  89. DISTRHO_SAFE_ASSERT_RETURN(m != nullptr,);
  90. DISTRHO_SAFE_ASSERT_RETURN(m->model == this,);
  91. if (widgets.find(m) == widgets.end())
  92. return;
  93. if (widgetNeedsDeletion[m])
  94. delete widgets[m];
  95. widgets.erase(m);
  96. widgetNeedsDeletion.erase(m);
  97. }
  98. };
  99. template <class TModule, class TModuleWidget>
  100. CardinalPluginModel<TModule, TModuleWidget>* createModel(const std::string slug)
  101. {
  102. return new CardinalPluginModel<TModule, TModuleWidget>(slug);
  103. }
  104. }
  105. #define createModel createModelOldVCV
  106. #include_next "helpers.hpp"
  107. #undef createModel