forked from east/tw-webgl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsonexp.py
executable file
·80 lines (62 loc) · 2.04 KB
/
jsonexp.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
#!/usr/bin/env python2
from tml.tml import *
from tml.items import *
import json
import sys
import re
from os.path import basename, splitext
def normalizeFilename(name):
#return re.sub('[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]', '_', name)
return re.sub('\W', '_', name)
def build_json(path):
t = Teemap(path);
mapname = splitext(basename(path))[0]
groups = []
for curGroup in t.groups:
grp = { 'layers': [],
'parallaxX': curGroup.parallax_x,
'parallaxY': curGroup.parallax_y,
'offsX': curGroup.offset_x,
'offsY': curGroup.offset_y,
}
for curLayer in curGroup.layers:
if curLayer.__class__ == TileLayer:
l = { 'type': "tilelayer", 'size': [0, 0], 'tiles': [], 'tileFlags': [], 'tex': "",
'color': curLayer.color}
# size
l['size'][0] = curLayer._width
l['size'][1] = curLayer._height
# texture png
if curLayer.image_id != -1:
l['tex'] = os.sep.join([normalizeFilename(mapname), normalizeFilename(t.images[curLayer.image_id].name) + '.png'])
for curTile in curLayer.tiles:
l['tiles'] += [curTile.index]
l['tileFlags']+= [curTile._flags]
elif curLayer.__class__ == QuadLayer:
l = { 'type': "quadlayer", 'tex': "", 'quads': []}
if curLayer.image_id != -1:
l['tex'] = os.sep.join([normalizeFilename(mapname), normalizeFilename(t.images[curLayer.image_id].name) + '.png'])
for q in curLayer.quads:
l['quads'] += [{
'pos_env': q.pos_env,
'pos_env_offs': q.pos_env_offset,
'color_env': q.color_env,
'color_env_offs': q.color_env_offset,
'points': q.points,
'colors': q.colors,
'texcoords': q.texcoords,
}]
else:
continue;
grp['layers'] += [l]
groups += [grp]
return json.dumps({'groups': groups}, separators=(',', ': '), sort_keys=True)
if len(sys.argv) < 2:
print("Usage: %s map1 [map2]" % (sys.argv[0]))
sys.exit(1)
for curMap in sys.argv[1:]:
f = open(curMap+".json", "w")
f.write(build_json(curMap).decode('utf-8'));
f.close();
print(curMap+".json")
sys.exit(0)