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.

103 lines
2.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  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); }