tiled_tileset_import_plugin.gd 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. enum { PRESET_DEFAULT, PRESET_PIXEL_ART }
  25. const TiledMapReader = preload("tiled_map_reader.gd")
  26. func get_importer_name():
  27. return "vnen.tiled_tileset_importer"
  28. func get_visible_name():
  29. return "TileSet from Tiled"
  30. func get_recognized_extensions():
  31. if ProjectSettings.get_setting("tiled_importer/enable_json_format"):
  32. return ["json", "tsx"]
  33. else:
  34. return ["tsx"]
  35. func get_save_extension():
  36. return "res"
  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_import_options(preset):
  46. return [
  47. {
  48. "name": "custom_properties",
  49. "default_value": true
  50. },
  51. {
  52. "name": "tile_metadata",
  53. "default_value": false
  54. },
  55. {
  56. "name": "image_flags",
  57. "default_value": 0 if preset == PRESET_PIXEL_ART else Texture.FLAGS_DEFAULT,
  58. "property_hint": PROPERTY_HINT_FLAGS,
  59. "hint_string": "Mipmaps,Repeat,Filter,Anisotropic,sRGB,Mirrored Repeat"
  60. },
  61. {
  62. "name": "embed_internal_images",
  63. "default_value": true if preset == PRESET_PIXEL_ART else false
  64. },
  65. {
  66. "name": "save_tiled_properties",
  67. "default_value": false
  68. },
  69. {
  70. "name": "apply_offset",
  71. "default_value": false
  72. },
  73. {
  74. "name": "post_import_script",
  75. "default_value": "",
  76. "property_hint": PROPERTY_HINT_FILE,
  77. "hint_string": "*.gd;GDScript"
  78. }
  79. ]
  80. func get_option_visibility(option, options):
  81. return true
  82. func import(source_file, save_path, options, r_platform_variants, r_gen_files):
  83. var map_reader = TiledMapReader.new()
  84. var tileset = map_reader.build_tileset(source_file, options)
  85. if typeof(tileset) != TYPE_OBJECT:
  86. # Error happened
  87. return tileset
  88. # Post imports script
  89. if not options.post_import_script.empty():
  90. var script = load(options.post_import_script)
  91. if not script or not script is GDScript:
  92. printerr("Post import script is not a GDScript.")
  93. return ERR_INVALID_PARAMETER
  94. script = script.new()
  95. if not script.has_method("post_import"):
  96. printerr("Post import script does not have a 'post_import' method.")
  97. return ERR_INVALID_PARAMETER
  98. tileset = script.post_import(tileset)
  99. if not tileset or not tileset is TileSet:
  100. printerr("Invalid TileSet returned from post import script.")
  101. return ERR_INVALID_DATA
  102. return ResourceSaver.save("%s.%s" % [save_path, get_save_extension()], tileset)