[12762] | 1 | using System;
|
---|
| 2 | using System.Linq;
|
---|
| 3 | using System.Text;
|
---|
| 4 | using System.Windows;
|
---|
| 5 | using System.Windows.Media;
|
---|
| 6 | using System.Windows.Media.Animation;
|
---|
| 7 |
|
---|
| 8 | namespace SharpVectors.Runtime
|
---|
| 9 | {
|
---|
| 10 | /// <summary>
|
---|
| 11 | /// This provides a wrapper for the Scoreboard, which is used for opacity animation.
|
---|
| 12 | /// </summary>
|
---|
| 13 | public sealed class SvgAnimator : FrameworkElement
|
---|
| 14 | {
|
---|
| 15 | #region Private Fields
|
---|
| 16 |
|
---|
| 17 | private bool _isStarted;
|
---|
| 18 | private string _targetName;
|
---|
| 19 |
|
---|
| 20 | private Storyboard _storyboard;
|
---|
| 21 | private DoubleAnimation _opacityAnimation;
|
---|
| 22 |
|
---|
| 23 | #endregion
|
---|
| 24 |
|
---|
| 25 | #region Constructors and Destructor
|
---|
| 26 |
|
---|
| 27 | public SvgAnimator()
|
---|
| 28 | {
|
---|
| 29 | NameScope.SetNameScope(this, new NameScope());
|
---|
| 30 |
|
---|
| 31 | _opacityAnimation = new DoubleAnimation();
|
---|
| 32 | _opacityAnimation.From = 0.9;
|
---|
| 33 | _opacityAnimation.To = 0.0;
|
---|
| 34 | _opacityAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
|
---|
| 35 | _opacityAnimation.AutoReverse = false;
|
---|
| 36 | _opacityAnimation.RepeatBehavior = RepeatBehavior.Forever;
|
---|
| 37 |
|
---|
| 38 | _storyboard = new Storyboard();
|
---|
| 39 |
|
---|
| 40 | _storyboard.Children.Add(_opacityAnimation);
|
---|
| 41 |
|
---|
| 42 | Storyboard.SetTargetProperty(_opacityAnimation, new PropertyPath(
|
---|
| 43 | Brush.OpacityProperty));
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | #endregion
|
---|
| 47 |
|
---|
| 48 | #region Public Properties
|
---|
| 49 |
|
---|
| 50 | public bool IsAnimating
|
---|
| 51 | {
|
---|
| 52 | get
|
---|
| 53 | {
|
---|
| 54 | return _isStarted;
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public string TargetName
|
---|
| 59 | {
|
---|
| 60 | get
|
---|
| 61 | {
|
---|
| 62 | return _targetName;
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | #endregion
|
---|
| 67 |
|
---|
| 68 | #region Public Methods
|
---|
| 69 |
|
---|
| 70 | public void Start(string targetName, object scopedElement)
|
---|
| 71 | {
|
---|
| 72 | if (String.IsNullOrEmpty(targetName))
|
---|
| 73 | {
|
---|
| 74 | return;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | if (_isStarted)
|
---|
| 78 | {
|
---|
| 79 | this.Stop();
|
---|
| 80 | }
|
---|
| 81 | this.RegisterName(targetName, scopedElement);
|
---|
| 82 |
|
---|
| 83 | Storyboard.SetTargetName(_opacityAnimation, targetName);
|
---|
| 84 |
|
---|
| 85 | _storyboard.Begin(this, true);
|
---|
| 86 |
|
---|
| 87 | _targetName = targetName;
|
---|
| 88 | _isStarted = true;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public void Stop()
|
---|
| 92 | {
|
---|
| 93 | _storyboard.Stop(this);
|
---|
| 94 |
|
---|
| 95 | if (!String.IsNullOrEmpty(_targetName))
|
---|
| 96 | {
|
---|
| 97 | this.UnregisterName(_targetName);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | _isStarted = false;
|
---|
| 101 | _targetName = null;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | #endregion
|
---|
| 105 | }
|
---|
| 106 | }
|
---|