Skip to content

Commit

Permalink
- fix #583
Browse files Browse the repository at this point in the history
- fix #544
  • Loading branch information
christoph-hart committed Oct 16, 2024
1 parent 4572cb0 commit bd6073b
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 161 deletions.
2 changes: 1 addition & 1 deletion currentGitHash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
66bdd1eb58f13d0b2b742d79533176365f78a784
4572cb0361e619914f05c8a26845f6310d53fcc8
2 changes: 1 addition & 1 deletion hi_backend/backend/currentGit.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define PREVIOUS_HISE_COMMIT "66bdd1eb58f13d0b2b742d79533176365f78a784"
#define PREVIOUS_HISE_COMMIT "4572cb0361e619914f05c8a26845f6310d53fcc8"
78 changes: 78 additions & 0 deletions hi_scripting/scripting/scriptnode/api/DspNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,84 @@ void HostHelpers::setNumDataObjectsFromValueTree(OpaqueNode& on, const ValueTree
}

#if USE_BACKEND
ValueTree DuplicateHelpers::findRoot(const ValueTree& v)
{
auto p = v.getParent();

if(!p.isValid())
return v;

return findRoot(p);
}

void DuplicateHelpers::removeOutsideConnections(const Array<ValueTree>& newNodes,
const Array<DspNetwork::IdChange>& idChanges)
{
for(auto& ct: newNodes)
{
Array<ValueTree> connectionsToRemove;

valuetree::Helpers::forEach(ct, [&](ValueTree& c)
{
if(c.getType() == PropertyIds::Connection)
{
auto thisTarget = c[PropertyIds::NodeId].toString();
auto found = false;

for(const auto& ch: idChanges)
{
found |= ch.newId == thisTarget;
}

if(!found)
connectionsToRemove.add(c);
}

return false;
});

for(const auto& c: connectionsToRemove)
c.getParent().removeChild(c, nullptr);
}
}

int DuplicateHelpers::getIndexInRoot(const ValueTree& v)
{
auto p = findRoot(v);

int index = 0;
auto iptr = &index;

valuetree::Helpers::forEach(p, [&iptr, v](ValueTree& c)
{
*iptr = *iptr + 1;
return c == v;
});

return index;
}

int DuplicateHelpers::compareElements(const WeakReference<NodeBase>& n1, const WeakReference<NodeBase>& n2)
{
if(n1 == n2)
return 0;

if(n1 != nullptr && n2 != nullptr)
{
auto i1 = getIndexInRoot(n1->getValueTree());
auto i2 = getIndexInRoot(n2->getValueTree());

if(i1 < i2)
return 1;
if(i1 > i2)
return -1;
}

jassertfalse;
return 0;
}


void DspNetworkListeners::DspNetworkGraphRootListener::onChangeStatic(DspNetworkGraphRootListener& l, NodeBase* n)
{
if(n != nullptr)
Expand Down
12 changes: 12 additions & 0 deletions hi_scripting/scripting/scriptnode/api/DspNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,18 @@ struct HostHelpers

struct DspNetworkGraph;

struct DuplicateHelpers
{
static ValueTree findRoot(const ValueTree& v);

static void removeOutsideConnections(const Array<ValueTree>& newNodes, const Array<DspNetwork::IdChange>& idChanges);

static int getIndexInRoot(const ValueTree& v);

// This sorts it reversed so that the index works when duplicating
static int compareElements(const WeakReference<NodeBase>& n1, const WeakReference<NodeBase>& n2);
};

struct DspNetworkListeners
{
struct DspNetworkGraphRootListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ TemplateNodeFactory::TemplateNodeFactory(DspNetwork* n) :

Array<DspNetwork::IdChange> changes;
auto newTree = n->cloneValueTreeWithNewIds(v, changes, false);
DuplicateHelpers::removeOutsideConnections({ newTree }, changes );

for(auto& c: changes)
n->changeNodeId(newTree, c.oldId, c.newId, nullptr);
Expand Down
188 changes: 37 additions & 151 deletions hi_scripting/scripting/scriptnode/ui/DspNetworkComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1582,12 +1582,28 @@ bool DspNetworkGraph::Actions::swapOrientation(DspNetworkGraph& g)

for (auto n : l)
{
if (auto sn = dynamic_cast<SerialNode*>(n.get()))
if(auto sn = dynamic_cast<SerialNode*>(n.get()))
{
sn->isVertical.storeValue(!sn->isVertical.getValue(), sn->getUndoManager());

auto dg = &g;

auto f = [sn, dg]()
{
if(auto b = dg->getComponent(sn))
{
auto cb = dg->getLocalArea(b, b->getLocalBounds());
dg->findParentComponentOfClass<ZoomableViewport>()->zoomToRectangle(cb);
}
};

MessageManager::callAsync(f);

return true;
}
}

g.setTransform({});
g.findParentComponentOfClass<ZoomableViewport>()->centerCanvas();


return true;
}
Expand Down Expand Up @@ -1712,99 +1728,6 @@ bool DspNetworkGraph::Actions::unfreezeNode(NodeBase::Ptr node)
}
}













