TileMapDefs2D.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #include "../Container/RefCounted.h"
  25. #include "../Urho2D/Sprite2D.h"
  26. namespace Urho3D
  27. {
  28. class XMLElement;
  29. /// Orientation.
  30. enum Orientation2D
  31. {
  32. /// Orthogonal.
  33. O_ORTHOGONAL = 0,
  34. /// Isometric.
  35. O_ISOMETRIC,
  36. /// Staggered.
  37. O_STAGGERED,
  38. /// Hexagonal.
  39. O_HEXAGONAL
  40. };
  41. /// Tile map information.
  42. /// @fakeref
  43. struct URHO3D_API TileMapInfo2D
  44. {
  45. /// Orientation.
  46. Orientation2D orientation_;
  47. /// Width.
  48. int width_;
  49. /// Height.
  50. int height_;
  51. /// Tile width.
  52. float tileWidth_;
  53. /// Tile height.
  54. float tileHeight_;
  55. /// Return map width.
  56. /// @property
  57. float GetMapWidth() const;
  58. /// return map height.
  59. /// @property
  60. float GetMapHeight() const;
  61. /// Convert tmx position to Urho position.
  62. Vector2 ConvertPosition(const Vector2& position) const;
  63. /// Convert tile index to position.
  64. Vector2 TileIndexToPosition(int x, int y) const;
  65. /// Convert position to tile index, if out of map return false.
  66. bool PositionToTileIndex(int& x, int& y, const Vector2& position) const;
  67. };
  68. /// Tile map layer type.
  69. enum TileMapLayerType2D
  70. {
  71. /// Tile layer.
  72. LT_TILE_LAYER = 0,
  73. /// Object group.
  74. LT_OBJECT_GROUP,
  75. /// Image layer.
  76. LT_IMAGE_LAYER,
  77. /// Invalid.
  78. LT_INVALID = 0xffff
  79. };
  80. /// Tile map object type.
  81. enum TileMapObjectType2D
  82. {
  83. /// Rectangle.
  84. OT_RECTANGLE = 0,
  85. /// Ellipse.
  86. OT_ELLIPSE,
  87. /// Polygon.
  88. OT_POLYGON,
  89. /// Polyline.
  90. OT_POLYLINE,
  91. /// Tile.
  92. OT_TILE,
  93. /// Invalid.
  94. OT_INVALID = 0xffff
  95. };
  96. /// Property set.
  97. class URHO3D_API PropertySet2D : public RefCounted
  98. {
  99. public:
  100. PropertySet2D();
  101. ~PropertySet2D() override;
  102. /// Load from XML element.
  103. void Load(const XMLElement& element);
  104. /// Return has property.
  105. bool HasProperty(const String& name) const;
  106. /// Return property value.
  107. const String& GetProperty(const String& name) const;
  108. protected:
  109. /// Property name to property value mapping.
  110. HashMap<String, String> nameToValueMapping_;
  111. };
  112. /// Tile flipping flags.
  113. static const unsigned FLIP_HORIZONTAL = 0x80000000u;
  114. static const unsigned FLIP_VERTICAL = 0x40000000u;
  115. static const unsigned FLIP_DIAGONAL = 0x20000000u;
  116. static const unsigned FLIP_RESERVED = 0x10000000u;
  117. static const unsigned FLIP_ALL = FLIP_HORIZONTAL | FLIP_VERTICAL | FLIP_DIAGONAL | FLIP_RESERVED;
  118. /// Tile define.
  119. class URHO3D_API Tile2D : public RefCounted
  120. {
  121. public:
  122. /// Construct.
  123. Tile2D();
  124. /// Return gid.
  125. /// @property
  126. unsigned GetGid() const { return gid_ & ~FLIP_ALL; }
  127. /// Return flip X.
  128. /// @property
  129. bool GetFlipX() const { return gid_ & FLIP_HORIZONTAL; }
  130. /// Return flip Y.
  131. /// @property
  132. bool GetFlipY() const { return gid_ & FLIP_VERTICAL; }
  133. /// Return swap X and Y.
  134. /// @property
  135. bool GetSwapXY() const { return gid_ & FLIP_DIAGONAL; }
  136. /// Return sprite.
  137. /// @property
  138. Sprite2D* GetSprite() const;
  139. /// Return has property.
  140. bool HasProperty(const String& name) const;
  141. /// Return property.
  142. const String& GetProperty(const String& name) const;
  143. private:
  144. friend class TmxTileLayer2D;
  145. /// Gid.
  146. unsigned gid_;
  147. /// Sprite.
  148. SharedPtr<Sprite2D> sprite_;
  149. /// Property set.
  150. SharedPtr<PropertySet2D> propertySet_;
  151. };
  152. /// Tile map object.
  153. class URHO3D_API TileMapObject2D : public RefCounted
  154. {
  155. public:
  156. TileMapObject2D();
  157. /// Return type.
  158. /// @property
  159. TileMapObjectType2D GetObjectType() const { return objectType_; }
  160. /// Return name.
  161. /// @property
  162. const String& GetName() const { return name_; }
  163. /// Return type.
  164. /// @property
  165. const String& GetType() const { return type_; }
  166. /// Return position.
  167. /// @property
  168. const Vector2& GetPosition() const { return position_; }
  169. /// Return size (for rectangle and ellipse).
  170. /// @property
  171. const Vector2& GetSize() const { return size_; }
  172. /// Return number of points (use for script).
  173. /// @property
  174. unsigned GetNumPoints() const;
  175. /// Return point at index (use for script).
  176. const Vector2& GetPoint(unsigned index) const;
  177. /// Return tile Gid.
  178. /// @property
  179. unsigned GetTileGid() const { return gid_ & ~FLIP_ALL; }
  180. /// Return tile flip X.
  181. /// @property
  182. bool GetTileFlipX() const { return gid_ & FLIP_HORIZONTAL; }
  183. /// Return tile flip Y.
  184. /// @property
  185. bool GetTileFlipY() const { return gid_ & FLIP_VERTICAL; }
  186. /// Return tile swap X and Y.
  187. /// @property
  188. bool GetTileSwapXY() const { return gid_ & FLIP_DIAGONAL; }
  189. /// Return tile sprite.
  190. /// @property
  191. Sprite2D* GetTileSprite() const;
  192. /// Return has property.
  193. bool HasProperty(const String& name) const;
  194. /// Return property value.
  195. const String& GetProperty(const String& name) const;
  196. private:
  197. friend class TmxObjectGroup2D;
  198. /// Object type.
  199. TileMapObjectType2D objectType_{};
  200. /// Name.
  201. String name_;
  202. /// Type.
  203. String type_;
  204. /// Position.
  205. Vector2 position_;
  206. /// Size (for rectangle and ellipse).
  207. Vector2 size_;
  208. /// Points(for polygon and polyline).
  209. Vector<Vector2> points_;
  210. /// Gid (for tile).
  211. unsigned gid_{};
  212. /// Sprite (for tile).
  213. SharedPtr<Sprite2D> sprite_;
  214. /// Property set.
  215. SharedPtr<PropertySet2D> propertySet_;
  216. };
  217. }