Example #1
0
 void OnEnable()
 {
     windowIcon         = EditorGUIUtility.Load("Assets/NodeMachine/Editor/Editor Resources/State Machine Icon.png") as Texture2D;
     titlePlain.image   = windowIcon;
     titleUnsaved.image = windowIcon;
     titleContent       = titlePlain;
     NodeMachineGUIUtils.Init();
     _propertyMenu           = new PropertyMenu(this);
     _errorPanel             = new ErrorPanel(this);
     Undo.undoRedoPerformed += OnUndoRedo;
     if (_model == null)
     {
         _model = AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(_modelInstanceID), typeof(NodeMachineModel)) as NodeMachineModel;
     }
     if (_model != null)
     {
         LoadModel(_model);
         if (_selectedLink != null)
         {
             _selectedLink = _model.GetLinkFromID(_selectedLink.ID);
         }
         if (_selectedNode != null)
         {
             _selectedNode = _model.GetNodeFromID(_selectedNode.ID);
         }
     }
     else
     {
         _selectedLink = null;
         _selectedNode = null;
     }
     FindNodeMenuHandlers();
 }
Example #2
0
        void OnGUI()
        {
            if (_model == null)
            {
                if (_selectedMachine == null)
                {
                    NodeMachineGUIUtils.DrawNothingOpenScreen(this);
                }
                else
                {
                    NodeMachineGUIUtils.DrawMachineNoModelScreen(this);
                }
                return;
            }

            Vector2 center = new Vector2(position.width / 2, position.height / 2) - (_nodeEditor.position / 2);

            _offset = _uncenteredOffset + center;

            titleContent = _changesMade ? NodeMachineEditor.titleUnsaved : NodeMachineEditor.titlePlain;

            bool livePreview = _selectedMachine != null && EditorApplication.isPlaying;

            bool modelNeedsSaving = false;

            _nodeEditor = new Rect(250, 30, position.width - 250, position.height - 30);
            _sideMenu   = new Rect(10, 20, 230, position.height - 20);
            _toolbar    = new Rect(260, 5, position.width - 260, 25);

            // ----- TOOLBAR --------

            GUILayout.BeginArea(_toolbar);
            GUILayout.BeginHorizontal();

            _drawTransparentLinks = GUILayout.Toggle(_drawTransparentLinks, " Link X-Ray", GUILayout.ExpandWidth(false));
            GUILayout.Label("  ", GUILayout.ExpandWidth(false));
            _showInivisibleNodes = GUILayout.Toggle(_showInivisibleNodes, " Reveal hidden nodes", GUILayout.ExpandWidth(false));
            GUILayout.Label("  ", GUILayout.ExpandWidth(false));

            GUILayout.EndHorizontal();
            GUILayout.EndArea();

            // ----- SIDE MENU ------

            GUILayout.BeginArea(_sideMenu);
            GUILayout.BeginVertical();

            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

            EditorGUILayout.LabelField(_model.name, EditorStyles.boldLabel);

            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

            EditorGUILayout.Space();

            float checkinTimeBeforeDraw     = _model.CheckinTime;
            bool  supportParallelBeforeDraw = _model.supportParallel;

            _model.CheckinTime     = EditorGUILayout.FloatField("Checkin time", _model.CheckinTime);
            _model.supportParallel = EditorGUILayout.Toggle("Parallel flow", _model.supportParallel);
            if (checkinTimeBeforeDraw != _model.CheckinTime || supportParallelBeforeDraw != _model.supportParallel)
            {
                modelNeedsSaving = true;
            }

            EditorGUILayout.Space();

            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

            EditorGUILayout.Space();

            _propertyMenu.DrawMenu(EditorApplication.isPlayingOrWillChangePlaymode, _selectedMachine);

            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

            // --------- ERRORS ---------

            EditorGUILayout.Space();

            _errorPanel.Draw();

            EditorGUILayout.Space();

            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

            GUILayout.EndVertical();
            GUILayout.EndArea();

            // ----- NODE EDITOR -----

            GUILayout.BeginArea(_nodeEditor, new GUIStyle("Box"));

            NodeMachineGUIUtils.DrawGrid(10, 0.2f, Color.gray, this); // * zoom
            NodeMachineGUIUtils.DrawGrid(50, 0.4f, Color.gray, this); // * zoom

            foreach (Link link in _model.GetLinks())
            {
                if (livePreview)
                {
                    NodeMachineGUIUtils.DrawLink(link, _selectedMachine.CurrentLinks.Contains(link), this);
                }
                else
                {
                    NodeMachineGUIUtils.DrawLink(link, false, this);
                }
            }

            if (_creatingLink)
            {
                Rect fromTransform = new Rect(_selectedNode.drawnTransform.position - _nodeEditor.position, _selectedNode.drawnTransform.size);
                NodeMachineGUIUtils.DrawArrow(fromTransform, Event.current.mousePosition, this);
            }

            foreach (Node node in _model.GetNodes())
            {
                if (livePreview && node is RunnableNode)
                {
                    RunnableNode runnable = node as RunnableNode;
                    if (_selectedMachine.CurrentRunnables.Contains(node))
                    {
                        if (NodeMachineGUIUtils.DrawNode(node, runnable.activeBackground, this, Event.current))
                        {
                            modelNeedsSaving = true;
                        }
                    }
                    else
                    {
                        if (NodeMachineGUIUtils.DrawNode(node, this, Event.current))
                        {
                            modelNeedsSaving = true;
                        }
                    }
                }
                else
                {
                    if (NodeMachineGUIUtils.DrawNode(node, this, Event.current))
                    {
                        modelNeedsSaving = true;
                    }
                }
            }

            // Draw transparent links over everything - looks like links are showing through from behind nodes
            if (_drawTransparentLinks)
            {
                foreach (Link link in _model.GetLinks())
                {
                    if (livePreview)
                    {
                        NodeMachineGUIUtils.DrawTransparentLink(link, _selectedMachine.CurrentLinks.Contains(link), this);
                    }
                    else
                    {
                        NodeMachineGUIUtils.DrawTransparentLink(link, false, this);
                    }
                }
            }

            GUILayout.EndArea();

            // --------------------- PROCESS EVENTS ----------------------------

            if (_nodeEditor.Contains(Event.current.mousePosition))
            {
                Vector2 totalOffset = _offset + _nodeEditor.position;
                foreach (Node node in _model.GetNodes().Reverse())
                {
                    if (node.visible || _showInivisibleNodes)
                    {
                        if (node.ProcessEvents(Event.current, _zoom, SelectNode, ProcessNodeContextMenu))
                        {
                            modelNeedsSaving = true;
                        }
                    }
                }
                foreach (Link link in _model.GetLinks())
                {
                    link.ProcessEvents(Event.current, totalOffset, ProcessLinkContextMenu, SelectLink);
                }
            }

            ProcessEvents(Event.current);

            if (modelNeedsSaving)
            {
                MarkUnsaved();
            }
            if (GUI.changed)
            {
                Repaint();
            }
        }