From 53f274de2fb791c1373c5bc2a17efa7c2eef7462 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 7 Aug 2022 16:20:57 +0200 Subject: [PATCH] Ported: AnimatedSprite Fix updating inspector when SpriteFrames is modified - kleonc https://github.com/godotengine/godot/commit/8ae246f15efcca2dc72bfbad99373b942c6e6482 --- editor/editor_inspector.cpp | 21 +++++---- .../plugins/sprite_frames_editor_plugin.cpp | 44 +++++++++++++++++-- scene/2d/animated_sprite.cpp | 26 +++++------ scene/2d/animated_sprite.h | 2 - 4 files changed, 62 insertions(+), 31 deletions(-) diff --git a/editor/editor_inspector.cpp b/editor/editor_inspector.cpp index eeff112ea..f1a232373 100644 --- a/editor/editor_inspector.cpp +++ b/editor/editor_inspector.cpp @@ -30,15 +30,6 @@ #include "editor_inspector.h" -#include "core/os/input.h" -#include "core/os/keyboard.h" -#include "editor_node.h" -#include "editor_property_name_processor.h" -#include "editor_scale.h" -#include "editor_settings.h" -#include "multi_node_edit.h" -#include "scene/property_utils.h" -#include "scene/resources/packed_scene.h" #include "core/array.h" #include "core/class_db.h" #include "core/dictionary.h" @@ -46,7 +37,9 @@ #include "core/hash_map.h" #include "core/math/math_defs.h" #include "core/math/math_funcs.h" +#include "core/os/input.h" #include "core/os/input_event.h" +#include "core/os/keyboard.h" #include "core/os/memory.h" #include "core/os/os.h" #include "core/script_language.h" @@ -55,6 +48,11 @@ #include "editor/doc/doc_data.h" #include "editor/editor_data.h" #include "editor/editor_help.h" +#include "editor_node.h" +#include "editor_property_name_processor.h" +#include "editor_scale.h" +#include "editor_settings.h" +#include "multi_node_edit.h" #include "scene/2d/canvas_item.h" #include "scene/gui/box_container.h" #include "scene/gui/label.h" @@ -64,7 +62,9 @@ #include "scene/gui/scroll_bar.h" #include "scene/main/node.h" #include "scene/main/scene_tree.h" +#include "scene/property_utils.h" #include "scene/resources/font.h" +#include "scene/resources/packed_scene.h" #include "scene/resources/style_box.h" #include "servers/visual_server.h" @@ -1437,8 +1437,7 @@ void EditorInspector::update_tree() { String group_base; VBoxContainer *category_vbox = nullptr; - List - plist; + List plist; object->get_property_list(&plist, true); HashMap item_path; diff --git a/editor/plugins/sprite_frames_editor_plugin.cpp b/editor/plugins/sprite_frames_editor_plugin.cpp index 67dbf69a1..ce48635b3 100644 --- a/editor/plugins/sprite_frames_editor_plugin.cpp +++ b/editor/plugins/sprite_frames_editor_plugin.cpp @@ -53,6 +53,7 @@ #include "editor/editor_node.h" #include "editor/editor_scale.h" #include "editor/editor_settings.h" +#include "editor/editor_inspector.h" #include "scene/2d/animated_sprite.h" #include "scene/3d/sprite_3d.h" #include "scene/gui/box_container.h" @@ -769,9 +770,12 @@ void SpriteFramesEditor::_animation_name_edited() { undo_redo->add_undo_method(frames, "rename_animation", name, edited_anim); for (List::Element *E = nodes.front(); E; E = E->next()) { - String current = E->get()->call("get_animation"); + StringName current = E->get()->call("get_animation"); + if (current != edited_anim) { + continue; + } undo_redo->add_do_method(E->get(), "set_animation", name); - undo_redo->add_undo_method(E->get(), "set_animation", edited_anim); + undo_redo->add_undo_method(E->get(), "set_animation", current); } undo_redo->add_do_method(this, "_update_library"); @@ -799,8 +803,14 @@ void SpriteFramesEditor::_animation_add() { undo_redo->add_do_method(this, "_update_library"); undo_redo->add_undo_method(this, "_update_library"); + // Assign the newly added animation to the edited anim sprite and to all other anim sprites having invalid animation. + Object *edited_anim_sprite = EditorNode::get_singleton()->get_inspector()->get_edited_object(); for (List::Element *E = nodes.front(); E; E = E->next()) { - String current = E->get()->call("get_animation"); + StringName current = E->get()->call("get_animation"); + if (frames->has_animation(current) && E->get() != edited_anim_sprite) { + continue; + } + undo_redo->add_do_method(E->get(), "set_animation", name); undo_redo->add_undo_method(E->get(), "set_animation", current); } @@ -830,15 +840,41 @@ void SpriteFramesEditor::_animation_remove_confirmed() { undo_redo->add_undo_method(frames, "add_animation", edited_anim); undo_redo->add_undo_method(frames, "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim)); undo_redo->add_undo_method(frames, "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim)); + int fc = frames->get_frame_count(edited_anim); for (int i = 0; i < fc; i++) { Ref frame = frames->get_frame(edited_anim, i); undo_redo->add_undo_method(frames, "add_frame", edited_anim, frame); } + + StringName new_edited_anim = StringName(); + + List anim_names; + frames->get_animation_list(&anim_names); + anim_names.sort_custom(); + + // If removing not the last animation, make the first animation left the new edited one. + if (anim_names.size() > 1) { + new_edited_anim = edited_anim != anim_names.front()->get() ? anim_names.front()->get() : anim_names.front()->next()->get(); + + List nodes; + _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref(frames)); + + for (List::Element *E = nodes.front(); E; E = E->next()) { + StringName current = E->get()->call("get_animation"); + if (current != edited_anim) { + continue; + } + + undo_redo->add_do_method(E->get(), "set_animation", new_edited_anim); + undo_redo->add_undo_method(E->get(), "set_animation", current); + } + } + undo_redo->add_do_method(this, "_update_library"); undo_redo->add_undo_method(this, "_update_library"); - edited_anim = StringName(); + edited_anim = new_edited_anim; undo_redo->commit_action(); } diff --git a/scene/2d/animated_sprite.cpp b/scene/2d/animated_sprite.cpp index 73ac254f1..cb5ee85b8 100644 --- a/scene/2d/animated_sprite.cpp +++ b/scene/2d/animated_sprite.cpp @@ -148,7 +148,7 @@ void SpriteFrames::clear(const StringName &p_anim) { void SpriteFrames::clear_all() { animations.clear(); - add_animation("default"); + add_animation("default"); // Also emits changed. } void SpriteFrames::add_animation(const StringName &p_anim) { @@ -156,13 +156,16 @@ void SpriteFrames::add_animation(const StringName &p_anim) { animations[p_anim] = Anim(); animations[p_anim].normal_name = String(p_anim) + NORMAL_SUFFIX; + emit_changed(); } bool SpriteFrames::has_animation(const StringName &p_anim) const { return animations.has(p_anim); } void SpriteFrames::remove_animation(const StringName &p_anim) { - animations.erase(p_anim); + if (animations.erase(p_anim)) { + emit_changed(); + } } void SpriteFrames::rename_animation(const StringName &p_prev, const StringName &p_next) { @@ -173,17 +176,8 @@ void SpriteFrames::rename_animation(const StringName &p_prev, const StringName & animations.erase(p_prev); animations[p_next] = anim; animations[p_next].normal_name = String(p_next) + NORMAL_SUFFIX; -} -Vector SpriteFrames::_get_animation_list() const { - Vector ret; - List al; - get_animation_list(&al); - for (List::Element *E = al.front(); E; E = E->next()) { - ret.push_back(E->get()); - } - - return ret; + emit_changed(); } void SpriteFrames::get_animation_list(List *r_animations) const { @@ -596,8 +590,12 @@ bool AnimatedSprite::is_flipped_v() const { void AnimatedSprite::_res_changed() { set_frame(frame); - _change_notify("frame"); - _change_notify("animation"); + + // Calling _change_notify("frame") and _change_notify("animation") instead wouldn't + // make EditorInspector trigger calls to _validate_property(property) which would + // lead to not updating valid values for "frame" and "animation" properties. + _change_notify(); + update(); } diff --git a/scene/2d/animated_sprite.h b/scene/2d/animated_sprite.h index 592492248..3b4839bfe 100644 --- a/scene/2d/animated_sprite.h +++ b/scene/2d/animated_sprite.h @@ -58,8 +58,6 @@ class SpriteFrames : public Resource { Array _get_animations() const; void _set_animations(const Array &p_animations); - Vector _get_animation_list() const; - protected: static void _bind_methods();