void GetStoryboardCreationData( BubblesTask task, out int millisecondsPerUnit, out Func <ContentPresenter, double> getTo, out DependencyProperty animatedProperty, out IEnumerable <BubbleViewModel> bubbles) { switch (task.TaskType) { case BubblesTaskType.Burst: millisecondsPerUnit = 250; getTo = cp => (task.IsUndo ? 1.0 : 0.0); animatedProperty = UIElement.OpacityProperty; bubbles = task.Bubbles; break; case BubblesTaskType.MoveDown: millisecondsPerUnit = 115; getTo = _bubbleCanvas.CalculateTop; animatedProperty = Canvas.TopProperty; // Sort the bubbles to ensure that the columns move // in sync with each other in an appealing way. bubbles = from bubble in task.Bubbles orderby bubble.PreviousColumn orderby bubble.PreviousRow descending select bubble; break; case BubblesTaskType.MoveRight: millisecondsPerUnit = 115; getTo = _bubbleCanvas.CalculateLeft; animatedProperty = Canvas.LeftProperty; // Sort the bubbles to ensure that the rows move // in sync with each other in an appealing way. bubbles = from bubble in task.Bubbles orderby bubble.PreviousRow descending orderby bubble.PreviousColumn descending select bubble; break; default: throw new ArgumentException("Unrecognized BubblesTaskType: " + task.TaskType); } if (task.IsUndo) { bubbles = bubbles.Reverse(); } }
BubblesTask CreateUndoTask(BubblesTask originalTask) { var bubbles = originalTask.Bubbles.ToList(); Func <IEnumerable <BubbleViewModel> > getBubbles; Action complete; switch (originalTask.TaskType) { case BubblesTaskType.MoveRight: getBubbles = delegate { _bubbleMatrix.IsIdle = false; _bubbleMatrix.ResetBubbleGroup(); bubbles.ForEach(b => b.BeginUndo()); return(bubbles); }; complete = delegate { bubbles.ForEach(b => b.EndUndo()); }; break; case BubblesTaskType.MoveDown: getBubbles = delegate { bubbles.ForEach(b => b.BeginUndo()); return(bubbles); }; complete = delegate { bubbles.ForEach(b => b.EndUndo()); }; break; case BubblesTaskType.Burst: getBubbles = delegate { bubbles.ForEach(b => _bubbleMatrix.AddBubble(b)); return(bubbles); }; complete = delegate { _bubbleMatrix.IsIdle = true; }; break; default: throw new ArgumentException("Unrecognized task type: " + originalTask.TaskType); } return(new BubblesTask(originalTask.TaskType, true, getBubbles, complete)); }
Storyboard CreateStoryboard( BubblesTask task, int millisecondsPerUnit, Func <ContentPresenter, double> getTo, DependencyProperty animatedProperty, BubbleViewModel[] bubbles) { if (!bubbles.Any()) { return(null); } var storyboard = new Storyboard(); var targetProperty = new PropertyPath(animatedProperty); var beginTime = TimeSpan.FromMilliseconds(0); var beginTimeIncrement = TimeSpan.FromMilliseconds(millisecondsPerUnit / bubbles.Count()); foreach (ContentPresenter presenter in this.GetBubblePresenters(bubbles)) { var bubble = presenter.DataContext as BubbleViewModel; var duration = CalculateDuration(task.TaskType, bubble, millisecondsPerUnit); var to = getTo(presenter); var anim = new EasingDoubleAnimation { BeginTime = beginTime, Duration = duration, Equation = EasingEquation.CubicEaseIn, To = to, }; Storyboard.SetTarget(anim, presenter); Storyboard.SetTargetProperty(anim, targetProperty); if (IsTaskStaggered(task.TaskType)) { beginTime = beginTime.Add(beginTimeIncrement); } storyboard.Children.Add(anim); } return(storyboard); }
internal Storyboard CreateStoryboard(BubblesTask task) { int millisecondsPerUnit; Func <ContentPresenter, double> getTo; DependencyProperty animatedProperty; IEnumerable <BubbleViewModel> bubbles; this.GetStoryboardCreationData( task, out millisecondsPerUnit, out getTo, out animatedProperty, out bubbles); return(this.CreateStoryboard( task, millisecondsPerUnit, getTo, animatedProperty, bubbles.ToArray())); }
void PerformTask(BubblesTask task, Storyboard storyboard) { if (storyboard != null) { // There are some bubbles that need to be animated, so we must // wait until the Storyboard finishs before completing the task. storyboard.Completed += delegate { this.CompleteTask(task); }; // Freeze the Storyboard to improve perf. storyboard.Freeze(); // Start animating the bubbles associated with the task. storyboard.Begin(this); } else { // There are no bubbles associated with this task, // so immediately move to the task completion phase. this.CompleteTask(task); } }
void CompleteTask(BubblesTask task) { task.Complete(); this.ProcessNextTask(); }