Merge pull request #60 from dcronqvist/overrideobject-bug

Add object override for rectangle objects
This commit is contained in:
dcronqvist 2024-11-17 15:56:37 +01:00 committed by GitHub
commit 1e41443704
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 557 additions and 11 deletions

View file

@ -0,0 +1,242 @@
using System.Numerics;
namespace DotTiled.Tests;
public partial class TestData
{
public static Map MapOverrideObjectBug(string fileExt) => new Map
{
Class = "",
Orientation = MapOrientation.Orthogonal,
Width = 5,
Height = 5,
TileWidth = 32,
TileHeight = 32,
Infinite = false,
ParallaxOriginX = 0,
ParallaxOriginY = 0,
RenderOrder = RenderOrder.RightDown,
CompressionLevel = -1,
BackgroundColor = new Color { R = 0, G = 0, B = 0, A = 0 },
Version = "1.10",
TiledVersion = "1.11.0",
NextLayerID = 8,
NextObjectID = 8,
Tilesets = [
new Tileset
{
Version = "1.10",
TiledVersion = "1.11.0",
FirstGID = 1,
Name = "tileset",
TileWidth = 32,
TileHeight = 32,
TileCount = 24,
Columns = 8,
Source = $"tileset.{(fileExt == "tmx" ? "tsx" : "tsj")}",
Image = new Image
{
Format = ImageFormat.Png,
Source = "tileset.png",
Width = 256,
Height = 96,
}
}
],
Layers = [
new Group
{
ID = 2,
Name = "Root",
Layers = [
new ObjectLayer
{
ID = 3,
Name = "Objects",
Objects = [
new RectangleObject
{
ID = 1,
Name = "Object 1",
X = 25.6667f,
Y = 28.6667f,
Width = 31.3333f,
Height = 31.3333f
},
new PointObject
{
ID = 3,
Name = "P1",
X = 117.667f,
Y = 48.6667f
},
new EllipseObject
{
ID = 4,
Name = "Circle1",
X = 77f,
Y = 72.3333f,
Width = 34.6667f,
Height = 34.6667f
},
new PolygonObject
{
ID = 5,
Name = "Poly",
X = 20.6667f,
Y = 114.667f,
Points = [
new Vector2(0, 0),
new Vector2(104,20),
new Vector2(35.6667f, 32.3333f)
],
Template = fileExt == "tmx" ? "poly.tx" : "poly.tj",
Properties = [
new StringProperty { Name = "templateprop", Value = "helo there" }
]
},
new TileObject
{
ID = 6,
Name = "TileObj",
GID = 7,
X = -35,
Y = 110.333f,
Width = 64,
Height = 146
},
new RectangleObject
{
ID = 7,
Name = "",
Template = fileExt == "tmx" ? "random.tx" : "random.tj",
Type = "randomclass",
X = 134.552f,
Y = 113.638f
}
]
},
new Group
{
ID = 5,
Name = "Sub",
Layers = [
new TileLayer
{
ID = 7,
Name = "Tile 3",
Width = 5,
Height = 5,
Data = new Data
{
Encoding = DataEncoding.Csv,
GlobalTileIDs = new Optional<uint[]>([
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0
]),
FlippingFlags = new Optional<FlippingFlags[]>([
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None
])
}
},
new TileLayer
{
ID = 6,
Name = "Tile 2",
Width = 5,
Height = 5,
Data = new Data
{
Encoding = DataEncoding.Csv,
GlobalTileIDs = new Optional<uint[]>([
0, 15, 15, 0, 0,
0, 15, 15, 0, 0,
0, 15, 15, 15, 0,
15, 15, 15, 0, 0,
0, 0, 0, 0, 0
]),
FlippingFlags = new Optional<FlippingFlags[]>([
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None
])
}
}
]
},
new ImageLayer
{
ID = 4,
Name = "ImageLayer",
Image = new Image
{
Format = ImageFormat.Png,
Source = "tileset.png",
Width = fileExt == "tmx" ? 256u : 0, // Currently, json format does not
Height = fileExt == "tmx" ? 96u : 0 // include image dimensions in image layer https://github.com/mapeditor/tiled/issues/4028
},
RepeatX = true
},
new TileLayer
{
ID = 1,
Name = "Tile Layer 1",
Width = 5,
Height = 5,
Data = new Data
{
Encoding = DataEncoding.Csv,
GlobalTileIDs = new Optional<uint[]>([
1, 1, 1, 1, 1,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0
]),
FlippingFlags = new Optional<FlippingFlags[]>([
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None
])
}
}
]
}
]
};
public static IReadOnlyCollection<ICustomTypeDefinition> MapOverrideObjectBugCustomTypeDefinitions() => [
new CustomClassDefinition
{
Name = "TestClass",
UseAs = CustomClassUseAs.Map,
Members = [
new BoolProperty
{
Name = "classbool",
Value = true
},
new StringProperty
{
Name = "classstring",
Value = "Hello there default value"
}
]
},
new CustomClassDefinition
{
Name = "randomclass"
}
];
}

