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.

120 lines
3.5KB

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