public static uCanvas LoadNodeCanvas(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                Debug.LogWarning("Cannot load: path is null");
                return(null);
            }

            // Fetch all objects in the save file
            ScriptableObject[] objects = ResourceManager.LoadResources <ScriptableObject>(path);
            if (objects == null || objects.Length == 0)
            {
                throw new UnityException("Cannot Load NodeCanvas: The specified path '" + path + "' does not point to a save file!");
            }

            // Filter out the NodeCanvas out of these objects
            uCanvas nodeCanvas = objects.Single((ScriptableObject obj) => (obj as uCanvas) != null) as uCanvas;

            if (nodeCanvas == null)
            {
                throw new UnityException("Cannot Load NodeCanvas: The file at the specified path '" + path + "' is no valid save file as it does not contain a NodeCanvas!");
            }

            uCanvas copy = CreateWorkingCopy(nodeCanvas);

            // Path
            copy.Path = path;

            // Returning
            return(copy);
        }
 // Canvas functions
 public void SaveCanvas(string path)
 {
     if (currentCanvas == null)
     {
         return;
     }
     currentCanvas = uNodeEditorSaveManager.SaveNodeCanvas(path, currentCanvas);
 }
 public uNodeManager()
 {
     currentCanvas = null;
     if (uNodeEditorState.savedCanvas != null)
     {
         currentCanvas = uNodeEditorState.savedCanvas;
     }
     uNodeEditorSaveManager.Flush();
 }
 public void AddConnection(uNode parent, uNode child)
 {
     if (IsValidConnection(parent, child))
     {
         parent.AddChild(child);
         child.Parent  = parent;
         currentCanvas = uNodeEditorSaveManager.SaveCanvasState(currentCanvas);
     }
 }
 public void RemoveConnection(uNode child)
 {
     if (child.Parent != null)
     {
         child.Parent.RemoveChild(child);
         child.Parent  = null;
         currentCanvas = uNodeEditorSaveManager.SaveCanvasState(currentCanvas);
     }
 }
 public void NewCanvas()
 {
     if (currentCanvas != null)
     {
         uNodeEditorSaveManager.Flush();
         // @To do ask for confirmation to clear the current canvas
         // Ask for save (?)
     }
     currentCanvas = ScriptableObject.CreateInstance <uCanvas>();
     currentCanvas.Init("NewCancas");
     currentCanvas = uNodeEditorSaveManager.SaveCanvasState(currentCanvas);
 }
        public void LoadCanvas(string path)
        {
            uCanvas c = uNodeEditorSaveManager.LoadNodeCanvas(path);

            if (c == null)
            {
                return;
            }
            currentCanvas = c;
            for (int i = 0; i < currentCanvas.Nodes.Count; i++)
            {
                currentCanvas.Nodes[i].InputKnob  = new uKnob(currentCanvas.Nodes[i]);
                currentCanvas.Nodes[i].OutputKnob = new uKnob(currentCanvas.Nodes[i]);
            }
            uNodeEditorSaveManager.Flush();
        }
        public void CreateNewNode()
        {
            if (currentCanvas == null)
            {
                return;
            }
            uNode   n = ScriptableObject.CreateInstance <uTerrainNode>();
            Vector2 p = uNodeEditor.editor.CanvasWindowRect.Contains(uNodeEditorState.mousePosition)
                            ? uNodeEditorState.mousePosition
                            : new Vector2(200f, 200f);

            // @ To do : scale p with EditorZoom
            n.Init("Node" + currentCanvas.Nodes.Count, p);
            currentCanvas.Nodes.Add(n);
            currentCanvas = uNodeEditorSaveManager.SaveCanvasState(currentCanvas);
        }
        public static uCanvas CreateWorkingCopy(uCanvas canvas)
        {
            uCanvas ret = ScriptableObject.CreateInstance <uCanvas>();

            ret.Init(canvas.Name);

            // Copy each node
            foreach (uNode n in canvas.Nodes)
            {
                uNode copy = n.GetCopy();
                ret.Nodes.Add(copy);
            }

            // Restore hirearchy
            ret.SetParentFromCopy();

            return(ret);
        }
 public void DeleteNode(uNode n)
 {
     currentCanvas.Nodes.Remove(n);
     if (n.Parent != null)
     {
         n.Parent.Childrens.Remove(n);
     }
     if (n.Childrens.Count > 0)
     {
         for (int i = 0; i < n.Childrens.Count; i++)
         {
             n.Childrens[i].Parent = null;
         }
         n.Childrens.Clear();
     }
     Object.DestroyImmediate(n);
     currentCanvas = uNodeEditorSaveManager.SaveCanvasState(currentCanvas);
 }
        public static uCanvas SaveCanvasState(uCanvas c)
        {
            // New branch created
            if (currentIndex + 1 < canvasStates.Count)
            {
                for (int i = currentIndex + 1; i < canvasStates.Count; i++)
                {
                    canvasStates.RemoveAt(i);
                }
            }
            canvasStates.Add(CreateWorkingCopy(c));
            currentIndex++;

            // Maximum amout of canvas saved at runtime
            if (canvasStates.Count > uNodeEditorSettings.maxRuntimeCanvasSaved)
            {
                canvasStates.RemoveAt(0);
                currentIndex--;
            }

            return(c);
        }
        // Native save
        public static uCanvas SaveNodeCanvas(string path, uCanvas nodeCanvas)
        {
            if (string.IsNullOrEmpty(path))
            {
                Debug.LogWarning("Cannot save: No path to save");
                return(nodeCanvas);
            }
            if (nodeCanvas == null)
            {
                Debug.LogWarning("Cannot save: The NodeCanvas is null!");
                return(null);
            }

            string p = path;

            path = path.Replace(Application.dataPath, "Assets");

            // Write canvas
            AssetDatabase.DeleteAsset(path);
            AssetDatabase.CreateAsset(nodeCanvas, path);

            // Write nodes + contents
            for (int i = 0; i < nodeCanvas.Nodes.Count; i++)
            {
                AddSubAsset(nodeCanvas.Nodes[i], nodeCanvas);
            }

            // Saving
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            // Returning a copy
            uCanvas ret = CreateWorkingCopy(nodeCanvas);

            ret.Path = p;
            return(ret);
        }
 public void AddNewNode(uNode n)
 {
     currentCanvas.Nodes.Add(n);
     currentCanvas = uNodeEditorSaveManager.SaveCanvasState(currentCanvas);
 }
