SpriterData2D.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. /// \file
  23. #pragma once
  24. namespace pugi
  25. {
  26. class xml_node;
  27. }
  28. namespace Urho3D
  29. {
  30. namespace Spriter
  31. {
  32. struct Animation;
  33. struct BoneTimelineKey;
  34. struct CharacterMap;
  35. struct Entity;
  36. struct File;
  37. struct Folder;
  38. struct MainlineKey;
  39. struct MapInstruction;
  40. struct Ref;
  41. struct SpatialInfo;
  42. struct SpatialTimelineKey;
  43. struct SpriterData;
  44. struct SpriteTimelineKey;
  45. struct Timeline;
  46. struct TimelineKey;
  47. /// Spriter data.
  48. struct SpriterData
  49. {
  50. SpriterData() = default;
  51. ~SpriterData();
  52. void Reset();
  53. bool Load(const pugi::xml_node& node);
  54. bool Load(const void* data, size_t size);
  55. int scmlVersion_{};
  56. String generator_;
  57. String generatorVersion_;
  58. PODVector<Folder*> folders_;
  59. PODVector<Entity*> entities_;
  60. };
  61. /// Folder.
  62. struct Folder
  63. {
  64. Folder() = default;
  65. ~Folder();
  66. void Reset();
  67. bool Load(const pugi::xml_node& node);
  68. int id_{};
  69. String name_;
  70. PODVector<File*> files_;
  71. };
  72. /// File.
  73. struct File
  74. {
  75. explicit File(Folder* folder);
  76. ~File() = default;
  77. bool Load(const pugi::xml_node& node);
  78. Folder* folder_{};
  79. int id_{};
  80. String name_;
  81. float width_{};
  82. float height_{};
  83. float pivotX_{};
  84. float pivotY_{};
  85. };
  86. /// Entity.
  87. struct Entity
  88. {
  89. Entity() = default;
  90. ~Entity();
  91. void Reset();
  92. bool Load(const pugi::xml_node& node);
  93. int id_{};
  94. String name_;
  95. PODVector<CharacterMap*> characterMaps_;
  96. PODVector<Animation*> animations_;
  97. };
  98. /// Character map.
  99. struct CharacterMap
  100. {
  101. CharacterMap() = default;
  102. ~CharacterMap();
  103. void Reset();
  104. bool Load(const pugi::xml_node& node);
  105. int id_{};
  106. String name_;
  107. PODVector<MapInstruction*> maps_;
  108. };
  109. /// Map instruction.
  110. struct MapInstruction
  111. {
  112. MapInstruction() = default;
  113. ~MapInstruction() = default;
  114. bool Load(const pugi::xml_node& node);
  115. int folder_{};
  116. int file_{};
  117. int targetFolder_{};
  118. int targetFile_{};
  119. };
  120. /// Animation.
  121. struct Animation
  122. {
  123. Animation() = default;
  124. ~Animation();
  125. void Reset();
  126. bool Load(const pugi::xml_node& node);
  127. int id_{};
  128. String name_;
  129. float length_{};
  130. bool looping_{};
  131. PODVector<MainlineKey*> mainlineKeys_;
  132. PODVector<Timeline*> timelines_;
  133. };
  134. /// Mainline key.
  135. struct MainlineKey
  136. {
  137. MainlineKey() = default;
  138. ~MainlineKey();
  139. void Reset();
  140. bool Load(const pugi::xml_node& node);
  141. int id_{};
  142. float time_{};
  143. PODVector<Ref*> boneRefs_;
  144. PODVector<Ref*> objectRefs_;
  145. };
  146. /// Ref.
  147. struct Ref
  148. {
  149. Ref() = default;
  150. ~Ref() = default;
  151. bool Load(const pugi::xml_node& node);
  152. int id_{};
  153. int parent_{};
  154. int timeline_{};
  155. int key_{};
  156. int zIndex_{};
  157. };
  158. /// Object type.
  159. enum ObjectType
  160. {
  161. BONE = 0,
  162. SPRITE
  163. };
  164. /// Timeline.
  165. struct Timeline
  166. {
  167. Timeline() = default;
  168. ~Timeline();
  169. void Reset();
  170. bool Load(const pugi::xml_node& node);
  171. int id_{};
  172. String name_;
  173. ObjectType objectType_;
  174. PODVector<SpatialTimelineKey*> keys_;
  175. };
  176. /// Curve type.
  177. enum CurveType
  178. {
  179. INSTANT = 0,
  180. LINEAR,
  181. QUADRATIC,
  182. CUBIC
  183. };
  184. /// Timeline key.
  185. struct TimelineKey
  186. {
  187. explicit TimelineKey(Timeline* timeline);
  188. virtual ~TimelineKey() = default;
  189. virtual ObjectType GetObjectType() const = 0;
  190. virtual TimelineKey* Clone() const = 0;
  191. virtual bool Load(const pugi::xml_node& node);
  192. virtual void Interpolate(const TimelineKey& other, float t) = 0;
  193. TimelineKey& operator=(const TimelineKey& rhs);
  194. float GetTByCurveType(float currentTime, float nextTimelineTime) const;
  195. Timeline* timeline_{};
  196. int id_{};
  197. float time_{};
  198. CurveType curveType_{};
  199. float c1_{};
  200. float c2_{};
  201. };
  202. /// Spatial info.
  203. struct SpatialInfo
  204. {
  205. SpatialInfo(float x, float y, float angle, float scale_x, float scale_y, float alpha = 1, int spin = 1);
  206. SpatialInfo UnmapFromParent(const SpatialInfo& parentInfo) const;
  207. void Interpolate(const SpatialInfo& other, float t);
  208. float x_;
  209. float y_;
  210. float angle_;
  211. float scaleX_;
  212. float scaleY_;
  213. float alpha_;
  214. int spin_;
  215. };
  216. /// Spatial timeline key.
  217. struct SpatialTimelineKey : TimelineKey
  218. {
  219. explicit SpatialTimelineKey(Timeline* timeline);
  220. ~SpatialTimelineKey() override = default;
  221. bool Load(const pugi::xml_node& node) override;
  222. void Interpolate(const TimelineKey& other, float t) override;
  223. SpatialTimelineKey& operator=(const SpatialTimelineKey& rhs) = default;
  224. SpatialInfo info_;
  225. };
  226. /// Bone timeline key.
  227. struct BoneTimelineKey : SpatialTimelineKey
  228. {
  229. explicit BoneTimelineKey(Timeline* timeline);
  230. ~BoneTimelineKey() override = default;
  231. ObjectType GetObjectType() const override { return BONE; }
  232. TimelineKey* Clone() const override;
  233. bool Load(const pugi::xml_node& node) override;
  234. void Interpolate(const TimelineKey& other, float t) override;
  235. BoneTimelineKey& operator=(const BoneTimelineKey& rhs) = default;
  236. float length_{};
  237. float width_{};
  238. };
  239. /// Sprite timeline key.
  240. struct SpriteTimelineKey : SpatialTimelineKey
  241. {
  242. explicit SpriteTimelineKey(Timeline* timeline);
  243. ~SpriteTimelineKey() override = default;
  244. ObjectType GetObjectType() const override { return SPRITE; }
  245. TimelineKey* Clone() const override;
  246. bool Load(const pugi::xml_node& node) override;
  247. void Interpolate(const TimelineKey& other, float t) override;
  248. SpriteTimelineKey& operator=(const SpriteTimelineKey& rhs);
  249. int folderId_{};
  250. int fileId_{};
  251. bool useDefaultPivot_{};
  252. float pivotX_{};
  253. float pivotY_{};
  254. /// Run time data.
  255. int zIndex_{};
  256. };
  257. }
  258. }