init
This commit is contained in:
commit
f389e4e807
186 changed files with 10853 additions and 0 deletions
|
|
@ -0,0 +1,345 @@
|
|||
@tool
|
||||
extends "../base_sprite_resource_creator.gd"
|
||||
|
||||
const wizard_config = preload("../../config/wizard_config.gd")
|
||||
|
||||
var _DEFAULT_ANIMATION_LIBRARY = "" # GLOBAL
|
||||
|
||||
func create_animations(target_node: Node, player: AnimationPlayer, aseprite_files: Dictionary, options: Dictionary):
|
||||
var result = _import(target_node, player, aseprite_files, options)
|
||||
|
||||
if result != result_code.SUCCESS:
|
||||
logger.error(result_code.get_error_message(result))
|
||||
|
||||
|
||||
func _import(target_node: Node, player: AnimationPlayer, aseprite_files: Dictionary, options: Dictionary):
|
||||
var source_file = aseprite_files.data_file
|
||||
var sprite_sheet = aseprite_files.sprite_sheet
|
||||
var data = _aseprite_file_exporter.load_json_content(source_file)
|
||||
|
||||
if not data.is_ok:
|
||||
return data.code
|
||||
|
||||
var content = data.content
|
||||
|
||||
var context = {}
|
||||
|
||||
if target_node is CanvasItem:
|
||||
target_node.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
|
||||
else:
|
||||
target_node.texture_filter = BaseMaterial3D.TEXTURE_FILTER_NEAREST
|
||||
|
||||
var texture := _load_texture(sprite_sheet, options.get("should_create_portable_texture", false))
|
||||
|
||||
_setup_texture(target_node, texture, content, context, options.slice != "")
|
||||
var result = _configure_animations(target_node, player, content, context, options)
|
||||
if result != result_code.SUCCESS:
|
||||
return result
|
||||
|
||||
return _cleanup_animations(target_node, player, content, options)
|
||||
|
||||
|
||||
func _load_texture(sprite_sheet: String, use_portable_tex: bool) -> Texture2D:
|
||||
if use_portable_tex:
|
||||
return _load_compressed_texture(sprite_sheet)
|
||||
|
||||
var texture = ResourceLoader.load(sprite_sheet, 'Image', ResourceLoader.CACHE_MODE_IGNORE)
|
||||
texture.take_over_path(sprite_sheet)
|
||||
return texture
|
||||
|
||||
|
||||
func _configure_animations(target_node: Node, player: AnimationPlayer, content: Dictionary, context: Dictionary, options: Dictionary):
|
||||
var frames = _aseprite.get_content_frames(content)
|
||||
var slice_rect = null
|
||||
if options.slice != "":
|
||||
options["slice_rect"] = _aseprite.get_slice_rect(content, options.slice)
|
||||
|
||||
if not player.has_animation_library(_DEFAULT_ANIMATION_LIBRARY):
|
||||
player.add_animation_library(_DEFAULT_ANIMATION_LIBRARY, AnimationLibrary.new())
|
||||
|
||||
if content.meta.has("frameTags") and content.meta.frameTags.size() > 0:
|
||||
var result = result_code.SUCCESS
|
||||
for tag in content.meta.frameTags:
|
||||
var selected_frames = frames.slice(tag.from, tag.to + 1)
|
||||
result = _add_animation_frames(target_node, player, tag.name, selected_frames, context, options, tag.direction, int(tag.get("repeat", -1)))
|
||||
if result != result_code.SUCCESS:
|
||||
break
|
||||
return result
|
||||
else:
|
||||
return _add_animation_frames(target_node, player, "default", frames, context, options)
|
||||
|
||||
|
||||
func _add_animation_frames(target_node: Node, player: AnimationPlayer, anim_name: String, frames: Array, context: Dictionary, options: Dictionary, direction = 'forward', repeat = -1):
|
||||
var animation_name = anim_name
|
||||
var library_name = _DEFAULT_ANIMATION_LIBRARY
|
||||
var is_loopable = _config.is_default_animation_loop_enabled()
|
||||
var slice_rect = options.get("slice_rect")
|
||||
var is_importing_slice: bool = slice_rect != null
|
||||
|
||||
var anim_tokens := anim_name.split("/")
|
||||
|
||||
if anim_tokens.size() > 2:
|
||||
push_error("Invalid animation name: %s" % animation_name)
|
||||
return
|
||||
elif anim_tokens.size() == 2:
|
||||
library_name = anim_tokens[0]
|
||||
animation_name = anim_tokens[1]
|
||||
|
||||
if not _validate_animation_name(animation_name):
|
||||
push_error("Invalid animation name: %s" % animation_name)
|
||||
return
|
||||
|
||||
# Create library if doesn't exist
|
||||
if library_name != _DEFAULT_ANIMATION_LIBRARY and not player.has_animation_library(library_name):
|
||||
player.add_animation_library(library_name, AnimationLibrary.new())
|
||||
|
||||
# Check loop
|
||||
if animation_name.begins_with(_config.get_animation_loop_exception_prefix()):
|
||||
animation_name = animation_name.substr(_config.get_animation_loop_exception_prefix().length())
|
||||
is_loopable = not is_loopable
|
||||
|
||||
# Add library
|
||||
if not player.get_animation_library(library_name).has_animation(animation_name):
|
||||
player.get_animation_library(library_name).add_animation(animation_name, Animation.new())
|
||||
|
||||
var full_name = (
|
||||
animation_name if library_name == "" else "%s/%s" % [library_name, animation_name]
|
||||
)
|
||||
|
||||
var animation = player.get_animation(full_name)
|
||||
_cleanup_tracks(target_node, player, animation)
|
||||
|
||||
var frame_track = _get_property_track_path(player, target_node, _get_frame_property(is_importing_slice))
|
||||
var frame_track_index = _create_track(target_node, animation, frame_track)
|
||||
|
||||
if direction == "reverse" or direction == "pingpong_reverse":
|
||||
frames.reverse()
|
||||
|
||||
var animation_length = 0
|
||||
|
||||
var repetition = 1
|
||||
|
||||
if repeat != -1:
|
||||
is_loopable = false
|
||||
repetition = repeat
|
||||
|
||||
for i in range(repetition):
|
||||
for frame in frames:
|
||||
var frame_key = _get_frame_key(target_node, frame, context, slice_rect)
|
||||
animation.track_insert_key(frame_track_index, animation_length, frame_key)
|
||||
|
||||
var duration = frame.duration
|
||||
if options.get("convert_to_fps"):
|
||||
var old_ms = options.get("convert_ms_field")
|
||||
var target_fps = options.get("convert_fps_field")
|
||||
duration = (frame.duration / old_ms) * (1000 / target_fps)
|
||||
|
||||
animation_length += duration / 1000
|
||||
|
||||
# Godot 4 has an Animation.LOOP_PINGPONG mode, however it does not
|
||||
# behave like in Aseprite, so I'm keeping the custom implementation
|
||||
if direction.begins_with("pingpong"):
|
||||
var working_frames = frames.duplicate()
|
||||
working_frames.remove_at(working_frames.size() - 1)
|
||||
if is_loopable or (repetition > 1 and i < repetition - 1):
|
||||
working_frames.remove_at(0)
|
||||
working_frames.reverse()
|
||||
|
||||
for frame in working_frames:
|
||||
var frame_key = _get_frame_key(target_node, frame, context, slice_rect)
|
||||
animation.track_insert_key(frame_track_index, animation_length, frame_key)
|
||||
animation_length += frame.duration / 1000
|
||||
|
||||
# if keep_anim_length is enabled only adjust length if
|
||||
# - there aren't other tracks besides metas and frame
|
||||
# - the current animation is shorter than new one
|
||||
if not options.keep_anim_length or (animation.get_track_count() == 1 or animation.length < animation_length):
|
||||
animation.length = animation_length
|
||||
|
||||
animation.loop_mode = Animation.LOOP_LINEAR if is_loopable else Animation.LOOP_NONE
|
||||
|
||||
return result_code.SUCCESS
|
||||
|
||||
|
||||
const _INVALID_TOKENS := ["/", ":", ",", "["]
|
||||
|
||||
|
||||
func _validate_animation_name(name: String) -> bool:
|
||||
return not _INVALID_TOKENS.any(func(token: String): return token in name)
|
||||
|
||||
|
||||
func _create_track(target_node: Node, animation: Animation, track: String) -> int:
|
||||
var track_index = animation.find_track(track, Animation.TYPE_VALUE)
|
||||
|
||||
if track_index != -1:
|
||||
animation.remove_track(track_index)
|
||||
|
||||
track_index = animation.add_track(Animation.TYPE_VALUE)
|
||||
animation.track_set_path(track_index, track)
|
||||
animation.track_set_interpolation_loop_wrap(track_index, false)
|
||||
animation.value_track_set_update_mode(track_index, Animation.UPDATE_DISCRETE)
|
||||
|
||||
return track_index
|
||||
|
||||
|
||||
func _get_property_track_path(player: AnimationPlayer, target_node: Node, prop: String) -> String:
|
||||
var node_path = player.get_node(player.root_node).get_path_to(target_node)
|
||||
return "%s:%s" % [node_path, prop]
|
||||
|
||||
|
||||
func _cleanup_animations(target_node: Node, player: AnimationPlayer, content: Dictionary, options: Dictionary):
|
||||
if not (content.meta.has("frameTags") and content.meta.frameTags.size() > 0):
|
||||
return result_code.SUCCESS
|
||||
|
||||
_remove_unused_animations(target_node, player, content)
|
||||
|
||||
_hide_unused_nodes(player, content)
|
||||
|
||||
return result_code.SUCCESS
|
||||
|
||||
|
||||
## remove tracks and animations for nodes without animation tag
|
||||
func _remove_unused_animations(target_node: Node, player: AnimationPlayer, content: Dictionary):
|
||||
var tags: Array[String] = []
|
||||
for t in content.meta.frameTags:
|
||||
tags.push_back(_animation_name_without_loop_prefix(t.name))
|
||||
|
||||
for a in player.get_animation_list():
|
||||
if tags.has(a):
|
||||
continue
|
||||
var animation := player.get_animation(a)
|
||||
|
||||
for p in _get_props_to_cleanup():
|
||||
var track = _get_property_track_path(player, target_node, p)
|
||||
var track_index = animation.find_track(track, Animation.TYPE_VALUE)
|
||||
if track_index != -1:
|
||||
animation.remove_track(track_index)
|
||||
|
||||
|
||||
if animation.get_track_count() == 0:
|
||||
var p = _get_animation_data(a)
|
||||
player.get_animation_library(p.library).remove_animation(p.animation)
|
||||
|
||||
|
||||
## control visibility track when node has "hide unused" option available
|
||||
func _hide_unused_nodes(player: AnimationPlayer, content: Dictionary):
|
||||
var root_node := player.get_node(player.root_node)
|
||||
var all_animations := player.get_animation_list()
|
||||
var all_sprite_nodes: Array[Node] = []
|
||||
var animation_sprites := {}
|
||||
|
||||
for a in all_animations:
|
||||
var animation := player.get_animation(a)
|
||||
var sprite_nodes: Array[Node] = []
|
||||
|
||||
# get all supported nodes in animation
|
||||
for track_idx in animation.get_track_count():
|
||||
var raw_path := animation.track_get_path(track_idx)
|
||||
|
||||
var path := _remove_properties_from_path(raw_path)
|
||||
var sprite_node := root_node.get_node(path)
|
||||
|
||||
if not _is_supported_node(sprite_node):
|
||||
continue
|
||||
|
||||
# ignore nodes with no wizard config or not supposed to be hidden
|
||||
if not wizard_config.has_config(sprite_node) or not wizard_config.load_config(sprite_node).get("set_vis_track", false):
|
||||
continue
|
||||
|
||||
if not sprite_nodes.has(sprite_node):
|
||||
sprite_nodes.append(sprite_node)
|
||||
if not all_sprite_nodes.has(sprite_node):
|
||||
all_sprite_nodes.append(sprite_node)
|
||||
|
||||
animation_sprites[animation] = sprite_nodes
|
||||
|
||||
for animation: Animation in animation_sprites:
|
||||
var sprite_nodes : Array[Node] = animation_sprites[animation]
|
||||
for node in all_sprite_nodes:
|
||||
# node should be visible if they are in the list and have tracks available
|
||||
var node_visibility: bool = sprite_nodes.has(node) and _relevant_track_count(node, player, animation) > 0
|
||||
var visible_track := _get_property_track_path(player, node, "visible")
|
||||
var visible_track_index := _create_track(node, animation, visible_track)
|
||||
animation.track_insert_key(visible_track_index, 0, node_visibility)
|
||||
|
||||
|
||||
func list_layers(file: String, only_visibles = false) -> Array:
|
||||
return _aseprite.list_layers(file, only_visibles)
|
||||
|
||||
|
||||
func list_slices(file: String) -> Array:
|
||||
return _aseprite.list_slices(file)
|
||||
|
||||
|
||||
func _remove_properties_from_path(path: NodePath) -> NodePath:
|
||||
var string_path := path as String
|
||||
if !(":" in string_path):
|
||||
return string_path as NodePath
|
||||
|
||||
var property_path := path.get_concatenated_subnames() as String
|
||||
string_path = string_path.substr(0, string_path.length() - property_path.length() - 1)
|
||||
|
||||
return string_path as NodePath
|
||||
|
||||
|
||||
func _cleanup_tracks(target_node: Node, player: AnimationPlayer, animation: Animation):
|
||||
for track_key in ["texture", "hframes", "vframes", "region_rect", "frame"]:
|
||||
var track = _get_property_track_path(player, target_node, track_key)
|
||||
var track_index = animation.find_track(track, Animation.TYPE_VALUE)
|
||||
if track_index != -1:
|
||||
animation.remove_track(track_index)
|
||||
|
||||
|
||||
func _setup_texture(target_node: Node, texture: Texture2D, content: Dictionary, context: Dictionary, is_importing_slice: bool):
|
||||
push_error("_setup_texture not implemented!")
|
||||
|
||||
|
||||
func _get_frame_property(is_importing_slice: bool) -> String:
|
||||
push_error("_get_frame_property not implemented!")
|
||||
return ""
|
||||
|
||||
|
||||
func _get_frame_key(target_node: Node, frame: Dictionary, context: Dictionary, slice_info: Variant):
|
||||
push_error("_get_frame_key not implemented!")
|
||||
|
||||
|
||||
func _get_props_to_cleanup() -> Array[String]:
|
||||
push_error("_props_to_cleanup not implemented!")
|
||||
return []
|
||||
|
||||
|
||||
func _is_supported_node(target_node: Node):
|
||||
return target_node is Sprite2D or target_node is Sprite3D or target_node is TextureRect
|
||||
|
||||
|
||||
func _get_animation_data(animaton_name: String) -> Dictionary:
|
||||
var parts = animaton_name.split("/")
|
||||
|
||||
if parts.size() == 2:
|
||||
return {
|
||||
"library": parts[0],
|
||||
"animation": parts[1],
|
||||
}
|
||||
return {
|
||||
"library": _DEFAULT_ANIMATION_LIBRARY,
|
||||
"animation": parts[0],
|
||||
}
|
||||
|
||||
|
||||
func _relevant_track_count(target_node: Node, player: AnimationPlayer, animation: Animation) -> int:
|
||||
var track_count := 0
|
||||
for p in _get_props_to_cleanup():
|
||||
if p == "visible":
|
||||
continue
|
||||
var track = _get_property_track_path(player, target_node, p)
|
||||
var track_index = animation.find_track(track, Animation.TYPE_VALUE)
|
||||
if track_index != -1:
|
||||
track_count += 1
|
||||
|
||||
return track_count
|
||||
|
||||
|
||||
func _animation_name_without_loop_prefix(animation_name: String) -> String:
|
||||
if animation_name.begins_with(_config.get_animation_loop_exception_prefix()):
|
||||
return animation_name.substr(_config.get_animation_loop_exception_prefix().length())
|
||||
return animation_name
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://b36acqd4tlnkl
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
extends "animation_creator.gd"
|
||||
|
||||
|
||||
func _setup_texture(sprite: Node, texture: Texture2D, content: Dictionary, context: Dictionary, is_importing_slice: bool):
|
||||
sprite.texture = texture
|
||||
|
||||
if content.frames.is_empty():
|
||||
return
|
||||
|
||||
if is_importing_slice:
|
||||
sprite.region_enabled = true
|
||||
sprite.hframes = 1
|
||||
sprite.vframes = 1
|
||||
sprite.frame = 0
|
||||
else:
|
||||
sprite.region_enabled = false
|
||||
sprite.hframes = content.meta.size.w / content.frames[0].sourceSize.w
|
||||
sprite.vframes = content.meta.size.h / content.frames[0].sourceSize.h
|
||||
|
||||
|
||||
func _get_frame_property(is_importing_slice: bool) -> String:
|
||||
return "frame" if not is_importing_slice else "region_rect"
|
||||
|
||||
|
||||
func _get_props_to_cleanup() -> Array[String]:
|
||||
return ["frame", "region_rect", "visible"]
|
||||
|
||||
|
||||
|
||||
func _get_frame_key(sprite: Node, frame: Dictionary, context: Dictionary, slice_info: Variant):
|
||||
if slice_info != null:
|
||||
return _create_slice_rect(frame, slice_info)
|
||||
return _calculate_frame_index(sprite,frame)
|
||||
|
||||
|
||||
func _calculate_frame_index(sprite: Node, frame: Dictionary) -> int:
|
||||
var column = floor(frame.frame.x * sprite.hframes / sprite.texture.get_width())
|
||||
var row = floor(frame.frame.y * sprite.vframes / sprite.texture.get_height())
|
||||
return (row * sprite.hframes) + column
|
||||
|
||||
|
||||
func _create_slice_rect(frame_data: Dictionary, slice_rect: Rect2) -> Rect2:
|
||||
var frame = frame_data.frame
|
||||
return Rect2(
|
||||
frame.x + slice_rect.position.x,
|
||||
frame.y + slice_rect.position.y,
|
||||
slice_rect.size.x,
|
||||
slice_rect.size.y
|
||||
)
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://ccht4wna4ihcj
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
extends "animation_creator.gd"
|
||||
|
||||
|
||||
func _setup_texture(target_node: Node, texture: Texture2D, content: Dictionary, context: Dictionary, _is_importing_slice: bool):
|
||||
context["base_texture"] = texture
|
||||
|
||||
|
||||
func _get_frame_property(_is_importing_slice: bool) -> String:
|
||||
return "texture"
|
||||
|
||||
|
||||
func _get_props_to_cleanup() -> Array[String]:
|
||||
return ["texture", "visible"]
|
||||
|
||||
|
||||
func _get_frame_key(target_node: Node, frame: Dictionary, context: Dictionary, slice_info: Variant):
|
||||
return _get_atlas_texture(context["base_texture"], frame, slice_info)
|
||||
|
||||
|
||||
func _get_atlas_texture(base_texture: Texture2D, frame_data: Dictionary, slice_info: Variant) -> AtlasTexture:
|
||||
var tex = AtlasTexture.new()
|
||||
tex.atlas = base_texture
|
||||
tex.region = Rect2(Vector2(frame_data.frame.x, frame_data.frame.y), Vector2(frame_data.frame.w, frame_data.frame.h))
|
||||
tex.filter_clip = true
|
||||
|
||||
if slice_info != null:
|
||||
tex.region.position.x += slice_info.position.x
|
||||
tex.region.position.y += slice_info.position.y
|
||||
tex.region.size.x = slice_info.size.x
|
||||
tex.region.size.y = slice_info.size.y
|
||||
|
||||
return tex
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://b8kakwybff660
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
@tool
|
||||
extends RefCounted
|
||||
|
||||
const logger = preload("../config/logger.gd")
|
||||
var result_code = preload("../config/result_codes.gd")
|
||||
var _aseprite = preload("../aseprite/aseprite.gd").new()
|
||||
var _aseprite_file_exporter = preload("../aseprite/file_exporter.gd").new()
|
||||
|
||||
var _config = preload("../config/config.gd").new()
|
||||
|
||||
|
||||
func _load_compressed_texture(sprite_sheet: String) -> PortableCompressedTexture2D:
|
||||
var global_path = ProjectSettings.globalize_path(sprite_sheet)
|
||||
var image = Image.load_from_file(global_path)
|
||||
if image == null or image.is_empty():
|
||||
logger.error('Failed to load sprite sheet image', sprite_sheet)
|
||||
return null
|
||||
var tex := PortableCompressedTexture2D.new()
|
||||
tex.create_from_image(image, PortableCompressedTexture2D.COMPRESSION_MODE_LOSSLESS)
|
||||
return tex
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://ceuwfyr4il56t
|
||||
|
|
@ -0,0 +1,297 @@
|
|||
@tool
|
||||
extends "../base_sprite_resource_creator.gd"
|
||||
|
||||
enum {
|
||||
FILE_EXPORT_MODE,
|
||||
LAYERS_EXPORT_MODE
|
||||
}
|
||||
|
||||
###
|
||||
### Create SpriteFrames from aseprite files and insert
|
||||
### them to the animated_sprite node
|
||||
###
|
||||
func create_animations(animated_sprite: Node, aseprite_files: Dictionary, options: Dictionary) -> void:
|
||||
var sprite_frames_result = _create_sprite_frames(aseprite_files, options)
|
||||
if not sprite_frames_result.is_ok:
|
||||
logger.error(result_code.get_error_message(sprite_frames_result.code))
|
||||
return
|
||||
|
||||
animated_sprite.frames = sprite_frames_result.content.resource
|
||||
|
||||
if animated_sprite is CanvasItem:
|
||||
animated_sprite.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
|
||||
else:
|
||||
animated_sprite.texture_filter = BaseMaterial3D.TEXTURE_FILTER_NEAREST
|
||||
|
||||
|
||||
func create_resources(source_files: Array, options: Dictionary = {}) -> Dictionary:
|
||||
var resources = []
|
||||
|
||||
for o in source_files:
|
||||
if o.is_empty():
|
||||
return result_code.error(result_code.ERR_ASEPRITE_EXPORT_FAILED)
|
||||
|
||||
var result = _create_sprite_frames(o, options)
|
||||
|
||||
if not result.is_ok:
|
||||
return result
|
||||
|
||||
resources.push_back({
|
||||
"data_file": o.data_file,
|
||||
"resource": result.content.resource,
|
||||
"extra_gen_files": result.content.extra_gen_files
|
||||
})
|
||||
|
||||
return result_code.result(resources)
|
||||
|
||||
|
||||
func _create_sprite_frames(data: Dictionary, options: Dictionary) -> Dictionary:
|
||||
var aseprite_resources = _load_aseprite_resources(data, options)
|
||||
if not aseprite_resources.is_ok:
|
||||
return aseprite_resources
|
||||
|
||||
var resource = _create_sprite_frames_with_animations(
|
||||
aseprite_resources.content.metadata,
|
||||
aseprite_resources.content.texture,
|
||||
options,
|
||||
)
|
||||
|
||||
return result_code.result({
|
||||
"resource": resource,
|
||||
"extra_gen_files": aseprite_resources.content.extra_gen_files
|
||||
})
|
||||
|
||||
func _load_aseprite_resources(aseprite_data: Dictionary, options: Dictionary) -> Dictionary:
|
||||
var content_result = _aseprite_file_exporter.load_json_content(aseprite_data.data_file)
|
||||
|
||||
if not content_result.is_ok:
|
||||
return content_result
|
||||
|
||||
var result = _load_or_create_texture_resource(aseprite_data.sprite_sheet, options)
|
||||
|
||||
if result.get("error", false):
|
||||
return result_code.error(result_code.ERR_ASEPRITE_EXPORT_FAILED)
|
||||
|
||||
return result_code.result({
|
||||
"metadata": content_result.content,
|
||||
"texture": result.texture,
|
||||
"extra_gen_files": result.extra_gen_files
|
||||
})
|
||||
|
||||
|
||||
func _load_or_create_texture_resource(sprite_sheet: String, options: Dictionary) -> Dictionary:
|
||||
if not options.get("should_create_portable_texture", false):
|
||||
var tex = _load_texture(sprite_sheet)
|
||||
if tex == null:
|
||||
return { "texture": null, "error": true, "extra_gen_files": [] }
|
||||
return { "texture": tex, "extra_gen_files": [] }
|
||||
|
||||
if options.get("sheet_base_path", "") == "":
|
||||
var tex = create_packed_texture(sprite_sheet)
|
||||
if tex == null:
|
||||
return { "texture": null, "error": true, "extra_gen_files": [] }
|
||||
return { "texture": tex, "extra_gen_files": [] }
|
||||
|
||||
var texture_path = "%s.%s.texture.res" % [options.sheet_base_path, sprite_sheet.get_file().get_basename() ]
|
||||
var tex = create_packed_texture(sprite_sheet, texture_path)
|
||||
if tex == null:
|
||||
return { "texture": null, "error": true, "extra_gen_files": [texture_path] }
|
||||
|
||||
return {
|
||||
"texture": tex,
|
||||
"extra_gen_files": [texture_path]
|
||||
}
|
||||
|
||||
|
||||
func save_resources(resources: Array) -> int:
|
||||
for resource in resources:
|
||||
var code = _save_resource(resource.resource, resource.data_file)
|
||||
if code != OK:
|
||||
return code
|
||||
return OK
|
||||
|
||||
|
||||
func _save_resource(resource, source_path: String) -> int:
|
||||
var save_path = "%s.%s" % [source_path.get_basename(), "res"]
|
||||
var code = ResourceSaver.save(resource, save_path, ResourceSaver.FLAG_REPLACE_SUBRESOURCE_PATHS)
|
||||
resource.take_over_path(save_path)
|
||||
return code
|
||||
|
||||
|
||||
func _create_sprite_frames_with_animations(content: Dictionary, texture, options: Dictionary) -> SpriteFrames:
|
||||
var frame_cache = {}
|
||||
var frames = _aseprite.get_content_frames(content)
|
||||
var sprite_frames := SpriteFrames.new()
|
||||
sprite_frames.remove_animation("default")
|
||||
|
||||
var frame_rect: Variant = null
|
||||
|
||||
# currently, aseprite does not work with the --slice option, so we need to manually
|
||||
# do it. https://github.com/aseprite/aseprite/issues/2469
|
||||
if options.get("slice", "") != "":
|
||||
frame_rect = _aseprite.get_slice_rect(content, options.slice)
|
||||
|
||||
if content.meta.has("frameTags") and content.meta.frameTags.size() > 0:
|
||||
for tag in content.meta.frameTags:
|
||||
var selected_frames = frames.slice(tag.from, tag.to + 1)
|
||||
_add_animation_frames(
|
||||
sprite_frames,
|
||||
tag.name,
|
||||
selected_frames,
|
||||
texture,
|
||||
frame_rect,
|
||||
{ "should_round_fps": options.get("should_round_fps", true) },
|
||||
tag.direction,
|
||||
int(tag.get("repeat", -1)),
|
||||
frame_cache
|
||||
)
|
||||
else:
|
||||
_add_animation_frames(sprite_frames, "default", frames, texture, frame_rect, { "should_round_fps": options.get("should_round_fps", true) })
|
||||
|
||||
return sprite_frames
|
||||
|
||||
|
||||
func _add_animation_frames(
|
||||
sprite_frames: SpriteFrames,
|
||||
anim_name: String,
|
||||
frames: Array,
|
||||
texture,
|
||||
frame_rect: Variant,
|
||||
options: Dictionary,
|
||||
direction = 'forward',
|
||||
repeat = -1,
|
||||
frame_cache = {}
|
||||
):
|
||||
var animation_name := anim_name
|
||||
var is_loopable = _config.is_default_animation_loop_enabled()
|
||||
|
||||
var loop_prefix = _config.get_animation_loop_exception_prefix()
|
||||
if animation_name.begins_with(loop_prefix):
|
||||
animation_name = anim_name.trim_prefix(loop_prefix)
|
||||
is_loopable = not is_loopable
|
||||
|
||||
sprite_frames.add_animation(animation_name)
|
||||
|
||||
var min_duration = _get_min_duration(frames)
|
||||
var fps = _calculate_fps(min_duration, options.should_round_fps)
|
||||
|
||||
if direction == "reverse" or direction == "pingpong_reverse":
|
||||
frames.reverse()
|
||||
|
||||
var repetition = 1
|
||||
|
||||
if repeat != -1:
|
||||
is_loopable = false
|
||||
repetition = repeat
|
||||
|
||||
for i in range(repetition):
|
||||
for frame in frames:
|
||||
_add_to_sprite_frames(sprite_frames, animation_name, texture, frame, min_duration, frame_cache, frame_rect)
|
||||
|
||||
if direction.begins_with("pingpong"):
|
||||
var working_frames = frames.duplicate()
|
||||
working_frames.remove_at(working_frames.size() - 1)
|
||||
if is_loopable or (repetition > 1 and i < repetition - 1):
|
||||
working_frames.remove_at(0)
|
||||
working_frames.reverse()
|
||||
|
||||
for frame in working_frames:
|
||||
_add_to_sprite_frames(sprite_frames, animation_name, texture, frame, min_duration, frame_cache, frame_rect)
|
||||
|
||||
sprite_frames.set_animation_loop(animation_name, is_loopable)
|
||||
sprite_frames.set_animation_speed(animation_name, fps)
|
||||
|
||||
|
||||
func _calculate_fps(min_duration: int, should_round: bool) -> float:
|
||||
if should_round:
|
||||
return ceil(1000.0 / min_duration)
|
||||
return 1000.0 / min_duration
|
||||
|
||||
|
||||
func _get_min_duration(frames) -> int:
|
||||
var min_duration = 100000
|
||||
for frame in frames:
|
||||
if frame.duration < min_duration:
|
||||
min_duration = frame.duration
|
||||
return min_duration
|
||||
|
||||
|
||||
func _load_texture(path) -> CompressedTexture2D:
|
||||
ResourceLoader.load_threaded_request(path, "CompressedTexture2D", false, ResourceLoader.CACHE_MODE_REPLACE)
|
||||
return ResourceLoader.load_threaded_get(path)
|
||||
|
||||
|
||||
func create_packed_texture(sprite_sheet: String, save_path: String = "") -> PortableCompressedTexture2D:
|
||||
var tex := _load_compressed_texture(sprite_sheet)
|
||||
if tex == null:
|
||||
return null
|
||||
# if no path was provided, we just want the texture in memory
|
||||
if save_path == "":
|
||||
return tex
|
||||
|
||||
var exit_code = ResourceSaver.save(tex, save_path)
|
||||
if exit_code != OK:
|
||||
logger.error(result_code.get_error_message(result_code.ERR_ASEPRITE_EXPORT_FAILED), sprite_sheet)
|
||||
return null
|
||||
|
||||
# Use CACHE_MODE_REPLACE to ensure we load the freshly saved texture,
|
||||
# not a stale cached version from a previous import
|
||||
return ResourceLoader.load(save_path, "", ResourceLoader.CACHE_MODE_REPLACE)
|
||||
|
||||
|
||||
func _add_to_sprite_frames(
|
||||
sprite_frames,
|
||||
animation_name: String,
|
||||
texture,
|
||||
frame: Dictionary,
|
||||
min_duration: int,
|
||||
frame_cache: Dictionary,
|
||||
frame_rect: Variant,
|
||||
):
|
||||
var atlas : AtlasTexture = _create_atlastexture_from_frame(texture, frame, sprite_frames, frame_cache, frame_rect)
|
||||
var duration = frame.duration / min_duration
|
||||
sprite_frames.add_frame(animation_name, atlas, duration)
|
||||
|
||||
|
||||
func _create_atlastexture_from_frame(
|
||||
image,
|
||||
frame_data,
|
||||
sprite_frames: SpriteFrames,
|
||||
frame_cache: Dictionary,
|
||||
frame_rect: Variant,
|
||||
) -> AtlasTexture:
|
||||
var frame = frame_data.frame
|
||||
var region := Rect2(frame.x, frame.y, frame.w, frame.h)
|
||||
|
||||
# this is to manually set the slice
|
||||
if frame_rect != null:
|
||||
region.position.x += frame_rect.position.x
|
||||
region.position.y += frame_rect.position.y
|
||||
region.size.x = frame_rect.size.x
|
||||
region.size.y = frame_rect.size.y
|
||||
|
||||
var key := "%s_%s_%s_%s" % [frame.x, frame.y, frame.w, frame.h]
|
||||
var texture = frame_cache.get(key)
|
||||
|
||||
if texture != null and texture.atlas == image:
|
||||
return texture
|
||||
|
||||
var atlas_texture := AtlasTexture.new()
|
||||
atlas_texture.atlas = image
|
||||
atlas_texture.region = region
|
||||
|
||||
frame_cache[key] = atlas_texture
|
||||
|
||||
return atlas_texture
|
||||
|
||||
|
||||
func list_layers(file: String, only_visibles = false) -> Array:
|
||||
return _aseprite.list_layers(file, only_visibles)
|
||||
|
||||
|
||||
func list_slices(file: String) -> Array:
|
||||
return _aseprite.list_slices(file)
|
||||
|
||||
|
||||
func _get_file_basename(file_path: String) -> String:
|
||||
return file_path.get_file().trim_suffix('.%s' % file_path.get_extension())
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://b086vre035ff2
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
@tool
|
||||
extends "../base_sprite_resource_creator.gd"
|
||||
|
||||
func load_texture(target_node: Node, aseprite_files: Dictionary, options: Dictionary) -> void:
|
||||
var source_file = aseprite_files.data_file
|
||||
var sprite_sheet = aseprite_files.sprite_sheet
|
||||
var data = _aseprite_file_exporter.load_json_content(source_file)
|
||||
var texture
|
||||
|
||||
if options.get("should_create_portable_texture", false):
|
||||
texture = _load_compressed_texture(sprite_sheet)
|
||||
else:
|
||||
texture = ResourceLoader.load(sprite_sheet)
|
||||
|
||||
if not data.is_ok:
|
||||
logger.error("Failed to load aseprite source", source_file)
|
||||
return
|
||||
|
||||
if options.slice == "":
|
||||
target_node.texture = texture
|
||||
else:
|
||||
var region = _aseprite.get_slice_rect(data.content, options.slice)
|
||||
var atlas_texture := AtlasTexture.new()
|
||||
atlas_texture.atlas = texture
|
||||
atlas_texture.region = region
|
||||
target_node.texture = atlas_texture
|
||||
|
||||
if target_node is CanvasItem:
|
||||
target_node.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
|
||||
else:
|
||||
target_node.texture_filter = BaseMaterial3D.TEXTURE_FILTER_NEAREST
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://l5x70eacca0s
|
||||
Loading…
Add table
Add a link
Reference in a new issue