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.

99 lines
3.0KB

  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. namespace rack {
  31. struct CardinalPluginModelHelper {
  32. virtual ~CardinalPluginModelHelper() {}
  33. virtual void createCachedModuleWidget(rack::engine::Module* m) = 0;
  34. virtual void clearCachedModuleWidget(rack::engine::Module* m) = 0;
  35. };
  36. template <class TModule, class TModuleWidget>
  37. struct CardinalPluginModel : plugin::Model, CardinalPluginModelHelper
  38. {
  39. std::unordered_map<rack::engine::Module*, app::ModuleWidget*> widgets;
  40. rack::engine::Module* createModule() override
  41. {
  42. engine::Module* const m = new TModule;
  43. m->model = this;
  44. return m;
  45. }
  46. app::ModuleWidget* createModuleWidget(rack::engine::Module* const m) override
  47. {
  48. TModule* tm = NULL;
  49. if (m) {
  50. assert(m->model == this);
  51. if (widgets.find(m) != widgets.end())
  52. return widgets[m];
  53. tm = dynamic_cast<TModule*>(m);
  54. }
  55. app::ModuleWidget* mw = new TModuleWidget(tm);
  56. mw->setModel(this);
  57. return mw;
  58. }
  59. void createCachedModuleWidget(rack::engine::Module* const m) override
  60. {
  61. assert(m != nullptr); if (m == nullptr) return;
  62. assert(m->model == this); if (m->model != this) return;
  63. TModule* const tm = dynamic_cast<TModule*>(m);
  64. TModuleWidget* const mw = new TModuleWidget(tm);
  65. mw->setModel(this);
  66. widgets[m] = mw;
  67. }
  68. void clearCachedModuleWidget(rack::engine::Module* const m) override
  69. {
  70. assert(m != nullptr); if (m == nullptr) return;
  71. assert(m->model == this); if (m->model != this) return;
  72. widgets.erase(m);
  73. }
  74. };
  75. template <class TModule, class TModuleWidget>
  76. CardinalPluginModel<TModule, TModuleWidget>* createModel(std::string slug)
  77. {
  78. CardinalPluginModel<TModule, TModuleWidget>* const o = new CardinalPluginModel<TModule, TModuleWidget>();
  79. o->slug = slug;
  80. return o;
  81. }
  82. }
  83. #define createModel createModelOldVCV
  84. #include_next "helpers.hpp"