private void AnimateMazeRunnerVisualToCell(int col, int row, TravelSpeed travelSpeed, int crumbCount) { _animationIsRunning = true; if (_batch != null) { _batch.Dispose(); } _batch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation); var animation = _compositor.CreateVector3KeyFrameAnimation(); var easing = _compositor.CreateLinearEasingFunction(); var destination = new Vector3((float)(col * _cellSize), (float)(row * _cellSize), 0.0F); animation.InsertKeyFrame(1.0f, destination, easing); switch (travelSpeed) { case TravelSpeed.Walk: animation.Duration = TimeSpan.FromMilliseconds(500 * crumbCount); break; case TravelSpeed.Run: animation.Duration = TimeSpan.FromSeconds(0.1); break; case TravelSpeed.Jump: animation.Duration = TimeSpan.FromMilliseconds(1); break; } _mazeRunnerVisual.StartAnimation(nameof(_mazeRunnerVisual.Offset), animation); _batch.End(); _batch.Completed += _batch_Completed; }
// This animation has constant duration, speedy changes depending on the distance // between sourceElement and targetElement. public async Task StartAnimation2(FrameworkElement sourceElement, FrameworkElement targetElement) { Point point = sourceElement.TransformToVisual(_rootElement).TransformPoint(new Point(0, 0)); CompositionDrawingSurface surface = await CompositionDrawingSurfaceFacade1.GetCompositionDrawingSurface(sourceElement, _compositor); SpriteVisual spriteVisual = _compositor.CreateSpriteVisual(); spriteVisual.Brush = _compositor.CreateSurfaceBrush(surface); spriteVisual.Size = new Vector2((float)surface.Size.Width, (float)surface.Size.Height); spriteVisual.Offset = new Vector3((float)point.X, (float)point.Y, 0f); _containerVisual.Children.InsertAtBottom(spriteVisual); Vector3 targetOffset = GetTargetOffset(targetElement); Vector3KeyFrameAnimation offsetAnimation = _compositor.CreateVector3KeyFrameAnimation(); Vector2 targetSize = GetTargetSize(targetElement); Vector2KeyFrameAnimation sizeAnimation = _compositor.CreateVector2KeyFrameAnimation(); var newWidth = (float)(sourceElement.ActualWidth * 1.3); var newHeight = (float)(sourceElement.ActualHeight * 1.3); var newX = (float)(point.X - (newWidth - sourceElement.ActualWidth) / 2); var newY = (float)(point.Y - (newHeight - sourceElement.ActualHeight) / 2); double sizeDurationInMs = 250; double distance = Math.Sqrt(Math.Pow(targetOffset.X - newX, 2) + Math.Pow(targetOffset.Y - newY, 2)); double offsetDurationInMs = distance / 2; double animationDurationInMs = sizeDurationInMs + offsetDurationInMs; sizeAnimation.Duration = TimeSpan.FromMilliseconds(animationDurationInMs); offsetAnimation.Duration = TimeSpan.FromMilliseconds(animationDurationInMs); SetAnimationDefautls(offsetAnimation); SetAnimationDefautls(sizeAnimation); var normalizedProgressKey0 = (float)(sizeDurationInMs / animationDurationInMs); offsetAnimation.InsertKeyFrame(normalizedProgressKey0, new Vector3(newX, newY, 0f)); sizeAnimation.InsertKeyFrame(normalizedProgressKey0, new Vector2(newWidth, newHeight)); const float normalizedProgressKey1 = 1f; offsetAnimation.InsertKeyFrame(normalizedProgressKey1, targetOffset, _compositor.CreateLinearEasingFunction()); sizeAnimation.InsertKeyFrame(normalizedProgressKey1, targetSize, _compositor.CreateLinearEasingFunction()); CompositionScopedBatch myScopedBatch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation); var batchCompletitionAwaiter = new BatchCompletitionAwaiter(myScopedBatch); spriteVisual.StartAnimation("Offset", offsetAnimation); spriteVisual.StartAnimation("Size", sizeAnimation); myScopedBatch.End(); await batchCompletitionAwaiter.Completed(); myScopedBatch.Dispose(); spriteVisual.Dispose(); surface.Dispose(); offsetAnimation.Dispose(); sizeAnimation.Dispose(); }
private void OnFlashTimerTick(object sender, object e) { _reverseAnimationActive = true; //Flip button visual var btn1Visual = ElementCompositionPreview.GetElementVisual(_firstButton); var btn2Visual = ElementCompositionPreview.GetElementVisual(_secondButton); var compositor = btn1Visual.Compositor; //Get a visual for the content var btn1Content = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(_firstButton, 0), 0); var btn1ContentVisual = ElementCompositionPreview.GetElementVisual(btn1Content as FrameworkElement); var btn2Content = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(_secondButton, 0), 0); var btn2ContentVisual = ElementCompositionPreview.GetElementVisual(btn2Content as FrameworkElement); var easing = compositor.CreateLinearEasingFunction(); if (_reverseFlipBatchAnimation != null) { _reverseFlipBatchAnimation.Completed -= ReverseFlipBatchAnimation_Completed; _reverseFlipBatchAnimation.Dispose(); } _reverseFlipBatchAnimation = compositor.CreateScopedBatch(CompositionBatchTypes.Animation); _reverseFlipBatchAnimation.Completed += ReverseFlipBatchAnimation_Completed; ScalarKeyFrameAnimation flipAnimation = compositor.CreateScalarKeyFrameAnimation(); flipAnimation.InsertKeyFrame(0.000001f, 0); flipAnimation.InsertKeyFrame(0.999999f, -180, easing); flipAnimation.InsertKeyFrame(1f, 0); flipAnimation.Duration = TimeSpan.FromMilliseconds(400); flipAnimation.IterationBehavior = AnimationIterationBehavior.Count; flipAnimation.IterationCount = 1; btn1Visual.CenterPoint = new Vector3((float)(0.5 * _firstButton.ActualWidth), (float)(0.5f * _firstButton.ActualHeight), 0f); btn1Visual.RotationAxis = new Vector3(0.0f, 1f, 0f); btn2Visual.CenterPoint = new Vector3((float)(0.5 * _secondButton.ActualWidth), (float)(0.5f * _secondButton.ActualHeight), 0f); btn2Visual.RotationAxis = new Vector3(0.0f, 1f, 0f); ScalarKeyFrameAnimation appearAnimation = compositor.CreateScalarKeyFrameAnimation(); appearAnimation.InsertKeyFrame(0.0f, 1); appearAnimation.InsertKeyFrame(0.399999f, 1); appearAnimation.InsertKeyFrame(0.4f, 0); appearAnimation.InsertKeyFrame(1f, 0); appearAnimation.Duration = TimeSpan.FromMilliseconds(400); appearAnimation.IterationBehavior = AnimationIterationBehavior.Count; appearAnimation.IterationCount = 1; btn1Visual.StartAnimation(nameof(btn1Visual.RotationAngleInDegrees), flipAnimation); btn2Visual.StartAnimation(nameof(btn2Visual.RotationAngleInDegrees), flipAnimation); btn1ContentVisual.StartAnimation(nameof(btn1ContentVisual.Opacity), appearAnimation); btn2ContentVisual.StartAnimation(nameof(btn2ContentVisual.Opacity), appearAnimation); _reverseFlipBatchAnimation.End(); _flashTimer.Stop(); GazeInput.SetInteraction(buttonMatrix, Interaction.Enabled); }
private void CleanupScopeBatch() { if (_scopeBatch != null) { _scopeBatch.Completed -= ScopeBatch_Completed; _scopeBatch.Dispose(); _scopeBatch = null; } }
async void ResetBoard() { PlayAgainText.Visibility = Visibility.Collapsed; _gameOver = false; _firstButton = null; _secondButton = null; _numMoves = 0; MoveCountTextBlock.Text = _numMoves.ToString(); _remaining = _boardRows * _boardColumns; var pairs = (_boardRows * _boardColumns) / 2; List <string> listContent; if (_usePictures) { try { listContent = await GetPicturesContent(pairs); } catch { listContent = GetSymbolContent(pairs); _usePictures = false; } } else { listContent = GetSymbolContent(pairs); } List <Button> listButtons = ShuffleList(GetButtonList()); var compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; if (_resetBatchAnimation != null) { _resetBatchAnimation.Completed -= ResetBatchAnimation_Completed; _resetBatchAnimation.Dispose(); } _resetBatchAnimation = compositor.CreateScopedBatch(CompositionBatchTypes.Animation); _resetBatchAnimation.Completed += ResetBatchAnimation_Completed;; foreach (Button button in listButtons) { FlipCardFaceDown(button); GazeInput.SetInteraction(button, Interaction.Inherited); } _resetBatchAnimation.End(); for (int i = 0; i < _boardRows * _boardColumns; i += 2) { listButtons[i].Tag = listContent[i / 2]; listButtons[i + 1].Tag = listContent[i / 2]; } }
/// <summary> /// 停止故事板。 /// </summary> public void Stop() { _animationBatch?.Dispose(); _animationBatch = null; foreach (var animation in Children) { animation.TargetVisual.StopAnimation(animation.TargetProperty.ToString()); } }
/// <summary> /// 开始故事板。 /// </summary> public void Start() { if (_animationBatch != null) { _animationBatch.Completed -= _animationBatch_Completed; _animationBatch.Dispose(); } _animationBatch = AnimationBatchFactory.Singleton.StartAnimations(Children); if (_animationBatch == null) { return; } _animationBatch.Completed += _animationBatch_Completed; }
private void Batch_Completed(object sender, CompositionBatchCompletedEventArgs args) { // 1. Remove event handler from storyboard CompositionScopedBatch target = sender as CompositionScopedBatch; if (target != null) { target.Completed -= Batch_Completed; target.Dispose(); _currentGroup?.Dispose(); // 1.1 If this is the most recent storyboard, allow us to interact with content if (target == _currentBatch) { RestoreContentPresenterInteractivity(_newPresenter); _newPresenter.IsHitTestVisible = true; } } else { // if no target, we've manually called this. Make it work. _newPresenter.IsHitTestVisible = true; RestoreContentPresenterInteractivity(_newPresenter); } _currentBatch = null; _currentGroup = null; // Remove the "old" content (i.e. the thing we've animated out) from the VisualTree ContentPresenter presenter; lock (_presenters) presenter = _presenters.Dequeue(); _clientArea.Children.Remove(presenter); presenter.Content = null; if (presenter == _oldPresenter) { _oldPresenter = null; } presenter = null; }
private void SlideBoard(int start, int end, int delta, int increment) { if (_busyAnimating) { return; } if (GameOver) { return; } _busyAnimating = true; var compositor = ElementCompositionPreview.GetElementVisual(Window.Current.Content).Compositor; if (_slideBatchAnimation != null) { _slideBatchAnimation.Completed -= SlideBatchAnimation_Completed; _slideBatchAnimation.Dispose(); } _slideBatchAnimation = compositor.CreateScopedBatch(CompositionBatchTypes.Animation); _slideBatchAnimation.Completed += SlideBatchAnimation_Completed; var firstslideBatchAnimation = compositor.CreateScopedBatch(CompositionBatchTypes.Animation); firstslideBatchAnimation.Completed += FirstslideBatchAnimation_Completed; firstslideBatchAnimation.Comment = start + "," + end + "," + delta + "," + increment; bool change = false; for (int i = 0; i < _boardSize; i++) { change = SlideRowOrCol(start, end, delta) || change; start += increment; end += increment; } firstslideBatchAnimation.Comment = firstslideBatchAnimation.Comment + "," + change; firstslideBatchAnimation.End(); }
private void HideMazeRunnerVisual(int crumbCount) { var animation = _compositor.CreateScalarKeyFrameAnimation(); var easing = _compositor.CreateLinearEasingFunction(); animation.InsertKeyFrame(1.0f, 0, easing); animation.Duration = TimeSpan.FromMilliseconds(50); if (crumbCount > 1) { crumbCount -= 1; } animation.DelayTime = TimeSpan.FromMilliseconds(500 * crumbCount); if ((_curRow == _numRows - 1) && (_curCol == _numCols - 1)) { animation.DelayTime = TimeSpan.FromMilliseconds(500); if (_endBatch != null) { _endBatch.Dispose(); } _endBatch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation); var scaleAnimation = _compositor.CreateVector3KeyFrameAnimation(); scaleAnimation.InsertKeyFrame(1f, new Vector3(0.50f, 0.50f, 0f)); scaleAnimation.Duration = TimeSpan.FromMilliseconds(500); _mazeRunnerVisual.CenterPoint = new System.Numerics.Vector3((float)_mazeRunnerVisual.Size.X / 2, (float)_mazeRunnerVisual.Size.Y, 0f); _mazeRunnerVisual.StartAnimation(nameof(_mazeRunnerVisual.Scale), scaleAnimation); _endBatch.End(); _endBatch.Completed += _endBatch_Completed; } _mazeRunnerVisual.StartAnimation(nameof(_mazeRunnerVisual.Opacity), animation); }
private async void FlipCardFaceUp(Button btn) { //Flip button visual var btnVisual = ElementCompositionPreview.GetElementVisual(btn); var compositor = btnVisual.Compositor; //Get a visual for the content var btnContent = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(btn, 0), 0); var btnContentVisual = ElementCompositionPreview.GetElementVisual(btnContent as FrameworkElement); var easing = compositor.CreateLinearEasingFunction(); if (_flipBatchAnimation != null) { _flipBatchAnimation.Completed -= FlipBatchAnimation_Completed; _flipBatchAnimation.Dispose(); } _flipBatchAnimation = compositor.CreateScopedBatch(CompositionBatchTypes.Animation); _flipBatchAnimation.Completed += FlipBatchAnimation_Completed; ScalarKeyFrameAnimation flipAnimation = compositor.CreateScalarKeyFrameAnimation(); flipAnimation.InsertKeyFrame(0.000001f, 180); flipAnimation.InsertKeyFrame(1f, 0, easing); flipAnimation.Duration = TimeSpan.FromMilliseconds(800); flipAnimation.IterationBehavior = AnimationIterationBehavior.Count; flipAnimation.IterationCount = 1; btnVisual.CenterPoint = new Vector3((float)(0.5 * btn.ActualWidth), (float)(0.5f * btn.ActualHeight), 0f); btnVisual.RotationAxis = new Vector3(0.0f, 1f, 0f); ScalarKeyFrameAnimation appearAnimation = compositor.CreateScalarKeyFrameAnimation(); appearAnimation.InsertKeyFrame(0.0f, 0); appearAnimation.InsertKeyFrame(0.599999f, 0); appearAnimation.InsertKeyFrame(0.6f, 0.5f); appearAnimation.InsertKeyFrame(1f, 1); appearAnimation.Duration = TimeSpan.FromMilliseconds(800); appearAnimation.IterationBehavior = AnimationIterationBehavior.Count; appearAnimation.IterationCount = 1; btnVisual.StartAnimation(nameof(btnVisual.RotationAngleInDegrees), flipAnimation); btnContentVisual.StartAnimation(nameof(btnContentVisual.Opacity), appearAnimation); _flipBatchAnimation.End(); if (_usePictures) { var file = await StorageFile.GetFileFromPathAsync(btn.Tag.ToString()); using (var stream = await file.OpenAsync(FileAccessMode.Read)) { var image = new Image(); var bmp = new BitmapImage(); await bmp.SetSourceAsync(stream); image.Source = bmp; btn.Content = image; } } else { btn.Content = btn.Tag.ToString(); } }
bool SwapBlank(int row, int col) { //Prevent tile slides once puzzle is solved if (DialogGrid.Visibility == Visibility.Visible || _gameOver) { return(false); } if (!((((row == _blankRow - 1) || (row == _blankRow + 1)) && (col == _blankCol)) || (((col == _blankCol - 1) || (col == _blankCol + 1)) && (row == _blankRow)))) { return(false); } GazeInput.DwellFeedbackProgressBrush = new SolidColorBrush(Colors.Transparent); _animationActive = true; GazeInput.SetInteraction(GameGrid, Interaction.Disabled); //Slide button visual Button btn = _buttons[row, col]; Button blankBtn = _buttons[_blankRow, _blankCol]; //Get Visuals for the selected button that is going to appear to slide and for the blank button var btnVisual = ElementCompositionPreview.GetElementVisual(btn); var compositor = btnVisual.Compositor; var blankBtnVisual = ElementCompositionPreview.GetElementVisual(blankBtn); var easing = compositor.CreateLinearEasingFunction(); if (_slideBatchAnimation != null) { _slideBatchAnimation.Completed -= SlideBatchAnimation_Completed; _slideBatchAnimation.Dispose(); } _slideBatchAnimation = compositor.CreateScopedBatch(CompositionBatchTypes.Animation); _slideBatchAnimation.Completed += SlideBatchAnimation_Completed; //Create an animation to first move the blank button with its updated contents to //instantly appear in the position position of the selected button //then slide that button back into its original position var slideAnimation = compositor.CreateVector3KeyFrameAnimation(); slideAnimation.InsertKeyFrame(0f, btnVisual.Offset); slideAnimation.InsertKeyFrame(1f, blankBtnVisual.Offset, easing); slideAnimation.Duration = TimeSpan.FromMilliseconds(500); //Apply the slide animation to the blank button blankBtnVisual.StartAnimation(nameof(btnVisual.Offset), slideAnimation); //Pulse after slide if sliding to correct position if (((_blankRow * _boardSize) + _blankCol + 1).ToString() == btn.Content.ToString()) { var springSpeed = 50; blankBtnVisual.CenterPoint = new System.Numerics.Vector3((float)blankBtn.ActualWidth / 2, (float)blankBtn.ActualHeight / 2, 0f); var scaleAnimation = compositor.CreateSpringVector3Animation(); scaleAnimation.InitialValue = new System.Numerics.Vector3(0.9f, 0.9f, 0f); scaleAnimation.FinalValue = new System.Numerics.Vector3(1.0f, 1.0f, 0f); scaleAnimation.DampingRatio = 0.4f; scaleAnimation.Period = TimeSpan.FromMilliseconds(springSpeed); scaleAnimation.DelayTime = TimeSpan.FromMilliseconds(500); blankBtnVisual.StartAnimation(nameof(blankBtnVisual.Scale), scaleAnimation); } _slideBatchAnimation.End(); //Swap content of the selected button with the blank button and clear the selected button _buttons[_blankRow, _blankCol].Content = _buttons[row, col].Content; _buttons[row, col].Content = ""; _blankRow = row; _blankCol = col; //Note there is some redunancy in the following settings that corrects the UI at board load as well as tile slide //Force selected button to the bottom and the blank button to the top Canvas.SetZIndex(btn, -_boardSize); Canvas.SetZIndex(blankBtn, 0); //Update the background colors of the two buttons to reflect their new condition btn.Background = _blankTileBrush; blankBtn.Background = _solidTileBrush; //Update the visibility to collapse the selected button that is now blank btn.Visibility = Visibility.Collapsed; blankBtn.Visibility = Visibility.Visible; //Disable eye control for the new empty button so that there are no inappropriate dwell indicators GazeInput.SetInteraction(blankBtn, Interaction.Inherited); GazeInput.SetInteraction(btn, Interaction.Disabled); return(true); }