private void UpdateNodePositionMetadata(StateNode node, Vector2 position)
 {
     DoWithUndoAndDirtyFlag(() =>
     {
         RebuildMetadata();
     });
 }
示例#2
0
        public TransitionConnection(StateNode source, StateNode target, Type triggerType, Dictionary <object, ScriptableGuardCondition[]> triggersWithGuardConditions = null)
        {
            Source = source;
            Target = target;

            _triggerType = triggerType;

            if (triggersWithGuardConditions != null)
            {
                foreach (var triggerWithGuardConditions in triggersWithGuardConditions)
                {
                    var t = new TriggerWithGuardConditions(_triggerType, triggerWithGuardConditions.Key, triggerWithGuardConditions.Value);
                    t.OnTriggerChanged         += CallOnTriggersWithGuardConditionsChangedEvent;
                    t.OnGuardConditionsChanged += CallOnTriggersWithGuardConditionsChangedEvent;

                    _triggersWithGuardConditionsList.Add(t);
                }
            }

            _controlsAreaStyle         = new GUIStyle();
            _controlsAreaStyle.padding = new RectOffset(ControlPaddingLeft, ControlPaddingRight, ControlPaddingTop, ControlPaddingBottom);

            _simpleLabelStyle          = new GUIStyle();
            _simpleLabelStyle.wordWrap = true;

            _currentColor = NormalColor;
        }
 private void RemoveTransitionsRelatedTo(StateNode node)
 {
     for (int i = 0; i < _transitions.Count; i++)
     {
         if (_transitions[i].Source == node || _transitions[i].Target == node)
         {
             RemoveTransition(_transitions[i]);
             i--;
         }
     }
 }
        public void SetInitialStateNode(StateNode node)
        {
            _nodes.ForEach(n =>
            {
                if (n.IsInitial)
                {
                    n.AsNormal();
                }
            });

            node.AsInitial();
        }
        private bool ContainsTransitionWithSourceAndTarget(StateNode source, StateNode target)
        {
            for (int i = 0; i < _transitions.Count; i++)
            {
                if (_transitions[i].Source == source && _transitions[i].Target == target)
                {
                    return(true);
                }
            }

            return(false);
        }
示例#6
0
        public ParentConnection(StateNode parent, StateNode child)
        {
            Parent = parent;
            Child  = child;

            _controlsAreaStyle         = new GUIStyle();
            _controlsAreaStyle.padding = new RectOffset(ControlPaddingLeft, ControlPaddingRight, ControlPaddingTop, ControlPaddingBottom);

            _simpleLabelStyle          = new GUIStyle();
            _simpleLabelStyle.wordWrap = true;

            _currentColor = NormalColor;
        }
        private void RemoveParentConnectionsRelatedTo(StateNode node)
        {
            RemoveChildFromParent(node);

            for (int i = 0; i < _parentConnections.Count; i++)
            {
                if (node == _parentConnections[i].Parent)
                {
                    RemoveChildFromParent(_parentConnections[i].Child);
                    i--;
                }
            }
        }
        public void SetInitialChildNodeOf(StateNode parent, StateNode initialChild)
        {
            var parentConnections = _parentConnections.Where(connection => connection.Parent == parent);

            foreach (var connection in parentConnections)
            {
                connection.Child.AsNormal();
            }

            initialChild.AsInitialChild();

            _builder.SetInitialChildOf(parent.StateId, initialChild.StateId);
        }
        public StateNode GetParentOf(StateNode node)
        {
            for (int i = 0; i < _parentConnections.Count; i++)
            {
                var current = _parentConnections[i];

                if (current.Child == node)
                {
                    return(current.Parent);
                }
            }

            return(null);
        }
        public bool HasParent(StateNode node)
        {
            for (int i = 0; i < _parentConnections.Count; i++)
            {
                var current = _parentConnections[i];

                if (current.Child == node)
                {
                    return(true);
                }
            }

            return(false);
        }
        private void InternalAddNode(StateNode node)
        {
            DoWithUndoAndDirtyFlag(() =>
            {
                node.OnStateIdChanged     += (changedNode, previousId, currentId) => RecordAndRebuild();
                node.OnStateObjectChanged += (changedNode, previousStateObj, currentStateObj) => RecordAndRebuild();
                node.OnPositionChanged    += UpdateNodePositionMetadata;

                _nodes.Add(node);

                if (_nodes.Count == 1)
                {
                    SetInitialStateNode(node);
                }
            });
        }
        public void AddTransition(StateNode source, StateNode target)
        {
            DoWithUndoAndDirtyFlag(() =>
            {
                if (ContainsTransitionWithSourceAndTarget(source, target))
                {
                    return;
                }

                var newTransition = new TransitionConnection(source, target, _builder.TriggerType);

                newTransition.OnTriggersWithGuardConditionsChanged += connection => RecordAndRebuild();

                _transitions.Add(newTransition);
            });
        }
