-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainWindow.xaml.cs
108 lines (91 loc) · 3.09 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace CoroutinesForWpf.Example
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const double StepSize = 40;
private const int StepCount = 6; // must be even
private const int MiddleStep = StepCount / 2;
private readonly Ellipse _ball;
private readonly Step[] _steps;
private readonly BallAnimation _ballAnimation;
private Point _center;
private Point _start;
public MainWindow()
{
InitializeComponent();
Loaded += OnLoaded;
SizeChanged += OnSizeChanged;
_ball = new Ellipse { Width = 20, Height = 20, Stroke = Brushes.Black, StrokeThickness = 2 };
_steps = Enumerable.Range(0, StepCount).Select(_ => new Step(StepSize, StepSize * StepCount)).ToArray();
_ballAnimation = new BallAnimation(_ball, StepSize);
}
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
Initialize();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
Scene.Children.Add(_ball);
foreach (var step in _steps)
{
Scene.Children.Add(step.Horizontal);
Scene.Children.Add(step.Vertical);
}
Initialize();
Coroutine.Start(StairsAnimation());
Coroutine.Start(_ballAnimation);
}
private void Initialize()
{
_center = new Point(Math.Round(Scene.ActualWidth / 2), Math.Round(Scene.ActualHeight / 2));
_start = _center + new Vector(-MiddleStep * StepSize, -MiddleStep * StepSize);
foreach (UIElement sceneChild in Scene.Children)
{
Canvas.SetLeft(sceneChild, 0);
Canvas.SetTop(sceneChild, 0);
}
_ballAnimation.Center = _center;
}
private IEnumerator StairsAnimation()
{
yield return BuildStairs();
yield return RunStairs();
}
private IEnumerator BuildStairs()
{
while (_steps[0].Position.X < _start.X + StepCount * StepSize - 1)
{
for (int i = 0; i < _steps.Length; i++)
{
if (i == 0 || _steps[i - 1].Position.X >= _start.X + StepSize)
{
_steps[i].Update(_start, true);
}
}
_ballAnimation.Continue = _steps[MiddleStep].Position.X >= _start.X + StepSize;
yield return null;
}
}
private IEnumerator RunStairs()
{
while (true)
{
foreach (var step in _steps)
{
step.Update(_start, true);
}
yield return null;
}
}
}
}