MotionPathAnimation.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using DocumentFormat.OpenXml.Presentation;
  2. using HTEXLib.Models.Inner;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace HTEXLib.Animations
  10. {
  11. public class MotionPathAnimation : SimpleAnimation
  12. {
  13. public List<PathPart> motionPath { get; set; }
  14. private SlideSize SlideSizes;
  15. public double MultiplierX()
  16. {
  17. if (SlideSizes == null || SlideSizes.Cx == 0)
  18. {
  19. return 1;
  20. }
  21. else if (SlideSizes.Cx < Globals.px914400)
  22. {
  23. return Globals.px914400*1.0 / SlideSizes.Cx * Globals.px96;
  24. }
  25. else
  26. {
  27. return SlideSizes.Cx *1.0 * Globals.px96 / Globals.px914400;
  28. }
  29. }
  30. public double MultiplierY()
  31. {
  32. if (SlideSizes == null || SlideSizes.Cy == 0)
  33. {
  34. return 1;
  35. }
  36. else if (SlideSizes.Cy < Globals.px914400)
  37. {
  38. return Globals.px914400*1.0 / SlideSizes.Cy * Globals.px96;
  39. }
  40. else
  41. {
  42. return SlideSizes.Cy *1.0 * Globals.px96 / Globals.px914400;
  43. }
  44. }
  45. public MotionPathAnimation(CommonTimeNode commonTimeNode, int slideIndex, SlideSize SlideSizes)
  46. {
  47. this.SlideSizes = SlideSizes;
  48. InitialState = 4;
  49. timingType = commonTimeNode.NodeType;
  50. Type = AnimationTypes.MotionPath;
  51. AnimateMotion motion = null;
  52. foreach (Object xmlEl in commonTimeNode.Descendants())
  53. if (xmlEl.GetType().Equals(typeof(AnimateMotion)))
  54. motion = (AnimateMotion)xmlEl;
  55. if (motion == null)
  56. return;
  57. String path = motion.Path.Value;
  58. String[] parts = path.Split();
  59. motionPath = new List<PathPart>();
  60. int indexPart = -1;
  61. bool isX = true;
  62. foreach (string part in parts)
  63. {
  64. if ("".Equals(part) || "E".Equals(part)) //We add our End tag
  65. continue;
  66. Double coords = 0.0;
  67. if (!Double.TryParse(part, NumberStyles.Float, CultureInfo.InvariantCulture, out coords))
  68. {
  69. isX = true;
  70. if (indexPart >= 0)
  71. { //FIX FOR POINTS WITH 3 COORDINATES UNTIL WE KNOW WHAT THEY ARE.
  72. List<PathPoint> previousPartPoints = motionPath[indexPart].points;
  73. if (previousPartPoints[previousPartPoints.Count - 1].Y.CompareTo(0) == 0)
  74. previousPartPoints.Remove(previousPartPoints[previousPartPoints.Count - 1]);
  75. }
  76. indexPart++;
  77. motionPath.Add(new PathPart());
  78. motionPath[indexPart].typeCharacter = part;
  79. }
  80. else if (isX)
  81. {
  82. coords = coords * MultiplierX();
  83. PathPoint newPoint = new PathPoint();
  84. newPoint.X = coords;
  85. motionPath[indexPart].points.Add(newPoint); // We have a new point
  86. isX = !isX;
  87. }
  88. else
  89. {
  90. coords = coords * MultiplierY();
  91. motionPath[indexPart].points[motionPath[indexPart].points.Count - 1].Y = coords; //Set Y for the last point
  92. isX = !isX;
  93. }
  94. }
  95. FixAnimationTimings(motion.CommonBehavior, slideIndex);
  96. generateAdditionDataString(motionPath);
  97. }
  98. public MotionPathAnimation(CommonTimeNode commonTimeNode, int slideIndex)
  99. {
  100. InitialState = 4;
  101. timingType = commonTimeNode.NodeType;
  102. Type = AnimationTypes.MotionPath;
  103. AnimateMotion motion = null;
  104. foreach (Object xmlEl in commonTimeNode.Descendants())
  105. if (xmlEl.GetType().Equals(typeof(AnimateMotion)))
  106. motion = (AnimateMotion)xmlEl;
  107. if (motion == null)
  108. return;
  109. String path = motion.Path.Value;
  110. String[] parts = path.Split();
  111. motionPath = new List<PathPart>();
  112. int indexPart = -1;
  113. bool isX = true;
  114. foreach (string part in parts)
  115. {
  116. if ("".Equals(part) || "E".Equals(part)) //We add our End tag
  117. continue;
  118. Double coords = 0.0;
  119. if (!Double.TryParse(part, NumberStyles.Float, CultureInfo.InvariantCulture, out coords))
  120. {
  121. isX = true;
  122. if (indexPart >= 0)
  123. { //FIX FOR POINTS WITH 3 COORDINATES UNTIL WE KNOW WHAT THEY ARE.
  124. List<PathPoint> previousPartPoints = motionPath[indexPart].points;
  125. //if (previousPartPoints[previousPartPoints.Count - 1].Y.CompareTo(0) == 0)
  126. // previousPartPoints.Remove(previousPartPoints[previousPartPoints.Count - 1]);
  127. }
  128. indexPart++;
  129. motionPath.Add(new PathPart());
  130. motionPath[indexPart].typeCharacter = part;
  131. }
  132. else if (isX)
  133. {
  134. PathPoint newPoint = new PathPoint();
  135. newPoint.X = coords;
  136. motionPath[indexPart].points.Add(newPoint); // We have a new point
  137. isX = !isX;
  138. }
  139. else
  140. {
  141. motionPath[indexPart].points[motionPath[indexPart].points.Count - 1].Y = coords; //Set Y for the last point
  142. isX = !isX;
  143. }
  144. }
  145. FixAnimationTimings(motion.CommonBehavior, slideIndex);
  146. generateAdditionDataString(motionPath);
  147. }
  148. private void generateAdditionDataString(List<PathPart> motionPath)
  149. {
  150. double wholeDistance = 0.0;
  151. PathPoint lastPoint = null;
  152. foreach (PathPart part in motionPath)
  153. {
  154. wholeDistance += part.Distance(lastPoint);
  155. lastPoint = part.LastPoint();
  156. }
  157. PathPart previousPart = null;
  158. String result = "";
  159. foreach (PathPart part in motionPath)
  160. {
  161. result += "|" + part.typeCharacter + " ";
  162. bool needsComma = false;
  163. if (previousPart != null && previousPart.LastPoint() != null)
  164. {
  165. needsComma = true;
  166. result += previousPart.LastPoint().X.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB")) + "," +
  167. previousPart.LastPoint().Y.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
  168. }
  169. foreach (PathPoint point in part.points)
  170. {
  171. result += (needsComma ? "," : "") +
  172. point.X.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB")) + "," +
  173. point.Y.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
  174. needsComma = true;
  175. }
  176. if (previousPart != null) //First part doesn't have timing
  177. {
  178. double timing = (part.Distance(previousPart.LastPoint()) / wholeDistance) * ((double)Length / (double)1000);
  179. result += "," + timing.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
  180. }
  181. previousPart = part;
  182. }
  183. if (previousPart.points!=null&&previousPart.points.Count > 0) {
  184. result += "|E " + previousPart.LastPoint().X.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB")) + "," +
  185. previousPart.LastPoint().Y.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
  186. }
  187. AdditionalData = "'" + result + "'";
  188. }
  189. public class PathPart
  190. {
  191. public string typeCharacter { get; set; }
  192. public List<PathPoint> points { get; set; }
  193. public PathPart()
  194. {
  195. points = new List<PathPoint>();
  196. }
  197. public PathPoint LastPoint()
  198. {
  199. if (points == null || points.Count == 0)
  200. return null;
  201. return points[points.Count - 1];
  202. }
  203. public double Distance(PathPoint LastPoint)
  204. {
  205. double result = 0.0;
  206. if (points.Count == 1 && LastPoint != null)
  207. return distanceBetweenPoints(points[0], LastPoint);
  208. PathPoint previousPoint = null;
  209. foreach (PathPoint point in points)
  210. {
  211. if (previousPoint != null)
  212. result += distanceBetweenPoints(point, previousPoint);
  213. previousPoint = point;
  214. }
  215. return result;
  216. }
  217. private static double distanceBetweenPoints(PathPoint point1, PathPoint point2)
  218. {
  219. return System. Math.Sqrt(System.Math.Pow(point1.X - point2.X, 2) + System.Math.Pow(point1.Y - point2.Y, 2));
  220. }
  221. }
  222. public class PathPoint
  223. {
  224. public double X { get; set; }
  225. public double Y { get; set; }
  226. }
  227. }
  228. }