From 4a904d0a7c90dd2e3fe71070a0200589bca9a19e Mon Sep 17 00:00:00 2001 From: Unrealomega Date: Mon, 14 Sep 2020 02:16:36 -0400 Subject: [PATCH 1/5] Added Pixel Art Preset and removed unused code * Bump the version to 1.0.6 * Added Pixel Art preset to deal with filter settings that cause certain tileset/spritesheet to become blurry. * Removed an unused code that creates a spritesheet folder when importing a tileset. --- addons/codeandweb.texturepacker/plugin.cfg | 2 +- .../texturepacker_import_spritesheet.gd | 24 +++++++++------ .../texturepacker_import_tileset.gd | 29 +++++++++++-------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/addons/codeandweb.texturepacker/plugin.cfg b/addons/codeandweb.texturepacker/plugin.cfg index e24b8c5..fd4b331 100644 --- a/addons/codeandweb.texturepacker/plugin.cfg +++ b/addons/codeandweb.texturepacker/plugin.cfg @@ -3,6 +3,6 @@ config_version=3 name="TexturePacker Importer" description="Importer for SpriteSheets created using TexturePacker" -version="1.0.5" +version="1.0.6" author="Andreas Loew / CodeAndWeb GmbH" script="codeandweb.texturepacker_importer.gd" diff --git a/addons/codeandweb.texturepacker/texturepacker_import_spritesheet.gd b/addons/codeandweb.texturepacker/texturepacker_import_spritesheet.gd index 5cd701b..0f472f9 100644 --- a/addons/codeandweb.texturepacker/texturepacker_import_spritesheet.gd +++ b/addons/codeandweb.texturepacker/texturepacker_import_spritesheet.gd @@ -25,7 +25,7 @@ extends EditorImportPlugin var imageLoader = preload("image_loader.gd").new() -enum Preset { PRESET_DEFAULT } +enum Preset { PRESET_DEFAULT, PRESET_PIXEL_ART } # const TiledMapReader = preload("tiled_map_reader.gd") @@ -60,10 +60,16 @@ func get_preset_count(): func get_preset_name(preset): match preset: Preset.PRESET_DEFAULT: return "Default" + Preset.PRESET_PIXEL_ART: return "Pixel Art" func get_import_options(preset): - return [] + return [{ + "name": "image_flags", + "default_value": 0 if preset == Preset.PRESET_PIXEL_ART else Texture.FLAGS_DEFAULT, + "property_hint": PROPERTY_HINT_FLAGS, + "hint_string": "Mipmaps,Repeat,Filter,Anisotropic,sRGB,Mirrored Repeat" + }] func get_option_visibility(option, options): @@ -76,19 +82,19 @@ func get_import_order(): func import(source_file, save_path, options, r_platform_variants, r_gen_files): print("Importing sprite sheet from "+source_file); - + var sheets = read_sprite_sheet(source_file) var sheetFolder = source_file.get_basename()+".sprites" create_folder(sheetFolder) - + for sheet in sheets.textures: var sheetFile = source_file.get_base_dir()+"/"+sheet.image - var image = load_image(sheetFile, "ImageTexture", []) + var image = load_image(sheetFile, "ImageTexture", options) create_atlas_textures(sheetFolder, sheet, image, r_gen_files) return ResourceSaver.save("%s.%s" % [save_path, get_save_extension()], Resource.new()) - - + + func create_folder(folder): var dir = Directory.new() if !dir.dir_exists(folder): @@ -115,7 +121,7 @@ func create_atlas_texture(sheetFolder, sprite, image, r_gen_files): func save_resource(name, texture): create_folder(name.get_base_dir()) - + var status = ResourceSaver.save(name, texture) if status != OK: printerr("Failed to save resource "+name) @@ -137,4 +143,4 @@ func read_sprite_sheet(fileName): func load_image(rel_path, source_path, options): return imageLoader.load_image(rel_path, source_path, options) - + diff --git a/addons/codeandweb.texturepacker/texturepacker_import_tileset.gd b/addons/codeandweb.texturepacker/texturepacker_import_tileset.gd index 3dcc053..1dc90f5 100644 --- a/addons/codeandweb.texturepacker/texturepacker_import_tileset.gd +++ b/addons/codeandweb.texturepacker/texturepacker_import_tileset.gd @@ -25,7 +25,7 @@ extends EditorImportPlugin var imageLoader = preload("image_loader.gd").new() -enum Preset { PRESET_DEFAULT } +enum Preset { PRESET_DEFAULT, PRESET_PIXEL_ART } # const TiledMapReader = preload("tiled_map_reader.gd") @@ -60,10 +60,16 @@ func get_preset_count(): func get_preset_name(preset): match preset: Preset.PRESET_DEFAULT: return "Default" + Preset.PRESET_PIXEL_ART: return "Pixel Art" func get_import_options(preset): - return [] + return [{ + "name": "image_flags", + "default_value": 0 if preset == Preset.PRESET_PIXEL_ART else Texture.FLAGS_DEFAULT, + "property_hint": PROPERTY_HINT_FLAGS, + "hint_string": "Mipmaps,Repeat,Filter,Anisotropic,sRGB,Mirrored Repeat" + }] func get_option_visibility(option, options): @@ -73,14 +79,14 @@ func get_option_visibility(option, options): func get_import_order(): return 200 + func import(source_file, save_path, options, r_platform_variants, r_gen_files): var sheets = read_sprite_sheet(source_file) - var sheetFolder = source_file.get_basename()+".sprites"; - create_folder(sheetFolder) var fileName = "%s.%s" % [source_file.get_basename(), "res"] var tileSet + if File.new().file_exists(fileName): tileSet = ResourceLoader.load(fileName, "TileSet") else: @@ -89,16 +95,15 @@ func import(source_file, save_path, options, r_platform_variants, r_gen_files): var usedIds = [] for sheet in sheets.textures: var sheetFile = source_file.get_base_dir()+"/"+sheet.image - var image = load_image(sheetFile, "ImageTexture", []) + var image = load_image(sheetFile, "ImageTexture", options) r_gen_files.push_back(sheet.image) create_tiles(tileSet, sheet, image, usedIds) - - prune_tileset(tileSet, usedIds) + + prune_tileset(tileSet, usedIds) r_gen_files.push_back(fileName) - ResourceSaver.save(fileName, tileSet) + return ResourceSaver.save(fileName, tileSet) - return ResourceSaver.save("%s.%s" % [save_path, get_save_extension()], Resource.new()) func prune_tileset(tileSet, usedIds): usedIds.sort() @@ -121,7 +126,7 @@ func create_tiles(tileSet, sheet, image, r_usedIds): func create_tile(tileSet, sprite, image): var tileName = sprite.filename.get_basename() - + var id = tileSet.find_tile_by_name(tileName) if id==-1: id = tileSet.get_last_unused_tile_id() @@ -136,7 +141,7 @@ func create_tile(tileSet, sprite, image): func save_resource(name, texture): create_folder(name.get_base_dir()) - + var status = ResourceSaver.save(name, texture) if status != OK: printerr("Failed to save resource "+name) @@ -158,4 +163,4 @@ func read_sprite_sheet(fileName): func load_image(rel_path, source_path, options): return imageLoader.load_image(rel_path, source_path, options) - + From 3ecc62b158a518eb236ded9f016a8390b1263895 Mon Sep 17 00:00:00 2001 From: Unrealomega Date: Mon, 14 Sep 2020 08:44:14 -0400 Subject: [PATCH 2/5] Consolidation and making for slightly more robust error coding * Consolidated the packer into one file as a lot of it's functions are similar. Only issue I can find is that the options don't seem to apply to existing spritesheets .tres files, which may be because it's not recreating them each time. * Removed the extra ResourceSaver in Spritesheet's code, looks like it was a holdover from the tiled code. --- .../codeandweb.texturepacker_importer.gd | 15 +- .../codeandweb.texturepacker/image_loader.gd | 1 + .../texturepacker_import.gd | 223 ++++++++++++++++++ 3 files changed, 229 insertions(+), 10 deletions(-) create mode 100644 addons/codeandweb.texturepacker/texturepacker_import.gd diff --git a/addons/codeandweb.texturepacker/codeandweb.texturepacker_importer.gd b/addons/codeandweb.texturepacker/codeandweb.texturepacker_importer.gd index 285078e..5a0d8eb 100644 --- a/addons/codeandweb.texturepacker/codeandweb.texturepacker_importer.gd +++ b/addons/codeandweb.texturepacker/codeandweb.texturepacker_importer.gd @@ -23,22 +23,17 @@ tool extends EditorPlugin -var import_plugin_tilesheet = null -var import_plugin_spritesheet = null +var import_plugin = null func get_name(): return "TexturePacker Importer" func _enter_tree(): - import_plugin_tilesheet = preload("texturepacker_import_tileset.gd").new() - add_import_plugin(import_plugin_tilesheet) - import_plugin_spritesheet = preload("texturepacker_import_spritesheet.gd").new() - add_import_plugin(import_plugin_spritesheet) + import_plugin = preload("texturepacker_import.gd").new() + add_import_plugin(import_plugin) func _exit_tree(): - remove_import_plugin(import_plugin_spritesheet) - import_plugin_spritesheet = null - remove_import_plugin(import_plugin_tilesheet) - import_plugin_tilesheet = null + remove_import_plugin(import_plugin) + import_plugin = null diff --git a/addons/codeandweb.texturepacker/image_loader.gd b/addons/codeandweb.texturepacker/image_loader.gd index 76ff60d..1f717c9 100644 --- a/addons/codeandweb.texturepacker/image_loader.gd +++ b/addons/codeandweb.texturepacker/image_loader.gd @@ -28,6 +28,7 @@ func load_image(rel_path, source_path, options): var embed = options.embed_internal_images if "embed_internal_images" in options else false var ext = rel_path.get_extension().to_lower() + if not ext in ["png","jpg","webp","pvr","tga"]: printerr("Unsupported image format: %s. Use PNG, JPG, WEBP, PVR or TGA instead." % [ext]) return ERR_FILE_UNRECOGNIZED diff --git a/addons/codeandweb.texturepacker/texturepacker_import.gd b/addons/codeandweb.texturepacker/texturepacker_import.gd new file mode 100644 index 0000000..f1387ad --- /dev/null +++ b/addons/codeandweb.texturepacker/texturepacker_import.gd @@ -0,0 +1,223 @@ +# The MIT License (MIT) +# +# Copyright (c) 2018 Andreas Loew / CodeAndWeb GmbH www.codeandweb.com +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +tool +extends EditorImportPlugin + +var imageLoader = preload("image_loader.gd").new() + +enum Preset { PRESET_DEFAULT, PRESET_PIXEL_ART } + +func _notification(what): + if what == NOTIFICATION_PREDELETE: + imageLoader.free() + +func get_importer_name(): + return "codeandweb.texturepacker_import" + + +func get_visible_name(): + return "TexturePacker Sheet" + + +func get_recognized_extensions(): + return ["tpsheet", "tpset"] + + +func get_save_extension(): + return "res" + + +func get_resource_type(): + return "Resource" + + +func get_preset_count(): + return Preset.size() + + +func get_preset_name(preset): + match preset: + Preset.PRESET_DEFAULT: return "Default" + Preset.PRESET_PIXEL_ART: return "Pixel Art" + + +func get_import_options(preset): + return [{ + "name": "image_flags", + "default_value": 0 if preset == Preset.PRESET_PIXEL_ART else Texture.FLAGS_DEFAULT, + "property_hint": PROPERTY_HINT_FLAGS, + "hint_string": "Mipmaps,Repeat,Filter,Anisotropic,sRGB,Mirrored Repeat" + }] + + +func get_option_visibility(option, options): + return true + + +func get_import_order(): + return 200 + + +func import(source_file, save_path, options, r_platform_variants, r_gen_files): + var sheets = read_sprite_sheet(source_file) + + match (source_file.get_extension()): + "tpsheet": + # Spritesheet + print("Importing sprite sheet from "+source_file); + + var sheetFolder = source_file.get_basename()+".sprites" + if create_folder(sheetFolder) == OK: + var status + + for sheet in sheets.textures: + var sheetFile = source_file.get_base_dir()+"/"+sheet.image + var image = imageLoader.load_image(sheetFile, "ImageTexture", options) + + print(sheetFile) + + status = create_atlas_textures(sheetFolder, sheet, image, r_gen_files) + + if status != OK: + break + + return status # Return if we were successful or not + + return # totally exit out of this + "tpset": + # Tileset + var fileName = "%s.%s" % [source_file.get_basename(), "res"] + + var tileSet + + if File.new().file_exists(fileName): + tileSet = ResourceLoader.load(fileName, "TileSet") + else: + tileSet = TileSet.new() + + var usedIds = [] + for sheet in sheets.textures: + var sheetFile = source_file.get_base_dir()+"/"+sheet.image + var image = imageLoader.load_image(sheetFile, "ImageTexture", options) + r_gen_files.push_back(sheet.image) + create_tiles(tileSet, sheet, image, usedIds) + + prune_tileset(tileSet, usedIds) + + r_gen_files.push_back(fileName) + + return ResourceSaver.save(fileName, tileSet) + +#### Spritesheet code start #### +func create_folder(folder): + var dir = Directory.new() + + if !dir.dir_exists(folder): + var status = dir.make_dir_recursive(folder) + + if status != OK: + printerr("Failed to create folder: " + folder) + + return status + + +func create_atlas_textures(sheetFolder, sheet, image, r_gen_files): + var status + + for sprite in sheet.sprites: + status = create_atlas_texture(sheetFolder, sprite, image, r_gen_files) + + if status != OK: + break + + return status + + +func create_atlas_texture(sheetFolder, sprite, image, r_gen_files): + var texture = AtlasTexture.new() + texture.atlas = image + + var name = sheetFolder+"/"+sprite.filename.get_basename()+".tres" + texture.region = Rect2(sprite.region.x,sprite.region.y,sprite.region.w,sprite.region.h) + texture.margin = Rect2(sprite.margin.x, sprite.margin.y, sprite.margin.w, sprite.margin.h) + r_gen_files.push_back(name) + + return save_resource(name, texture) +#### Spritesheet code end #### + +#### Tileset code start #### +func create_tiles(tileSet, sheet, image, r_usedIds): + for sprite in sheet.sprites: + r_usedIds.push_back(create_tile(tileSet, sprite, image)) + + +func create_tile(tileSet, sprite, image): + var tileName = sprite.filename.get_basename() + + var id = tileSet.find_tile_by_name(tileName) + if id==-1: + id = tileSet.get_last_unused_tile_id() + tileSet.create_tile(id) + tileSet.tile_set_name(id, tileName) + + tileSet.tile_set_texture(id, image) + tileSet.tile_set_region(id, Rect2(sprite.region.x,sprite.region.y,sprite.region.w,sprite.region.h)) + tileSet.tile_set_texture_offset(id, Vector2(sprite.margin.x, sprite.margin.y)) + return id + + +func prune_tileset(tileSet, usedIds): + usedIds.sort() + + for id in tileSet.get_tiles_ids(): + if !usedIds.has(id): + tileSet.remove_tile(id) +#### Tileset code end #### + +func read_sprite_sheet(fileName): + var file = File.new() + + if file.open(fileName, file.READ) != OK: + printerr("Failed to load "+fileName) + + var text = file.get_as_text() + var dict = JSON.parse(text).result + + if !dict: + printerr("Invalid json data in "+fileName) + + file.close() + + return dict + + +func save_resource(name, texture): + create_folder(name.get_base_dir()) + + var status = ResourceSaver.save(name, texture) + + if status != OK: + printerr("Failed to save resource "+name) + + return status + From 6e0bde26a9801c12523282580a799bc150266117 Mon Sep 17 00:00:00 2001 From: Unrealomega Date: Mon, 14 Sep 2020 08:53:31 -0400 Subject: [PATCH 3/5] Spritesheet Presets working properly now Fixed bug that was preventing presets on Spritesheets from working properly. --- .../texturepacker_import.gd | 5 + .../texturepacker_import_spritesheet.gd | 146 --------------- .../texturepacker_import_tileset.gd | 166 ------------------ 3 files changed, 5 insertions(+), 312 deletions(-) delete mode 100644 addons/codeandweb.texturepacker/texturepacker_import_spritesheet.gd delete mode 100644 addons/codeandweb.texturepacker/texturepacker_import_tileset.gd diff --git a/addons/codeandweb.texturepacker/texturepacker_import.gd b/addons/codeandweb.texturepacker/texturepacker_import.gd index f1387ad..4d5b90b 100644 --- a/addons/codeandweb.texturepacker/texturepacker_import.gd +++ b/addons/codeandweb.texturepacker/texturepacker_import.gd @@ -87,6 +87,7 @@ func import(source_file, save_path, options, r_platform_variants, r_gen_files): print("Importing sprite sheet from "+source_file); var sheetFolder = source_file.get_basename()+".sprites" + if create_folder(sheetFolder) == OK: var status @@ -106,6 +107,8 @@ func import(source_file, save_path, options, r_platform_variants, r_gen_files): return # totally exit out of this "tpset": # Tileset + print("Importing tileset from "+source_file); + var fileName = "%s.%s" % [source_file.get_basename(), "res"] var tileSet @@ -140,6 +143,8 @@ func create_folder(folder): return status + return OK # No need to create a new folder + func create_atlas_textures(sheetFolder, sheet, image, r_gen_files): var status diff --git a/addons/codeandweb.texturepacker/texturepacker_import_spritesheet.gd b/addons/codeandweb.texturepacker/texturepacker_import_spritesheet.gd deleted file mode 100644 index 0f472f9..0000000 --- a/addons/codeandweb.texturepacker/texturepacker_import_spritesheet.gd +++ /dev/null @@ -1,146 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2018 Andreas Loew / CodeAndWeb GmbH www.codeandweb.com -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -tool -extends EditorImportPlugin - -var imageLoader = preload("image_loader.gd").new() - -enum Preset { PRESET_DEFAULT, PRESET_PIXEL_ART } - -# const TiledMapReader = preload("tiled_map_reader.gd") - -func _notification(what): - if what == NOTIFICATION_PREDELETE: - imageLoader.free() - -func get_importer_name(): - return "codeandweb.texturepacker_import_spritesheet" - - -func get_visible_name(): - return "SpriteSheet from TexturePacker" - - -func get_recognized_extensions(): - return ["tpsheet"] - - -func get_save_extension(): - return "res" - - -func get_resource_type(): - return "Resource" - - -func get_preset_count(): - return Preset.size() - - -func get_preset_name(preset): - match preset: - Preset.PRESET_DEFAULT: return "Default" - Preset.PRESET_PIXEL_ART: return "Pixel Art" - - -func get_import_options(preset): - return [{ - "name": "image_flags", - "default_value": 0 if preset == Preset.PRESET_PIXEL_ART else Texture.FLAGS_DEFAULT, - "property_hint": PROPERTY_HINT_FLAGS, - "hint_string": "Mipmaps,Repeat,Filter,Anisotropic,sRGB,Mirrored Repeat" - }] - - -func get_option_visibility(option, options): - return true - - -func get_import_order(): - return 200 - - -func import(source_file, save_path, options, r_platform_variants, r_gen_files): - print("Importing sprite sheet from "+source_file); - - var sheets = read_sprite_sheet(source_file) - var sheetFolder = source_file.get_basename()+".sprites" - create_folder(sheetFolder) - - for sheet in sheets.textures: - var sheetFile = source_file.get_base_dir()+"/"+sheet.image - var image = load_image(sheetFile, "ImageTexture", options) - create_atlas_textures(sheetFolder, sheet, image, r_gen_files) - - return ResourceSaver.save("%s.%s" % [save_path, get_save_extension()], Resource.new()) - - -func create_folder(folder): - var dir = Directory.new() - if !dir.dir_exists(folder): - if dir.make_dir_recursive(folder) != OK: - printerr("Failed to create folder: " + folder) - - -func create_atlas_textures(sheetFolder, sheet, image, r_gen_files): - for sprite in sheet.sprites: - if !create_atlas_texture(sheetFolder, sprite, image, r_gen_files): - return false - return true - - -func create_atlas_texture(sheetFolder, sprite, image, r_gen_files): - var texture = AtlasTexture.new() - texture.atlas = image - var name = sheetFolder+"/"+sprite.filename.get_basename()+".tres" - texture.region = Rect2(sprite.region.x,sprite.region.y,sprite.region.w,sprite.region.h) - texture.margin = Rect2(sprite.margin.x, sprite.margin.y, sprite.margin.w, sprite.margin.h) - r_gen_files.push_back(name) - return save_resource(name, texture) - - -func save_resource(name, texture): - create_folder(name.get_base_dir()) - - var status = ResourceSaver.save(name, texture) - if status != OK: - printerr("Failed to save resource "+name) - return false - return true - - -func read_sprite_sheet(fileName): - var file = File.new() - if file.open(fileName, file.READ) != OK: - printerr("Failed to load "+fileName) - var text = file.get_as_text() - var dict = JSON.parse(text).result - if !dict: - printerr("Invalid json data in "+fileName) - file.close() - return dict - - -func load_image(rel_path, source_path, options): - return imageLoader.load_image(rel_path, source_path, options) - diff --git a/addons/codeandweb.texturepacker/texturepacker_import_tileset.gd b/addons/codeandweb.texturepacker/texturepacker_import_tileset.gd deleted file mode 100644 index 1dc90f5..0000000 --- a/addons/codeandweb.texturepacker/texturepacker_import_tileset.gd +++ /dev/null @@ -1,166 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2018 Andreas Loew / CodeAndWeb GmbH www.codeandweb.com -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -tool -extends EditorImportPlugin - -var imageLoader = preload("image_loader.gd").new() - -enum Preset { PRESET_DEFAULT, PRESET_PIXEL_ART } - -# const TiledMapReader = preload("tiled_map_reader.gd") - -func _notification(what): - if what == NOTIFICATION_PREDELETE: - imageLoader.free() - -func get_importer_name(): - return "codeandweb.texturepacker_import_tileset" - - -func get_visible_name(): - return "TileSet from TexturePacker" - - -func get_recognized_extensions(): - return ["tpset"] - - -func get_save_extension(): - return "res" - - -func get_resource_type(): - return "Resource" - - -func get_preset_count(): - return Preset.size() - - -func get_preset_name(preset): - match preset: - Preset.PRESET_DEFAULT: return "Default" - Preset.PRESET_PIXEL_ART: return "Pixel Art" - - -func get_import_options(preset): - return [{ - "name": "image_flags", - "default_value": 0 if preset == Preset.PRESET_PIXEL_ART else Texture.FLAGS_DEFAULT, - "property_hint": PROPERTY_HINT_FLAGS, - "hint_string": "Mipmaps,Repeat,Filter,Anisotropic,sRGB,Mirrored Repeat" - }] - - -func get_option_visibility(option, options): - return true - - -func get_import_order(): - return 200 - - -func import(source_file, save_path, options, r_platform_variants, r_gen_files): - var sheets = read_sprite_sheet(source_file) - - var fileName = "%s.%s" % [source_file.get_basename(), "res"] - - var tileSet - - if File.new().file_exists(fileName): - tileSet = ResourceLoader.load(fileName, "TileSet") - else: - tileSet = TileSet.new() - - var usedIds = [] - for sheet in sheets.textures: - var sheetFile = source_file.get_base_dir()+"/"+sheet.image - var image = load_image(sheetFile, "ImageTexture", options) - r_gen_files.push_back(sheet.image) - create_tiles(tileSet, sheet, image, usedIds) - - prune_tileset(tileSet, usedIds) - - r_gen_files.push_back(fileName) - return ResourceSaver.save(fileName, tileSet) - - -func prune_tileset(tileSet, usedIds): - usedIds.sort() - for id in tileSet.get_tiles_ids(): - if !usedIds.has(id): - tileSet.remove_tile(id) - - -func create_folder(folder): - var dir = Directory.new() - if !dir.dir_exists(folder): - if dir.make_dir_recursive(folder) != OK: - printerr("Failed to create folder: " + folder) - - -func create_tiles(tileSet, sheet, image, r_usedIds): - for sprite in sheet.sprites: - r_usedIds.push_back(create_tile(tileSet, sprite, image)) - - -func create_tile(tileSet, sprite, image): - var tileName = sprite.filename.get_basename() - - var id = tileSet.find_tile_by_name(tileName) - if id==-1: - id = tileSet.get_last_unused_tile_id() - tileSet.create_tile(id) - tileSet.tile_set_name(id, tileName) - - tileSet.tile_set_texture(id, image) - tileSet.tile_set_region(id, Rect2(sprite.region.x,sprite.region.y,sprite.region.w,sprite.region.h)) - tileSet.tile_set_texture_offset(id, Vector2(sprite.margin.x, sprite.margin.y)) - return id - - -func save_resource(name, texture): - create_folder(name.get_base_dir()) - - var status = ResourceSaver.save(name, texture) - if status != OK: - printerr("Failed to save resource "+name) - return false - return true - - -func read_sprite_sheet(fileName): - var file = File.new() - if file.open(fileName, file.READ) != OK: - printerr("Failed to load "+fileName) - var text = file.get_as_text() - var dict = JSON.parse(text).result - if !dict: - printerr("Invalid json data in "+fileName) - file.close() - return dict - - -func load_image(rel_path, source_path, options): - return imageLoader.load_image(rel_path, source_path, options) - From ffdb5d1ca8963f8479b36031521e5f808c111474 Mon Sep 17 00:00:00 2001 From: Unrealomega Date: Mon, 14 Sep 2020 09:10:37 -0400 Subject: [PATCH 4/5] Readability update Made some small changes that are purely for readability. Also removed some debug stuff. --- .../texturepacker_import.gd | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/addons/codeandweb.texturepacker/texturepacker_import.gd b/addons/codeandweb.texturepacker/texturepacker_import.gd index 4d5b90b..70c70e1 100644 --- a/addons/codeandweb.texturepacker/texturepacker_import.gd +++ b/addons/codeandweb.texturepacker/texturepacker_import.gd @@ -79,6 +79,7 @@ func get_import_order(): func import(source_file, save_path, options, r_platform_variants, r_gen_files): + # Dict containing all sprite information var sheets = read_sprite_sheet(source_file) match (source_file.get_extension()): @@ -95,8 +96,6 @@ func import(source_file, save_path, options, r_platform_variants, r_gen_files): var sheetFile = source_file.get_base_dir()+"/"+sheet.image var image = imageLoader.load_image(sheetFile, "ImageTexture", options) - print(sheetFile) - status = create_atlas_textures(sheetFolder, sheet, image, r_gen_files) if status != OK: @@ -118,14 +117,15 @@ func import(source_file, save_path, options, r_platform_variants, r_gen_files): else: tileSet = TileSet.new() - var usedIds = [] + var tiles = [] + for sheet in sheets.textures: var sheetFile = source_file.get_base_dir()+"/"+sheet.image var image = imageLoader.load_image(sheetFile, "ImageTexture", options) r_gen_files.push_back(sheet.image) - create_tiles(tileSet, sheet, image, usedIds) + create_tiles(tileSet, sheet, image, tiles) - prune_tileset(tileSet, usedIds) + prune_tileset(tileSet, tiles) r_gen_files.push_back(fileName) @@ -171,9 +171,9 @@ func create_atlas_texture(sheetFolder, sprite, image, r_gen_files): #### Spritesheet code end #### #### Tileset code start #### -func create_tiles(tileSet, sheet, image, r_usedIds): +func create_tiles(tileSet, sheet, image, tiles): for sprite in sheet.sprites: - r_usedIds.push_back(create_tile(tileSet, sprite, image)) + tiles.push_back(create_tile(tileSet, sprite, image)) func create_tile(tileSet, sprite, image): @@ -191,11 +191,11 @@ func create_tile(tileSet, sprite, image): return id -func prune_tileset(tileSet, usedIds): - usedIds.sort() +func prune_tileset(tileSet, tiles): + tiles.sort() for id in tileSet.get_tiles_ids(): - if !usedIds.has(id): + if !tiles.has(id): tileSet.remove_tile(id) #### Tileset code end #### @@ -217,12 +217,13 @@ func read_sprite_sheet(fileName): func save_resource(name, texture): - create_folder(name.get_base_dir()) + var status = create_folder(name.get_base_dir()) - var status = ResourceSaver.save(name, texture) + if status == OK: + status = ResourceSaver.save(name, texture) - if status != OK: - printerr("Failed to save resource "+name) + if status != OK: + printerr("Failed to save resource "+name) return status From 0d70527b4a51d321d8982a6bd3147c36c2c50b34 Mon Sep 17 00:00:00 2001 From: Unrealomega Date: Mon, 14 Sep 2020 09:13:48 -0400 Subject: [PATCH 5/5] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 340337f..5b77db7 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,12 @@ Important: Enable the plugin on the Project Settings. # Release notes +## 1.0.6 + +* Consolidated both plugin files into a single plugin for sanity sake. +* Added image flags to .tpsheet and .tpset. +* Removed some excess code and added a bit more error checking. + ## 1.0.5 (2020-06-16) * Fixed syntax to support Godot 3.2.2