示例#1
0
 private static void LoadNodes(EasyGraphAsset easyGraphAsset, EasyGraphView easyGraphView)
 {
     foreach (var nodeData in easyGraphAsset.commonNodeData)
     {
         easyGraphView.LoadAndAddNode(nodeData);
     }
 }
示例#2
0
        private static void SaveNodesAndEdges(EasyGraphView easyGraphView, EasyGraphAsset easyGraphAsset)
        {
            var edges = ExtractEdges(easyGraphView).Where(x => x.input.node != null && x.output.node != null).ToArray();

            for (var i = 0; i < edges.Count(); i++)
            {
                var outputNode = edges[i].output.node as BaseNode;
                var inputNode  = edges[i].input.node as BaseNode;
                easyGraphAsset.edgeData.Add(new EdgeData
                {
                    sourcePort     = edges[i].output.portName,
                    targetPort     = edges[i].input.portName,
                    sourceNodeGUID = outputNode.GUID,
                    targetNodeGUID = inputNode.GUID,
                });
            }

            foreach (var node in ExtractNodes(easyGraphView))
            {
                easyGraphAsset.commonNodeData.Add(new CommonNodeData
                {
                    nodeGUID       = node.GUID,
                    nodeTypeId     = node.GetType().AssemblyQualifiedName,
                    serializedData = node.Serialize(),
                    position       = node.GetPosition().position
                });
            }
        }
示例#3
0
        public static void SaveGraphView(EasyGraphView easyGraphView, string guid)
        {
            string assetPath;

            if (string.IsNullOrEmpty(guid))
            {
                string filePath = EditorUtility.SaveFilePanel("where to save", ".", "new EasyGraph", "asset");

                if (string.IsNullOrEmpty(filePath))
                {
                    return;
                }

                if (File.Exists(filePath))
                {
                    Debug.Log("will overwrite");
                }
                assetPath = FileUtil.GetProjectRelativePath(filePath);
            }
            else
            {
                string path = AssetDatabase.GUIDToAssetPath(guid);
                assetPath = path;
            }

            var newAsset = ScriptableObject.CreateInstance <EasyGraphAsset>();

            SaveGraphView(easyGraphView, newAsset);

            AssetDatabase.CreateAsset(newAsset, assetPath);
            AssetDatabase.SaveAssets();
        }
示例#4
0
        public void Initialize(EditorWindow window, EasyGraphView graphView)
        {
            _window    = window;
            _graphView = graphView;


            _icon = new Texture2D(1, 1);
            _icon.SetPixel(0, 0, new Color(0, 0, 0, 0));
            _icon.Apply();
        }
示例#5
0
        private void CreateGraphView()
        {
            _graphView = new EasyGraphView(this)
            {
                name = "GraphViewDemo",
            };
            _graphView.StretchToParentSize();
            _graphView.deleteSelection += DeleteSelectionImplementation;

            rootVisualElement.Add(_graphView);
        }
示例#6
0
        private static void LoadCommentGroups(EasyGraphAsset easyGraphAsset, EasyGraphView easyGraphView)
        {
            foreach (var groupData in easyGraphAsset.commentGroupData)
            {
                var group = easyGraphView.CreateCommentGroup(
                    new Rect(groupData.position, EasyGraphView.CommentGroupSize),
                    groupData);

                group.AddElements(ExtractNodes(easyGraphView).Where(x => groupData.managedNodes.Contains(x.GUID)));
            }
        }
示例#7
0
        private static void ClearGraph(EasyGraphView easyGraphView)
        {
            var nodes = ExtractNodes(easyGraphView);

            foreach (var node in nodes)
            {
                var edges = ExtractEdges(easyGraphView);
                edges.Where(x => x.input.node == node || x.output.node == node).ToList()
                .ForEach(edge => easyGraphView.RemoveElement(edge));
                easyGraphView.RemoveElement(node);
            }
        }
示例#8
0
        public static void LoadGraphViewAsset(EasyGraphAsset easyGraphAsset, EasyGraphView easyGraphView)
        {
            if (easyGraphAsset == null)
            {
                Debug.LogError("failed to load");
                return;
            }

            ClearGraph(easyGraphView);
            LoadNodes(easyGraphAsset, easyGraphView);
            LoadEdges(easyGraphAsset, easyGraphView);
            LoadCommentGroups(easyGraphAsset, easyGraphView);
        }
示例#9
0
 private void CleanUp()
 {
     if (_graphView != null)
     {
         _graphView.deleteSelection -= DeleteSelectionImplementation;
         rootVisualElement.Remove(_graphView);
         _graphView = null;
     }
     if (_toolbar != null)
     {
         rootVisualElement.Remove(_toolbar);
         _toolbar = null;
     }
     _miniMap = null;
 }
示例#10
0
        private static void SaveCommentGroups(EasyGraphView easyGraphView, EasyGraphAsset easyGraphAsset)
        {
            var groups = ExtractGroups(easyGraphView);

            foreach (var group in groups)
            {
                var nodeGuidList = group.containedElements.Where(x => x is BaseNode).Cast <BaseNode>().Select(x => x.GUID).ToList();
                easyGraphAsset.commentGroupData.Add(new CommentGroupData
                {
                    managedNodes = nodeGuidList,
                    title        = group.title,
                    position     = group.GetPosition().position
                });
            }
        }
示例#11
0
        public static void LoadGraphViewAsset(EasyGraphView easyGraphView)
        {
            string filePath = EditorUtility.OpenFilePanel("where to load?", ".", "asset");

            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }

            if (!File.Exists(filePath))
            {
                Debug.LogError("not exist");
                return;
            }

            string assetPath      = FileUtil.GetProjectRelativePath(filePath);
            var    easyGraphAsset = AssetDatabase.LoadAssetAtPath <EasyGraphAsset>(assetPath);

            LoadGraphViewAsset(easyGraphAsset, easyGraphView);
        }
示例#12
0
        private static void LoadEdges(EasyGraphAsset easyGraphAsset, EasyGraphView easyGraphView)
        {
            cache.Clear();

            foreach (var node in ExtractNodes(easyGraphView))
            {
                cache.Add(node.GUID, node);
            }

            foreach (var e in easyGraphAsset.edgeData)
            {
                var sourceNode = cache[e.sourceNodeGUID];
                var targetNode = cache[e.targetNodeGUID];

                var sourcePort = sourceNode.outputContainer.Q <Port>(e.sourcePort);
                var targetPort = targetNode.inputContainer.Q <Port>(e.targetPort);

                var edge = MakeEdge(sourcePort, targetPort);
                easyGraphView.Add(edge);
            }

            cache.Clear();
        }
示例#13
0
 private static void SaveGraphView(EasyGraphView easyGraphView, EasyGraphAsset easyGraphAsset)
 {
     SaveNodesAndEdges(easyGraphView, easyGraphAsset);
     SaveCommentGroups(easyGraphView, easyGraphAsset);
 }
示例#14
0
 private static IEnumerable <Group> ExtractGroups(EasyGraphView easyGraphView)
 {
     return(easyGraphView.graphElements.ToList().Where(e => e is Group).Cast <Group>());
 }
示例#15
0
 private static IEnumerable <Edge> ExtractEdges(EasyGraphView easyGraphView)
 {
     return(easyGraphView.edges.ToList());
 }
示例#16
0
 private static IEnumerable <BaseNode> ExtractNodes(EasyGraphView easyGraphView)
 {
     return(easyGraphView.nodes.ToList().Where(n => n is BaseNode).Cast <BaseNode>());
 }