Esempio n. 1
0
        public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context)
        {
            Node node = null;

            switch (SearchTreeEntry.name)
            {
            case "Clip":
                node = new ClipNodeUI();

                break;

            case "Layer Mixer":
                node = new LayerMixerNodeUI();

                break;

            case "Mixer":
                node = new MixerNodeUI();

                break;

            case "1D Blendspace":
                node = new Blendspace1DNodeUI();

                break;

            case "2D Blendspace":
                node = new Blendspace2DNodeUI();

                break;

            case "State Machine":
                node = new StateMachineNodeUI();

                break;

            case "Wildcard":
                node = new WildcardNodeUI();

                break;

            case "Output":
                node = new OutputNodeUI();
                GraphView.OutputIndicatorNode = (OutputNodeUI)node;
                break;

            default:
                break;
            }

            if (node is BaseNodeUI baseNode)
            {
                baseNode.ID = Guid.NewGuid().ToString();
            }

            GraphView.AddNode(node, context.screenMousePosition);

            return(true);
        }
        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);
        }
Esempio n. 3
0
        private void OpenAnimationGraphAsset(AnimationGraphAsset asset)
        {
            AnimationGraphAsset = asset;

            _closeButton.style.display = DisplayStyle.Flex;

            _assetName.style.display = DisplayStyle.Flex;
            _assetName.text          = asset.name;

            _closeToAssetNameSpacer.style.display = DisplayStyle.Flex;
            _assetNameToSaveSpacer.style.display  = DisplayStyle.Flex;

            rootVisualElement.Remove(GraphView);
            CreateGraphView();

            Dictionary <NodeAsset, BaseNodeUI> nodeMap = new Dictionary <NodeAsset, BaseNodeUI>();

            asset.Nodes.ForEach(n =>
            {
                BaseNodeUI nodeUI = null;

                switch (n.Data)
                {
                case ClipNode clipNode:
                    nodeUI = new ClipNodeUI();
                    break;

                case MixerNode mixerNode:
                    nodeUI = new MixerNodeUI();
                    break;

                case LayerMixerNode layerMixerNode:
                    nodeUI = new LayerMixerNodeUI();
                    break;

                case Blendspace1DNode blendspace1DNode:
                    nodeUI = new Blendspace1DNodeUI();
                    break;

                case Blendspace2DNode blendspace2DNode:
                    nodeUI = new Blendspace2DNodeUI();
                    break;

                case StateMachineNode stateMachineNode:
                    nodeUI = new StateMachineNodeUI();
                    break;

                case WildcardNode wildcardNode:
                    nodeUI = new WildcardNodeUI();
                    break;

                default:
                    break;
                }

                if (nodeUI != null)
                {
                    GraphView.AddNode(nodeUI);
                    nodeUI.SetPosition(new Rect(n.Position, Vector2.zero));
                    nodeUI.expanded = n.IsExpanded;
                    nodeMap.Add(n, nodeUI);
                }
            });

            foreach (var entry in nodeMap)
            {
                entry.Value.LoadData(GraphView, entry.Key, nodeMap);
            }

            if (asset.OutputIndicatorNode != null)
            {
                var outputIndicatorNode = new OutputNodeUI();
                GraphView.OutputIndicatorNode = outputIndicatorNode;
                GraphView.AddNode(outputIndicatorNode);
                outputIndicatorNode.SetPosition(new Rect(asset.OutputIndicatorNode.Position, Vector2.zero));
                outputIndicatorNode.expanded = asset.OutputIndicatorNode.IsExpanded;

                if (asset.OutputNode != null)
                {
                    BaseNodeUI outputNode = nodeMap[asset.OutputNode];
                    Edge       edge       = new Edge {
                        output = outputNode.OutputPort, input = outputIndicatorNode.InputPort
                    };
                    edge.input.Connect(edge);
                    edge.output.Connect(edge);
                    GraphView.AddElement(edge);
                    GraphView.OutputNode = outputNode;
                }
            }
        }