コード例 #1
0
        public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context)
        {
            StateMachineBaseNodeUI node = null;

            switch (SearchTreeEntry.name)
            {
            case "State":
                node = new StateNodeUI();
                break;

            case "Any State":
                node = new AnyStateNodeUI();
                break;

            case "Any State Priority Manager":
                node = new AnyStatePriorityManagerNodeUI();
                break;

            case "Parameter":
                node = new ParameterNodeUI();
                break;

            default:
                break;
            }

            GraphView.AddNode(node, context.screenMousePosition);

            if (node is StateNodeUI stateNode && States.Items.Count == 1)
            {
                CreateConnection(GraphView.EntryNode, stateNode, false);
            }

            if (node is INamedItem namedNode)
            {
                RenameEditor.Open(namedNode.Name, newName => namedNode.Name = newName);
            }

            return(true);
        }
コード例 #2
0
        public void LoadStateMachine(StateMachineNodeUI stateMachineUI)
        {
            CreateGraphView();

            void SetNodePositionAndExpansion(Node node, NodeVisualInfo info)
            {
                node.SetPosition(new Rect(info.Position, Vector2.zero));
                node.expanded = info.IsExpanded;
            }

            NodeUI = stateMachineUI;

            var stateMachine = (StateMachineNode)stateMachineUI.StateMachineNodeAsset.Data;

            var parameterMap     = new Dictionary <Parameter, ParameterNodeUI>();
            var valueProviderMap = new Dictionary <IValueProvider, StateMachineBaseNodeUI>();

            foreach (var parameter in stateMachine.Parameters.Values)
            {
                var parameterNode = new ParameterNodeUI();
                GraphView.AddNode(parameterNode);
                SetNodePositionAndExpansion(parameterNode, stateMachineUI.StateMachineNodeAsset.ParameterMap[parameter.Id]);
                parameterNode.LoadData(parameter);
                parameterMap.Add(parameter, parameterNode);
                valueProviderMap.Add(parameter.ValueProvider, parameterNode);
            }

            var stateToNodeMap = new Dictionary <State, StateNodeUI>();
            var nodeToStateMap = new Dictionary <StateNodeUI, State>();

            for (int i = 0; i < stateMachine.States.Count; i++)
            {
                string stateName = stateMachine.States.KeyAt(i);
                State  state     = stateMachine.States.At(i);

                var stateNode = new StateNodeUI();
                stateNode.Name = stateName;
                GraphView.AddNode(stateNode);
                SetNodePositionAndExpansion(stateNode, stateMachineUI.StateMachineNodeAsset.StateMap[state.Id]);
                stateToNodeMap.Add(state, stateNode);
                nodeToStateMap.Add(stateNode, state);
                valueProviderMap.Add(state.Time, stateNode);
                valueProviderMap.Add(state.PreviousTime, stateNode);
                valueProviderMap.Add(state.NormalizedTime, stateNode);
                valueProviderMap.Add(state.PreviousNormalizedTime, stateNode);
            }

            var anyStateMap = new Dictionary <AnyState, AnyStateNodeUI>();

            for (int i = 0; i < stateMachine.AnyStates.Items.Count; i++)
            {
                string   anyStateName = stateMachine.AnyStates.Items.KeyAt(i);
                AnyState anyState     = stateMachine.AnyStates.Items.At(i);

                //if (anyState == null) { continue; }

                var anyStateNode = new AnyStateNodeUI();
                anyStateNode.Name = anyStateName;
                GraphView.AddNode(anyStateNode);
                SetNodePositionAndExpansion(anyStateNode, stateMachineUI.StateMachineNodeAsset.AnyStateMap[anyState.Id]);
                anyStateNode.LoadData(GraphView, anyState, stateToNodeMap);
                anyStateMap.Add(anyState, anyStateNode);

                foreach (var transition in anyState.ExitTransitions)
                {
                    var transitionConnection = (TransitionConnectionUI)CreateConnection(anyStateNode, stateToNodeMap[transition.DestinationState], false);
                    LoadTransitionInfo(transitionConnection.Transitions[transitionConnection.Transitions.Count - 1], transition);
                }
            }

            if (NodeUI.StateMachineNodeAsset.AnyStatePriorityManager != null)
            {
                var anyStatePriorityManagerNode = new AnyStatePriorityManagerNodeUI();
                GraphView.AddNode(anyStatePriorityManagerNode);
                SetNodePositionAndExpansion(anyStatePriorityManagerNode, stateMachineUI.StateMachineNodeAsset.AnyStatePriorityManager);
                anyStatePriorityManagerNode.LoadData(GraphView, stateMachine.AnyStates.Items.Values, anyStateMap);
            }

            foreach (var state in stateMachine.States.Values)
            {
                foreach (var transition in state.ExitTransitions)
                {
                    StateNodeUI stateNode            = stateToNodeMap[(State)transition.SourceState];
                    var         transitionConnection = (TransitionConnectionUI)CreateConnection(stateNode, stateToNodeMap[transition.DestinationState], false);
                    LoadTransitionInfo(transitionConnection.Transitions[transitionConnection.Transitions.Count - 1], transition);
                }
            }

            void LoadTransitionInfo(TransitionInfo transitionInfo, Transition transition)
            {
                transitionInfo.DurationType            = transition.DurationType;
                transitionInfo.Duration                = transition.Duration;
                transitionInfo.OffsetType              = transition.OffsetType;
                transitionInfo.Offset                  = transition.Offset;
                transitionInfo.InterruptionSource      = transition.InterruptionSource;
                transitionInfo.OrderedInterruption     = transition.OrderedInterruption;
                transitionInfo.InterruptableByAnyState = transition.InterruptableByAnyState;
                transitionInfo.PlayAfterTransition     = transition.PlayAfterTransition;

                transition.Conditions.ForEach(condition =>
                {
                    TransitionInfoCondition infoCondition = new TransitionInfoCondition();
                    transitionInfo.Conditions.Add(infoCondition);

                    if (condition.ValueProvider == null)
                    {
                        return;
                    }

                    StateMachineBaseNodeUI node = valueProviderMap[condition.ValueProvider];

                    if (node is StateNodeUI stateNode)
                    {
                        State state = nodeToStateMap[stateNode];
                        infoCondition.ProviderSourceType = ValueProviderSourceType.State;
                        infoCondition.State = stateNode;
                        infoCondition.StateValueProvider = condition.ValueProvider == state.PreviousTime ? StateValueProviders.PreviousTime
                                                         : condition.ValueProvider == state.Time ? StateValueProviders.Time
                                                         : condition.ValueProvider == state.NormalizedTime ? StateValueProviders.NormalizedTime
                                                         : StateValueProviders.PreviousNormalizedTime;
                        FloatConditionEvaluator floatEvaluator = (FloatConditionEvaluator)condition.Evaluator;
                        infoCondition.FloatComparison          = floatEvaluator.Comparison;
                        infoCondition.FloatComparisonValue     = floatEvaluator.ComparisonValue;
                    }
                    else
                    {
                        ParameterNodeUI parameterNode    = (ParameterNodeUI)node;
                        infoCondition.ProviderSourceType = ValueProviderSourceType.Parameter;
                        infoCondition.Parameter          = parameterNode;

                        switch (condition.Evaluator)
                        {
                        case BoolConditionEvaluator boolEvaluator:
                            infoCondition.BoolComparisonValue = boolEvaluator.ComparisonValue == Bool.True;
                            break;

                        case IntConditionEvaluator intEvaluator:
                            infoCondition.IntComparison      = intEvaluator.Comparison;
                            infoCondition.IntComparisonValue = intEvaluator.ComparisonValue;
                            break;

                        case FloatConditionEvaluator floatEvaluator:
                            infoCondition.FloatComparison      = floatEvaluator.Comparison;
                            infoCondition.FloatComparisonValue = floatEvaluator.ComparisonValue;
                            break;
                        }
                    }
                });
            }

            EntryNodeUI entryStateNode = new EntryNodeUI();

            GraphView.AddNode(entryStateNode);
            GraphView.EntryNode = entryStateNode;
            if (NodeUI.StateMachineNodeAsset.EntryState != null)
            {
                SetNodePositionAndExpansion(entryStateNode, NodeUI.StateMachineNodeAsset.EntryState);
            }
            entryStateNode.LoadData(GraphView, stateMachine.EntryState, stateToNodeMap);
        }