Added my gdx atlas to phaser atlas converter

This commit is contained in:
lanxu 2017-12-17 14:40:01 +02:00
parent 646d3aaaea
commit 418d465437

View File

@ -0,0 +1,165 @@
#!/usr/bin/env python
"""
Archive extract script with sub-directory creation
"""
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
import io
import re
import sys
import json
class GdxAtlasFrame:
imageName = None
rotate = None
xy = None
size = None
orig = None
offset = None
index = None
class GdxAtlas:
imageName = None
size = None
format = None
filter = None
repeat = None
frames = []
def serialize(obj):
return (obj.__class__, obj.__dict__)
def main(input):
print(input)
# Read file to file_contents variable
lines = []
with open(input) as f:
for line in f:
lines.append(line)
f.closed
# Now we have the whole atlas file line-by-line in lines variable
# Atlas roughly follows the following format
# First non-empty line => name of the texture file
# size: w,h
# format: <format>
# filter: <filter>,<filter>
# repeat: none
# first non-parameter here is the name of the single image in atlas
# after that there is n parameters (usually the following)
# rotate: true/false
# xy: x,y
# size: w,h
# orig: w,h
# offset: x,y
# index: index
gdxAtlas = GdxAtlas()
state = 'getname'
paramRegex = re.compile('(.+):(.+)')
valueCommaRegex = re.compile('(.+),(.+)')
currentFrame = None
for line in lines:
line = line.rstrip()
if line == '':
continue
if state == 'getname':
gdxAtlas.imageName = line
state = 'getparams'
elif state == 'getparams':
res = paramRegex.findall(line)
if res:
key = res[0][0].strip()
value = res[0][1].strip()
isCommaSeparated = valueCommaRegex.findall(value)
if isCommaSeparated:
a = isCommaSeparated[0][0].strip()
b = isCommaSeparated[0][1].strip()
# header
if currentFrame is None:
if key == 'size':
gdxAtlas.size = (int(a), int(b))
if key == 'format':
gdxAtlas.format = value
if key == 'filter' or key == 'repeat':
print(key + ' is not supported')
# frames
if currentFrame is not None:
if key == 'rotate':
currentFrame.rotate = value
if key == 'xy':
currentFrame.xy = (int(a), int(b))
if key == 'size':
currentFrame.size = (int(a), int(b))
if key == 'orig':
currentFrame.orig = (int(a), int(b))
if key == 'offset':
currentFrame.offset = (int(a), int(b))
else:
# previous state ended!
if currentFrame is not None:
gdxAtlas.frames.append(currentFrame)
currentFrame = GdxAtlasFrame()
currentFrame.imageName = line
else:
print('unknown state')
if currentFrame is not None:
gdxAtlas.frames.append(currentFrame)
json_str = gdxAtlasToPhaserJson(gdxAtlas)
with io.open(input+'.json', 'w', encoding='utf8') as outfile:
outfile.write(str(json_str))
outfile.closed
print('wrote '+ input + '.json')
def printGdxAtlas(gdxAtlas):
print(serialize(gdxAtlas))
for frame in gdxAtlas.frames:
print(serialize(frame))
def gdxAtlasToPhaserJson(gdxAtlas):
print('Converting to json')
data = {
'frames': [],
'meta': {
'app': 'libgdx',
'version': 'latest',
'image': gdxAtlas.imageName,
'format': gdxAtlas.format,
'size': { 'w': gdxAtlas.size[0], 'h': gdxAtlas.size[1] },
'scale': 1
}
}
for frame in gdxAtlas.frames:
framedata = {
'filename': frame.imageName,
'frame': { 'x': frame.xy[0], 'y': frame.xy[1], 'w': frame.size[0], 'h': frame.size[1] },
'rotated': frame.rotate == 'true',
'trimmed': False,
'spriteSourceSize': { 'x': frame.offset[0], 'y': frame.offset[1], 'w': frame.orig[0], 'h': frame.orig[1] },
'sourceSize': { 'w': frame.orig[0], 'h': frame.orig[1] }
}
data['frames'].append(framedata)
json_str = json.dumps(data, indent=4, sort_keys=False, separators=(',', ': '), ensure_ascii=False)
return json_str
if __name__ == "__main__":
main(sys.argv[1])