/// <summary> /// Manufactures a random diagram /// </summary> /// <param name="diagramName"></param> /// <returns></returns> Serializablediagram IdiagramSerializer.Loaddiagram(string diagramName) { var diagram = _saveddiagrams.SingleOrDefault(s => s.diagramName == diagramName); return(diagram); // create a random diagram var numberOfNodes = UnityEngine.Random.Range(10, 40); var numberOfConnectors = UnityEngine.Random.Range(numberOfNodes * 2, numberOfNodes * 5); var nodeList = new List <SerializableNode>(); for (int i = 0; i < numberOfNodes; i++) { var newNode = new SerializableNode(Guid.NewGuid(), i.ToString(), "", Color.blue); nodeList.Add(newNode); } var connectorList = new List <SerializableConnector>(); for (int i = 0; i < numberOfConnectors; i++) { var nodeInd = UnityEngine.Random.Range(0, numberOfNodes); var originNode = nodeList[nodeInd]; nodeInd = UnityEngine.Random.Range(0, numberOfNodes); var endNode = nodeList[nodeInd]; var newConnector = new SerializableConnector(originNode.uniqueID, endNode.uniqueID); connectorList.Add(newConnector); } var sdiagram = new Serializablediagram("Random diagram - " + Guid.NewGuid().ToString(), nodeList, connectorList); return(sdiagram); }
/// <summary> /// Initialises this diagram with a serializable diagram /// </summary> /// <param name="serialized"></param> public void Initialisediagram(Serializablediagram serialized) { if (_initialised) { throw new System.Exception("Trying to use 'Initialise diagram' on an already initialized diagram"); } StartCoroutine(Populatediagram(serialized)); _initialised = true; }
/// <summary> /// Creates a diagram from the serialized data given /// </summary> /// <param name="serializeddiagram"></param> /// <returns></returns> public static Diagram Creatediagram(Serializablediagram serializeddiagram) { GameObject diagramObj = new GameObject(serializeddiagram.diagramName + " root object"); // create the diagram var diagram = diagramObj.AddComponent <Diagram>(); diagram.diagramName = serializeddiagram.diagramName; diagram.Initialisediagram(serializeddiagram); return(diagram); }
/// <summary> /// Coroutine that progressively creates nodes and the connects them /// </summary> /// <param name="serialized"></param> /// <returns></returns> private IEnumerator Populatediagram(Serializablediagram serialized) { // create nodes var nodes = serialized.nodes; foreach (var node in nodes) { var newNode = NodeFactory.CreateNode(this, node); _nodes.Add(newNode.UniqueID, newNode); } // create connectors var connectors = serialized.connectors; foreach (var connector in connectors) { var newConnector = ConnectorFactory.CreateConnector(this, connector); } yield return(null); }
/// <summary> /// Saves a diagram, this will only last for this session /// </summary> /// <param name="diagram"></param> void IdiagramSerializer.Savediagram(Serializablediagram diagram) { _saveddiagrams.Add(diagram); }