View file

@ -0,0 +1,171 @@
{ "compressionlevel":-1,
"height":5,
"infinite":false,
"layers":[
{
"id":2,
"layers":[
{
"draworder":"topdown",
"id":3,
"name":"Objects",
"objects":[
{
"height":31.3333,
"id":1,
"name":"Object 1",
"rotation":0,
"type":"",
"visible":true,
"width":31.3333,
"x":25.6667,
"y":28.6667
},
{
"height":0,
"id":3,
"name":"P1",
"point":true,
"rotation":0,
"type":"",
"visible":true,
"width":0,
"x":117.667,
"y":48.6667
},
{
"ellipse":true,
"height":34.6667,
"id":4,
"name":"Circle1",
"rotation":0,
"type":"",
"visible":true,
"width":34.6667,
"x":77,
"y":72.3333
},
{
"id":5,
"template":"poly.tj",
"x":20.6667,
"y":114.667
},
{
"gid":7,
"height":146,
"id":6,
"name":"TileObj",
"rotation":0,
"type":"",
"visible":true,
"width":64,
"x":-35,
"y":110.333
},
{
"id":7,
"template":"random.tj",
"type":"randomclass",
"x":134.551764025448,
"y":113.637941006362
}],
"opacity":1,
"type":"objectgroup",
"visible":true,
"x":0,
"y":0
},
{
"id":5,
"layers":[
{
"data":[0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0],
"height":5,
"id":7,
"name":"Tile 3",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
},
{
"data":[0, 15, 15, 0, 0,
0, 15, 15, 0, 0,
0, 15, 15, 15, 0,
15, 15, 15, 0, 0,
0, 0, 0, 0, 0],
"height":5,
"id":6,
"name":"Tile 2",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
}],
"name":"Sub",
"opacity":1,
"type":"group",
"visible":true,
"x":0,
"y":0
},
{
"id":4,
"image":"tileset.png",
"name":"ImageLayer",
"opacity":1,
"repeatx":true,
"type":"imagelayer",
"visible":true,
"x":0,
"y":0
},
{
"data":[1, 1, 1, 1, 1,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0],
"height":5,
"id":1,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
}],
"name":"Root",
"opacity":1,
"type":"group",
"visible":true,
"x":0,
"y":0
}],
"nextlayerid":8,
"nextobjectid":8,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.11.0",
"tileheight":32,
"tilesets":[
{
"firstgid":1,
"source":"tileset.tsj"
}],
"tilewidth":32,
"type":"map",
"version":"1.10",
"width":5
}

View file

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="5" height="5" tilewidth="32" tileheight="32" infinite="0" nextlayerid="8" nextobjectid="8">
<tileset firstgid="1" source="tileset.tsx"/>
<group id="2" name="Root">
<objectgroup id="3" name="Objects">
<object id="1" name="Object 1" x="25.6667" y="28.6667" width="31.3333" height="31.3333"/>
<object id="3" name="P1" x="117.667" y="48.6667">
<point/>
</object>
<object id="4" name="Circle1" x="77" y="72.3333" width="34.6667" height="34.6667">
<ellipse/>
</object>
<object id="5" template="poly.tx" x="20.6667" y="114.667"/>
<object id="6" name="TileObj" gid="7" x="-35" y="110.333" width="64" height="146"/>
<object id="7" template="random.tx" type="randomclass" x="134.552" y="113.638"/>
</objectgroup>
<group id="5" name="Sub">
<layer id="7" name="Tile 3" width="5" height="5">
<data encoding="csv">
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0
</data>
</layer>
<layer id="6" name="Tile 2" width="5" height="5">
<data encoding="csv">
0,15,15,0,0,
0,15,15,0,0,
0,15,15,15,0,
15,15,15,0,0,
0,0,0,0,0
</data>
</layer>
</group>
<imagelayer id="4" name="ImageLayer" repeatx="1">
<image source="tileset.png" width="256" height="96"/>
</imagelayer>
<layer id="1" name="Tile Layer 1" width="5" height="5">
<data encoding="csv">
1,1,1,1,1,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0
</data>
</layer>
</group>
</map>

