TmxFile2D.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #pragma once
  23. #include "../Resource/Resource.h"
  24. #include "../Urho2D/TileMapDefs2D.h"
  25. namespace Urho3D
  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. public:
  36. TmxLayer2D(TmxFile2D* tmxFile, TileMapLayerType2D type);
  37. ~TmxLayer2D() override = default;
  38. /// Return tmx file.
  39. TmxFile2D* GetTmxFile() const;
  40. /// Return type.
  41. TileMapLayerType2D GetType() const { return type_; }
  42. /// Return name.
  43. const String& GetName() const { return name_; }
  44. /// Return width.
  45. int GetWidth() const { return width_; }
  46. /// Return height.
  47. int GetHeight() const { return height_; }
  48. /// Return is visible.
  49. bool IsVisible() const { return visible_; }
  50. /// Return has property (use for script).
  51. bool HasProperty(const String& name) const;
  52. /// Return property value (use for script).
  53. const String& GetProperty(const String& name) const;
  54. protected:
  55. /// Load layer info.
  56. void LoadInfo(const XMLElement& element);
  57. /// Load property set.
  58. void LoadPropertySet(const XMLElement& element);
  59. /// Tmx file.
  60. WeakPtr<TmxFile2D> tmxFile_;
  61. /// Layer type.
  62. TileMapLayerType2D type_;
  63. /// Name.
  64. String name_;
  65. /// Width.
  66. int width_{};
  67. /// Height.
  68. int height_{};
  69. /// Visible.
  70. bool visible_{};
  71. /// Property set.
  72. SharedPtr<PropertySet2D> propertySet_;
  73. };
  74. /// Tmx tile layer.
  75. class TmxTileLayer2D : public TmxLayer2D
  76. {
  77. public:
  78. explicit TmxTileLayer2D(TmxFile2D* tmxFile);
  79. /// Load from XML element.
  80. bool Load(const XMLElement& element, const TileMapInfo2D& info);
  81. /// Return tile.
  82. Tile2D* GetTile(int x, int y) const;
  83. protected:
  84. /// Tiles.
  85. Vector<SharedPtr<Tile2D> > tiles_;
  86. };
  87. /// Tmx objects layer.
  88. class TmxObjectGroup2D : public TmxLayer2D
  89. {
  90. public:
  91. explicit TmxObjectGroup2D(TmxFile2D* tmxFile);
  92. /// Load from XML element.
  93. bool Load(const XMLElement& element, const TileMapInfo2D& info);
  94. /// Store object.
  95. void StoreObject(const XMLElement& objectElem, const SharedPtr<TileMapObject2D>& object, const TileMapInfo2D& info, bool isTile = false);
  96. /// Return number of objects.
  97. unsigned GetNumObjects() const { return objects_.Size(); }
  98. /// Return tile map object at index.
  99. TileMapObject2D* GetObject(unsigned index) const;
  100. private:
  101. /// Objects.
  102. Vector<SharedPtr<TileMapObject2D> > objects_;
  103. };
  104. /// Tmx image layer.
  105. class TmxImageLayer2D : public TmxLayer2D
  106. {
  107. public:
  108. explicit TmxImageLayer2D(TmxFile2D* tmxFile);
  109. /// Load from XML element.
  110. bool Load(const XMLElement& element, const TileMapInfo2D& info);
  111. /// Return position.
  112. const Vector2& GetPosition() const { return position_; }
  113. /// Return source.
  114. const String& GetSource() const { return source_; }
  115. /// Return sprite.
  116. Sprite2D* GetSprite() const;
  117. private:
  118. /// Position.
  119. Vector2 position_;
  120. /// Source.
  121. String source_;
  122. /// Sprite.
  123. SharedPtr<Sprite2D> sprite_;
  124. };
  125. /// Tile map file.
  126. class URHO3D_API TmxFile2D : public Resource
  127. {
  128. URHO3D_OBJECT(TmxFile2D, Resource);
  129. public:
  130. /// Construct.
  131. explicit TmxFile2D(Context* context);
  132. /// Destruct.
  133. ~TmxFile2D() override;
  134. /// Register object factory.
  135. static void RegisterObject(Context* context);
  136. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  137. bool BeginLoad(Deserializer& source) override;
  138. /// Finish resource loading. Always called from the main thread. Return true if successful.
  139. bool EndLoad() override;
  140. /// Set Tilemap information.
  141. bool SetInfo(Orientation2D orientation, int width, int height, float tileWidth, float tileHeight);
  142. /// Add layer at index, if index > number of layers then append to end.
  143. void AddLayer(unsigned index, TmxLayer2D *layer);
  144. /// Append layer to end.
  145. void AddLayer(Urho3D::TmxLayer2D* layer);
  146. /// Return Tilemap information.
  147. const TileMapInfo2D& GetInfo() const { return info_; }
  148. /// Return tile sprite by gid, if not exist return 0.
  149. Sprite2D* GetTileSprite(unsigned gid) const;
  150. /// Return tile collision shapes for a given gid.
  151. Vector<SharedPtr<TileMapObject2D> > GetTileCollisionShapes(unsigned gid) const;
  152. /// Return tile property set by gid, if not exist return 0.
  153. PropertySet2D* GetTilePropertySet(unsigned gid) const;
  154. /// Return number of layers.
  155. unsigned GetNumLayers() const { return layers_.Size(); }
  156. /// Return layer at index.
  157. const TmxLayer2D* GetLayer(unsigned index) const;
  158. /// Set texture edge offset for all sprites, in pixels.
  159. /// @property{set_edgeOffset}
  160. void SetSpriteTextureEdgeOffset(float offset);
  161. /// Return texture edge offset, in pixels.
  162. /// @property{get_edgeOffset}
  163. float GetSpriteTextureEdgeOffset() const { return edgeOffset_; }
  164. private:
  165. /// Load TSX file.
  166. SharedPtr<XMLFile> LoadTSXFile(const String& source);
  167. /// Load tile set.
  168. bool LoadTileSet(const XMLElement& element);
  169. /// XML file used during loading.
  170. SharedPtr<XMLFile> loadXMLFile_;
  171. /// TSX name to XML file mapping.
  172. HashMap<String, SharedPtr<XMLFile> > tsxXMLFiles_;
  173. /// Tile map information.
  174. TileMapInfo2D info_{};
  175. /// Gid to tile sprite mapping.
  176. HashMap<unsigned, SharedPtr<Sprite2D> > gidToSpriteMapping_;
  177. /// Gid to tile property set mapping.
  178. HashMap<unsigned, SharedPtr<PropertySet2D> > gidToPropertySetMapping_;
  179. /// Gid to tile collision shape mapping.
  180. HashMap<unsigned, Vector<SharedPtr<TileMapObject2D> > > gidToCollisionShapeMapping_;
  181. /// Layers.
  182. Vector<TmxLayer2D*> layers_;
  183. /// Texture edge offset.
  184. float edgeOffset_;
  185. };
  186. }