using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace MeadowGames.UINodeConnect4.UICSerialization
{
///
/// A data class with lists for SerializableNode and SerializableConnection.
///
[System.Serializable]
public class SerializableGraph
{
///
/// Constructor for the SerializableGraph
///
/// Elements to serialize
/// Element to serialize
/// Generate new unique SID for this SerializableNode
public SerializableGraph(List nodes, List connections, bool serializeWithNewSID = true)
{
ToSerializable(nodes, connections, serializeWithNewSID);
}
///
/// Constructor for the SerializableGraph
///
/// Elements to serialize
/// Generate new unique SID for this SerializableNode
public SerializableGraph(List elements, bool serializeWithNewSID = true)
{
ToSerializable(elements, serializeWithNewSID);
}
///
/// Constructor for the SerializableGraph
///
/// Elements to serialize
/// Generate new unique SID for this SerializableNode
public SerializableGraph(List elements, bool serializeWithNewSID = true)
{
ToSerializable(elements, serializeWithNewSID);
}
public List serializableNodes = new List();
public List serializableConnections = new List();
void ToSerializable(List nodes, List connections, bool serializeWithNewSID = true)
{
List elements = new List();
elements.AddRange(nodes.Cast().ToArray());
elements.AddRange(connections.Cast().ToArray());
ToSerializable(elements, serializeWithNewSID);
}
void ToSerializable(List elements, bool serializeWithNewSID = true)
{
ToSerializable(elements, serializeWithNewSID);
}
void ToSerializable(List elements, bool serializeWithNewSID = true)
{
ToSerializable(elements, serializeWithNewSID);
}
void ToSerializable(List elements, bool serializeWithNewSID = true)
{
Dictionary sIDPortMap = new Dictionary();
serializableNodes = new List();
List connections = new List();
foreach (T element in elements)
{
Node node = element as Node;
if (node)
{
SerializableNode serializableNode = new SerializableNode(node, serializeWithNewSID, false);
serializableNodes.Add(serializableNode);
serializableNode.serializablePorts = new List();
foreach (Port port in node.ports)
{
SerializablePort serializablePort = new SerializablePort(port, serializeWithNewSID);
serializablePort.nodeSID = serializableNode.sID;
serializableNode.serializablePorts.Add(serializablePort);
sIDPortMap.Add(port.SID, serializablePort.sID);
}
}
else
{
Connection connection = element as Connection;
if (connection != null)
{
connections.Add(connection);
}
}
}
serializableConnections = new List();
foreach (Connection connection in connections)
{
string p0 = sIDPortMap.TryGetValue(connection.port0.SID);
string p1 = sIDPortMap.TryGetValue(connection.port1.SID);
if (p0 != null && p1 != null)
{
SerializableConnection serializableConnection = new SerializableConnection(connection, serializeWithNewSID);
serializableConnection.port0SID = sIDPortMap[connection.port0.SID];
serializableConnection.port1SID = sIDPortMap[connection.port1.SID];
serializableConnections.Add(serializableConnection);
}
}
}
}
}