示例#1
0
        /// <summary>
        /// Performs a state transition for a gesture
        /// </summary>
        /// <param name="eventId">The gesture for which to change state</param>
        public virtual void PerformTransition(FSMEventId eventId)
        {
            FSMStateId        oldStateId    = this.currentState != null ? this.currentState.Id : FSMStateId.Unknown;
            var               transitionKey = this.GetTransitionKey(eventId);
            IState <TContext> oldState      = this.currentState;

            if (!transitions.ContainsKey(transitionKey))
            {
                throw new ApplicationException(string.Format("No transition for event {0} found", eventId.ToString()));
            }

            currentState = transitions[transitionKey];

            if (currentState == null)
            {
                throw new ApplicationException(string.Format("Invalid state for event {0}", eventId.ToString()));
            }

            // we only enter/exit states if anything really changed
            if (currentState.Id != transitionKey.Key)
            {
                if (oldState != null)
                {
                    oldState.StateExited();
                }

                currentState.StateEntered(this.sharedContext);

                if (this.StateChanged != null)
                {
                    this.StateChanged(this, new StateChangedEventArgs()
                    {
                        OldState = oldStateId, NewState = currentState.Id
                    });
                }
            }
        }
示例#2
0
        /// <summary>
        /// Add a state transition to the FSM
        /// </summary>
        /// <param name="state">The state</param>
        /// <param name="eventId">The transition event</param>
        public void AddTransition(FSMStateId fromState, FSMEventId eventId, IState <TContext> state)
        {
            var transitionKey = new KeyValuePair <FSMStateId, FSMEventId>(fromState, eventId);

            Debug.Assert(!transitions.ContainsKey(transitionKey), string.Format("Overwriting gesture {0}", eventId.ToString()));

            state.Controller = this;
            this.transitions[transitionKey] = state;
        }