Пример #1
0
    private void GenerateToolbar()
    {
        var toolbar = new Toolbar();

        ObjectField objectField  = new ObjectField();
        Toggle      gridCheckbox = new Toggle();
        ToolbarMenu fileMenu     = new ToolbarMenu {
            text = "File", style = { width = 50 }
        };

        #region File Menu
        //Should iterate through ActionModuleGroup
        fileMenu.menu.AppendAction("New Module", action =>
        {
            if (string.IsNullOrEmpty(graphView.LoadedFileName))
            {
                objectField.value = null;
                graphView.CreateEmptyNewGraph();
            }
        });

        fileMenu.menu.AppendSeparator();
        fileMenu.menu.AppendAction("Save", action =>
        {
            ActionContainer actionContainer;
            if (string.IsNullOrEmpty(graphView.LoadedFileName))
            {
                actionContainer = Save();
                if (actionContainer)
                {
                    objectField.SetValueWithoutNotify(actionContainer);
                }
                return;
            }
            actionContainer = Save();
            if (actionContainer)
            {
                objectField.SetValueWithoutNotify(actionContainer);
            }
        });
        fileMenu.menu.AppendAction("Save As", action =>
        {
            var actionContainer = SaveAs();
            if (actionContainer)
            {
                objectField.SetValueWithoutNotify(actionContainer);
            }
        });
        bool isGridActive = false;
        fileMenu.menu.AppendSeparator();
        fileMenu.menu.AppendAction("Activate Grid", action =>
        {
            isGridActive = !isGridActive;
            graphView.ToggleGrid(!isGridActive);
        }, action =>
        {
            if (isGridActive)
            {
                return(DropdownMenuAction.Status.Normal);
            }
            return(DropdownMenuAction.Status.Checked);
        });

        #endregion

        #region Object Loader
        objectField.objectType = typeof(ActionContainer);
        objectField.RegisterCallback <ChangeEvent <Object> >(evt =>
        {
            if (evt.newValue == null)
            {
                objectField.SetValueWithoutNotify(evt.previousValue);
                return;
            }
            var saveUtility = GraphSaveUtility.GetInstance(graphView);

            //if dirty
            if (!graphView.IsDirty)
            {
                //clears and loadGraph
                saveUtility.LoadGraph((ActionContainer)evt.newValue);
                string loadedName        = ((ActionContainer)evt.newValue).ContainerName;
                graphView.LoadedFileName = loadedName;
                graphView.SetName(loadedName);
                return;
            }
            //Graph is dirty
            int option = EditorUtility.DisplayDialogComplex("Unsaved Changes",
                                                            "Do you want to save the changes you made before?", "Save", "Cancel", "Don't Save");

            switch (option)
            {
            case 1:     //Cancel
                objectField.SetValueWithoutNotify(evt.previousValue);
                break;

            case 0:     // Save
                graphView.SetDirty(!(saveUtility.SaveGraph(graphView.LoadedFileName) != null &&
                                     saveUtility.LoadGraph((ActionContainer)evt.newValue)));
                break;

            case 2:     // Don't Save.
                graphView.SetDirty(!saveUtility.LoadGraph((ActionContainer)evt.newValue));
                break;
            }
        });
        #endregion

        toolbar.Add(fileMenu);
        toolbar.Add(objectField);

        //Adds toolbar to window
        rootVisualElement.Add(toolbar);
    }