TmxFile2D.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // Copyright (c) 2008-2016 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. #pragma once
  23. #include "../Resource/Resource.h"
  24. #include "../Atomic2D/TileMapDefs2D.h"
  25. namespace Atomic
  26. {
  27. class Sprite2D;
  28. class Texture2D;
  29. class TmxFile2D;
  30. class XMLElement;
  31. class XMLFile;
  32. /// Tmx layer.
  33. class TmxLayer2D : public RefCounted
  34. {
  35. ATOMIC_REFCOUNTED(TmxLayer2D)
  36. public:
  37. // ATOMIC BEGIN
  38. // default arguments for script bindings on subclasses
  39. TmxLayer2D(TmxFile2D* tmxFile = 0, TileMapLayerType2D type = LT_INVALID);
  40. // ATOMIC END
  41. virtual ~TmxLayer2D();
  42. /// Return tmx file.
  43. TmxFile2D* GetTmxFile() const;
  44. /// Return type.
  45. TileMapLayerType2D GetType() const { return type_; }
  46. /// Return name.
  47. const String& GetName() const { return name_; }
  48. /// Return width.
  49. int GetWidth() const { return width_; }
  50. /// Return height.
  51. int GetHeight() const { return height_; }
  52. /// Return is visible.
  53. bool IsVisible() const { return visible_; }
  54. /// Return has property (use for script).
  55. bool HasProperty(const String& name) const;
  56. /// Return property value (use for script).
  57. const String& GetProperty(const String& name) const;
  58. protected:
  59. /// Load layer info.
  60. void LoadInfo(const XMLElement& element);
  61. /// Load property set.
  62. void LoadPropertySet(const XMLElement& element);
  63. /// Tmx file.
  64. WeakPtr<TmxFile2D> tmxFile_;
  65. /// Layer type.
  66. TileMapLayerType2D type_;
  67. /// Name.
  68. String name_;
  69. /// Width.
  70. int width_;
  71. /// Height.
  72. int height_;
  73. /// Visible.
  74. bool visible_;
  75. /// Property set.
  76. SharedPtr<PropertySet2D> propertySet_;
  77. };
  78. /// Tmx tile layer.
  79. class TmxTileLayer2D : public TmxLayer2D
  80. {
  81. ATOMIC_REFCOUNTED(TmxTileLayer2D)
  82. public:
  83. TmxTileLayer2D(TmxFile2D* tmxFile);
  84. /// Load from XML element.
  85. bool Load(const XMLElement& element, const TileMapInfo2D& info);
  86. /// Return tile.
  87. Tile2D* GetTile(int x, int y) const;
  88. protected:
  89. /// Tile.
  90. Vector<SharedPtr<Tile2D> > tiles_;
  91. };
  92. /// Tmx image layer.
  93. class TmxObjectGroup2D : public TmxLayer2D
  94. {
  95. ATOMIC_REFCOUNTED(TmxObjectGroup2D)
  96. public:
  97. TmxObjectGroup2D(TmxFile2D* tmxFile);
  98. // ATOMIC BEGIN
  99. /// Load from XML element.
  100. bool Load(const XMLElement& element, const TileMapInfo2D& info, bool local = false);
  101. // ATOMIC END
  102. /// Return number of objects.
  103. unsigned GetNumObjects() const { return objects_.Size(); }
  104. /// Return tile map object at index.
  105. TileMapObject2D* GetObject(unsigned index) const;
  106. private:
  107. /// Objects.
  108. Vector<SharedPtr<TileMapObject2D> > objects_;
  109. };
  110. /// Tmx image layer.
  111. class TmxImageLayer2D : public TmxLayer2D
  112. {
  113. ATOMIC_REFCOUNTED(TmxImageLayer2D)
  114. public:
  115. TmxImageLayer2D(TmxFile2D* tmxFile);
  116. /// Load from XML element.
  117. bool Load(const XMLElement& element, const TileMapInfo2D& info);
  118. /// Return position.
  119. const Vector2& GetPosition() const { return position_; }
  120. /// Return source.
  121. const String& GetSource() const { return source_; }
  122. /// Return sprite.
  123. Sprite2D* GetSprite() const;
  124. private:
  125. /// Position.
  126. Vector2 position_;
  127. /// Source.
  128. String source_;
  129. /// Sprite.
  130. SharedPtr<Sprite2D> sprite_;
  131. };
  132. /// Tile map file.
  133. class ATOMIC_API TmxFile2D : public Resource
  134. {
  135. ATOMIC_OBJECT(TmxFile2D, Resource);
  136. public:
  137. /** Construct.
  138. * This will load a Tiled .tmx file for use. Not all
  139. * Tile options are supported. Some important limitions are:
  140. * - The Tile Layer Format must be XML
  141. * - The tilesets used in the map must be made from sprite sheets. Individual images are not supported
  142. */
  143. TmxFile2D(Context* context);
  144. /// Destruct.
  145. virtual ~TmxFile2D();
  146. /// Register object factory.
  147. static void RegisterObject(Context* context);
  148. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  149. virtual bool BeginLoad(Deserializer& source);
  150. /// Finish resource loading. Always called from the main thread. Return true if successful.
  151. virtual bool EndLoad();
  152. /// Set Tilemap information.
  153. bool SetInfo(Orientation2D orientation, int width, int height, float tileWidth, float tileHeight);
  154. /// Add layer at index, if index > number of layers then append to end.
  155. void AddLayer(unsigned index, TmxLayer2D *layer);
  156. /// Append layer to end.
  157. void AddLayer(Atomic::TmxLayer2D* layer);
  158. /// Return Tilemap information.
  159. const TileMapInfo2D& GetInfo() const { return info_; }
  160. /// Return tile sprite by gid, if not exist return 0.
  161. Sprite2D* GetTileSprite(int gid) const;
  162. /// Return tile property set by gid, if not exist return 0.
  163. PropertySet2D* GetTilePropertySet(int gid) const;
  164. /// Return number of layers.
  165. unsigned GetNumLayers() const { return layers_.Size(); }
  166. /// Return layer at index.
  167. const TmxLayer2D* GetLayer(unsigned index) const;
  168. // BEGIN ATOMIC
  169. TmxObjectGroup2D* GetTileObjectGroup(int gid) const;
  170. // END ATOMIC
  171. private:
  172. /// Load TSX file.
  173. SharedPtr<XMLFile> LoadTSXFile(const String& source);
  174. /// Load tile set.
  175. bool LoadTileSet(const XMLElement& element);
  176. /// XML file used during loading.
  177. SharedPtr<XMLFile> loadXMLFile_;
  178. /// TSX name to XML file mapping.
  179. HashMap<String, SharedPtr<XMLFile> > tsxXMLFiles_;
  180. /// Tile map information.
  181. TileMapInfo2D info_;
  182. /// Tile set textures.
  183. Vector<SharedPtr<Texture2D> > tileSetTextures_;
  184. /// Gid to tile sprite mapping.
  185. HashMap<int, SharedPtr<Sprite2D> > gidToSpriteMapping_;
  186. /// Gid to tile property set mapping.
  187. HashMap<int, SharedPtr<PropertySet2D> > gidToPropertySetMapping_;
  188. // ATOMIC BEGIN
  189. /// Layers.
  190. Vector<SharedPtr<TmxLayer2D>> layers_;
  191. /// Gid to tile objectgroup mapping.
  192. HashMap<int, SharedPtr<TmxObjectGroup2D> > gidToObjectGroupMapping_;
  193. // ATOMIC END
  194. };
  195. }