public static AXParametricObject addNodeToCurrentModel(string nodeName, bool panTo = true, AXParametricObject basedOnPO = null)
    {
        AXModel model = getOrMakeSelectedModel();


        Undo.RegisterCompleteObjectUndo(model, "Add Generator named " + nodeName);


        AXParametricObject npo = model.createNode(nodeName, panTo, basedOnPO);

        if (npo == null)
        {
            return(null);
        }

        if (basedOnPO != null)
        {
            //Debug.Log("basedOnPO.Name="+basedOnPO.Name);
            basedOnPO.copyTransformTo(npo);
        }



        ArchimatixEngine.repaintGraphEditorIfExistsAndOpen();


        bool isInstanceofCurrentGrouper = ((npo.generator is IReplica) && model.currentWorkingGroupPO != null && basedOnPO == model.currentWorkingGroupPO);


        if (model.currentWorkingGroupPO != null)
        {
            List <AXParametricObject> npos = new List <AXParametricObject>();
            npos.Add(npo);


            if (!isInstanceofCurrentGrouper)
            {
                model.currentWorkingGroupPO.addGroupees(npos);
            }
        }

        if (isInstanceofCurrentGrouper)
        {
            model.currentWorkingGroupPO = model.currentWorkingGroupPO.grouper;
        }

        return(npo);
    }
    public static void processEventCommand(Event e, AXModel model)
    {
        if (e.type == EventType.ValidateCommand)
        {
            e.Use();
        }

        //Debug.Log("-> processEventCommand 1: PROCESS COMMAND "+e.commandName);


        var view = SceneView.lastActiveSceneView;

        string focusedControlName = GUI.GetNameOfFocusedControl();

        if (model != null)
        {
            // intercept this command and use for po's
            switch (e.commandName)
            {
            case "UndoRedoPerformed":
                //Debug.Log ("UndoRedoPerformed");
                model.cleanGraph();
                model.autobuild();

                for (int i = 0; i < model.selectedPOs.Count; i++)
                {
                    model.selectedPOs[i].generator.adjustWorldMatrices();
                }

                // SCENEVIEW
                if (SceneView.lastActiveSceneView != null)
                {
                    SceneView.lastActiveSceneView.Repaint();
                }


                //model.setRenderMode( AXModel.RenderMode.GameObjects );
                model.cacheThumbnails("AXGeometryTools.Utilities::processEventCommand::UndoRedoPerformed");


                if (e.type != EventType.Repaint && e.type != EventType.Layout)
                {
                    e.Use();
                }
                break;


            case "SelectAll":

                Debug.Log("SelectAll");
                model.selectAll();


                e.Use();
                break;

            case "Copy":
                //Debug.Log ("COPY ..."+ GUI.GetNameOfFocusedControl());

                //Debug.Log ("buf: " + EditorGUIUtility.systemCopyBuffer);

                //Debug.Log("-"+focusedControlName+"-");
                //Debug.Log((string.IsNullOrEmpty(focusedControlName) || !focusedControlName.Contains("_Text_")));
                if (string.IsNullOrEmpty(focusedControlName) || !focusedControlName.Contains("_Text_"))
                {
                    if (model.selectedPOs.Count > 0)
                    {
                        EditorGUIUtility.systemCopyBuffer = LibraryEditor.poWithSubNodes_2_JSON(model.selectedPOs[0], true);
                    }
                    if (e.type != EventType.Repaint && e.type != EventType.Layout)
                    {
                        e.Use();
                    }
                }

                break;

            case "Paste":
                //Debug.Log ("PASTE");
                //Debug.Log(GUI.GetNameOfFocusedControl());

                //if (string.IsNullOrEmpty(GUI.GetNameOfFocusedControl()))
                //{
                Undo.RegisterCompleteObjectUndo(model, "Paste");
                string focusedControlNameBeforePaste = GUI.GetNameOfFocusedControl();
                if (string.IsNullOrEmpty(focusedControlNameBeforePaste) || !focusedControlNameBeforePaste.Contains("_Text_"))
                {
                    //model.deselectAll();

                    Library.pasteParametricObjectFromString(EditorGUIUtility.systemCopyBuffer);
                    model.autobuild();

                    if (e.type != EventType.Repaint && e.type != EventType.Layout)
                    {
                        e.Use();
                    }
                }
                model.autobuild();

                break;

            case "Duplicate":
                Undo.RegisterCompleteObjectUndo(model, "Duplicate");

                //Debug.Log ("Duplicate Command");
                if (model.selectedPOs.Count > 0)
                {
                    AXParametricObject selectedPO = model.selectedPOs[0];

                    instancePO(selectedPO);
                }

                model.autobuild();

                if (e.type != EventType.Repaint && e.type != EventType.Layout)
                {
                    e.Use();
                }
                break;

            case "Cut":
                Undo.RegisterCompleteObjectUndo(model, "Cut");
                //EditorGUIUtility.systemCopyBuffer = JSONSerializersAX.allSelectedPOsAsJson(model);


                if (string.IsNullOrEmpty(focusedControlName) || !focusedControlName.Contains("_Text_"))
                {
                    if (model.selectedPOs.Count > 0)
                    {
                        EditorGUIUtility.systemCopyBuffer = LibraryEditor.poWithSubNodes_2_JSON(model.selectedPOs[0], true);
                    }


                    if (e.shift)
                    {
                        model.deleteSelectedPOsAndInputs();
                    }
                    else
                    {
                        model.deleteSelectedPOs();
                    }

                    model.autobuild();
                    if (e.type != EventType.Repaint && e.type != EventType.Layout)
                    {
                        e.Use();
                    }
                }



                break;

            case "SoftDelete":
            case "Delete":

                //Debug.Log("DELETE");
                // see if it is a selected point on a curve
                //if (ArchimatixEngine

                /*
                 * FreeCurve selectedFreeCurve = null;
                 *
                 * if ( model.selectedPOs != null && model.selectedPOs.Count == 1 &&  model.selectedPOs[0] != null && model.selectedPOs[0].generator != null &&  model.selectedPOs[0].generator is FreeCurve)
                 * {
                 *      selectedFreeCurve = (FreeCurve) model.selectedPOs[0].generator;
                 * }
                 *
                 * if (selectedFreeCurve != null && selectedFreeCurve.selectedIndices != null  &&  selectedFreeCurve.selectedIndices.Count > 0)
                 * {
                 *      // delete points
                 *      selectedFreeCurve.deleteSelected();
                 * }
                 */

                // SELECTED POINTS TO DELETE?
                //Debug.Log("focusedControlName="+focusedControlName);
                if (string.IsNullOrEmpty(focusedControlName) || !focusedControlName.Contains("_Text_"))
                {
                    bool foundSelectedPoints = false;

                    if (model.activeFreeCurves.Count > 0)
                    {
                        for (int i = 0; i < model.activeFreeCurves.Count; i++)
                        {
                            FreeCurve gener = (FreeCurve)model.activeFreeCurves[i].generator;


                            //Debug.Log("gener.hasSelectedPoints()="+gener.hasSelectedPoints());
                            if (gener.hasSelectedPoints())
                            {
                                foundSelectedPoints = true;
                                gener.deleteSelected();
                            }
                        }
                    }
                    //Debug.Log("foundSelectedPoints="+foundSelectedPoints);

                    if (foundSelectedPoints)
                    {
                        ArchimatixEngine.mouseIsDownOnHandle = false;
                    }
                    else if (e.shift)
                    {
                        Undo.RegisterCompleteObjectUndo(model, "Delete Nodes");
                        model.deleteSelectedPOsAndInputs();
                    }
                    // [S.Darkwell: changed else to this else if to fix bug: "Fix pressing Delete key without Node selected still registers undo event" https://archimatixbeta.slack.com/files/s.darkwell/F1DJRQ3LL/fix_pressing_delete_key_without_node_selected_still_registers_undo_event.cs  - 2016.06.02]
                    else if (model.selectedPOs.Count > 0)
                    {
                        Undo.RegisterCompleteObjectUndo(model, "Delete Node");
                        model.deleteSelectedPOs();
                    }
                    else if (model.selectedParameterInputRelation != null)
                    {
                        Undo.RegisterCompleteObjectUndo(model, "Delete Dependancy");
                        model.selectedParameterInputRelation.makeIndependent();
                        model.selectedParameterInputRelation = null;
                    }
                    else if (model.selectedRelationInGraph != null)
                    {
                        Undo.RegisterCompleteObjectUndo(model, "Delete Relation");
                        model.unrelate(model.selectedRelationInGraph);
                        model.selectedRelationInGraph = null;
                    }

                    //Debug.Log("*********************************** DELETE");
                    model.remapMaterialTools();

                    model.autobuild();

                    //Debug.Log("caching here G");
                    model.cacheThumbnails();
                    e.Use();
                }



                break;



            case "FrameSelected":

                if (view != null)
                {
                    float framePadding = 400;


                    if (model.selectedPOs == null || model.selectedPOs.Count == 0)
                    {
                        //model.selectAll();
                        model.selectAllVisibleInGroup(model.currentWorkingGroupPO);


                        Rect allRect = AXUtilities.getBoundaryRectFromPOs(model.selectedPOs);
                        allRect.x      -= framePadding;
                        allRect.y      -= framePadding;
                        allRect.width  += framePadding * 2;
                        allRect.height += framePadding * 2;

                        AXNodeGraphEditorWindow.zoomToRectIfOpen(allRect);
                        model.deselectAll();
                    }
                    else if (model.selectedPOs.Count > 1)
                    {
                        Rect allRect = AXUtilities.getBoundaryRectFromPOs(model.selectedPOs);
                        allRect.x      -= framePadding;
                        allRect.y      -= framePadding;
                        allRect.width  += framePadding * 2;
                        allRect.height += framePadding * 2;

                        AXNodeGraphEditorWindow.zoomToRectIfOpen(allRect);
                    }
                    else
                    {
                        //frame first po
                        AXParametricObject currPO = model.cycleSelectedPO;                        // model.selectedPOs[0];

                        if (currPO == null)
                        {
                            currPO = model.mostRecentlySelectedPO;
                        }

                        if (currPO == null && model.selectedPOs != null && model.selectedPOs.Count > 0)
                        {
                            currPO = model.selectedPOs[0];
                        }

                        if (currPO == null)
                        {
                            currPO = model.selectFirstHeadPO();
                        }

                        if (currPO != null)
                        {
                            Matrix4x4 m = model.transform.localToWorldMatrix * currPO.worldDisplayMatrix;                            // * currPO.getLocalMatrix();

                            if (m.isIdentity)
                            {
                                m = currPO.generator.localMatrix;
                            }

                            Vector3 position = m.MultiplyPoint(currPO.bounds.center);


                            if (currPO.bounds.size.magnitude > .005 && currPO.bounds.size.magnitude < 10000)
                            {
                                view.LookAt(position, view.camera.transform.rotation, currPO.bounds.size.magnitude * 1.01f);
                            }
                            else
                            {
                                //Debug.Log("FrameSelected - select ParametricObjectObject bounds not good: "+currPO.bounds.size.magnitude);
                            }

                            //if (currPO.grouper != null )
                            //{
                            AXNodeGraphEditorWindow.displayGroupIfOpen(currPO.grouper);
                            //}
                            //model.beginPanningToPoint(currPO.rect.center);
                            AXNodeGraphEditorWindow.zoomToRectIfOpen(currPO.rect);
                        }
                    }
                    if (e.type != EventType.Repaint && e.type != EventType.Layout)
                    {
                        e.Use();
                    }
                }

                break;
            }
        }

        // EDITOR WINDOW

        ArchimatixEngine.repaintGraphEditorIfExistsAndOpen();

        // SCENEVIEW
        if (view != null)
        {
            view.Repaint();
        }


        lastCommand = e.commandName;
    }
    public static void processEventCommandKeyDown(Event e, AXModel model)
    {
        switch (e.keyCode)
        {
        case KeyCode.L:
            if (model.selectedPOs != null && model.selectedPOs.Count == 1)
            {
                LibraryEditor.doSave_MenuItem(model.selectedPOs[0]);

                if (e.type != EventType.Repaint && e.type != EventType.Layout)
                {
                    e.Use();
                }
            }
            break;

        case KeyCode.V:
            // this paste waste uncaught by EventType.ExecuteCommand or EventType.ValidateCommand
            // beacuse alt or command were held down
            Undo.RegisterCompleteObjectUndo(model, "Paste");

            if (e.shift)
            {
                if (e.alt)
                {
                    model.duplicateSelectedPOs();
                }
                else
                {
                    model.replicateSelectedPOs();
                }

                model.isAltered(6);


                if (e.type != EventType.Repaint && e.type != EventType.Layout)
                {
                    e.Use();
                }
            }

            break;

        case KeyCode.D:
            Undo.RegisterCompleteObjectUndo(model, "Duplicate");
            //Debug.Log ("DUPLICATE.....KeyCode.D");
            if (model.selectedPOs.Count > 0)
            {
                AXParametricObject selectedPO = model.selectedPOs[0];
                if (e.shift)
                {
                    replicatePO(selectedPO);
                }
                else if (e.alt)
                {
                    duplicatePO(selectedPO);
                }
                else
                {
                    //Debug.Log ("instancePO");
                    instancePO(selectedPO);
                }
            }

            // "Duplicate" is caught by the validateCommand and processed here in processEventCommand()

            model.isAltered(7);

            if (e.type != EventType.Repaint && e.type != EventType.Layout)
            {
                e.Use();
            }
            break;

        case KeyCode.Backspace:

            string focusedControlName = GUI.GetNameOfFocusedControl();


            if (string.IsNullOrEmpty(focusedControlName) || !focusedControlName.Contains("_Text_"))
            {
                Undo.RegisterCompleteObjectUndo(model, "Delete");

                if (e.shift)
                {
                    model.deleteSelectedPOsAndInputs();
                }
                else
                {
                    model.deleteSelectedPOs();
                }

                model.isAltered(8);
                e.Use();
            }

            break;
        }

        // Update EditorWindow
        ArchimatixEngine.repaintGraphEditorIfExistsAndOpen();
    }
