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.

137 lines
4.0KB

  1. /*
  2. * DISTRHO Cardinal Plugin
  3. * Copyright (C) 2021-2022 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. #ifndef HEADLESS
  33. namespace asset {
  34. void updateForcingBlackSilverScrewMode(std::string slug);
  35. }
  36. #endif
  37. struct CardinalPluginModelHelper : plugin::Model {
  38. virtual app::ModuleWidget* createModuleWidgetFromEngineLoad(engine::Module* m) = 0;
  39. virtual void removeCachedModuleWidget(engine::Module* m) = 0;
  40. };
  41. template <class TModule, class TModuleWidget>
  42. struct CardinalPluginModel : CardinalPluginModelHelper
  43. {
  44. std::unordered_map<engine::Module*, TModuleWidget*> widgets;
  45. std::unordered_map<engine::Module*, bool> widgetNeedsDeletion;
  46. CardinalPluginModel(const std::string slug)
  47. {
  48. this->slug = slug;
  49. }
  50. engine::Module* createModule() override
  51. {
  52. engine::Module* const m = new TModule;
  53. m->model = this;
  54. return m;
  55. }
  56. app::ModuleWidget* createModuleWidget(engine::Module* const m) override
  57. {
  58. TModule* tm = nullptr;
  59. if (m)
  60. {
  61. DISTRHO_SAFE_ASSERT_RETURN(m->model == this, nullptr);
  62. if (widgets.find(m) != widgets.end())
  63. {
  64. widgetNeedsDeletion[m] = false;
  65. return widgets[m];
  66. }
  67. tm = dynamic_cast<TModule*>(m);
  68. }
  69. #ifndef HEADLESS
  70. asset::updateForcingBlackSilverScrewMode(slug);
  71. #endif
  72. app::ModuleWidget* const tmw = new TModuleWidget(tm);
  73. DISTRHO_CUSTOM_SAFE_ASSERT_RETURN(m != nullptr ? m->model->name.c_str() : "null", tmw->module == m, nullptr);
  74. tmw->setModel(this);
  75. return tmw;
  76. }
  77. app::ModuleWidget* createModuleWidgetFromEngineLoad(engine::Module* const m) override
  78. {
  79. DISTRHO_SAFE_ASSERT_RETURN(m != nullptr, nullptr);
  80. DISTRHO_SAFE_ASSERT_RETURN(m->model == this, nullptr);
  81. TModule* const tm = dynamic_cast<TModule*>(m);
  82. DISTRHO_SAFE_ASSERT_RETURN(tm != nullptr, nullptr);
  83. #ifndef HEADLESS
  84. asset::updateForcingBlackSilverScrewMode(slug);
  85. #endif
  86. TModuleWidget* const tmw = new TModuleWidget(tm);
  87. DISTRHO_SAFE_ASSERT_RETURN(tmw->module == m, nullptr);
  88. tmw->setModel(this);
  89. widgets[m] = tmw;
  90. widgetNeedsDeletion[m] = true;
  91. return tmw;
  92. }
  93. void removeCachedModuleWidget(engine::Module* const m) override
  94. {
  95. DISTRHO_SAFE_ASSERT_RETURN(m != nullptr,);
  96. DISTRHO_SAFE_ASSERT_RETURN(m->model == this,);
  97. if (widgets.find(m) == widgets.end())
  98. return;
  99. if (widgetNeedsDeletion[m])
  100. delete widgets[m];
  101. widgets.erase(m);
  102. widgetNeedsDeletion.erase(m);
  103. }
  104. };
  105. template <class TModule, class TModuleWidget>
  106. CardinalPluginModel<TModule, TModuleWidget>* createModel(const std::string slug)
  107. {
  108. return new CardinalPluginModel<TModule, TModuleWidget>(slug);
  109. }
  110. }
  111. #define createModel createModelOldVCV
  112. #include_next "helpers.hpp"
  113. #undef createModel