Browse Source

Merge pull request #37253 from KoBeWi/animass

Allow to load multiple animations at once
Max Hilbrunner 4 years ago
parent
commit
b86a1cc248

+ 38 - 35
editor/plugins/animation_player_editor_plugin.cpp

@@ -345,7 +345,7 @@ void AnimationPlayerEditor::_animation_rename() {
 
 void AnimationPlayerEditor::_animation_load() {
 	ERR_FAIL_COND(!player);
-	file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
+	file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
 	file->clear_filters();
 	List<String> extensions;
 
@@ -355,7 +355,6 @@ void AnimationPlayerEditor::_animation_load() {
 	}
 
 	file->popup_file_dialog();
-	current_option = RESOURCE_LOAD;
 }
 
 void AnimationPlayerEditor::_animation_save_in_path(const Ref<Resource> &p_resource, const String &p_path) {
@@ -416,7 +415,6 @@ void AnimationPlayerEditor::_animation_save_as(const Ref<Resource> &p_resource)
 	file->set_current_path(path);
 	file->set_title(TTR("Save Resource As..."));
 	file->popup_file_dialog();
-	current_option = RESOURCE_SAVE;
 }
 
 void AnimationPlayerEditor::_animation_remove() {
@@ -718,44 +716,48 @@ void AnimationPlayerEditor::_animation_edit() {
 	}
 }
 
-void AnimationPlayerEditor::_dialog_action(String p_path) {
-	switch (current_option) {
-		case RESOURCE_LOAD: {
-			ERR_FAIL_COND(!player);
+void AnimationPlayerEditor::_save_animation(String p_file) {
+	String current = animation->get_item_text(animation->get_selected());
+	if (current != "") {
+		Ref<Animation> anim = player->get_animation(current);
 
-			Ref<Resource> res = ResourceLoader::load(p_path, "Animation");
-			ERR_FAIL_COND_MSG(res.is_null(), "Cannot load Animation from file '" + p_path + "'.");
-			ERR_FAIL_COND_MSG(!res->is_class("Animation"), "Loaded resource from file '" + p_path + "' is not Animation.");
+		ERR_FAIL_COND(!Object::cast_to<Resource>(*anim));
 
-			String anim_name = p_path.get_file();
-			int ext_pos = anim_name.rfind(".");
-			if (ext_pos != -1) {
-				anim_name = anim_name.substr(0, ext_pos);
-			}
+		RES current_res = RES(Object::cast_to<Resource>(*anim));
 
-			undo_redo->create_action(TTR("Load Animation"));
-			undo_redo->add_do_method(player, "add_animation", anim_name, res);
-			undo_redo->add_undo_method(player, "remove_animation", anim_name);
-			if (player->has_animation(anim_name)) {
-				undo_redo->add_undo_method(player, "add_animation", anim_name, player->get_animation(anim_name));
-			}
-			undo_redo->add_do_method(this, "_animation_player_changed", player);
-			undo_redo->add_undo_method(this, "_animation_player_changed", player);
-			undo_redo->commit_action();
-			break;
-		}
-		case RESOURCE_SAVE: {
-			String current = animation->get_item_text(animation->get_selected());
-			if (current != "") {
-				Ref<Animation> anim = player->get_animation(current);
+		_animation_save_in_path(current_res, p_file);
+	}
+}
 
-				ERR_FAIL_COND(!Object::cast_to<Resource>(*anim));
+void AnimationPlayerEditor::_load_animations(Vector<String> p_files) {
+	ERR_FAIL_COND(!player);
 
-				RES current_res = RES(Object::cast_to<Resource>(*anim));
+	for (int i = 0; i < p_files.size(); i++) {
+		String file = p_files[i];
 
-				_animation_save_in_path(current_res, p_path);
-			}
+		Ref<Resource> res = ResourceLoader::load(file, "Animation");
+		ERR_FAIL_COND_MSG(res.is_null(), "Cannot load Animation from file '" + file + "'.");
+		ERR_FAIL_COND_MSG(!res->is_class("Animation"), "Loaded resource from file '" + file + "' is not Animation.");
+		if (file.rfind("/") != -1) {
+			file = file.substr(file.rfind("/") + 1, file.length());
+		}
+		if (file.rfind("\\") != -1) {
+			file = file.substr(file.rfind("\\") + 1, file.length());
+		}
+
+		if (file.find(".") != -1) {
+			file = file.substr(0, file.find("."));
 		}
+
+		undo_redo->create_action(TTR("Load Animation"));
+		undo_redo->add_do_method(player, "add_animation", file, res);
+		undo_redo->add_undo_method(player, "remove_animation", file);
+		if (player->has_animation(file)) {
+			undo_redo->add_undo_method(player, "add_animation", file, player->get_animation(file));
+		}
+		undo_redo->add_do_method(this, "_animation_player_changed", player);
+		undo_redo->add_undo_method(this, "_animation_player_changed", player);
+		undo_redo->commit_action();
 	}
 }
 
@@ -1695,7 +1697,8 @@ AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor, AnimationPlay
 
 	animation->connect("item_selected", callable_mp(this, &AnimationPlayerEditor::_animation_selected));
 
-	file->connect("file_selected", callable_mp(this, &AnimationPlayerEditor::_dialog_action));
+	file->connect("file_selected", callable_mp(this, &AnimationPlayerEditor::_save_animation));
+	file->connect("files_selected", callable_mp(this, &AnimationPlayerEditor::_load_animations));
 	frame->connect("value_changed", callable_mp(this, &AnimationPlayerEditor::_seek_value_changed), make_binds(true, false));
 	scale->connect("text_submitted", callable_mp(this, &AnimationPlayerEditor::_scale_changed));
 

+ 2 - 2
editor/plugins/animation_player_editor_plugin.h

@@ -112,7 +112,6 @@ class AnimationPlayerEditor : public VBoxContainer {
 
 	EditorFileDialog *file;
 	ConfirmationDialog *delete_dialog;
-	int current_option;
 
 	struct BlendEditor {
 		AcceptDialog *dialog = nullptr;
@@ -185,7 +184,8 @@ class AnimationPlayerEditor : public VBoxContainer {
 	void _animation_duplicate();
 	void _animation_resource_edit();
 	void _scale_changed(const String &p_scale);
-	void _dialog_action(String p_file);
+	void _save_animation(String p_file);
+	void _load_animations(Vector<String> p_files);
 	void _seek_frame_changed(const String &p_frame);
 	void _seek_value_changed(float p_value, bool p_set = false, bool p_timeline_only = false);
 	void _blend_editor_next_changed(const int p_idx);