// Define and setup the Green Square Visual that will be animated public void SetupVisual() { // Intialize the Compositor _compositor = new Compositor(); _root = ElementCompositionPreview.GetElementVisual(Container); _compositor = _root.Compositor; _linear = _compositor.CreateLinearEasingFunction(); // Create Green Square var colorVisual = _compositor.CreateSpriteVisual(); colorVisual.Brush = _compositor.CreateColorBrush(Colors.Green); colorVisual.Size = new Vector2(150.0f, 150.0f); colorVisual.Offset = new Vector3(250.0f, 50.0f, 0.0f); colorVisual.CenterPoint = new Vector3(75.0f, 75.0f, 0.0f); _target = colorVisual; // Create Blue Square var colorVisual2 = _compositor.CreateSpriteVisual(); colorVisual2.Brush = _compositor.CreateColorBrush(Colors.Aqua); colorVisual2.Size = new Vector2(200.0f, 150.0f); colorVisual2.Offset = new Vector3(25.0f, 50.0f, 0.0f); colorVisual2.IsVisible = false; _target2 = colorVisual2; // Add the Blue and Green square visuals to the tree _mainContainer = _compositor.CreateContainerVisual(); ElementCompositionPreview.SetElementChildVisual(Container, _mainContainer); _mainContainer.Children.InsertAtTop(_target); _mainContainer.Children.InsertAtTop(_target2); // Create Scoped batch for animations _batch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation); // Add Animation1 to the batch Animation1(_target); // Suspend the batch to exclude an animation _batch.Suspend(); // Exluding Animation2 from batch Animation2(_target); // Resuming the batch to collect additional animations _batch.Resume(); // Add Animation3 to the batch Animation3(_target); // Batch is ended an no objects can be added _batch.End(); // Method triggered when batch completion event fires _batch.Completed += OnBatchCompleted; }
private bool AddAdjacent(int start, int end, int delta) { bool addSuccess = false; int cur = start; for (int i = 0; i < _boardSize - 1; i++) { if ((Cells[cur + delta].IntVal != 0) && (Cells[cur].IntVal > 0) && (Cells[cur].IntVal == Cells[cur + delta].IntVal)) { ///Animate cell movement var slideToCellVisual = ElementCompositionPreview.GetElementVisual(Buttons[cur]); var compositor = slideToCellVisual.Compositor; var slideFromCellVisual = ElementCompositionPreview.GetElementVisual(Buttons[cur + delta]); //very brittle way to get visuals for the text fields inside the cell template var lvp = VisualTreeHelper.GetChild(Buttons[cur], 0); var border = VisualTreeHelper.GetChild(lvp, 0); var grid = VisualTreeHelper.GetChild(border, 0); var cellText = VisualTreeHelper.GetChild(grid, 0); var answerText = VisualTreeHelper.GetChild(grid, 1); var answerTextVisual = ElementCompositionPreview.GetElementVisual(answerText as FrameworkElement); var cellTextVisual = ElementCompositionPreview.GetElementVisual(cellText as FrameworkElement); var easing = compositor.CreateLinearEasingFunction(); ///Scale the ToCell to breifly be twice the size and then back down to regular size /// slideToCellVisual.CenterPoint = new System.Numerics.Vector3(slideToCellVisual.Size.X / 2, slideToCellVisual.Size.Y / 2, 0f); var scaleUpAnimation = compositor.CreateVector3KeyFrameAnimation(); scaleUpAnimation.InsertKeyFrame(0f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing); scaleUpAnimation.InsertKeyFrame(0.5f, new System.Numerics.Vector3(1.5f, 1.5f, 0f), easing); scaleUpAnimation.InsertKeyFrame(1f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing); scaleUpAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed); ///Slide the ToCell back from the FromCell position ///Be sure to return it to its original position afterwards var initialFromOffset = new Vector3(slideFromCellVisual.Offset.X, slideFromCellVisual.Offset.Y, slideFromCellVisual.Offset.Z); var initialToOffset = new Vector3(slideToCellVisual.Offset.X, slideToCellVisual.Offset.Y, slideToCellVisual.Offset.Z); var slideAnimation = compositor.CreateVector3KeyFrameAnimation(); slideAnimation.InsertKeyFrame(0, initialFromOffset); slideAnimation.InsertKeyFrame(1, initialToOffset, easing); slideAnimation.Duration = TimeSpan.FromMilliseconds(_SlideSpeed / 2); //show cell text var showCellTextAnimation = compositor.CreateScalarKeyFrameAnimation(); showCellTextAnimation.InsertKeyFrame(0, 0f); showCellTextAnimation.InsertKeyFrame(1, 1f); showCellTextAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed * 2); //Show answer var showAnswerAnimation = compositor.CreateScalarKeyFrameAnimation(); showAnswerAnimation.InsertKeyFrame(0, 1f); showAnswerAnimation.InsertKeyFrame(1, 0.0f); showAnswerAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed * 3); var scaleAnswerAnimation = compositor.CreateVector3KeyFrameAnimation(); scaleAnswerAnimation.InsertKeyFrame(0f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing); scaleAnswerAnimation.InsertKeyFrame(0.5f, new System.Numerics.Vector3(1.5f, 1.5f, 0f), easing); scaleAnswerAnimation.InsertKeyFrame(1f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing); scaleAnswerAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed); slideToCellVisual.StartAnimation(nameof(slideToCellVisual.Offset), slideAnimation); _slideBatchAnimation.Suspend(); _addAdjacentBatchAnimation.Suspend(); slideToCellVisual.StartAnimation(nameof(slideToCellVisual.Scale), scaleUpAnimation); answerTextVisual.StartAnimation(nameof(answerTextVisual.Opacity), showAnswerAnimation); answerTextVisual.StartAnimation(nameof(answerTextVisual.Scale), scaleAnswerAnimation); cellTextVisual.StartAnimation(nameof(cellTextVisual.Opacity), showCellTextAnimation); _addAdjacentBatchAnimation.Resume(); _slideBatchAnimation.Resume(); Canvas.SetZIndex(Buttons[cur], GetHighestButtonIndex() + 1); Cells[cur].AnswerString = Cells[cur].IntVal.ToString() + " + " + Cells[cur + delta].IntVal.ToString(); Cells[cur].IntVal += Cells[cur + delta].IntVal; Cells[cur + delta].IntVal = 0; Score += Cells[cur].IntVal; addSuccess = true; } cur += delta; } return(addSuccess); }
// Define and setup the Green Square Visual that will be animated public void SetupVisual() { // Intialize the Compositor _compositor = new Compositor(); _root = (ContainerVisual)ElementCompositionPreview.GetElementVisual(Container); _compositor = _root.Compositor; _linear = _compositor.CreateLinearEasingFunction(); // Create Green Square var colorVisual = _compositor.CreateSpriteVisual(); colorVisual.Brush = _compositor.CreateColorBrush(Colors.Green); colorVisual.Size = new Vector2(150.0f, 150.0f); colorVisual.Offset = new Vector3(250.0f, 50.0f, 0.0f); colorVisual.CenterPoint = new Vector3(75.0f, 75.0f, 0.0f); _target = colorVisual; // Create Blue Square var colorVisual2 = _compositor.CreateSpriteVisual(); colorVisual2.Brush = _compositor.CreateColorBrush(Colors.Aqua); colorVisual2.Size = new Vector2(200.0f, 150.0f); colorVisual2.Offset = new Vector3(25.0f, 50.0f, 0.0f); colorVisual2.IsVisible = false; _target2 = colorVisual2; // Add the Blue and Green square visuals to the tree _mainContainer = _compositor.CreateContainerVisual(); ElementCompositionPreview.SetElementChildVisual(Container, _mainContainer); _mainContainer.Children.InsertAtTop(_target); _mainContainer.Children.InsertAtTop(_target2); // Create Scoped batch for animations _batch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation); // Add Animation1 to the batch Animation1(_target); // Suspend the batch to exclude an animation _batch.Suspend(); // Exluding Animation2 from batch Animation2(_target); // Resuming the batch to collect additional animations _batch.Resume(); // Add Animation3 to the batch Animation3(_target); // Batch is ended an no objects can be added _batch.End(); // Method triggered when batch completion event fires _batch.Completed += OnBatchCompleted; }
private int AddAdjacent(int start, int end, int delta) { int doubledVal = 0; int totalBonus = -1; //If it returns -1 then no additions happened regardless of whether they were bonus worthy int cur = start; for (int i = 0; i < _boardSize - 1; i++) { if ((Cells[cur + delta].IntVal != 0) && (Cells[cur].IntVal > 0) && (Cells[cur].IntVal == Cells[cur + delta].IntVal)) { ///Animate cell movement var slideToCellVisual = ElementCompositionPreview.GetElementVisual(Buttons[cur]); var compositor = slideToCellVisual.Compositor; var slideFromCellVisual = ElementCompositionPreview.GetElementVisual(Buttons[cur + delta]); //very brittle way to get visuals for the text fields inside the cell template var lvp = VisualTreeHelper.GetChild(Buttons[cur], 0); var border = VisualTreeHelper.GetChild(lvp, 0); var grid = VisualTreeHelper.GetChild(border, 0); var cellText = VisualTreeHelper.GetChild(grid, 0); var answerText = VisualTreeHelper.GetChild(grid, 1); var answerTextVisual = ElementCompositionPreview.GetElementVisual(answerText as FrameworkElement); var cellTextVisual = ElementCompositionPreview.GetElementVisual(cellText as FrameworkElement); CubicBezierEasingFunction easing = compositor.CreateCubicBezierEasingFunction(new Vector2(.86f, 0.0f), new Vector2(.07f, 1.00f)); ///Scale the ToCell to breifly be twice the size and then back down to regular size /// slideToCellVisual.CenterPoint = new System.Numerics.Vector3(slideToCellVisual.Size.X / 2, slideToCellVisual.Size.Y / 2, 0f); var scaleUpAnimation = compositor.CreateVector3KeyFrameAnimation(); scaleUpAnimation.InsertKeyFrame(0f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing); scaleUpAnimation.InsertKeyFrame(0.5f, new System.Numerics.Vector3(1.2f, 1.2f, 0f), easing); scaleUpAnimation.InsertKeyFrame(1f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing); scaleUpAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed); ///Slide the ToCell back from the FromCell position ///Be sure to return it to its original position afterwards var initialFromOffset = new Vector3(slideFromCellVisual.Offset.X, slideFromCellVisual.Offset.Y, slideFromCellVisual.Offset.Z); var initialToOffset = new Vector3(slideToCellVisual.Offset.X, slideToCellVisual.Offset.Y, slideToCellVisual.Offset.Z); var slideAnimation = compositor.CreateVector3KeyFrameAnimation(); slideAnimation.InsertKeyFrame(0, initialFromOffset); slideAnimation.InsertKeyFrame(1, initialToOffset, easing); slideAnimation.Duration = TimeSpan.FromMilliseconds(_SlideSpeed / 2); //show cell text var showCellTextAnimation = compositor.CreateScalarKeyFrameAnimation(); showCellTextAnimation.InsertKeyFrame(0, 0f); showCellTextAnimation.InsertKeyFrame(1, 1f); showCellTextAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed * 2); //Show answer var showAnswerAnimation = compositor.CreateScalarKeyFrameAnimation(); showAnswerAnimation.InsertKeyFrame(0, 1f); showAnswerAnimation.InsertKeyFrame(1, 0.0f); showAnswerAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed * 3); var scaleAnswerAnimation = compositor.CreateVector3KeyFrameAnimation(); scaleAnswerAnimation.InsertKeyFrame(0f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing); scaleAnswerAnimation.InsertKeyFrame(0.5f, new System.Numerics.Vector3(1.5f, 1.5f, 0f), easing); scaleAnswerAnimation.InsertKeyFrame(1f, new System.Numerics.Vector3(1.0f, 1.0f, 0f), easing); scaleAnswerAnimation.Duration = TimeSpan.FromMilliseconds(_AddSpeed); slideToCellVisual.StartAnimation(nameof(slideToCellVisual.Offset), slideAnimation); _slideBatchAnimation.Suspend(); _addAdjacentBatchAnimation.Suspend(); slideToCellVisual.StartAnimation(nameof(slideToCellVisual.Scale), scaleUpAnimation); if (_boardSpeed == BoardSpeed.Slow) { answerTextVisual.StartAnimation(nameof(answerTextVisual.Opacity), showAnswerAnimation); answerTextVisual.StartAnimation(nameof(answerTextVisual.Scale), scaleAnswerAnimation); cellTextVisual.StartAnimation(nameof(cellTextVisual.Opacity), showCellTextAnimation); } _addAdjacentBatchAnimation.Resume(); _slideBatchAnimation.Resume(); Canvas.SetZIndex(Buttons[cur], GetHighestButtonIndex() + 1); if (_boardSpeed == BoardSpeed.Slow) { Cells[cur].AnswerString = Cells[cur].IntVal.ToString() + " + " + Cells[cur + delta].IntVal.ToString(); } Cells[cur].IntVal += Cells[cur + delta].IntVal; doubledVal = Cells[cur].IntVal; if (totalBonus > -1) { totalBonus += MilestoneBonus(doubledVal); } else { totalBonus = 0; totalBonus += MilestoneBonus(doubledVal); } if (doubledVal > HighTile) { HighTile = doubledVal; } Cells[cur + delta].IntVal = 0; Score += Cells[cur].IntVal; } cur += delta; } return(totalBonus); }