diff --git a/addons/gif-importer/importer_plugin.gd b/addons/gif-importer/importer_plugin.gd new file mode 100644 index 00000000..23cab3ce --- /dev/null +++ b/addons/gif-importer/importer_plugin.gd @@ -0,0 +1,74 @@ +# Derived from https://github.com/jegor377/godot-gdgifexporter + +@tool +extends EditorImportPlugin + + +func _can_import_threaded(): + return false + + +func _get_priority(): + return 100 + + +func _get_import_order(): + # 数字越小越提前 + return ResourceImporter.IMPORT_ORDER_DEFAULT - 100 + + +func _get_importer_name(): + return "gif.animated.texture.plugin" + + +func _get_visible_name(): + return "Sprite Frames (Thread Safe)" + + +func _get_recognized_extensions(): + return ["gif"] + + +func _get_save_extension(): + return "tres" + + +func _get_resource_type(): + return "SpriteFrames" + + +func _get_preset_count(): + return 0 + + +func _get_preset_name(i): + return "" + + +func _get_import_options(_path, _i): + return [] + + +func _get_option_visibility(path: String, option_name: StringName, options: Dictionary) -> bool: + return false + + +# 防止同时加载多个文件时卡死 +static var MUTEX = Mutex.new() + + +func _import(source_file, save_path, options, platform_variants, gen_files): + if GifManager == null: + printerr("GifManager is not available!") + return FAILED + var file = FileAccess.open(source_file, FileAccess.READ) + var buffer = file.get_buffer(file.get_length()) + file.close() + MUTEX.lock() + var frames = GifManager.sprite_frames_from_buffer(buffer, 0, 30) + var filename = save_path + "." + _get_save_extension() + var code = ResourceSaver.save(frames, filename) + # await get_tree().create_timer(0.1).timeout + MUTEX.unlock() + print_debug("Imported GIF frames: ", frames.get_frame_count("gif"), " from ", source_file) + return code diff --git a/addons/gif-importer/plugin.cfg b/addons/gif-importer/plugin.cfg new file mode 100644 index 00000000..0b3b15a4 --- /dev/null +++ b/addons/gif-importer/plugin.cfg @@ -0,0 +1,7 @@ +[plugin] + +name="GIF Importer" +description="GifManger 的线程安全修正版本" +author="cakipaul" +version="1.0" +script="plugin.gd" diff --git a/addons/gif-importer/plugin.gd b/addons/gif-importer/plugin.gd new file mode 100644 index 00000000..7227133c --- /dev/null +++ b/addons/gif-importer/plugin.gd @@ -0,0 +1,23 @@ +@tool +extends EditorPlugin + +var import_sprite_frames_plugin +# var post_import_plugin + + +func _enter_tree(): + if GifManager == null: + printerr( + "GifManager is not available, please make sure you have the gif-importer plugin installed." + ) + import_sprite_frames_plugin = preload("importer_plugin.gd").new() + # post_import_plugin = preload("post_import.gd").new() + add_import_plugin(import_sprite_frames_plugin, true) + # add_scene_format_importer_plugin(post_import_plugin) + + +func _exit_tree(): + remove_import_plugin(import_sprite_frames_plugin) + import_sprite_frames_plugin = null + # remove_scene_format_importer_plugin(post_import_plugin) + # post_import_plugin = null diff --git a/asset/art/gif/【胖小孩背着残疾小孩】-侧面呼吸.gif.import b/asset/art/gif/【胖小孩背着残疾小孩】-侧面呼吸.gif.import index 774dbf23..02b2f98e 100644 --- a/asset/art/gif/【胖小孩背着残疾小孩】-侧面呼吸.gif.import +++ b/asset/art/gif/【胖小孩背着残疾小孩】-侧面呼吸.gif.import @@ -1,8 +1,8 @@ [remap] -importer="import_gif_to_sprite_frames" +importer="gif.animated.texture.plugin" type="SpriteFrames" -uid="uid://bgfi4y5vrdv3" +uid="uid://c365bkp2y53f3" path="res://.godot/imported/【胖小孩背着残疾小孩】-侧面呼吸.gif-41cf8f62c31ce34b394d9506a4d3fb39.tres" [deps] @@ -12,4 +12,3 @@ dest_files=["res://.godot/imported/【胖小孩背着残疾小孩】-侧面呼 [params] -frames_per_second=30 diff --git a/asset/art/gif/【胖小孩背着残疾小孩】-呼吸.gif.import b/asset/art/gif/【胖小孩背着残疾小孩】-呼吸.gif.import index 0899d2e2..3e9a2e95 100644 --- a/asset/art/gif/【胖小孩背着残疾小孩】-呼吸.gif.import +++ b/asset/art/gif/【胖小孩背着残疾小孩】-呼吸.gif.import @@ -1,8 +1,8 @@ [remap] -importer="import_gif_to_sprite_frames" +importer="gif.animated.texture.plugin" type="SpriteFrames" -uid="uid://cubcamrofhvtr" +uid="uid://cqi3cn2mbebj" path="res://.godot/imported/【胖小孩背着残疾小孩】-呼吸.gif-c79ab4a31a499090e54afdff7675adfe.tres" [deps] @@ -12,4 +12,3 @@ dest_files=["res://.godot/imported/【胖小孩背着残疾小孩】-呼吸.gif- [params] -frames_per_second=30 diff --git a/asset/art/gif/【胖小孩背着残疾小孩】-正面呼吸.gif.import b/asset/art/gif/【胖小孩背着残疾小孩】-正面呼吸.gif.import index ef96fdf4..98c3bc0d 100644 --- a/asset/art/gif/【胖小孩背着残疾小孩】-正面呼吸.gif.import +++ b/asset/art/gif/【胖小孩背着残疾小孩】-正面呼吸.gif.import @@ -1,8 +1,8 @@ [remap] -importer="import_gif_to_sprite_frames" +importer="gif.animated.texture.plugin" type="SpriteFrames" -uid="uid://77k8twghlrj6" +uid="uid://deor6xh8ho3fh" path="res://.godot/imported/【胖小孩背着残疾小孩】-正面呼吸.gif-9675740292b31deb89f7a836f70478f0.tres" [deps] @@ -12,4 +12,3 @@ dest_files=["res://.godot/imported/【胖小孩背着残疾小孩】-正面呼 [params] -frames_per_second=30 diff --git a/asset/art/gif/【胖小孩背着残疾小孩】-正面抖肩.gif.import b/asset/art/gif/【胖小孩背着残疾小孩】-正面抖肩.gif.import index 78e39412..d8011785 100644 --- a/asset/art/gif/【胖小孩背着残疾小孩】-正面抖肩.gif.import +++ b/asset/art/gif/【胖小孩背着残疾小孩】-正面抖肩.gif.import @@ -1,8 +1,8 @@ [remap] -importer="import_gif_to_sprite_frames" +importer="gif.animated.texture.plugin" type="SpriteFrames" -uid="uid://j15catnhfmwg" +uid="uid://xk4fvx44lu7e" path="res://.godot/imported/【胖小孩背着残疾小孩】-正面抖肩.gif-4259b71a0f04bca96bf0e07177319913.tres" [deps] @@ -12,4 +12,3 @@ dest_files=["res://.godot/imported/【胖小孩背着残疾小孩】-正面抖 [params] -frames_per_second=30 diff --git a/asset/art/gif/【胖小孩背着残疾小孩】-画画.gif.import b/asset/art/gif/【胖小孩背着残疾小孩】-画画.gif.import index 5675cd0f..627a197c 100644 --- a/asset/art/gif/【胖小孩背着残疾小孩】-画画.gif.import +++ b/asset/art/gif/【胖小孩背着残疾小孩】-画画.gif.import @@ -1,8 +1,8 @@ [remap] -importer="import_gif_to_sprite_frames" +importer="gif.animated.texture.plugin" type="SpriteFrames" -uid="uid://p6158w0210vj" +uid="uid://bivlhoec73pq" path="res://.godot/imported/【胖小孩背着残疾小孩】-画画.gif-3b458561a577cef811ccd651b4b69272.tres" [deps] @@ -12,4 +12,3 @@ dest_files=["res://.godot/imported/【胖小孩背着残疾小孩】-画画.gif- [params] -frames_per_second=30 diff --git a/asset/art/gif/右1跑动男孩.gif.import b/asset/art/gif/右1跑动男孩.gif.import index 04717a70..c650f831 100644 --- a/asset/art/gif/右1跑动男孩.gif.import +++ b/asset/art/gif/右1跑动男孩.gif.import @@ -1,8 +1,8 @@ [remap] -importer="import_gif_to_sprite_frames" +importer="gif.animated.texture.plugin" type="SpriteFrames" -uid="uid://doonfsppjqmj6" +uid="uid://deniyxsdbrt1p" path="res://.godot/imported/右1跑动男孩.gif-9079138d4476d9c9b2c631defe192e44.tres" [deps] @@ -12,4 +12,3 @@ dest_files=["res://.godot/imported/右1跑动男孩.gif-9079138d4476d9c9b2c631de [params] -frames_per_second=30 diff --git a/asset/art/gif/右1跑动男孩ceshi.gif b/asset/art/gif/右1跑动男孩ceshi.gif deleted file mode 100644 index ea3e9388..00000000 Binary files a/asset/art/gif/右1跑动男孩ceshi.gif and /dev/null differ diff --git a/asset/art/gif/右1跑动男孩ceshi.gif.import b/asset/art/gif/右1跑动男孩ceshi.gif.import deleted file mode 100644 index 814d747c..00000000 --- a/asset/art/gif/右1跑动男孩ceshi.gif.import +++ /dev/null @@ -1,15 +0,0 @@ -[remap] - -importer="import_gif_to_sprite_frames" -type="SpriteFrames" -uid="uid://5f7eir4l853i" -path="res://.godot/imported/右1跑动男孩ceshi.gif-fd8f8dadf4a76203a9d34f450056efd3.tres" - -[deps] - -source_file="res://asset/art/gif/右1跑动男孩ceshi.gif" -dest_files=["res://.godot/imported/右1跑动男孩ceshi.gif-fd8f8dadf4a76203a9d34f450056efd3.tres"] - -[params] - -frames_per_second=30 diff --git a/config/animation/gif_importer.gd b/config/animation/gif_importer.gd new file mode 100644 index 00000000..9fcd0ac4 --- /dev/null +++ b/config/animation/gif_importer.gd @@ -0,0 +1,41 @@ +extends Control + +var gif_path = "res://asset/art/gif/" +var sprite_frames = preload("res://config/animation/entity_sprite_frames.tres") + + +func _ready(): + var gif_files = [] + for file in DirAccess.open(gif_path).get_files(): + if file.get_extension() == "gif": + gif_files.append(file) + for gif_file in gif_files: + var path = gif_path + gif_file + var frames = load(path) as SpriteFrames + if frames and frames.has_animation("gif"): + var animation_name = gif_file.get_basename() + var dir_name = gif_path + animation_name + "/" + if DirAccess.dir_exists_absolute(dir_name): + # 如果已经存在则跳过,避免重复导入 + # 重新导入时需要删除文件夹 + continue + DirAccess.make_dir_absolute(dir_name) + if not sprite_frames.has_animation(animation_name): + sprite_frames.add_animation(animation_name) + else: + sprite_frames.clear(animation_name) + sprite_frames.set_animation_loop(animation_name, frames.get_animation_loop("gif")) + sprite_frames.set_animation_speed(animation_name, frames.get_animation_speed("gif")) + for i in range(frames.get_frame_count("gif")): + var texture = frames.get_frame_texture("gif", i) as Texture2D + var image = texture.get_image() + # save image to dir + var image_path = dir_name + str(i) + ".png" + image.save_png(image_path) + var new_texture := ImageTexture.create_from_image(image) + # 设置资源路径,sprite_frames 中便会保存引用路径而不是二进制数据 + new_texture.resource_path = image_path + var duration = frames.get_frame_duration("gif", i) + sprite_frames.add_frame(animation_name, new_texture, duration) + print("Imported: ", animation_name, " frames: ", frames.get_frame_count("gif")) + ResourceSaver.save(sprite_frames) diff --git a/config/animation/gif_importer.tscn b/config/animation/gif_importer.tscn new file mode 100644 index 00000000..c6da8984 --- /dev/null +++ b/config/animation/gif_importer.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=2 format=3 uid="uid://b0a8yr25ujbgd"] + +[ext_resource type="Script" path="res://config/animation/gif_importer.gd" id="1_4ckcb"] + +[node name="GifImporter" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_right = 564.0 +offset_bottom = 316.0 +grow_horizontal = 2 +grow_vertical = 2 +scale = Vector2(0.5, 0.5) +script = ExtResource("1_4ckcb") diff --git a/project.godot b/project.godot index f03ba76d..9f3cc4d7 100644 --- a/project.godot +++ b/project.godot @@ -37,7 +37,6 @@ buses/default_bus_layout="res://config/default_bus_layout.tres" [autoload] DebugMenu="*res://addons/debug_menu/debug_menu.tscn" -DialogueManager="*res://addons/dialogue_manager/dialogue_manager.gd" GlobalConfigManager="*res://manager/config_manager/global_config_manager.gd" ArchiveManager="*res://manager/archive_manager/archive_manager.gd" SceneManager="*res://manager/deploy/scene/scene_manager.gd" @@ -45,6 +44,7 @@ AudioManager="*res://manager/audio_manager/audio_manager.gd" EventManager="*res://manager/event_manager/event_manager.gd" CgManager="*res://manager/cg_manager/cg_manager.gd" InputManager="res://manager/input/input_manager.gd" +DialogueManager="*res://addons/dialogue_manager/dialogue_manager.gd" [debug] @@ -69,7 +69,7 @@ window/stretch/mode="canvas_items" [editor_plugins] -enabled=PackedStringArray("res://addons/debug_menu/plugin.cfg", "res://addons/dialogue_manager/plugin.cfg", "res://addons/project-statistics/plugin.cfg") +enabled=PackedStringArray("res://addons/debug_menu/plugin.cfg", "res://addons/dialogue_manager/plugin.cfg", "res://addons/gif-importer/plugin.cfg", "res://addons/project-statistics/plugin.cfg") [gui] @@ -83,9 +83,6 @@ gif.animated.texture.plugin={ "MipMaps": false } import_gif_to_animated_texture={} -import_gif_to_sprite_frames={ -"frames_per_second": 30 -} [input]