コード例 #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);
        }
コード例 #2
0
        // ----------------------------------------------------------------------
        // OK
        public static iCS_EditorObject CreateOnExitPackage(iCS_EditorObject parent, Vector2 globalPos)
        {
            if (parent == null)
            {
                return(null);
            }
            var iStorage = parent.IStorage;

            OpenTransaction(iStorage);
            iCS_EditorObject package = null;

            try {
                package = _CreatePackage(parent, globalPos, iCS_Strings.OnExit, VSObjectType.OnStateExit);
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (package == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            package.Description = iCS_ObjectTooltips.OnExit;
            CloseTransaction(iStorage, "Create " + package.DisplayName);
            SystemEvents.AnnounceVisualScriptElementAdded(package);
            return(package);
        }
コード例 #3
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);
        }
コード例 #4
0
        // ----------------------------------------------------------------------
        public static void DeleteKeepChildren(iCS_EditorObject obj)
        {
            if (!IsDeletionAllowed())
            {
                return;
            }
            var iStorage = obj.IStorage;

            OpenTransaction(iStorage);
            try {
                var newParent  = obj.ParentNode;
                var childNodes = obj.BuildListOfChildNodes(_ => true);
                var childPos   = P.map(n => n.GlobalPosition, childNodes);
                iStorage.AnimateGraph(obj,
                                      _ => {
                    // Move the selection to the parent node
                    var parent = obj.ParentNode;
                    iStorage.SelectedObject = parent;

                    P.forEach(n => { iStorage.ChangeParent(n, newParent); }, childNodes);
                    SystemEvents.AnnounceVisualScriptElementWillBeRemoved(obj);
                    iStorage.DestroyInstance(obj.InstanceId);
                    P.zipWith((n, p) => { n.LocalAnchorFromGlobalPosition = p; }, childNodes, childPos);
                    iStorage.ForcedRelayoutOfTree();
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return;
            }
            CloseTransaction(iStorage, "Delete " + obj.DisplayName);
        }
コード例 #5
0
        // ======================================================================
        // Object destruction.
        // ----------------------------------------------------------------------
        public static bool DeleteObject(iCS_EditorObject obj)
        {
#if SHOW_DEBUG
            Debug.Log("iCanScript: Deleting => " + obj.DisplayName);
#endif
            if (obj == null)
            {
                return(false);
            }
            if (!IsDeletionAllowed())
            {
                return(false);
            }
            if (!obj.CanBeDeleted())
            {
                ShowNotification("Port cannot be deleted=> " + obj.FullName);
                return(false);
            }
            var name     = obj.DisplayName;
            var iStorage = obj.IStorage;
            OpenTransaction(iStorage);
            if (obj.IsInstanceNodePort)
            {
                try {
                    iStorage.AnimateGraph(null,
                                          _ => {
                        iStorage.SelectedObject = obj.ParentNode;
                        SystemEvents.AnnounceVisualScriptElementWillBeRemoved(obj);
                        iStorage.PropertiesWizardDestroyAllObjectsAssociatedWithPort(obj);
                        iStorage.ForcedRelayoutOfTree();
                    }
                                          );
                }
                catch (System.Exception) {
                    CancelTransaction(iStorage);
                    return(false);
                }
                CloseTransaction(iStorage, "Delete " + name);
                return(true);
            }
            // TODO: Should animate parent node on node delete.
            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    // Move the selection to the parent node
                    var parent = obj.ParentNode;
                    iStorage.SelectedObject = parent;
                    SystemEvents.AnnounceVisualScriptElementWillBeRemoved(obj);
                    iStorage.DestroyInstance(obj.InstanceId);
                    iStorage.ForcedRelayoutOfTree();
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(false);
            }
            CloseTransaction(iStorage, "Delete " + name);
            return(true);
        }
コード例 #6
0
        // ======================================================================
        // 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);
        }
