Ejemplo n.º 1
0
        public bool GoToState(VisualState state, bool useTransitions)
        {
            if (state == CurrentState)
            {
                return(true);
            }

            string           currentStateName     = CurrentState != null ? CurrentState.Name : String.Empty;
            VisualTransition transition           = useTransitions ? GetTransition(Transitions, currentStateName, state.Name) : null;
            Storyboard       transitionStoryboard = transition != null ? transition.Storyboard : null;

            Storyboard storyboard;

            if (transitionStoryboard != null && state.Storyboard != null)
            {
                // create a sequential animation with the transition storyboard first and then the state storyboard
                SequentialTimeline sequentialTimeline = new SequentialTimeline();
                sequentialTimeline.Children.Add(transitionStoryboard);
                sequentialTimeline.Children.Add(state.Storyboard);

                storyboard = new Storyboard();
                storyboard.Children.Add(sequentialTimeline);
            }
            else
            {
                storyboard = transitionStoryboard ?? state.Storyboard;
            }

            StartNewStoryboard(storyboard);

            CurrentState = state;
            return(true);
        }
Ejemplo n.º 2
0
        // Token: 0x06000D02 RID: 3330 RVA: 0x00030728 File Offset: 0x0002E928
        internal static VisualTransition GetTransition(FrameworkElement element, VisualStateGroup group, VisualState from, VisualState to)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            if (group == null)
            {
                throw new ArgumentNullException("group");
            }
            if (to == null)
            {
                throw new ArgumentNullException("to");
            }
            VisualTransition visualTransition  = null;
            VisualTransition visualTransition2 = null;
            int num = -1;
            IList <VisualTransition> list = (IList <VisualTransition>)group.Transitions;

            if (list != null)
            {
                foreach (VisualTransition visualTransition3 in list)
                {
                    if (visualTransition2 == null && visualTransition3.IsDefault)
                    {
                        visualTransition2 = visualTransition3;
                    }
                    else
                    {
                        int         num2   = -1;
                        VisualState state  = group.GetState(visualTransition3.From);
                        VisualState state2 = group.GetState(visualTransition3.To);
                        if (from == state)
                        {
                            num2++;
                        }
                        else if (state != null)
                        {
                            continue;
                        }
                        if (to == state2)
                        {
                            num2 += 2;
                        }
                        else if (state2 != null)
                        {
                            continue;
                        }
                        if (num2 > num)
                        {
                            num = num2;
                            visualTransition = visualTransition3;
                        }
                    }
                }
            }
            return(visualTransition ?? visualTransition2);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get the most appropriate transition between two states.
        /// </summary>
        /// <param name="element">Element being transitioned.</param>
        /// <param name="group">Group being transitioned.</param>
        /// <param name="from">VisualState being transitioned from.</param>
        /// <param name="to">VisualState being transitioned to.</param>
        /// <returns>
        /// The most appropriate transition between the desired states.
        /// </returns>
        internal static VisualTransition GetTransition(FrameworkElement element, VisualStateGroup group, VisualState from, VisualState to)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            if (group == null)
            {
                throw new ArgumentNullException("group");
            }

            if (to == null)
            {
                throw new ArgumentNullException("to");
            }

            VisualTransition best = null;
            VisualTransition defaultTransition = null;
            int bestScore = -1;

            IList <VisualTransition> transitions = (IList <VisualTransition>)group.Transitions;

            if (transitions != null)
            {
                foreach (VisualTransition transition in transitions)
                {
                    if (defaultTransition == null && transition.IsDefault)
                    {
                        defaultTransition = transition;
                        continue;
                    }

                    int score = -1;

                    VisualState transitionFromState = group.GetState(transition.From);
                    VisualState transitionToState   = group.GetState(transition.To);

                    if (from == transitionFromState)
                    {
                        score += 1;
                    }
                    else if (transitionFromState != null)
                    {
                        continue;
                    }

                    if (to == transitionToState)
                    {
                        score += 2;
                    }
                    else if (transitionToState != null)
                    {
                        continue;
                    }

                    if (score > bestScore)
                    {
                        bestScore = score;
                        best      = transition;
                    }
                }
            }

            return(best ?? defaultTransition);
        }
