123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- using DocumentFormat.OpenXml.Presentation;
- using HTEXLib.Models.Inner;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace HTEXLib.Animations
- {
- public class MotionPathAnimation : SimpleAnimation
- {
- public List<PathPart> motionPath { get; set; }
- private SlideSize SlideSizes;
- public double MultiplierX()
- {
- if (SlideSizes == null || SlideSizes.Cx == 0)
- {
- return 1;
- }
- else if (SlideSizes.Cx < Globals.px914400)
- {
- return Globals.px914400*1.0 / SlideSizes.Cx * Globals.px96;
- }
- else
- {
- return SlideSizes.Cx *1.0 * Globals.px96 / Globals.px914400;
- }
- }
- public double MultiplierY()
- {
- if (SlideSizes == null || SlideSizes.Cy == 0)
- {
- return 1;
- }
- else if (SlideSizes.Cy < Globals.px914400)
- {
- return Globals.px914400*1.0 / SlideSizes.Cy * Globals.px96;
- }
- else
- {
- return SlideSizes.Cy *1.0 * Globals.px96 / Globals.px914400;
- }
- }
- public MotionPathAnimation(CommonTimeNode commonTimeNode, int slideIndex, SlideSize SlideSizes)
- {
- this.SlideSizes = SlideSizes;
- InitialState = 4;
- timingType = commonTimeNode.NodeType;
- Type = AnimationTypes.MotionPath;
- AnimateMotion motion = null;
- foreach (Object xmlEl in commonTimeNode.Descendants())
- if (xmlEl.GetType().Equals(typeof(AnimateMotion)))
- motion = (AnimateMotion)xmlEl;
- if (motion == null)
- return;
- String path = motion.Path.Value;
- String[] parts = path.Split();
- motionPath = new List<PathPart>();
- int indexPart = -1;
- bool isX = true;
- foreach (string part in parts)
- {
- if ("".Equals(part) || "E".Equals(part)) //We add our End tag
- continue;
- Double coords = 0.0;
- if (!Double.TryParse(part, NumberStyles.Float, CultureInfo.InvariantCulture, out coords))
- {
- isX = true;
- if (indexPart >= 0)
- { //FIX FOR POINTS WITH 3 COORDINATES UNTIL WE KNOW WHAT THEY ARE.
- List<PathPoint> previousPartPoints = motionPath[indexPart].points;
- if (previousPartPoints[previousPartPoints.Count - 1].Y.CompareTo(0) == 0)
- previousPartPoints.Remove(previousPartPoints[previousPartPoints.Count - 1]);
- }
- indexPart++;
- motionPath.Add(new PathPart());
- motionPath[indexPart].typeCharacter = part;
- }
- else if (isX)
- {
- coords = coords * MultiplierX();
- PathPoint newPoint = new PathPoint();
- newPoint.X = coords;
- motionPath[indexPart].points.Add(newPoint); // We have a new point
- isX = !isX;
- }
- else
- {
- coords = coords * MultiplierY();
- motionPath[indexPart].points[motionPath[indexPart].points.Count - 1].Y = coords; //Set Y for the last point
- isX = !isX;
- }
- }
- FixAnimationTimings(motion.CommonBehavior, slideIndex);
- generateAdditionDataString(motionPath);
- }
- public MotionPathAnimation(CommonTimeNode commonTimeNode, int slideIndex)
- {
- InitialState = 4;
- timingType = commonTimeNode.NodeType;
- Type = AnimationTypes.MotionPath;
- AnimateMotion motion = null;
- foreach (Object xmlEl in commonTimeNode.Descendants())
- if (xmlEl.GetType().Equals(typeof(AnimateMotion)))
- motion = (AnimateMotion)xmlEl;
- if (motion == null)
- return;
- String path = motion.Path.Value;
- String[] parts = path.Split();
- motionPath = new List<PathPart>();
- int indexPart = -1;
- bool isX = true;
- foreach (string part in parts)
- {
- if ("".Equals(part) || "E".Equals(part)) //We add our End tag
- continue;
- Double coords = 0.0;
- if (!Double.TryParse(part, NumberStyles.Float, CultureInfo.InvariantCulture, out coords))
- {
- isX = true;
- if (indexPart >= 0)
- { //FIX FOR POINTS WITH 3 COORDINATES UNTIL WE KNOW WHAT THEY ARE.
- List<PathPoint> previousPartPoints = motionPath[indexPart].points;
- //if (previousPartPoints[previousPartPoints.Count - 1].Y.CompareTo(0) == 0)
- // previousPartPoints.Remove(previousPartPoints[previousPartPoints.Count - 1]);
- }
- indexPart++;
- motionPath.Add(new PathPart());
- motionPath[indexPart].typeCharacter = part;
- }
- else if (isX)
- {
- PathPoint newPoint = new PathPoint();
- newPoint.X = coords;
- motionPath[indexPart].points.Add(newPoint); // We have a new point
- isX = !isX;
- }
- else
- {
- motionPath[indexPart].points[motionPath[indexPart].points.Count - 1].Y = coords; //Set Y for the last point
- isX = !isX;
- }
- }
- FixAnimationTimings(motion.CommonBehavior, slideIndex);
- generateAdditionDataString(motionPath);
- }
- private void generateAdditionDataString(List<PathPart> motionPath)
- {
- double wholeDistance = 0.0;
- PathPoint lastPoint = null;
- foreach (PathPart part in motionPath)
- {
- wholeDistance += part.Distance(lastPoint);
- lastPoint = part.LastPoint();
- }
- PathPart previousPart = null;
- String result = "";
- foreach (PathPart part in motionPath)
- {
- result += "|" + part.typeCharacter + " ";
- bool needsComma = false;
- if (previousPart != null && previousPart.LastPoint() != null)
- {
- needsComma = true;
- result += previousPart.LastPoint().X.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB")) + "," +
- previousPart.LastPoint().Y.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
- }
- foreach (PathPoint point in part.points)
- {
- result += (needsComma ? "," : "") +
- point.X.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB")) + "," +
- point.Y.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
- needsComma = true;
- }
- if (previousPart != null) //First part doesn't have timing
- {
- double timing = (part.Distance(previousPart.LastPoint()) / wholeDistance) * ((double)Length / (double)1000);
- result += "," + timing.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
- }
- previousPart = part;
- }
- if (previousPart.points!=null&&previousPart.points.Count > 0) {
- result += "|E " + previousPart.LastPoint().X.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB")) + "," +
- previousPart.LastPoint().Y.ToString("0.##########", CultureInfo.CreateSpecificCulture("en-GB"));
- }
-
- AdditionalData = "'" + result + "'";
- }
- public class PathPart
- {
- public string typeCharacter { get; set; }
- public List<PathPoint> points { get; set; }
- public PathPart()
- {
- points = new List<PathPoint>();
- }
- public PathPoint LastPoint()
- {
- if (points == null || points.Count == 0)
- return null;
- return points[points.Count - 1];
- }
- public double Distance(PathPoint LastPoint)
- {
- double result = 0.0;
- if (points.Count == 1 && LastPoint != null)
- return distanceBetweenPoints(points[0], LastPoint);
- PathPoint previousPoint = null;
- foreach (PathPoint point in points)
- {
- if (previousPoint != null)
- result += distanceBetweenPoints(point, previousPoint);
- previousPoint = point;
- }
- return result;
- }
- private static double distanceBetweenPoints(PathPoint point1, PathPoint point2)
- {
- return System. Math.Sqrt(System.Math.Pow(point1.X - point2.X, 2) + System.Math.Pow(point1.Y - point2.Y, 2));
- }
- }
- public class PathPoint
- {
- public double X { get; set; }
- public double Y { get; set; }
- }
- }
- }
|