Browse Source

TmxFile2D: Fix determination of object type

TmxObjectGroup2D::Load was using the presence of the "width" and
"height" attributes for deciding whether an object was a rectangle,
ellipse or something else. This is wrong, and the approach breaks for
maps saved in more recent versions of Tiled. This is because the width
and height are only saved when they differ from the default (0), and
because the width and height are now also used to store the size of tile
objects.

So for maps saved with Tiled >= 0.12, this change:

* Fixes identification of rectangle and ellipse objects with 0 width and
  height (which was leading to the entire map failing to load).
* Fixes identification of tile objects.
Thorbjørn Lindeijer 10 years ago
parent
commit
094d146c3c
1 changed files with 37 additions and 29 deletions
  1. 37 29
      Source/Urho3D/Urho2D/TmxFile2D.cpp

+ 37 - 29
Source/Urho3D/Urho2D/TmxFile2D.cpp

@@ -163,47 +163,52 @@ bool TmxObjectGroup2D::Load(const XMLElement& element, const TileMapInfo2D& info
         if (objectElem.HasAttribute("type"))
             object->type_ = objectElem.GetAttribute("type");
 
-        Vector2 position(objectElem.GetFloat("x"), objectElem.GetFloat("y"));
-
-        if (objectElem.HasAttribute("width") || objectElem.HasAttribute("height"))
-        {
-            if (!objectElem.HasChild("ellipse"))
-                object->objectType_ = OT_RECTANGLE;
-            else
-                object->objectType_ = OT_ELLIPSE;
+        if (objectElem.HasAttribute("gid"))
+            object->objectType_ = OT_TILE;
+        else if (objectElem.HasChild("polygon"))
+            object->objectType_ = OT_POLYGON;
+        else if (objectElem.HasChild("polyline"))
+            object->objectType_ = OT_POLYLINE;
+        else if (objectElem.HasChild("ellipse"))
+            object->objectType_ = OT_ELLIPSE;
+        else
+            object->objectType_ = OT_RECTANGLE;
 
-            Vector2 size(objectElem.GetFloat("width"), objectElem.GetFloat("height"));
+        const Vector2 position(objectElem.GetFloat("x"), objectElem.GetFloat("y"));
+        const Vector2 size(objectElem.GetFloat("width"), objectElem.GetFloat("height"));
 
+        switch (object->objectType_) {
+        case OT_RECTANGLE:
+        case OT_ELLIPSE:
             object->position_ = info.ConvertPosition(Vector2(position.x_, position.y_ + size.y_));
             object->size_ = Vector2(size.x_ * PIXEL_SIZE, size.y_ * PIXEL_SIZE);
-        }
-        else if (objectElem.HasAttribute("gid"))
-        {
-            object->objectType_ = OT_TILE;
+            break;
+
+        case OT_TILE:
             object->position_ = info.ConvertPosition(position);
             object->gid_ = objectElem.GetInt("gid");
             object->sprite_ = tmxFile_->GetTileSprite(object->gid_);
-        }
-        else
-        {
-            Vector<String> points;
 
-            if (objectElem.HasChild("polygon"))
+            if (objectElem.HasAttribute("width") || objectElem.HasAttribute("height"))
             {
-                object->objectType_ = OT_POLYGON;
-
-                XMLElement polygonElem = objectElem.GetChild("polygon");
-                points = polygonElem.GetAttribute("points").Split(' ');
+                object->size_ = Vector2(size.x_ * PIXEL_SIZE, size.y_ * PIXEL_SIZE);
             }
-            else if (objectElem.HasChild("polyline"))
+            else if (object->sprite_)
             {
-                object->objectType_ = OT_POLYLINE;
-
-                XMLElement polylineElem = objectElem.GetChild("polyline");
-                points = polylineElem.GetAttribute("points").Split(' ');
+                IntVector2 spriteSize = object->sprite_->GetRectangle().Size();
+                object->size_ = Vector2(spriteSize.x_, spriteSize.y_);
             }
-            else
-                return false;
+
+            break;
+
+        case OT_POLYGON:
+        case OT_POLYLINE:
+        {
+            Vector<String> points;
+
+            const char *name = object->objectType_ == OT_POLYGON ? "polygon" : "polyline";
+            XMLElement polygonElem = objectElem.GetChild(name);
+            points = polygonElem.GetAttribute("points").Split(' ');
 
             if (points.Size() <= 1)
                 continue;
@@ -216,6 +221,9 @@ bool TmxObjectGroup2D::Load(const XMLElement& element, const TileMapInfo2D& info
                 Vector2 point = position + ToVector2(points[i]);
                 object->points_[i] = info.ConvertPosition(point);
             }
+
+            break;
+        }
         }
 
         if (objectElem.HasChild("properties"))