Ejemplo n.º 4
0
        private static Storyboard GenerateDynamicTransitionAnimations(FrameworkElement root, VisualStateGroup group, VisualState newState, VisualTransition transition)
        {
            IEasingFunction easingFunction = null;
            Storyboard      dynamic        = new Storyboard();

            if (transition != null)
            {
                if (transition.GeneratedDuration != null)
                {
                    dynamic.Duration = transition.GeneratedDuration;
                }

                easingFunction = transition.GeneratedEasingFunction;
            }
            else
            {
                dynamic.Duration = new Duration(TimeSpan.Zero);
            }

            Dictionary <TimelineDataToken, Timeline> currentAnimations    = FlattenTimelines(group.CurrentStoryboards);
            Dictionary <TimelineDataToken, Timeline> transitionAnimations = FlattenTimelines(transition != null ? transition.Storyboard : null);
            Dictionary <TimelineDataToken, Timeline> newStateAnimations   = FlattenTimelines(newState.Storyboard);

            // Remove any animations that the transition already animates.
            // There is no need to create an interstitial animation if one already exists.
            foreach (KeyValuePair <TimelineDataToken, Timeline> pair in transitionAnimations)
            {
                currentAnimations.Remove(pair.Key);
                newStateAnimations.Remove(pair.Key);
            }

            // Generate the "to" animations
            foreach (KeyValuePair <TimelineDataToken, Timeline> pair in newStateAnimations)
            {
                // The new "To" Animation -- the root is passed as a reference point for name
                // lookup.
                Timeline toAnimation = GenerateToAnimation(root, pair.Value, easingFunction, true);

                // If the animation is of a type that we can't generate transition animations
                // for, GenerateToAnimation will return null, and we should just keep going.
                if (toAnimation != null)
                {
                    toAnimation.Duration = dynamic.Duration;
                    dynamic.Children.Add(toAnimation);
                }

                // Remove this from the list of current state animations we have to consider next
                currentAnimations.Remove(pair.Key);
            }

            // Generate the "from" animations
            foreach (KeyValuePair <TimelineDataToken, Timeline> pair in currentAnimations)
            {
                Timeline fromAnimation = GenerateFromAnimation(root, pair.Value, easingFunction);
                if (fromAnimation != null)
                {
                    fromAnimation.Duration = dynamic.Duration;
                    dynamic.Children.Add(fromAnimation);
                }
            }

            return(dynamic);
        }
