1+ tool
2+ extends Reference
3+
4+ const FORMAT_TEXTURE_PACKER_XML = 0
5+ const FORMAT_TEXTURE_JSON = 1
6+ const FORMAT_ATTILA_JSON = 2
7+ const FORMAT_KENNEY_SPRITESHEET = 3
8+
9+ var imagePath = ""
10+ var width = 0
11+ var height = 0
12+ var sprites = []
13+
14+ class SpirteFrame extends Reference :
15+ var name = ""
16+ var region = Rect2 (0 , 0 , 0 , 0 )
17+ var originFrame = Rect2 (0 , 0 , 0 , 0 )
18+ var pivot = Vector2 (0 , 0 )
19+ var rotation = 0
20+
21+
22+ func loadFromFile (path , format ):
23+ var file = File .new ()
24+ file .open (path , File .READ )
25+ if file .is_open ():
26+ var fileContent = file .get_as_text ()
27+ parse (fileContent , format )
28+ file .close ()
29+ return self
30+
31+ func parse (fileContent , format ):
32+ var atlas = null
33+ self .sprites .clear ()
34+ if format == FORMAT_TEXTURE_PACKER_XML :
35+ atlas = _parseTexturePackerXML (fileContent )
36+ elif format == FORMAT_TEXTURE_JSON :
37+ atlas = _parseTexturePackerJson (fileContent )
38+ elif format == FORMAT_ATTILA_JSON :
39+ atlas = _parseAttilaJson (fileContent )
40+ elif format == FORMAT_KENNEY_SPRITESHEET :
41+ atlas = _parseKenneySpritsheet (fileContent )
42+ if atlas != null :
43+ if atlas .has ("imagePath" ):
44+ self .imagePath = atlas ["imagePath" ]
45+ if atlas .has ("width" ):
46+ self .width = atlas ["width" ]
47+ if atlas .has ("height" ):
48+ self .height = atlas ["height" ]
49+ if atlas .has ("sprites" ):
50+ for f in atlas ["sprites" ]:
51+ var sprite = SpirteFrame .new ()
52+ sprite .name = f ["name" ]
53+ sprite .region = Rect2 ( f ["x" ] , f ["y" ], f ["width" ], f ["height" ])
54+ sprite .originFrame = Rect2 ( f ["orignX" ] , f ["orignY" ], f ["orignWidth" ], f ["orignHeight" ])
55+ sprite .pivot = Vector2 (f ["pivotX" ], f ["pivotY" ])
56+ sprite .rotation = f ["rotation" ]
57+ self .sprites .append (sprite )
58+ return self
59+
60+ func _parseTexturePackerXML (xmlContent ):
61+ """
62+ Parse Atlas from XML content which is exported with TexturePacker as "XML(generic)"
63+ """
64+ var atlas = null
65+ var sprites = []
66+ var xmlParser = XMLParser .new ()
67+ if OK == xmlParser .open_buffer (xmlContent .to_utf8 ()):
68+ var err = xmlParser .read ()
69+ if err == OK :
70+ atlas = {}
71+ atlas ["sprites" ] = sprites
72+ while (err != ERR_FILE_EOF ):
73+ if xmlParser .get_node_type () == xmlParser .NODE_ELEMENT :
74+ if xmlParser .get_node_name () == "TextureAtlas" :
75+ atlas ["imagePath" ] = xmlParser .get_named_attribute_value ("imagePath" )
76+ atlas ["width" ] = xmlParser .get_named_attribute_value ("width" )
77+ atlas ["height" ] = xmlParser .get_named_attribute_value ("height" )
78+ elif xmlParser .get_node_name () == "sprite" :
79+ var sprite = {}
80+ sprite ["name" ] = xmlParser .get_named_attribute_value ("n" )
81+ sprite ["x" ] = xmlParser .get_named_attribute_value ("x" )
82+ sprite ["y" ] = xmlParser .get_named_attribute_value ("y" )
83+ sprite ["width" ] = xmlParser .get_named_attribute_value ("w" )
84+ sprite ["height" ] = xmlParser .get_named_attribute_value ("h" )
85+ sprite ["pivotX" ] = xmlParser .get_named_attribute_value ("pX" )
86+ sprite ["pivotY" ] = xmlParser .get_named_attribute_value ("pY" )
87+ if xmlParser .has_attribute ("oX" ):
88+ sprite ["orignX" ] = xmlParser .get_named_attribute_value ("oX" )
89+ else :
90+ sprite ["orignX" ] = 0.0
91+ if xmlParser .has_attribute ("oY" ):
92+ sprite ["orignY" ] = xmlParser .get_named_attribute_value ("oY" )
93+ else :
94+ sprite ["orignY" ] = 0.0
95+ if xmlParser .has_attribute ("oW" ):
96+ sprite ["orignWidth" ] = xmlParser .get_named_attribute_value ("oW" )
97+ else :
98+ sprite ["orignWidth" ] = 0.0
99+ if xmlParser .has_attribute ("oH" ):
100+ sprite ["orignHeight" ] = xmlParser .get_named_attribute_value ("oH" )
101+ else :
102+ sprite ["orignHeight" ] = 0.0
103+ if xmlParser .has_attribute ("r" ) and xmlParser .get_named_attribute_value ("r" ) == "y" :
104+ sprite ["rotation" ] = deg2rad (90 )
105+ else :
106+ sprite ["rotation" ] = 0
107+ sprites .append (sprite )
108+ err = xmlParser .read ()
109+ return atlas
110+
111+ func _parseTexturePackerJson (jsonContent ):
112+ """
113+ Parse Atlas from json content which is exported from TexturePacker as "JSON"
114+ """
115+ var atlas = null
116+ var sprites = []
117+ var jsonParser = {}
118+ if OK == jsonParser .parse_json (jsonContent ):
119+ atlas = {}
120+ atlas ["sprites" ] = sprites
121+ if jsonParser .has ("meta" ) and jsonParser .has ("frames" ):
122+ atlas ["imagePath" ] = jsonParser ["meta" ]["image" ]
123+ atlas ["width" ] = jsonParser ["meta" ]["size" ]["w" ]
124+ atlas ["height" ] = jsonParser ["meta" ]["size" ]["h" ]
125+ var frames = jsonParser ["frames" ]
126+ for key in frames .keys ():
127+ var sprite = {}
128+ var f = frames [key ]
129+ sprite ["name" ] = key
130+ sprite ["x" ] = f ["frame" ]["x" ]
131+ sprite ["y" ] = f ["frame" ]["y" ]
132+ sprite ["width" ] = f ["frame" ]["w" ]
133+ sprite ["height" ] = f ["frame" ]["h" ]
134+ sprite ["pivotX" ] = f ["pivot" ]["x" ]
135+ sprite ["pivotY" ] = f ["pivot" ]["y" ]
136+ sprite ["orignX" ] = f ["spriteSourceSize" ]["x" ]
137+ sprite ["orignY" ] = f ["spriteSourceSize" ]["y" ]
138+ sprite ["orignWidth" ] = f ["spriteSourceSize" ]["w" ]
139+ sprite ["orignHeight" ] = f ["spriteSourceSize" ]["h" ]
140+ sprite ["rotation" ] = 0
141+ if f ["rotated" ]:
142+ sprite ["rotation" ] = deg2rad (90 )
143+ sprites .append (sprite )
144+ return atlas
145+
146+ func _parseAttilaJson (jsonContent ):
147+ """
148+ Parse Atlas from json content which is exported from Attila
149+ """
150+ var atlas = null
151+ var sprites = []
152+ var jsonParser = {}
153+ if OK == jsonParser .parse_json (jsonContent ):
154+ atlas = {}
155+ var keys = jsonParser .keys ()
156+ if keys .size () > 0 :
157+ if jsonParser [keys [0 ]].has ("dst" ) and jsonParser [keys [0 ]].has ("hash" ):
158+ atlas ["imagePath" ] = jsonParser [keys [0 ]]["dst" ]
159+ atlas ["sprites" ] = sprites
160+ for key in keys :
161+ var sprite = {}
162+ var f = jsonParser [key ]
163+ sprite ["name" ] = key
164+ sprite ["x" ] = f ["x" ]
165+ sprite ["y" ] = f ["y" ]
166+ sprite ["width" ] = f ["w" ]
167+ sprite ["height" ] = f ["h" ]
168+ sprite ["pivotX" ] = 0
169+ sprite ["pivotY" ] = 0
170+ sprite ["orignX" ] = 0
171+ sprite ["orignY" ] = 0
172+ sprite ["orignWidth" ] = f ["w" ]
173+ sprite ["orignHeight" ] = f ["h" ]
174+ sprite ["rotation" ] = - deg2rad (f ["rotate" ])
175+ if f ["rotate" ] == 90 :
176+ sprite ["width" ] = f ["h" ]
177+ sprite ["height" ] = f ["w" ]
178+ sprites .append (sprite )
179+ atlas ["width" ] = 0
180+ atlas ["height" ] = 0
181+ return atlas
182+
183+ func _parseKenneySpritsheet (xmlContent ):
184+ """
185+ Parse Atlas from XML content which is exported with TexturePacker as "XML(generic)"
186+ """
187+ var atlas = null
188+ var sprites = []
189+ var xmlParser = XMLParser .new ()
190+ if OK == xmlParser .open_buffer (xmlContent .to_utf8 ()):
191+ var err = xmlParser .read ()
192+ if err == OK :
193+ atlas = {}
194+ atlas ["sprites" ] = sprites
195+ while (err != ERR_FILE_EOF ):
196+ if xmlParser .get_node_type () == xmlParser .NODE_ELEMENT :
197+ if xmlParser .get_node_name () == "TextureAtlas" :
198+ atlas ["imagePath" ] = xmlParser .get_named_attribute_value ("imagePath" )
199+ # atlas["width"] = xmlParser.get_named_attribute_value("width")
200+ # atlas["height"] = xmlParser.get_named_attribute_value("height")
201+ elif xmlParser .get_node_name () == "SubTexture" :
202+ var sprite = {}
203+ sprite ["name" ] = xmlParser .get_named_attribute_value ("name" )
204+ sprite ["x" ] = xmlParser .get_named_attribute_value ("x" )
205+ sprite ["y" ] = xmlParser .get_named_attribute_value ("y" )
206+ sprite ["width" ] = xmlParser .get_named_attribute_value ("width" )
207+ sprite ["height" ] = xmlParser .get_named_attribute_value ("height" )
208+ sprite ["pivotX" ] = 0
209+ sprite ["pivotY" ] = 0
210+ sprite ["orignX" ] = 0.0
211+ sprite ["orignY" ] = 0.0
212+ sprite ["orignWidth" ] = 0.0
213+ sprite ["orignHeight" ] = 0.0
214+ sprite ["rotation" ] = 0
215+ sprites .append (sprite )
216+ err = xmlParser .read ()
217+ return atlas
0 commit comments