Esempio n. 4
0
        public static AXParametricObject pasteParametricObjectFromString(String json_string)
        {
            if (string.IsNullOrEmpty(json_string))
            {
                json_string = EditorGUIUtility.systemCopyBuffer;
            }

            if (string.IsNullOrEmpty(json_string))
            {
                return(null);
            }

            AXModel model = null;

            // 1. First choice is to use a selected model....
            if (ArchimatixEngine.currentModel != null)
            {
                model = ArchimatixEngine.currentModel;
            }

            bool doInstantiation = true;

            if (model == null)
            {
                // ok, are there other models in the scene that the user doesn't realize arene't selected?
                AXModel[] axModels = GameObject.FindObjectsOfType(typeof(AXModel)) as AXModel[];

                if (axModels != null)
                {
                    if (axModels.Length == 1)
                    {
                        // Offer this model as a button,  but also give opportunity to create a new one...
                        int option = EditorUtility.DisplayDialogComplex("Instantiating Library Item", "There is no AX model currently selected", "Cancel", "Add to " + axModels[0], "Create a new Model");


                        switch (option)
                        {
                        // Save Scene
                        case 0:
                            doInstantiation = false;
                            break;

                        // Save and Quit.
                        case 1:
                            model = axModels[0];
                            break;

                        case 2:
                            GameObject axgo = AXEditorUtilities.createNewModel();
                            model           = axgo.GetComponent <AXModel>();
                            doInstantiation = false;
                            break;

                        default:
                            doInstantiation = false;
                            break;
                        }
                    }

                    else if (axModels.Length > 1)
                    {
                        // Throw up a dialog to see if use wants to go select one of the existing models or create a new one.

                        if (EditorUtility.DisplayDialog("Instantiating Library Item", "There is no AX model currently selected", "Select and Existing Model", "Create a new Model"))
                        {
                            return(null);
                        }
                    }
                }
            }


            if (!doInstantiation)
            {
                return(null);
            }



            if (model == null)
            {                   // Well, then, we tried everything....
                GameObject axgo = AXEditorUtilities.createNewModel();
                model = axgo.GetComponent <AXModel>();
            }



            List <AXParametricObject> poList       = new List <AXParametricObject>();
            List <AXRelation>         relationList = new List <AXRelation>();

            AX.SimpleJSON.JSONNode json = null;
            try
            {
                json = AX.SimpleJSON.JSON.Parse(json_string);
            }
            catch
            {
                return(null);
            }
            if (json == null)
            {
                return(null);
            }

            if (json["parametric_objects"] != null)
            {
                foreach (AX.SimpleJSON.JSONNode poNode in json["parametric_objects"].AsArray)
                {
                    poList.Add(JSONSerializersAX.ParametricObjectFromJSON(poNode));
                }
            }

            foreach (AX.SimpleJSON.JSONNode rNode in json["relations"].AsArray)
            {
                relationList.Add(AXRelation.fromJSON(rNode));
            }

            //Debug.Log (rNode);



            // make sure there is a game object ready to instantiate this on



            if (model == null)
            {
                // check if the user really wants to create a new model...

                GameObject axgo = AXEditorUtilities.createNewModel();
                model = axgo.GetComponent <AXModel>();
            }

            Undo.RegisterCompleteObjectUndo(model, "Add Library Object");

            AXParametricObject head_po = poList[0];



            if (model.currentWorkingGroupPO != null && model.currentWorkingGroupPO.Name != head_po.Name)
            {
                head_po.grouperKey = model.currentWorkingGroupPO.Guid;
            }
            else
            {
                head_po.grouperKey = null;
            }



            // ADD AND RIGUP
            // !!! Actually add the library item (as a list of PO's and Relations) to the model
            model.addAndRigUp(poList, relationList);



            head_po.startStowInputs();
            //if (poList[0].is2D() && ArchimatixEngine.workingAxis != (int) Axis.NONE)
            //	poList[0].intValue ("Axis", ArchimatixEngine.workingAxis);


            model.setRenderMode(AXModel.RenderMode.GameObjects);
            model.generate();

            ArchimatixEngine.currentModel = model;
            Selection.activeGameObject    = model.gameObject;


            model.selectOnlyPO(head_po);
            head_po.focusMe = true;


            //AXParametricObject mostRecentPO = model.recentlySelectedPO;
            AXParametricObject mostRecentPO = model.recentlySelectedPO;            //getPOSelectedBefore(head_po);



            //Debug.Log ("model.focusPointInGraphEditor="+model.focusPointInGraphEditor);

            float pos_x = (model.focusPointInGraphEditor.x) + 100;          // + UnityEngine.Random.Range(-100, 300);
            float pos_y = (model.focusPointInGraphEditor.y - 250) + UnityEngine.Random.Range(-10, 0);

            if (mostRecentPO != null)
            {
                //pos_x =  mostRecentPO.rect.x + 200;
                //pos_y =  mostRecentPO.rect.y;
            }

            //Debug.Log (new Rect(pos_x, pos_y, 150, 100));
            head_po.rect = new Rect(pos_x, pos_y, 150, 500);


            //Debug.Log ("OK "+po.Name+".focusMe = " + po.focusMe);
            // make sure it is in view in the editor window....

            //model.startPanningToRect(head_po.rect);


            //model.cleanGraph();
            model.autobuild();
            head_po.generator.adjustWorldMatrices();


            model.selectOnlyPO(head_po);



            // Paste inside grouper?
            if (model.currentWorkingGroupPO != null && model.currentWorkingGroupPO.Name != head_po.Name)
            {
                model.currentWorkingGroupPO.addGroupee(head_po);
            }


            if (model.currentWorkingGroupPO != null && model.currentWorkingGroupPO.Name == head_po.Name)
            {
                model.currentWorkingGroupPO = model.currentWorkingGroupPO.grouper;
            }


            //model.beginPanningToRect(head_po.rect);
            //AXNodeGraphEditorWindow.zoomToRectIfOpen(head_po.rect);

            //Debug.Log(" 1 model.selectedPOs.Count="+model.selectedPOs.Count + " " + model.selectedPOs[0].Name );
            // EDITOR WINDOW
            ArchimatixEngine.repaintGraphEditorIfExistsAndOpen();



            // SCENEVIEW
            if (SceneView.lastActiveSceneView != null)
            {
                //Debug.Log("REPAINT");
                SceneView.lastActiveSceneView.Repaint();
            }

            //Debug.Log("--> ArchimatixEngine.currentModel=" + ArchimatixEngine.currentModel);


            return(head_po);
        }