Changes to API

Should make integration with game engines easier in general.

Puts the burden of color parsing on Pytiled. As well as formatting layer data into a two dimensional list from a straight one dimensional.
This commit is contained in:
Darren Eberly
2021-02-21 00:58:30 -05:00
parent ed825c5b71
commit 674b4b50c4
24 changed files with 700 additions and 738 deletions

View File

@@ -10,56 +10,70 @@ EXPECTED = [
id=1,
size=common_types.Size(8, 6),
offset=common_types.OrderedPair(1, 3),
properties={"test": "test property",},
properties={
"test": "test property",
},
data=[
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
[
1,
2,
3,
4,
5,
6,
7,
8,
],
[
9,
10,
11,
12,
13,
14,
15,
16,
],
[
17,
18,
19,
20,
21,
22,
23,
24,
],
[
25,
26,
27,
28,
29,
30,
31,
32,
],
[
33,
34,
35,
36,
37,
38,
39,
40,
],
[
41,
42,
43,
44,
45,
46,
47,
48,
],
],
),
layer.LayerGroup(
@@ -94,7 +108,7 @@ EXPECTED = [
visible=True,
id=3,
image=Path("../../images/tile_04.png"),
transparent_color=common_types.Color("#000000"),
transparent_color=common_types.Color(0, 0, 0, 255),
),
layer.ImageLayer(
name="Image Layer 2",

View File

@@ -10,54 +10,66 @@ EXPECTED = [
id=1,
size=common_types.Size(8, 6),
data=[
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
[
1,
2,
3,
4,
5,
6,
7,
8,
],
[
9,
10,
11,
12,
13,
14,
15,
16,
],
[
17,
18,
19,
20,
21,
22,
23,
24,
],
[
25,
26,
27,
28,
29,
30,
31,
32,
],
[
33,
34,
35,
36,
37,
38,
39,
40,
],
[
41,
42,
43,
44,
45,
46,
47,
48,
],
],
),
layer.LayerGroup(
@@ -92,6 +104,6 @@ EXPECTED = [
visible=True,
id=3,
image=Path("../../images/tile_04.png"),
transparent_color=common_types.Color("#000000"),
transparent_color=common_types.Color(0, 0, 0, 255),
),
]

View File

@@ -10,54 +10,66 @@ EXPECTED = [
id=1,
size=common_types.Size(8, 6),
data=[
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
[
1,
2,
3,
4,
5,
6,
7,
8,
],
[
9,
10,
11,
12,
13,
14,
15,
16,
],
[
17,
18,
19,
20,
21,
22,
23,
24,
],
[
25,
26,
27,
28,
29,
30,
31,
32,
],
[
33,
34,
35,
36,
37,
38,
39,
40,
],
[
41,
42,
43,
44,
45,
46,
47,
48,
],
],
),
layer.LayerGroup(
@@ -92,6 +104,6 @@ EXPECTED = [
visible=True,
id=3,
image=Path("../../images/tile_04.png"),
transparent_color=common_types.Color("#000000"),
transparent_color=common_types.Color(0, 0, 0, 255),
),
]

View File

@@ -10,54 +10,66 @@ EXPECTED = [
id=1,
size=common_types.Size(8, 6),
data=[
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
[
1,
2,
3,
4,
5,
6,
7,
8,
],
[
9,
10,
11,
12,
13,
14,
15,
16,
],
[
17,
18,
19,
20,
21,
22,
23,
24,
],
[
25,
26,
27,
28,
29,
30,
31,
32,
],
[
33,
34,
35,
36,
37,
38,
39,
40,
],
[
41,
42,
43,
44,
45,
46,
47,
48,
],
],
),
layer.LayerGroup(
@@ -92,6 +104,6 @@ EXPECTED = [
visible=True,
id=3,
image=Path("../../images/tile_04.png"),
transparent_color=common_types.Color("#000000"),
transparent_color=common_types.Color(0, 0, 0, 255),
),
]

View File

@@ -10,82 +10,116 @@ EXPECTED = [
id=1,
size=common_types.Size(16, 16),
offset=common_types.OrderedPair(163.089434111595, 116.462603878116),
properties={"test": "test property",},
properties={
"test": "test property",
},
chunks=[
layer.Chunk(
coordinates=common_types.OrderedPair(0, 0),
size=common_types.Size(4, 8),
data=[
1,
2,
3,
4,
9,
10,
11,
12,
17,
18,
19,
20,
25,
26,
27,
28,
33,
34,
35,
36,
41,
42,
43,
44,
0,
0,
0,
0,
0,
0,
0,
0,
[
1,
2,
3,
4,
],
[
9,
10,
11,
12,
],
[
17,
18,
19,
20,
],
[
25,
26,
27,
28,
],
[
33,
34,
35,
36,
],
[
41,
42,
43,
44,
],
[
0,
0,
0,
0,
],
[
0,
0,
0,
0,
],
],
),
layer.Chunk(
coordinates=common_types.OrderedPair(4, 0),
size=common_types.Size(4, 8),
data=[
5,
6,
7,
8,
13,
14,
15,
16,
21,
22,
23,
24,
29,
30,
31,
32,
37,
38,
39,
40,
45,
46,
47,
48,
0,
0,
0,
0,
0,
0,
0,
0,
[
5,
6,
7,
8,
],
[
13,
14,
15,
16,
],
[
21,
22,
23,
24,
],
[
29,
30,
31,
32,
],
[
37,
38,
39,
40,
],
[
45,
46,
47,
48,
],
[
0,
0,
0,
0,
],
[
0,
0,
0,
0,
],
],
),
],

View File

@@ -10,268 +10,30 @@ EXPECTED = [
id=1,
size=common_types.Size(16, 16),
offset=common_types.OrderedPair(1, 3),
properties={"test": "test property",},
properties={
"test": "test property",
},
chunks=[
layer.Chunk(
coordinates=common_types.OrderedPair(0, 0),
size=common_types.Size(16, 16),
data=[
1,
2,
3,
4,
5,
6,
7,
8,
0,
0,
0,
0,
0,
0,
0,
0,
9,
10,
11,
12,
13,
14,
15,
16,
0,
0,
0,
0,
0,
0,
0,
0,
17,
18,
19,
20,
21,
22,
23,
24,
0,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, 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, 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, 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, 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],
[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, 0, 0, 0, 0, 0, 0, 0],
],
)
],

View File

@@ -15,106 +15,115 @@ EXPECTED = tiled_map.TiledMap(
size=common_types.Size(10, 10),
id=1,
data=[
3,
3,
3,
3,
9,
9,
9,
9,
17,
17,
3,
3,
3,
9,
9,
9,
9,
17,
17,
17,
3,
3,
3,
9,
9,
9,
9,
9,
17,
17,
3,
3,
1,
7,
9,
9,
9,
15,
17,
17,
1,
1,
12,
5,
7,
7,
7,
15,
15,
15,
12,
1,
5,
5,
7,
7,
7,
15,
15,
15,
2,
2,
5,
5,
5,
5,
4,
14,
14,
14,
2,
2,
5,
5,
5,
4,
14,
14,
14,
14,
2,
2,
2,
5,
5,
5,
4,
14,
14,
14,
2,
2,
2,
2,
5,
5,
4,
4,
14,
14,
[
3,
3,
3,
3,
9,
9,
9,
9,
17,
17,
],
[
3,
3,
3,
9,
9,
9,
9,
17,
17,
17,
],
[
3,
3,
3,
9,
9,
9,
9,
9,
17,
17,
],
[3, 3, 1, 7, 9, 9, 9, 15, 17, 17],
[
1,
1,
12,
5,
7,
7,
7,
15,
15,
15,
],
[
12,
1,
5,
5,
7,
7,
7,
15,
15,
15,
],
[
2,
2,
5,
5,
5,
5,
4,
14,
14,
14,
],
[
2,
2,
5,
5,
5,
4,
14,
14,
14,
14,
],
[
2,
2,
2,
5,
5,
5,
4,
14,
14,
14,
],
[
2,
2,
2,
2,
5,
5,
4,
4,
14,
14,
],
],
)
],

View File

@@ -13,7 +13,7 @@ EXPECTED = tiled_map.TiledMap(
tiled_version="1.4.1",
tile_size=common_types.Size(32, 32),
version=1.4,
background_color=common_types.Color("#ff0004"),
background_color=common_types.Color(255, 0, 4, 255),
tilesets={
1: tileset.Tileset(
columns=8,
@@ -33,7 +33,7 @@ EXPECTED = tiled_map.TiledMap(
},
properties={
"bool property - true": True,
"color property": common_types.Color("#ff49fcff"),
"color property": common_types.Color(255, 73, 252, 255),
"file property": Path("../../../../../../var/log/syslog"),
"float property": 1.23456789,
"int property": 13,

View File

@@ -0,0 +1,18 @@
import pytest
from pytiled_parser.util import parse_color
def test_parse_color_wrong_format():
with pytest.raises(ValueError):
color = parse_color("#ff0000ff0")
def test_parse_color_no_hash():
color = parse_color("ff0000")
assert color == (255, 0, 0, 255)
def test_parse_color_no_alpha():
color = parse_color("#ff0000")
assert color == (255, 0, 0, 255)

View File

@@ -16,6 +16,6 @@ EXPECTED = tileset.Tileset(
tile_height=32,
tile_width=32,
version=1.2,
background_color=Color("#5500ff"),
background_color=Color(85, 0, 255, 255),
type="tileset",
)

View File

@@ -18,7 +18,7 @@ EXPECTED = tileset.Tileset(
version=1.2,
properties={
"bool property": True,
"color property": Color("#ff0000ff"),
"color property": Color(255, 0, 0, 255),
"float property": 5.6,
"int property": 5,
"string property": "testing",

View File

@@ -16,6 +16,6 @@ EXPECTED = tileset.Tileset(
tile_height=32,
tile_width=32,
version=1.2,
transparent_color=Color("#ff00ff"),
transparent_color=Color(255, 0, 255, 255),
type="tileset",
)

View File

@@ -14,8 +14,8 @@ EXPECTED = tileset.Tileset(
version=1.2,
type="tileset",
grid=tileset.Grid(orientation="orthogonal", width=1, height=1),
tiles=[
tileset.Tile(
tiles={
0: tileset.Tile(
animation=[
tileset.Frame(duration=100, tile_id=0),
tileset.Frame(duration=100, tile_id=1),
@@ -29,7 +29,7 @@ EXPECTED = tileset.Tileset(
properties={"float property": 2.2},
type="tile",
),
tileset.Tile(
1: tileset.Tile(
id=1,
image=Path("../../images/tile_02.png"),
image_height=32,
@@ -67,7 +67,7 @@ EXPECTED = tileset.Tileset(
properties={"string property": "testing"},
type="tile",
),
tileset.Tile(
2: tileset.Tile(
id=2,
image=Path("../../images/tile_03.png"),
image_height=32,
@@ -75,12 +75,12 @@ EXPECTED = tileset.Tileset(
properties={"bool property": True},
type="tile",
),
tileset.Tile(
3: tileset.Tile(
id=3,
image=Path("../../images/tile_04.png"),
image_height=32,
image_width=32,
type="tile",
),
],
},
)

View File

@@ -26,246 +26,246 @@ EXPECTED = tileset.Tileset(
tileset.Terrain(name="Pavement", tile=29),
tileset.Terrain(name="Dirt", tile=29),
],
tiles=[
tileset.Tile(
tiles={
0: tileset.Tile(
id=0,
terrain=tileset.TileTerrain(
top_left=0, top_right=0, bottom_left=0, bottom_right=1
),
),
tileset.Tile(
1: tileset.Tile(
id=1,
terrain=tileset.TileTerrain(
top_left=0, top_right=0, bottom_left=1, bottom_right=1
),
),
tileset.Tile(
2: tileset.Tile(
id=2,
terrain=tileset.TileTerrain(
top_left=0, top_right=0, bottom_left=1, bottom_right=0
),
),
tileset.Tile(
3: tileset.Tile(
id=3,
terrain=tileset.TileTerrain(
top_left=3, top_right=3, bottom_left=3, bottom_right=0
),
),
tileset.Tile(
4: tileset.Tile(
id=4,
terrain=tileset.TileTerrain(
top_left=3, top_right=3, bottom_left=0, bottom_right=3
),
),
tileset.Tile(
5: tileset.Tile(
id=5,
terrain=tileset.TileTerrain(
top_left=0, top_right=0, bottom_left=0, bottom_right=3
),
),
tileset.Tile(
6: tileset.Tile(
id=6,
terrain=tileset.TileTerrain(
top_left=0, top_right=0, bottom_left=3, bottom_right=3
),
),
tileset.Tile(
7: tileset.Tile(
id=7,
terrain=tileset.TileTerrain(
top_left=0, top_right=0, bottom_left=3, bottom_right=0
),
),
tileset.Tile(
8: tileset.Tile(
id=8,
terrain=tileset.TileTerrain(
top_left=0, top_right=1, bottom_left=0, bottom_right=1
),
),
tileset.Tile(
9: tileset.Tile(
id=9,
terrain=tileset.TileTerrain(
top_left=1, top_right=1, bottom_left=1, bottom_right=1
),
),
tileset.Tile(
10: tileset.Tile(
id=10,
terrain=tileset.TileTerrain(
top_left=1, top_right=0, bottom_left=1, bottom_right=0
),
),
tileset.Tile(
11: tileset.Tile(
id=11,
terrain=tileset.TileTerrain(
top_left=3, top_right=0, bottom_left=3, bottom_right=3
),
),
tileset.Tile(
12: tileset.Tile(
id=12,
terrain=tileset.TileTerrain(
top_left=0, top_right=3, bottom_left=3, bottom_right=3
),
),
tileset.Tile(
13: tileset.Tile(
id=13,
terrain=tileset.TileTerrain(
top_left=0, top_right=3, bottom_left=0, bottom_right=3
),
),
tileset.Tile(
14: tileset.Tile(
id=14,
terrain=tileset.TileTerrain(
top_left=3, top_right=3, bottom_left=3, bottom_right=3
),
),
tileset.Tile(
15: tileset.Tile(
id=15,
terrain=tileset.TileTerrain(
top_left=3, top_right=0, bottom_left=3, bottom_right=0
),
),
tileset.Tile(
16: tileset.Tile(
id=16,
terrain=tileset.TileTerrain(
top_left=0, top_right=1, bottom_left=0, bottom_right=0
),
),
tileset.Tile(
17: tileset.Tile(
id=17,
terrain=tileset.TileTerrain(
top_left=1, top_right=1, bottom_left=0, bottom_right=0
),
),
tileset.Tile(
18: tileset.Tile(
id=18,
terrain=tileset.TileTerrain(
top_left=1, top_right=0, bottom_left=0, bottom_right=0
),
),
tileset.Tile(
19: tileset.Tile(
id=19,
terrain=tileset.TileTerrain(
top_left=1, top_right=1, bottom_left=1, bottom_right=0
),
),
tileset.Tile(
20: tileset.Tile(
id=20,
terrain=tileset.TileTerrain(
top_left=1, top_right=1, bottom_left=0, bottom_right=1
),
),
tileset.Tile(
21: tileset.Tile(
id=21,
terrain=tileset.TileTerrain(
top_left=0, top_right=3, bottom_left=0, bottom_right=0
),
),
tileset.Tile(
22: tileset.Tile(
id=22,
terrain=tileset.TileTerrain(
top_left=3, top_right=3, bottom_left=0, bottom_right=0
),
),
tileset.Tile(
23: tileset.Tile(
id=23,
terrain=tileset.TileTerrain(
top_left=3, top_right=0, bottom_left=0, bottom_right=0
),
),
tileset.Tile(
24: tileset.Tile(
id=24,
terrain=tileset.TileTerrain(
top_left=0, top_right=0, bottom_left=0, bottom_right=2
),
),
tileset.Tile(
25: tileset.Tile(
id=25,
terrain=tileset.TileTerrain(
top_left=0, top_right=0, bottom_left=2, bottom_right=2
),
),
tileset.Tile(
26: tileset.Tile(
id=26,
terrain=tileset.TileTerrain(
top_left=0, top_right=0, bottom_left=2, bottom_right=0
),
),
tileset.Tile(
27: tileset.Tile(
id=27,
terrain=tileset.TileTerrain(
top_left=1, top_right=0, bottom_left=1, bottom_right=1
),
),
tileset.Tile(
28: tileset.Tile(
id=28,
terrain=tileset.TileTerrain(
top_left=0, top_right=1, bottom_left=1, bottom_right=1
),
),
tileset.Tile(
29: tileset.Tile(
id=29,
terrain=tileset.TileTerrain(
top_left=0, top_right=0, bottom_left=0, bottom_right=0
),
),
tileset.Tile(
32: tileset.Tile(
id=32,
terrain=tileset.TileTerrain(
top_left=0, top_right=2, bottom_left=0, bottom_right=2
),
),
tileset.Tile(
33: tileset.Tile(
id=33,
terrain=tileset.TileTerrain(
top_left=2, top_right=2, bottom_left=2, bottom_right=2
),
),
tileset.Tile(
34: tileset.Tile(
id=34,
terrain=tileset.TileTerrain(
top_left=2, top_right=0, bottom_left=2, bottom_right=0
),
),
tileset.Tile(
35: tileset.Tile(
id=35,
terrain=tileset.TileTerrain(
top_left=2, top_right=2, bottom_left=2, bottom_right=0
),
),
tileset.Tile(
36: tileset.Tile(
id=36,
terrain=tileset.TileTerrain(
top_left=2, top_right=2, bottom_left=0, bottom_right=2
),
),
tileset.Tile(
40: tileset.Tile(
id=40,
terrain=tileset.TileTerrain(
top_left=0, top_right=2, bottom_left=0, bottom_right=0
),
),
tileset.Tile(
41: tileset.Tile(
id=41,
terrain=tileset.TileTerrain(
top_left=2, top_right=2, bottom_left=0, bottom_right=0
),
),
tileset.Tile(
42: tileset.Tile(
id=42,
terrain=tileset.TileTerrain(
top_left=2, top_right=0, bottom_left=0, bottom_right=0
),
),
tileset.Tile(
43: tileset.Tile(
id=43,
terrain=tileset.TileTerrain(
top_left=2, top_right=0, bottom_left=2, bottom_right=2
),
),
tileset.Tile(
44: tileset.Tile(
id=44,
terrain=tileset.TileTerrain(
top_left=0, top_right=2, bottom_left=2, bottom_right=2
),
),
],
},
)