protected override void OnActivated(EventArgs e) { var height = new DoubleAnimation(555, TimeSpan.FromMilliseconds(300)); var width = new DoubleAnimation(333, TimeSpan.FromMilliseconds(300)); //var top = new DoubleAnimation(Top - 600, TimeSpan.FromSeconds(1)); //var left = new DoubleAnimation(Left - 500, TimeSpan.FromSeconds(1)); //Storyboard.SetTargetProperty(anim, new PropertyPath("Height")); //Storyboard.SetTargetProperty(width, new PropertyPath("Width")); // var sb = new Storyboard(); // sb.Children.Add(width); // sb.Children.Add(anim); //sb.Children.Add(top); //sb.Children.Add(left); // Storyboard.SetTarget(sb, mw); // mw.BeginStoryboard(sb); Viewbox.BeginAnimation(HeightProperty, height); Viewbox.BeginAnimation(WidthProperty, width); }
public void Move(int x, int y, Direction dir) { Point StartPos = new Point(x, y); Point EndPos = new Point(x, y); var val = Cells[x, y].Value; var color = Cells[x, y].Fill.Fill; int xOffset = 0; int yOffset = 0; if (dir == Direction.Left) { xOffset = -1; if (x == 0) { return; } } else if (dir == Direction.Right) { xOffset = 1; if (x == CellsCount - 1) { return; } } else if (dir == Direction.Up) { yOffset = -1; if (y == 0) { return; } } else if (dir == Direction.Down) { yOffset = 1; if (y == CellsCount - 1) { return; } } if (Cells[x + xOffset, y + yOffset].Value == Cells[x, y].Value && !Cells[x + xOffset, y + yOffset].Joined && !Cells[x, y].Joined && Cells[x, y].Value != 0) { var value = Cells[x, y].Value; Cells[x, y].Value = 0; Cells[x + xOffset, y + yOffset].Value = value * 2; Score += value * 2; EndPos.X = x + xOffset; EndPos.Y = y + yOffset; Cells[x + xOffset, y + yOffset].Joined = true; if (value > 0) { Moved = true; } } else { bool end = false; for (; !end;) { if (Cells[x + xOffset, y + yOffset].Value == 0 && Cells[x, y].Value != 0) { var value = Cells[x, y].Value; Cells[x, y].Value = 0; Cells[x + xOffset, y + yOffset].Value = value; if (Cells[x, y].Joined) { Cells[x, y].Joined = false; Cells[x + xOffset, y + yOffset].Joined = true; } if (!Cells[x, y].Joined) { Cells[x + xOffset, y + yOffset].Joined = false; } EndPos.X = x + xOffset; EndPos.Y = y + yOffset; x += xOffset; y += yOffset; if (value > 0) { Moved = true; } ; } else { if (Cells[x + xOffset, y + yOffset].Value == Cells[x, y].Value && !Cells[x + xOffset, y + yOffset].Joined && !Cells[x, y].Joined && Cells[x, y].Value != 0) { var value = Cells[x, y].Value; Cells[x, y].Value = 0; Cells[x + xOffset, y + yOffset].Value = value * 2; Score += value * 2; EndPos.X = x + xOffset; EndPos.Y = y + yOffset; Cells[x + xOffset, y + yOffset].Joined = true; if (value > 0) { Moved = true; } } break; } if (dir == Direction.Left && x <= 0) { end = true; } else if (dir == Direction.Right && x >= CellsCount - 1) { end = true; } else if (dir == Direction.Up && y <= 0) { end = true; } else if (dir == Direction.Down && y >= CellsCount - 1) { end = true; } } } var width = CanvasSize.Width - 2 * BlockMargin; var height = CanvasSize.Height - 2 * BlockMargin; var cellWidth = width / CellsCount - (CellsCount - 1.0) / CellsCount * BlockMargin; var cellHeight = height / CellsCount - (CellsCount - 1.0) / CellsCount * BlockMargin; var x1 = StartPos.X * cellWidth + BlockMargin * (StartPos.X + 1); var y1 = StartPos.Y * cellWidth + BlockMargin * (StartPos.Y + 1); var x2 = EndPos.X * cellWidth + BlockMargin * (EndPos.X + 1); var y2 = EndPos.Y * cellWidth + BlockMargin * (EndPos.Y + 1); var itteration = 0; var itterations = 10; var cellFill = new Rectangle(); cellFill.Fill = GetColor(val); cellFill.Width = cellWidth; cellFill.Height = cellHeight; cellFill.SetValue(LeftProperty, (x2 - x1) * itteration / (double)itterations + x1); cellFill.SetValue(TopProperty, (y2 - y1) * itteration / (double)itterations + y1); cellFill.RadiusX = BorderRadius; cellFill.RadiusY = BorderRadius; var label = new Label(); label.Content = val.ToString(); var viewBox = new Viewbox(); label.Foreground = ((Label)Cells[(int)StartPos.X, (int)StartPos.Y].Content.Child).Foreground; viewBox.Child = label; viewBox.Width = cellWidth; viewBox.Height = cellHeight; viewBox.SetValue(LeftProperty, (x2 - x1) * itteration / (double)itterations + x1); viewBox.SetValue(TopProperty, (y2 - y1) * itteration / (double)itterations + y1); if (val != 0) { AnimatedCells++; Playground.Children.Add(cellFill); Playground.Children.Add(viewBox); var animationX = new DoubleAnimation(x1, x2, new Duration(TimeSpan.FromSeconds(0.2))); var animationY = new DoubleAnimation(y1, y2, new Duration(TimeSpan.FromSeconds(0.2))); animationX.Completed += (object sender, EventArgs e) => { Playground.Children.Remove(cellFill); Playground.Children.Remove(viewBox); UpdateGrid((int)EndPos.X, (int)EndPos.Y); if (AnimatedCells >= FilledCells) { LockKeyPress = false; UpdateGrid(); } var soltionExists = CheckCells(); if (!soltionExists) { LockKeyPress = true; Menu.Visibility = Visibility.Visible; Fill.Visibility = Visibility.Visible; MenuMessage.Content = "Game over"; var animation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(0.2))); Menu.BeginAnimation(OpacityProperty, animation); Fill.BeginAnimation(OpacityProperty, animation); } foreach (var cell in Cells) { if (cell.Value == 2048) { LockKeyPress = true; Menu.Visibility = Visibility.Visible; Fill.Visibility = Visibility.Visible; MenuMessage.Content = "You win"; var animation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(0.2))); Menu.BeginAnimation(OpacityProperty, animation); Fill.BeginAnimation(OpacityProperty, animation); } } }; cellFill.BeginAnimation(LeftProperty, animationX); cellFill.BeginAnimation(TopProperty, animationY); viewBox.BeginAnimation(LeftProperty, animationX); viewBox.BeginAnimation(TopProperty, animationY); Cells[(int)StartPos.X, (int)StartPos.Y].Fill.Fill = ForeColor; ((Label)Cells[(int)StartPos.X, (int)StartPos.Y].Content.Child).Content = ""; } }
public bool Anim(bool up) { if (_animating > 0 || _texts.Count <= 1) { return(false); } TranslateTransform ttTop = (TranslateTransform)_top.RenderTransform; TranslateTransform ttBottom = (TranslateTransform)_bottom.RenderTransform; _animating = 4; Viewbox disappearing = null; Viewbox appearing = null; if (up) { appearing = _bottom; disappearing = _top; DoubleAnimation doubleAnimationTop = new DoubleAnimation { From = 0, To = -RootGrid.ActualHeight / 2, Duration = _duration, FillBehavior = FillBehavior.Stop }; doubleAnimationTop.Completed += (s, _) => AnimCompleted(); ttTop.Y = RootGrid.ActualHeight; ttTop.BeginAnimation(TranslateTransform.YProperty, doubleAnimationTop); DoubleAnimation doubleAnimationBottom = new DoubleAnimation { From = RootGrid.ActualHeight, To = 0, Duration = _duration, FillBehavior = FillBehavior.Stop }; doubleAnimationBottom.Completed += (s, _) => AnimCompleted(); ttBottom.Y = 0; ttBottom.BeginAnimation(TranslateTransform.YProperty, doubleAnimationBottom); _index = NextIndex(); ((TextBlock)_bottom.Child).Text = _texts[_index]; _lastY = 1; } else { appearing = _bottom; disappearing = _top; DoubleAnimation doubleAnimationTop = new DoubleAnimation { From = 0, To = RootGrid.ActualHeight, Duration = _duration, FillBehavior = FillBehavior.Stop }; doubleAnimationTop.Completed += (s, _) => AnimCompleted(); ttTop.Y = RootGrid.ActualHeight; ttTop.BeginAnimation(TranslateTransform.YProperty, doubleAnimationTop); DoubleAnimation doubleAnimationBottom = new DoubleAnimation { From = -RootGrid.ActualHeight / 2, To = 0, Duration = _duration, FillBehavior = FillBehavior.Stop }; doubleAnimationBottom.Completed += (s, _) => AnimCompleted(); ttBottom.Y = 0; ttBottom.BeginAnimation(TranslateTransform.YProperty, doubleAnimationBottom); _index = PreviousIndex(); ((TextBlock)_bottom.Child).Text = _texts[_index]; _lastY = -1; } DoubleAnimation fadeOut = new DoubleAnimation { From = 1, To = 0, Duration = _duration.TimeSpan.Subtract(new TimeSpan((int)(_duration.TimeSpan.Ticks * 0.5))), FillBehavior = FillBehavior.Stop }; fadeOut.Completed += (s, _) => AnimCompleted(); disappearing.Opacity = 0; disappearing.BeginAnimation(OpacityProperty, fadeOut); DoubleAnimation fadeIn = new DoubleAnimation { From = 0, To = 1, Duration = _duration.TimeSpan.Subtract(new TimeSpan((int)(_duration.TimeSpan.Ticks * 0.5))), FillBehavior = FillBehavior.Stop }; fadeOut.Completed += (s, _) => AnimCompleted(); appearing.Opacity = 1; appearing.BeginAnimation(OpacityProperty, fadeIn); return(true); }