Skip to content

Commit b9ca312

Browse files
author
Chris Bradfield
committed
Space Rocks Tutorial - Part 10
1 parent 215d63b commit b9ca312

File tree

454 files changed

+2258
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

454 files changed

+2258
-3
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
tool
2+
3+
extends EditorPlugin
4+
5+
var importerPlugin = null
6+
var importer = preload("./importer_gui.tscn")
7+
8+
func _enter_tree():
9+
importer = importer.instance()
10+
get_base_control().add_child(importer)
11+
importer.connect("confim_import", self, "_rowImport")
12+
13+
importerPlugin = ImportPlugin.new()
14+
importerPlugin.connect("show_import_dialog", self, "_showDialog")
15+
importerPlugin.connect("import_atlas", self, "_importAtlas")
16+
add_import_plugin(importerPlugin)
17+
18+
func _exit_tree():
19+
remove_import_plugin(importerPlugin)
20+
21+
func _showDialog(from):
22+
print(from)
23+
importer.showDialog(from)
24+
25+
func _importAtlas(path, meta):
26+
print(path, meta.get_options())
27+
importer.import(path, meta)
28+
29+
func _rowImport(path, meta):
30+
importerPlugin.import(path, meta)
31+
32+
# The import plugin class
33+
class ImportPlugin extends EditorImportPlugin:
34+
signal show_import_dialog(from)
35+
signal import_atlas(path, from)
36+
37+
func get_name():
38+
return "com.geequlim.gdplugin.atlas.importer"
39+
40+
func get_visible_name():
41+
return "2D Atals from other tools"
42+
43+
func import_dialog(from):
44+
emit_signal("show_import_dialog",from)
45+
46+
func import(path, from):
47+
emit_signal("import_atlas", path, from)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
tool
2+
extends Panel
3+
4+
var title = "" setget _set_title
5+
var frame_meta = null setget _setFrameMeta
6+
var texture = null setget _setTexture
7+
8+
func _set_title(text):
9+
get_node("HBox/Title").set_text(text)
10+
title = text
11+
12+
func _setFrameMeta(frame):
13+
frame_meta = frame
14+
invalidate()
15+
16+
func _setTexture(tex):
17+
texture = tex
18+
invalidate()
19+
20+
func invalidate():
21+
if frame_meta != null:
22+
# print( inst2dict(frame_meta))
23+
self.title = frame_meta.name
24+
if texture != null:
25+
_set_icon(texture, frame_meta.region, frame_meta.rotation)
26+
if texture == null:
27+
get_node("HBox/Icon/Sprite").set_texture(null)
28+
update()
29+
30+
func _set_icon(tex, region, rotation):
31+
var sprite = get_node("HBox/Icon/Sprite")
32+
sprite.set_texture(tex)
33+
sprite.set_region(true)
34+
sprite.set_region_rect(region)
35+
sprite.set_rot(rotation)
36+
var scale = 1.0
37+
if region.size.width > 70 or region.size.height > 70:
38+
if region.size.width >= region.size.height:
39+
scale = 70.0 / region.size.width
40+
else:
41+
scale = 70.0 / region.size.height
42+
sprite.set_scale(Vector2(scale, scale))
43+
sprite.update()
44+
45+
func _ready():
46+
self.texture = null
47+
self.title = ""
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[gd_scene load_steps=3 format=1]
2+
3+
[ext_resource path="res://addons/atlas_importer/frame_item.gd" type="Script" id=1]
4+
5+
[sub_resource type="StyleBoxFlat" id=1]
6+
7+
content_margin/left = -1.0
8+
content_margin/right = -1.0
9+
content_margin/top = -1.0
10+
content_margin/bottom = -1.0
11+
bg_color = Color( 0.191583, 0.172974, 0.203125, 1 )
12+
light_color = Color( 0.210938, 0.200226, 0.200226, 1 )
13+
dark_color = Color( 0.132812, 0.127175, 0.126068, 1 )
14+
border_size = 2
15+
border_blend = true
16+
draw_bg = true
17+
18+
[node name="Panel" type="Panel"]
19+
20+
focus/ignore_mouse = false
21+
focus/stop_mouse = false
22+
size_flags/horizontal = 2
23+
size_flags/vertical = 2
24+
margin/left = 0.0
25+
margin/top = 0.0
26+
margin/right = 568.0
27+
margin/bottom = 80.0
28+
custom_styles/panel = SubResource( 1 )
29+
script/script = ExtResource( 1 )
30+
31+
[node name="HBox" type="HBoxContainer" parent="."]
32+
33+
anchor/right = 1
34+
anchor/bottom = 1
35+
focus/ignore_mouse = false
36+
focus/stop_mouse = true
37+
size_flags/horizontal = 3
38+
size_flags/vertical = 3
39+
margin/left = 0.0
40+
margin/top = 0.0
41+
margin/right = 0.0
42+
margin/bottom = 0.0
43+
alignment = 0
44+
45+
[node name="Icon" type="Control" parent="HBox"]
46+
47+
focus/ignore_mouse = false
48+
focus/stop_mouse = true
49+
size_flags/horizontal = 3
50+
size_flags/vertical = 3
51+
margin/left = 0.0
52+
margin/top = 0.0
53+
margin/right = 282.0
54+
margin/bottom = 80.0
55+
56+
[node name="Sprite" type="Sprite" parent="HBox/Icon"]
57+
58+
transform/pos = Vector2( 40, 40 )
59+
60+
[node name="Title" type="Label" parent="HBox"]
61+
62+
focus/ignore_mouse = true
63+
focus/stop_mouse = true
64+
size_flags/horizontal = 3
65+
size_flags/vertical = 0
66+
margin/left = 286.0
67+
margin/top = 33.0
68+
margin/right = 568.0
69+
margin/bottom = 47.0
70+
percent_visible = 1.0
71+
lines_skipped = 0
72+
max_lines_visible = -1
73+
74+
25.5 KB
Loading

0 commit comments

Comments
 (0)