Skip to content

Commit

Permalink
Space Rocks Tutorial - Part 10
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Bradfield committed May 18, 2017
1 parent 215d63b commit b9ca312
Show file tree
Hide file tree
Showing 454 changed files with 2,258 additions and 3 deletions.
217 changes: 217 additions & 0 deletions Space_Rocks_tutorial/Part 10/addons/atlas_importer/atlas.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
tool
extends Reference

const FORMAT_TEXTURE_PACKER_XML = 0
const FORMAT_TEXTURE_JSON = 1
const FORMAT_ATTILA_JSON = 2
const FORMAT_KENNEY_SPRITESHEET = 3

var imagePath = ""
var width = 0
var height = 0
var sprites = []

class SpirteFrame extends Reference:
var name = ""
var region = Rect2(0, 0, 0, 0)
var originFrame = Rect2(0, 0, 0, 0)
var pivot = Vector2(0, 0)
var rotation = 0


func loadFromFile(path, format):
var file = File.new()
file.open(path, File.READ)
if file.is_open():
var fileContent = file.get_as_text()
parse(fileContent, format)
file.close()
return self

func parse(fileContent, format):
var atlas = null
self.sprites.clear()
if format == FORMAT_TEXTURE_PACKER_XML:
atlas = _parseTexturePackerXML(fileContent)
elif format == FORMAT_TEXTURE_JSON:
atlas = _parseTexturePackerJson(fileContent)
elif format == FORMAT_ATTILA_JSON:
atlas = _parseAttilaJson(fileContent)
elif format == FORMAT_KENNEY_SPRITESHEET:
atlas = _parseKenneySpritsheet(fileContent)
if atlas != null:
if atlas.has("imagePath"):
self.imagePath = atlas["imagePath"]
if atlas.has("width"):
self.width = atlas["width"]
if atlas.has("height"):
self.height = atlas["height"]
if atlas.has("sprites"):
for f in atlas["sprites"]:
var sprite = SpirteFrame.new()
sprite.name = f["name"]
sprite.region = Rect2( f["x"] , f["y"], f["width"], f["height"])
sprite.originFrame = Rect2( f["orignX"] , f["orignY"], f["orignWidth"], f["orignHeight"])
sprite.pivot = Vector2(f["pivotX"], f["pivotY"])
sprite.rotation = f["rotation"]
self.sprites.append(sprite)
return self

func _parseTexturePackerXML(xmlContent):
"""
Parse Atlas from XML content which is exported with TexturePacker as "XML(generic)"
"""
var atlas = null
var sprites = []
var xmlParser = XMLParser.new()
if OK == xmlParser.open_buffer(xmlContent.to_utf8()):
var err = xmlParser.read()
if err == OK:
atlas = {}
atlas["sprites"] = sprites
while(err != ERR_FILE_EOF):
if xmlParser.get_node_type() == xmlParser.NODE_ELEMENT:
if xmlParser.get_node_name() == "TextureAtlas":
atlas["imagePath"] = xmlParser.get_named_attribute_value("imagePath")
atlas["width"] = xmlParser.get_named_attribute_value("width")
atlas["height"] = xmlParser.get_named_attribute_value("height")
elif xmlParser.get_node_name() == "sprite":
var sprite = {}
sprite["name"] = xmlParser.get_named_attribute_value("n")
sprite["x"] = xmlParser.get_named_attribute_value("x")
sprite["y"] = xmlParser.get_named_attribute_value("y")
sprite["width"] = xmlParser.get_named_attribute_value("w")
sprite["height"] = xmlParser.get_named_attribute_value("h")
sprite["pivotX"] = xmlParser.get_named_attribute_value("pX")
sprite["pivotY"] = xmlParser.get_named_attribute_value("pY")
if xmlParser.has_attribute("oX"):
sprite["orignX"] = xmlParser.get_named_attribute_value("oX")
else:
sprite["orignX"] = 0.0
if xmlParser.has_attribute("oY"):
sprite["orignY"] = xmlParser.get_named_attribute_value("oY")
else:
sprite["orignY"] = 0.0
if xmlParser.has_attribute("oW"):
sprite["orignWidth"] = xmlParser.get_named_attribute_value("oW")
else:
sprite["orignWidth"] = 0.0
if xmlParser.has_attribute("oH"):
sprite["orignHeight"] = xmlParser.get_named_attribute_value("oH")
else:
sprite["orignHeight"] = 0.0
if xmlParser.has_attribute("r") and xmlParser.get_named_attribute_value("r") == "y":
sprite["rotation"] = deg2rad(90)
else:
sprite["rotation"] = 0
sprites.append(sprite)
err = xmlParser.read()
return atlas