View file

@ -0,0 +1,31 @@
{ "object":
{
"height":0,
"id":5,
"name":"Poly",
"polygon":[
{
"x":0,
"y":0
},
{
"x":104,
"y":20
},
{
"x":35.6667,
"y":32.3333
}],
"properties":[
{
"name":"templateprop",
"type":"string",
"value":"helo there"
}],
"rotation":0,
"type":"",
"visible":true,
"width":0
},
"type":"template"
}

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<template>
<object name="Poly">
<properties>
<property name="templateprop" value="helo there"/>
</properties>
<polygon points="0,0 104,20 35.6667,32.3333"/>
</object>
</template>

View file

@ -0,0 +1,12 @@
{ "object":
{
"height":0,
"id":7,
"name":"",
"rotation":0,
"type":"randomclass",
"visible":true,
"width":0
},
"type":"template"
}

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<template>
<object type="randomclass"/>
</template>

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View file

@ -0,0 +1,14 @@
{ "columns":8,
"image":"tileset.png",
"imageheight":96,
"imagewidth":256,
"margin":0,
"name":"tileset",
"spacing":0,
"tilecount":24,
"tiledversion":"1.11.0",
"tileheight":32,
"tilewidth":32,
"type":"tileset",
"version":"1.10"
}

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.11.0" name="tileset" tilewidth="32" tileheight="32" tilecount="24" columns="8">
<image source="tileset.png" width="256" height="96"/>
</tileset>

View file

@ -46,5 +46,6 @@ public static partial class TestData
[GetMapPath("map-with-deep-props"), (string f) => MapWithDeepProps(), MapWithDeepPropsCustomTypeDefinitions()], [GetMapPath("map-with-deep-props"), (string f) => MapWithDeepProps(), MapWithDeepPropsCustomTypeDefinitions()],
[GetMapPath("map-with-class"), (string f) => MapWithClass(), MapWithClassCustomTypeDefinitions()], [GetMapPath("map-with-class"), (string f) => MapWithClass(), MapWithClassCustomTypeDefinitions()],
[GetMapPath("map-with-class-and-props"), (string f) => MapWithClassAndProps(), MapWithClassAndPropsCustomTypeDefinitions()], [GetMapPath("map-with-class-and-props"), (string f) => MapWithClassAndProps(), MapWithClassAndPropsCustomTypeDefinitions()],
[GetMapPath("map-override-object-bug"), (string f) => MapOverrideObjectBug(f), MapOverrideObjectBugCustomTypeDefinitions()],
]; ];
} }

View file

@ -148,8 +148,6 @@ public abstract partial class TmxReaderBase
if (obj is null) if (obj is null)
return foundObject; return foundObject;
if (obj.GetType() != foundObject.GetType())
{
obj.ID = foundObject.ID; obj.ID = foundObject.ID;
obj.Name = foundObject.Name; obj.Name = foundObject.Name;
obj.Type = foundObject.Type; obj.Type = foundObject.Type;
@ -161,6 +159,9 @@ public abstract partial class TmxReaderBase
obj.Visible = foundObject.Visible; obj.Visible = foundObject.Visible;
obj.Properties = Helpers.MergeProperties(obj.Properties, foundObject.Properties).ToList(); obj.Properties = Helpers.MergeProperties(obj.Properties, foundObject.Properties).ToList();
obj.Template = foundObject.Template; obj.Template = foundObject.Template;
if (obj.GetType() != foundObject.GetType())
{
return obj; return obj;
} }
@ -231,6 +232,13 @@ public abstract partial class TmxReaderBase
return obj; return obj;
} }
internal static RectangleObject OverrideObject(RectangleObject obj, RectangleObject foundObject)
{
obj.Width = foundObject.Width;
obj.Height = foundObject.Height;
return obj;
}
internal TextObject ReadTextObject() internal TextObject ReadTextObject()
{ {
// Attributes // Attributes