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.

125 lines
3.7KB

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