TmxFile2D.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // Copyright (c) 2008-2015 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. REFCOUNTED(TmxLayer2D)
  36. public:
  37. TmxLayer2D(TmxFile2D* tmxFile, TileMapLayerType2D type);
  38. virtual ~TmxLayer2D();
  39. /// Return tmx file.
  40. TmxFile2D* GetTmxFile() const;
  41. /// Return type.
  42. TileMapLayerType2D GetType() const { return type_; }
  43. /// Return name.
  44. const String& GetName() const { return name_; }
  45. /// Return width.
  46. int GetWidth() const { return width_; }
  47. /// Return height.
  48. int GetHeight() const { return height_; }
  49. /// Return is visible.
  50. bool IsVisible() const { return visible_; }
  51. /// Return has property (use for script).
  52. bool HasProperty(const String& name) const;
  53. /// Return property value (use for script).
  54. const String& GetProperty(const String& name) const;
  55. protected:
  56. /// Load layer info.
  57. void LoadInfo(const XMLElement& element);
  58. /// Load property set.
  59. void LoadPropertySet(const XMLElement& element);
  60. /// Tmx file.
  61. WeakPtr<TmxFile2D> tmxFile_;
  62. /// Layer type.
  63. TileMapLayerType2D type_;
  64. /// Name.
  65. String name_;
  66. /// Width.
  67. int width_;
  68. /// Height.
  69. int height_;
  70. /// Visible.
  71. bool visible_;
  72. /// Property set.
  73. SharedPtr<PropertySet2D> propertySet_;
  74. };
  75. /// Tmx tile layer.
  76. class TmxTileLayer2D : public TmxLayer2D
  77. {
  78. REFCOUNTED(TmxTileLayer2D)
  79. public:
  80. TmxTileLayer2D(TmxFile2D* tmxFile);
  81. /// Load from XML element.
  82. bool Load(const XMLElement& element, const TileMapInfo2D& info);
  83. /// Return tile.
  84. Tile2D* GetTile(int x, int y) const;
  85. protected:
  86. /// Tile.
  87. Vector<SharedPtr<Tile2D> > tiles_;
  88. };
  89. /// Tmx image layer.
  90. class TmxObjectGroup2D : public TmxLayer2D
  91. {
  92. REFCOUNTED(TmxObjectGroup2D)
  93. public:
  94. TmxObjectGroup2D(TmxFile2D* tmxFile);
  95. /// Load from XML element.
  96. bool Load(const XMLElement& element, const TileMapInfo2D& info, bool local = false);
  97. /// Return number of objects.
  98. unsigned GetNumObjects() const { return objects_.Size(); }
  99. /// Return tile map object at index.
  100. TileMapObject2D* GetObject(unsigned index) const;
  101. private:
  102. /// Objects.
  103. Vector<SharedPtr<TileMapObject2D> > objects_;
  104. };
  105. /// Tmx image layer.
  106. class TmxImageLayer2D : public TmxLayer2D
  107. {
  108. REFCOUNTED(TmxImageLayer2D)
  109. public:
  110. TmxImageLayer2D(TmxFile2D* tmxFile);
  111. /// Load from XML element.
  112. bool Load(const XMLElement& element, const TileMapInfo2D& info);
  113. /// Return position.
  114. const Vector2& GetPosition() const { return position_; }
  115. /// Return source.
  116. const String& GetSource() const { return source_; }
  117. /// Return sprite.
  118. Sprite2D* GetSprite() const;
  119. private:
  120. /// Position.
  121. Vector2 position_;
  122. /// Source.
  123. String source_;
  124. /// Sprite.
  125. SharedPtr<Sprite2D> sprite_;
  126. };
  127. /// Tile map file.
  128. class ATOMIC_API TmxFile2D : public Resource
  129. {
  130. OBJECT(TmxFile2D);
  131. public:
  132. /// Construct.
  133. TmxFile2D(Context* context);
  134. /// Destruct.
  135. virtual ~TmxFile2D();
  136. /// Register object factory.
  137. static void RegisterObject(Context* context);
  138. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  139. virtual bool BeginLoad(Deserializer& source);
  140. /// Finish resource loading. Always called from the main thread. Return true if successful.
  141. virtual bool EndLoad();
  142. /// Return information.
  143. const TileMapInfo2D& GetInfo() const { return info_; }
  144. /// Return tile sprite by gid, if not exist return 0.
  145. Sprite2D* GetTileSprite(int gid) const;
  146. /// Return tile property set by gid, if not exist return 0.
  147. PropertySet2D* GetTilePropertySet(int gid) const;
  148. /// Return tile object group by gid, if not exist return 0.
  149. TmxObjectGroup2D* GetTileObjectGroup(int gid) const;
  150. /// Return number of layers.
  151. unsigned GetNumLayers() const { return layers_.Size(); }
  152. /// Return layer at index.
  153. const TmxLayer2D* GetLayer(unsigned index) const;
  154. private:
  155. /// Load TSX file.
  156. SharedPtr<XMLFile> LoadTSXFile(const String& source);
  157. /// Load tile set.
  158. bool LoadTileSet(const XMLElement& element);
  159. /// XML file used during loading.
  160. SharedPtr<XMLFile> loadXMLFile_;
  161. /// TSX name to XML file mapping.
  162. HashMap<String, SharedPtr<XMLFile> > tsxXMLFiles_;
  163. /// Tile map information.
  164. TileMapInfo2D info_;
  165. /// Tile set textures.
  166. Vector<SharedPtr<Texture2D> > tileSetTextures_;
  167. /// Gid to tile sprite mapping.
  168. HashMap<int, SharedPtr<Sprite2D> > gidToSpriteMapping_;
  169. /// Gid to tile property set mapping.
  170. HashMap<int, SharedPtr<PropertySet2D> > gidToPropertySetMapping_;
  171. /// Gid to tile objectgroup mapping.
  172. HashMap<int, SharedPtr<TmxObjectGroup2D> > gidToObjectGroupMapping_;
  173. /// Layers.
  174. Vector<TmxLayer2D*> layers_;
  175. };
  176. }