func _parseTexturePackerJson(jsonContent):
"""
Parse Atlas from json content which is exported from TexturePacker as "JSON"
"""
var atlas = null
var sprites = []
var jsonParser = {}
if OK == jsonParser.parse_json(jsonContent):
atlas = {}
atlas["sprites"] = sprites
if jsonParser.has("meta") and jsonParser.has("frames"):
atlas["imagePath"] = jsonParser["meta"]["image"]
atlas["width"] = jsonParser["meta"]["size"]["w"]
atlas["height"] = jsonParser["meta"]["size"]["h"]
var frames = jsonParser["frames"]
for key in frames.keys():
var sprite = {}
var f = frames[key]
sprite["name"] = key
sprite["x"] = f["frame"]["x"]
sprite["y"] = f["frame"]["y"]
sprite["width"] = f["frame"]["w"]
sprite["height"] = f["frame"]["h"]
sprite["pivotX"] = f["pivot"]["x"]
sprite["pivotY"] = f["pivot"]["y"]
sprite["orignX"] = f["spriteSourceSize"]["x"]
sprite["orignY"] = f["spriteSourceSize"]["y"]
sprite["orignWidth"] = f["spriteSourceSize"]["w"]
sprite["orignHeight"] = f["spriteSourceSize"]["h"]
sprite["rotation"] = 0
if f["rotated"]:
sprite["rotation"] = deg2rad(90)
sprites.append(sprite)
return atlas

func _parseAttilaJson(jsonContent):
"""
Parse Atlas from json content which is exported from Attila
"""
var atlas = null
var sprites = []
var jsonParser = {}
if OK == jsonParser.parse_json(jsonContent):
atlas = {}
var keys = jsonParser.keys()
if keys.size() > 0:
if jsonParser[keys[0]].has("dst") and jsonParser[keys[0]].has("hash"):
atlas["imagePath"] = jsonParser[keys[0]]["dst"]
atlas["sprites"] = sprites
for key in keys:
var sprite = {}
var f = jsonParser[key]
sprite["name"] = key
sprite["x"] = f["x"]
sprite["y"] = f["y"]
sprite["width"] = f["w"]
sprite["height"] = f["h"]
sprite["pivotX"] = 0
sprite["pivotY"] = 0
sprite["orignX"] = 0
sprite["orignY"] = 0
sprite["orignWidth"] = f["w"]
sprite["orignHeight"] = f["h"]
sprite["rotation"] = -deg2rad(f["rotate"])
if f["rotate"] == 90:
sprite["width"] = f["h"]
sprite["height"] = f["w"]
sprites.append(sprite)
atlas["width"] = 0
atlas["height"] = 0
return atlas

func _parseKenneySpritsheet(xmlContent):
"""
Parse Atlas from XML content which is exported with TexturePacker as "XML(generic)"
"""
var atlas = null
var sprites = []
var xmlParser = XMLParser.new()
if OK == xmlParser.open_buffer(xmlContent.to_utf8()):
var err = xmlParser.read()
if err == OK:
atlas = {}
atlas["sprites"] = sprites
while(err != ERR_FILE_EOF):
if xmlParser.get_node_type() == xmlParser.NODE_ELEMENT:
if xmlParser.get_node_name() == "TextureAtlas":
atlas["imagePath"] = xmlParser.get_named_attribute_value("imagePath")
# atlas["width"] = xmlParser.get_named_attribute_value("width")
# atlas["height"] = xmlParser.get_named_attribute_value("height")
elif xmlParser.get_node_name() == "SubTexture":
var sprite = {}
sprite["name"] = xmlParser.get_named_attribute_value("name")
sprite["x"] = xmlParser.get_named_attribute_value("x")
sprite["y"] = xmlParser.get_named_attribute_value("y")
sprite["width"] = xmlParser.get_named_attribute_value("width")
sprite["height"] = xmlParser.get_named_attribute_value("height")
sprite["pivotX"] = 0
sprite["pivotY"] = 0
sprite["orignX"] = 0.0
sprite["orignY"] = 0.0
sprite["orignWidth"] = 0.0
sprite["orignHeight"] = 0.0
sprite["rotation"] = 0
sprites.append(sprite)
err = xmlParser.read()
return atlas
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
tool

extends EditorPlugin

var importerPlugin = null
var importer = preload("./importer_gui.tscn")

