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.

127 lines
3.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. namespace juce
  18. {
  19. struct RuleBasedTopologySource::Internal : public TopologySource::Listener,
  20. private AsyncUpdater
  21. {
  22. Internal (RuleBasedTopologySource& da, TopologySource& bd) : owner (da), detector (bd)
  23. {
  24. detector.addListener (this);
  25. }
  26. ~Internal() override
  27. {
  28. detector.removeListener (this);
  29. }
  30. void clearRules()
  31. {
  32. if (! rules.isEmpty())
  33. {
  34. rules.clear();
  35. triggerAsyncUpdate();
  36. }
  37. }
  38. void addRule (Rule* r)
  39. {
  40. if (r != nullptr)
  41. {
  42. rules.add (r);
  43. triggerAsyncUpdate();
  44. }
  45. }
  46. void topologyChanged() override
  47. {
  48. cancelPendingUpdate();
  49. regenerateTopology();
  50. }
  51. void handleAsyncUpdate() override
  52. {
  53. topologyChanged();
  54. }
  55. void regenerateTopology()
  56. {
  57. auto newTopology = detector.getCurrentTopology();
  58. for (auto rule : rules)
  59. rule->transformTopology (newTopology);
  60. if (topology != newTopology)
  61. {
  62. topology = newTopology;
  63. owner.listeners.call ([] (TopologySource::Listener& l) { l.topologyChanged(); });
  64. }
  65. }
  66. void setActive (bool shouldBeActive)
  67. {
  68. detector.setActive (shouldBeActive);
  69. }
  70. bool isActive() const
  71. {
  72. return detector.isActive();
  73. }
  74. RuleBasedTopologySource& owner;
  75. TopologySource& detector;
  76. BlockTopology topology;
  77. OwnedArray<Rule> rules;
  78. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Internal)
  79. };
  80. RuleBasedTopologySource::RuleBasedTopologySource (TopologySource& d)
  81. {
  82. internal.reset (new Internal (*this, d));
  83. }
  84. RuleBasedTopologySource::~RuleBasedTopologySource()
  85. {
  86. internal = nullptr;
  87. }
  88. BlockTopology RuleBasedTopologySource::getCurrentTopology() const { return internal->topology; }
  89. void RuleBasedTopologySource::clearRules() { internal->clearRules(); }
  90. void RuleBasedTopologySource::addRule (Rule* r) { internal->addRule (r); }
  91. void RuleBasedTopologySource::setActive (bool shouldBeActive)
  92. {
  93. internal->setActive (shouldBeActive);
  94. }
  95. bool RuleBasedTopologySource::isActive() const
  96. {
  97. return internal->isActive();
  98. }
  99. } // namespace juce