Newer
Older
DungeonShooting / addons / vnen.tiled_importer / tiled_tileset_import_plugin.gd
@小李xl 小李xl on 21 May 2022 3 KB 安装Tiled插件
  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_tileset_importer"
  32.  
  33. func get_visible_name():
  34. return "TileSet from Tiled"
  35.  
  36. func get_recognized_extensions():
  37. if ProjectSettings.get_setting("tiled_importer/enable_json_format"):
  38. return ["json", "tsx"]
  39. else:
  40. return ["tsx"]
  41.  
  42. func get_save_extension():
  43. return "res"
  44.  
  45. func get_import_order():
  46. return 100
  47.  
  48. func get_resource_type():
  49. return "TileSet"
  50.  
  51. func get_preset_count():
  52. return 2
  53.  
  54. func get_preset_name(preset):
  55. match preset:
  56. PRESET_DEFAULT: return "Default"
  57. PRESET_PIXEL_ART: return "Pixel Art"
  58.  
  59. func get_import_options(preset):
  60. return [
  61. {
  62. "name": "custom_properties",
  63. "default_value": true
  64. },
  65. {
  66. "name": "tile_metadata",
  67. "default_value": false
  68. },
  69. {
  70. "name": "image_flags",
  71. "default_value": 0 if preset == PRESET_PIXEL_ART else Texture.FLAGS_DEFAULT,
  72. "property_hint": PROPERTY_HINT_FLAGS,
  73. "hint_string": "Mipmaps,Repeat,Filter,Anisotropic,sRGB,Mirrored Repeat"
  74. },
  75. {
  76. "name": "embed_internal_images",
  77. "default_value": true if preset == PRESET_PIXEL_ART else false
  78. },
  79. {
  80. "name": "save_tiled_properties",
  81. "default_value": false
  82. },
  83. {
  84. "name": "post_import_script",
  85. "default_value": "",
  86. "property_hint": PROPERTY_HINT_FILE,
  87. "hint_string": "*.gd;GDScript"
  88. }
  89. ]
  90.  
  91. func get_option_visibility(option, options):
  92. return true
  93.  
  94. func import(source_file, save_path, options, r_platform_variants, r_gen_files):
  95. var map_reader = TiledMapReader.new()
  96.  
  97. var tileset = map_reader.build_tileset(source_file, options)
  98.  
  99. if typeof(tileset) != TYPE_OBJECT:
  100. # Error happened
  101. return tileset
  102.  
  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.  
  110. script = script.new()
  111. if not script.has_method("post_import"):
  112. printerr("Post import script does not have a 'post_import' method.")
  113. return ERR_INVALID_PARAMETER
  114.  
  115. tileset = script.post_import(tileset)
  116.  
  117. if not tileset or not tileset is TileSet:
  118. printerr("Invalid TileSet returned from post import script.")
  119. return ERR_INVALID_DATA
  120.  
  121. return ResourceSaver.save("%s.%s" % [save_path, get_save_extension()], tileset)