tiled_tileset_import_plugin.gd 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # The MIT License (MIT)
  2. #
  3. # Copyright (c) 2018 George Marques
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in all
  13. # copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. @tool
  23. extends EditorImportPlugin
  24. ################################################################################
  25. ################################################################################
  26. enum { PRESET_DEFAULT, PRESET_PIXEL_ART }
  27. func _get_importer_name():
  28. return "vnen.tiled_tileset_importer"
  29. func _get_visible_name():
  30. return "TileSet from Tiled"
  31. func _get_recognized_extensions():
  32. return ["tsx"]
  33. func _get_save_extension():
  34. return "res"
  35. func _get_import_order():
  36. return 0
  37. func _get_resource_type():
  38. return "TileSet"
  39. func _get_preset_count():
  40. return 2
  41. func _get_preset_name(preset):
  42. match preset:
  43. PRESET_DEFAULT: return "Default"
  44. PRESET_PIXEL_ART: return "Pixel Art"
  45. func _get_priority():
  46. return 1
  47. func _get_import_options(path, preset):
  48. return [
  49. {
  50. "name": "custom_properties",
  51. "default_value": true
  52. },
  53. {
  54. "name": "tile_metadata",
  55. "default_value": false
  56. },
  57. {
  58. "name": "embed_internal_images",
  59. "default_value": true if preset == PRESET_PIXEL_ART else false
  60. },
  61. {
  62. "name": "save_tiled_properties",
  63. "default_value": false
  64. },
  65. {
  66. "name": "apply_offset",
  67. "default_value": false
  68. },
  69. {
  70. "name": "post_import_script",
  71. "default_value": "",
  72. "property_hint": PROPERTY_HINT_FILE,
  73. "hint_string": "*.gd;GDScript"
  74. }
  75. ]
  76. func _get_option_visibility(path, option, options):
  77. return true
  78. func _import(source_file, save_path, options, r_platform_variants, r_gen_files):
  79. var mapReader = TiledMapReader.new()
  80. var tileset = mapReader.build_tileset(source_file, options)
  81. if typeof(tileset) != TYPE_OBJECT:
  82. # Error happened
  83. return tileset
  84. # Post imports script
  85. if not options.post_import_script.is_empty():
  86. var script = load(options.post_import_script)
  87. if not script or not script is GDScript:
  88. printerr("Post import script is not a GDScript.")
  89. return ERR_INVALID_PARAMETER
  90. script = script.new()
  91. if not script.has_method("post_import"):
  92. printerr("Post import script does not have a 'post_import' method.")
  93. return ERR_INVALID_PARAMETER
  94. tileset = script.post_import(tileset)
  95. if not tileset or not tileset is TileSet:
  96. printerr("Invalid TileSet returned from post import script.")
  97. return ERR_INVALID_DATA
  98. return ResourceSaver.save(tileset, "%s.%s" % [save_path, _get_save_extension()])