Ejemplo n.º 5
0
        private static bool GoToStateInternal(FrameworkElement control, FrameworkElement stateGroupsRoot, VisualStateGroup group, VisualState state, bool useTransitions)
        {
            if (stateGroupsRoot == null)
            {
                throw new ArgumentNullException("stateGroupsRoot");
            }

            if (state == null)
            {
                throw new ArgumentNullException("state");
            }

            if (group == null)
            {
                throw new InvalidOperationException();
            }

            VisualState lastState = group.CurrentState;

            if (lastState == state)
            {
                return(true);
            }

            // Get the transition Storyboard. Even if there are no transitions specified, there might
            // be properties that we're rolling back to their default values.
            VisualTransition transition = useTransitions ? VisualStateManager.GetTransition(stateGroupsRoot, group, lastState, state) : null;

            // Generate dynamicTransition Storyboard
            Storyboard dynamicTransition = GenerateDynamicTransitionAnimations(stateGroupsRoot, group, state, transition);

            // If the transition is null, then we want to instantly snap. The dynamicTransition will
            // consist of everything that is being moved back to the default state.
            // If the transition.Duration and explicit storyboard duration is zero, then we want both the dynamic
            // and state Storyboards to happen in the same tick, so we start them at the same time.
            if (transition == null || (transition.GeneratedDuration == DurationZero &&
                                       (transition.Storyboard == null || transition.Storyboard.Duration == DurationZero)))
            {
                // Start new state Storyboard and stop any previously running Storyboards
                if (transition != null && transition.Storyboard != null)
                {
                    group.StartNewThenStopOld(stateGroupsRoot, transition.Storyboard, state.Storyboard);
                }
                else
                {
                    group.StartNewThenStopOld(stateGroupsRoot, state.Storyboard);
                }

                // Fire both CurrentStateChanging and CurrentStateChanged events
                group.RaiseCurrentStateChanging(stateGroupsRoot, lastState, state, control);
                group.RaiseCurrentStateChanged(stateGroupsRoot, lastState, state, control);
            }
            else
            {
                // In this case, we have an interstitial storyboard of duration > 0 and/or
                // explicit storyboard of duration >0 , so we need
                // to run them first, and then we'll run the state storyboard.
                // we have to wait for both storyboards to complete before
                // starting the steady state animations.
                transition.DynamicStoryboardCompleted = false;

                // Hook up generated Storyboard's Completed event handler
                dynamicTransition.Completed += delegate(object sender, EventArgs e)
                {
                    if (transition.Storyboard == null || transition.ExplicitStoryboardCompleted)
                    {
                        if (ShouldRunStateStoryboard(control, stateGroupsRoot, state, group))
                        {
                            group.StartNewThenStopOld(stateGroupsRoot, state.Storyboard);
                        }

                        group.RaiseCurrentStateChanged(stateGroupsRoot, lastState, state, control);
                    }

                    transition.DynamicStoryboardCompleted = true;
                };

                if (transition.Storyboard != null && transition.ExplicitStoryboardCompleted == true)
                {
                    EventHandler transitionCompleted = null;
                    transitionCompleted = new EventHandler(delegate(object sender, EventArgs e)
                    {
                        if (transition.DynamicStoryboardCompleted)
                        {
                            if (ShouldRunStateStoryboard(control, stateGroupsRoot, state, group))
                            {
                                group.StartNewThenStopOld(stateGroupsRoot, state.Storyboard);
                            }

                            group.RaiseCurrentStateChanged(stateGroupsRoot, lastState, state, control);
                        }

                        transition.Storyboard.Completed       -= transitionCompleted;
                        transition.ExplicitStoryboardCompleted = true;
                    });

                    // hook up explicit storyboard's Completed event handler
                    transition.ExplicitStoryboardCompleted = false;
                    transition.Storyboard.Completed       += transitionCompleted;
                }

                // Start transition and dynamicTransition Storyboards
                // Stop any previously running Storyboards
                group.StartNewThenStopOld(stateGroupsRoot, transition.Storyboard, dynamicTransition);

                group.RaiseCurrentStateChanging(stateGroupsRoot, lastState, state, control);
            }

            group.CurrentState = state;

            return(true);
        }
        private static Storyboard GenerateDynamicTransitionAnimations(FrameworkElement root, VisualStateGroup group, VisualState newState, VisualTransition transition)
        {
            Storyboard dynamic = new Storyboard();

            if (transition != null && transition.GeneratedDuration != null)
            {
                dynamic.Duration = transition.GeneratedDuration;
            }
            else
            {
                dynamic.Duration = new Duration(TimeSpan.Zero);
            }

            Dictionary <TimelineDataToken, Timeline> currentAnimations    = FlattenTimelines(group.CurrentStoryboards);
            Dictionary <TimelineDataToken, Timeline> transitionAnimations = FlattenTimelines(transition != null ? transition.Storyboard : null);
            Dictionary <TimelineDataToken, Timeline> newStateAnimations   = FlattenTimelines(newState.Storyboard);

            // Remove any animations that the transition already animates.
            // There is no need to create an interstitial animation if one already exists.
            foreach (KeyValuePair <TimelineDataToken, Timeline> pair in transitionAnimations)
            {
                currentAnimations.Remove(pair.Key);
                newStateAnimations.Remove(pair.Key);
            }

            // Generate the "to" animations
            foreach (KeyValuePair <TimelineDataToken, Timeline> pair in newStateAnimations)
            {
                // The new "To" Animation -- the root is passed as a reference point for name
                // lookup.
                Timeline toAnimation = GenerateToAnimation(root, pair.Value, true);

                // If the animation is of a type that we can't generate transition animations
                // for, GenerateToAnimation will return null, and we should just keep going.
                if (toAnimation != null)
                {
                    toAnimation.Duration = dynamic.Duration;
                    dynamic.Children.Add(toAnimation);
                }

                // Remove this from the list of current state animations we have to consider next
                currentAnimations.Remove(pair.Key);
            }

            // Generate the "from" animations
            foreach (KeyValuePair <TimelineDataToken, Timeline> pair in currentAnimations)
            {
                Timeline fromAnimation = GenerateFromAnimation(pair.Value);
                if (fromAnimation != null)
                {
                    fromAnimation.Duration = dynamic.Duration;
                    string targetName = Storyboard.GetTargetName(pair.Value);
                    Storyboard.SetTargetName(fromAnimation, targetName);

                    // If the targetName of the existing Animation is known, then look up the
                    // target
                    DependencyObject target = String.IsNullOrEmpty(targetName) ?
                                              null : root.FindName(targetName) as DependencyObject;
                    if (target != null)
                    {
                        Storyboard.SetTarget(fromAnimation, target);
                    }

                    PropertyPath propertyName = Storyboard.GetTargetProperty(pair.Value);
                    Storyboard.SetTargetProperty(fromAnimation, propertyName);
                    dynamic.Children.Add(fromAnimation);
                }
            }

            return(dynamic);
        }
