SkeletonJson.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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 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. skeletonData->_x = Json::getFloat(skeleton, "x", 0);
  117. skeletonData->_y = Json::getFloat(skeleton, "y", 0);
  118. skeletonData->_width = Json::getFloat(skeleton, "width", 0);
  119. skeletonData->_height = Json::getFloat(skeleton, "height", 0);
  120. skeletonData->_fps = Json::getFloat(skeleton, "fps", 30);
  121. skeletonData->_audioPath = Json::getString(skeleton, "audio", 0);
  122. skeletonData->_imagesPath = Json::getString(skeleton, "images", 0);
  123. }
  124. /* Bones. */
  125. bones = Json::getItem(root, "bones");
  126. skeletonData->_bones.setSize(bones->_size, 0);
  127. int bonesCount = 0;
  128. for (boneMap = bones->_child, i = 0; boneMap; boneMap = boneMap->_next, ++i) {
  129. BoneData *data;
  130. const char *transformMode;
  131. BoneData *parent = 0;
  132. const char *parentName = Json::getString(boneMap, "parent", 0);
  133. if (parentName) {
  134. parent = skeletonData->findBone(parentName);
  135. if (!parent) {
  136. delete skeletonData;
  137. setError(root, "Parent bone not found: ", parentName);
  138. return NULL;
  139. }
  140. }
  141. data = new(__FILE__, __LINE__) BoneData(bonesCount, Json::getString(boneMap, "name", 0), parent);
  142. data->_length = Json::getFloat(boneMap, "length", 0) * _scale;
  143. data->_x = Json::getFloat(boneMap, "x", 0) * _scale;
  144. data->_y = Json::getFloat(boneMap, "y", 0) * _scale;
  145. data->_rotation = Json::getFloat(boneMap, "rotation", 0);
  146. data->_scaleX = Json::getFloat(boneMap, "scaleX", 1);
  147. data->_scaleY = Json::getFloat(boneMap, "scaleY", 1);
  148. data->_shearX = Json::getFloat(boneMap, "shearX", 0);
  149. data->_shearY = Json::getFloat(boneMap, "shearY", 0);
  150. transformMode = Json::getString(boneMap, "transform", "normal");
  151. data->_transformMode = TransformMode_Normal;
  152. if (strcmp(transformMode, "normal") == 0) data->_transformMode = TransformMode_Normal;
  153. else if (strcmp(transformMode, "onlyTranslation") == 0) data->_transformMode = TransformMode_OnlyTranslation;
  154. else if (strcmp(transformMode, "noRotationOrReflection") == 0) data->_transformMode = TransformMode_NoRotationOrReflection;
  155. else if (strcmp(transformMode, "noScale") == 0) data->_transformMode = TransformMode_NoScale;
  156. else if (strcmp(transformMode, "noScaleOrReflection") == 0) data->_transformMode = TransformMode_NoScaleOrReflection;
  157. data->_skinRequired = Json::getBoolean(boneMap, "skin", false);
  158. skeletonData->_bones[i] = data;
  159. bonesCount++;
  160. }
  161. /* Slots. */
  162. slots = Json::getItem(root, "slots");
  163. if (slots) {
  164. Json *slotMap;
  165. skeletonData->_slots.ensureCapacity(slots->_size);
  166. skeletonData->_slots.setSize(slots->_size, 0);
  167. for (slotMap = slots->_child, i = 0; slotMap; slotMap = slotMap->_next, ++i) {
  168. SlotData *data;
  169. const char *color;
  170. const char *dark;
  171. Json *item;
  172. const char *boneName = Json::getString(slotMap, "bone", 0);
  173. BoneData *boneData = skeletonData->findBone(boneName);
  174. if (!boneData) {
  175. delete skeletonData;
  176. setError(root, "Slot bone not found: ", boneName);
  177. return NULL;
  178. }
  179. data = new(__FILE__, __LINE__) SlotData(i, Json::getString(slotMap, "name", 0), *boneData);
  180. color = Json::getString(slotMap, "color", 0);
  181. if (color) {
  182. Color &c = data->getColor();
  183. c.r = toColor(color, 0);
  184. c.g = toColor(color, 1);
  185. c.b = toColor(color, 2);
  186. c.a = toColor(color, 3);
  187. }
  188. dark = Json::getString(slotMap, "dark", 0);
  189. if (dark) {
  190. Color &darkColor = data->getDarkColor();
  191. darkColor.r = toColor(dark, 0);
  192. darkColor.g = toColor(dark, 1);
  193. darkColor.b = toColor(dark, 2);
  194. darkColor.a = 1;
  195. data->setHasDarkColor(true);
  196. }
  197. item = Json::getItem(slotMap, "attachment");
  198. if (item) data->setAttachmentName(item->_valueString);
  199. item = Json::getItem(slotMap, "blend");
  200. if (item) {
  201. if (strcmp(item->_valueString, "additive") == 0) data->_blendMode = BlendMode_Additive;
  202. else if (strcmp(item->_valueString, "multiply") == 0) data->_blendMode = BlendMode_Multiply;
  203. else if (strcmp(item->_valueString, "screen") == 0) data->_blendMode = BlendMode_Screen;
  204. }
  205. skeletonData->_slots[i] = data;
  206. }
  207. }
  208. /* IK constraints. */
  209. ik = Json::getItem(root, "ik");
  210. if (ik) {
  211. Json *constraintMap;
  212. skeletonData->_ikConstraints.ensureCapacity(ik->_size);
  213. skeletonData->_ikConstraints.setSize(ik->_size, 0);
  214. for (constraintMap = ik->_child, i = 0; constraintMap; constraintMap = constraintMap->_next, ++i) {
  215. const char *targetName;
  216. IkConstraintData *data = new(__FILE__, __LINE__) IkConstraintData(Json::getString(constraintMap, "name", 0));
  217. data->setOrder(Json::getInt(constraintMap, "order", 0));
  218. data->setSkinRequired(Json::getBoolean(constraintMap, "skin", false));
  219. boneMap = Json::getItem(constraintMap, "bones");
  220. data->_bones.ensureCapacity(boneMap->_size);
  221. data->_bones.setSize(boneMap->_size, 0);
  222. for (boneMap = boneMap->_child, ii = 0; boneMap; boneMap = boneMap->_next, ++ii) {
  223. data->_bones[ii] = skeletonData->findBone(boneMap->_valueString);
  224. if (!data->_bones[ii]) {
  225. delete skeletonData;
  226. setError(root, "IK bone not found: ", boneMap->_valueString);
  227. return NULL;
  228. }
  229. }
  230. targetName = Json::getString(constraintMap, "target", 0);
  231. data->_target = skeletonData->findBone(targetName);
  232. if (!data->_target) {
  233. delete skeletonData;
  234. setError(root, "Target bone not found: ", targetName);
  235. return NULL;
  236. }
  237. data->_mix = Json::getFloat(constraintMap, "mix", 1);
  238. data->_softness = Json::getFloat(constraintMap, "softness", 0) * _scale;
  239. data->_bendDirection = Json::getInt(constraintMap, "bendPositive", 1) ? 1 : -1;
  240. data->_compress = Json::getInt(constraintMap, "compress", 0) ? true: false;
  241. data->_stretch = Json::getInt(constraintMap, "stretch", 0) ? true: false;
  242. data->_uniform = Json::getInt(constraintMap, "uniform", 0) ? true: false;
  243. skeletonData->_ikConstraints[i] = data;
  244. }
  245. }
  246. /* Transform constraints. */
  247. transform = Json::getItem(root, "transform");
  248. if (transform) {
  249. Json *constraintMap;
  250. skeletonData->_transformConstraints.ensureCapacity(transform->_size);
  251. skeletonData->_transformConstraints.setSize(transform->_size, 0);
  252. for (constraintMap = transform->_child, i = 0; constraintMap; constraintMap = constraintMap->_next, ++i) {
  253. const char *name;
  254. TransformConstraintData *data = new(__FILE__, __LINE__) TransformConstraintData(Json::getString(constraintMap, "name", 0));
  255. data->setOrder(Json::getInt(constraintMap, "order", 0));
  256. data->setSkinRequired(Json::getBoolean(constraintMap, "skin", false));
  257. boneMap = Json::getItem(constraintMap, "bones");
  258. data->_bones.ensureCapacity(boneMap->_size);
  259. data->_bones.setSize(boneMap->_size, 0);
  260. for (boneMap = boneMap->_child, ii = 0; boneMap; boneMap = boneMap->_next, ++ii) {
  261. data->_bones[ii] = skeletonData->findBone(boneMap->_valueString);
  262. if (!data->_bones[ii]) {
  263. delete skeletonData;
  264. setError(root, "Transform bone not found: ", boneMap->_valueString);
  265. return NULL;
  266. }
  267. }
  268. name = Json::getString(constraintMap, "target", 0);
  269. data->_target = skeletonData->findBone(name);
  270. if (!data->_target) {
  271. delete skeletonData;
  272. setError(root, "Target bone not found: ", name);
  273. return NULL;
  274. }
  275. data->_local = Json::getInt(constraintMap, "local", 0) ? true : false;
  276. data->_relative = Json::getInt(constraintMap, "relative", 0) ? true : false;
  277. data->_offsetRotation = Json::getFloat(constraintMap, "rotation", 0);
  278. data->_offsetX = Json::getFloat(constraintMap, "x", 0) * _scale;
  279. data->_offsetY = Json::getFloat(constraintMap, "y", 0) * _scale;
  280. data->_offsetScaleX = Json::getFloat(constraintMap, "scaleX", 0);
  281. data->_offsetScaleY = Json::getFloat(constraintMap, "scaleY", 0);
  282. data->_offsetShearY = Json::getFloat(constraintMap, "shearY", 0);
  283. data->_rotateMix = Json::getFloat(constraintMap, "rotateMix", 1);
  284. data->_translateMix = Json::getFloat(constraintMap, "translateMix", 1);
  285. data->_scaleMix = Json::getFloat(constraintMap, "scaleMix", 1);
  286. data->_shearMix = Json::getFloat(constraintMap, "shearMix", 1);
  287. skeletonData->_transformConstraints[i] = data;
  288. }
  289. }
  290. /* Path constraints */
  291. path = Json::getItem(root, "path");
  292. if (path) {
  293. Json *constraintMap;
  294. skeletonData->_pathConstraints.ensureCapacity(path->_size);
  295. skeletonData->_pathConstraints.setSize(path->_size, 0);
  296. for (constraintMap = path->_child, i = 0; constraintMap; constraintMap = constraintMap->_next, ++i) {
  297. const char *name;
  298. const char *item;
  299. PathConstraintData *data = new(__FILE__, __LINE__) PathConstraintData(Json::getString(constraintMap, "name", 0));
  300. data->setOrder(Json::getInt(constraintMap, "order", 0));
  301. data->setSkinRequired(Json::getBoolean(constraintMap, "skin", false));
  302. boneMap = Json::getItem(constraintMap, "bones");
  303. data->_bones.ensureCapacity(boneMap->_size);
  304. data->_bones.setSize(boneMap->_size, 0);
  305. for (boneMap = boneMap->_child, ii = 0; boneMap; boneMap = boneMap->_next, ++ii) {
  306. data->_bones[ii] = skeletonData->findBone(boneMap->_valueString);
  307. if (!data->_bones[ii]) {
  308. delete skeletonData;
  309. setError(root, "Path bone not found: ", boneMap->_valueString);
  310. return NULL;
  311. }
  312. }
  313. name = Json::getString(constraintMap, "target", 0);
  314. data->_target = skeletonData->findSlot(name);
  315. if (!data->_target) {
  316. delete skeletonData;
  317. setError(root, "Target slot not found: ", name);
  318. return NULL;
  319. }
  320. item = Json::getString(constraintMap, "positionMode", "percent");
  321. if (strcmp(item, "fixed") == 0) {
  322. data->_positionMode = PositionMode_Fixed;
  323. } else if (strcmp(item, "percent") == 0) {
  324. data->_positionMode = PositionMode_Percent;
  325. }
  326. item = Json::getString(constraintMap, "spacingMode", "length");
  327. if (strcmp(item, "length") == 0) data->_spacingMode = SpacingMode_Length;
  328. else if (strcmp(item, "fixed") == 0) data->_spacingMode = SpacingMode_Fixed;
  329. else if (strcmp(item, "percent") == 0) data->_spacingMode = SpacingMode_Percent;
  330. item = Json::getString(constraintMap, "rotateMode", "tangent");
  331. if (strcmp(item, "tangent") == 0) data->_rotateMode = RotateMode_Tangent;
  332. else if (strcmp(item, "chain") == 0) data->_rotateMode = RotateMode_Chain;
  333. else if (strcmp(item, "chainScale") == 0) data->_rotateMode = RotateMode_ChainScale;
  334. data->_offsetRotation = Json::getFloat(constraintMap, "rotation", 0);
  335. data->_position = Json::getFloat(constraintMap, "position", 0);
  336. if (data->_positionMode == PositionMode_Fixed) data->_position *= _scale;
  337. data->_spacing = Json::getFloat(constraintMap, "spacing", 0);
  338. if (data->_spacingMode == SpacingMode_Length || data->_spacingMode == SpacingMode_Fixed) data->_spacing *= _scale;
  339. data->_rotateMix = Json::getFloat(constraintMap, "rotateMix", 1);
  340. data->_translateMix = Json::getFloat(constraintMap, "translateMix", 1);
  341. skeletonData->_pathConstraints[i] = data;
  342. }
  343. }
  344. /* Skins. */
  345. skins = Json::getItem(root, "skins");
  346. if (skins) {
  347. Json *skinMap;
  348. skeletonData->_skins.ensureCapacity(skins->_size);
  349. skeletonData->_skins.setSize(skins->_size, 0);
  350. int skinsIndex = 0;
  351. for (skinMap = skins->_child, i = 0; skinMap; skinMap = skinMap->_next, ++i) {
  352. Json *attachmentsMap;
  353. Json *curves;
  354. Skin *skin = nullptr;
  355. auto skinName = Json::getString(skinMap, "name", "");
  356. if (strlen(skinName) == 0) {
  357. skinName = skinMap->_name;
  358. }
  359. skin = new(__FILE__, __LINE__) Skin(skinName);
  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(skinName, "default") == 0) {
  410. skeletonData->_defaultSkin = skin;
  411. }
  412. auto attachmentsData = Json::getItem(skinMap, "attachments");
  413. if (!attachmentsData) {
  414. attachmentsData = skinMap;
  415. }
  416. for (attachmentsMap = attachmentsData->_child; attachmentsMap; attachmentsMap = attachmentsMap->_next) {
  417. SlotData* slot = skeletonData->findSlot(attachmentsMap->_name);
  418. Json *attachmentMap;
  419. for (attachmentMap = attachmentsMap->_child; attachmentMap; attachmentMap = attachmentMap->_next) {
  420. Attachment *attachment = NULL;
  421. const char *skinAttachmentName = attachmentMap->_name;
  422. const char *attachmentName = Json::getString(attachmentMap, "name", skinAttachmentName);
  423. const char *attachmentPath = Json::getString(attachmentMap, "path", attachmentName);
  424. const char *color;
  425. Json *entry;
  426. const char *typeString = Json::getString(attachmentMap, "type", "region");
  427. AttachmentType type;
  428. if (strcmp(typeString, "region") == 0) type = AttachmentType_Region;
  429. else if (strcmp(typeString, "mesh") == 0) type = AttachmentType_Mesh;
  430. else if (strcmp(typeString, "linkedmesh") == 0) type = AttachmentType_Linkedmesh;
  431. else if (strcmp(typeString, "boundingbox") == 0) type = AttachmentType_Boundingbox;
  432. else if (strcmp(typeString, "path") == 0) type = AttachmentType_Path;
  433. else if (strcmp(typeString, "clipping") == 0) type = AttachmentType_Clipping;
  434. else if (strcmp(typeString, "point") == 0) type = AttachmentType_Point;
  435. else {
  436. delete skeletonData;
  437. setError(root, "Unknown attachment type: ", typeString);
  438. return NULL;
  439. }
  440. switch (type) {
  441. case AttachmentType_Region: {
  442. attachment = _attachmentLoader->newRegionAttachment(*skin, attachmentName, attachmentPath);
  443. if (!attachment) {
  444. // delete skeletonData;
  445. // setError(root, "Error reading attachment: ", skinAttachmentName);
  446. // return NULL;
  447. continue;
  448. }
  449. RegionAttachment *region = static_cast<RegionAttachment *>(attachment);
  450. region->_path = attachmentPath;
  451. region->_x = Json::getFloat(attachmentMap, "x", 0) * _scale;
  452. region->_y = Json::getFloat(attachmentMap, "y", 0) * _scale;
  453. region->_scaleX = Json::getFloat(attachmentMap, "scaleX", 1);
  454. region->_scaleY = Json::getFloat(attachmentMap, "scaleY", 1);
  455. region->_rotation = Json::getFloat(attachmentMap, "rotation", 0);
  456. region->_width = Json::getFloat(attachmentMap, "width", 32) * _scale;
  457. region->_height = Json::getFloat(attachmentMap, "height", 32) * _scale;
  458. color = Json::getString(attachmentMap, "color", 0);
  459. if (color) {
  460. region->getColor().r = toColor(color, 0);
  461. region->getColor().g = toColor(color, 1);
  462. region->getColor().b = toColor(color, 2);
  463. region->getColor().a = toColor(color, 3);
  464. }
  465. region->updateOffset();
  466. _attachmentLoader->configureAttachment(region);
  467. break;
  468. }
  469. case AttachmentType_Mesh:
  470. case AttachmentType_Linkedmesh: {
  471. attachment = _attachmentLoader->newMeshAttachment(*skin, attachmentName, attachmentPath);
  472. if (!attachment) {
  473. // delete skeletonData;
  474. // setError(root, "Error reading attachment: ", skinAttachmentName);
  475. // return NULL;
  476. continue;
  477. }
  478. MeshAttachment *mesh = static_cast<MeshAttachment *>(attachment);
  479. mesh->_path = attachmentPath;
  480. color = Json::getString(attachmentMap, "color", 0);
  481. if (color) {
  482. mesh->getColor().r = toColor(color, 0);
  483. mesh->getColor().g = toColor(color, 1);
  484. mesh->getColor().b = toColor(color, 2);
  485. mesh->getColor().a = toColor(color, 3);
  486. }
  487. mesh->_width = Json::getFloat(attachmentMap, "width", 32) * _scale;
  488. mesh->_height = Json::getFloat(attachmentMap, "height", 32) * _scale;
  489. entry = Json::getItem(attachmentMap, "parent");
  490. if (!entry) {
  491. int verticesLength;
  492. entry = Json::getItem(attachmentMap, "triangles");
  493. mesh->_triangles.ensureCapacity(entry->_size);
  494. mesh->_triangles.setSize(entry->_size, 0);
  495. for (entry = entry->_child, ii = 0; entry; entry = entry->_next, ++ii)
  496. mesh->_triangles[ii] = (unsigned short) entry->_valueInt;
  497. entry = Json::getItem(attachmentMap, "uvs");
  498. verticesLength = entry->_size;
  499. mesh->_regionUVs.ensureCapacity(verticesLength);
  500. mesh->_regionUVs.setSize(verticesLength, 0);
  501. for (entry = entry->_child, ii = 0; entry; entry = entry->_next, ++ii)
  502. mesh->_regionUVs[ii] = entry->_valueFloat;
  503. readVertices(attachmentMap, mesh, verticesLength);
  504. mesh->updateUVs();
  505. mesh->_hullLength = Json::getInt(attachmentMap, "hull", 0);
  506. entry = Json::getItem(attachmentMap, "edges");
  507. if (entry) {
  508. mesh->_edges.ensureCapacity(entry->_size);
  509. mesh->_edges.setSize(entry->_size, 0);
  510. for (entry = entry->_child, ii = 0; entry; entry = entry->_next, ++ii)
  511. mesh->_edges[ii] = entry->_valueInt;
  512. }
  513. _attachmentLoader->configureAttachment(mesh);
  514. } else {
  515. bool inheritDeform = Json::getInt(attachmentMap, "deform", 1) ? true : false;
  516. LinkedMesh *linkedMesh = new(__FILE__, __LINE__) LinkedMesh(mesh,
  517. String(Json::getString(attachmentMap, "skin", 0)), slot->getIndex(), String(entry->_valueString),
  518. inheritDeform);
  519. _linkedMeshes.add(linkedMesh);
  520. }
  521. break;
  522. }
  523. case AttachmentType_Boundingbox: {
  524. attachment = _attachmentLoader->newBoundingBoxAttachment(*skin, attachmentName);
  525. BoundingBoxAttachment *box = static_cast<BoundingBoxAttachment *>(attachment);
  526. int vertexCount = Json::getInt(attachmentMap, "vertexCount", 0) << 1;
  527. readVertices(attachmentMap, box, vertexCount);
  528. _attachmentLoader->configureAttachment(attachment);
  529. break;
  530. }
  531. case AttachmentType_Path: {
  532. attachment = _attachmentLoader->newPathAttachment(*skin, attachmentName);
  533. PathAttachment *pathAttatchment = static_cast<PathAttachment *>(attachment);
  534. int vertexCount = 0;
  535. pathAttatchment->_closed = Json::getInt(attachmentMap, "closed", 0) ? true : false;
  536. pathAttatchment->_constantSpeed = Json::getInt(attachmentMap, "constantSpeed", 1) ? true : false;
  537. vertexCount = Json::getInt(attachmentMap, "vertexCount", 0);
  538. readVertices(attachmentMap, pathAttatchment, vertexCount << 1);
  539. pathAttatchment->_lengths.ensureCapacity(vertexCount / 3);
  540. pathAttatchment->_lengths.setSize(vertexCount / 3, 0);
  541. curves = Json::getItem(attachmentMap, "lengths");
  542. for (curves = curves->_child, ii = 0; curves; curves = curves->_next, ++ii)
  543. pathAttatchment->_lengths[ii] = curves->_valueFloat * _scale;
  544. _attachmentLoader->configureAttachment(attachment);
  545. break;
  546. }
  547. case AttachmentType_Point: {
  548. attachment = _attachmentLoader->newPointAttachment(*skin, attachmentName);
  549. PointAttachment *point = static_cast<PointAttachment *>(attachment);
  550. point->_x = Json::getFloat(attachmentMap, "x", 0) * _scale;
  551. point->_y = Json::getFloat(attachmentMap, "y", 0) * _scale;
  552. point->_rotation = Json::getFloat(attachmentMap, "rotation", 0);
  553. _attachmentLoader->configureAttachment(attachment);
  554. break;
  555. }
  556. case AttachmentType_Clipping: {
  557. attachment = _attachmentLoader->newClippingAttachment(*skin, attachmentName);
  558. ClippingAttachment *clip = static_cast<ClippingAttachment *>(attachment);
  559. int vertexCount = 0;
  560. const char *end = Json::getString(attachmentMap, "end", 0);
  561. if (end) clip->_endSlot = skeletonData->findSlot(end);
  562. vertexCount = Json::getInt(attachmentMap, "vertexCount", 0) << 1;
  563. readVertices(attachmentMap, clip, vertexCount);
  564. _attachmentLoader->configureAttachment(attachment);
  565. break;
  566. }
  567. }
  568. skin->setAttachment(slot->getIndex(), skinAttachmentName, attachment);
  569. }
  570. }
  571. }
  572. }
  573. /* Linked meshes. */
  574. int n = _linkedMeshes.size();
  575. for (i = 0; i < n; ++i) {
  576. LinkedMesh *linkedMesh = _linkedMeshes[i];
  577. Skin *skin = linkedMesh->_skin.length() == 0 ? skeletonData->getDefaultSkin() : skeletonData->findSkin(linkedMesh->_skin);
  578. if (skin == NULL) {
  579. // delete skeletonData;
  580. // setError(root, "Skin not found: ", linkedMesh->_skin.buffer());
  581. // return NULL;
  582. continue;
  583. }
  584. Attachment *parent = skin->getAttachment(linkedMesh->_slotIndex, linkedMesh->_parent);
  585. if (parent == NULL) {
  586. // delete skeletonData;
  587. // setError(root, "Parent mesh not found: ", linkedMesh->_parent.buffer());
  588. // return NULL;
  589. continue;
  590. }
  591. linkedMesh->_mesh->_deformAttachment = linkedMesh->_inheritDeform ? static_cast<VertexAttachment*>(parent) : linkedMesh->_mesh;
  592. linkedMesh->_mesh->setParentMesh(static_cast<MeshAttachment *>(parent));
  593. linkedMesh->_mesh->updateUVs();
  594. _attachmentLoader->configureAttachment(linkedMesh->_mesh);
  595. }
  596. ContainerUtil::cleanUpVectorOfPointers(_linkedMeshes);
  597. _linkedMeshes.clear();
  598. /* Events. */
  599. events = Json::getItem(root, "events");
  600. if (events) {
  601. Json *eventMap;
  602. skeletonData->_events.ensureCapacity(events->_size);
  603. skeletonData->_events.setSize(events->_size, 0);
  604. for (eventMap = events->_child, i = 0; eventMap; eventMap = eventMap->_next, ++i) {
  605. EventData *eventData = new(__FILE__, __LINE__) EventData(String(eventMap->_name));
  606. eventData->_intValue = Json::getInt(eventMap, "int", 0);
  607. eventData->_floatValue = Json::getFloat(eventMap, "float", 0);
  608. const char *stringValue = Json::getString(eventMap, "string", 0);
  609. eventData->_stringValue = stringValue;
  610. const char *audioPath = Json::getString(eventMap, "audio", 0);
  611. eventData->_audioPath = audioPath;
  612. if (audioPath) {
  613. eventData->_volume = Json::getFloat(eventMap, "volume", 1);
  614. eventData->_balance = Json::getFloat(eventMap, "balance", 0);
  615. }
  616. skeletonData->_events[i] = eventData;
  617. }
  618. }
  619. /* Animations. */
  620. animations = Json::getItem(root, "animations");
  621. if (animations) {
  622. Json *animationMap;
  623. skeletonData->_animations.ensureCapacity(animations->_size);
  624. skeletonData->_animations.setSize(animations->_size, 0);
  625. int animationsIndex = 0;
  626. for (animationMap = animations->_child; animationMap; animationMap = animationMap->_next) {
  627. Animation *animation = readAnimation(animationMap, skeletonData);
  628. if (!animation) {
  629. // delete skeletonData;
  630. // delete root;
  631. // return NULL;
  632. continue;
  633. }
  634. skeletonData->_animations[animationsIndex++] = animation;
  635. }
  636. }
  637. delete root;
  638. return skeletonData;
  639. }
  640. float SkeletonJson::toColor(const char *value, size_t index) {
  641. char digits[3];
  642. char *error;
  643. int color;
  644. if (index >= strlen(value) / 2) return -1;
  645. value += index * 2;
  646. digits[0] = *value;
  647. digits[1] = *(value + 1);
  648. digits[2] = '\0';
  649. color = (int) strtoul(digits, &error, 16);
  650. if (*error != 0) return -1;
  651. return color / (float) 255;
  652. }
  653. void SkeletonJson::readCurve(Json *frame, CurveTimeline *timeline, size_t frameIndex) {
  654. Json *curve = Json::getItem(frame, "curve");
  655. if (!curve) return;
  656. if (curve->_type == Json::JSON_STRING && strcmp(curve->_valueString, "stepped") == 0)
  657. timeline->setStepped(frameIndex);
  658. else {
  659. float c1 = Json::getFloat(frame, "curve", 0);
  660. float c2 = Json::getFloat(frame, "c2", 0);
  661. float c3 = Json::getFloat(frame, "c3", 1);
  662. float c4 = Json::getFloat(frame, "c4", 1);
  663. timeline->setCurve(frameIndex, c1, c2, c3, c4);
  664. }
  665. }
  666. Animation *SkeletonJson::readAnimation(Json *root, SkeletonData *skeletonData) {
  667. Vector<Timeline *> timelines;
  668. float duration = 0;
  669. size_t frameIndex;
  670. Json *valueMap;
  671. int timelinesCount = 0;
  672. Json *bones = Json::getItem(root, "bones");
  673. Json *slots = Json::getItem(root, "slots");
  674. Json *ik = Json::getItem(root, "ik");
  675. Json *transform = Json::getItem(root, "transform");
  676. Json *paths = Json::getItem(root, "path");
  677. if (paths == NULL) {
  678. paths = Json::getItem(root, "paths");
  679. }
  680. Json *deform = Json::getItem(root, "deform");
  681. Json *drawOrder = Json::getItem(root, "drawOrder");
  682. Json *events = Json::getItem(root, "events");
  683. Json *boneMap, *slotMap, *constraintMap;
  684. if (!drawOrder) drawOrder = Json::getItem(root, "draworder");
  685. for (boneMap = bones ? bones->_child : NULL; boneMap; boneMap = boneMap->_next)
  686. timelinesCount += boneMap->_size;
  687. for (slotMap = slots ? slots->_child : NULL; slotMap; slotMap = slotMap->_next)
  688. timelinesCount += slotMap->_size;
  689. timelinesCount += ik ? ik->_size : 0;
  690. timelinesCount += transform ? transform->_size : 0;
  691. for (constraintMap = paths ? paths->_child : NULL; constraintMap; constraintMap = constraintMap->_next)
  692. timelinesCount += constraintMap->_size;
  693. for (constraintMap = deform ? deform->_child : NULL; constraintMap; constraintMap = constraintMap->_next)
  694. for (slotMap = constraintMap->_child; slotMap; slotMap = slotMap->_next)
  695. timelinesCount += slotMap->_size;
  696. if (drawOrder) ++timelinesCount;
  697. if (events) ++timelinesCount;
  698. /** Slot timelines. */
  699. for (slotMap = slots ? slots->_child : 0; slotMap; slotMap = slotMap->_next) {
  700. Json *timelineMap;
  701. int slotIndex = skeletonData->findSlotIndex(slotMap->_name);
  702. if (slotIndex == -1) {
  703. ContainerUtil::cleanUpVectorOfPointers(timelines);
  704. setError(NULL, "Slot not found: ", slotMap->_name);
  705. return NULL;
  706. }
  707. for (timelineMap = slotMap->_child; timelineMap; timelineMap = timelineMap->_next) {
  708. if (strcmp(timelineMap->_name, "attachment") == 0) {
  709. AttachmentTimeline *timeline = new(__FILE__, __LINE__) AttachmentTimeline(timelineMap->_size);
  710. timeline->_slotIndex = slotIndex;
  711. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  712. Json *name = Json::getItem(valueMap, "name");
  713. String attachmentName = name->_type == Json::JSON_NULL ? "" : name->_valueString;
  714. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), attachmentName);
  715. }
  716. timelines.add(timeline);
  717. timelinesCount++;
  718. duration = MathUtil::max(duration, timeline->_frames[timelineMap->_size - 1]);
  719. } else if (strcmp(timelineMap->_name, "color") == 0) {
  720. ColorTimeline *timeline = new(__FILE__, __LINE__) ColorTimeline(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, "color", 0);
  724. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), toColor(s, 0), toColor(s, 1),
  725. toColor(s, 2), toColor(s, 3));
  726. readCurve(valueMap, timeline, frameIndex);
  727. }
  728. timelines.add(timeline);
  729. timelinesCount++;
  730. duration = MathUtil::max(duration, timeline->_frames[(timelineMap->_size - 1) * ColorTimeline::ENTRIES]);
  731. } else if (strcmp(timelineMap->_name, "twoColor") == 0) {
  732. TwoColorTimeline *timeline = new(__FILE__, __LINE__) TwoColorTimeline(timelineMap->_size);
  733. timeline->_slotIndex = slotIndex;
  734. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  735. const char *s = Json::getString(valueMap, "light", 0);
  736. const char *ds = Json::getString(valueMap, "dark", 0);
  737. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), toColor(s, 0), toColor(s, 1),
  738. toColor(s, 2), toColor(s, 3), toColor(ds, 0), toColor(ds, 1), toColor(ds, 2));
  739. readCurve(valueMap, timeline, frameIndex);
  740. }
  741. timelines.add(timeline);
  742. timelinesCount++;
  743. duration = MathUtil::max(duration,
  744. timeline->_frames[(timelineMap->_size - 1) * TwoColorTimeline::ENTRIES]);
  745. } else {
  746. ContainerUtil::cleanUpVectorOfPointers(timelines);
  747. setError(NULL, "Invalid timeline type for a slot: ", timelineMap->_name);
  748. return NULL;
  749. }
  750. }
  751. }
  752. /** Bone timelines. */
  753. for (boneMap = bones ? bones->_child : 0; boneMap; boneMap = boneMap->_next) {
  754. Json *timelineMap;
  755. int boneIndex = skeletonData->findBoneIndex(boneMap->_name);
  756. if (boneIndex == -1) {
  757. ContainerUtil::cleanUpVectorOfPointers(timelines);
  758. setError(NULL, "Bone not found: ", boneMap->_name);
  759. return NULL;
  760. }
  761. for (timelineMap = boneMap->_child; timelineMap; timelineMap = timelineMap->_next) {
  762. if (strcmp(timelineMap->_name, "rotate") == 0) {
  763. RotateTimeline *timeline = new(__FILE__, __LINE__) RotateTimeline(timelineMap->_size);
  764. timeline->_boneIndex = boneIndex;
  765. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  766. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), Json::getFloat(valueMap, "angle", 0));
  767. readCurve(valueMap, timeline, frameIndex);
  768. }
  769. timelines.add(timeline);
  770. timelinesCount++;
  771. duration = MathUtil::max(duration, timeline->_frames[(timelineMap->_size - 1) * RotateTimeline::ENTRIES]);
  772. } else {
  773. int isScale = strcmp(timelineMap->_name, "scale") == 0;
  774. int isTranslate = strcmp(timelineMap->_name, "translate") == 0;
  775. int isShear = strcmp(timelineMap->_name, "shear") == 0;
  776. if (isScale || isTranslate || isShear) {
  777. float timelineScale = isTranslate ? _scale : 1;
  778. float defaultValue = 0;
  779. TranslateTimeline *timeline = 0;
  780. if (isScale) {
  781. timeline = new(__FILE__, __LINE__) ScaleTimeline(timelineMap->_size);
  782. defaultValue = 1;
  783. } else if (isTranslate) {
  784. timeline = new(__FILE__, __LINE__) TranslateTimeline(timelineMap->_size);
  785. } else if (isShear) {
  786. timeline = new(__FILE__, __LINE__) ShearTimeline(timelineMap->_size);
  787. }
  788. timeline->_boneIndex = boneIndex;
  789. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  790. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0),
  791. Json::getFloat(valueMap, "x", defaultValue) * timelineScale,
  792. Json::getFloat(valueMap, "y", defaultValue) * timelineScale);
  793. readCurve(valueMap, timeline, frameIndex);
  794. }
  795. timelines.add(timeline);
  796. timelinesCount++;
  797. duration = MathUtil::max(duration, timeline->_frames[(timelineMap->_size - 1) * TranslateTimeline::ENTRIES]);
  798. } else {
  799. ContainerUtil::cleanUpVectorOfPointers(timelines);
  800. setError(NULL, "Invalid timeline type for a bone: ", timelineMap->_name);
  801. return NULL;
  802. }
  803. }
  804. }
  805. }
  806. /** IK constraint timelines. */
  807. for (constraintMap = ik ? ik->_child : 0; constraintMap; constraintMap = constraintMap->_next) {
  808. IkConstraintData *constraint = skeletonData->findIkConstraint(constraintMap->_name);
  809. IkConstraintTimeline *timeline = new(__FILE__, __LINE__) IkConstraintTimeline(constraintMap->_size);
  810. for (frameIndex = 0; frameIndex < skeletonData->_ikConstraints.size(); ++frameIndex) {
  811. if (constraint == skeletonData->_ikConstraints[frameIndex]) {
  812. timeline->_ikConstraintIndex = frameIndex;
  813. break;
  814. }
  815. }
  816. for (valueMap = constraintMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  817. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), Json::getFloat(valueMap, "mix", 1),
  818. Json::getFloat(valueMap, "softness", 0) * _scale, Json::getInt(valueMap, "bendPositive", 1) ? 1 : -1,
  819. Json::getInt(valueMap, "compress", 0) ? true : false, Json::getInt(valueMap, "stretch", 0) ? true : false);
  820. readCurve(valueMap, timeline, frameIndex);
  821. }
  822. timelines.add(timeline);
  823. timelinesCount++;
  824. duration = MathUtil::max(duration, timeline->_frames[(constraintMap->_size - 1) * IkConstraintTimeline::ENTRIES]);
  825. }
  826. /** Transform constraint timelines. */
  827. for (constraintMap = transform ? transform->_child : 0; constraintMap; constraintMap = constraintMap->_next) {
  828. TransformConstraintData *constraint = skeletonData->findTransformConstraint(constraintMap->_name);
  829. TransformConstraintTimeline *timeline = new(__FILE__, __LINE__) TransformConstraintTimeline(constraintMap->_size);
  830. for (frameIndex = 0; frameIndex < skeletonData->_transformConstraints.size(); ++frameIndex) {
  831. if (constraint == skeletonData->_transformConstraints[frameIndex]) {
  832. timeline->_transformConstraintIndex = frameIndex;
  833. break;
  834. }
  835. }
  836. for (valueMap = constraintMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  837. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0),
  838. Json::getFloat(valueMap, "rotateMix", 1), Json::getFloat(valueMap, "translateMix", 1),
  839. Json::getFloat(valueMap, "scaleMix", 1), Json::getFloat(valueMap, "shearMix", 1));
  840. readCurve(valueMap, timeline, frameIndex);
  841. }
  842. timelines.add(timeline);
  843. timelinesCount++;
  844. duration = MathUtil::max(duration, timeline->_frames[(constraintMap->_size - 1) * TransformConstraintTimeline::ENTRIES]);
  845. }
  846. /** Path constraint timelines. */
  847. for (constraintMap = paths ? paths->_child : 0; constraintMap; constraintMap = constraintMap->_next) {
  848. size_t constraintIndex = 0, i;
  849. Json *timelineMap;
  850. PathConstraintData *data = skeletonData->findPathConstraint(constraintMap->_name);
  851. if (!data) {
  852. ContainerUtil::cleanUpVectorOfPointers(timelines);
  853. setError(NULL, "Path constraint not found: ", constraintMap->_name);
  854. return NULL;
  855. }
  856. for (i = 0; i < skeletonData->_pathConstraints.size(); i++) {
  857. if (skeletonData->_pathConstraints[i] == data) {
  858. constraintIndex = i;
  859. break;
  860. }
  861. }
  862. for (timelineMap = constraintMap->_child; timelineMap; timelineMap = timelineMap->_next) {
  863. const char *timelineName = timelineMap->_name;
  864. if (strcmp(timelineName, "position") == 0 || strcmp(timelineName, "spacing") == 0) {
  865. PathConstraintPositionTimeline *timeline;
  866. float timelineScale = 1;
  867. if (strcmp(timelineName, "spacing") == 0) {
  868. timeline = new(__FILE__, __LINE__) PathConstraintSpacingTimeline(timelineMap->_size);
  869. if (data->_spacingMode == SpacingMode_Length || data->_spacingMode == SpacingMode_Fixed) {
  870. timelineScale = _scale;
  871. }
  872. } else {
  873. timeline = new(__FILE__, __LINE__) PathConstraintPositionTimeline(timelineMap->_size);
  874. if (data->_positionMode == PositionMode_Fixed) {
  875. timelineScale = _scale;
  876. }
  877. }
  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, timelineName, 0) * timelineScale);
  882. readCurve(valueMap, timeline, frameIndex);
  883. }
  884. timelines.add(timeline);
  885. timelinesCount++;
  886. duration = MathUtil::max(duration, timeline->_frames[(timelineMap->_size - 1) *
  887. PathConstraintPositionTimeline::ENTRIES]);
  888. } else if (strcmp(timelineName, "mix") == 0) {
  889. PathConstraintMixTimeline *timeline = new(__FILE__, __LINE__) PathConstraintMixTimeline(timelineMap->_size);
  890. timeline->_pathConstraintIndex = constraintIndex;
  891. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  892. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0),
  893. Json::getFloat(valueMap, "rotateMix", 1),
  894. Json::getFloat(valueMap, "translateMix", 1));
  895. readCurve(valueMap, timeline, frameIndex);
  896. }
  897. timelines.add(timeline);
  898. timelinesCount++;
  899. duration = MathUtil::max(duration, timeline->_frames[(timelineMap->_size - 1) * PathConstraintMixTimeline::ENTRIES]);
  900. }
  901. }
  902. }
  903. /** Deform timelines. */
  904. for (constraintMap = deform ? deform->_child : NULL; constraintMap; constraintMap = constraintMap->_next) {
  905. Skin *skin = skeletonData->findSkin(constraintMap->_name);
  906. for (slotMap = constraintMap->_child; slotMap; slotMap = slotMap->_next) {
  907. int slotIndex = skeletonData->findSlotIndex(slotMap->_name);
  908. Json *timelineMap;
  909. for (timelineMap = slotMap->_child; timelineMap; timelineMap = timelineMap->_next) {
  910. DeformTimeline *timeline;
  911. int weighted, deformLength;
  912. Attachment *baseAttachment = skin->getAttachment(slotIndex, timelineMap->_name);
  913. if (!baseAttachment) {
  914. ContainerUtil::cleanUpVectorOfPointers(timelines);
  915. // setError(NULL, "Attachment not found: ", timelineMap->_name);
  916. // return NULL;
  917. continue;
  918. }
  919. VertexAttachment *attachment = static_cast<VertexAttachment *>(baseAttachment);
  920. weighted = attachment->_bones.size() != 0;
  921. Vector<float> &verts = attachment->_vertices;
  922. deformLength = weighted ? verts.size() / 3 * 2 : verts.size();
  923. timeline = new(__FILE__, __LINE__) DeformTimeline(timelineMap->_size);
  924. timeline->_slotIndex = slotIndex;
  925. timeline->_attachment = attachment;
  926. for (valueMap = timelineMap->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  927. Json *vertices = Json::getItem(valueMap, "vertices");
  928. Vector<float> deformed;
  929. if (!vertices) {
  930. if (weighted) {
  931. deformed.setSize(deformLength, 0);
  932. } else {
  933. deformed.clearAndAddAll(attachment->_vertices);
  934. }
  935. } else {
  936. int v, start = Json::getInt(valueMap, "offset", 0);
  937. Json *vertex;
  938. deformed.setSize(deformLength, 0);
  939. if (_scale == 1) {
  940. for (vertex = vertices->_child, v = start; vertex; vertex = vertex->_next, ++v) {
  941. deformed[v] = vertex->_valueFloat;
  942. }
  943. } else {
  944. for (vertex = vertices->_child, v = start; vertex; vertex = vertex->_next, ++v) {
  945. deformed[v] = vertex->_valueFloat * _scale;
  946. }
  947. }
  948. if (!weighted) {
  949. Vector<float> &verticesAttachment = attachment->_vertices;
  950. for (v = 0; v < deformLength; ++v) {
  951. deformed[v] += verticesAttachment[v];
  952. }
  953. }
  954. }
  955. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), deformed);
  956. readCurve(valueMap, timeline, frameIndex);
  957. }
  958. timelines.add(timeline);
  959. timelinesCount++;
  960. duration = MathUtil::max(duration, timeline->_frames[timelineMap->_size - 1]);
  961. }
  962. }
  963. }
  964. /** Draw order timeline. */
  965. if (drawOrder) {
  966. DrawOrderTimeline *timeline = new(__FILE__, __LINE__) DrawOrderTimeline(drawOrder->_size);
  967. for (valueMap = drawOrder->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  968. int ii;
  969. Vector<int> drawOrder2;
  970. Json *offsets = Json::getItem(valueMap, "offsets");
  971. if (offsets) {
  972. Json *offsetMap;
  973. Vector<int> unchanged;
  974. unchanged.ensureCapacity(skeletonData->_slots.size() - offsets->_size);
  975. unchanged.setSize(skeletonData->_slots.size() - offsets->_size, 0);
  976. size_t originalIndex = 0, unchangedIndex = 0;
  977. drawOrder2.ensureCapacity(skeletonData->_slots.size());
  978. drawOrder2.setSize(skeletonData->_slots.size(), 0);
  979. for (ii = (int)skeletonData->_slots.size() - 1; ii >= 0; --ii)
  980. drawOrder2[ii] = -1;
  981. for (offsetMap = offsets->_child; offsetMap; offsetMap = offsetMap->_next) {
  982. int slotIndex = skeletonData->findSlotIndex(Json::getString(offsetMap, "slot", 0));
  983. if (slotIndex == -1) {
  984. ContainerUtil::cleanUpVectorOfPointers(timelines);
  985. setError(NULL, "Slot not found: ", Json::getString(offsetMap, "slot", 0));
  986. return NULL;
  987. }
  988. /* Collect unchanged items. */
  989. while (originalIndex != (size_t)slotIndex)
  990. unchanged[unchangedIndex++] = originalIndex++;
  991. /* Set changed items. */
  992. drawOrder2[originalIndex + Json::getInt(offsetMap, "offset", 0)] = originalIndex;
  993. originalIndex++;
  994. }
  995. /* Collect remaining unchanged items. */
  996. while (originalIndex < skeletonData->_slots.size())
  997. unchanged[unchangedIndex++] = originalIndex++;
  998. /* Fill in unchanged items. */
  999. for (ii = (int)skeletonData->_slots.size() - 1; ii >= 0; ii--)
  1000. if (drawOrder2[ii] == -1) drawOrder2[ii] = unchanged[--unchangedIndex];
  1001. }
  1002. timeline->setFrame(frameIndex, Json::getFloat(valueMap, "time", 0), drawOrder2);
  1003. }
  1004. timelines.add(timeline);
  1005. timelinesCount++;
  1006. duration = MathUtil::max(duration, timeline->_frames[drawOrder->_size - 1]);
  1007. }
  1008. /** Event timeline. */
  1009. if (events) {
  1010. EventTimeline *timeline = new(__FILE__, __LINE__) EventTimeline(events->_size);
  1011. for (valueMap = events->_child, frameIndex = 0; valueMap; valueMap = valueMap->_next, ++frameIndex) {
  1012. Event *event;
  1013. EventData *eventData = skeletonData->findEvent(Json::getString(valueMap, "name", 0));
  1014. if (!eventData) {
  1015. ContainerUtil::cleanUpVectorOfPointers(timelines);
  1016. setError(NULL, "Event not found: ", Json::getString(valueMap, "name", 0));
  1017. return NULL;
  1018. }
  1019. event = new(__FILE__, __LINE__) Event(Json::getFloat(valueMap, "time", 0), *eventData);
  1020. event->_intValue = Json::getInt(valueMap, "int", eventData->_intValue);
  1021. event->_floatValue = Json::getFloat(valueMap, "float", eventData->_floatValue);
  1022. event->_stringValue = Json::getString(valueMap, "string", eventData->_stringValue.buffer());
  1023. if (!eventData->_audioPath.isEmpty()) {
  1024. event->_volume = Json::getFloat(valueMap, "volume", 1);
  1025. event->_balance = Json::getFloat(valueMap, "balance", 0);
  1026. }
  1027. timeline->setFrame(frameIndex, event);
  1028. }
  1029. timelines.add(timeline);
  1030. timelinesCount++;
  1031. duration = MathUtil::max(duration, timeline->_frames[events->_size - 1]);
  1032. }
  1033. return new(__FILE__, __LINE__) Animation(String(root->_name), timelines, duration);
  1034. }
  1035. void SkeletonJson::readVertices(Json *attachmentMap, VertexAttachment *attachment, size_t verticesLength) {
  1036. Json *entry;
  1037. size_t i, n, nn, entrySize;
  1038. Vector<float> vertices;
  1039. attachment->setWorldVerticesLength(verticesLength);
  1040. entry = Json::getItem(attachmentMap, "vertices");
  1041. entrySize = entry->_size;
  1042. vertices.ensureCapacity(entrySize);
  1043. vertices.setSize(entrySize, 0);
  1044. for (entry = entry->_child, i = 0; entry; entry = entry->_next, ++i)
  1045. vertices[i] = entry->_valueFloat;
  1046. if (verticesLength == entrySize) {
  1047. if (_scale != 1) {
  1048. for (i = 0; i < entrySize; ++i)
  1049. vertices[i] *= _scale;
  1050. }
  1051. attachment->getVertices().clearAndAddAll(vertices);
  1052. return;
  1053. }
  1054. Vertices bonesAndWeights;
  1055. bonesAndWeights._bones.ensureCapacity(verticesLength * 3);
  1056. bonesAndWeights._vertices.ensureCapacity(verticesLength * 3 * 3);
  1057. for (i = 0, n = entrySize; i < n;) {
  1058. int boneCount = (int) vertices[i++];
  1059. bonesAndWeights._bones.add(boneCount);
  1060. for (nn = i + boneCount * 4; i < nn; i += 4) {
  1061. bonesAndWeights._bones.add((int) vertices[i]);
  1062. bonesAndWeights._vertices.add(vertices[i + 1] * _scale);
  1063. bonesAndWeights._vertices.add(vertices[i + 2] * _scale);
  1064. bonesAndWeights._vertices.add(vertices[i + 3]);
  1065. }
  1066. }
  1067. attachment->getVertices().clearAndAddAll(bonesAndWeights._vertices);
  1068. attachment->getBones().clearAndAddAll(bonesAndWeights._bones);
  1069. }
  1070. void SkeletonJson::setError(Json *root, const String &value1, const String &value2) {
  1071. _error = String(value1).append(value2);
  1072. delete root;
  1073. }