|
| 1 | +using OpenBoardAnim.Models; |
| 2 | +using System.Windows; |
| 3 | +using System.Windows.Controls; |
| 4 | +using System.Windows.Media; |
| 5 | +using System.Windows.Media.Animation; |
| 6 | +using System.Windows.Shapes; |
| 7 | + |
| 8 | +namespace OpenBoardAnim.Utils |
| 9 | +{ |
| 10 | + public class PathAnimationHelper |
| 11 | + { |
| 12 | + private Canvas _canvas; |
| 13 | + private List<Path> _paths; |
| 14 | + private GraphicModelBase _graphic; |
| 15 | + private UIElement _hand; |
| 16 | + List<double> _lengths = new List<double>(); |
| 17 | + public TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(); |
| 18 | + public PathAnimationHelper(Canvas canvas, |
| 19 | + List<Path> paths, |
| 20 | + GraphicModelBase graphic, |
| 21 | + UIElement hand) |
| 22 | + { |
| 23 | + _canvas = canvas; |
| 24 | + _paths = paths; |
| 25 | + _graphic = graphic; |
| 26 | + foreach (var path in _paths) |
| 27 | + { |
| 28 | + double item = GetTotalLength((PathGeometry)path.Data); |
| 29 | + _lengths.Add(item); |
| 30 | + // Set up the dash array and offset |
| 31 | + path.StrokeDashArray = new DoubleCollection(new double[] { item }); |
| 32 | + path.StrokeDashOffset = item; |
| 33 | + } |
| 34 | + |
| 35 | + _hand = hand; |
| 36 | + Canvas.SetLeft(_hand, _graphic.X); |
| 37 | + Canvas.SetTop(_hand, _graphic.Y); |
| 38 | + } |
| 39 | + public async void AnimatePathOnCanvas() |
| 40 | + { |
| 41 | + Storyboard storyboard = new(); |
| 42 | + TimeSpan beginTime = TimeSpan.Zero; |
| 43 | + for (int i = 0; i < _paths.Count; i++) |
| 44 | + { |
| 45 | + Path path = _paths[i]; |
| 46 | + // Add the path to the canvas |
| 47 | + _canvas.Children.Add(path); |
| 48 | + Canvas.SetLeft(path, _graphic.X); |
| 49 | + Canvas.SetTop(path, _graphic.Y); |
| 50 | + double ratio = _lengths[i] / _lengths.Sum(); |
| 51 | + // Create the animation |
| 52 | + TimeSpan timeSpan = TimeSpan.FromSeconds(_graphic.Duration * ratio); |
| 53 | + DoubleAnimation dashOffsetAnimation = new DoubleAnimation |
| 54 | + { |
| 55 | + From = _lengths[i], |
| 56 | + To = 0, |
| 57 | + Duration = timeSpan, |
| 58 | + BeginTime = beginTime |
| 59 | + }; |
| 60 | + |
| 61 | + // Create the animation |
| 62 | + MatrixAnimationUsingPath matrixAnimation = new MatrixAnimationUsingPath |
| 63 | + { |
| 64 | + PathGeometry = (PathGeometry)path.Data, |
| 65 | + Duration = timeSpan, |
| 66 | + BeginTime = beginTime |
| 67 | + }; |
| 68 | + beginTime += timeSpan; |
| 69 | + // Create a MatrixTransform for the ellipse |
| 70 | + MatrixTransform matrixTransform = new MatrixTransform(); |
| 71 | + _hand.RenderTransform = matrixTransform; |
| 72 | + DependencyProperty[] propertyChain = [UIElement.RenderTransformProperty, MatrixTransform.MatrixProperty]; |
| 73 | + string thePath = "(0).(1)"; |
| 74 | + PropertyPath myPropertyPath = new(thePath, propertyChain); |
| 75 | + // Start the animation |
| 76 | + Storyboard.SetTarget(matrixAnimation, _hand); |
| 77 | + Storyboard.SetTargetProperty(matrixAnimation, myPropertyPath); |
| 78 | + Storyboard.SetTarget(dashOffsetAnimation, path); |
| 79 | + Storyboard.SetTargetProperty(dashOffsetAnimation, new PropertyPath(Shape.StrokeDashOffsetProperty)); |
| 80 | + storyboard.Children.Add(matrixAnimation); |
| 81 | + storyboard.Children.Add(dashOffsetAnimation); |
| 82 | + } |
| 83 | + storyboard.Completed += DashOffsetAnimation_Completed; |
| 84 | + storyboard.Begin(); |
| 85 | + |
| 86 | + await Task.CompletedTask; |
| 87 | + } |
| 88 | + private void DashOffsetAnimation_Completed(object sender, EventArgs e) |
| 89 | + { |
| 90 | + Canvas.SetLeft(_hand, 2000); |
| 91 | + Canvas.SetTop(_hand, 1100); |
| 92 | + tcs?.TrySetResult(true); |
| 93 | + } |
| 94 | + |
| 95 | + // Helper method to calculate total length of the geometry |
| 96 | + private static double GetTotalLength(PathGeometry geometry) |
| 97 | + { |
| 98 | + double length = 0; |
| 99 | + foreach (PathFigure figure in geometry.Figures) |
| 100 | + { |
| 101 | + Point start = figure.StartPoint; |
| 102 | + foreach (PathSegment segment in figure.Segments) |
| 103 | + { |
| 104 | + if (segment is LineSegment line) |
| 105 | + { |
| 106 | + length += (line.Point - start).Length; |
| 107 | + start = line.Point; |
| 108 | + } |
| 109 | + else if (segment is PolyLineSegment polyLine) |
| 110 | + { |
| 111 | + foreach (Point point in polyLine.Points) |
| 112 | + { |
| 113 | + length += (point - start).Length; |
| 114 | + start = point; |
| 115 | + } |
| 116 | + } |
| 117 | + // Add cases for other segment types if needed |
| 118 | + } |
| 119 | + } |
| 120 | + return length; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments