DataParser.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. using System;
  2. using System.Collections.Generic;
  3. namespace DragonBones
  4. {
  5. /**
  6. * @private
  7. */
  8. public abstract class DataParser
  9. {
  10. protected const string DATA_VERSION_2_3 = "2.3";
  11. protected const string DATA_VERSION_3_0 = "3.0";
  12. protected const string DATA_VERSION_4_0 = "4.0";
  13. protected const string DATA_VERSION_4_5 = "4.5";
  14. protected const string DATA_VERSION_5_0 = "5.0";
  15. protected const string DATA_VERSION = DATA_VERSION_5_0;
  16. protected static readonly List<string> DATA_VERSIONS = new List<string>()
  17. {
  18. DATA_VERSION_5_0,
  19. DATA_VERSION_4_5,
  20. DATA_VERSION_4_0,
  21. DATA_VERSION_3_0,
  22. DATA_VERSION_2_3
  23. };
  24. protected const string TEXTURE_ATLAS = "TextureAtlas";
  25. protected const string SUB_TEXTURE = "SubTexture";
  26. protected const string FORMAT = "format";
  27. protected const string IMAGE_PATH = "imagePath";
  28. protected const string WIDTH = "width";
  29. protected const string HEIGHT = "height";
  30. protected const string ROTATED = "rotated";
  31. protected const string FRAME_X = "frameX";
  32. protected const string FRAME_Y = "frameY";
  33. protected const string FRAME_WIDTH = "frameWidth";
  34. protected const string FRAME_HEIGHT = "frameHeight";
  35. protected const string DRADON_BONES = "dragonBones";
  36. protected const string ARMATURE = "armature";
  37. protected const string BONE = "bone";
  38. protected const string IK = "ik";
  39. protected const string SLOT = "slot";
  40. protected const string SKIN = "skin";
  41. protected const string DISPLAY = "display";
  42. protected const string ANIMATION = "animation";
  43. protected const string Z_ORDER = "zOrder";
  44. protected const string FFD = "ffd";
  45. protected const string FRAME = "frame";
  46. protected const string ACTIONS = "actions";
  47. protected const string EVENTS = "events";
  48. protected const string INTS = "ints";
  49. protected const string FLOATS = "floats";
  50. protected const string STRINGS = "strings";
  51. protected const string PIVOT = "pivot";
  52. protected const string TRANSFORM = "transform";
  53. protected const string AABB = "aabb";
  54. protected const string COLOR = "color";
  55. protected const string FILTER = "filter";
  56. protected const string VERSION = "version";
  57. protected const string COMPATIBLE_VERSION = "compatibleVersion";
  58. protected const string FRAME_RATE = "frameRate";
  59. protected const string TYPE = "type";
  60. protected const string SUB_TYPE = "subType";
  61. protected const string NAME = "name";
  62. protected const string PARENT = "parent";
  63. protected const string SHARE = "share";
  64. protected const string PATH = "path";
  65. protected const string LENGTH = "length";
  66. protected const string DISPLAY_INDEX = "displayIndex";
  67. protected const string BLEND_MODE = "blendMode";
  68. protected const string INHERIT_TRANSLATION = "inheritTranslation";
  69. protected const string INHERIT_ROTATION = "inheritRotation";
  70. protected const string INHERIT_SCALE = "inheritScale";
  71. protected const string INHERIT_ANIMATION = "inheritAnimation";
  72. protected const string INHERIT_FFD = "inheritFFD";
  73. protected const string TARGET = "target";
  74. protected const string BEND_POSITIVE = "bendPositive";
  75. protected const string CHAIN = "chain";
  76. protected const string WEIGHT = "weight";
  77. protected const string FADE_IN_TIME = "fadeInTime";
  78. protected const string PLAY_TIMES = "playTimes";
  79. protected const string SCALE = "scale";
  80. protected const string OFFSET = "offset";
  81. protected const string POSITION = "position";
  82. protected const string DURATION = "duration";
  83. protected const string TWEEN_EASING = "tweenEasing";
  84. protected const string TWEEN_ROTATE = "tweenRotate";
  85. protected const string TWEEN_SCALE = "tweenScale";
  86. protected const string CURVE = "curve";
  87. protected const string EVENT = "event";
  88. protected const string SOUND = "sound";
  89. protected const string ACTION = "action";
  90. protected const string DEFAULT_ACTIONS = "defaultActions";
  91. protected const string X = "x";
  92. protected const string Y = "y";
  93. protected const string SKEW_X = "skX";
  94. protected const string SKEW_Y = "skY";
  95. protected const string SCALE_X = "scX";
  96. protected const string SCALE_Y = "scY";
  97. protected const string ALPHA_OFFSET = "aO";
  98. protected const string RED_OFFSET = "rO";
  99. protected const string GREEN_OFFSET = "gO";
  100. protected const string BLUE_OFFSET = "bO";
  101. protected const string ALPHA_MULTIPLIER = "aM";
  102. protected const string RED_MULTIPLIER = "rM";
  103. protected const string GREEN_MULTIPLIER = "gM";
  104. protected const string BLUE_MULTIPLIER = "bM";
  105. protected const string UVS = "uvs";
  106. protected const string VERTICES = "vertices";
  107. protected const string TRIANGLES = "triangles";
  108. protected const string WEIGHTS = "weights";
  109. protected const string SLOT_POSE = "slotPose";
  110. protected const string BONE_POSE = "bonePose";
  111. protected const string TWEEN = "tween";
  112. protected const string KEY = "key";
  113. protected const string COLOR_TRANSFORM = "colorTransform";
  114. protected const string TIMELINE = "timeline";
  115. protected const string IS_GLOBAL = "isGlobal";
  116. protected const string PIVOT_X = "pX";
  117. protected const string PIVOT_Y = "pY";
  118. protected const string Z = "z";
  119. protected const string LOOP = "loop";
  120. protected const string AUTO_TWEEN = "autoTween";
  121. protected const string HIDE = "hide";
  122. protected const string DEFAULT_NAME = "__default";
  123. protected static ArmatureType _getArmatureType(string value)
  124. {
  125. switch (value.ToLower())
  126. {
  127. case "stage":
  128. return ArmatureType.Armature;
  129. case "armature":
  130. return ArmatureType.Armature;
  131. case "movieclip":
  132. return ArmatureType.MovieClip;
  133. default:
  134. return ArmatureType.None;
  135. }
  136. }
  137. protected static DisplayType _getDisplayType(string value)
  138. {
  139. switch (value.ToLower())
  140. {
  141. case "image":
  142. return DisplayType.Image;
  143. case "armature":
  144. return DisplayType.Armature;
  145. case "mesh":
  146. return DisplayType.Mesh;
  147. case "boundingbox":
  148. return DisplayType.BoundingBox;
  149. default:
  150. return DisplayType.None;
  151. }
  152. }
  153. protected static BoundingBoxType _getBoundingBoxType(string value)
  154. {
  155. switch (value.ToLower())
  156. {
  157. case "rectangle":
  158. return BoundingBoxType.Rectangle;
  159. case "ellipse":
  160. return BoundingBoxType.Ellipse;
  161. case "polygon":
  162. return BoundingBoxType.Polygon;
  163. default:
  164. return BoundingBoxType.None;
  165. }
  166. }
  167. protected static BlendMode _getBlendMode(string value)
  168. {
  169. switch (value.ToLower())
  170. {
  171. case "normal":
  172. return BlendMode.Normal;
  173. case "add":
  174. return BlendMode.Add;
  175. case "alpha":
  176. return BlendMode.Alpha;
  177. case "darken":
  178. return BlendMode.Darken;
  179. case "difference":
  180. return BlendMode.Difference;
  181. case "erase":
  182. return BlendMode.Erase;
  183. case "hardlight":
  184. return BlendMode.HardLight;
  185. case "invert":
  186. return BlendMode.Invert;
  187. case "layer":
  188. return BlendMode.Layer;
  189. case "lighten":
  190. return BlendMode.Lighten;
  191. case "multiply":
  192. return BlendMode.Multiply;
  193. case "overlay":
  194. return BlendMode.Overlay;
  195. case "screen":
  196. return BlendMode.Screen;
  197. case "subtract":
  198. return BlendMode.Subtract;
  199. default:
  200. return BlendMode.None;
  201. }
  202. }
  203. protected static ActionType _getActionType(string value)
  204. {
  205. switch (value.ToLower())
  206. {
  207. case "play":
  208. case "gotoandplay":
  209. return ActionType.Play;
  210. default:
  211. return ActionType.None;
  212. }
  213. }
  214. protected bool _isOldData = false; // For 2.x ~ 3.x
  215. protected bool _isGlobalTransform = false; // For 2.x ~ 3.x
  216. protected bool _isAutoTween = false; // For 2.x ~ 3.x
  217. protected float _animationTweenEasing = 0.0f; // For 2.x ~ 3.x
  218. protected readonly Point _timelinePivot = new Point(); // For 2.x ~ 3.x
  219. protected readonly Point _helpPoint = new Point();
  220. protected readonly Transform _helpTransformA = new Transform();
  221. protected readonly Transform _helpTransformB = new Transform();
  222. protected readonly Matrix _helpMatrix = new Matrix();
  223. protected readonly List<BoneData> _rawBones = new List<BoneData>(); // For skinned mesh
  224. protected DragonBonesData _data = null;
  225. protected ArmatureData _armature = null;
  226. protected SkinData _skin = null;
  227. protected SkinSlotData _skinSlotData = null;
  228. protected AnimationData _animation = null;
  229. protected object _timeline = null;
  230. public DataParser() { }
  231. /**
  232. * @private
  233. */
  234. public abstract DragonBonesData ParseDragonBonesData(Dictionary<string, object> rawData, float scale = 1.0f);
  235. /**
  236. * @private
  237. */
  238. public abstract void ParseTextureAtlasData(Dictionary<string, object> rawData, TextureAtlasData textureAtlasData, float scale = 0.0f);
  239. private void _getTimelineFrameMatrix(AnimationData animation, BoneTimelineData timeline, float position, Transform transform) // Support 2.x ~ 3.x data.
  240. {
  241. var frameIndex = (int)Math.Floor(position * animation.frameCount / animation.duration);
  242. if (timeline.frames.Count == 1 || frameIndex >= timeline.frames.Count)
  243. {
  244. transform.CopyFrom(timeline.frames[0].transform);
  245. }
  246. else
  247. {
  248. var frame = timeline.frames[frameIndex];
  249. float tweenProgress = 0.0f;
  250. if (frame.tweenEasing != DragonBones.NO_TWEEN)
  251. {
  252. tweenProgress = (position - frame.position) / frame.duration;
  253. if (frame.tweenEasing != 0.0f)
  254. {
  255. tweenProgress = TweenTimelineState<BoneFrameData, BoneTimelineData>._getEasingValue(tweenProgress, frame.tweenEasing);
  256. }
  257. }
  258. else if (frame.curve != null)
  259. {
  260. tweenProgress = (position - frame.position) / frame.duration;
  261. tweenProgress = TweenTimelineState<BoneFrameData, BoneTimelineData>._getCurveEasingValue(tweenProgress, frame.curve);
  262. }
  263. var nextFrame = frame.next;
  264. transform.x = nextFrame.transform.x - frame.transform.x;
  265. transform.y = nextFrame.transform.y - frame.transform.y;
  266. transform.skewX = Transform.NormalizeRadian(nextFrame.transform.skewX - frame.transform.skewX);
  267. transform.skewY = Transform.NormalizeRadian(nextFrame.transform.skewY - frame.transform.skewY);
  268. transform.scaleX = nextFrame.transform.scaleX - frame.transform.scaleX;
  269. transform.scaleY = nextFrame.transform.scaleY - frame.transform.scaleY;
  270. transform.x = frame.transform.x + transform.x * tweenProgress;
  271. transform.y = frame.transform.y + transform.y * tweenProgress;
  272. transform.skewX = frame.transform.skewX + transform.skewX * tweenProgress;
  273. transform.skewY = frame.transform.skewY + transform.skewY * tweenProgress;
  274. transform.scaleX = frame.transform.scaleX + transform.scaleX * tweenProgress;
  275. transform.scaleY = frame.transform.scaleY + transform.scaleY * tweenProgress;
  276. }
  277. transform.Add(timeline.originTransform);
  278. }
  279. protected void _globalToLocal(ArmatureData armature) // Support 2.x ~ 3.x data.
  280. {
  281. var keyFrames = new List<BoneFrameData>();
  282. var bones = armature.sortedBones.ToArray();
  283. Array.Reverse(bones);
  284. foreach (var bone in bones)
  285. {
  286. if (bone.parent != null)
  287. {
  288. bone.parent.transform.ToMatrix(_helpMatrix);
  289. _helpMatrix.Invert();
  290. _helpMatrix.TransformPoint(bone.transform.x, bone.transform.y, _helpPoint);
  291. bone.transform.x = _helpPoint.x;
  292. bone.transform.y = _helpPoint.y;
  293. bone.transform.rotation -= bone.parent.transform.rotation;
  294. }
  295. foreach (var pair in armature.animations)
  296. {
  297. var animation = pair.Value;
  298. var timeline = animation.GetBoneTimeline(bone.name);
  299. if (timeline == null)
  300. {
  301. continue;
  302. }
  303. var parentTimeline = bone.parent != null ? animation.GetBoneTimeline(bone.parent.name) : null;
  304. _helpTransformB.CopyFrom(timeline.originTransform);
  305. keyFrames.Clear();
  306. var isFirstFrame = true;
  307. foreach (var frame in timeline.frames)
  308. {
  309. if (keyFrames.Contains(frame))
  310. {
  311. continue;
  312. }
  313. keyFrames.Add(frame);
  314. if (parentTimeline != null)
  315. {
  316. _getTimelineFrameMatrix(animation, parentTimeline, frame.position, _helpTransformA);
  317. frame.transform.Add(_helpTransformB);
  318. _helpTransformA.ToMatrix(_helpMatrix);
  319. _helpMatrix.Invert();
  320. _helpMatrix.TransformPoint(frame.transform.x, frame.transform.y, _helpPoint);
  321. frame.transform.x = _helpPoint.x;
  322. frame.transform.y = _helpPoint.y;
  323. frame.transform.rotation -= _helpTransformA.rotation;
  324. }
  325. else
  326. {
  327. frame.transform.Add(_helpTransformB);
  328. }
  329. frame.transform.Minus(bone.transform);
  330. if (isFirstFrame)
  331. {
  332. isFirstFrame = false;
  333. timeline.originTransform.CopyFrom(frame.transform);
  334. frame.transform.Identity();
  335. }
  336. else
  337. {
  338. frame.transform.Minus(timeline.originTransform);
  339. }
  340. }
  341. }
  342. }
  343. }
  344. protected void _mergeFrameToAnimationTimeline(float framePostion, List<ActionData> actions, List<EventData> events)
  345. {
  346. var frameStart = (int)Math.Floor(framePostion * _armature.frameRate); // uint()
  347. var frames = _animation.frames;
  348. if (frames.Count == 0)
  349. {
  350. var startFrame = BaseObject.BorrowObject<AnimationFrameData>(); // Add start frame.
  351. startFrame.position = 0.0f;
  352. if (_animation.frameCount > 1) {
  353. DragonBones.ResizeList (frames, (int)_animation.frameCount + 1, null); // One more count for zero duration frame.
  354. var endFrame = BaseObject.BorrowObject<AnimationFrameData> (); // Add end frame to keep animation timeline has two different frames atleast.
  355. endFrame.position = _animation.frameCount / _armature.frameRate;
  356. frames[0] = startFrame;
  357. frames[(int)_animation.frameCount] = endFrame;
  358. }
  359. else // TODO
  360. {
  361. DragonBones.ResizeList(frames, 1, null);
  362. frames[0] = startFrame;
  363. }
  364. }
  365. AnimationFrameData insertedFrame = null;
  366. var replacedFrame = frames[frameStart];
  367. if (replacedFrame != null && (frameStart == 0 || frames[frameStart - 1] == replacedFrame.prev)) // Key frame.
  368. {
  369. insertedFrame = replacedFrame;
  370. }
  371. else
  372. {
  373. insertedFrame = BaseObject.BorrowObject<AnimationFrameData>(); // Create frame.
  374. insertedFrame.position = frameStart / _armature.frameRate;
  375. frames[frameStart] = insertedFrame;
  376. for (int i = frameStart + 1, l = frames.Count; i < l; ++i) // Clear replaced frame.
  377. {
  378. var frame = frames[i];
  379. if (replacedFrame != null && frame == replacedFrame)
  380. {
  381. frames[i] = null;
  382. }
  383. }
  384. }
  385. if (actions != null) // Merge actions.
  386. {
  387. foreach (var action in actions)
  388. {
  389. insertedFrame.actions.Add(action);
  390. }
  391. }
  392. if (events != null) // Merge events.
  393. {
  394. foreach (var evt in events)
  395. {
  396. insertedFrame.events.Add(evt);
  397. }
  398. }
  399. // Modify frame link and duration.
  400. AnimationFrameData prevFrame = null;
  401. AnimationFrameData nextFrame = null;
  402. for (int i = 0, l = frames.Count; i < l; ++i)
  403. {
  404. var currentFrame = frames[i];
  405. if (currentFrame != null && nextFrame != currentFrame)
  406. {
  407. nextFrame = currentFrame;
  408. if (prevFrame != null)
  409. {
  410. nextFrame.prev = prevFrame;
  411. prevFrame.next = nextFrame;
  412. prevFrame.duration = nextFrame.position - prevFrame.position;
  413. }
  414. prevFrame = nextFrame;
  415. }
  416. else
  417. {
  418. frames[i] = prevFrame;
  419. }
  420. }
  421. nextFrame.duration = _animation.duration - nextFrame.position;
  422. nextFrame = frames[0];
  423. prevFrame.next = nextFrame;
  424. nextFrame.prev = prevFrame;
  425. }
  426. }
  427. }