-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtmx.py
1288 lines (971 loc) · 43 KB
/
tmx.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Simple TMX library
# Copyright (c) 2014-2016 onpon4 <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This library reads and writes the Tiled TMX format in a simple way.
This is useful for map editors or generic level editors, and it's also
useful for using a map editor or generic level editor like Tiled to edit
your game's levels.
To load a TMX file, use :meth:`tmx.TileMap.load`. You can then read the
attributes of the returned :class:`tmx.TileMap` object, modify the
attributes to your liking, and save your changes with
:meth:`tmx.TileMap.save`. That's it! Simple, isn't it?
At the request of the developer of Tiled, this documentation does not
explain in detail what each attribute means. For that, please see the
TMX format specification, found here:
http://doc.mapeditor.org/reference/tmx-map-format/
"""
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
__version__ = "1.8.1"
import os
import xml.etree.ElementTree as ET
import base64
import gzip
import zlib
import warnings
import six
__all__ = ["TileMap", "Image", "ImageLayer", "Layer", "LayerTile", "Object",
"ObjectGroup", "Property", "TerrainType", "Tile", "Tileset",
"Frame", "data_decode", "data_encode"]
class TileMap(object):
"""
This class loads, stores, and saves TMX files.
.. attribute:: version
The TMX format version.
.. attribute:: orientation
Map orientation. Can be "orthogonal", "isometric", "staggered",
or "hexagonal".
.. attribute:: renderorder
The order in which tiles are rendered. Can be ``"right-down"``,
``"right-up"``, ``"left-down"``, or ``"left-up"``. Default is
``"right-down"``.
.. attribute:: width
The width of the map in tiles.
.. attribute:: height
The height of the map in tiles.
.. attribute:: tilewidth
The width of a tile.
.. attribute:: tileheight
The height of a tile.
.. attribute:: staggeraxis
Determines which axis is staggered. Can be "x" or "y". Set to
:const:`None` to not set it. Only meaningful for staggered and
hexagonal maps.
.. attribute:: staggerindex
Determines what indexes along the staggered axis are shifted.
Can be "even" or "odd". Set to :const:`None` to not set it.
Only meaningful for staggered and hexagonal maps.
.. attribute:: hexsidelength
Side length of the hexagon in hexagonal tiles. Set to
:const:`None` to not set it. Only meaningful for hexagonal maps.
.. attribute:: backgroundcolor
The background color of the map as a hex string (e.g.
``"FF0000"`` or ``"#00FF00"``), or :const:`None` if no background
color is defined.
.. attribute:: nextobjectid
The next available ID for new objects. Set to :const:`None` to
not set it.
.. attribute:: properties
A list of :class:`Property` objects indicating the map's
properties.
.. attribute:: tilesets
A list of :class:`Tileset` objects indicating the map's tilesets.
.. attribute:: layers
A list of :class:`Layer`, :class:`ObjectGroup`, and
:class:`ImageLayer` objects indicating the map's tile layers,
object groups, and image layers, respectively. Those that appear
in this list first are rendered first (i.e. furthest in the
back).
"""
def __init__(self):
self.version = "1.0"
self.orientation = "orthogonal"
self.renderorder = "right-down"
self.width = 0
self.height = 0
self.tilewidth = 32
self.tileheight = 32
self.staggeraxis = None
self.staggerindex = None
self.hexsidelength = None
self.backgroundcolor = None
self.nextobjectid = None
self.properties = []
self.tilesets = []
self.layers = []
@classmethod
def load(cls, fname):
"""
Load the TMX file with the indicated name and return a
:class:`TileMap` object representing it.
"""
self = cls()
tree = ET.parse(fname)
root = tree.getroot()
fd = os.path.dirname(fname)
self.version = root.attrib.get("version", self.version)
self.orientation = root.attrib.get("orientation", self.orientation)
self.renderorder = root.attrib.get("renderorder", self.renderorder)
self.width = int(root.attrib.get("width", self.width))
self.height = int(root.attrib.get("height", self.height))
self.tilewidth = int(root.attrib.get("tilewidth", self.tilewidth))
self.tileheight = int(root.attrib.get("tileheight", self.tileheight))
self.staggeraxis = root.attrib.get("staggeraxis", self.staggeraxis)
self.staggerindex = root.attrib.get("staggerindex", self.staggerindex)
self.hexsidelength = root.attrib.get("hexsidelength",
self.hexsidelength)
if self.hexsidelength is not None:
self.hexsidelength = int(self.hexsidelength)
self.backgroundcolor = root.attrib.get("backgroundcolor")
self.nextobjectid = root.attrib.get("nextobjectid", self.nextobjectid)
if self.nextobjectid is not None:
self.nextobjectid = int(self.nextobjectid)
def get_properties(properties_root):
properties = []
for prop in properties_root.findall("property"):
name = prop.attrib.get("name")
value = prop.attrib.get("value")
type_ = prop.attrib.get("type", "string")
if type_ == "bool":
value = (value.lower() == "true")
elif type_ == "int":
value = int(value)
elif type_ == "float":
value = float(value)
properties.append(Property(name, value))
return properties
def get_image(image_root, fd=fd):
format_ = image_root.attrib.get("format")
source = image_root.attrib.get("source")
if source is not None:
source = os.path.join(fd, source)
trans = image_root.attrib.get("trans")
width = image_root.attrib.get("width")
height = image_root.attrib.get("height")
data = None
for child in image_root:
if child.tag == "data":
data = child.text.strip()
return Image(format_, source, trans, width, height, data)
def get_animation(animation_root):
animation = []
for child in animation_root.findall("frame"):
tid = child.attrib.get("tileid")
if tid is not None:
tid = int(tid)
else:
tid = 0
duration = child.attrib.get("duration")
if duration is not None:
duration = int(duration)
else:
duration = 0
animation.append(Frame(tid, duration))
return animation
for child in root:
if child.tag == "properties":
self.properties.extend(get_properties(child))
elif child.tag == "tileset":
firstgid = int(child.attrib.get("firstgid"))
source = child.attrib.get("source")
if source is not None:
source = os.path.join(fd, source)
troot = ET.parse(source).getroot()
td = os.path.dirname(source)
else:
troot = child
td = fd
name = troot.attrib.get("name", "")
tilewidth = int(troot.attrib.get("tilewidth", 32))
tileheight = int(troot.attrib.get("tileheight", 32))
spacing = int(troot.attrib.get("spacing", 0))
margin = int(troot.attrib.get("margin", 0))
tilecount = troot.attrib.get("tilecount")
if tilecount is not None:
tilecount = int(tilecount)
columns = troot.attrib.get("columns")
if columns is not None:
columns = int(columns)
xoffset = 0
yoffset = 0
properties = []
image = None
terraintypes = []
tiles = []
for tchild in troot:
if tchild.tag == "tileoffset":
xoffset = int(tchild.attrib.get("x", xoffset))
yoffset = int(tchild.attrib.get("y", yoffset))
elif tchild.tag == "properties":
properties.extend(get_properties(tchild))
elif tchild.tag == "image":
image = get_image(tchild, td)
elif tchild.tag == "terraintypes":
for terrain in tchild.findall("terrain"):
trname = terrain.attrib.get("name")
trtile = terrain.attrib.get("tile")
trproperties = []
for trchild in terrain:
if trchild.tag == "properties":
trproperties.extend(get_properties(
trchild))
terraintypes.append(TerrainType(trname, trtile,
trproperties))
elif tchild.tag == "tile":
tid = tchild.attrib.get("id")
if tid is not None:
tid = int(tid)
titerrain = tchild.attrib.get("terrain")
tiprobability = tchild.attrib.get("probability")
tiproperties = []
timage = None
tianimation = None
for tichild in tchild:
if tichild.tag == "properties":
tiproperties.extend(get_properties(tichild))
elif tichild.tag == "image":
timage = get_image(tichild, td)
elif tichild.tag == "animation":
tianimation = get_animation(tichild)
tiles.append(Tile(tid, titerrain, tiprobability,
tiproperties, timage, tianimation))
self.tilesets.append(Tileset(firstgid, name, tilewidth,
tileheight, source, spacing,
margin, xoffset, yoffset,
tilecount, columns, properties,
image, terraintypes, tiles))
elif child.tag == "layer":
name = child.attrib.get("name", "")
opacity = float(child.attrib.get("opacity", 1))
visible = bool(int(child.attrib.get("visible", True)))
offsetx = int(child.attrib.get("offsetx", 0))
offsety = int(child.attrib.get("offsety", 0))
properties = []
tiles = []
for lchild in child:
if lchild.tag == "properties":
properties.extend(get_properties(lchild))
elif lchild.tag == "data":
encoding = lchild.attrib.get("encoding")
compression = lchild.attrib.get("compression")
if encoding:
tile_n = data_decode(lchild.text, encoding,
compression)
else:
tile_n = [int(tile.attrib.get("gid", 0))
for tile in lchild.findall("tile")]
for n in tile_n:
gid = (n - (n & 2 ** 31) - (n & 2 ** 30) -
(n & 2 ** 29))
hflip = bool(n & 2 ** 31)
vflip = bool(n & 2 ** 30)
dflip = bool(n & 2 ** 29)
tiles.append(LayerTile(gid, hflip, vflip, dflip))
self.layers.append(Layer(name, opacity, visible, offsetx,
offsety, properties, tiles))
elif child.tag == "objectgroup":
name = child.attrib.get("name", "")
color = child.attrib.get("color")
opacity = float(child.attrib.get("opacity", 1))
visible = bool(int(child.attrib.get("visible", True)))
offsetx = int(child.attrib.get("offsetx", 0))
offsety = int(child.attrib.get("offsety", 0))
draworder = child.attrib.get("draworder")
properties = []
objects = []
for ogchild in child:
if ogchild.tag == "properties":
properties.extend(get_properties(ogchild))
elif ogchild.tag == "object":
oid = ogchild.attrib.get("id")
oname = ogchild.attrib.get("name", "")
otype = ogchild.attrib.get("type", "")
ox = int(ogchild.attrib.get("x", 0))
oy = int(ogchild.attrib.get("y", 0))
owidth = int(ogchild.attrib.get("width", 0))
oheight = int(ogchild.attrib.get("height", 0))
orotation = float(ogchild.attrib.get("rotation", 0))
ogid = ogchild.attrib.get("gid")
if ogid is not None:
ogid = int(ogid)
ovisible = bool(int(ogchild.attrib.get("visible",
True)))
oproperties = []
oellipse = False
opolygon = None
opolyline = None
for ochild in ogchild:
if ochild.tag == "properties":
oproperties.extend(get_properties(ochild))
elif ochild.tag == "ellipse":
oellipse = True
elif ochild.tag == "polygon":
s = ochild.attrib.get("points", "").strip()
opolygon = []
for coord in s.split():
pos = []
for n in coord.split(','):
if n.isdigit():
pos.append(int(n))
else:
pos.append(float(n))
opolygon.append(tuple(pos))
elif ochild.tag == "polyline":
s = ochild.attrib.get("points", "").strip()
opolyline = []
for coord in s.split():
pos = []
for n in coord.split(','):
if n.isdigit():
pos.append(int(n))
else:
pos.append(float(n))
opolyline.append(tuple(pos))
objects.append(Object(oname, otype, ox, oy, owidth,
oheight, orotation, ogid,
ovisible, oproperties, oellipse,
opolygon, opolyline, oid))
self.layers.append(ObjectGroup(name, color, opacity, visible,
offsetx, offsety, draworder,
properties, objects))
elif child.tag == "imagelayer":
name = child.attrib.get("name", "")
x = int(child.attrib.get("offsetx", child.attrib.get("x", 0)))
y = int(child.attrib.get("offsety", child.attrib.get("y", 0)))
opacity = float(child.attrib.get("opacity", 1))
visible = bool(int(child.attrib.get("visible", True)))
properties = []
image = None
for ilchild in child:
if ilchild.tag == "properties":
properties.extend(get_properties(ilchild))
elif ilchild.tag == "image":
image = get_image(ilchild)
self.layers.append(ImageLayer(name, x, y, opacity, visible,
properties, image))
return self
def save(self, fname, data_encoding="base64", data_compression=True):
"""
Save the object to the file with the indicated name.
Arguments:
- ``data_encoding`` -- The encoding to use for layers. Can be
``"base64"`` or ``"csv"``. Set to :const:`None` for the
default encoding (currently ``"base64"``).
- ``data_compression`` -- Whether or not compression should be
used on layers if possible (currently only possible for
base64-encoded data).
"""
if data_encoding is None:
data_encoding = "base64"
def clean_attr(d):
new_d = {}
for i in d:
if d[i] is not None:
new_d[i] = str(d[i])
return new_d
attr = {"version": self.version, "orientation": self.orientation,
"renderorder": self.renderorder, "width": self.width,
"height": self.height, "tilewidth": self.tilewidth,
"tileheight": self.tileheight, "staggeraxis": self.staggeraxis,
"staggerindex": self.staggerindex,
"hexsidelength": self.hexsidelength,
"backgroundcolor": self.backgroundcolor,
"nextobjectid": self.nextobjectid}
root = ET.Element("map", attrib=clean_attr(attr))
fd = os.path.dirname(fname)
def get_properties_elem(properties):
elem = ET.Element("properties")
for prop in properties:
value = str(prop.value)
type_ = None
if isinstance(prop.value, bool):
value = "true" if prop.value else "false"
type_ = "bool"
elif isinstance(prop.value, int):
type_ = "int"
elif isinstance(prop.value, float):
type_ = "float"
prop_attr = {"name": prop.name, "value": value}
if type_:
prop_attr["type"] = type_
elem.append(ET.Element("property",
attrib=clean_attr(prop_attr)))
return elem
def get_image_elem(image_obj, fd=fd):
attr = {"format": image_obj.format, "trans": image_obj.trans,
"width": image_obj.width, "height": image_obj.height}
if image_obj.source:
attr["source"] = os.path.relpath(image_obj.source, fd)
elem = ET.Element("image", attrib=clean_attr(attr))
if image_obj.data is not None:
data_elem = ET.Element("data")
data_elem.text = image_obj.data
elem.append(data_elem)
return elem
def get_animation_elem(animation, fd=fd):
elem = ET.Element("animation")
for animation_obj in animation:
attr = {"tileid": animation_obj.tileid,
"duration": animation_obj.duration}
elem.append(ET.Element("frame", attrib=clean_attr(attr)))
return elem
if self.properties:
root.append(get_properties_elem(self.properties))
for tileset in self.tilesets:
attr = {"firstgid": tileset.firstgid, "name": tileset.name,
"tilewidth": tileset.tilewidth,
"tileheight": tileset.tileheight}
if tileset.source:
attr["source"] = os.path.relpath(tileset.source, fd)
if tileset.spacing:
attr["spacing"] = tileset.spacing
if tileset.margin:
attr["margin"] = tileset.margin
if tileset.tilecount:
attr["tilecount"] = tileset.tilecount
if tileset.columns:
attr["columns"] = tileset.columns
elem = ET.Element("tileset", attrib=clean_attr(attr))
if tileset.xoffset or tileset.yoffset:
attr = {"x": tileset.xoffset, "y": tileset.yoffset}
offset_elem = ET.Element("tileoffset", attrib=clean_attr(attr))
elem.append(offset_elem)
if tileset.properties:
elem.append(get_properties_elem(tileset.properties))
if tileset.image:
elem.append(get_image_elem(tileset.image))
if tileset.animation:
elem.append(get_animation_elem(tileset.animation))
if tileset.terraintypes:
ttypes_elem = ET.Element("terraintypes")
for terrain in tileset.terraintypes:
attr = {"name": terrain.name, "tile": terrain.tile}
terrain_elem = ET.Element("terrain",
attrib=clean_attr(attr))
if terrain.properties:
prop_elem = get_properties_elem(terrain.properties)
terrain_elem.append(prop_elem)
ttypes_elem.append(terrain_elem)
elem.append(ttypes_elem)
for tile in tileset.tiles:
attr = {"id": tile.id, "terrain": tile.terrain,
"probability": tile.probability}
tile_elem = ET.Element("tile", attrib=clean_attr(attr))
if tile.properties:
tile_elem.append(get_properties_elem(tile.properties))
if tile.image:
tile_elem.append(get_image_elem(tile.image))
elem.append(tile_elem)
root.append(elem)
for layer in self.layers:
if isinstance(layer, Layer):
attr = {"name": layer.name}
if layer.opacity != 1:
attr["opacity"] = layer.opacity
if not layer.visible:
attr["visible"] = "0"
if layer.offsetx:
attr["offsetx"] = layer.offsetx
if layer.offsety:
attr["offsety"] = layer.offsety
elem = ET.Element("layer", attrib=clean_attr(attr))
if layer.properties:
elem.append(get_properties_elem(layer.properties))
tile_n = [int(i) for i in layer.tiles]
attr = {"encoding": data_encoding,
"compression": "zlib" if data_compression else None}
data_elem = ET.Element("data", attrib=clean_attr(attr))
data_elem.text = data_encode(tile_n, data_encoding,
data_compression)
elem.append(data_elem)
root.append(elem)
elif isinstance(layer, ObjectGroup):
attr = {"name": objectgroup.name, "color": objectgroup.color}
if objectgroup.opacity != 1:
attr["opacity"] = objectgroup.opacity
if not objectgroup.visible:
attr["visible"] = "0"
if layer.offsetx:
attr["offsetx"] = layer.offsetx
if layer.offsety:
attr["offsety"] = layer.offsety
if layer.draworder:
attr["draworder"] = layer.draworder
elem = ET.Element("objectgroup", attrib=clean_attr(attr))
if objectgroup.properties:
elem.append(get_properties_elem(objectgroup.properties))
for obj in objectgroup.objects:
attr = {"id": obj.id, "name": obj.name, "type": obj.type,
"x": obj.x, "y": obj.y, "gid": obj.gid}
if obj.width:
attr["width"] = obj.width
if obj.height:
attr["height"] = obj.height
if obj.rotation:
attr["rotation"] = obj.rotation
if not obj.visible:
attr["visible"] = "0"
object_elem = ET.Element("object", attrib=clean_attr(attr))
if obj.ellipse:
object_elem.append(ET.Element("ellipse"))
elif obj.polygon is not None:
points = ' '.join(['{},{}'.format(*T)
for T in obj.polygon])
poly_elem = ET.Element("polygon",
attrib={"points": points})
object_elem.append(poly_elem)
elif obj.polyline is not None:
points = ' '.join(['{},{}'.format(*T)
for T in obj.polyline])
poly_elem = ET.Element("polyline",
attrib={"points": points})
object_elem.append(poly_elem)
elem.append(object_elem)
root.append(elem)
elif isinstance(layer, ImageLayer):
attr = {"name": imagelayer.name, "offsetx": imagelayer.offsetx,
"offsety": imagelayer.offsety, "x": imagelayer.offsetx,
"y": imagelayer.offsety}
if imagelayer.opacity != 1:
attr["opacity"] = imagelayer.opacity
if not imagelayer.visible:
attr["visible"] = "0"
elem = ET.Element("imagelayer", attrib=clean_attr(attr))
if imagelayer.properties:
elem.append(get_properties_elem(imagelayer.properties))
if imagelayer.image:
elem.append(get_image_elem(imagelayer.image))
root.append(elem)
else:
e = "{} is not a supported layer type.".format(
layer.__class__.__name__)
raise ValueError(e)
tree = ET.ElementTree(root)
tree.write(fname, encoding="UTF-8", xml_declaration=True)
class Image(object):
"""
.. attribute:: format
Indicates the format of image data if embedded. Should be an
extension like ``"png"``, ``"gif"``, ``"jpg"``, or ``"bmp"``.
Set to :const:`None` to not specify the format.
.. attribute:: source
The location of the image file referenced. If set to
:const:`None`, the image data is embedded.
.. attribute:: trans
The transparent color of the image as a hex string (e.g.
``"FF0000"`` or ``"#00FF00"``), or :const:`None` if no color is
treated as transparent.
.. attribute:: width
The width of the image in pixels; used for tile index correction
when the image changes. If set to :const:`None`, the image width
is not explicitly specified.
.. attribute:: height
The height of the image in pixels; used for tile index correction
when the image changes. If set to :const:`None`, the image
height is not explicitly specified.
.. attribute:: data
The image data if embedded, or :const:`None` if an external image
is referenced.
"""
def __init__(self, format_=None, source=None, trans=None, width=None,
height=None, data=None):
self.format = format_
self.source = source
self.trans = trans
self.width = width
self.height = height
self.data = data
class ImageLayer(object):
"""
.. attribute:: name
The name of the image layer.
.. attribute:: offsetx
The x position of the image layer in pixels.
.. attribute:: offsety
The y position of the image layer in pixels.
.. attribute:: opacity
The opacity of the image layer as a value from 0 to 1.
.. attribute:: visible
Whether or not the image layer is visible.
.. attribute:: properties
A list of :class:`Property` objects indicating the properties of
the image layer.
.. attribute:: image
An :class:`Image` object indicating the image of the image layer.
"""
def __init__(self, name, offsetx, offsety, opacity=1, visible=True,
properties=None, image=None):
self.name = name
self.offsetx = offsetx
self.offsety = offsety
self.opacity = opacity
self.visible = visible
self.properties = properties if properties else []
self.image = image
class Layer(object):
"""
.. attribute:: name
The name of the layer.
.. attribute:: opacity
The opacity of the layer as a value from 0 to 1.
.. attribute:: visible
Whether or not the layer is visible.
.. attribute:: offsetx
Rendering offset for this layer in pixels.
.. attribute:: offsety
Rendering offset for this layer in pixels.
.. attribute:: properties
A list of :class:`Property` objects indicating the properties of
the layer.
.. attribute:: tiles
A list of :class:`LayerTile` objects indicating the tiles of the
layer.
The coordinates of each tile is determined by the tile's index
within this list. Exactly how the tiles are positioned is
determined by the map orientation.
"""
def __init__(self, name, opacity=1, visible=True, offsetx=0, offsety=0,
properties=None, tiles=None):
self.name = name
self.opacity = opacity
self.visible = visible
self.offsetx = offsetx
self.offsety = offsety
self.properties = properties if properties else []
self.tiles = tiles if tiles else []
class LayerTile(object):
"""
.. attribute:: gid
The global ID of the tile. A value of ``0`` indicates no tile at
this position.
.. attribute:: hflip
Whether or not the tile is flipped horizontally.
.. attribute:: vflip
Whether or not the tile is flipped vertically.
.. attribute:: dflip
Whether or not the tile is flipped diagonally (X and Y axis
swapped).
"""
def __init__(self, gid, hflip=False, vflip=False, dflip=False):
self.gid = gid
self.hflip = hflip
self.vflip = vflip
self.dflip = dflip
def __int__(self):
r = self.gid
if self.hflip:
r |= 2 ** 31
if self.vflip:
r |= 2 ** 30
if self.dflip:
r |= 2 ** 29
return r
class Object(object):
"""
.. attribute:: id
Unique ID of the object as a string if set, or :const:`None`
otherwise.
.. attribute:: name
The name of the object. An arbitrary string.
.. attribute:: type
The type of the object. An arbitrary string.
.. attribute:: x
The x coordinate of the object in pixels. This is the
left edge of the object in orthogonal orientation, and the center
of the object otherwise.
.. attribute:: y
The y coordinate of the object in pixels. This is the bottom
edge of the object.
.. attribute:: width
The width of the object in pixels.
.. attribute:: height
The height of the object in pixels.
.. attribute:: rotation
The rotation of the object in degrees clockwise.
.. attribute:: gid
The tile to use as the object's image. Set to :const:`None` for
no reference to a tile.
.. attribute:: visible
Whether or not the object is visible.
.. attribute:: properties
A list of :class:`Property` objects indicating the object's
properties.
.. attribute:: ellipse
Whether or not the object should be an ellipse.
.. attribute:: polygon
A list of coordinate pair tuples relative to the object's
position indicating the points of the object's representation as
a polygon. Set to :const:`None` to not represent the object as a
polygon.
.. attribute:: polyline
A list of coordinate pair tuples relative to the object's
position indicating the points of the object's representation as
a polyline. Set to :const:`None` to not represent the object as
a polyline.
"""
def __init__(self, name, type_, x, y, width=0, height=0, rotation=0,
gid=None, visible=True, properties=None, ellipse=False,
polygon=None, polyline=None, id_=None):
self.name = name
self.type = type_
self.x = x
self.y = y
self.id = id_
self.width = width
self.height = height
self.rotation = rotation
self.gid = gid
self.visible = visible
self.properties = properties if properties else []
self.ellipse = ellipse
self.polygon = polygon
self.polyline = polyline
class ObjectGroup(object):
"""
.. attribute:: name
The name of the object group.
.. attribute:: color
The color used to display the objects in this group as a hex
string (e.g. ``"FF0000"`` or ``"#00FF00"``). Set to
:const:`None` for no color definition.
.. attribute:: opacity
The opacity of the object group as a value from 0 to 1.
.. attribute:: visible
Whether or not the object group is visible.
.. attribute:: offsetx
Rendering offset for this layer in pixels.
.. attribute:: offsety
Rendering offset for this layer in pixels.
.. attribute:: draworder
Can be "topdown" or "index". Set to :const:`None` to not define
this.
.. attribute:: properties
A list of :class:`Property` objects indicating the object group's
properties
.. attribute:: objects:
A list of :class:`Object` objects indicating the object group's
objects.
"""
def __init__(self, name, color=None, opacity=1, visible=True, offsetx=0,
offsety=0, draworder=None, properties=None, objects=None):
self.name = name
self.color = color
self.opacity = opacity
self.visible = visible
self.offsetx = offsetx
self.offsety = offsety
self.draworder = None