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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Tango.SharedUI.Controls
{
/// <summary>
/// Represents a control for holding a list of content controls and creates transition effects between them.
/// </summary>
public partial class MultiTransitionControl : UserControl
{
private int index;
private bool freezeAnimation;
private ContentControl element1 = null;
private ContentControl element2 = null;
private ContentControl fromElement;
private ContentControl toElement;
private Action navigationCompleteAction;
private bool _loaded;
#region Events
/// <summary>
/// Occurs when the transition has completed.
/// </summary>
public event Action<MultiTransitionControl> TransitionComplete;
#endregion
#region Constructors
/// <summary>
/// Initialize a new instance of MultiTransitionControl.
/// </summary>
public MultiTransitionControl()
{
Controls = new List<ContentControl>();
index = 0;
InitializeComponent();
this.Loaded += MultiTransitionControl_Loaded;
}
#endregion
#region Event Handlers
private void MultiTransitionControl_Loaded(object sender, RoutedEventArgs e)
{
if (!_loaded)
{
RefreshControls();
_loaded = true;
}
}
public void RefreshControls()
{
mainGrid.Children.Clear();
foreach (ContentControl control in Controls)
{
control.Visibility = System.Windows.Visibility.Hidden;
mainGrid.Children.Add(control);
}
if (Controls.Count > 0)
{
Controls[0].Visibility = System.Windows.Visibility.Visible;
SelectedControl = Controls[0];
fromElement = SelectedControl;
}
}
private void ani_BackCompleted(object sender, EventArgs e)
{
fromElement = toElement;
toElement = null;
freezeAnimation = true;
this.ApplyAnimationClock(MultiTransitionControl.MixProperty, null);
Mix = 0;
index++;
freezeAnimation = false;
IsAnimating = false;
OnTransitionCompleted();
}
private void ani_NextCompleted(object sender, EventArgs e)
{
fromElement = toElement;
toElement = null;
freezeAnimation = true;
this.ApplyAnimationClock(MultiTransitionControl.MixProperty, null);
Mix = 0;
index++;
freezeAnimation = false;
IsAnimating = false;
OnTransitionCompleted();
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the available content controls nested in the MultiTransitionControl.
/// </summary>
public List<ContentControl> Controls { get; set; }
/// <summary>
/// Gets or sets the transition style.
/// </summary>
public TransitionTypeEnum TransitionType
{
get { return (TransitionTypeEnum)GetValue(TransitionTypeProperty); }
set { SetValue(TransitionTypeProperty, value); }
}
public static readonly DependencyProperty TransitionTypeProperty =
DependencyProperty.Register("TransitionType", typeof(TransitionTypeEnum), typeof(MultiTransitionControl), new PropertyMetadata(TransitionTypeEnum.Fade) { PropertyChangedCallback = new PropertyChangedCallback(TransitionTypeChanged) });
private static void TransitionTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as MultiTransitionControl).ResetTransformation();
}
/// <summary>
/// This is where the actual transition occurs.
/// </summary>
private double Mix
{
get { return (double)GetValue(MixProperty); }
set
{
SetValue(MixProperty, value);
if (!freezeAnimation)
{
UpdateMix(value);
}
}
}
private static readonly DependencyProperty MixProperty =
DependencyProperty.Register("Mix", typeof(double), typeof(MultiTransitionControl), new PropertyMetadata(new PropertyChangedCallback(MixChanged)));
private static void MixChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MultiTransitionControl tr = d as MultiTransitionControl;
double value = (double)e.NewValue;
tr.UpdateMix(value);
}
/// <summary>
/// Gets or sets whether to always include a cross fade transition along with the selected transition style.
/// </summary>
public bool AlwaysFade
{
get { return (bool)GetValue(AlwaysFadeProperty); }
set { SetValue(AlwaysFadeProperty, value); }
}
public static readonly DependencyProperty AlwaysFadeProperty =
DependencyProperty.Register("AlwaysFade", typeof(bool), typeof(MultiTransitionControl), new PropertyMetadata(false));
/// <summary>
/// Gets or sets the interval in milliseconds for the transition to complete.
/// </summary>
public int AnimationTime
{
get { return (int)GetValue(AnimationTimeProperty); }
set { SetValue(AnimationTimeProperty, value); }
}
public static readonly DependencyProperty AnimationTimeProperty =
DependencyProperty.Register("AnimationTime", typeof(int), typeof(MultiTransitionControl), new PropertyMetadata(1000));
/// <summary>
/// Gets or sets the transition 'ease in' time.
/// </summary>
public double AnimationAcceleration
{
get { return (double)GetValue(AnimationAccelerationProperty); }
set { SetValue(AnimationAccelerationProperty, value); }
}
public static readonly DependencyProperty AnimationAccelerationProperty =
DependencyProperty.Register("AnimationAcceleration", typeof(double), typeof(MultiTransitionControl), new PropertyMetadata(0.5));
/// <summary>
/// Gets or sets the transition 'ease out' time.
/// </summary>
public double AnimationDeceleration
{
get { return (double)GetValue(AnimationDecelerationProperty); }
set { SetValue(AnimationDecelerationProperty, value); }
}
public static readonly DependencyProperty AnimationDecelerationProperty =
DependencyProperty.Register("AnimationDeceleration", typeof(double), typeof(MultiTransitionControl), new PropertyMetadata(0.5));
/// <summary>
/// Gets or sets whether a transition is taking place at the moment.
/// </summary>
public bool IsAnimating
{
get { return (bool)GetValue(IsAnimatingProperty); }
private set { SetValue(IsAnimatingProperty, value); }
}
public static readonly DependencyProperty IsAnimatingProperty =
DependencyProperty.Register("IsAnimating", typeof(bool), typeof(MultiTransitionControl), new PropertyMetadata(false));
/// <summary>
/// Gets or sets currently visible content control.
/// </summary>
public ContentControl SelectedControl
{
get { return (ContentControl)GetValue(SelectedControlProperty); }
set { SetValue(SelectedControlProperty, value); }
}
public static readonly DependencyProperty SelectedControlProperty =
DependencyProperty.Register("SelectedControl", typeof(ContentControl), typeof(MultiTransitionControl), new PropertyMetadata(null));
#endregion
#region Methods
/// <summary>
/// Updates the mix.
/// </summary>
/// <param name="value">The value.</param>
private void UpdateMix(double value)
{
if (freezeAnimation) return;
if (element1 == null || element2 == null)
{
return;
}
element1.Visibility = System.Windows.Visibility.Visible;
element2.Visibility = System.Windows.Visibility.Visible;
if (TransitionType == TransitionTypeEnum.Fade)
{
element1.RenderTransform = null;
element2.RenderTransform = null;
element1.Opacity = value;
element2.Opacity = 1 - value;
return;
}
else if (TransitionType == TransitionTypeEnum.Slide)
{
double v = value * 100;
v = (v * this.ActualWidth) / 100;
element2.RenderTransform = new TranslateTransform(v, 0);
element1.RenderTransform = new TranslateTransform(v - this.ActualWidth, 0);
}
else if (TransitionType == TransitionTypeEnum.Zoom)
{
element1.RenderTransformOrigin = new Point(0.5, 0.5);
element2.RenderTransformOrigin = new Point(0.5, 0.5);
element1.RenderTransform = new ScaleTransform(value, value);
element2.RenderTransform = new ScaleTransform(1 - value, 1 - value);
}
else if (TransitionType == TransitionTypeEnum.Spin)
{
double v = value * 100;
v = (v * 360) / 100;
var group1 = new TransformGroup();
group1.Children.Add(new RotateTransform(v));
group1.Children.Add(new ScaleTransform(value, value));
var group2 = new TransformGroup();
group2.Children.Add(new RotateTransform(360 - v));
group2.Children.Add(new ScaleTransform(1 - value, 1 - value));
element1.RenderTransform = group1;
element2.RenderTransform = group2;
}
else if (TransitionType == TransitionTypeEnum.Carousel)
{
double v = value * 100;
v = (v * this.ActualWidth) / 100;
var group1 = new TransformGroup();
group1.Children.Add(new ScaleTransform(value, value));
group1.Children.Add(new TranslateTransform(v - this.ActualWidth, 0));
var group2 = new TransformGroup();
group2.Children.Add(new TranslateTransform(v * 2, 0));
group2.Children.Add(new ScaleTransform(1 - value, 1 - value));
element1.RenderTransform = group1;
element2.RenderTransform = group2;
}
else if (TransitionType == TransitionTypeEnum.Skew)
{
double v = value * 100;
v = (v * 180) / 100;
element2.RenderTransform = new SkewTransform(v, 0);
element1.RenderTransform = new SkewTransform(v - 180, 0);
}
else if (TransitionType == TransitionTypeEnum.Push)
{
element1.RenderTransformOrigin = new Point(1, 0.5);
element2.RenderTransformOrigin = new Point(0, 0.5);
element1.RenderTransform = new ScaleTransform(value, 1);
element2.RenderTransform = new ScaleTransform(1 - value, 1);
}
if (AlwaysFade)
{
element1.Opacity = value;
element2.Opacity = 1 - value;
}
}
/// <summary>
/// Resets the transformation.
/// </summary>
private void ResetTransformation()
{
foreach (ContentControl control in Controls)
{
control.RenderTransformOrigin = new Point(0.5, 0.5);
}
}
/// <summary>
/// Execute a transition between the current selected control and the specified content control tag.
/// </summary>
/// <param name="tag">Tag of content control to navigate to.</param>
/// <param name="dataContext">DataContext to assign to the specified content control.</param>
/// <param name="navigationComplete">Navigation completed callback delegate.</param>
public void AutoNavigate(object tag, object dataContext = null, Action navigationComplete = null)
{
if (IsAnimating)
{
return;
}
ContentControl control = Controls.SingleOrDefault(x => x.Tag != null && x.Tag.ToString() == tag.ToString());
if (control == SelectedControl)
{
return;
}
if (control == null)
{
throw new Exception("Control with tag '" + tag.ToString() + "' not found! (Transition Control)");
}
if (dataContext != null)
{
control.DataContext = dataContext;
}
if (Controls.IndexOf(control) > Controls.IndexOf(fromElement))
{
navigationCompleteAction = navigationComplete;
toElement = control;
SelectedControl = control;
Next();
}
else if (Controls.IndexOf(control) < Controls.IndexOf(fromElement))
{
navigationCompleteAction = navigationComplete;
toElement = control;
SelectedControl = control;
Back();
}
}
private void Next()
{
if (IsAnimating)
{
return;
}
IsAnimating = true;
if (Controls.IndexOf(toElement) > Controls.IndexOf(fromElement))
{
element1 = toElement;
element2 = fromElement;
}
else
{
element1 = fromElement;
element2 = toElement;
}
if (element2 == null)
{
SelectedControl = Controls[0];
fromElement = SelectedControl;
element2 = fromElement;
}
freezeAnimation = true;
this.ApplyAnimationClock(MultiTransitionControl.MixProperty, null);
Mix = 0;
freezeAnimation = false;
DoubleAnimation ani = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(AnimationTime)), FillBehavior.HoldEnd);
ani.AccelerationRatio = AnimationAcceleration;
ani.DecelerationRatio = AnimationDeceleration;
ani.Completed += ani_NextCompleted;
this.BeginAnimation(MultiTransitionControl.MixProperty, ani);
}
private void Back()
{
if (IsAnimating) return;
IsAnimating = true;
if (Controls.IndexOf(toElement) > Controls.IndexOf(fromElement))
{
element1 = toElement;
element2 = fromElement;
}
else
{
element1 = fromElement;
element2 = toElement;
}
freezeAnimation = true;
this.ApplyAnimationClock(MultiTransitionControl.MixProperty, null);
Mix = 1;
freezeAnimation = false;
DoubleAnimation ani = new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(AnimationTime)), FillBehavior.HoldEnd);
ani.Completed += ani_BackCompleted;
ani.AccelerationRatio = AnimationAcceleration;
ani.DecelerationRatio = AnimationDeceleration;
this.BeginAnimation(MultiTransitionControl.MixProperty, ani);
}
#endregion
#region Virtuals
/// <summary>
/// Called when [transition completed].
/// </summary>
protected virtual void OnTransitionCompleted()
{
if (TransitionComplete != null) TransitionComplete(this);
if (SelectedControl != null)
{
Panel.SetZIndex(SelectedControl, Controls.Max(x => Panel.GetZIndex(x)) + 1);
}
if (navigationCompleteAction != null)
{
navigationCompleteAction();
navigationCompleteAction = null;
}
if (SelectedControl != null)
{
if (SelectedControl.Content is ITransitionView)
{
(SelectedControl.Content as ITransitionView).OnTransitionCompleted();
}
}
}
public interface ITransitionView
{
void OnTransitionCompleted();
}
#endregion
}
}
|