-
Notifications
You must be signed in to change notification settings - Fork 6
/
atlasConvert.py
82 lines (75 loc) · 2.16 KB
/
atlasConvert.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
# Wow! This is my first Python program
# It can convert LibGDX texture atlas format to Phaser format
# Created 2017/02/10
# author: stdio2016
import sys
def checkHead(file):
for i in range(6):
file.readline()
def readFrame(file, isFirst):
frameName = file.readline()
if len(frameName) == 0:
return None
rotate = readKey(file, 'rotate')
xy = getCoord(readKey(file, 'xy'))
size = getCoord(readKey(file, 'size'))
orig = getCoord(readKey(file, 'orig'))
offset = getCoord(readKey(file, 'offset'))
index = int(readKey(file, 'index'))
frameName = frameName[0:-1]
if index != -1:
frameName = frameName + '_' + str(index)
if isFirst:
print ('{')
else:
print (',{')
print ('\t"filename": "%s",' % escape(frameName))
print ('\t"frame": {"x":%d,"y":%d,"w":%d,"h":%d},' % (
xy[0], xy[1], size[0], size[1]))
print ('\t"rotated": %s,' % rotate)
print ('\t"trimmed": %s,' % ('false' if size == orig else 'true'))
print ('\t"spriteSourceSize": {"x":%d,"y":%d,"w":%d,"h":%d},' % (
offset[0], offset[1], size[0], size[1]))
print ('\t"sourceSize": {"w":%d,"h":%d}' % orig)
print ('}')
return True
def escape(str):
str = str.replace('\\', '\\\\')
str = str.replace('"', '\\"')
return str
def readKey(file, key):
str = file.readline()
str = str.strip()
colon = str.find(': ')
if colon == -1:
raise SyntaxError("Missing colon")
theKey = str[0:colon]
if theKey != key:
raise SyntaxError("Key %s mismatch" % key)
return str[colon+1:].lstrip()
def getCoord(str):
x, y = str.split(', ')
return (int(x), int(y))
def main():
if len(sys.argv) < 2:
print ("usage: python atlasConvert.py <libgdx atlas file>")
sys.exit()
inName = sys.argv[1]
f = open(inName)
checkHead(f)
notEnd = True
isFirst = True
print ('{"frames": [')
while notEnd:
data = readFrame(f, isFirst)
if data is None:
notEnd = False
isFirst = False
f.close()
print ('''],
\t"meta": {
\t\t"app": "atlasConvert.py and LibGDX texture packer",
\t\t"version": "0.1"
\t}
}''')
main()