SkeletonJson.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifdef SPINE_UE4
  30. #include "SpinePluginPrivatePCH.h"
  31. #endif
  32. #include <spine/SkeletonJson.h>
  33. #include <spine/CurveTimeline.h>
  34. #include <spine/VertexAttachment.h>
  35. #include <spine/Json.h>
  36. #include <spine/SkeletonData.h>
  37. #include <spine/Atlas.h>
  38. #include <spine/AtlasAttachmentLoader.h>
  39. #include <spine/LinkedMesh.h>
  40. #include <spine/Skin.h>
  41. #include <spine/ContainerUtil.h>
  42. #include <spine/BoneData.h>
  43. #include <spine/SlotData.h>
  44. #include <spine/IkConstraintData.h>
  45. #include <spine/TransformConstraintData.h>
  46. #include <spine/PathConstraintData.h>
  47. #include <spine/AttachmentType.h>
  48. #include <spine/RegionAttachment.h>
  49. #include <spine/BoundingBoxAttachment.h>
  50. #include <spine/MeshAttachment.h>
  51. #include <spine/PathAttachment.h>
  52. #include <spine/PointAttachment.h>
  53. #include <spine/ClippingAttachment.h>
  54. #include <spine/EventData.h>
  55. #include <spine/AttachmentTimeline.h>
  56. #include <spine/ColorTimeline.h>
  57. #include <spine/TwoColorTimeline.h>
  58. #include <spine/RotateTimeline.h>
  59. #include <spine/TranslateTimeline.h>
  60. #include <spine/ScaleTimeline.h>
  61. #include <spine/ShearTimeline.h>
  62. #include <spine/IkConstraintTimeline.h>
  63. #include <spine/TransformConstraintTimeline.h>
  64. #include <spine/PathConstraintPositionTimeline.h>
  65. #include <spine/PathConstraintSpacingTimeline.h>
  66. #include <spine/PathConstraintMixTimeline.h>
  67. #include <spine/DeformTimeline.h>
  68. #include <spine/DrawOrderTimeline.h>
  69. #include <spine/EventTimeline.h>
  70. #include <spine/Event.h>
  71. #include <spine/Vertices.h>
  72. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
  73. #define strdup _strdup
  74. #endif
  75. using namespace spine;
  76. SkeletonJson::SkeletonJson(Atlas *atlas) : _attachmentLoader(new(__FILE__, __LINE__) AtlasAttachmentLoader(atlas)),
  77. _scale(1), _ownsLoader(true)
  78. {}
  79. SkeletonJson::SkeletonJson(AttachmentLoader *attachmentLoader) : _attachmentLoader(attachmentLoader), _scale(1),
  80. _ownsLoader(false)
  81. {
  82. assert(_attachmentLoader != NULL);
  83. }
  84. SkeletonJson::~SkeletonJson() {
  85. ContainerUtil::cleanUpVectorOfPointers(_linkedMeshes);
  86. if (_ownsLoader) delete _attachmentLoader;
  87. }
  88. SkeletonData *SkeletonJson::readSkeletonDataFile(const String &path) {
  89. int length;
  90. SkeletonData *skeletonData;
  91. const char *json = SpineExtension::readFile(path, &length);
  92. if (length == 0 || !json) {
  93. setError(NULL, "Unable to read skeleton file: ", path);
  94. return NULL;
  95. }
  96. skeletonData = readSkeletonData(json);
  97. SpineExtension::free(json, __FILE__, __LINE__);
  98. return skeletonData;
  99. }
  100. SkeletonData *SkeletonJson::readSkeletonData(const char *json) {
  101. int i, ii;
  102. SkeletonData *skeletonData;
  103. Json *root, *skeleton, *bones, *boneMap, *ik, *transform, *path, *slots, *skins, *animations, *events;
  104. _error = "";
  105. _linkedMeshes.clear();
  106. root = new(__FILE__, __LINE__) Json(json);
  107. if (!root) {
  108. setError(NULL, "Invalid skeleton JSON: ", Json::getError());
  109. return NULL;
  110. }
  111. skeletonData = new(__FILE__, __LINE__) SkeletonData();
  112. skeleton = Json::getItem(root, "skeleton");
  113. if (skeleton) {
  114. skeletonData->_hash = Json::getString(skeleton, "hash", 0);
  115. skeletonData->_version = Json::getString(skeleton, "spine", 0);
  116. if ("3.8.75" == skeletonData->_version) {
  117. delete skeletonData;
  118. setError(root, "Unsupported skeleton data, please export with a newer version of Spine.", "");
  119. return NULL;
  120. }
  121. skeletonData->_x = Json::getFloat(skeleton, "x", 0);
  122. skeletonData->_y = Json::getFloat(skeleton, "y", 0);
  123. skeletonData->_width = Json::getFloat(skeleton, "width", 0);
  124. skeletonData->_height = Json::getFloat(skeleton, "height", 0);
  125. skeletonData->_fps = Json::getFloat(skeleton, "fps", 30);
  126. skeletonData->_audioPath = Json::getString(skeleton, "audio", 0);
  127. skeletonData->_imagesPath = Json::getString(skeleton, "images", 0);
  128. }
  129. /* Bones. */
  130. bones = Json::getItem(root, "bones");
  131. skeletonData->_bones.setSize(bones->_size, 0);
  132. int bonesCount = 0;
  133. for (boneMap = bones->_child, i = 0; boneMap; boneMap = boneMap->_next, ++i) {
  134. BoneData *data;
  135. const char *transformMode;
  136. BoneData *parent = 0;
  137. const char *parentName = Json::getString(boneMap, "parent", 0);
  138. if (parentName) {
  139. parent = skeletonData->findBone(parentName);
  140. if (!parent) {
  141. delete skeletonData;
  142. setError(root, "Parent bone not found: ", parentName);
  143. return NULL;
  144. }
  145. }
  146. data = new(__FILE__, __LINE__) BoneData(bonesCount, Json::getString(boneMap, "name", 0), parent);
  147. data->_length = Json::getFloat(boneMap, "length", 0) * _scale;
  148. data->_x = Json::getFloat(boneMap, "x", 0) * _scale;
  149. data->_y = Json::getFloat(boneMap, "y", 0) * _scale;
  150. data->_rotation = Json::getFloat(boneMap, "rotation", 0);
  151. data->_scaleX = Json::getFloat(boneMap, "scaleX", 1);
  152. data->_scaleY = Json::getFloat(boneMap, "scaleY", 1);
  153. data->_shearX = Json::getFloat(boneMap, "shearX", 0);
  154. data->_shearY = Json::getFloat(boneMap, "shearY", 0);
  155. transformMode = Json::getString(boneMap, "transform", "normal");
  156. data->_transformMode = TransformMode_Normal;
  157. if (strcmp(transformMode, "normal") == 0) data->_transformMode = TransformMode_Normal;
  158. else if (strcmp(transformMode, "onlyTranslation") == 0) data->_transformMode = TransformMode_OnlyTranslation;
  159. else if (strcmp(transformMode, "noRotationOrReflection") == 0) data->_transformMode = TransformMode_NoRotationOrReflection;
  160. else if (strcmp(transformMode, "noScale") == 0) data->_transformMode = TransformMode_NoScale;
  161. else if (strcmp(transformMode, "noScaleOrReflection") == 0) data->_transformMode = TransformMode_NoScaleOrReflection;
  162. data->_skinRequired = Json::getBoolean(boneMap, "skin", false);
  163. skeletonData->_bones[i] = data;
  164. bonesCount++;
  165. }
  166. /* Slots. */
  167. slots = Json::getItem(root, "slots");
  168. if (slots) {
  169. Json *slotMap;
  170. skeletonData->_slots.ensureCapacity(slots->_size);
  171. skeletonData->_slots.setSize(slots->_size, 0);
  172. for (slotMap = slots->_child, i = 0; slotMap; slotMap = slotMap->_next, ++i) {
  173. SlotData *data;
  174. const char *color;
  175. const char *dark;
  176. Json *item;
  177. const char *boneName = Json::getString(slotMap, "bone", 0);
  178. BoneData *boneData = skeletonData->findBone(boneName);
  179. if (!boneData) {
  180. delete skeletonData;
  181. setError(root, "Slot bone not found: ", boneName);
  182. return NULL;
  183. }
  184. data = new(__FILE__, __LINE__) SlotData(i, Json::getString(slotMap, "name", 0), *boneData);
  185. color = Json::getString(slotMap, "color", 0);
  186. if (color) {
  187. Color &c = data->getColor();
  188. c.r = toColor(color, 0);
  189. c.g = toColor(color, 1);
  190. c.b = toColor(color, 2);
  191. c.a = toColor(color, 3);
  192. }
  193. dark = Json::getString(slotMap, "dark", 0);
  194. if (dark) {
  195. Color &darkColor = data->getDarkColor();
  196. darkColor.r = toColor(dark, 0);
  197. darkColor.g = toColor(dark, 1);
  198. darkColor.b = toColor(dark, 2);
  199. darkColor.a = 1;
  200. data->setHasDarkColor(true);
  201. }
  202. item = Json::getItem(slotMap, "attachment");
  203. if (item) data->setAttachmentName(item->_valueString);
  204. item = Json::getItem(slotMap, "blend");
  205. if (item) {
  206. if (strcmp(item->_valueString, "additive") == 0) data->_blendMode = BlendMode_Additive;
  207. else if (strcmp(item->_valueString, "multiply") == 0) data->_blendMode = BlendMode_Multiply;
  208. else if (strcmp(item->_valueString, "screen") == 0) data->_blendMode = BlendMode_Screen;
  209. }
  210. skeletonData->_slots[i] = data;
  211. }
  212. }
  213. /* IK constraints. */
  214. ik = Json::getItem(root, "ik");
  215. if (ik) {
  216. Json *constraintMap;
  217. skeletonData->_ikConstraints.ensureCapacity(ik->_size);
  218. skeletonData->_ikConstraints.setSize(ik->_size, 0);
  219. for (constraintMap = ik->_child, i = 0; constraintMap; constraintMap = constraintMap->_next, ++i) {
  220. const char *targetName;
  221. IkConstraintData *data = new(__FILE__, __LINE__) IkConstraintData(Json::getString(constraintMap, "name", 0));
  222. data->setOrder(Json::getInt(constraintMap, "order", 0));
  223. data->setSkinRequired(Json::getBoolean(constraintMap, "skin", false));
  224. boneMap = Json::getItem(constraintMap, "bones");
  225. data->_bones.ensureCapacity(boneMap->_size);
  226. data->_bones.setSize(boneMap->_size, 0);
  227. for (boneMap = boneMap->_child, ii = 0; boneMap; boneMap = boneMap->_next, ++ii) {
  228. data->_bones[ii] = skeletonData->findBone(boneMap->_valueString);
  229. if (!data->_bones[ii]) {
  230. delete skeletonData;
  231. setError(root, "IK bone not found: ", boneMap->_valueString);
  232. return NULL;
  233. }
  234. }
  235. targetName = Json::getString(constraintMap, "target", 0);
  236. data->_target = skeletonData->findBone(targetName);
  237. if (!data->_target) {
  238. delete skeletonData;
  239. setError(root, "Target bone not found: ", targetName);
  240. return NULL;
  241. }
  242. data->_mix = Json::getFloat(constraintMap, "mix", 1);
  243. data->_softness = Json::getFloat(constraintMap, "softness", 0) * _scale;
  244. data->_bendDirection = Json::getInt(constraintMap, "bendPositive", 1) ? 1 : -1;
  245. data->_compress = Json::getInt(constraintMap, "compress", 0) ? true: false;
  246. data->_stretch = Json::getInt(constraintMap, "stretch", 0) ? true: false;
  247. data->_uniform = Json::getInt(constraintMap, "uniform", 0) ? true: false;
  248. skeletonData->_ikConstraints[i] = data;
  249. }
  250. }
  251. /* Transform constraints. */
  252. transform = Json::getItem(root, "transform");
  253. if (transform) {
  254. Json *constraintMap;
  255. skeletonData->_transformConstraints.ensureCapacity(transform->_size);
  256. skeletonData->_transformConstraints.setSize(transform->_size, 0);
  257. for (constraintMap = transform->_child, i = 0; constraintMap; constraintMap = constraintMap->_next, ++i) {
  258. const char *name;
  259. TransformConstraintData *data = new(__FILE__, __LINE__) TransformConstraintData(Json::getString(constraintMap, "name", 0));
  260. data->setOrder(Json::getInt(constraintMap, "order", 0));
  261. data->setSkinRequired(Json::getBoolean(constraintMap, "skin", false));
  262. boneMap = Json::getItem(constraintMap, "bones");
  263. data->_bones.ensureCapacity(boneMap->_size);
  264. data->_bones.setSize(boneMap->_size, 0);
  265. for (boneMap = boneMap->_child, ii = 0; boneMap; boneMap = boneMap->_next, ++ii) {
  266. data->_bones[ii] = skeletonData->findBone(boneMap->_valueString);
  267. if (!data->_bones[ii]) {
  268. delete skeletonData;
  269. setError(root, "Transform bone not found: ", boneMap->_valueString);
  270. return NULL;
  271. }
  272. }
  273. name = Json::getString(constraintMap, "target", 0);
  274. data->_target = skeletonData->findBone(name);
  275. if (!data->_target) {
  276. delete skeletonData;
  277. setError(root, "Target bone not found: ", name);
  278. return NULL;
  279. }
  280. data->_local = Json::getInt(constraintMap, "local", 0) ? true : false;
  281. data->_relative = Json::getInt(constraintMap, "relative", 0) ? true : false;
  282. data->_offsetRotation = Json::getFloat(constraintMap, "rotation", 0);
  283. data->_offsetX = Json::getFloat(constraintMap, "x", 0) * _scale;
  284. data->_offsetY = Json::getFloat(constraintMap, "y", 0) * _scale;
  285. data->_offsetScaleX = Json::getFloat(constraintMap, "scaleX", 0);
  286. data->_offsetScaleY = Json::getFloat(constraintMap, "scaleY", 0);
  287. data->_offsetShearY = Json::getFloat(constraintMap, "shearY", 0);
  288. data->_rotateMix = Json::getFloat(constraintMap, "rotateMix", 1);
  289. data->_translateMix = Json::getFloat(constraintMap, "translateMix", 1);
  290. data->_scaleMix = Json::getFloat(constraintMap, "scaleMix", 1);
  291. data->_shearMix = Json::getFloat(constraintMap, "shearMix", 1);
  292. skeletonData->_transformConstraints[i] = data;
  293. }
  294. }
  295. /* Path constraints */
  296. path = Json::getItem(root, "path");
  297. if (path) {
  298. Json *constraintMap;
  299. skeletonData->_pathConstraints.ensureCapacity(path->_size);
  300. skeletonData->_pathConstraints.setSize(path->_size, 0);
  301. for (constraintMap = path->_child, i = 0; constraintMap; constraintMap = constraintMap->_next, ++i) {
  302. const char *name;
  303. const char *item;
  304. PathConstraintData *data = new(__FILE__, __LINE__) PathConstraintData(Json::getString(constraintMap, "name", 0));
  305. data->setOrder(Json::getInt(constraintMap, "order", 0));
  306. data->setSkinRequired(Json::getBoolean(constraintMap, "skin", false));
  307. boneMap = Json::getItem(constraintMap, "bones");
  308. data->_bones.ensureCapacity(boneMap->_size);
  309. data->_bones.setSize(boneMap->_size, 0);
  310. for (boneMap = boneMap->_child, ii = 0; boneMap; boneMap = boneMap->_next, ++ii) {
  311. data->_bones[ii] = skeletonData->findBone(boneMap->_valueString);
  312. if (!data->_bones[ii]) {
  313. delete skeletonData;
  314. setError(root, "Path bone not found: ", boneMap->_valueString);
  315. return NULL;
  316. }
  317. }
  318. name = Json::getString(constraintMap, "target", 0);
  319. data->_target = skeletonData->findSlot(name);
  320. if (!data->_target) {
  321. delete skeletonData;
  322. setError(root, "Target slot not found: ", name);
  323. return NULL;
  324. }
  325. item = Json::getString(constraintMap, "positionMode", "percent");
  326. if (strcmp(item, "fixed") == 0) {
  327. data->_positionMode = PositionMode_Fixed;
  328. } else if (strcmp(item, "percent") == 0) {
  329. data->_positionMode = PositionMode_Percent;
  330. }
  331. item = Json::getString(constraintMap, "spacingMode", "length");
  332. if (strcmp(item, "length") == 0) data->_spacingMode = SpacingMode_Length;
  333. else if (strcmp(item, "fixed") == 0) data->_spacingMode = SpacingMode_Fixed;
  334. else if (strcmp(item, "percent") == 0) data->_spacingMode = SpacingMode_Percent;
  335. item = Json::getString(constraintMap, "rotateMode", "tangent");
  336. if (strcmp(item, "tangent") == 0) data->_rotateMode = RotateMode_Tangent;
  337. else if (strcmp(item, "chain") == 0) data->_rotateMode = RotateMode_Chain;
  338. else if (strcmp(item, "chainScale") == 0) data->_rotateMode = RotateMode_ChainScale;
  339. data->_offsetRotation = Json::getFloat(constraintMap, "rotation", 0);
  340. data->_position = Json::getFloat(constraintMap, "position", 0);
  341. if (data->_positionMode == PositionMode_Fixed) data->_position *= _scale;
  342. data->_spacing = Json::getFloat(constraintMap, "spacing", 0);
  343. if (data->_spacingMode == SpacingMode_Length || data->_spacingMode == SpacingMode_Fixed) data->_spacing *= _scale;
  344. data->_rotateMix = Json::getFloat(constraintMap, "rotateMix", 1);
  345. data->_translateMix = Json::getFloat(constraintMap, "translateMix", 1);
  346. skeletonData->_pathConstraints[i] = data;
  347. }
  348. }
  349. /* Skins. */
  350. skins = Json::getItem(root, "skins");
  351. if (skins) {
  352. Json *skinMap;
  353. skeletonData->_skins.ensureCapacity(skins->_size);
  354. skeletonData->_skins.setSize(skins->_size, 0);
  355. int skinsIndex = 0;
  356. for (skinMap = skins->_child, i = 0; skinMap; skinMap = skinMap->_next, ++i) {
  357. Json *attachmentsMap;
  358. Json *curves;
  359. Skin *skin = new(__FILE__, __LINE__) Skin(Json::getString(skinMap, "name", ""));
  360. Json *item = Json::getItem(skinMap, "bones");
  361. if (item) {
  362. for (item = item->_child; item; item = item->_next) {
  363. BoneData* data = skeletonData->findBone(item->_valueString);
  364. if (!data) {
  365. delete skeletonData;
  366. setError(root, String("Skin bone not found: "), item->_valueString);
  367. return NULL;
  368. }
  369. skin->getBones().add(data);
  370. }
  371. }
  372. item = Json::getItem(skinMap, "ik");
  373. if (item) {
  374. for (item = item->_child; item; item = item->_next) {
  375. IkConstraintData* data = skeletonData->findIkConstraint(item->_valueString);
  376. if (!data) {
  377. delete skeletonData;
  378. setError(root, String("Skin IK constraint not found: "), item->_valueString);
  379. return NULL;
  380. }
  381. skin->getConstraints().add(data);
  382. }
  383. }
  384. item = Json::getItem(skinMap, "transform");
  385. if (item) {
  386. for (item = item->_child; item; item = item->_next) {
  387. TransformConstraintData* data = skeletonData->findTransformConstraint(item->_valueString);
  388. if (!data) {
  389. delete skeletonData;
  390. setError(root, String("Skin transform constraint not found: "), item->_valueString);
  391. return NULL;
  392. }
  393. skin->getConstraints().add(data);
  394. }
  395. }
  396. item = Json::getItem(skinMap, "path");
  397. if (item) {
  398. for (item = item->_child; item; item = item->_next) {
  399. PathConstraintData* data = skeletonData->findPathConstraint(item->_valueString);
  400. if (!data) {
  401. delete skeletonData;
  402. setError(root, String("Skin path constraint not found: "), item->_valueString);
  403. return NULL;
  404. }
  405. skin->getConstraints().add(data);
  406. }
  407. }
  408. skeletonData->_skins[skinsIndex++] = skin;
  409. if (strcmp(Json::getString(skinMap, "name", ""), "default") == 0) {
  410. skeletonData->_defaultSkin = skin;
  411. }
  412. for (attachmentsMap = Json::getItem(skinMap, "attachments")->_child; attachmentsMap; attachmentsMap = attachmentsMap->_next) {
  413. SlotData* slot = skeletonData->findSlot(attachmentsMap->_name);
  414. Json *attachmentMap;
  415. for (attachmentMap = attachmentsMap->_child; attachmentMap; attachmentMap = attachmentMap->_next) {
  416. Attachment *attachment = NULL;
  417. const char *skinAttachmentName = attachmentMap->_name;
  418. const char *attachmentName = Json::getString(attachmentMap, "name", skinAttachmentName);
  419. const char *attachmentPath = Json::getString(attachmentMap, "path", attachmentName);
  420. const char *color;
  421. Json *entry;
  422. const char *typeString = Json::getString(attachmentMap, "type", "region");
  423. AttachmentType type;
  424. if (strcmp(typeString, "region") == 0) type = AttachmentType_Region;
  425. else if (strcmp(typeString, "mesh") == 0) type = AttachmentType_Mesh;
  426. else if (strcmp(typeString, "linkedmesh") == 0) type = AttachmentType_Linkedmesh;
  427. else if (strcmp(typeString, "boundingbox") == 0) type = AttachmentType_Boundingbox;
  428. else if (strcmp(typeString, "path") == 0) type = AttachmentType_Path;
  429. else if (strcmp(typeString, "clipping") == 0) type = AttachmentType_Clipping;
  430. else if (strcmp(typeString, "point") == 0) type = AttachmentType_Point;
  431. else {
  432. delete skeletonData;
  433. setError(root, "Unknown attachment type: ", typeString);
  434. return NULL;
  435. }
  436. switch (type) {
  437. case AttachmentType_Region: {
  438. attachment = _attachmentLoader->newRegionAttachment(*skin, attachmentName, attachmentPath);
  439. if (!attachment) {
  440. delete skeletonData;
  441. setError(root, "Error reading attachment: ", skinAttachmentName);
  442. return NULL;
  443. }
  444. RegionAttachment *region = static_cast<RegionAttachment *>(attachment);
  445. region->_path = attachmentPath;
  446. region->_x = Json::getFloat(attachmentMap, "x", 0) * _scale;
  447. region->_y = Json::getFloat(attachmentMap, "y", 0) * _scale;
  448. region->_scaleX = Json::getFloat(attachmentMap, "scaleX", 1);
  449. region->_scaleY = Json::getFloat(attachmentMap, "scaleY", 1);
  450. region->_rotation = Json::getFloat(attachmentMap, "rotation", 0);
  451. region->_width = Json::getFloat(attachmentMap, "width", 32) * _scale;
  452. region->_height = Json::getFloat(attachmentMap, "height", 32) * _scale;
  453. color = Json::getString(attachmentMap, "color", 0);
  454. if (color) {
  455. region->getColor().r = toColor(color, 0);
  456. region->getColor().g = toColor(color, 1);
  457. region->getColor().b = toColor(color, 2);
  458. region->getColor().a = toColor(color, 3);
  459. }
  460. region->updateOffset();
  461. _attachmentLoader->configureAttachment(region);
  462. break;
  463. }
  464. case AttachmentType_Mesh:
  465. case AttachmentType_Linkedmesh: {
  466. attachment = _attachmentLoader->newMeshAttachment(*skin, attachmentName, attachmentPath);
  467. if (!attachment) {
  468. delete skeletonData;
  469. setError(root, "Error reading attachment: ", skinAttachmentName);
  470. return NULL;
  471. }
  472. MeshAttachment *mesh = static_cast<MeshAttachment *>(attachment);
  473. mesh->_path = attachmentPath;
  474. color = Json::getString(attachmentMap, "color", 0);
  475. if (color) {
  476. mesh->getColor().r = toColor(color, 0);
  477. mesh->getColor().g = toColor(color, 1);
  478. mesh->getColor().b = toColor(color, 2);
  479. mesh->getColor().a = toColor(color, 3);
  480. }
  481. mesh->_width = Json::getFloat(attachmentMap, "width", 32) * _scale;
  482. mesh->_height = Json::getFloat(attachmentMap, "height", 32) * _scale;
  483. entry = Json::getItem(attachmentMap, "parent");
  484. if (!entry) {
  485. int verticesLength;
  486. entry = Json::getItem(attachmentMap, "triangles");
  487. mesh->_triangles.ensureCapacity(entry->_size);
  488. mesh->_triangles.setSize(entry->_size, 0);
  489. for (entry = entry->_child, ii = 0; entry; entry = entry->_next, ++ii)
  490. mesh->_triangles[ii] = (unsigned short) entry->_valueInt;
  491. entry = Json::getItem(attachmentMap, "uvs");
  492. verticesLength = entry->_size;
  493. mesh->_regionUVs.ensureCapacity(verticesLength);
  494. mesh->_regionUVs.setSize(verticesLength, 0);
  495. for (entry = entry->_child, ii = 0; entry; entry = entry->_next, ++ii)
  496. mesh->_regionUVs[ii] = entry->_valueFloat;
  497. readVertices(attachmentMap, mesh, verticesLength);
  498. mesh->updateUVs();
  499. mesh->_hullLength = Json::getInt(attachmentMap, "hull", 0);
  500. entry = Json::getItem(attachmentMap, "edges");
  501. if (entry) {
  502. mesh->_edges.ensureCapacity(entry->_size);
  503. mesh->_edges.setSize(entry->_size, 0);
  504. for (entry = entry->_child, ii = 0; entry; entry = entry->_next, ++ii)
  505. mesh->_edges[ii] = entry->_valueInt;
  506. }
  507. _attachmentLoader->configureAttachment(mesh);
  508. } else {
  509. bool inheritDeform = Json::getInt(attachmentMap, "deform", 1) ? true : false;
  510. LinkedMesh *linkedMesh = new(__FILE__, __LINE__) LinkedMesh(mesh,
  511. String(Json::getString(attachmentMap, "skin", 0)), slot->getIndex(), String(entry->_valueString),
  512. inheritDeform);
  513. _linkedMeshes.add(linkedMesh);
  514. }
  515. break;
  516. }
  517. case AttachmentType_Boundingbox: {
  518. attachment = _attachmentLoader->newBoundingBoxAttachment(*skin, attachmentName);
  519. BoundingBoxAttachment *box = static_cast<BoundingBoxAttachment *>(attachment);
  520. int vertexCount = Json::getInt(attachmentMap, "vertexCount", 0) << 1;
  521. readVertices(attachmentMap, box, vertexCount);
  522. _attachmentLoader->configureAttachment(attachment);
  523. break;
  524. }
  525. case AttachmentType_Path: {
  526. attachment = _attachmentLoader->newPathAttachment(*skin, attachmentName);
  527. PathAttachment *pathAttatchment = static_cast<PathAttachment *>(attachment);
  528. int vertexCount = 0;
  529. pathAttatchment->_closed = Json::getInt(attachmentMap, "closed", 0) ? true : false;
  530. pathAttatchment->_constantSpeed = Json::getInt(attachmentMap, "constantSpeed", 1) ? true : false;
  531. vertexCount = Json::getInt(attachmentMap, "vertexCount", 0);
  532. readVertices(attachmentMap, pathAttatchment, vertexCount << 1);
  533. pathAttatchment->_lengths.ensureCapacity(vertexCount / 3);
  534. pathAttatchment->_lengths.setSize(vertexCount / 3, 0);
  535. curves = Json::getItem(attachmentMap, "lengths");
  536. for (curves = curves->_child, ii = 0; curves; curves = curves->_next, ++ii)
  537. pathAttatchment->_lengths[ii] = curves->_valueFloat * _scale;
  538. _attachmentLoader->configureAttachment(attachment);
  539. break;
  540. }
  541. case AttachmentType_Point: {
  542. attachment = _attachmentLoader->newPointAttachment(*skin, attachmentName);
  543. PointAttachment *point = static_cast<PointAttachment *>(attachment);
  544. point->_x = Json::getFloat(attachmentMap, "x", 0) * _scale;
  545. point->_y = Json::getFloat(attachmentMap, "y", 0) * _scale;
  546. point->_rotation = Json::getFloat(attachmentMap, "rotation", 0);
  547. _attachmentLoader->configureAttachment(attachment);
  548. break;
  549. }
  550. case AttachmentType_Clipping: {
  551. attachment = _attachmentLoader->newClippingAttachment(*skin, attachmentName);
  552. ClippingAttachment *clip = static_cast<ClippingAttachment *>(attachment);
  553. int vertexCount = 0;
  554. const char *end = Json::getString(attachmentMap, "end", 0);
  555. if (end) clip->_endSlot = skeletonData->findSlot(end);
  556. vertexCount = Json::getInt(attachmentMap, "vertexCount", 0) << 1;
  557. readVertices(attachmentMap, clip, vertexCount);
  558. _attachmentLoader->configureAttachment(attachment);
  559. break;
  560. }
  561. }
  562. skin->setAttachment(slot->getIndex(), skinAttachmentName, attachment);
  563. }
  564. }
  565. }
  566. }
  567. /* Linked meshes. */
  568. int n = _linkedMeshes.size();
  569. for (i = 0; i < n; ++i) {
  570. LinkedMesh *linkedMesh = _linkedMeshes[i];
  571. Skin *skin = linkedMesh->_skin.length() == 0 ? skeletonData->getDefaultSkin() : skeletonData->findSkin(linkedMesh->_skin);
  572. if (skin == NULL) {
  573. delete skeletonData;
  574. setError(root, "Skin not found: ", linkedMesh->_skin.buffer());
  575. return NULL;
  576. }
  577. Attachment *parent = skin->getAttachment(linkedMesh->_slotIndex, linkedMesh->_parent);
  578. if (parent == NULL) {
  579. delete skeletonData;
  580. setError(root, "Parent mesh not found: ", linkedMesh->_parent.buffer());
  581. return NULL;
  582. }
  583. linkedMesh->_mesh->_deformAttachment = linkedMesh->_inheritDeform ? static_cast<VertexAttachment*>(parent) : linkedMesh->_mesh;
  584. linkedMesh->_mesh->setParentMesh(static_cast<MeshAttachment *>(parent));
  585. linkedMesh->_mesh->updateUVs();
  586. _attachmentLoader->configureAttachment(linkedMesh->_mesh);
  587. }
  588. ContainerUtil::cleanUpVectorOfPointers(_linkedMeshes);
  589. _linkedMeshes.clear();
  590. /* Events. */
  591. events = Json::getItem(root, "events");
  592. if (events) {
  593. Json *eventMap;
  594. skeletonData->_events.ensureCapacity(events->_size);
  595. skeletonData->_events.setSize(events->_size, 0);
  596. for (eventMap = events->_child, i = 0; eventMap; eventMap = eventMap->_next, ++i) {
  597. EventData *eventData = new(__FILE__, __LINE__) EventData(String(eventMap->_name));
  598. eventData->_intValue = Json::getInt(eventMap, "int", 0);
  599. eventData->_floatValue = Json::getFloat(eventMap, "float", 0);
  600. const char *stringValue = Json::getString(eventMap, "string", 0);
  601. eventData->_stringValue = stringValue;
  602. const char *audioPath = Json::getString(eventMap, "audio", 0);
  603. eventData->_audioPath = audioPath;
  604. if (audioPath) {
  605. eventData->_volume = Json::getFloat(eventMap, "volume", 1);
  606. eventData->_balance = Json::getFloat(eventMap, "balance", 0);
  607. }
  608. skeletonData->_events[i] = eventData;
  609. }
  610. }
  611. /* Animations. */
  612. animations = Json::getItem(root, "animations");
  613. if (animations) {
  614. Json *animationMap;
  615. skeletonData->_animations.ensureCapacity(animations->_size);
  616. skeletonData->_animations.setSize(animations->_size, 0);
  617. int animationsIndex = 0;
  618. for (animationMap = animations->_child; animationMap; animationMap = animationMap->_next) {
  619. Animation *animation = readAnimation(animationMap, skeletonData);
  620. if (!animation) {
  621. delete skeletonData;
  622. delete root;
  623. return NULL;
  624. }
  625. skeletonData->_animations[animationsIndex++] = animation;
  626. }
  627. }
  628. delete root;
  629. return skeletonData;
  630. }
  631. float SkeletonJson::toColor(const char *value, size_t index) {
  632. char digits[3];
  633. char *error;
  634. int color;
  635. if (index >= strlen(value) / 2) return -1;
  636. value += index * 2;
  637. digits[0] = *value;
  638. digits[1] = *(value + 1);
  639. digits[2] = '\0';
  640. color = (int) strtoul(digits, &error, 16);
  641. if (*error != 0) return -1;
  642. return color / (float) 255;
  643. }
  644. void SkeletonJson::readCurve(Json *frame, CurveTimeline *timeline, size_t frameIndex) {
  645. Json *curve = Json::getItem(frame, "curve");
  646. if (!curve) return;
  647. if (curve->_type == Json::JSON_STRING && strcmp(curve->_valueString, "stepped") == 0)
  648. timeline->setStepped(frameIndex);
  649. else {
  650. float c1 = Json::getFloat(frame, "curve", 0);
  651. float c2 = Json::getFloat(frame, "c2", 0);
  652. float c3 = Json::getFloat(frame, "c3", 1);
  653. float c4 = Json::getFloat(frame, "c4", 1);
  654. timeline->setCurve(frameIndex, c1, c2, c3, c4);
  655. }
  656. }
  657. Animation *SkeletonJson::readAnimation(Json *root, SkeletonData *skeletonData) {
  658. Vector<Timeline *> timelines;
  659. float duration = 0;
  660. size_t frameIndex;
  661. Json *valueMap;
  662. int timelinesCount = 0;
  663. Json *bones = Json::getItem(root, "bones");
  664. Json *slots = Json::getItem(root, "slots");
  665. Json *ik = Json::getItem(root, "ik");
  666. Json *transform = Json::getItem(root, "transform");
  667. Json *paths = Json::getItem(root, "path");
  668. Json *deform = Json::getItem(root, "deform");
  669. Json *drawOrder = Json::getItem(root, "drawOrder");
  670. Json *events = Json::getItem(root, "events");
  671. Json *boneMap, *slotMap, *constraintMap;
  672. if (!drawOrder) drawOrder = Json::getItem(root, "draworder");
  673. for (boneMap = bones ? bones->_child : NULL; boneMap; boneMap = boneMap->_next)
  674. timelinesCount += boneMap->_size;
  675. for (slotMap = slots ? slots->_child : NULL; slotMap; slotMap = slotMap->_next)
  676. timelinesCount += slotMap->_size;
  677. timelinesCount += ik ? ik->_size : 0;
  678. timelinesCount += transform ? transform->_size : 0;
  679. for (constraintMap = paths ? paths->_child : NULL; constraintMap; constraintMap = constraintMap->_next)
  680. timelinesCount += constraintMap->_size;
  681. for (constraintMap = deform ? deform->_child : NULL; constraintMap; constraintMap = constraintMap->_next)
  682. for (slotMap = constraintMap->_child; slotMap; slotMap = slotMap->_next)
  683. timelinesCount += slotMap->_size;
  684. if (drawOrder) ++timelinesCount;
  685. if (events) ++timelinesCount;
  686. /** Slot timelines. */
  687. for (slotMap = slots ? slots->_child : 0; slotMap; slotMap = slotMap->_next) {
  688. Json *timelineMap;
  689. int slotIndex = skeletonData->findSlotIndex(slotMap->_name);
  690. if (slotIndex == -1) {
  691. ContainerUtil::cleanUpVectorOfPointers(timelines);
  692. setError(NULL, "Slot not found: ", slotMap->_name);
  693. return NULL;
  694. }
  695. for (timelineMap = slotMap->_child; timelineMap; timelineMap = timelineMap->_next) {
  696. if (strcmp(timelineMap->_name, "attachment") == 0) {
  697. AttachmentTimeline *timeline = new(__FILE__, __LINE__) AttachmentTimeline(timelineMap->_size);
  698. timeline->_slotIndex = slotIndex;
  699. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  700. Json *name = Json::getItem(valueMap, "name");
  701. String attachmentName = name->_type == Json::JSON_NULL ? "" : name->_valueString;
  702. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), attachmentName);
  703. }
  704. timelines.add(timeline);
  705. timelinesCount++;
  706. duration = MathUtil::max(duration, timeline->_frames[timelineMap->_size - 1]);
  707. } else if (strcmp(timelineMap->_name, "color") == 0) {
  708. ColorTimeline *timeline = new(__FILE__, __LINE__) ColorTimeline(timelineMap->_size);
  709. timeline->_slotIndex = slotIndex;
  710. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  711. const char *s = Json::getString(valueMap, "color", 0);
  712. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), toColor(s, 0), toColor(s, 1),
  713. toColor(s, 2), toColor(s, 3));
  714. readCurve(valueMap, timeline, frameIndex);
  715. }
  716. timelines.add(timeline);
  717. timelinesCount++;
  718. duration = MathUtil::max(duration, timeline->_frames[(timelineMap->_size - 1) * ColorTimeline::ENTRIES]);
  719. } else if (strcmp(timelineMap->_name, "twoColor") == 0) {
  720. TwoColorTimeline *timeline = new(__FILE__, __LINE__) TwoColorTimeline(timelineMap->_size);
  721. timeline->_slotIndex = slotIndex;
  722. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  723. const char *s = Json::getString(valueMap, "light", 0);
  724. const char *ds = Json::getString(valueMap, "dark", 0);
  725. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), toColor(s, 0), toColor(s, 1),
  726. toColor(s, 2), toColor(s, 3), toColor(ds, 0), toColor(ds, 1), toColor(ds, 2));
  727. readCurve(valueMap, timeline, frameIndex);
  728. }
  729. timelines.add(timeline);
  730. timelinesCount++;
  731. duration = MathUtil::max(duration,
  732. timeline->_frames[(timelineMap->_size - 1) * TwoColorTimeline::ENTRIES]);
  733. } else {
  734. ContainerUtil::cleanUpVectorOfPointers(timelines);
  735. setError(NULL, "Invalid timeline type for a slot: ", timelineMap->_name);
  736. return NULL;
  737. }
  738. }
  739. }
  740. /** Bone timelines. */
  741. for (boneMap = bones ? bones->_child : 0; boneMap; boneMap = boneMap->_next) {
  742. Json *timelineMap;
  743. int boneIndex = skeletonData->findBoneIndex(boneMap->_name);
  744. if (boneIndex == -1) {
  745. ContainerUtil::cleanUpVectorOfPointers(timelines);
  746. setError(NULL, "Bone not found: ", boneMap->_name);
  747. return NULL;
  748. }
  749. for (timelineMap = boneMap->_child; timelineMap; timelineMap = timelineMap->_next) {
  750. if (strcmp(timelineMap->_name, "rotate") == 0) {
  751. RotateTimeline *timeline = new(__FILE__, __LINE__) RotateTimeline(timelineMap->_size);
  752. timeline->_boneIndex = boneIndex;
  753. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  754. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), Json::getFloat(valueMap, "angle", 0));
  755. readCurve(valueMap, timeline, frameIndex);
  756. }
  757. timelines.add(timeline);
  758. timelinesCount++;
  759. duration = MathUtil::max(duration, timeline->_frames[(timelineMap->_size - 1) * RotateTimeline::ENTRIES]);
  760. } else {
  761. int isScale = strcmp(timelineMap->_name, "scale") == 0;
  762. int isTranslate = strcmp(timelineMap->_name, "translate") == 0;
  763. int isShear = strcmp(timelineMap->_name, "shear") == 0;
  764. if (isScale || isTranslate || isShear) {
  765. float timelineScale = isTranslate ? _scale : 1;
  766. float defaultValue = 0;
  767. TranslateTimeline *timeline = 0;
  768. if (isScale) {
  769. timeline = new(__FILE__, __LINE__) ScaleTimeline(timelineMap->_size);
  770. defaultValue = 1;
  771. } else if (isTranslate) {
  772. timeline = new(__FILE__, __LINE__) TranslateTimeline(timelineMap->_size);
  773. } else if (isShear) {
  774. timeline = new(__FILE__, __LINE__) ShearTimeline(timelineMap->_size);
  775. }
  776. timeline->_boneIndex = boneIndex;
  777. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  778. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0),
  779. Json::getFloat(valueMap, "x", defaultValue) * timelineScale,
  780. Json::getFloat(valueMap, "y", defaultValue) * timelineScale);
  781. readCurve(valueMap, timeline, frameIndex);
  782. }
  783. timelines.add(timeline);
  784. timelinesCount++;
  785. duration = MathUtil::max(duration, timeline->_frames[(timelineMap->_size - 1) * TranslateTimeline::ENTRIES]);
  786. } else {
  787. ContainerUtil::cleanUpVectorOfPointers(timelines);
  788. setError(NULL, "Invalid timeline type for a bone: ", timelineMap->_name);
  789. return NULL;
  790. }
  791. }
  792. }
  793. }
  794. /** IK constraint timelines. */
  795. for (constraintMap = ik ? ik->_child : 0; constraintMap; constraintMap = constraintMap->_next) {
  796. IkConstraintData *constraint = skeletonData->findIkConstraint(constraintMap->_name);
  797. IkConstraintTimeline *timeline = new(__FILE__, __LINE__) IkConstraintTimeline(constraintMap->_size);
  798. for (frameIndex = 0; frameIndex < skeletonData->_ikConstraints.size(); ++frameIndex) {
  799. if (constraint == skeletonData->_ikConstraints[frameIndex]) {
  800. timeline->_ikConstraintIndex = frameIndex;
  801. break;
  802. }
  803. }
  804. for (valueMap = constraintMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  805. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), Json::getFloat(valueMap, "mix", 1),
  806. Json::getFloat(valueMap, "softness", 0) * _scale, Json::getInt(valueMap, "bendPositive", 1) ? 1 : -1,
  807. Json::getInt(valueMap, "compress", 0) ? true : false, Json::getInt(valueMap, "stretch", 0) ? true : false);
  808. readCurve(valueMap, timeline, frameIndex);
  809. }
  810. timelines.add(timeline);
  811. timelinesCount++;
  812. duration = MathUtil::max(duration, timeline->_frames[(constraintMap->_size - 1) * IkConstraintTimeline::ENTRIES]);
  813. }
  814. /** Transform constraint timelines. */
  815. for (constraintMap = transform ? transform->_child : 0; constraintMap; constraintMap = constraintMap->_next) {
  816. TransformConstraintData *constraint = skeletonData->findTransformConstraint(constraintMap->_name);
  817. TransformConstraintTimeline *timeline = new(__FILE__, __LINE__) TransformConstraintTimeline(constraintMap->_size);
  818. for (frameIndex = 0; frameIndex < skeletonData->_transformConstraints.size(); ++frameIndex) {
  819. if (constraint == skeletonData->_transformConstraints[frameIndex]) {
  820. timeline->_transformConstraintIndex = frameIndex;
  821. break;
  822. }
  823. }
  824. for (valueMap = constraintMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  825. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0),
  826. Json::getFloat(valueMap, "rotateMix", 1), Json::getFloat(valueMap, "translateMix", 1),
  827. Json::getFloat(valueMap, "scaleMix", 1), Json::getFloat(valueMap, "shearMix", 1));
  828. readCurve(valueMap, timeline, frameIndex);
  829. }
  830. timelines.add(timeline);
  831. timelinesCount++;
  832. duration = MathUtil::max(duration, timeline->_frames[(constraintMap->_size - 1) * TransformConstraintTimeline::ENTRIES]);
  833. }
  834. /** Path constraint timelines. */
  835. for (constraintMap = paths ? paths->_child : 0; constraintMap; constraintMap = constraintMap->_next) {
  836. size_t constraintIndex = 0, i;
  837. Json *timelineMap;
  838. PathConstraintData *data = skeletonData->findPathConstraint(constraintMap->_name);
  839. if (!data) {
  840. ContainerUtil::cleanUpVectorOfPointers(timelines);
  841. setError(NULL, "Path constraint not found: ", constraintMap->_name);
  842. return NULL;
  843. }
  844. for (i = 0; i < skeletonData->_pathConstraints.size(); i++) {
  845. if (skeletonData->_pathConstraints[i] == data) {
  846. constraintIndex = i;
  847. break;
  848. }
  849. }
  850. for (timelineMap = constraintMap->_child; timelineMap; timelineMap = timelineMap->_next) {
  851. const char *timelineName = timelineMap->_name;
  852. if (strcmp(timelineName, "position") == 0 || strcmp(timelineName, "spacing") == 0) {
  853. PathConstraintPositionTimeline *timeline;
  854. float timelineScale = 1;
  855. if (strcmp(timelineName, "spacing") == 0) {
  856. timeline = new(__FILE__, __LINE__) PathConstraintSpacingTimeline(timelineMap->_size);
  857. if (data->_spacingMode == SpacingMode_Length || data->_spacingMode == SpacingMode_Fixed) {
  858. timelineScale = _scale;
  859. }
  860. } else {
  861. timeline = new(__FILE__, __LINE__) PathConstraintPositionTimeline(timelineMap->_size);
  862. if (data->_positionMode == PositionMode_Fixed) {
  863. timelineScale = _scale;
  864. }
  865. }
  866. timeline->_pathConstraintIndex = constraintIndex;
  867. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  868. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0),
  869. Json::getFloat(valueMap, timelineName, 0) * timelineScale);
  870. readCurve(valueMap, timeline, frameIndex);
  871. }
  872. timelines.add(timeline);
  873. timelinesCount++;
  874. duration = MathUtil::max(duration, timeline->_frames[(timelineMap->_size - 1) *
  875. PathConstraintPositionTimeline::ENTRIES]);
  876. } else if (strcmp(timelineName, "mix") == 0) {
  877. PathConstraintMixTimeline *timeline = new(__FILE__, __LINE__) PathConstraintMixTimeline(timelineMap->_size);
  878. timeline->_pathConstraintIndex = constraintIndex;
  879. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  880. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0),
  881. Json::getFloat(valueMap, "rotateMix", 1),
  882. Json::getFloat(valueMap, "translateMix", 1));
  883. readCurve(valueMap, timeline, frameIndex);
  884. }
  885. timelines.add(timeline);
  886. timelinesCount++;
  887. duration = MathUtil::max(duration, timeline->_frames[(timelineMap->_size - 1) * PathConstraintMixTimeline::ENTRIES]);
  888. }
  889. }
  890. }
  891. /** Deform timelines. */
  892. for (constraintMap = deform ? deform->_child : NULL; constraintMap; constraintMap = constraintMap->_next) {
  893. Skin *skin = skeletonData->findSkin(constraintMap->_name);
  894. for (slotMap = constraintMap->_child; slotMap; slotMap = slotMap->_next) {
  895. int slotIndex = skeletonData->findSlotIndex(slotMap->_name);
  896. Json *timelineMap;
  897. for (timelineMap = slotMap->_child; timelineMap; timelineMap = timelineMap->_next) {
  898. DeformTimeline *timeline;
  899. int weighted, deformLength;
  900. Attachment *baseAttachment = skin->getAttachment(slotIndex, timelineMap->_name);
  901. if (!baseAttachment) {
  902. ContainerUtil::cleanUpVectorOfPointers(timelines);
  903. setError(NULL, "Attachment not found: ", timelineMap->_name);
  904. return NULL;
  905. }
  906. VertexAttachment *attachment = static_cast<VertexAttachment *>(baseAttachment);
  907. weighted = attachment->_bones.size() != 0;
  908. Vector<float> &verts = attachment->_vertices;
  909. deformLength = weighted ? verts.size() / 3 * 2 : verts.size();
  910. timeline = new(__FILE__, __LINE__) DeformTimeline(timelineMap->_size);
  911. timeline->_slotIndex = slotIndex;
  912. timeline->_attachment = attachment;
  913. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  914. Json *vertices = Json::getItem(valueMap, "vertices");
  915. Vector<float> deformed;
  916. if (!vertices) {
  917. if (weighted) {
  918. deformed.setSize(deformLength, 0);
  919. } else {
  920. deformed.clearAndAddAll(attachment->_vertices);
  921. }
  922. } else {
  923. int v, start = Json::getInt(valueMap, "offset", 0);
  924. Json *vertex;
  925. deformed.setSize(deformLength, 0);
  926. if (_scale == 1) {
  927. for (vertex = vertices->_child, v = start; vertex; vertex = vertex->_next, ++v) {
  928. deformed[v] = vertex->_valueFloat;
  929. }
  930. } else {
  931. for (vertex = vertices->_child, v = start; vertex; vertex = vertex->_next, ++v) {
  932. deformed[v] = vertex->_valueFloat * _scale;
  933. }
  934. }
  935. if (!weighted) {
  936. Vector<float> &verticesAttachment = attachment->_vertices;
  937. for (v = 0; v < deformLength; ++v) {
  938. deformed[v] += verticesAttachment[v];
  939. }
  940. }
  941. }
  942. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), deformed);
  943. readCurve(valueMap, timeline, frameIndex);
  944. }
  945. timelines.add(timeline);
  946. timelinesCount++;
  947. duration = MathUtil::max(duration, timeline->_frames[timelineMap->_size - 1]);
  948. }
  949. }
  950. }
  951. /** Draw order timeline. */
  952. if (drawOrder) {
  953. DrawOrderTimeline *timeline = new(__FILE__, __LINE__) DrawOrderTimeline(drawOrder->_size);
  954. for (valueMap = drawOrder->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  955. int ii;
  956. Vector<int> drawOrder2;
  957. Json *offsets = Json::getItem(valueMap, "offsets");
  958. if (offsets) {
  959. Json *offsetMap;
  960. Vector<int> unchanged;
  961. unchanged.ensureCapacity(skeletonData->_slots.size() - offsets->_size);
  962. unchanged.setSize(skeletonData->_slots.size() - offsets->_size, 0);
  963. size_t originalIndex = 0, unchangedIndex = 0;
  964. drawOrder2.ensureCapacity(skeletonData->_slots.size());
  965. drawOrder2.setSize(skeletonData->_slots.size(), 0);
  966. for (ii = (int)skeletonData->_slots.size() - 1; ii >= 0; --ii)
  967. drawOrder2[ii] = -1;
  968. for (offsetMap = offsets->_child; offsetMap; offsetMap = offsetMap->_next) {
  969. int slotIndex = skeletonData->findSlotIndex(Json::getString(offsetMap, "slot", 0));
  970. if (slotIndex == -1) {
  971. ContainerUtil::cleanUpVectorOfPointers(timelines);
  972. setError(NULL, "Slot not found: ", Json::getString(offsetMap, "slot", 0));
  973. return NULL;
  974. }
  975. /* Collect unchanged items. */
  976. while (originalIndex != (size_t)slotIndex)
  977. unchanged[unchangedIndex++] = originalIndex++;
  978. /* Set changed items. */
  979. drawOrder2[originalIndex + Json::getInt(offsetMap, "offset", 0)] = originalIndex;
  980. originalIndex++;
  981. }
  982. /* Collect remaining unchanged items. */
  983. while (originalIndex < skeletonData->_slots.size())
  984. unchanged[unchangedIndex++] = originalIndex++;
  985. /* Fill in unchanged items. */
  986. for (ii = (int)skeletonData->_slots.size() - 1; ii >= 0; ii--)
  987. if (drawOrder2[ii] == -1) drawOrder2[ii] = unchanged[--unchangedIndex];
  988. }
  989. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), drawOrder2);
  990. }
  991. timelines.add(timeline);
  992. timelinesCount++;
  993. duration = MathUtil::max(duration, timeline->_frames[drawOrder->_size - 1]);
  994. }
  995. /** Event timeline. */
  996. if (events) {
  997. EventTimeline *timeline = new(__FILE__, __LINE__) EventTimeline(events->_size);
  998. for (valueMap = events->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  999. Event *event;
  1000. EventData *eventData = skeletonData->findEvent(Json::getString(valueMap, "name", 0));
  1001. if (!eventData) {
  1002. ContainerUtil::cleanUpVectorOfPointers(timelines);
  1003. setError(NULL, "Event not found: ", Json::getString(valueMap, "name", 0));
  1004. return NULL;
  1005. }
  1006. event = new(__FILE__, __LINE__) Event(Json::getFloat(valueMap, "time", 0), *eventData);
  1007. event->_intValue = Json::getInt(valueMap, "int", eventData->_intValue);
  1008. event->_floatValue = Json::getFloat(valueMap, "float", eventData->_floatValue);
  1009. event->_stringValue = Json::getString(valueMap, "string", eventData->_stringValue.buffer());
  1010. if (!eventData->_audioPath.isEmpty()) {
  1011. event->_volume = Json::getFloat(valueMap, "volume", 1);
  1012. event->_balance = Json::getFloat(valueMap, "balance", 0);
  1013. }
  1014. timeline->setFrame(frameIndex, event);
  1015. }
  1016. timelines.add(timeline);
  1017. timelinesCount++;
  1018. duration = MathUtil::max(duration, timeline->_frames[events->_size - 1]);
  1019. }
  1020. return new(__FILE__, __LINE__) Animation(String(root->_name), timelines, duration);
  1021. }
  1022. void SkeletonJson::readVertices(Json *attachmentMap, VertexAttachment *attachment, size_t verticesLength) {
  1023. Json *entry;
  1024. size_t i, n, nn, entrySize;
  1025. Vector<float> vertices;
  1026. attachment->setWorldVerticesLength(verticesLength);
  1027. entry = Json::getItem(attachmentMap, "vertices");
  1028. entrySize = entry->_size;
  1029. vertices.ensureCapacity(entrySize);
  1030. vertices.setSize(entrySize, 0);
  1031. for (entry = entry->_child, i = 0; entry; entry = entry->_next, ++i)
  1032. vertices[i] = entry->_valueFloat;
  1033. if (verticesLength == entrySize) {
  1034. if (_scale != 1) {
  1035. for (i = 0; i < entrySize; ++i)
  1036. vertices[i] *= _scale;
  1037. }
  1038. attachment->getVertices().clearAndAddAll(vertices);
  1039. return;
  1040. }
  1041. Vertices bonesAndWeights;
  1042. bonesAndWeights._bones.ensureCapacity(verticesLength * 3);
  1043. bonesAndWeights._vertices.ensureCapacity(verticesLength * 3 * 3);
  1044. for (i = 0, n = entrySize; i < n;) {
  1045. int boneCount = (int) vertices[i++];
  1046. bonesAndWeights._bones.add(boneCount);
  1047. for (nn = i + boneCount * 4; i < nn; i += 4) {
  1048. bonesAndWeights._bones.add((int) vertices[i]);
  1049. bonesAndWeights._vertices.add(vertices[i + 1] * _scale);
  1050. bonesAndWeights._vertices.add(vertices[i + 2] * _scale);
  1051. bonesAndWeights._vertices.add(vertices[i + 3]);
  1052. }
  1053. }
  1054. attachment->getVertices().clearAndAddAll(bonesAndWeights._vertices);
  1055. attachment->getBones().clearAndAddAll(bonesAndWeights._bones);
  1056. }
  1057. void SkeletonJson::setError(Json *root, const String &value1, const String &value2) {
  1058. _error = String(value1).append(value2);
  1059. delete root;
  1060. }