Esempio n. 14
0
        private void ProcessEvents()
        {
            // Getting event info
            Event e = Event.current;

            if (e == null || !CanvasWindowRect.Contains(e.mousePosition))
            {
                return;
            }
            uNodeEditorState.mousePosition = e.mousePosition;
            bool leftClick = (e.button == 0), rightClick = (e.button == 1);

            uNodeEditorState.focusedNode        = manager.NodeAtPosition(uNodeEditorState.mousePosition);
            uNodeEditorState.selectedInputKnob  = manager.InputKnobAtPosition(uNodeEditorState.mousePosition);
            uNodeEditorState.selectedOutputKnob = manager.OutputKnobAtPosition(uNodeEditorState.mousePosition);

            // Event processing
            switch (e.type)
            {
            case EventType.MouseDrag:
                // Drag selected node
                if (leftClick && uNodeEditorState.selectedNode != null && uNodeEditorState.outputKnobSaved == null)
                {
                    uNodeEditorState.mouseDelta = e.delta * (1f / uNodeEditorState.zoom);
                    manager.MoveSelectedNode();
                }
                // Drawing a connection line
                if (leftClick && uNodeEditorState.outputKnobSaved != null)
                {
                    uNodeEditorState.currentLineDrawn = new uLine();
                    Vector2 mousePosWithZoom = uNodeEditorState.mousePosition * (1f / uNodeEditorState.zoom);
                    uNodeEditorState.currentLineDrawn.start = new Vector3(uNodeEditorState.outputKnobSaved.Rect.x,
                                                                          uNodeEditorState.outputKnobSaved.Rect.y,
                                                                          0f);
                    uNodeEditorState.currentLineDrawn.end = new Vector3(mousePosWithZoom.x,
                                                                        mousePosWithZoom.y,
                                                                        0f);
                }
                // Left click on canvas : move around canvas
                else if (leftClick && uNodeEditorState.outputKnobSaved == null && uNodeEditorState.selectedNode == null)
                {
                    uNodeEditorState.mouseDelta = e.delta * (1f / uNodeEditorState.zoom);
                    manager.MoveNodesAlongMouse();
                }
                break;

            case EventType.MouseUp:
                // Connection began in event MouseDrag, and release on an input connection of another node
                // Try to create the connection between the two
                if (uNodeEditorState.outputKnobSaved != null && uNodeEditorState.selectedInputKnob != null)
                {
                    manager.AddConnection(uNodeEditorState.outputKnobSaved.Parent, uNodeEditorState.selectedInputKnob.Parent);
                }
                // Click on a outputKnob without drawing : Collapse/Show children
                else if (uNodeEditorState.outputKnobSaved != null && uNodeEditorState.selectedInputKnob == null &&
                         uNodeEditorState.selectedOutputKnob == uNodeEditorState.outputKnobSaved)
                {
                    uNodeEditorState.selectedOutputKnob.ChangeShowState();
                }
                else if (uNodeEditorState.selectedNode != null)
                {
                    manager.CurrentCanvas = uNodeEditorSaveManager.SaveCanvasState(manager.CurrentCanvas);
                }

                // Release selected elements
                uNodeEditorState.currentLineDrawn = null;
                uNodeEditorState.selectedNode     = null;
                uNodeEditorState.outputKnobSaved  = null;
                break;

            case EventType.MouseDown:
                // Click on node output : Connection creation begin
                if (uNodeEditorState.selectedOutputKnob != null)
                {
                    uNodeEditorState.outputKnobSaved = uNodeEditorState.selectedOutputKnob;
                }
                // Click on node input : remove all connection
                if (uNodeEditorState.selectedInputKnob != null)
                {
                    manager.RemoveConnection(uNodeEditorState.selectedInputKnob.Parent);
                }
                // Click on node : select it
                else if (uNodeEditorState.focusedNode != null && uNodeEditorState.selectedNode == null)
                {
                    uNodeEditorState.selectedNode = uNodeEditorState.focusedNode;
                }

                // Context clicked on a node's property
                //if (rightClick && uNodeEditorState.focusedNode != null)
                //{

                //}
                // Context menu when clicked on canvas
                if (rightClick && uNodeEditorState.focusedNode == null)
                {
                    manager.ShowCanvasGenericMenu();
                }
                // Context menu when clicked on node
                else if (rightClick && uNodeEditorState.focusedNode != null)
                {
                    manager.ShowNodeGenericMenu();
                }
                break;

            case EventType.ScrollWheel:
                uNodeEditorState.zoom = Mathf.Min(uNodeEditorSettings.maxZoom, Mathf.Max(uNodeEditorState.zoom - e.delta.y
                                                                                         / uNodeEditorSettings.zoomSpeedFactor,
                                                                                         uNodeEditorSettings.minZoom));
                break;

            case EventType.KeyDown:
                if (e.shift && e.keyCode == KeyCode.S && manager.CurrentCanvas != null)
                {
                    manager.SaveCanvas(manager.CurrentCanvas.Path);
                }
                if (e.shift && e.keyCode == KeyCode.Z && manager.CurrentCanvas != null)
                {
                    uCanvas n = uNodeEditorSaveManager.GetBeforeState();
                    if (n != null)
                    {
                        manager.CurrentCanvas = n;
                    }
                }
                else if (e.shift && e.keyCode == KeyCode.Y && manager.CurrentCanvas != null)
                {
                    uCanvas n = uNodeEditorSaveManager.GetNextState();
                    if (n != null)
                    {
                        manager.CurrentCanvas = n;
                    }
                }
                break;
            }
            Repaint();
        }