Ejemplo n.º 7
0
        // Token: 0x06000CFE RID: 3326 RVA: 0x0003040C File Offset: 0x0002E60C
        private static Storyboard GenerateDynamicTransitionAnimations(FrameworkElement root, VisualStateGroup group, VisualState newState, VisualTransition transition)
        {
            IEasingFunction easingFunction = null;
            Storyboard      storyboard     = new Storyboard();

            if (transition != null)
            {
                Duration generatedDuration = transition.GeneratedDuration;
                storyboard.Duration = transition.GeneratedDuration;
                easingFunction      = transition.GeneratedEasingFunction;
            }
            else
            {
                storyboard.Duration = new Duration(TimeSpan.Zero);
            }
            Dictionary <VisualStateManager.TimelineDataToken, Timeline> dictionary  = VisualStateManager.FlattenTimelines(group.CurrentStoryboards);
            Dictionary <VisualStateManager.TimelineDataToken, Timeline> dictionary2 = VisualStateManager.FlattenTimelines((transition != null) ? transition.Storyboard : null);
            Dictionary <VisualStateManager.TimelineDataToken, Timeline> dictionary3 = VisualStateManager.FlattenTimelines(newState.Storyboard);

            foreach (KeyValuePair <VisualStateManager.TimelineDataToken, Timeline> keyValuePair in dictionary2)
            {
                dictionary.Remove(keyValuePair.Key);
                dictionary3.Remove(keyValuePair.Key);
            }
            foreach (KeyValuePair <VisualStateManager.TimelineDataToken, Timeline> keyValuePair2 in dictionary3)
            {
                Timeline timeline = VisualStateManager.GenerateToAnimation(root, keyValuePair2.Value, easingFunction, true);
                if (timeline != null)
                {
                    timeline.Duration = storyboard.Duration;
                    storyboard.Children.Add(timeline);
                }
                dictionary.Remove(keyValuePair2.Key);
            }
            foreach (KeyValuePair <VisualStateManager.TimelineDataToken, Timeline> keyValuePair3 in dictionary)
            {
                Timeline timeline2 = VisualStateManager.GenerateFromAnimation(root, keyValuePair3.Value, easingFunction);
                if (timeline2 != null)
                {
                    timeline2.Duration = storyboard.Duration;
                    storyboard.Children.Add(timeline2);
                }
            }
            return(storyboard);
        }