コード例 #7
0
 // ----------------------------------------------------------------------
 public void SaveStorage()
 {
     // -- Keep a copy of the selected object global position. --
     if (SelectedObject != null)
     {
         Storage.SelectedObjectPosition = SelectedObject.GlobalPosition;
     }
     // -- Tell Unity that our storage has changed. --
     iCS_StorageImp.CopyFromTo(Storage, EngineStorage);
     // -- Commit Undo transaction and forces redraw of inspector window. --
     EditorUtility.SetDirty(iCSMonoBehaviour);
     ++ModificationId;
     iCS_EditorController.RepaintAllEditors();
     SystemEvents.AnnouceVisualScriptSaved(this);
     // -- Perform sanity check. --
     SanityCheck();
 }
コード例 #8
0
        // ======================================================================
        // Create from Drag & Drop
        // ----------------------------------------------------------------------
        // OK
        public static iCS_EditorObject CreateGameObject(GameObject go, iCS_EditorObject parent, Vector2 globalPos)
        {
#if SHOW_DEBUG
            Debug.Log("iCanScript: Create Game Object => " + go.name);
#endif
            if (parent == null)
            {
                return(null);
            }
            if (!IsCreationAllowed())
            {
                return(null);
            }
            var iStorage = parent.IStorage;

            iCS_EditorObject instance = null;
            OpenTransaction(iStorage);
            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    parent.Unfold();
                    instance     = iStorage.CreatePackage(parent.InstanceId, go.name, VSObjectType.Package, go.GetType());
                    var thisPort = iStorage.PropertiesWizardGetInputThisPort(instance);
                    if (thisPort != null)
                    {
                        thisPort.Value = go;
                    }
                    instance.SetInitialPosition(globalPos);
                    iStorage.ForcedRelayoutOfTree();
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (instance == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            CloseTransaction(iStorage, "Create " + go.name);
            SystemEvents.AnnounceVisualScriptElementAdded(instance);
            return(instance);
        }
コード例 #9
0
        // ----------------------------------------------------------------------
        /// Creates a Unity event handler with Undo capabilities.
        ///
        /// @param parent The parent visual script object.
        /// @param globalPos The layout position for the event handler node.
        /// @param libraryEventHandler The library event handler object.
        /// @return The create Unity event handler node. _null_ on error.
        ///
        public static iCS_EditorObject CreateEventHandler(iCS_EditorObject parent, Vector2 globalPos, LibraryEventHandler libraryEventHandler)
        {
#if SHOW_DEBUG
            Debug.Log("iCanScript: Create Unity EVent Handler => " + libraryEventHandler.displayString);
#endif
            if (parent == null)
            {
                return(null);
            }
            if (!IsCreationAllowed())
            {
                return(null);
            }
            var iStorage = parent.IStorage;
            var nodeName = libraryEventHandler.nodeName;
            if (!iCS_AllowedChildren.CanAddChildNode(nodeName, VSObjectType.InstanceMessage, parent, iStorage))
            {
                return(null);
            }
            OpenTransaction(iStorage);
            iCS_EditorObject msgHandler = null;
            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    msgHandler = iStorage.CreateNode(parent.InstanceId, libraryEventHandler);
                    msgHandler.SetInitialPosition(globalPos);
                    msgHandler.ForEachChildPort(p => { p.AnimationStartRect = BuildRect(globalPos, Vector2.zero); });
                    iStorage.ForcedRelayoutOfTree();
                    iStorage.ReduceCollisionOffset();
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (msgHandler == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            CloseTransaction(iStorage, "Create " + nodeName);
            SystemEvents.AnnounceVisualScriptElementAdded(msgHandler);
            return(msgHandler);
        }
コード例 #10
0
        // ======================================================================
        // Instance Object creation.
        // ----------------------------------------------------------------------
        public static iCS_EditorObject CreateObjectInstance(iCS_EditorObject parent, Vector2 globalPos, Type instanceType)
        {
            if (instanceType == null)
            {
                return(null);
            }
            if (!IsCreationAllowed())
            {
                return(null);
            }
            instanceType = iCS_Types.RemoveRefOrPointer(instanceType);
#if SHOW_DEBUG
            Debug.Log("iCanScript: Create Object Instance => " + instanceType.Name);
#endif
            if (parent == null)
            {
                return(null);
            }
            var iStorage = parent.IStorage;
            var name     = instanceType.Name;
            OpenTransaction(iStorage);
            iCS_EditorObject instance = null;
            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    instance = iStorage.CreateObjectInstance(parent.InstanceId, name, instanceType);
                    instance.SetInitialPosition(globalPos);
                    iStorage.ForcedRelayoutOfTree();
                    iStorage.ReduceCollisionOffset();
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (instance == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            CloseTransaction(iStorage, "Create " + name);
            SystemEvents.AnnounceVisualScriptElementAdded(instance);
            return(instance);
        }
コード例 #11
0
 /// Shutdowns the application controller.
 public static void Shutdown()
 {
     // Shutdown all subsystems.
     PackageController.Shutdown();
     HelpController.Shutdown();
     SceneController.Shutdown();
     iCS_EditorController.Shutdown();
     iCS_VisualScriptDataController.Shutdown();
     LibraryController.Shutdown();
     GizmoController.Shutdown();
     SoftwareUpdateController.Shutdown();
     LicenseController.Shutdown();
     EditionController.Shutdown();
     BlinkController.Shutdown();
     TimerService.Shutdown();
     SystemEvents.Shutdown();
     ErrorController.Shutdown();
 }
コード例 #12
0
        // ----------------------------------------------------------------------
        // OK
        public static iCS_EditorObject CreateStateChart(iCS_EditorObject parent, Vector2 globalPos, string name)
        {
#if SHOW_DEBUG
            Debug.Log("iCanScript: Create State Chart => " + name);
#endif
            if (parent == null)
            {
                return(null);
            }
            if (!IsCreationAllowed())
            {
                return(null);
            }
            var iStorage = parent.IStorage;
            OpenTransaction(iStorage);
            iCS_EditorObject stateChart = null;
            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    stateChart = iStorage.CreateStateChart(parent.InstanceId, name);
                    stateChart.SetInitialPosition(globalPos);
                    iStorage.ForcedRelayoutOfTree();
                    // Automatically create entry state.
                    var entryState          = iStorage.CreateState(stateChart.InstanceId, "EntryState");
                    entryState.IsEntryState = true;
                    entryState.SetInitialPosition(globalPos);
                    iStorage.ForcedRelayoutOfTree();
                    iStorage.ReduceCollisionOffset();
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (stateChart == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            CloseTransaction(iStorage, "Create StateChart");
            SystemEvents.AnnounceVisualScriptElementAdded(stateChart);
            return(stateChart);
        }
コード例 #13
0
        // ----------------------------------------------------------------------
        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);
        }
コード例 #14
0
        // ----------------------------------------------------------------------
        public static void ChangeName(iCS_EditorObject obj, string name)
        {
            if (name == null)
            {
                return;
            }
            name.Trim();
            if (string.Compare(obj.DisplayName, name) == 0)
            {
                return;
            }
            var iStorage = obj.IStorage;

            if (obj.IsNode && string.IsNullOrEmpty(name))
            {
                name = NameUtility.ToDisplayName(obj.CodeName);
            }
            OpenTransaction(iStorage);
            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    obj.DisplayName = name;
                    if (obj.IsNode)
                    {
                        iStorage.ForcedRelayoutOfTree();
                    }
                    else if (obj.IsDataOrControlPort)
                    {
                        iStorage.ForcedRelayoutOfTree();
                    }
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return;
            }
            CloseTransaction(iStorage, "Change name => " + name, TransactionType.Field);
            iCS_EditorController.RepaintEditorsWithLabels();
            SystemEvents.AnnounceVisualScriptElementNameChanged(obj);
        }
コード例 #15
0
        // ======================================================================
        // Initialization all sub-systems
        // ----------------------------------------------------------------------
        static AppController()
        {
//        var diff= new Diff();
//        diff= null;

            // Start all sub-systems.
            ErrorController.Start();
            SystemEvents.Start();
            TimerService.Start();
            BlinkController.Start();
            EditionController.Start();
            LicenseController.Start();
            SoftwareUpdateController.Start();
            GizmoController.Start();
            LibraryController.Start();
            iCS_VisualScriptDataController.Start();
            iCS_EditorController.Start();
            SceneController.Start();
            HelpController.Start();
            PackageController.Start();
        }
コード例 #16
0
        // ----------------------------------------------------------------------
        // OK
        public static iCS_EditorObject CreateFunctionCallNode(iCS_EditorObject parent, Vector2 globalPos, LibraryObject libraryObject)
        {
#if SHOW_DEBUG
            Debug.Log("iCanScript: Create Function => " + libraryObject.displayString);
#endif
            if (parent == null || libraryObject == null)
            {
                return(null);
            }
            if (!IsCreationAllowed())
            {
                return(null);
            }
            var iStorage = parent.IStorage;
            OpenTransaction(iStorage);
            iCS_EditorObject function = null;
            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    function = iStorage.CreateNode(parent.InstanceId, libraryObject);
                    function.SetInitialPosition(globalPos);
                    iStorage.ForcedRelayoutOfTree();
                    iStorage.ReduceCollisionOffset();
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (function == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            var name = libraryObject.nodeName;
            CloseTransaction(iStorage, "Create " + name);
            SystemEvents.AnnounceVisualScriptElementAdded(function);
            return(function);
        }
コード例 #17
0
        // -------------------------------------------------------------------------
        public static iCS_EditorObject CreateTriggerPort(iCS_EditorObject parent)
        {
            if (parent == null)
            {
                return(null);
            }
            if (!IsCreationAllowed())
            {
                return(null);
            }
            var iStorage = parent.IStorage;

            OpenTransaction(iStorage);
            iCS_EditorObject port = null;

            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    port      = iStorage.CreateTriggerPort(parent.InstanceId);
                    var pRect = parent.GlobalRect;
                    port.SetInitialPosition(new Vector2(0.5f * (pRect.x + pRect.xMax), pRect.yMax));
                    iStorage.ForcedRelayoutOfTree();
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (port == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            CloseTransaction(iStorage, "Create Trigger Port");
            SystemEvents.AnnounceVisualScriptElementAdded(port);
            return(port);
        }
コード例 #18
0
        // ======================================================================
        /// Creates an inline code node.
        ///
        /// @param parent The parent node in which to create the inline code.
        /// @param globalPosition The layout position where to new node should be created.
        /// @param name The name of the node to create.
        ///
        public static iCS_EditorObject CreateInlineCode(iCS_EditorObject parent, Vector2 globalPos, string name)
        {
            var iStorage = parent.IStorage;

            OpenTransaction(iStorage);
            iCS_EditorObject package = null;

            try {
                package = _CreatePackage(parent, globalPos, name, VSObjectType.InlineCode, typeof(InlineCode));
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (package == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            CloseTransaction(iStorage, "Create " + name);
            SystemEvents.AnnounceVisualScriptElementAdded(package);
            return(package);
        }
コード例 #19
0
        // ======================================================================
        // Object creation
        // ----------------------------------------------------------------------
        public static iCS_EditorObject CreatePackage(iCS_EditorObject parent, Vector2 globalPos, string name, VSObjectType objectType = VSObjectType.Package, Type runtimeType = null)
        {
#if SHOW_DEBUG
            Debug.Log("iCanScript: CreatePackage => " + name);
#endif
            var iStorage = parent.IStorage;
            OpenTransaction(iStorage);
            iCS_EditorObject package = null;
            try {
                package = _CreatePackage(parent, globalPos, name, objectType, runtimeType);
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (package == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            CloseTransaction(iStorage, "Create " + name);
            SystemEvents.AnnounceVisualScriptElementAdded(package);
            return(package);
        }
コード例 #20
0
        // ======================================================================
        // Node Wrapping in package.
        // ----------------------------------------------------------------------
        public static iCS_EditorObject WrapInPackage(iCS_EditorObject obj)
        {
            if (obj == null || !obj.CanHavePackageAsParent())
            {
                return(null);
            }
            if (!IsCreationAllowed())
            {
                return(null);
            }
            var iStorage = obj.IStorage;

            OpenTransaction(iStorage);
            iCS_EditorObject package = null;

            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    package = iStorage.WrapInPackage(obj);
                    iStorage.ForcedRelayoutOfTree();
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (package == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            CloseTransaction(iStorage, "Wrap : " + obj.DisplayName);
            SystemEvents.AnnounceVisualScriptElementAdded(package);
            return(package);
        }
コード例 #21
0
        // ----------------------------------------------------------------------
        public static iCS_EditorObject CreateTransition(iCS_EditorObject fromStatePort, iCS_EditorObject toState, Vector2 toStatePortPos)
        {
#if SHOW_DEBUG
            Debug.Log("iCanScript: Create Transition Package");
#endif
            if (fromStatePort == null || toState == null)
            {
                return(null);
            }
            if (!IsCreationAllowed())
            {
                return(null);
            }
            var iStorage = toState.IStorage;
            OpenTransaction(iStorage);
            iCS_EditorObject transitionPackage = null;
            try {
                // Create toStatePort
                iCS_EditorObject toStatePort = iStorage.CreatePort("", toState.InstanceId, typeof(void), VSObjectType.InStatePort);
                // Update port positions
                toStatePort.SetInitialPosition(toStatePortPos);
                toStatePort.UpdatePortEdge();
                fromStatePort.UpdatePortEdge();
                // Temporally connect state ports together.
                iStorage.SetSource(toStatePort, fromStatePort);
                // Create transition package
                transitionPackage = iStorage.CreateTransition(fromStatePort, toStatePort);
                // Try to position the transition in the middle
                var fromStatePortPos = fromStatePort.GlobalPosition;
                var globalPos        = 0.5f * (fromStatePortPos + toStatePortPos);
                transitionPackage.SetInitialPosition(globalPos);
                transitionPackage.Iconize();
                // Attempt to proper edge for transition ports.
                var outTransitionPort = toStatePort.ProducerPort;
                var inTransitionPort  = iStorage.GetInTransitionPort(transitionPackage);
                var diff = toStatePortPos - fromStatePortPos;
                if (Mathf.Abs(diff.x) > Mathf.Abs(diff.y))
                {
                    if (Vector2.Dot(diff, Vector2.right) > 0)
                    {
                        inTransitionPort.Edge  = NodeEdge.Left;
                        toStatePort.Edge       = NodeEdge.Left;
                        outTransitionPort.Edge = NodeEdge.Right;
                        fromStatePort.Edge     = NodeEdge.Right;
                    }
                    else
                    {
                        inTransitionPort.Edge  = NodeEdge.Right;
                        toStatePort.Edge       = NodeEdge.Right;
                        outTransitionPort.Edge = NodeEdge.Left;
                        fromStatePort.Edge     = NodeEdge.Left;
                    }
                }
                else
                {
                    if (Vector2.Dot(diff, Vector2.up) > 0)
                    {
                        inTransitionPort.Edge  = NodeEdge.Top;
                        toStatePort.Edge       = NodeEdge.Top;
                        outTransitionPort.Edge = NodeEdge.Bottom;
                        fromStatePort.Edge     = NodeEdge.Bottom;
                    }
                    else
                    {
                        inTransitionPort.Edge  = NodeEdge.Bottom;
                        toStatePort.Edge       = NodeEdge.Bottom;
                        outTransitionPort.Edge = NodeEdge.Top;
                        fromStatePort.Edge     = NodeEdge.Top;
                    }
                }
                inTransitionPort.PortPositionRatio  = 0.5f;
                outTransitionPort.PortPositionRatio = 0.5f;
                // Layout the graph
                iStorage.ForcedRelayoutOfTree();
                iStorage.ReduceCollisionOffset();
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (transitionPackage == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            CloseTransaction(iStorage, "Create Transition");
            SystemEvents.AnnounceVisualScriptElementAdded(transitionPackage);
            return(transitionPackage);
        }