-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadingControl.xaml.cs
More file actions
325 lines (288 loc) · 9.29 KB
/
LoadingControl.xaml.cs
File metadata and controls
325 lines (288 loc) · 9.29 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media.Animation;
using System.ComponentModel;
using System;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections.Generic;
using System.Linq;
namespace YourNamespace
{
/// <summary>
/// Interaction logic for Loading.xaml
/// </summary>
public partial class LoadingControl : UserControl, INotifyPropertyChanged
{
private const string COLOR_ANIM_TARGET_PROP = "Fill.(SolidColorBrush.Color)";
private const string DOUBLE_ANIM_TARGET_PROP = "RenderTransform.ScaleY";
private Storyboard storyboard;
private BeginTimeConverter beginTimeConverter;
private HalfDurationConverter halfDurationConverter;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public LoadingControl()
{
storyboard = new Storyboard();
halfDurationConverter = new HalfDurationConverter();
InitializeComponent();
this.Loaded += (s, e) => ResetRectangles();
}
public bool HideCenter
{
get { return (bool)GetValue(HideCenterProperty); }
set
{
SetValue(HideCenterProperty, value);
OnPropertyChanged("HideCenter");
ResetRectangles();
}
}
public int SideLength
{
get { return (int)GetValue(SideLengthProperty); }
set
{
SetValue(SideLengthProperty, value);
OnPropertyChanged("SideLength");
ResetRectangles();
}
}
public string Message
{
get { return (string)GetValue(MessageProperty); }
set
{
SetValue(MessageProperty, value);
OnPropertyChanged("Message");
}
}
public Color Color1
{
get { return (Color)GetValue(Color1Property); }
set
{
SetValue(Color1Property, value);
OnPropertyChanged("Color1");
}
}
public Color Color2
{
get { return (Color)GetValue(Color2Property); }
set
{
SetValue(Color2Property, value);
OnPropertyChanged("Color2");
}
}
public Duration SquareAnimationDuration
{
get { return (Duration)GetValue(SquareAnimationDurationProperty); }
set
{
SetValue(SquareAnimationDurationProperty, value);
OnPropertyChanged("SquareAnimationDuration");
}
}
public IEasingFunction ColorEasing
{
get { return (IEasingFunction)GetValue(ColorEasingProperty); }
set
{
SetValue(ColorEasingProperty, value);
OnPropertyChanged("ColorEasing");
}
}
public IEasingFunction SizeEasing
{
get { return (IEasingFunction)GetValue(SizeEasingProperty); }
set
{
SetValue(SizeEasingProperty, value);
OnPropertyChanged("SizeEasing");
}
}
private void ResetRectangles()
{
rectangles.Children.Clear();
storyboard.Children.Clear();
int rectsCount = SideLength * SideLength;
if (HideCenter && SideLength > 2)
rectsCount -= (SideLength - 2) * (SideLength - 2);
BeginTimeConverter beginTimeConverter = new BeginTimeConverter(ref rectsCount);
int[,] indexes = Spiral(SideLength);
for (int x = 0; x < SideLength; x++)
{
for (int y = 0; y < SideLength; y++)
{
if (HideCenter && !(y == 0 || y == SideLength - 1 || x == 0 || x == SideLength - 1))
{
rectangles.Children.Add(new UIElement());
continue;
}
AddNewRectangle(indexes[x, y], ref beginTimeConverter);
}
}
storyboard.Begin();
}
private void AddNewRectangle(int i, ref BeginTimeConverter btc)
{
var rectName = "rect" + i;
var newRect = new Rectangle { Name = rectName };
rectangles.Children.Add(newRect);
storyboard.Children.Add(GenerateColorAnimation(ref i, ref newRect, ref btc));
storyboard.Children.Add(GenerateDoubleAnimation(ref i, ref newRect, ref btc));
}
private ColorAnimation GenerateColorAnimation(ref int i, ref Rectangle rect, ref BeginTimeConverter beginTimeConverter)
{
var colorAnim = new ColorAnimation
{
RepeatBehavior = RepeatBehavior.Forever,
AutoReverse = true
};
BindingOperations.SetBinding(colorAnim, ColorAnimation.BeginTimeProperty,
new Binding
{
Path = new PropertyPath("SquareAnimationDuration"),
Source = this,
Converter = beginTimeConverter,
ConverterParameter = i
});
BindingOperations.SetBinding(colorAnim, ColorAnimation.DurationProperty,
new Binding
{
Path = new PropertyPath("SquareAnimationDuration"),
Source = this,
Converter = halfDurationConverter,
ConverterParameter = colorAnim.AutoReverse
});
BindingOperations.SetBinding(colorAnim, ColorAnimation.FromProperty,
new Binding { Path = new PropertyPath("Color1"), Source = this });
BindingOperations.SetBinding(colorAnim, ColorAnimation.ToProperty,
new Binding { Path = new PropertyPath("Color2"), Source = this });
BindingOperations.SetBinding(colorAnim, ColorAnimation.EasingFunctionProperty,
new Binding { Path = new PropertyPath("ColorEasing"), Source = this });
Storyboard.SetTarget(colorAnim, rect);
Storyboard.SetTargetProperty(colorAnim, new PropertyPath(COLOR_ANIM_TARGET_PROP));
return colorAnim;
}
private DoubleAnimation GenerateDoubleAnimation(ref int i, ref Rectangle rect, ref BeginTimeConverter beginTimeConverter)
{
var doubleAnim = new DoubleAnimation
{
RepeatBehavior = RepeatBehavior.Forever,
AutoReverse = true,
From = 1,
To = 0.15
};
BindingOperations.SetBinding(doubleAnim, DoubleAnimation.BeginTimeProperty,
new Binding
{
Path = new PropertyPath("SquareAnimationDuration"),
Source = this,
Converter = beginTimeConverter,
ConverterParameter = i
});
BindingOperations.SetBinding(doubleAnim, DoubleAnimation.DurationProperty,
new Binding
{
Path = new PropertyPath("SquareAnimationDuration"),
Source = this,
Converter = halfDurationConverter,
ConverterParameter = doubleAnim.AutoReverse
});
BindingOperations.SetBinding(doubleAnim, DoubleAnimation.EasingFunctionProperty,
new Binding { Path = new PropertyPath("SizeEasing"), Source = this });
Storyboard.SetTarget(doubleAnim, rect);
Storyboard.SetTargetProperty(doubleAnim, new PropertyPath(DOUBLE_ANIM_TARGET_PROP));
return doubleAnim;
}
//https://rosettacode.org/wiki/Spiral_matrix#C.23
public int[,] Spiral(int n)
{
int[,] result = new int[n, n];
int pos = 0;
int count = n;
int value = -n;
int sum = -1;
do
{
value = -1 * value / n;
for (int i = 0; i < count; i++)
{
sum += value;
result[sum / n, sum % n] = pos++;
}
value *= n;
count--;
for (int i = 0; i < count; i++)
{
sum += value;
result[sum / n, sum % n] = pos++;
}
} while (count > 0);
return result;
}
public static readonly DependencyProperty HideCenterProperty =
DependencyProperty.Register("HideCenter", typeof(bool),
typeof(LoadingControl), new PropertyMetadata(true));
public static readonly DependencyProperty SideLengthProperty =
DependencyProperty.Register("SideLength", typeof(int),
typeof(LoadingControl), new PropertyMetadata(3));
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message", typeof(string),
typeof(LoadingControl), new PropertyMetadata(null));
public static readonly DependencyProperty Color1Property =
DependencyProperty.Register("Color1", typeof(Color),
typeof(LoadingControl), new PropertyMetadata(Colors.Green));
public static readonly DependencyProperty Color2Property =
DependencyProperty.Register("Color2", typeof(Color),
typeof(LoadingControl), new PropertyMetadata(Colors.Yellow));
public static readonly DependencyProperty SquareAnimationDurationProperty =
DependencyProperty.Register("SquareAnimationDuration", typeof(Duration),
typeof(LoadingControl), new PropertyMetadata(new Duration(TimeSpan.FromSeconds(0.5))));
public static readonly DependencyProperty ColorEasingProperty =
DependencyProperty.Register("ColorEasing", typeof(IEasingFunction),
typeof(LoadingControl), new PropertyMetadata(null));
public static readonly DependencyProperty SizeEasingProperty =
DependencyProperty.Register("SizeEasing", typeof(IEasingFunction),
typeof(LoadingControl), new PropertyMetadata(null));
private class BeginTimeConverter : IValueConverter
{
private readonly int rectsCount;
public BeginTimeConverter(ref int rectsCount)
{
this.rectsCount = rectsCount;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return TimeSpan.FromSeconds((((Duration)value).TimeSpan.TotalSeconds / rectsCount) * (int)parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
private class HalfDurationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool isAutoReversed = parameter == null ? false : (bool)parameter;
return isAutoReversed ? new Duration(TimeSpan.FromSeconds(((Duration)value).TimeSpan.TotalSeconds / 2.0)) : value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
}