#if 0
if (auto hc = node->getAsRestorableNode())
{
// Check if there is already a node that was unfrozen

for (auto n : node->getRootNetwork()->getListOfUnconnectedNodes())
{
if (n->getValueTree()[PropertyIds::FreezedId].toString() == node->getId())
{
auto newTree = n->getValueTree();
auto oldTree = node->getValueTree();
auto um = node->getUndoManager();

auto f = [oldTree, newTree, um]()
{
auto p = oldTree.getParent();

int position = p.indexOf(oldTree);
p.removeChild(oldTree, um);
p.addChild(newTree, position, um);
};

MessageManager::callAsync(f);

auto nw = node->getRootNetwork();

auto s = [newTree, nw]()
{
auto newNode = nw->getNodeForValueTree(newTree);
nw->deselectAll();
nw->addToSelection(newNode, ModifierKeys());
};

MessageManager::callAsync(s);

return true;
}
}

auto t = hc->getSnippetText();

if (t.isNotEmpty())
{
auto newTree = ValueTreeConverters::convertBase64ToValueTree(t, true);
newTree = node->getRootNetwork()->cloneValueTreeWithNewIds(newTree);
newTree.setProperty(PropertyIds::FreezedPath, node->getValueTree()[PropertyIds::FactoryPath], nullptr);
newTree.setProperty(PropertyIds::FreezedId, node->getId(), nullptr);

{
auto oldTree = node->getValueTree();
auto um = node->getUndoManager();

auto newNode = node->getRootNetwork()->createFromValueTree(true, newTree, true);

auto f = [oldTree, newTree, um]()
{
auto p = oldTree.getParent();

int position = p.indexOf(oldTree);
p.removeChild(oldTree, um);
p.addChild(newTree, position, um);
};

MessageManager::callAsync(f);

auto nw = node->getRootNetwork();

auto s = [newNode, nw]()
{
nw->deselectAll();
nw->addToSelection(newNode, ModifierKeys());
};

MessageManager::callAsync(s);
}
}

return true;
}
#endif

return false;
}

Expand Down Expand Up @@ -2526,69 +2449,20 @@ bool DspNetworkGraph::Actions::showKeyboardPopup(DspNetworkGraph& g, KeyboardPop
return true;
}

struct DuplicateHelpers
{
static ValueTree findRoot(const ValueTree& v)
{
auto p = v.getParent();

if(!p.isValid())
return v;

return findRoot(p);
}

static int getIndexInRoot(const ValueTree& v)
{
auto p = findRoot(v);

int index = 0;
auto iptr = &index;

valuetree::Helpers::forEach(p, [&iptr, v](ValueTree& c)
{
*iptr = *iptr + 1;
return c == v;
});

return index;
}

// This sorts it reversed so that the index works when duplicating
static int compareElements(const WeakReference<NodeBase>& n1, const WeakReference<NodeBase>& n2)
{
if(n1 == n2)
return 0;

if(n1 != nullptr && n2 != nullptr)
{
auto i1 = getIndexInRoot(n1->getValueTree());
auto i2 = getIndexInRoot(n2->getValueTree());

if(i1 < i2)
return 1;
if(i1 > i2)
return -1;
}

jassertfalse;
return 0;
}
};



bool DspNetworkGraph::Actions::duplicateSelection(DspNetworkGraph& g)
{
#if USE_BACKEND
int insertIndex = 0;

for (auto n : g.network->getSelection())
{
auto tree = n->getValueTree();
insertIndex = jmax(insertIndex, tree.getParent().indexOf(tree) + 1);
}




Array<DspNetwork::IdChange> changes;

struct TreeData
Expand All @@ -2599,7 +2473,6 @@ bool DspNetworkGraph::Actions::duplicateSelection(DspNetworkGraph& g)

auto sortedSelection = g.network->getSelection();


DuplicateHelpers h;

sortedSelection.sort(h);
Expand All @@ -2623,14 +2496,25 @@ bool DspNetworkGraph::Actions::duplicateSelection(DspNetworkGraph& g)
{
g.network->changeNodeId(ct.newTree, c.oldId, c.newId, nullptr);
}

}

Array<ValueTree> newNodes;

for(auto& ct: clonedTrees)
newNodes.add(ct.newTree);

h.removeOutsideConnections(newNodes, changes);

for(auto& ct: clonedTrees)
{
g.network->createFromValueTree(true, ct.newTree, true);
ct.location.getParent().addChild(ct.newTree, insertIndex, g.network->getUndoManager());
insertIndex = ct.location.getParent().indexOf(ct.newTree);
}

g.network->runPostInitFunctions();


#endif
return true;
}

Expand Down Expand Up @@ -3067,6 +2951,8 @@ void KeyboardPopup::addNodeAndClose(String path)
for(auto& c: changes)
network->changeNodeId(newTree, c.oldId, c.newId, nullptr);

BACKEND_ONLY(DuplicateHelpers::removeOutsideConnections(newTree, changes));

newNode = network->createFromValueTree(true, newTree, true);

auto as = dynamic_cast<AssignableObject*>(container);
Expand Down
3 changes: 1 addition & 2 deletions hi_scripting/scripting/scriptnode/ui/NodeComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,7 @@ void NodeComponent::paintOverChildren(Graphics& g)
if (isBeingCopied())
{
Path p;
p.loadPathFromData(HiBinaryData::ProcessorEditorHeaderIcons::addIcon,
sizeof(HiBinaryData::ProcessorEditorHeaderIcons::addIcon));
p.loadPathFromData(HiBinaryData::ProcessorEditorHeaderIcons::addIcon, HiBinaryData::ProcessorEditorHeaderIcons::addIcon_Size);

auto b = getLocalBounds().toFloat().withSizeKeepingCentre(32, 32);

Expand Down
Loading

0 comments on commit bd6073b

Please sign in to comment.