silver-fang-mini-game/create-phaser-atlas.py
2024-09-28 10:50:10 +03:00

99 lines
2.1 KiB
Python
Executable File

#!/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
from os import walk
import PIL
from PIL import Image
class AtlasFrame:
filename = None
xy = None
size = None
rotate = False
orig = None
offset = None
class Atlas:
image = None
format = None
size = None
frames = []
def serialize(obj):
return (obj.__class__, obj.__dict__)
def create_atlas(path):
f = []
for (dirpath, dirnames, filenames) in walk(path):
f.extend(filenames)
break
for filename in f:
print(filename)
print(f)
atlas = Atlas()
atlas.image = 'atlas.png'
atlas.format = 'png'
atlas.size = [10, 10]
return atlas
def main(dir):
print(dir)
atlas = create_atlas(dir)
json_str = createPhaserJson(atlas)
json_filename= 'assets.json'
with io.open(json_filename, 'w', encoding='utf8') as outfile:
outfile.write(str(json_str))
outfile.closed
print('wrote '+ json_filename )
def createPhaserJson(atlas):
data = {
'frames': [],
'meta': {
'app': 'libgdx',
'version': 'latest',
'image': atlas.image,
'format': atlas.format,
'size': { 'w': atlas.size[0], 'h': atlas.size[1] },
'scale': 1
}
}
for frame in atlas.frames:
framedata = {
'filename': frame.filename,
'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])