SpriterData2D.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Math/MathDefs.h"
  24. #include "../Urho2D/SpriterData2D.h"
  25. #include <PugiXml/pugixml.hpp>
  26. #include <cstring>
  27. using namespace pugi;
  28. namespace Urho3D
  29. {
  30. namespace Spriter
  31. {
  32. SpriterData::~SpriterData()
  33. {
  34. Reset();
  35. }
  36. void SpriterData::Reset()
  37. {
  38. if (!folders_.Empty())
  39. {
  40. for (unsigned i = 0; i < folders_.Size(); ++i)
  41. delete folders_[i];
  42. folders_.Clear();
  43. }
  44. if (!entities_.Empty())
  45. {
  46. for (unsigned i = 0; i < entities_.Size(); ++i)
  47. delete entities_[i];
  48. entities_.Clear();
  49. }
  50. }
  51. bool SpriterData::Load(const pugi::xml_node& node)
  52. {
  53. Reset();
  54. if (strcmp(node.name(), "spriter_data") != 0)
  55. return false;
  56. scmlVersion_ = node.attribute("scml_version").as_int();
  57. generator_ = node.attribute("generator").as_string();
  58. generatorVersion_ = node.attribute("scml_version").as_string();
  59. for (xml_node folderNode = node.child("folder"); !folderNode.empty(); folderNode = folderNode.next_sibling("folder"))
  60. {
  61. folders_.Push(new Folder());
  62. if (!folders_.Back()->Load(folderNode))
  63. return false;
  64. }
  65. for (xml_node entityNode = node.child("entity"); !entityNode.empty(); entityNode = entityNode.next_sibling("entity"))
  66. {
  67. entities_.Push(new Entity());
  68. if (!entities_.Back()->Load(entityNode))
  69. return false;
  70. }
  71. return true;
  72. }
  73. bool SpriterData::Load(const void* data, size_t size)
  74. {
  75. xml_document document;
  76. if (!document.load_buffer(data, size))
  77. return false;
  78. return Load(document.child("spriter_data"));
  79. }
  80. Folder::~Folder()
  81. {
  82. Reset();
  83. }
  84. void Folder::Reset()
  85. {
  86. for (unsigned i = 0; i < files_.Size(); ++i)
  87. delete files_[i];
  88. files_.Clear();
  89. }
  90. bool Folder::Load(const pugi::xml_node& node)
  91. {
  92. Reset();
  93. if (strcmp(node.name(), "folder") != 0)
  94. return false;
  95. id_ = node.attribute("id").as_int();
  96. name_ = node.attribute("name").as_string();
  97. for (xml_node fileNode = node.child("file"); !fileNode.empty(); fileNode = fileNode.next_sibling("file"))
  98. {
  99. files_.Push(new File(this));
  100. if (!files_.Back()->Load(fileNode))
  101. return false;
  102. }
  103. return true;
  104. }
  105. File::File(Folder* folder) :
  106. folder_(folder)
  107. {
  108. }
  109. bool File::Load(const pugi::xml_node& node)
  110. {
  111. if (strcmp(node.name(), "file") != 0)
  112. return false;
  113. id_ = node.attribute("id").as_int();
  114. name_ = node.attribute("name").as_string();
  115. width_ = node.attribute("width").as_float();
  116. height_ = node.attribute("height").as_float();
  117. pivotX_ = node.attribute("pivot_x").as_float(0.0f);
  118. pivotY_ = node.attribute("pivot_y").as_float(1.0f);
  119. return true;
  120. }
  121. Entity::~Entity()
  122. {
  123. Reset();
  124. }
  125. void Entity::Reset()
  126. {
  127. for (unsigned i = 0; i < characterMaps_.Size(); ++i)
  128. delete characterMaps_[i];
  129. characterMaps_.Clear();
  130. for (unsigned i = 0; i < animations_.Size(); ++i)
  131. delete animations_[i];
  132. animations_.Clear();
  133. }
  134. bool Entity::Load(const pugi::xml_node& node)
  135. {
  136. Reset();
  137. if (strcmp(node.name(), "entity") != 0)
  138. return false;
  139. id_ = node.attribute("id").as_int();
  140. name_ = node.attribute("name").as_string();
  141. for (xml_node characterMapNode = node.child("character_map"); !characterMapNode.empty(); characterMapNode = characterMapNode.next_sibling("character_map"))
  142. {
  143. characterMaps_.Push(new CharacterMap());
  144. if (!characterMaps_.Back()->Load(characterMapNode))
  145. return false;
  146. }
  147. for (xml_node animationNode = node.child("animation"); !animationNode.empty(); animationNode = animationNode.next_sibling("animation"))
  148. {
  149. animations_.Push(new Animation());
  150. if (!animations_.Back()->Load(animationNode))
  151. return false;
  152. }
  153. return true;
  154. }
  155. CharacterMap::~CharacterMap()
  156. {
  157. Reset();
  158. }
  159. void CharacterMap::Reset()
  160. {
  161. for (unsigned i = 0; i < maps_.Size(); ++i)
  162. delete maps_[i];
  163. maps_.Clear();
  164. }
  165. bool CharacterMap::Load(const pugi::xml_node& node)
  166. {
  167. Reset();
  168. if (strcmp(node.name(), "character_map") != 0)
  169. return false;
  170. id_ = node.attribute("id").as_int();
  171. name_ = node.attribute("name").as_string();
  172. for (xml_node mapNode = node.child("map"); !mapNode.empty(); mapNode = mapNode.next_sibling("map"))
  173. {
  174. maps_.Push(new MapInstruction());
  175. if (!maps_.Back()->Load(mapNode))
  176. return false;
  177. }
  178. return true;
  179. }
  180. bool MapInstruction::Load(const pugi::xml_node& node)
  181. {
  182. if (strcmp(node.name(), "map") != 0)
  183. return false;
  184. folder_ = node.attribute("folder").as_int();
  185. file_ = node.attribute("file").as_int();
  186. targetFolder_ = node.attribute("target_folder").as_int(-1);
  187. targetFile_ = node.attribute("target_file").as_int(-1);
  188. return true;
  189. }
  190. Animation::~Animation()
  191. {
  192. Reset();
  193. }
  194. void Animation::Reset()
  195. {
  196. if (!mainlineKeys_.Empty())
  197. {
  198. for (unsigned i = 0; i < mainlineKeys_.Size(); ++i)
  199. delete mainlineKeys_[i];
  200. mainlineKeys_.Clear();
  201. }
  202. for (unsigned i = 0; i < timelines_.Size(); ++i)
  203. delete timelines_[i];
  204. timelines_.Clear();
  205. }
  206. bool Animation::Load(const pugi::xml_node& node)
  207. {
  208. Reset();
  209. if (strcmp(node.name(), "animation") != 0)
  210. return false;
  211. id_ = node.attribute("id").as_int();
  212. name_ = node.attribute("name").as_string();
  213. length_ = node.attribute("length").as_float() * 0.001f;
  214. looping_ = node.attribute("looping").as_bool(true);
  215. xml_node mainlineNode = node.child("mainline");
  216. for (xml_node keyNode = mainlineNode.child("key"); !keyNode.empty(); keyNode = keyNode.next_sibling("key"))
  217. {
  218. mainlineKeys_.Push(new MainlineKey());
  219. if (!mainlineKeys_.Back()->Load(keyNode))
  220. return false;
  221. }
  222. for (xml_node timelineNode = node.child("timeline"); !timelineNode.empty(); timelineNode = timelineNode.next_sibling("timeline"))
  223. {
  224. timelines_.Push(new Timeline());
  225. if (!timelines_.Back()->Load(timelineNode))
  226. return false;
  227. }
  228. return true;
  229. }
  230. MainlineKey::~MainlineKey()
  231. {
  232. Reset();
  233. }
  234. void MainlineKey::Reset()
  235. {
  236. for (unsigned i = 0; i < boneRefs_.Size(); ++i)
  237. delete boneRefs_[i];
  238. boneRefs_.Clear();
  239. for (unsigned i = 0; i < objectRefs_.Size(); ++i)
  240. delete objectRefs_[i];
  241. objectRefs_.Clear();
  242. }
  243. bool MainlineKey::Load(const pugi::xml_node& node)
  244. {
  245. id_ = node.attribute("id").as_int();
  246. time_ = node.attribute("time").as_float() * 0.001f;
  247. for (xml_node boneRefNode = node.child("bone_ref"); !boneRefNode.empty(); boneRefNode = boneRefNode.next_sibling("bone_ref"))
  248. {
  249. boneRefs_.Push(new Ref());
  250. if (!boneRefs_.Back()->Load(boneRefNode))
  251. return false;
  252. }
  253. for (xml_node objectRefNode = node.child("object_ref"); !objectRefNode.empty(); objectRefNode = objectRefNode.next_sibling("object_ref"))
  254. {
  255. objectRefs_.Push(new Ref());
  256. if (!objectRefs_.Back()->Load(objectRefNode))
  257. return false;
  258. }
  259. return true;
  260. }
  261. bool Ref::Load(const pugi::xml_node& node)
  262. {
  263. if (strcmp(node.name(), "bone_ref") != 0 && strcmp(node.name(), "object_ref") != 0)
  264. return false;
  265. id_ = node.attribute("id").as_int();
  266. parent_ = node.attribute("parent").as_int(-1);
  267. timeline_ = node.attribute("timeline").as_int();
  268. key_ = node.attribute("key").as_int();
  269. zIndex_ = node.attribute("z_index").as_int();
  270. return true;
  271. }
  272. Timeline::~Timeline()
  273. {
  274. Reset();
  275. }
  276. void Timeline::Reset()
  277. {
  278. for (unsigned i = 0; i < keys_.Size(); ++i)
  279. delete keys_[i];
  280. keys_.Clear();
  281. }
  282. bool Timeline::Load(const pugi::xml_node& node)
  283. {
  284. Reset();
  285. if (strcmp(node.name(), "timeline") != 0)
  286. return false;
  287. id_ = node.attribute("id").as_int();
  288. name_ = node.attribute("name").as_string();
  289. String typeString;
  290. xml_attribute typeAttr = node.attribute("type");
  291. if (typeAttr.empty())
  292. typeString = node.attribute("object_type").as_string("sprite");
  293. else
  294. typeString = typeAttr.as_string("sprite");
  295. if (typeString == "bone")
  296. {
  297. objectType_ = BONE;
  298. for (xml_node keyNode = node.child("key"); !keyNode.empty(); keyNode = keyNode.next_sibling("key"))
  299. {
  300. keys_.Push(new BoneTimelineKey(this));
  301. if (!keys_.Back()->Load(keyNode))
  302. return false;
  303. }
  304. }
  305. else if (typeString == "sprite")
  306. {
  307. objectType_ = SPRITE;
  308. for (xml_node keyNode = node.child("key"); !keyNode.empty(); keyNode = keyNode.next_sibling("key"))
  309. {
  310. keys_.Push(new SpriteTimelineKey(this));
  311. if (!keys_.Back()->Load(keyNode))
  312. return false;
  313. }
  314. }
  315. else
  316. {
  317. // Unsupported object type now.
  318. return false;
  319. }
  320. return true;
  321. }
  322. TimelineKey::TimelineKey(Timeline* timeline)
  323. {
  324. this->timeline_ = timeline;
  325. }
  326. bool TimelineKey::Load(const pugi::xml_node& node)
  327. {
  328. if (strcmp(node.name(), "key") != 0)
  329. return false;
  330. id_ = node.attribute("id").as_int();
  331. time_ = node.attribute("time").as_float() * 0.001f;
  332. String curveType = node.attribute("curve_type").as_string("linear");
  333. if (curveType == "instant")
  334. curveType_ = INSTANT;
  335. else if (curveType == "linear")
  336. curveType_ = LINEAR;
  337. else if (curveType == "quadratic")
  338. curveType_ = QUADRATIC;
  339. else if (curveType == "cubic")
  340. curveType_ = CUBIC;
  341. else
  342. curveType_ = LINEAR;
  343. c1_ = node.attribute("c1").as_float();
  344. c2_ = node.attribute("c2").as_float();
  345. return true;
  346. }
  347. TimelineKey& TimelineKey::operator=(const TimelineKey& rhs)
  348. {
  349. id_ = rhs.id_;
  350. time_ = rhs.time_;
  351. curveType_ = rhs.curveType_;
  352. c1_ = rhs.c1_;
  353. c2_ = rhs.c2_;
  354. return *this;
  355. }
  356. // From http://www.brashmonkey.com/ScmlDocs/ScmlReference.html
  357. inline float Linear(float a, float b, float t)
  358. {
  359. return a + (b - a) * t;
  360. }
  361. inline float Quadratic(float a, float b, float c, float t)
  362. {
  363. return Linear(Linear(a, b, t), Linear(b, c, t), t);
  364. }
  365. inline float Cubic(float a, float b, float c, float d, float t)
  366. {
  367. return Linear(Quadratic(a, b, c, t), Quadratic(b, c, d, t), t);
  368. }
  369. float TimelineKey::GetTByCurveType(float currentTime, float nextTimelineTime) const
  370. {
  371. if (curveType_ == INSTANT)
  372. return 0.0f;
  373. float t = (currentTime - time_) / (nextTimelineTime - time_);
  374. switch (curveType_)
  375. {
  376. case LINEAR:
  377. return t;
  378. case QUADRATIC:
  379. return Quadratic(0.0f, c1_, 1.0f, t);
  380. case CUBIC:
  381. return Cubic(0.0f, c1_, c2_, 1.0f, t);
  382. default:
  383. return 0.0f;
  384. }
  385. }
  386. SpatialInfo::SpatialInfo(float x, float y, float angle, float scale_x, float scale_y, float alpha, int spin) :
  387. x_(x),
  388. y_(y),
  389. angle_(angle),
  390. scaleX_(scale_x),
  391. scaleY_(scale_y),
  392. alpha_(alpha),
  393. spin_(spin)
  394. {
  395. }
  396. SpatialInfo SpatialInfo::UnmapFromParent(const SpatialInfo& parentInfo) const
  397. {
  398. float unmappedX;
  399. float unmappedY;
  400. float unmappedAngle = angle_ + parentInfo.angle_;
  401. float unmappedScaleX = scaleX_ * parentInfo.scaleX_;
  402. float unmappedScaleY = scaleY_ * parentInfo.scaleY_;
  403. float unmappedAlpha = alpha_ * parentInfo.alpha_;
  404. if (x_ != 0.0f || y_ != 0.0f)
  405. {
  406. float preMultX = x_ * parentInfo.scaleX_;
  407. float preMultY = y_ * parentInfo.scaleY_;
  408. float s = Sin(parentInfo.angle_);
  409. float c = Cos(parentInfo.angle_);
  410. unmappedX = (preMultX * c) - (preMultY * s) + parentInfo.x_;
  411. unmappedY = (preMultX * s) + (preMultY * c) + parentInfo.y_;
  412. }
  413. else
  414. {
  415. unmappedX = parentInfo.x_;
  416. unmappedY = parentInfo.y_;
  417. }
  418. return {unmappedX, unmappedY, unmappedAngle, unmappedScaleX, unmappedScaleY, unmappedAlpha, spin_};
  419. }
  420. void SpatialInfo::Interpolate(const SpatialInfo& other, float t)
  421. {
  422. x_ = Linear(x_, other.x_, t);
  423. y_ = Linear(y_, other.y_, t);
  424. if (spin_ > 0.0f && (other.angle_ - angle_ < 0.0f))
  425. {
  426. angle_ = Linear(angle_, other.angle_ + 360.0f, t);
  427. }
  428. else if (spin_ < 0.0f && (other.angle_ - angle_ > 0.0f))
  429. {
  430. angle_ = Linear(angle_, other.angle_ - 360.0f, t);
  431. }
  432. else
  433. {
  434. angle_ = Linear(angle_, other.angle_, t);
  435. }
  436. scaleX_ = Linear(scaleX_, other.scaleX_, t);
  437. scaleY_ = Linear(scaleY_, other.scaleY_, t);
  438. alpha_ = Linear(alpha_, other.alpha_, t);
  439. }
  440. SpatialTimelineKey::SpatialTimelineKey(Timeline* timeline) :
  441. TimelineKey(timeline),
  442. info_(0.f, 0.f, 0.f, 1.f, 1.f)
  443. {
  444. }
  445. bool SpatialTimelineKey::Load(const xml_node& node)
  446. {
  447. if (!TimelineKey::Load(node))
  448. return false;
  449. xml_node childNode = node.child("bone");
  450. if (childNode.empty())
  451. childNode = node.child("object");
  452. info_.x_ = childNode.attribute("x").as_float();
  453. info_.y_ = childNode.attribute("y").as_float();
  454. info_.angle_ = childNode.attribute("angle").as_float();
  455. info_.scaleX_ = childNode.attribute("scale_x").as_float(1.0f);
  456. info_.scaleY_ = childNode.attribute("scale_y").as_float(1.0f);
  457. info_.alpha_ = childNode.attribute("a").as_float(1.0f);
  458. info_.spin_ = node.attribute("spin").as_int(1);
  459. return true;
  460. }
  461. void SpatialTimelineKey::Interpolate(const TimelineKey& other, float t)
  462. {
  463. const auto& o = (const SpatialTimelineKey&)other;
  464. info_.Interpolate(o.info_, t);
  465. }
  466. BoneTimelineKey::BoneTimelineKey(Timeline* timeline) :
  467. SpatialTimelineKey(timeline)
  468. {
  469. }
  470. TimelineKey* BoneTimelineKey::Clone() const
  471. {
  472. auto* result = new BoneTimelineKey(timeline_);
  473. *result = *this;
  474. return result;
  475. }
  476. bool BoneTimelineKey::Load(const xml_node& node)
  477. {
  478. if (!SpatialTimelineKey::Load(node))
  479. return false;
  480. xml_node boneNode = node.child("bone");
  481. length_ = boneNode.attribute("length").as_float(200.0f);
  482. width_ = boneNode.attribute("width").as_float(10.0f);
  483. return true;
  484. }
  485. void BoneTimelineKey::Interpolate(const TimelineKey& other, float t)
  486. {
  487. SpatialTimelineKey::Interpolate(other, t);
  488. const auto& o = (const BoneTimelineKey&)other;
  489. length_ = Linear(length_, o.length_, t);
  490. width_ = Linear(width_, o.width_, t);
  491. }
  492. TimelineKey* SpriteTimelineKey::Clone() const
  493. {
  494. auto* result = new SpriteTimelineKey(timeline_);
  495. *result = *this;
  496. return result;
  497. }
  498. SpriteTimelineKey::SpriteTimelineKey(Timeline* timeline) :
  499. SpatialTimelineKey(timeline)
  500. {
  501. }
  502. bool SpriteTimelineKey::Load(const pugi::xml_node& node)
  503. {
  504. if (!SpatialTimelineKey::Load(node))
  505. return false;
  506. xml_node objectNode = node.child("object");
  507. folderId_ = objectNode.attribute("folder").as_int(-1);
  508. fileId_ = objectNode.attribute("file").as_int(-1);
  509. xml_attribute pivotXAttr = objectNode.attribute("pivot_x");
  510. xml_attribute pivotYAttr = objectNode.attribute("pivot_y");
  511. if (pivotXAttr.empty() && pivotYAttr.empty())
  512. useDefaultPivot_ = true;
  513. else
  514. {
  515. useDefaultPivot_ = false;
  516. pivotX_ = pivotXAttr.as_float(0.0f);
  517. pivotY_ = pivotYAttr.as_float(1.0f);
  518. }
  519. return true;
  520. }
  521. void SpriteTimelineKey::Interpolate(const TimelineKey& other, float t)
  522. {
  523. SpatialTimelineKey::Interpolate(other, t);
  524. const auto& o = (const SpriteTimelineKey&)other;
  525. pivotX_ = Linear(pivotX_, o.pivotX_, t);
  526. pivotY_ = Linear(pivotY_, o.pivotY_, t);
  527. }
  528. SpriteTimelineKey& SpriteTimelineKey::operator=(const SpriteTimelineKey& rhs)
  529. {
  530. SpatialTimelineKey::operator=(rhs);
  531. folderId_ = rhs.folderId_;
  532. fileId_ = rhs.fileId_;
  533. useDefaultPivot_ = rhs.useDefaultPivot_;
  534. pivotX_ = rhs.pivotX_;
  535. pivotY_ = rhs.pivotY_;
  536. return *this;
  537. }
  538. }
  539. }