Browse Source

Change Properties2D derived from RefCounted.

aster 11 years ago
parent
commit
2615d583b5
2 changed files with 33 additions and 22 deletions
  1. 24 15
      Source/Engine/Urho2D/TmxFile2D.cpp
  2. 9 7
      Source/Engine/Urho2D/TmxFile2D.h

+ 24 - 15
Source/Engine/Urho2D/TmxFile2D.cpp

@@ -68,8 +68,7 @@ const String& Properties2D::GetProperty(const String& name) const
 }
 
 Tile2D::Tile2D() : 
-    gid_(0), 
-    properties_(0)
+    gid_(0) 
 {
 }
 
@@ -93,10 +92,8 @@ const String& Tile2D::GetProperty(const String& name) const
     return properties_->GetProperty(name);
 }
 
-
 TileObject2D::TileObject2D()
 {
-
 }
 
 unsigned TileObject2D::GetNumPoints() const
@@ -119,12 +116,16 @@ Sprite2D* TileObject2D::GetTileSprite() const
 
 bool TileObject2D::HasProperty(const String& name) const
 {
-    return properties_.HasProperty(name);
+    if (!properties_)
+        return false;
+    return properties_->HasProperty(name);
 }
 
 const String& TileObject2D::GetProperty(const String& name) const
 {
-    return properties_.GetProperty(name);
+    if (!properties_)
+        return String::EMPTY;
+    return properties_->GetProperty(name);
 }
 
 TmxLayer2D::TmxLayer2D(TmxFile2D* tmxFile, TmxLayerType2D type) :
@@ -145,12 +146,16 @@ TmxFile2D* TmxLayer2D::GetTmxFile() const
 
 bool TmxLayer2D::HasProperty(const String& name) const
 {
-    return properties_.HasProperty(name);
+    if (!properties_)
+        return false;
+    return properties_->HasProperty(name);
 }
 
 const String& TmxLayer2D::GetProperty(const String& name) const
 {
-    return properties_.GetProperty(name);
+    if (!properties_)
+        return String::EMPTY;
+    return properties_->GetProperty(name);
 }
 
 void TmxLayer2D::LoadInfo(const XMLElement& element)
@@ -166,7 +171,8 @@ void TmxLayer2D::LoadInfo(const XMLElement& element)
 
 void TmxLayer2D::LoadProperties(const XMLElement& element)
 {
-    properties_.Load(element);
+    properties_ = new Properties2D();
+    properties_->Load(element);
 }
 
 TmxTileLayer2D::TmxTileLayer2D(TmxFile2D* tmxFile) :
@@ -289,7 +295,10 @@ bool TmxObjectGroup2D::Load(const XMLElement& element)
         }
 
         if (objectElem.HasChild("properties"))
-            object->properties_.Load(objectElem.GetChild("properties"));
+        {
+            object->properties_ = new Properties2D();
+            object->properties_->Load(objectElem.GetChild("properties"));
+        }
 
         objects_.Push(object);
     }
@@ -467,12 +476,12 @@ Sprite2D* TmxFile2D::GetTileSprite(int gid) const
     return i->second_;
 }
 
-const Properties2D* TmxFile2D::GetTileProperties(int gid) const
+Properties2D* TmxFile2D::GetTileProperties(int gid) const
 {
-    HashMap<int, Properties2D>::ConstIterator i = tileProperties_.Find(gid);
+    HashMap<int, SharedPtr<Properties2D> >::ConstIterator i = tileProperties_.Find(gid);
     if (i == tileProperties_.End())
         return 0;
-    return &(i->second_);
+    return i->second_;
 }
 
 const TmxLayer2D* TmxFile2D::GetLayer(unsigned index) const
@@ -535,8 +544,8 @@ bool TmxFile2D::LoadTileSet(const XMLElement& element)
     {
         if (tileElem.HasChild("properties"))
         {
-            Properties2D properties;
-            properties.Load(tileElem.GetChild("properties"));
+            SharedPtr<Properties2D> properties(new Properties2D());
+            properties->Load(tileElem.GetChild("properties"));
             tileProperties_[firstgid + tileElem.GetInt("id")] = properties;
         }
     }

+ 9 - 7
Source/Engine/Urho2D/TmxFile2D.h

@@ -36,7 +36,7 @@ class XMLElement;
 class XMLFile;
 
 /// Peroperies.
-class URHO3D_API Properties2D
+class URHO3D_API Properties2D : public RefCounted
 {
 public:
     Properties2D();
@@ -77,7 +77,7 @@ private:
     /// Sprite.
     SharedPtr<Sprite2D> sprite_;
     /// Properties.
-    const Properties2D* properties_;
+    SharedPtr<Properties2D> properties_;
 };
 
 /// Tile object type.
@@ -93,6 +93,8 @@ enum TileObjectType2D
     OT_POLYLINE,
     /// Tile.
     OT_TILE,
+    /// Invalid.
+    OT_INVALID = 0xffff
 };
 
 /// Tile map object.
@@ -134,7 +136,7 @@ private:
     /// Sprite (for tile).
     SharedPtr<Sprite2D> sprite_;
     /// Properties.
-    Properties2D  properties_;
+    SharedPtr<Properties2D> properties_;
 };
 
 /// Tmx layer type.
@@ -147,7 +149,7 @@ enum TmxLayerType2D
     /// Image layer.
     LT_IMAGE_LAYER,
     /// Invalid.
-    LT_INVALID,
+    LT_INVALID = 0xffff
 };
 
 /// Tmx layer.
@@ -194,7 +196,7 @@ protected:
     /// Visible.
     bool visible_;
     /// Properties.
-    Properties2D properties_;
+    SharedPtr<Properties2D> properties_;
 };
 
 /// Tmx tile layer.
@@ -275,7 +277,7 @@ public:
     /// Return tile sprite by gid.
     Sprite2D* GetTileSprite(int gid) const;
     /// Return tile properties by gid.
-    const Properties2D* GetTileProperties(int gid) const;
+    Properties2D* GetTileProperties(int gid) const;
     /// Return number of layers.
     unsigned GetNumLayers() const { return layers_.Size(); }
     /// Return layer at index.
@@ -308,7 +310,7 @@ private:
     /// Gid to tile sprite mapping.
     HashMap<int, SharedPtr<Sprite2D> > tileSprites_;
     /// Gid to tile properties mapping.
-    HashMap<int, Properties2D> tileProperties_;
+    HashMap<int, SharedPtr<Properties2D> > tileProperties_;
     /// Layers.
     Vector<TmxLayer2D*> layers_;
 };