tiled_import_plugin.gd 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_importer"
  28. func get_visible_name():
  29. return "Scene from Tiled"
  30. func get_recognized_extensions():
  31. if ProjectSettings.get_setting("tiled_importer/enable_json_format"):
  32. return ["json", "tmx"]
  33. else:
  34. return ["tmx"]
  35. func get_save_extension():
  36. return "scn"
  37. func get_priority():
  38. return 1
  39. func get_import_order():
  40. return 100
  41. func get_resource_type():
  42. return "PackedScene"
  43. func get_preset_count():
  44. return 2
  45. func get_preset_name(preset):
  46. match preset:
  47. PRESET_DEFAULT: return "Default"
  48. PRESET_PIXEL_ART: return "Pixel Art"
  49. func get_import_options(preset):
  50. return [
  51. {
  52. "name": "custom_properties",
  53. "default_value": true
  54. },
  55. {
  56. "name": "tile_metadata",
  57. "default_value": false
  58. },
  59. {
  60. "name": "uv_clip",
  61. "default_value": true
  62. },
  63. {
  64. "name": "image_flags",
  65. "default_value": 0 if preset == PRESET_PIXEL_ART else Texture.FLAGS_DEFAULT,
  66. "property_hint": PROPERTY_HINT_FLAGS,
  67. "hint_string": "Mipmaps,Repeat,Filter,Anisotropic,sRGB,Mirrored Repeat"
  68. },
  69. {
  70. "name": "collision_layer",
  71. "default_value": 1,
  72. "property_hint": PROPERTY_HINT_LAYERS_2D_PHYSICS
  73. },
  74. {
  75. "name": "embed_internal_images",
  76. "default_value": true if preset == PRESET_PIXEL_ART else false
  77. },
  78. {
  79. "name": "save_tiled_properties",
  80. "default_value": false
  81. },
  82. {
  83. "name": "add_background",
  84. "default_value": true
  85. },
  86. {
  87. "name": "post_import_script",
  88. "default_value": "",
  89. "property_hint": PROPERTY_HINT_FILE,
  90. "hint_string": "*.gd;GDScript"
  91. }
  92. ]
  93. func get_option_visibility(option, options):
  94. return true
  95. func import(source_file, save_path, options, r_platform_variants, r_gen_files):
  96. var map_reader = TiledMapReader.new()
  97. # Offset is only optional for importing TileSets
  98. options.apply_offset = true
  99. var scene = map_reader.build(source_file, options)
  100. if typeof(scene) != TYPE_OBJECT:
  101. # Error happened
  102. return scene
  103. # Post imports script
  104. if not options.post_import_script.empty():
  105. var script = load(options.post_import_script)
  106. if not script or not script is GDScript:
  107. printerr("Post import script is not a GDScript.")
  108. return ERR_INVALID_PARAMETER
  109. script = script.new()
  110. if not script.has_method("post_import"):
  111. printerr("Post import script does not have a 'post_import' method.")
  112. return ERR_INVALID_PARAMETER
  113. scene = script.post_import(scene)
  114. if not scene or not scene is Node2D:
  115. printerr("Invalid scene returned from post import script.")
  116. return ERR_INVALID_DATA
  117. var packed_scene = PackedScene.new()
  118. packed_scene.pack(scene)
  119. return ResourceSaver.save("%s.%s" % [save_path, get_save_extension()], packed_scene)