Exemple #1
0
        // ----------------------------------------------------------------------
        public static iCS_EditorObject WrapMultiSelectionInPackage(iCS_IStorage iStorage)
        {
            if (iStorage == null)
            {
                return(null);
            }
            if (!IsCreationAllowed())
            {
                return(null);
            }
            var selectedObjects = iStorage.FilterMultiSelectionForWrapInPackage();

            if (selectedObjects == null || selectedObjects.Length == 0)
            {
                return(null);
            }

            iCS_EditorObject package = null;

            OpenTransaction(iStorage);
            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    var childrenRects = P.map(n => n.GlobalRect, selectedObjects);
                    package           = iStorage.WrapInPackage(selectedObjects);
                    if (package != null)
                    {
                        var r   = Math3D.Union(childrenRects);
                        var pos = Math3D.Middle(r);
                        package.SetInitialPosition(Math3D.Middle(r));
                        iStorage.ForcedRelayoutOfTree();
                        package.myAnimatedRect.StartValue = BuildRect(pos, Vector2.zero);
                        for (int i = 0; i < selectedObjects.Length; ++i)
                        {
                            selectedObjects[i].SetInitialPosition(iCS_EditorObject.PositionFrom(childrenRects[i]));
                            selectedObjects[i].LocalSize = iCS_EditorObject.SizeFrom(childrenRects[i]);
                        }
                        iStorage.ForcedRelayoutOfTree();
                        iStorage.ReduceCollisionOffset();
                    }
                    else
                    {
                        Debug.LogWarning("iCanScript: Unable to create a suitable package.");
                    }
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (package == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            CloseTransaction(iStorage, "Wrap Selection");
            SystemEvents.AnnounceVisualScriptElementAdded(package);
            return(package);
        }
Exemple #2
0
        // ----------------------------------------------------------------------
        public static bool DeleteMultiSelectedObjects(iCS_IStorage iStorage)
        {
#if SHOW_DEBUG
            Debug.Log("iCanScript: Multi-Select Delete");
#endif
            if (iStorage == null)
            {
                return(false);
            }
            if (!IsDeletionAllowed())
            {
                return(false);
            }
            var selectedObjects = iStorage.GetMultiSelectedObjects();
            if (selectedObjects == null || selectedObjects.Length == 0)
            {
                return(false);
            }
            if (selectedObjects.Length == 1)
            {
                DeleteObject(selectedObjects[0]);
                return(true);
            }
            OpenTransaction(iStorage);
            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    foreach (var obj in selectedObjects)
                    {
                        if (!obj.CanBeDeleted())
                        {
                            ShowNotification("Fix port=> \"" + obj.DisplayName + "\" from node=> \"" + obj.ParentNode.FullName + "\" cannot be deleted.");
                            continue;
                        }
                        // Move the selection to the parent node
                        var parent = obj.ParentNode;
                        iStorage.SelectedObject = parent;

                        SystemEvents.AnnounceVisualScriptElementWillBeRemoved(obj);

                        if (obj.IsInstanceNodePort)
                        {
                            iStorage.PropertiesWizardDestroyAllObjectsAssociatedWithPort(obj);
                        }
                        else
                        {
                            iStorage.DestroyInstance(obj.InstanceId);
                        }
                        iStorage.ForcedRelayoutOfTree();
                    }
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(false);
            }
            CloseTransaction(iStorage, "Delete Selection");
            return(true);
        }
 public static void CancelMultiSelectionNodeRelocation(iCS_IStorage iStorage)
 {
     // Animate node back to its original position.
     iStorage.AnimateGraph(null,
                           _ => {
         iStorage.CancelMultiSelectionNodeRelocation();
         iStorage.ForcedRelayoutOfTree();
     }
                           );
     CancelTransaction(iStorage);
 }
        // ======================================================================
        // Undo/Redo is a condition detected by the storage.  It simulates
        // a User Command.
        static void ReloadEditorData(iCS_IStorage iStorage, Action reloadAction)
        {
            var animationStarts = new Rect[iStorage.EditorObjects.Count];

            iStorage.ForEach(obj => { animationStarts[obj.InstanceId] = obj.AnimationTargetRect; });
            var previousScrollPosition = iStorage.ScrollPosition;
            var previousScale          = iStorage.GuiScale;

            iStorage.AnimateGraph(null,
                                  _ => {
                // Keep a copy of the animation start Rect.
                // Rebuild editor data.
                try {
                    reloadAction();
                }
                catch (System.Exception e) {
                    Debug.LogWarning("iCanScript: Problem found regenerating data: " + e.Message);
                }
                // Rebuild layout
                iStorage.ForcedRelayoutOfTree();
                // Put back the animation start Rect.
                int len = animationStarts.Length;
                for (int id = 0; id < len; ++id)
                {
                    if (iStorage.IsValid(id))
                    {
                        var obj = iStorage.EditorObjects[id];
                        obj.ResetAnimationRect(animationStarts[id]);
                    }
                }
                var visualEditor = iCS_EditorController.FindVisualEditor();
                if (visualEditor != null)
                {
                    var animationTime = PreferencesController.AnimationTime;
                    visualEditor.AnimateScrollPosition(previousScrollPosition,
                                                       iStorage.ScrollPosition,
                                                       animationTime);
                    visualEditor.AnimateScale(previousScale,
                                              iStorage.GuiScale,
                                              animationTime);
                }
            }
                                  );
            // Update central visual script data
            iCS_VisualScriptDataController.Update();
            // Repaint all windows that could have changed.
            iCS_EditorController.RepaintAllEditors();
            // Force redraw of Inspector Window.
            EditorUtility.SetDirty(iStorage.iCSMonoBehaviour);
            // Annouce that an undo occured.
            SystemEvents.AnnouceVisualScriptUndo(iStorage);
        }
        // ----------------------------------------------------------------------
        public static void PasteIntoGraph(iCS_MonoBehaviourImp sourceMonoBehaviour, iCS_EngineObject sourceRoot,
                                          iCS_IStorage iStorage, iCS_EditorObject parent, Vector2 globalPos)
        {
            if (!IsCreationAllowed())
            {
                return;
            }
            OpenTransaction(iStorage);
            iCS_EditorObject pasted = null;

            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    if (parent.IsFoldedInLayout || parent.IsIconizedInLayout)
                    {
                        parent.Unfold();
                    }
                    iCS_IStorage srcIStorage = new iCS_IStorage(sourceMonoBehaviour);
                    iCS_EditorObject srcRoot = srcIStorage.EditorObjects[sourceRoot.InstanceId];
                    pasted = iStorage.Copy(srcRoot, srcIStorage, parent, globalPos, iStorage);
                    if (pasted != null)
                    {
                        if (pasted.IsUnfoldedInLayout)
                        {
                            pasted.Fold();
                        }
                        iStorage.SelectedObject = pasted;
                        iStorage.ForcedRelayoutOfTree();
                    }
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return;
            }
            if (pasted != null)
            {
                SystemEvents.AnnounceVisualScriptElementAdded(pasted);
            }
            CloseTransaction(iStorage, "Add Prefab " + sourceRoot.RawName);
        }
        // ----------------------------------------------------------------------
        static iCS_EditorObject CreateAttachedMethod(iCS_EditorObject port, iCS_IStorage iStorage, Vector2 globalPos, LibraryObject libraryObject)
        {
            iCS_EditorObject newNodeParent = iStorage.GetNodeAt(globalPos);

            if (newNodeParent == null)
            {
                return(null);
            }
            if (!newNodeParent.IsKindOfPackage || newNodeParent.IsBehaviour)
            {
                return(null);
            }
            // FIXME: Animation & force layout sometime conflict.
            // Open a transaction for multi-operations
            iCS_UserCommands.OpenTransaction(iStorage);
            iCS_EditorObject method = null;

            iStorage.AnimateGraph(null,
                                  _ => {
                if (newNodeParent.IsInstanceNode)
                {
                    method = iCS_UserCommands.CreatePropertiesWizardElement(newNodeParent, libraryObject);
                }
                else
                {
                    method = iCS_UserCommands.CreateFunctionCallNode(newNodeParent, globalPos, libraryObject);
                }

                // Inverse effective data flow if new node is inside port parent.
                bool isConsumerPort = port.IsInputPort;
                var portParent      = port.ParentNode;
                if (portParent.IsParentOf(method))
                {
                    isConsumerPort = !isConsumerPort;
                }
                iCS_EditorObject attachedPort = null;
                iCS_EditorObject providerPort = null;
                iCS_EditorObject consumerPort = null;
                if (isConsumerPort)
                {
                    iCS_EditorObject[] outputPorts = Prelude.filter(x => iCS_Types.IsA(port.RuntimeType, x.RuntimeType), iStorage.GetChildOutputDataPorts(method));
                    // Connect if only one possibility.
                    if (outputPorts.Length == 1)
                    {
                        attachedPort = outputPorts[0];
                        consumerPort = port;
                        providerPort = attachedPort;
                    }
                    else
                    {
                        var bestPort = GetClosestMatch(port, outputPorts);
                        if (bestPort != null)
                        {
                            attachedPort = bestPort;
                            consumerPort = port;
                            providerPort = bestPort;
                        }
                    }
                }
                else
                {
                    iCS_EditorObject[] inputPorts = Prelude.filter(x => iCS_Types.IsA(x.RuntimeType, port.RuntimeType), iStorage.GetChildInputDataPorts(method));
                    // Connect if only one posiibility
                    if (inputPorts.Length == 1)
                    {
                        attachedPort = inputPorts[0];
                        consumerPort = attachedPort;
                        providerPort = port;
                    }
                    // Multiple choices exist so try the one with the closest name.
                    else
                    {
                        var bestPort = GetClosestMatch(port, inputPorts);
                        if (bestPort != null)
                        {
                            attachedPort = bestPort;
                            consumerPort = bestPort;
                            providerPort = port;
                        }
                    }
                }
                // Position attached port and layout binding.
                if (attachedPort != null && consumerPort != null && providerPort != null)
                {
                    iStorage.AutoLayoutPort(attachedPort, port.GlobalPosition, attachedPort.ParentNode.GlobalPosition);
                    iStorage.SetAndAutoLayoutNewDataConnection(consumerPort, providerPort);
                }
                iStorage.ForcedRelayoutOfTree();
            }
                                  );
            iCS_UserCommands.CloseTransaction(iStorage, "Create => " + libraryObject.nodeName);
            return(method);
        }