Ejemplo n.º 8
0
        // Token: 0x06000CFA RID: 3322 RVA: 0x000300A4 File Offset: 0x0002E2A4
        private static bool GoToStateInternal(FrameworkElement control, FrameworkElement stateGroupsRoot, VisualStateGroup group, VisualState state, bool useTransitions)
        {
            if (stateGroupsRoot == null)
            {
                throw new ArgumentNullException("stateGroupsRoot");
            }
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }
            if (group == null)
            {
                throw new InvalidOperationException();
            }
            VisualState lastState = group.CurrentState;

            if (lastState == state)
            {
                return(true);
            }
            VisualTransition transition = useTransitions ? VisualStateManager.GetTransition(stateGroupsRoot, group, lastState, state) : null;
            Storyboard       storyboard = VisualStateManager.GenerateDynamicTransitionAnimations(stateGroupsRoot, group, state, transition);

            if (transition == null || (transition.GeneratedDuration == VisualStateManager.DurationZero && (transition.Storyboard == null || transition.Storyboard.Duration == VisualStateManager.DurationZero)))
            {
                if (transition != null && transition.Storyboard != null)
                {
                    group.StartNewThenStopOld(stateGroupsRoot, new Storyboard[]
                    {
                        transition.Storyboard,
                        state.Storyboard
                    });
                }
                else
                {
                    group.StartNewThenStopOld(stateGroupsRoot, new Storyboard[]
                    {
                        state.Storyboard
                    });
                }
                group.RaiseCurrentStateChanging(stateGroupsRoot, lastState, state, control);
                group.RaiseCurrentStateChanged(stateGroupsRoot, lastState, state, control);
            }
            else
            {
                transition.DynamicStoryboardCompleted = false;
                storyboard.Completed += delegate(object sender, EventArgs e)
                {
                    if (transition.Storyboard == null || transition.ExplicitStoryboardCompleted)
                    {
                        if (VisualStateManager.ShouldRunStateStoryboard(control, stateGroupsRoot, state, group))
                        {
                            group.StartNewThenStopOld(stateGroupsRoot, new Storyboard[]
                            {
                                state.Storyboard
                            });
                        }
                        group.RaiseCurrentStateChanged(stateGroupsRoot, lastState, state, control);
                    }
                    transition.DynamicStoryboardCompleted = true;
                };
                if (transition.Storyboard != null && transition.ExplicitStoryboardCompleted)
                {
                    EventHandler transitionCompleted = null;
                    transitionCompleted = delegate(object sender, EventArgs e)
                    {
                        if (transition.DynamicStoryboardCompleted)
                        {
                            if (VisualStateManager.ShouldRunStateStoryboard(control, stateGroupsRoot, state, group))
                            {
                                group.StartNewThenStopOld(stateGroupsRoot, new Storyboard[]
                                {
                                    state.Storyboard
                                });
                            }
                            group.RaiseCurrentStateChanged(stateGroupsRoot, lastState, state, control);
                        }
                        transition.Storyboard.Completed       -= transitionCompleted;
                        transition.ExplicitStoryboardCompleted = true;
                    };
                    transition.ExplicitStoryboardCompleted = false;
                    transition.Storyboard.Completed       += transitionCompleted;
                }
                group.StartNewThenStopOld(stateGroupsRoot, new Storyboard[]
                {
                    transition.Storyboard,
                    storyboard
                });
                group.RaiseCurrentStateChanging(stateGroupsRoot, lastState, state, control);
            }
            group.CurrentState = state;
            return(true);
        }