JSONGenerator.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. using ClearSlideLibrary.Dom;
  2. using DocumentFormat.OpenXml;
  3. using DocumentFormat.OpenXml.Presentation;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Text.RegularExpressions;
  9. namespace ClearSlideLibrary.Animations
  10. {
  11. public class JSONGenerator
  12. {
  13. public PPTSlide Slide { get; set; }
  14. public JSONGenerator(PPTSlide slide)
  15. {
  16. this.Slide = slide;
  17. }
  18. public JSONGenerator(List<PPTSlide> slides)
  19. {
  20. this.Slide = slides.ElementAt(0); //TODO: add support for more than one slide
  21. }
  22. public string GetAnimaVariable()
  23. {
  24. return "var animations = " + GetAnimationsJSON() + ";";
  25. }
  26. /*
  27. * Fix the timing for animations that need to start without a click. Make the tree simpler by removing nodes that are empty or nested.
  28. *
  29. * When a node has a start type != ClickEffect , we add the Animations from this timenode to the animations list in the previous time node.
  30. * If it is type After the previous one we need to set Delay = Length of the previous one.
  31. *
  32. * Removing nested nodes:
  33. * Example 1: Node -> Node -> Node - every node has a single child. In this case we can create one time node with the timing for both.
  34. * Example 2: We have only one node (one simple animation) and all the other are nested (inner animations) - we remove the parent time node and
  35. * add its timing to the timing of the nested animations.
  36. *
  37. */
  38. private List<IAnimation> fixAnimationTimings(List<IAnimation> animations)
  39. {
  40. animations = fixNestedAnimations(animations);
  41. List<IAnimation> result = new List<IAnimation>();
  42. if (animations == null || animations.Count == 0)
  43. return result;
  44. //If we have only one animation and all the others are nested - add the delay to the inner animations and remove this one.
  45. if (animations.Count == 1 && animations[0].InnerAnimations != null && animations[0].InnerAnimations.Count > 0)
  46. {
  47. foreach (IAnimation anim in animations[0].InnerAnimations)
  48. anim.Start += animations[0].Start;
  49. return fixAnimationTimings(animations[0].InnerAnimations);
  50. }
  51. IAnimation previousAnimation = null;
  52. foreach (IAnimation animation in animations)
  53. {
  54. SimpleAnimation sAnimation = (SimpleAnimation)animation;
  55. if (previousAnimation == null)
  56. {
  57. previousAnimation = sAnimation;
  58. continue;
  59. }
  60. //These are the cases where animation starts without a click. There migth be other cases which are not handled yet.
  61. else if (sAnimation.timingType.HasValue && (
  62. TimeNodeValues.WithEffect.Equals(sAnimation.timingType.Value) ||
  63. TimeNodeValues.AfterEffect.Equals(sAnimation.timingType.Value) ||
  64. TimeNodeValues.WithGroup.Equals(sAnimation.timingType.Value) ||
  65. TimeNodeValues.AfterGroup.Equals(sAnimation.timingType.Value)))
  66. {
  67. int newLenegth = 0;
  68. if (TimeNodeValues.AfterEffect.Equals(sAnimation.timingType.Value) ||
  69. TimeNodeValues.AfterGroup.Equals(sAnimation.timingType.Value))
  70. {
  71. /*If animation is a part of a group then it has delay according to the start of the parent time node (animation).
  72. * In this case we use this delay because it is the proper way power point handles such animations.
  73. * If by some reason there isn't such delay and the animation type is set to 'After' previous
  74. * we set the start point to right after the previous animation ends." */
  75. if (sAnimation.Start <= previousAnimation.Length + previousAnimation.Start)
  76. sAnimation.Start = previousAnimation.Length + previousAnimation.Start;
  77. if (sAnimation.InnerAnimations != null)
  78. foreach (IAnimation anim in sAnimation.InnerAnimations)
  79. anim.Start = anim.Start + sAnimation.Start;
  80. newLenegth = sAnimation.Length + sAnimation.Start;
  81. }
  82. else
  83. {
  84. newLenegth = (sAnimation.Length + sAnimation.Start >
  85. previousAnimation.Length + previousAnimation.Start)
  86. ? sAnimation.Length + sAnimation.Start
  87. : previousAnimation.Length + previousAnimation.Start;
  88. }
  89. /*Joins the current animation and the previous one and recalculates the length*/
  90. if (previousAnimation.InnerAnimations != null && previousAnimation.InnerAnimations.Count > 0)
  91. {
  92. previousAnimation.Length = newLenegth;
  93. if (sAnimation.InnerAnimations == null || sAnimation.InnerAnimations.Count == 0)
  94. previousAnimation.InnerAnimations.Add(sAnimation);
  95. else
  96. previousAnimation.InnerAnimations.AddRange(sAnimation.InnerAnimations);
  97. continue;
  98. }
  99. else
  100. {
  101. SimpleAnimation tempAnimation = new SimpleAnimation();
  102. tempAnimation.Length = newLenegth;
  103. tempAnimation.timingType = ((SimpleAnimation)previousAnimation).timingType;
  104. tempAnimation.InnerAnimations = new List<IAnimation>();
  105. if (previousAnimation.InnerAnimations == null || previousAnimation.InnerAnimations.Count == 0)
  106. tempAnimation.InnerAnimations.Add(previousAnimation);
  107. else
  108. tempAnimation.InnerAnimations.AddRange(previousAnimation.InnerAnimations);
  109. if (sAnimation.InnerAnimations == null || sAnimation.InnerAnimations.Count == 0)
  110. tempAnimation.InnerAnimations.Add(sAnimation);
  111. else
  112. tempAnimation.InnerAnimations.AddRange(sAnimation.InnerAnimations);
  113. previousAnimation = tempAnimation;
  114. continue;
  115. }
  116. }
  117. result.Add(previousAnimation);
  118. previousAnimation = sAnimation;
  119. }
  120. result.Add(previousAnimation);
  121. return result;
  122. }
  123. /*
  124. * When the animation has only one inner animation then return the inner animation. Do this recursive until we get the real one.
  125. */
  126. private IAnimation getAnimationFromNested(IAnimation sAnimation)
  127. {
  128. if (sAnimation.InnerAnimations != null && sAnimation.InnerAnimations.Count == 1)
  129. {
  130. sAnimation.InnerAnimations[0].Start += sAnimation.Start;
  131. return getAnimationFromNested(sAnimation.InnerAnimations[0]);
  132. }
  133. if (((SimpleAnimation)sAnimation).timingType == null && sAnimation.InnerAnimations != null)
  134. ((SimpleAnimation)sAnimation).timingType = ((SimpleAnimation)getAnimationFromNested(sAnimation.InnerAnimations[0])).timingType;
  135. return sAnimation;
  136. }
  137. /*
  138. * Generating the JSON for the animations for one slide only. It has some support for nested animations but not everything works.
  139. */
  140. public string GetAnimationsJSON()
  141. {
  142. if (Slide == null)
  143. {
  144. return "{s1:{t:{i:1000,c:1,v:0,n:0},g:0,a:0,f:0,c:0}}";
  145. }
  146. String result = "";
  147. int animations = 0;
  148. List<IAnimation> fixedAnimations = fixAnimationTimings(Slide.Animations);
  149. animations = fixedAnimations.Count;
  150. if (Slide.Transition == null)
  151. {
  152. result = "{s1:{t:{i:0,c:1,v:0,n:0},g:0,a:0,f:0,c:##ANIMATIONS_PLACEHOLDER##}}";
  153. }
  154. else
  155. {
  156. result = "{s1:{t:{i:" + (Slide.Transition.Length + Slide.Transition.Start) +
  157. ",c:" + Slide.Transition.GetJsonString() + "},g:0,a:0,f:0,c:##ANIMATIONS_PLACEHOLDER##}}";
  158. }
  159. if (animations == 0)
  160. {
  161. result = result.Replace("##ANIMATIONS_PLACEHOLDER##", "0");
  162. return result;
  163. }
  164. SimpleAnimation checkEffectFirstAnim = (SimpleAnimation)fixedAnimations[0];
  165. if (checkEffectFirstAnim.timingType.HasValue && checkEffectFirstAnim.timingType.Value != TimeNodeValues.ClickEffect)
  166. {
  167. result = result.Replace("n:0", "n:1");
  168. //If it is just one Animation we change this in the transition! - the transition is like the first Animation!
  169. }
  170. string animationsString = "{i:" + animations + getAnimationsString(fixedAnimations, true) + "}";
  171. result = result.Replace("##ANIMATIONS_PLACEHOLDER##", animationsString);
  172. return result;
  173. }
  174. /*
  175. * Generate JSON for animation tree. Recursive going into the tree - there is some difference in the animation string
  176. * for the first level and the next levels.
  177. */
  178. private string getAnimationsString(List<IAnimation> animations, bool firstLevel)
  179. {
  180. String animationList = "";
  181. int index = 0;
  182. foreach (IAnimation animation in animations)
  183. {
  184. if (animation.InnerAnimations == null || animation.InnerAnimations.Count == 0)
  185. {
  186. if (!firstLevel)
  187. animationList = animationList + ",c" + index++ + ":" + animation.GetJsonString();
  188. else
  189. animationList = animationList + ",c" + index++ + ":{i:" + (((SimpleAnimation)animation).getCalculatedLength() + animation.Start) + ",c0:" + animation.GetJsonString() + "}";
  190. }
  191. else
  192. // Nested animatons (several animations in one time node, or in different time nodes that start together!
  193. {
  194. String innerAnimationsString = getAnimationsString(animation.InnerAnimations, false);
  195. animationList = animationList + ",c" + index++ + ":{i:" + (((SimpleAnimation)animation).getCalculatedLength() + animation.Start) +
  196. innerAnimationsString + "}";
  197. }
  198. }
  199. return animationList;
  200. }
  201. public static IAnimation GenerateTransitionAnimationObject(Transition trans)
  202. {
  203. if (trans == null || trans.FirstChild == null)
  204. return null;
  205. TransitionAnimation result = new TransitionAnimation();
  206. if (trans.Speed != null && TransitionSpeedValues.Fast.Equals(trans.Speed.Value))
  207. result.Length = 500;
  208. else if (trans.Speed != null && TransitionSpeedValues.Slow.Equals(trans.Speed.Value))
  209. result.Length = 2000;
  210. else if (trans.Duration != null)
  211. result.Length = int.Parse(trans.Duration.Value);
  212. else
  213. result.Length = 1000;
  214. result.Start = 0;
  215. result.InitialState = -1;
  216. result.Repetitions = 1;
  217. if (trans.AdvanceOnClick != null)
  218. result.AdvanceOnClick = trans.AdvanceOnClick.Value;
  219. else result.AdvanceOnClick = true;
  220. if (typeof(StripsTransition) == trans.FirstChild.GetType())
  221. {
  222. StripsTransition transition = (StripsTransition)trans.FirstChild;
  223. result.Type = AnimationTypes.Strips;
  224. if (transition.Direction != null &&
  225. TransitionCornerDirectionValues.RightUp.Equals(transition.Direction.Value))
  226. result.AdditionalData = "7";
  227. else if (transition.Direction != null &&
  228. TransitionCornerDirectionValues.LeftDown.Equals(transition.Direction.Value))
  229. result.AdditionalData = "9";
  230. else if (transition.Direction != null &&
  231. TransitionCornerDirectionValues.RightDown.Equals(transition.Direction.Value))
  232. result.AdditionalData = "8";
  233. else
  234. result.AdditionalData = "6";
  235. }
  236. else if (typeof(PushTransition) == trans.FirstChild.GetType())
  237. {
  238. PushTransition transition = (PushTransition)trans.FirstChild;
  239. result.Type = AnimationTypes.Push;
  240. if (transition.Direction != null &&
  241. TransitionSlideDirectionValues.Right.Equals(transition.Direction.Value))
  242. result.AdditionalData = "3";
  243. else if (transition.Direction != null &&
  244. TransitionSlideDirectionValues.Up.Equals(transition.Direction.Value))
  245. result.AdditionalData = "4";
  246. else if (transition.Direction != null &&
  247. TransitionSlideDirectionValues.Left.Equals(transition.Direction.Value))
  248. result.AdditionalData = "2";
  249. else result.AdditionalData = "1";
  250. }
  251. else if (typeof(WipeTransition) == trans.FirstChild.GetType())
  252. {
  253. WipeTransition transition = (WipeTransition)trans.FirstChild;
  254. result.Type = AnimationTypes.Wipe;
  255. if (transition.Direction != null &&
  256. TransitionSlideDirectionValues.Right.Equals(transition.Direction.Value))
  257. result.AdditionalData = "4";
  258. else if (transition.Direction != null &&
  259. TransitionSlideDirectionValues.Up.Equals(transition.Direction.Value))
  260. result.AdditionalData = "3";
  261. else if (transition.Direction != null &&
  262. TransitionSlideDirectionValues.Left.Equals(transition.Direction.Value))
  263. result.AdditionalData = "2";
  264. else result.AdditionalData = "1";
  265. }
  266. else if (typeof(FadeTransition) == trans.FirstChild.GetType())
  267. {
  268. FadeTransition transition = (FadeTransition)trans.FirstChild;
  269. result.Type = AnimationTypes.Fade;
  270. if (transition.ThroughBlack != null && transition.ThroughBlack.Value)
  271. result.Type = AnimationTypes.FadeThroughBlack;
  272. result.AdditionalData = "1";
  273. }
  274. else if (typeof(CutTransition) == trans.FirstChild.GetType())
  275. {
  276. CutTransition transition = (CutTransition)trans.FirstChild;
  277. result.Type = AnimationTypes.Cut;
  278. result.AdditionalData = "2";
  279. if (transition.ThroughBlack != null && transition.ThroughBlack.Value)
  280. {
  281. result.Type = AnimationTypes.CutThroughBlack;
  282. result.AdditionalData = "3";
  283. }
  284. }
  285. else if (typeof(DissolveTransition) == trans.FirstChild.GetType())
  286. {
  287. result.Type = AnimationTypes.DissolveIn;
  288. result.AdditionalData = "1";
  289. }
  290. else if (typeof(WedgeTransition) == trans.FirstChild.GetType())
  291. {
  292. result.Type = AnimationTypes.Wedge;
  293. result.AdditionalData = "1";
  294. }
  295. else if (typeof(PullTransition) == trans.FirstChild.GetType())
  296. {
  297. PullTransition transition = (PullTransition)trans.FirstChild;
  298. result.Type = AnimationTypes.UnCover;
  299. if (transition.Direction != null && "d".Equals(transition.Direction.Value.ToString()))
  300. result.AdditionalData = "9";
  301. else if (transition.Direction != null && "r".Equals(transition.Direction.Value.ToString()))
  302. result.AdditionalData = "11";
  303. else if (transition.Direction != null && "u".Equals(transition.Direction.Value.ToString()))
  304. result.AdditionalData = "12";
  305. else if (transition.Direction != null && "ld".Equals(transition.Direction.Value.ToString()))
  306. result.AdditionalData = "13";
  307. else if (transition.Direction != null && "lu".Equals(transition.Direction.Value.ToString()))
  308. result.AdditionalData = "14";
  309. else if (transition.Direction != null && "rd".Equals(transition.Direction.Value.ToString()))
  310. result.AdditionalData = "15";
  311. else if (transition.Direction != null && "ru".Equals(transition.Direction.Value.ToString()))
  312. result.AdditionalData = "16";
  313. else result.AdditionalData = "10";
  314. }
  315. else if (typeof(ZoomTransition) == trans.FirstChild.GetType())
  316. {
  317. ZoomTransition transition = (ZoomTransition)trans.FirstChild;
  318. result.Type = AnimationTypes.Box;
  319. if (transition.Direction != null && TransitionInOutDirectionValues.In == transition.Direction.Value)
  320. result.AdditionalData = "19";
  321. else result.AdditionalData = "20";
  322. }
  323. else if (typeof(ZoomTransition) == trans.FirstChild.GetType())
  324. {
  325. ZoomTransition transition = (ZoomTransition)trans.FirstChild;
  326. result.Type = AnimationTypes.Box;
  327. if (transition.Direction != null && TransitionInOutDirectionValues.In == transition.Direction.Value)
  328. result.AdditionalData = "19";
  329. else result.AdditionalData = "20";
  330. }
  331. else if (typeof(WheelTransition) == trans.FirstChild.GetType())
  332. {
  333. WheelTransition transition = (WheelTransition)trans.FirstChild;
  334. result.Type = AnimationTypes.Wheel;
  335. if (transition.Spokes != null && transition.Spokes.Value == 1)
  336. result.AdditionalData = "1";
  337. else if (transition.Spokes != null && transition.Spokes.Value == 2)
  338. result.AdditionalData = "2";
  339. else if (transition.Spokes != null && transition.Spokes.Value == 3)
  340. result.AdditionalData = "3";
  341. else if (transition.Spokes != null && transition.Spokes.Value == 8)
  342. result.AdditionalData = "8";
  343. else result.AdditionalData = "4";
  344. }
  345. else if (typeof(SplitTransition) == trans.FirstChild.GetType())
  346. {
  347. SplitTransition transition = (SplitTransition)trans.FirstChild;
  348. result.Type = AnimationTypes.Split;
  349. if (transition.Direction != null && TransitionInOutDirectionValues.In == transition.Direction.Value)
  350. {
  351. if (transition.Orientation != null && DirectionValues.Vertical.Equals(transition.Orientation.Value))
  352. result.AdditionalData = "25";
  353. else
  354. result.AdditionalData = "23";
  355. }
  356. else
  357. {
  358. if (transition.Orientation != null && DirectionValues.Vertical.Equals(transition.Orientation.Value))
  359. result.AdditionalData = "26";
  360. else
  361. result.AdditionalData = "24";
  362. }
  363. }
  364. else if (typeof(CircleTransition) == trans.FirstChild.GetType())
  365. {
  366. result.Type = AnimationTypes.Circle;
  367. result.AdditionalData = "20";
  368. }
  369. else if (typeof(DiamondTransition) == trans.FirstChild.GetType())
  370. {
  371. result.Type = AnimationTypes.Diamond;
  372. result.AdditionalData = "20";
  373. }
  374. else if (typeof(PlusTransition) == trans.FirstChild.GetType())
  375. {
  376. result.Type = AnimationTypes.Plus;
  377. result.AdditionalData = "20";
  378. }
  379. else if (typeof(NewsflashTransition) == trans.FirstChild.GetType())
  380. {
  381. result.Type = AnimationTypes.Newsflash;
  382. result.AdditionalData = "20";
  383. }
  384. return result;
  385. }
  386. public SimpleAnimation getSimpleAnimationFromCommonTimeNodePreset(CommonTimeNode commonTimeNode, SlideSize SlideSizes)
  387. {
  388. SimpleAnimation result = new SimpleAnimation();
  389. if (AnimationTypes.TypePath.Equals(commonTimeNode.PresetClass))
  390. {
  391. return new MotionPathAnimation(commonTimeNode, Slide.slideIndex, SlideSizes);
  392. }
  393. else if (AnimationTypes.TypeEntrance.Equals(commonTimeNode.PresetClass))
  394. {
  395. result.InitialState = 1;
  396. }
  397. else if (AnimationTypes.TypeExit.Equals(commonTimeNode.PresetClass))
  398. {
  399. result.InitialState = 2;
  400. }
  401. else if (AnimationTypes.TypeEmphasis.Equals(commonTimeNode.PresetClass))
  402. {
  403. result.InitialState = 3;
  404. }
  405. else return null;
  406. if (commonTimeNode.PresetId == null)
  407. return null;
  408. result.timingType = commonTimeNode.NodeType;
  409. //Get the speed from one of the nodes common behavior. Hopefully all nodes have the same speed (since the animation is the same).
  410. foreach (Object xmlEl in commonTimeNode.Descendants())
  411. if (xmlEl.GetType().Equals(typeof(CommonBehavior)))
  412. {
  413. CommonBehavior bhvr = ((CommonBehavior)xmlEl);
  414. if (bhvr.CommonTimeNode != null)
  415. {
  416. result.FixAnimationTimings(bhvr, Slide.slideIndex);
  417. if (result.Length <= 1)
  418. continue;
  419. if (result.Start == 0)
  420. {
  421. Condition condition = commonTimeNode.StartConditionList.FirstChild as Condition;
  422. if (!condition.Delay.Equals("indefinite"))
  423. result.Start = int.Parse(condition.Delay);
  424. }
  425. break;
  426. }
  427. }
  428. if (result.Length <= 1)
  429. result.Length = 100; //Default value??
  430. if (AnimationTypes.TypeEmphasis.Equals(commonTimeNode.PresetClass))
  431. result = handleEmphasisAnimation(commonTimeNode, result);
  432. else
  433. result = handleEntranceAnimation(commonTimeNode, result);
  434. if (result.AdditionalData == null || result.AdditionalData == "0") //There are default values. Horizontal In = horizontal + in ;)
  435. switch (commonTimeNode.PresetSubtype.Value)
  436. {
  437. case 0: result.AdditionalData = "0"; break;
  438. case 4: result.AdditionalData = "3"; break; //From bottom
  439. case 2: result.AdditionalData = "2"; break; //From right
  440. case 1: result.AdditionalData = "1"; break; //From top
  441. case 8: result.AdditionalData = "4"; break; //From left
  442. case 6: result.AdditionalData = "8"; break; //Bottom right
  443. case 3: result.AdditionalData = "7"; break; //Top right
  444. case 9: result.AdditionalData = "6"; break; //Top right
  445. case 12: result.AdditionalData = "9"; break; //Bottom left
  446. case 10: result.AdditionalData = "16"; break; //Horizontal
  447. case 5: result.AdditionalData = "17"; break; //Vertical
  448. case 26: result.AdditionalData = "23"; break; //Horizontal in
  449. case 42: result.AdditionalData = "24"; break; //Horizontal out
  450. case 21: result.AdditionalData = "25"; break; //Vertical in
  451. case 37: result.AdditionalData = "26"; break; //Vertical out
  452. case 16: result.AdditionalData = "19"; break; //in
  453. case 32: result.AdditionalData = "20"; break; //out
  454. }
  455. checkIsText(result);
  456. return result;
  457. }
  458. private void checkIsText(SimpleAnimation result)
  459. {
  460. int res;
  461. bool isTextWithEffect = int.TryParse(result.ObjectId, out res) && PPTShape.effectShapes.Contains(Slide.slideIndex + "_" + res);
  462. int tryParse = 0;
  463. if (!int.TryParse(result.ObjectId, out tryParse) || isTextWithEffect) {
  464. return;//if it is like 123p -> return;
  465. }
  466. foreach (PPTShapeBase shape in Slide.ContainerShape.Elements)
  467. if (typeof(PPTShape).Equals(shape.GetType()) && ((PPTShape)shape).IsText)
  468. {
  469. string shapeId = shape.NonVisualShapeProp.Id;
  470. string shapeObjectId = "s1s" + result.ObjectId;
  471. if (shapeId.Equals(shapeObjectId))
  472. {
  473. result.ObjectId = result.ObjectId + "p0";// TODO check when multiple paragraphs
  474. }
  475. }
  476. }
  477. private EmphasisAnimation handleEmphasisAnimation(CommonTimeNode commonTimeNode, SimpleAnimation simpleAnim)
  478. {
  479. EmphasisAnimation result = new EmphasisAnimation(simpleAnim);
  480. result.setRgbColor(commonTimeNode, Slide);
  481. switch (commonTimeNode.PresetId.Value) //Presets for Entrance/Exit
  482. {
  483. case 3: result.Type = AnimationTypes.ChangeFontColor; break;
  484. case 19: result.Type = AnimationTypes.ColorBlend; break;
  485. case 14: result.Type = AnimationTypes.Blast; break;
  486. case 26: result.Type = AnimationTypes.FlashBulb; break;
  487. case 35: result.Type = AnimationTypes.Blink; break;
  488. case 9:
  489. {
  490. result.Type = AnimationTypes.Transparency;
  491. result.Transparency = getTransparence(commonTimeNode);
  492. } break;
  493. case 20:
  494. {
  495. result.Type = AnimationTypes.ColorWave;
  496. result.Length *= 2;
  497. result.e2 = result.Length / 10;
  498. result.e1 = 2;
  499. } break;
  500. case 16:
  501. {
  502. result.Type = AnimationTypes.BrushOnColor;
  503. result.e2 = result.Length / 25;
  504. result.e1 = 2;
  505. } break;
  506. case 33:
  507. {
  508. result.Type = AnimationTypes.VerticalHighlight;
  509. result.Length *= 2;
  510. } break;
  511. case 27:
  512. {
  513. result.Type = AnimationTypes.Flicker;
  514. result.Length *= 2;
  515. } break;
  516. case 36:
  517. {
  518. result.Type = AnimationTypes.Shimmer;
  519. result.e2 = result.Length / 5;
  520. result.Length *= 2;
  521. foreach (Object obj in commonTimeNode.Descendants())
  522. if (obj.GetType().Equals(typeof(AnimateScale)))
  523. {
  524. ((EmphasisAnimation)result).ScaleX = ((AnimateScale)obj).ToPosition.X.Value / 1000;
  525. ((EmphasisAnimation)result).ScaleY = ((AnimateScale)obj).ToPosition.Y.Value / 1000;
  526. break;
  527. }
  528. } break;
  529. case 28:
  530. {
  531. result.Type = AnimationTypes.GrowwithColor;
  532. result.e2 = result.Length / 10;
  533. result.e1 = 2;
  534. } break;
  535. case 32:
  536. {
  537. result.Type = AnimationTypes.Teeter;
  538. result.Length *= 2;
  539. } break;
  540. case 34:
  541. {
  542. result.Type = AnimationTypes.Wave;
  543. result.e2 = result.Length / 5;
  544. result.Length *= 2;
  545. } break;
  546. case 8:
  547. {
  548. result.Type = AnimationTypes.Spin;
  549. foreach (Object obj in commonTimeNode.Descendants())
  550. if (obj.GetType().Equals(typeof(AnimateRotation)))
  551. {
  552. ((EmphasisAnimation)result).RotationDegrees = ((AnimateRotation)obj).By / 60000;
  553. break;
  554. }
  555. } break;
  556. case 6:
  557. {
  558. result.Type = AnimationTypes.GrowShrink;
  559. foreach (Object obj in commonTimeNode.Descendants())
  560. if (obj.GetType().Equals(typeof(AnimateScale)))
  561. {
  562. ((EmphasisAnimation)result).ScaleX = ((AnimateScale)obj).ByPosition.X.Value / 1000;
  563. ((EmphasisAnimation)result).ScaleY = ((AnimateScale)obj).ByPosition.Y.Value / 1000;
  564. break;
  565. }
  566. } break;
  567. default: return null;
  568. }
  569. return result;
  570. }
  571. private SimpleAnimation handleEntranceAnimation(CommonTimeNode commonTimeNode, SimpleAnimation result)
  572. {
  573. switch (commonTimeNode.PresetId.Value) //Presets for Entrance/Exit
  574. {
  575. case 1: result.Type = AnimationTypes.Appear; break;
  576. case 54: result.Type = AnimationTypes.Glide; break;
  577. case 19: result.Type = AnimationTypes.Swivel; break;
  578. case 42: result.Type = AnimationTypes.Ascend; break;
  579. case 3: result.Type = AnimationTypes.Blinds; break;
  580. case 4: result.Type = AnimationTypes.Box; break;
  581. case 25: result.Type = AnimationTypes.Boomerang; break;
  582. case 43: result.Type = AnimationTypes.CenterRevolve; break;
  583. case 5: result.Type = AnimationTypes.Checkerboard; break;
  584. case 50: result.Type = AnimationTypes.Compress; break;
  585. case 7: result.Type = AnimationTypes.TypeEntrance.Equals(commonTimeNode.PresetClass) ? AnimationTypes.CrawlIn : AnimationTypes.CrawlOut; break;
  586. case 47: result.Type = AnimationTypes.Descend; break;
  587. case 48: result.Type = AnimationTypes.Sling; break;
  588. case 29: result.Type = AnimationTypes.TypeEntrance.Equals(commonTimeNode.PresetClass) ? AnimationTypes.EaseIn : AnimationTypes.EaseOut; break;
  589. case 55: result.Type = AnimationTypes.TypeEntrance.Equals(commonTimeNode.PresetClass) ? AnimationTypes.Expand : AnimationTypes.Contract; break;
  590. case 10: result.Type = AnimationTypes.Fade; break;
  591. case 53: result.Type = AnimationTypes.FadedZoom; break;
  592. case 11: result.Type = AnimationTypes.FlashOnce; break;
  593. case 2: result.Type = AnimationTypes.TypeEntrance.Equals(commonTimeNode.PresetClass) ? AnimationTypes.FlyIn : AnimationTypes.FlyOut; break;
  594. case 58: result.Type = AnimationTypes.Fold; break;
  595. case 31: result.Type = AnimationTypes.GrowTurn; break;
  596. case 12: result.Type = AnimationTypes.TypeEntrance.Equals(commonTimeNode.PresetClass) ? AnimationTypes.PeekIn : AnimationTypes.PeekOut; break;
  597. case 16: result.Type = AnimationTypes.Split; break;
  598. case 35: result.Type = AnimationTypes.Pinwheel; break;
  599. case 14: result.Type = AnimationTypes.RandomBars; break;
  600. case 37: result.Type = AnimationTypes.TypeEntrance.Equals(commonTimeNode.PresetClass) ? AnimationTypes.RiseUp : AnimationTypes.SinkDown; break;
  601. case 49: result.Type = AnimationTypes.Spinner; break;
  602. case 22: result.Type = AnimationTypes.Wipe; break;
  603. case 18: result.Type = AnimationTypes.Strips; break;
  604. case 26: result.Type = AnimationTypes.Bounce; result.Length = (int)((result.Length * 100) / 29); break;//time tunning
  605. case 6: result.Type = AnimationTypes.Circle; break;
  606. case 28: result.Type = AnimationTypes.Credits; break;
  607. case 52: result.Type = AnimationTypes.TypeEntrance.Equals(commonTimeNode.PresetClass) ? AnimationTypes.CurveUp : AnimationTypes.CurveDown; break;
  608. case 8: result.Type = AnimationTypes.Diamond; break;
  609. case 9: result.Type = AnimationTypes.Dissolve; break;
  610. case 30: result.Type = AnimationTypes.Float; break;
  611. case 34: result.Type = AnimationTypes.LightSpeed; result.Length = (int)((result.Length * 5) / 3); break;//time tunning
  612. case 51: result.Type = AnimationTypes.Magnify; result.Length = (int)((result.Length * 200) / 77); break;//time tunning
  613. case 13: result.Type = AnimationTypes.Plus; break;
  614. case 15: result.Type = AnimationTypes.TypeEntrance.Equals(commonTimeNode.PresetClass) ? AnimationTypes.SpiralIn : AnimationTypes.SpiralOut; break;
  615. case 17: result.Type = AnimationTypes.Stretch; break;
  616. case 39: result.Type = AnimationTypes.Thread; break;
  617. case 20: result.Type = AnimationTypes.Wedge; break;
  618. case 21: result.Type = AnimationTypes.Wheel; result.AdditionalData = "" + commonTimeNode.PresetSubtype.Value; break;
  619. case 23: result.Type = AnimationTypes.Zoom; break;
  620. default: return null;
  621. }
  622. return result;
  623. }
  624. private double getTransparence(CommonTimeNode commonTimeNode)
  625. {
  626. if (typeof(AnimateEffect) == commonTimeNode.LastChild.LastChild.GetType())
  627. {
  628. AnimateEffect animEffect = (AnimateEffect)commonTimeNode.LastChild.LastChild;
  629. string value = animEffect.PropertyList.Value;
  630. if (value != null && value.IndexOf("opacity:") != -1)
  631. {
  632. string opacityStr = value.Substring(9);//we need 0.75 from "opacity: 0.75"
  633. double opacity = double.Parse(opacityStr,CultureInfo.GetCultureInfo("en-US").NumberFormat);
  634. return 1 - opacity;
  635. }
  636. }
  637. return 0;
  638. }
  639. private List<IAnimation> fixNestedAnimations(List<IAnimation> animations)
  640. {
  641. if (animations == null || animations.Count == 0)
  642. return null;
  643. List<IAnimation> result = new List<IAnimation>();
  644. foreach (IAnimation anim in animations)
  645. {
  646. IAnimation animation = getAnimationFromNested(anim);
  647. animation.InnerAnimations = fixNestedAnimations(animation.InnerAnimations);
  648. result.Add(animation);
  649. }
  650. return result;
  651. }
  652. }
  653. }