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