Esempio n. 15
0
        private void DrawSideWindow()
        {
            sideWindowWidth = Mathf.Min(600, Mathf.Max(200, (int)(position.width / 5)));
            GUILayout.BeginArea(SideWindowRect, GUI.skin.box);

            // Management
            GUILayout.Label(new GUIContent("Management"), uNodeEditorSettings.boldStyle);
            if (GUILayout.Button(new GUIContent("New Canvas", "Loads an empty Canvas")))
            {
                manager.NewCanvas();
                flagRepainting = true;
            }

            if (GUILayout.Button(new GUIContent("Load Canvas", "Loads the Canvas")))
            {
                string path = EditorUtility.OpenFilePanel("Load Canvas", "Assets/", "asset");
                manager.LoadCanvas(path);
            }

            if (GUILayout.Button(new GUIContent("Save As", "Saves the Canvas")) && manager.CurrentCanvas != null)
            {
                string path = EditorUtility.SaveFilePanelInProject("Save Canvas", "test", "asset", "Save Canvas");
                manager.SaveCanvas(path);
            }

            if (GUILayout.Button(new GUIContent("Save", "Saves the Canvas")) && manager.CurrentCanvas != null)
            {
                manager.SaveCanvas(manager.CurrentCanvas.Path);
            }


            // Actions
            GUILayout.Space(20);
            GUILayout.Label(new GUIContent("Actions"), uNodeEditorSettings.boldStyle);
            if (GUILayout.Button(new GUIContent("New Node", "Creates new uNode")) && manager.CurrentCanvas != null)
            {
                manager.CreateNewNode();
                flagRepainting = true;
            }

            if (GUILayout.Button(new GUIContent("Undo", "Undo Last Action")) && manager.CurrentCanvas != null)
            {
                uCanvas n = uNodeEditorSaveManager.GetBeforeState();
                if (n != null)
                {
                    manager.CurrentCanvas = n;
                    flagRepainting        = true;
                }
            }

            if (GUILayout.Button(new GUIContent("Redo", "Redo Last Action")) && manager.CurrentCanvas != null)
            {
                uCanvas n = uNodeEditorSaveManager.GetNextState();
                if (n != null)
                {
                    manager.CurrentCanvas = n;
                    flagRepainting        = true;
                }
            }

            if (GUILayout.Button(new GUIContent("Settings", "Open Settings Panel")))
            {
                uNodeSettingsWindow.ShowWindow(this);
            }


            // Information
            GUILayout.Space(20);
            GUILayout.Label("Current Canvas : " + (manager.CurrentCanvas != null ? manager.CurrentCanvas.Name : "No Canvas"), uNodeEditorSettings.boldStyle);
            EditorGUILayout.Slider(new GUIContent("Zoom", "Use the Mousewheel to zoom/unzoom"), uNodeEditorState.zoom, 0.6f, 10.0f);
            GUILayout.Label("Cursor Position : " + uNodeEditorState.mousePosition);
            GUILayout.Label("Cursor delta : " + uNodeEditorState.mouseDelta);
            GUILayout.Label("Focused Node : " + (uNodeEditorState.focusedNode == null ? "null" : uNodeEditorState.focusedNode.Position.ToString()));
            GUILayout.Label("Selected Node : " + (uNodeEditorState.selectedNode == null ? "null" : uNodeEditorState.selectedNode.Position.ToString()));
            GUILayout.Label("Output Knob : " + (uNodeEditorState.outputKnobSaved == null ? "null" : uNodeEditorState.outputKnobSaved.Rect.ToString()));
            GUILayout.Label("Input Knob : " + (uNodeEditorState.selectedInputKnob == null ? "null" : uNodeEditorState.selectedInputKnob.Rect.ToString()));

            GUILayout.Space(20);
            GUILayout.Label("Current Canvas : " + uNodeEditorSaveManager.CurrentIndex);
            GUILayout.Label("Saved Canvas Count : " + uNodeEditorSaveManager.CanvasStates.Count);

            GUILayout.EndArea();
        }