MotionPathAnimation.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using DocumentFormat.OpenXml.Presentation;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using ClearSlideLibrary.PPTBuilder;
  9. using ClearSlideLibrary.Dom;
  10. namespace ClearSlideLibrary.Animations
  11. {
  12. /*
  13. * Generates animation for a motion path. I am not sure if it works for all kinds of paths because there are lines, curves, lassos etc.
  14. * It calculates the distance for each part of the path (each curve, line etc - not between each point).
  15. * We calculate the whole distance and give each part timing = (PartDistance / WholeDistance) * wholeTime.
  16. * The speed in the path is constant. In PowerPoint there is acceleration and deceleration but we don't care about that yet ;)
  17. */
  18. class MotionPathAnimation : SimpleAnimation
  19. {
  20. private List<PathPart> motionPath;
  21. private SlideSize SlideSizes;
  22. public int MultiplierX()
  23. {
  24. if (SlideSizes == null || SlideSizes.Cx == 0)
  25. {
  26. return 1;
  27. }
  28. else if (SlideSizes.Cx < Globals.LEAST_COMMON_MULTIPLE_100_254)
  29. {
  30. return Convert.ToInt32((double)(Globals.LEAST_COMMON_MULTIPLE_100_254) / SlideSizes.Cx);
  31. }
  32. else
  33. {
  34. return Convert.ToInt32(SlideSizes.Cx / (double)(Globals.LEAST_COMMON_MULTIPLE_100_254));
  35. }
  36. }
  37. public int MultiplierY()
  38. {
  39. if (SlideSizes == null || SlideSizes.Cy == 0)
  40. {
  41. return 1;
  42. }
  43. else if (SlideSizes.Cy < Globals.LEAST_COMMON_MULTIPLE_100_254)
  44. {
  45. return Convert.ToInt32((double)(Globals.LEAST_COMMON_MULTIPLE_100_254) / SlideSizes.Cy);
  46. }
  47. else
  48. {
  49. return Convert.ToInt32(SlideSizes.Cy / (double)(Globals.LEAST_COMMON_MULTIPLE_100_254));
  50. }
  51. }
  52. public MotionPathAnimation(CommonTimeNode commonTimeNode, int slideIndex, SlideSize SlideSizes)
  53. {
  54. this.SlideSizes = SlideSizes;
  55. InitialState = 4;
  56. timingType = commonTimeNode.NodeType;
  57. Type = AnimationTypes.MotionPath;
  58. AnimateMotion motion = null;
  59. foreach (Object xmlEl in commonTimeNode.Descendants())
  60. if (xmlEl.GetType().Equals(typeof(AnimateMotion)))
  61. motion = (AnimateMotion)xmlEl;
  62. if (motion == null)
  63. return;
  64. String path = motion.Path.Value;
  65. String[] parts = path.Split();
  66. motionPath = new List<PathPart>();
  67. int indexPart = -1;
  68. bool isX = true;
  69. foreach (string part in parts)
  70. {
  71. if ("".Equals(part) || "E".Equals(part)) //We add our End tag
  72. continue;
  73. Double coords = 0.0;
  74. if (!Double.TryParse(part, NumberStyles.Float, CultureInfo.InvariantCulture, out coords))
  75. {
  76. isX = true;
  77. if (indexPart >= 0)
  78. { //FIX FOR POINTS WITH 3 COORDINATES UNTIL WE KNOW WHAT THEY ARE.
  79. List<PathPoint> previousPartPoints = motionPath[indexPart].points;
  80. if (previousPartPoints[previousPartPoints.Count - 1].Y.CompareTo(0) == 0)
  81. previousPartPoints.Remove(previousPartPoints[previousPartPoints.Count - 1]);
  82. }
  83. indexPart++;
  84. motionPath.Add(new PathPart());
  85. motionPath[indexPart].typeCharacter = part;
  86. }
  87. else if (isX)
  88. {
  89. coords = coords * MultiplierX();
  90. PathPoint newPoint = new PathPoint();
  91. newPoint.X = coords;
  92. motionPath[indexPart].points.Add(newPoint); // We have a new point
  93. isX = !isX;
  94. }
  95. else
  96. {
  97. coords = coords * MultiplierY();
  98. motionPath[indexPart].points[motionPath[indexPart].points.Count - 1].Y = coords; //Set Y for the last point
  99. isX = !isX;
  100. }
  101. }
  102. FixAnimationTimings(motion.CommonBehavior, slideIndex);
  103. generateAdditionDataString(motionPath);
  104. }
  105. private void generateAdditionDataString(List<PathPart> motionPath)
  106. {
  107. double wholeDistance = 0.0;
  108. PathPoint lastPoint = null;
  109. foreach (PathPart part in motionPath)
  110. {
  111. wholeDistance += part.Distance(lastPoint);
  112. lastPoint = part.LastPoint();
  113. }
  114. PathPart previousPart = null;
  115. String result = "";
  116. foreach (PathPart part in motionPath)
  117. {
  118. result += "|" + part.typeCharacter + " ";
  119. bool needsComma = false;
  120. if (previousPart != null && previousPart.LastPoint()!=null)
  121. {
  122. needsComma = true;
  123. result += previousPart.LastPoint().X.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB")) + "," +
  124. previousPart.LastPoint().Y.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
  125. }
  126. foreach (PathPoint point in part.points)
  127. {
  128. result += (needsComma ? "," : "") +
  129. point.X.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB")) + "," +
  130. point.Y.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
  131. needsComma = true;
  132. }
  133. if (previousPart != null) //First part doesn't have timing
  134. {
  135. double timing = (part.Distance(previousPart.LastPoint()) / wholeDistance) * ((double)Length / (double)1000);
  136. result += "," + timing.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
  137. }
  138. previousPart = part;
  139. }
  140. result += "|E " + previousPart.LastPoint().X.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB")) + "," +
  141. previousPart.LastPoint().Y.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
  142. AdditionalData = "'" + result + "'";
  143. }
  144. private class PathPart
  145. {
  146. public string typeCharacter { get; set; }
  147. public List<PathPoint> points { get; set; }
  148. public PathPart()
  149. {
  150. points = new List<PathPoint>();
  151. }
  152. public PathPoint LastPoint()
  153. {
  154. if (points == null || points.Count == 0)
  155. return null;
  156. return points[points.Count - 1];
  157. }
  158. public double Distance(PathPoint LastPoint)
  159. {
  160. double result = 0.0;
  161. if (points.Count == 1 && LastPoint != null)
  162. return distanceBetweenPoints(points[0], LastPoint);
  163. PathPoint previousPoint = null;
  164. foreach (PathPoint point in points)
  165. {
  166. if (previousPoint != null)
  167. result += distanceBetweenPoints(point, previousPoint);
  168. previousPoint = point;
  169. }
  170. return result;
  171. }
  172. private static double distanceBetweenPoints(PathPoint point1, PathPoint point2)
  173. {
  174. return Math.Sqrt(Math.Pow(point1.X - point2.X, 2) + Math.Pow(point1.Y - point2.Y, 2));
  175. }
  176. }
  177. class PathPoint
  178. {
  179. public double X { get; set; }
  180. public double Y { get; set; }
  181. }
  182. }
  183. }