Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CrdtExamples.jucer
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
file="Source/crdtsimNetwork.cpp"/>
<FILE id="bqM8dy" name="crdtsimNetwork.h" compile="0" resource="0"
file="Source/crdtsimNetwork.h"/>
<FILE id="rPl5Gc" name="crdtsimNetworkComponent.cpp" compile="1" resource="0"
file="Source/crdtsimNetworkComponent.cpp"/>
<FILE id="NnhleM" name="crdtsimNetworkComponent.h" compile="0" resource="0"
file="Source/crdtsimNetworkComponent.h"/>
<FILE id="uUtJTh" name="crdtsimNode.cpp" compile="1" resource="0" file="Source/crdtsimNode.cpp"/>
<FILE id="ifcsQN" name="crdtsimNode.h" compile="0" resource="0" file="Source/crdtsimNode.h"/>
</GROUP>
Expand All @@ -26,9 +30,10 @@
<XCODE_MAC targetFolder="Builds/MacOSX" extraCompilerFlags="-Wall -Wextra -Wpedantic">
<CONFIGURATIONS>
<CONFIGURATION name="Debug" osxSDK="10.9 SDK" osxCompatibility="default" osxArchitecture="default"
isDebug="1" optimisation="1" targetName="CrdtExamples"/>
isDebug="1" optimisation="1" targetName="CrdtExamples" cppLanguageStandard="c++11"/>
<CONFIGURATION name="Release" osxSDK="10.9 SDK" osxCompatibility="default" osxArchitecture="default"
isDebug="0" optimisation="3" targetName="CrdtExamples" linkTimeOptimisation="1"/>
isDebug="0" optimisation="3" targetName="CrdtExamples" linkTimeOptimisation="1"
cppLanguageStandard="c++11"/>
</CONFIGURATIONS>
<MODULEPATHS>
<MODULEPATH id="juce_core" path="lib/JUCE/modules"/>
Expand Down
19 changes: 11 additions & 8 deletions Source/MainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#define MAINCOMPONENT_H_INCLUDED

#include "../JuceLibraryCode/JuceHeader.h"
#include "crdtsimNetworkComponent.h"
#include "crdtsimNetwork.h"

//==============================================================================
/*
Expand All @@ -22,6 +24,12 @@ class MainContentComponent : public AnimatedAppComponent
//==============================================================================
MainContentComponent ()
{
//MODEL
networkComponent.setNodesValueTree (network.getNodesValueTree ());
networkComponent.setConnexionsValueTree (network.getConnexionsValueTree ());
network.createNode ();
//VIEW
addAndMakeVisible (&networkComponent);
setSize (800, 600);
setFramesPerSecond (60);
}
Expand All @@ -47,18 +55,13 @@ class MainContentComponent : public AnimatedAppComponent

void resized () override
{
// This is called when the MainContentComponent is resized.
// If you add any child components, this is where you should
// update their positions.
networkComponent.setBounds (getLocalBounds ());
}


private:
//==============================================================================

// Your private member variables go here...


crdtsim::NetworkComponent networkComponent;
crdtsim::Network network;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};

Expand Down
3 changes: 3 additions & 0 deletions Source/crdtsimConnexionComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "crdtsimConnexionComponent.h"

using namespace crdtsim;
10 changes: 10 additions & 0 deletions Source/crdtsimConnexionComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef CRDTSIMCONNEXIONCOMPONENT_H_INCLUDED
#define CRDTSIMCONNEXIONCOMPONENT_H_INCLUDED
#include "JuceHeader.h"
namespace crdtsim
{
class NodeComponent : public juce::Component
{
};
} //namespace crdtsim
#endif // CRDTSIMCONNEXIONCOMPONENT_H_INCLUDED
57 changes: 56 additions & 1 deletion Source/crdtsimNetwork.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include "crdtsimNetwork.h"
#include "JuceHeader.h"
#include <algorithm>

namespace crdtsim
{
Network::Network () : valueTree ("RootNetwork") {}
int Network::size () const { return nodes.size (); }
int Network::createNode ()
{
auto newNode = Node{lastNodeIdentifier++};
nodes.push_back (newNode);
jassert (size () > 0);
getNodesValueTree ().getOrCreateChildWithName (getNodeIdentifier (nodes.back ().getIdentifier ()), nullptr);
jassert (valueTree.getNumChildren () > 0);
return nodes.back ().getIdentifier ();
}
const Node* Network::getNode (int identifier) const
Expand Down Expand Up @@ -39,6 +41,7 @@ bool Network::eraseNode (int identifier)
return false;
}
nodes.erase (nodeToRemodeIt, std::end (nodes));
getNodesValueTree ().removeChild (getNodesValueTree ().getChildWithName (getNodeIdentifier (identifier)), nullptr);
eraseAllConnexionsWithNode (identifier);
return true;
}
Expand All @@ -64,6 +67,7 @@ bool Network::createConnexion (int sourceIdentifier, int destinationIdentifier)
if (std::find (std::begin (connexions), std::end (connexions), connexionToAdd) == std::end (connexions))
{
connexions.push_back (connexionToAdd);
getConnexionsValueTree ().getOrCreateChildWithName (getConnexionIdentifier (sourceIdentifier, destinationIdentifier), nullptr);
}
return true;
}
Expand All @@ -77,6 +81,7 @@ bool Network::eraseConnexion (int sourceIdentifier, int destinationIdentifier)
return false;
}
connexions.erase (findResult);
getConnexionsValueTree ().removeChild (getConnexionsValueTree ().getChildWithName (getConnexionIdentifier (sourceIdentifier, destinationIdentifier)), nullptr);
return true;
}
bool Network::connexionExists (int sourceIdentifier, int destinationIdentifier)
Expand All @@ -98,6 +103,22 @@ void Network::eraseAllConnexionsWithNode (int nodeIdentifier)
};
connexions.erase (std::remove_if (std::begin (connexions), std::end (connexions), sameDestinationPredicate), std::end (connexions));
}
juce::ValueTree Network::getNodesValueTree ()
{
return valueTree.getOrCreateChildWithName ("NODES", nullptr);
}
juce::ValueTree Network::getConnexionsValueTree ()
{
return valueTree.getOrCreateChildWithName ("CONNEXIONS", nullptr);
}
juce::Identifier Network::getNodeIdentifier (int nodeIdentifier)
{
return juce::String ("NODE#" + std::to_string (nodeIdentifier));
}
juce::Identifier Network::getConnexionIdentifier (int sourceIdentifier, int destinationIdentifier)
{
return juce::String ("CONNEXION#" + std::to_string (sourceIdentifier) + "->" + std::to_string (destinationIdentifier));
}


class TestNetwork : public juce::UnitTest
Expand Down Expand Up @@ -228,6 +249,40 @@ class TestNetwork : public juce::UnitTest
expect (!network.connexionExists (nodeIdentifier1, nodeIdentifier2));
expect (!network.connexionExists (nodeIdentifier2, nodeIdentifier3));
}
{
beginTest ("Network creates a child to ValueTree when creating a node.");
Network network;
auto initialValueTreeSize = network.getNodesValueTree ().getNumChildren ();
network.createNode ();
expectEquals (network.getNodesValueTree ().getNumChildren (), initialValueTreeSize + 1, "Network hasn't taken care of its tree.");
}
{
beginTest ("Network deletes a child to ValueTree when deleting a node.");
Network network;
auto nodeId = network.createNode ();
auto initialValueTreeSize = network.getNodesValueTree ().getNumChildren ();
network.eraseNode (nodeId);
expectEquals (network.getNodesValueTree ().getNumChildren (), initialValueTreeSize - 1, "Network hasn't taken care of its tree.");
}
{
beginTest ("Network creates a child to ValueTree when creating a connexion.");
Network network;
auto nodeId1 = network.createNode ();
auto nodeId2 = network.createNode ();
auto initialValueTreeSize = network.getConnexionsValueTree ().getNumChildren ();
network.createConnexion (nodeId1, nodeId2);
expectEquals (network.getConnexionsValueTree ().getNumChildren (), initialValueTreeSize + 1, "Network hasn't taken care of its tree.");
}
{
beginTest ("Network deletes a child to ValueTree when deleting a connexion.");
Network network;
auto nodeId1 = network.createNode ();
auto nodeId2 = network.createNode ();
network.createConnexion (nodeId1, nodeId2);
auto initialValueTreeSize = network.getConnexionsValueTree ().getNumChildren ();
network.eraseConnexion (nodeId1, nodeId2);
expectEquals (network.getConnexionsValueTree ().getNumChildren (), initialValueTreeSize - 1, "Network hasn't taken care of its tree.");
}
}
} testTestNetwork;
}
7 changes: 7 additions & 0 deletions Source/crdtsimNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@
#include <vector>
#include "crdtsimNode.h"
#include "crdtsimConnexion.h"
#include "JuceHeader.h"
namespace crdtsim
{
class Network
{
public:
Network ();
int size () const;
int createNode ();
const Node* getNode (int identifier) const;
bool eraseNode (int identifier);
bool createConnexion (int sourceIdentifier, int destinationIdentifier);
bool eraseConnexion (int sourceIdentifier, int destinationIdentifier);
bool connexionExists (int sourceIdentifier, int destinationIdentifier);
juce::ValueTree getNodesValueTree ();
juce::ValueTree getConnexionsValueTree ();

private:
std::vector<Node> nodes;
int lastNodeIdentifier{0};
std::vector<Connexion> connexions;

void eraseAllConnexionsWithNode (int nodeIdentifier);
juce::ValueTree valueTree;
static juce::Identifier getNodeIdentifier (int nodeIdentifier);
static juce::Identifier getConnexionIdentifier (int sourceIdentifier, int destinationIdentifier);
};
} //crdtsim
#endif // NETWORK_H_INCLUDED
82 changes: 82 additions & 0 deletions Source/crdtsimNetworkComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "crdtsimNetworkComponent.h"

using namespace crdtsim;

class NodesMultiDocumentPanelWindow : public MultiDocumentPanelWindow
{
public:
NodesMultiDocumentPanelWindow (Colour backgroundColour) : MultiDocumentPanelWindow (backgroundColour)
{
setTitleBarButtonsRequired (DocumentWindow::closeButton, true);
}
};

NetworkComponent::NetworkComponent ()
{
useFullscreenWhenOneDocument (false);
setLayoutMode (LayoutMode::FloatingWindows);
}
bool NetworkComponent::tryToCloseDocument (juce::Component* component)
{
//TODO
}
void NetworkComponent::setNodesValueTree (juce::ValueTree newValueTree)
{
nodesValueTree = newValueTree;
nodesValueTree.addListener (this);
}
void NetworkComponent::setConnexionsValueTree (juce::ValueTree newValueTree)
{
connexionsValueTree = newValueTree;
connexionsValueTree.addListener (this);
}
void NetworkComponent::valueTreePropertyChanged (ValueTree& treeWhosePropertyHasChanged, const Identifier& property)
{
//TODO
}
void NetworkComponent::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childWhichHasBeenAdded)
{
if (parentTree == nodesValueTree)
{
auto identifier = childWhichHasBeenAdded.getType ();
auto newNote = new juce::TextButton{};
newNote->setButtonText (identifier.toString ());
newNote->setSize (100, 100);
addDocument (newNote, Colours::lightblue.withAlpha (0.6f), true);
}
else if (parentTree == connexionsValueTree)
{
//TODO
}
else
{
jassertfalse;
}
}
void NetworkComponent::valueTreeChildRemoved (ValueTree& parentTree, ValueTree& childWhichHasBeenRemoved, int /*indexFromWhichChildWasRemoved*/)
{
if (parentTree == nodesValueTree)
{
//TODO
}
else if (parentTree == connexionsValueTree)
{
//TODO
}
else
{
jassertfalse;
}
}
void NetworkComponent::valueTreeChildOrderChanged (ValueTree& parentTreeWhoseChildrenHaveMoved, int oldIndex, int newIndex)
{
//TODO
}
void NetworkComponent::valueTreeParentChanged (ValueTree& treeWhoseParentHasChanged)
{
//TODO
}
MultiDocumentPanelWindow* NetworkComponent::createNewDocumentWindow ()
{
return new NodesMultiDocumentPanelWindow (getBackgroundColour ());
}
26 changes: 26 additions & 0 deletions Source/crdtsimNetworkComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef CRDTSIMNETWORKCOMPONENT_H_INCLUDED
#define CRDTSIMNETWORKCOMPONENT_H_INCLUDED

#include "JuceHeader.h"
namespace crdtsim
{
class NetworkComponent : public juce::MultiDocumentPanel,
public juce::ValueTree::Listener
{
public:
NetworkComponent ();
bool tryToCloseDocument (juce::Component* component) override;
void setNodesValueTree (juce::ValueTree newValueTree);
void setConnexionsValueTree (juce::ValueTree newValueTree);
void valueTreePropertyChanged (ValueTree& treeWhosePropertyHasChanged, const Identifier& property) override;
void valueTreeChildAdded (ValueTree& parentTree, ValueTree& childWhichHasBeenAdded) override;
void valueTreeChildRemoved (ValueTree& parentTree, ValueTree& childWhichHasBeenRemoved, int indexFromWhichChildWasRemoved) override;
void valueTreeChildOrderChanged (ValueTree& parentTreeWhoseChildrenHaveMoved, int oldIndex, int newIndex) override;
void valueTreeParentChanged (ValueTree& treeWhoseParentHasChanged) override;
MultiDocumentPanelWindow* createNewDocumentWindow () override;

private:
juce::ValueTree nodesValueTree, connexionsValueTree;
};
} //namespace crdtsim
#endif // CRDTSIMNETWORKCOMPONENT_H_INCLUDED