The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

105 lines
2.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. struct RuleBasedTopologySource::Internal : public TopologySource::Listener,
  18. private juce::AsyncUpdater
  19. {
  20. Internal (RuleBasedTopologySource& da, TopologySource& bd) : owner (da), detector (bd)
  21. {
  22. detector.addListener (this);
  23. }
  24. ~Internal()
  25. {
  26. detector.removeListener (this);
  27. }
  28. void clearRules()
  29. {
  30. if (! rules.isEmpty())
  31. {
  32. rules.clear();
  33. triggerAsyncUpdate();
  34. }
  35. }
  36. void addRule (Rule* r)
  37. {
  38. if (r != nullptr)
  39. {
  40. rules.add (r);
  41. triggerAsyncUpdate();
  42. }
  43. }
  44. void topologyChanged() override
  45. {
  46. cancelPendingUpdate();
  47. regenerateTopology();
  48. }
  49. void handleAsyncUpdate() override
  50. {
  51. topologyChanged();
  52. }
  53. void regenerateTopology()
  54. {
  55. auto newTopology = detector.getCurrentTopology();
  56. for (auto rule : rules)
  57. rule->transformTopology (newTopology);
  58. if (topology != newTopology)
  59. {
  60. topology = newTopology;
  61. owner.listeners.call (&TopologySource::Listener::topologyChanged);
  62. }
  63. }
  64. RuleBasedTopologySource& owner;
  65. TopologySource& detector;
  66. BlockTopology topology;
  67. juce::OwnedArray<Rule> rules;
  68. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Internal)
  69. };
  70. RuleBasedTopologySource::RuleBasedTopologySource (TopologySource& d)
  71. {
  72. internal = new Internal (*this, d);
  73. }
  74. RuleBasedTopologySource::~RuleBasedTopologySource()
  75. {
  76. internal = nullptr;
  77. }
  78. BlockTopology RuleBasedTopologySource::getCurrentTopology() const { return internal->topology; }
  79. void RuleBasedTopologySource::clearRules() { internal->clearRules(); }
  80. void RuleBasedTopologySource::addRule (Rule* r) { internal->addRule (r); }