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.

122 lines
3.6KB

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