func _enter_tree():
importer = importer.instance()
get_base_control().add_child(importer)
importer.connect("confim_import", self, "_rowImport")

importerPlugin = ImportPlugin.new()
importerPlugin.connect("show_import_dialog", self, "_showDialog")
importerPlugin.connect("import_atlas", self, "_importAtlas")
add_import_plugin(importerPlugin)

func _exit_tree():
remove_import_plugin(importerPlugin)

func _showDialog(from):
print(from)
importer.showDialog(from)

func _importAtlas(path, meta):
print(path, meta.get_options())
importer.import(path, meta)

func _rowImport(path, meta):
importerPlugin.import(path, meta)

# The import plugin class
class ImportPlugin extends EditorImportPlugin:
signal show_import_dialog(from)
signal import_atlas(path, from)

func get_name():
return "com.geequlim.gdplugin.atlas.importer"

func get_visible_name():
return "2D Atals from other tools"

func import_dialog(from):
emit_signal("show_import_dialog",from)

func import(path, from):
emit_signal("import_atlas", path, from)
47 changes: 47 additions & 0 deletions Space_Rocks_tutorial/Part 10/addons/atlas_importer/frame_item.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
tool
extends Panel

var title = "" setget _set_title
var frame_meta = null setget _setFrameMeta
var texture = null setget _setTexture

func _set_title(text):
get_node("HBox/Title").set_text(text)
title = text

func _setFrameMeta(frame):
frame_meta = frame
invalidate()

func _setTexture(tex):
texture = tex
invalidate()

func invalidate():
if frame_meta != null:
# print( inst2dict(frame_meta))
self.title = frame_meta.name
if texture != null:
_set_icon(texture, frame_meta.region, frame_meta.rotation)
if texture == null:
get_node("HBox/Icon/Sprite").set_texture(null)
update()

func _set_icon(tex, region, rotation):
var sprite = get_node("HBox/Icon/Sprite")
sprite.set_texture(tex)
sprite.set_region(true)
sprite.set_region_rect(region)
sprite.set_rot(rotation)
var scale = 1.0
if region.size.width > 70 or region.size.height > 70:
if region.size.width >= region.size.height:
scale = 70.0 / region.size.width
else:
scale = 70.0 / region.size.height
sprite.set_scale(Vector2(scale, scale))
sprite.update()

func _ready():
self.texture = null
self.title = ""
74 changes: 74 additions & 0 deletions Space_Rocks_tutorial/Part 10/addons/atlas_importer/frame_item.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
[gd_scene load_steps=3 format=1]

[ext_resource path="res://addons/atlas_importer/frame_item.gd" type="Script" id=1]

[sub_resource type="StyleBoxFlat" id=1]

content_margin/left = -1.0
content_margin/right = -1.0
content_margin/top = -1.0
content_margin/bottom = -1.0
bg_color = Color( 0.191583, 0.172974, 0.203125, 1 )
light_color = Color( 0.210938, 0.200226, 0.200226, 1 )
dark_color = Color( 0.132812, 0.127175, 0.126068, 1 )
border_size = 2
border_blend = true
draw_bg = true

[node name="Panel" type="Panel"]

focus/ignore_mouse = false
focus/stop_mouse = false
size_flags/horizontal = 2
size_flags/vertical = 2
margin/left = 0.0
margin/top = 0.0
margin/right = 568.0
margin/bottom = 80.0
custom_styles/panel = SubResource( 1 )
script/script = ExtResource( 1 )

[node name="HBox" type="HBoxContainer" parent="."]

anchor/right = 1
anchor/bottom = 1
focus/ignore_mouse = false
focus/stop_mouse = true
size_flags/horizontal = 3
size_flags/vertical = 3
margin/left = 0.0
margin/top = 0.0
margin/right = 0.0
margin/bottom = 0.0
alignment = 0

[node name="Icon" type="Control" parent="HBox"]

focus/ignore_mouse = false
focus/stop_mouse = true
size_flags/horizontal = 3
size_flags/vertical = 3
margin/left = 0.0
margin/top = 0.0
margin/right = 282.0
margin/bottom = 80.0

[node name="Sprite" type="Sprite" parent="HBox/Icon"]

transform/pos = Vector2( 40, 40 )

[node name="Title" type="Label" parent="HBox"]

focus/ignore_mouse = true
focus/stop_mouse = true
size_flags/horizontal = 3
size_flags/vertical = 0
margin/left = 286.0
margin/top = 33.0
margin/right = 568.0
margin/bottom = 47.0
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b9ca312

Please sign in to comment.