示例#13
0
        private void DisplayNodeOptionsAtPosition(StateNode node)
        {
            GenericMenu genericMenu = new GenericMenu();

            if (_window.HasParent(node))
            {
                genericMenu.AddItem(new GUIContent("Set as initial child"), false, () => _window.SetInitialChildNodeOf(_window.GetParentOf(node), node));
            }
            else
            {
                genericMenu.AddItem(new GUIContent("Set as initial state"), false, () => _window.SetInitialStateNode(node));
            }

            genericMenu.AddItem(new GUIContent("Add transition"), false, () => _window.BeginTransitionPreviewFrom(node));
            genericMenu.AddItem(new GUIContent("Add child state"), false, () => _window.BeginParentConnectionPreviewFrom(node));
            genericMenu.AddItem(new GUIContent("Remove node"), false, () => _window.RemoveNode(node));
            genericMenu.ShowAsContext();
        }
示例#14
0
        public void HandleEventFor(StateNode node, Event nodeEvent)
        {
            switch (nodeEvent.type)
            {
            case EventType.MouseDown:

                if (IsLeftMouseClick(nodeEvent.button))
                {
                    if (node.IsPointOverNode(nodeEvent.mousePosition))
                    {
                        if (_window.HasTransitionPreview())
                        {
                            _window.AddTransition(_window.GetSourceNodeFromTransitionPreview(), node);
                            _window.EndTransitionPreview();
                        }
                        else if (_window.HasParentConnectionPreview())
                        {
                            _window.AddChildTo(_window.GetParentNodeFromParentConnectionPreview(), node);
                            _window.EndParentConnectionPreview();
                        }

                        _window.Select(node);
                        nodeEvent.Use();
                    }
                }
                else if (IsRightMouseClick(nodeEvent.button) && _window.IsSelected(node))
                {
                    DisplayNodeOptionsAtPosition(node);
                    nodeEvent.Use();
                }

                break;

            case EventType.MouseDrag:

                if (IsLeftMouseClick(nodeEvent.button) && _window.IsSelected(node))
                {
                    node.Drag(nodeEvent.delta);
                    nodeEvent.Use();
                }

                break;
            }
        }
        public void RemoveChildFromParent(StateNode child)
        {
            for (int i = 0; i < _parentConnections.Count; i++)
            {
                if (_parentConnections[i].Child == child)
                {
                    child.AsNormal();

                    if (IsSelected(_parentConnections[i]))
                    {
                        DeselectAll();
                    }

                    _parentConnections.RemoveAt(i);

                    RecordAndRebuild();
                    break;
                }
            }
        }
        public void RemoveNode(StateNode node)
        {
            if (_nodes.Remove(node))
            {
                RemoveTransitionsRelatedTo(node);
                RemoveParentConnectionsRelatedTo(node);

                if (node.IsInitial)
                {
                    SetInitialStateNode(GetRoots()[0]);
                }

                if (IsSelected(node))
                {
                    DeselectAll();
                }

                RecordAndRebuild();
            }
        }
        public void AddChildTo(StateNode parent, StateNode child)
        {
            if (parent == child || HasParent(child) || GetParentOf(parent) == child)
            {
                return;
            }

            _parentConnections.Add(new ParentConnection(parent, child));

            if (child.IsInitial)
            {
                SetInitialStateNode(GetRoots()[0]);
            }

            if (_parentConnections.Where(connection => connection.Parent == parent).Count() == 1)
            {
                SetInitialChildNodeOf(parent, child);
            }

            RecordAndRebuild();
        }
        private void RebuildMetadata()
        {
            _metadata.StateNodesMetadata.Clear();

            _builder.RemoveMetadata(MetadataKey);

            var states = _builder.GetStates();

            if (states == null)
            {
                return;
            }

            for (int i = 0; i < states.Length; i++)
            {
                StateInfo currentState     = states[i];
                StateNode currentStateNode = GetNodeOf(currentState);

                _metadata.StateNodesMetadata.Add(new StateNodeMetadata(currentState.StateId, currentStateNode.Position));
            }

            _builder.SaveMetadata(MetadataKey, _metadata);
        }
 public TransitionConnectionPreview(StateNode source)
 {
     Source = source;
 }
 public void AddParentConnectionFrom(StateNode parent, StateNode child)
 {
     _parentConnections.Add(new ParentConnection(parent, child));
 }
 public void BeginParentConnectionPreviewFrom(StateNode parent)
 {
     _parentConnectionPreview = new ParentConnectionPreview(parent);
 }
 public void BeginTransitionPreviewFrom(StateNode source)
 {
     _transitionPreview = new TransitionConnectionPreview(source);
 }
示例#23
0
 public ParentConnectionPreview(StateNode parent)
 {
     Parent = parent;
 }
        private void AddNodeFrom(StateInfo stateInfo, StateNodeMetadata metadata)
        {
            var newNode = new StateNode(metadata.Position, _builder.StateIdType, GenerateStateNodeId(), stateInfo.StateObject, stateInfo.StateId);

            InternalAddNode(newNode);
        }
        public void AddNode(Vector2 mousePosition, ScriptableState stateObject = null)
        {
            var newNode = new StateNode(mousePosition, _builder.StateIdType, GenerateStateNodeId(), stateObject);

            InternalAddNode(newNode);
        }