Skip to content

Commit

Permalink
Fix Undo/Redo for Add and Paste actions that results in a new signal …
Browse files Browse the repository at this point in the history
…chain
  • Loading branch information
anjaldoshi committed Dec 13, 2023
1 parent b96bc82 commit d80edff
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
46 changes: 45 additions & 1 deletion Source/Processors/ProcessorGraph/ProcessorGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,13 @@ GenericProcessor* ProcessorGraph::createProcessor(Plugin::Description& descripti
} else {

GenericProcessor* rootSource = sourceNode;

if (sourceNode->isMerger())
{
Merger* merger = (Merger*) sourceNode;
rootSource = merger->getSourceNode(1);
}

while (rootSource->getSourceNode() != nullptr)
rootSource = rootSource->getSourceNode();

Expand Down Expand Up @@ -518,7 +525,12 @@ bool ProcessorGraph::checkForNewRootNodes(GenericProcessor* processor,

GenericProcessor* sourceA = merger->getSourceNode(0);
while (sourceA->getSourceNode() != nullptr)
sourceA = sourceA->getSourceNode();
{
if (sourceA->isMerger())
sourceA = ((Merger*) sourceA)->getSourceNode(1);
else
sourceA = sourceA->getSourceNode();
}

createEmptyProcessor(processor, rootNodes.indexOf(sourceA) + 1);

Expand Down Expand Up @@ -683,6 +695,7 @@ void ProcessorGraph::updateSettings(GenericProcessor* processor, bool signalChai
&& !processorToUpdate->getSourceNode()->isEmpty())
{
processorToUpdate = processorToUpdate->getSourceNode();
processor = processorToUpdate;
}

Array<Splitter*> splitters;
Expand Down Expand Up @@ -941,6 +954,37 @@ void ProcessorGraph::deleteNodes(Array<GenericProcessor*> processorsToDelete)
}
}


void ProcessorGraph::reconnectProcessors(int sourceNodeId,
int destNodeid)
{
GenericProcessor* sourceNode = getProcessorWithNodeId(sourceNodeId);
GenericProcessor* destNode = getProcessorWithNodeId(destNodeid);

// if dest node is a merger, switch to path that points to the processor being removed
if (sourceNode != nullptr && destNode != nullptr)
{
if (destNode->getSourceNode() != nullptr
&& destNode->getSourceNode()->isEmpty())
{
if (!isConsoleApp)
{
AccessClass::getGraphViewer()->removeNode(destNode->getSourceNode());
AccessClass::getEditorViewport()->removeEditor(destNode->getSourceNode()->getEditor());
}

rootNodes.remove(rootNodes.indexOf(destNode->getSourceNode()));
emptyProcessors.removeObject(destNode->getSourceNode());
}

sourceNode->setDestNode(destNode);
destNode->setSourceNode(sourceNode);

updateSettings(destNode);
}
}


void ProcessorGraph::clearSignalChain()
{

Expand Down
3 changes: 3 additions & 0 deletions Source/Processors/ProcessorGraph/ProcessorGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class TESTABLE ProcessorGraph : public AudioProcessorGraph
/* Creates connections in signal chain*/
void updateConnections();

/* Connects two processors if not already connected*/
void reconnectProcessors(int srcNodeId, int destNodeId);

/* Calls startAcquisition() for all processors*/
void startAcquisition();

Expand Down
23 changes: 19 additions & 4 deletions Source/Processors/ProcessorGraph/ProcessorGraphActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,31 @@ bool AddProcessor::undo()
settings = processorGraph->createNodeXml(processor, false);

processorGraph->deleteNodes(processorToDelete);

processorGraph->reconnectProcessors(sourceNodeId, destNodeId);
}

return true;
}


PasteProcessors::PasteProcessors(Array<XmlElement*> copyBuffer_,
int insertionPoint_) :
int insertionPoint_,
GenericProcessor* sourceProcessor,
GenericProcessor* destProcessor) :
processorGraph(AccessClass::getProcessorGraph()),
insertionPoint(insertionPoint_),
copyBuffer(copyBuffer_)
{

if (sourceProcessor != nullptr)
sourceNodeId = sourceProcessor->getNodeId();
else
sourceNodeId = -1;

if (destProcessor != nullptr)
destNodeId = destProcessor->getNodeId();
else
destNodeId = -1;
}

PasteProcessors::~PasteProcessors()
Expand All @@ -156,10 +168,11 @@ bool PasteProcessors::perform()
{
Array<GenericProcessor*> newProcessors;

for (int i = 0; i < copyBuffer.size(); i++)
// paste processors in reverse order so sources dont immediately create a new signal chain
for (int i = copyBuffer.size() - 1; i >= 0; i--)
{
newProcessors.add(processorGraph->createProcessorAtInsertionPoint(copyBuffer.getUnchecked(i),
insertionPoint++, true));
insertionPoint, true));
}

for (auto p : newProcessors)
Expand Down Expand Up @@ -190,6 +203,8 @@ bool PasteProcessors::undo()

processorGraph->deleteNodes(processorsToDelete);

processorGraph->reconnectProcessors(sourceNodeId, destNodeId);

nodeIds.clear();

return true;
Expand Down
10 changes: 9 additions & 1 deletion Source/Processors/ProcessorGraph/ProcessorGraphActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ class PasteProcessors : public UndoableAction

/** Constructor*/
PasteProcessors(Array<XmlElement*> copyBuffer,
int insertionPoint);
int insertionPoint,
GenericProcessor* source,
GenericProcessor* dest);

/** Destructor */
~PasteProcessors();
Expand All @@ -107,6 +109,12 @@ class PasteProcessors : public UndoableAction

Array<int> nodeIds;

// source node id of the first processor in the copy buffer
int sourceNodeId;

// destination node id of the last processor in the copy buffer
int destNodeId;

int insertionPoint;

ProcessorGraph* processorGraph;
Expand Down
14 changes: 13 additions & 1 deletion Source/UI/EditorViewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,19 @@ void EditorViewport::paste()

AccessClass::getProcessorGraph()->getUndoManager()->beginNewTransaction();

PasteProcessors* action = new PasteProcessors(processorInfo, insertionPoint);
GenericProcessor* source = nullptr;
GenericProcessor* dest = nullptr;
if (insertionPoint > 0)
{
source = editorArray[insertionPoint-1]->getProcessor();
}

if (editorArray.size() > insertionPoint)
{
dest = editorArray[insertionPoint]->getProcessor();
}

PasteProcessors* action = new PasteProcessors(processorInfo, insertionPoint, source, dest);

AccessClass::getProcessorGraph()->getUndoManager()->perform(action);

Expand Down

0 comments on commit d80edff

Please sign in to comment.