Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Sorter
- {
- public:
- static int compareElements(const juce::ValueTree& a, const juce::ValueTree& b)
- {
- int apos = static_cast<int>(a.getProperty("position"));
- int bpos = static_cast<int>(b.getProperty("position"));
- return (apos < bpos) ? -1 : ((bpos < apos) ? 1 : 0);
- }
- };
- struct Parent : public ValueTree::Listener
- {
- Parent(UndoManager* u)
- : tree(ValueTree("parent")),
- undo(u)
- {
- tree.addListener(this);
- }
- void valueTreePropertyChanged(juce::ValueTree& s, const juce::Identifier& p) override {}
- void valueTreeChildRemoved(juce::ValueTree& t, juce::ValueTree& t1, int i) override {}
- void valueTreeChildOrderChanged(juce::ValueTree& t, int i, int i1) override {}
- void valueTreeParentChanged(juce::ValueTree& t) override {}
- void valueTreeChildAdded(juce::ValueTree& p, juce::ValueTree& c) override
- {
- Sorter s;
- tree.sort(s, undo, false);
- }
- void add(ValueTree c)
- {
- tree.addChild(c, -1, undo);
- }
- ValueTree tree;
- UndoManager* undo;
- };
- int main(int argc, char** argv)
- {
- UndoManager undoManager;
- Parent parent(&undoManager);
- parent.add(ValueTree{"child1", {{ "position", 0}}});
- parent.add(ValueTree{"child2", {{ "position", 1000}}});
- undoManager.beginNewTransaction();
- parent.add(ValueTree{"child3", {{ "position", 500}}});
- std::cout << parent.tree.toXmlString() << "\n";
- undoManager.undo();
- std::cout << parent.tree.toXmlString() << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement