RingOfRaces/addons/vnen.tiled_importer/tiled_tileset_import_plugin.gd

123 lines
3.5 KiB
GDScript3
Raw Normal View History

2020-08-29 14:36:49 +00:00
# The MIT License (MIT)
#
# Copyright (c) 2018 George Marques
#
# 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.
2023-03-25 20:42:54 +00:00
@tool
2020-08-29 14:36:49 +00:00
extends EditorImportPlugin
enum { PRESET_DEFAULT, PRESET_PIXEL_ART }
const TiledMapReader = preload("tiled_map_reader.gd")
2023-03-25 20:42:54 +00:00
func _get_importer_name():
2020-08-29 14:36:49 +00:00
return "vnen.tiled_tileset_importer"
2023-03-25 20:42:54 +00:00
func _get_visible_name():
2020-08-29 14:36:49 +00:00
return "TileSet from Tiled"
2023-03-25 20:42:54 +00:00
func _get_recognized_extensions():
2020-08-29 14:36:49 +00:00
if ProjectSettings.get_setting("tiled_importer/enable_json_format"):
return ["json", "tsx"]
else:
return ["tsx"]
2023-03-25 20:42:54 +00:00
func _get_save_extension():
2020-08-29 14:36:49 +00:00
return "res"
2023-03-25 20:42:54 +00:00
func _get_resource_type():
2020-08-29 14:36:49 +00:00
return "TileSet"
2023-03-25 20:42:54 +00:00
func _get_preset_count():
2020-08-29 14:36:49 +00:00
return 2
2023-03-25 20:42:54 +00:00
func _get_preset_name(preset):
2020-08-29 14:36:49 +00:00
match preset:
PRESET_DEFAULT: return "Default"
PRESET_PIXEL_ART: return "Pixel Art"
2023-03-25 20:42:54 +00:00
func _get_import_options(preset):
2020-08-29 14:36:49 +00:00
return [
{
"name": "custom_properties",
"default_value": true
},
{
"name": "tile_metadata",
"default_value": false
},
{
"name": "image_flags",
2023-03-25 20:42:54 +00:00
"default_value": 0 if preset == PRESET_PIXEL_ART else Texture2D.FLAGS_DEFAULT,
2020-08-29 14:36:49 +00:00
"property_hint": PROPERTY_HINT_FLAGS,
"hint_string": "Mipmaps,Repeat,Filter,Anisotropic,sRGB,Mirrored Repeat"
},
{
"name": "embed_internal_images",
"default_value": true if preset == PRESET_PIXEL_ART else false
},
{
"name": "save_tiled_properties",
"default_value": false
},
{
"name": "apply_offset",
"default_value": false
},
{
"name": "post_import_script",
"default_value": "",
"property_hint": PROPERTY_HINT_FILE,
"hint_string": "*.gd;GDScript"
}
]
2023-03-25 20:42:54 +00:00
func _get_option_visibility(option, options):
2020-08-29 14:36:49 +00:00
return true
func import(source_file, save_path, options, r_platform_variants, r_gen_files):
var map_reader = TiledMapReader.new()
var tileset = map_reader.build_tileset(source_file, options)
if typeof(tileset) != TYPE_OBJECT:
# Error happened
return tileset
# Post imports script
2023-03-25 20:42:54 +00:00
if not options.post_import_script.is_empty():
2020-08-29 14:36:49 +00:00
var script = load(options.post_import_script)
if not script or not script is GDScript:
printerr("Post import script is not a GDScript.")
return ERR_INVALID_PARAMETER
script = script.new()
2023-03-25 20:42:54 +00:00
if not script.has_method("_post_import"):
printerr("Post import script does not have a '_post_import' method.")
2020-08-29 14:36:49 +00:00
return ERR_INVALID_PARAMETER
2023-03-25 20:42:54 +00:00
tileset = script._post_import(tileset)
2020-08-29 14:36:49 +00:00
if not tileset or not tileset is TileSet:
printerr("Invalid TileSet returned from post import script.")
return ERR_INVALID_DATA
2023-03-25 20:42:54 +00:00
return ResourceSaver.save("%s.%s" % [save_path, _get_save_extension()], tileset)