Path: blob/master/editor/animation/animation_tree_editor_plugin.cpp
21016 views
/**************************************************************************/1/* animation_tree_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "animation_tree_editor_plugin.h"3132#include "animation_blend_space_1d_editor.h"33#include "animation_blend_space_2d_editor.h"34#include "animation_blend_tree_editor_plugin.h"35#include "animation_state_machine_editor.h"36#include "editor/docks/editor_dock_manager.h"37#include "editor/editor_node.h"38#include "editor/gui/editor_bottom_panel.h"39#include "editor/settings/editor_command_palette.h"40#include "editor/themes/editor_scale.h"41#include "scene/animation/animation_blend_tree.h"42#include "scene/gui/button.h"43#include "scene/gui/margin_container.h"44#include "scene/gui/scroll_container.h"45#include "scene/gui/separator.h"4647void AnimationTreeEditor::edit(AnimationTree *p_tree) {48if (p_tree && !p_tree->is_connected("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed))) {49p_tree->connect("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed), CONNECT_DEFERRED);50}5152if (tree == p_tree) {53return;54}5556if (tree && tree->is_connected("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed))) {57tree->disconnect("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed));58}5960tree = p_tree;6162Vector<String> path;63if (tree) {64edit_path(path);65}66}6768void AnimationTreeEditor::_node_removed(Node *p_node) {69if (p_node == tree) {70tree = nullptr;71_clear_editors();72}73}7475void AnimationTreeEditor::_path_button_pressed(int p_path) {76edited_path.clear();77for (int i = 0; i <= p_path; i++) {78edited_path.push_back(button_path[i]);79}80}8182void AnimationTreeEditor::_animation_list_changed() {83AnimationNodeBlendTreeEditor *bte = AnimationNodeBlendTreeEditor::get_singleton();84if (bte) {85bte->update_graph();86}87}8889void AnimationTreeEditor::_update_path() {90while (path_hb->get_child_count() > 1) {91memdelete(path_hb->get_child(1));92}9394Ref<ButtonGroup> group;95group.instantiate();9697Button *b = memnew(Button);98b->set_text(TTR("Root"));99b->set_toggle_mode(true);100b->set_button_group(group);101b->set_pressed(true);102b->set_focus_mode(FOCUS_ACCESSIBILITY);103b->connect(SceneStringName(pressed), callable_mp(this, &AnimationTreeEditor::_path_button_pressed).bind(-1));104path_hb->add_child(b);105for (int i = 0; i < button_path.size(); i++) {106b = memnew(Button);107b->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);108b->set_text(button_path[i]);109b->set_toggle_mode(true);110b->set_button_group(group);111path_hb->add_child(b);112b->set_pressed(true);113b->set_focus_mode(FOCUS_ACCESSIBILITY);114b->connect(SceneStringName(pressed), callable_mp(this, &AnimationTreeEditor::_path_button_pressed).bind(i));115}116}117118void AnimationTreeEditor::edit_path(const Vector<String> &p_path) {119button_path.clear();120121Ref<AnimationNode> node = tree->get_root_animation_node();122123if (node.is_valid()) {124current_root = node->get_instance_id();125126for (int i = 0; i < p_path.size(); i++) {127Ref<AnimationNode> child = node->get_child_by_name(p_path[i]);128ERR_BREAK(child.is_null());129node = child;130button_path.push_back(p_path[i]);131}132133edited_path = button_path;134135for (int i = 0; i < editors.size(); i++) {136if (editors[i]->can_edit(node)) {137editors[i]->edit(node);138editors[i]->show();139} else {140editors[i]->edit(Ref<AnimationNode>());141editors[i]->hide();142}143}144} else {145current_root = ObjectID();146edited_path = button_path;147for (int i = 0; i < editors.size(); i++) {148editors[i]->edit(Ref<AnimationNode>());149editors[i]->hide();150}151}152153_update_path();154}155156void AnimationTreeEditor::_clear_editors() {157button_path.clear();158current_root = ObjectID();159edited_path = button_path;160for (int i = 0; i < editors.size(); i++) {161editors[i]->edit(Ref<AnimationNode>());162editors[i]->hide();163}164_update_path();165}166167Vector<String> AnimationTreeEditor::get_edited_path() const {168return button_path;169}170171void AnimationTreeEditor::enter_editor(const String &p_path) {172Vector<String> path = edited_path;173path.push_back(p_path);174edit_path(path);175}176177void AnimationTreeEditor::_notification(int p_what) {178switch (p_what) {179case NOTIFICATION_PROCESS: {180ObjectID root;181if (tree && tree->get_root_animation_node().is_valid()) {182root = tree->get_root_animation_node()->get_instance_id();183}184185if (root != current_root) {186edit_path(Vector<String>());187}188189if (button_path.size() != edited_path.size()) {190edit_path(edited_path);191}192} break;193194case NOTIFICATION_ENTER_TREE: {195get_tree()->connect("node_removed", callable_mp(this, &AnimationTreeEditor::_node_removed));196} break;197198case NOTIFICATION_EXIT_TREE: {199get_tree()->disconnect("node_removed", callable_mp(this, &AnimationTreeEditor::_node_removed));200} break;201}202}203204AnimationTreeEditor *AnimationTreeEditor::singleton = nullptr;205206void AnimationTreeEditor::add_plugin(AnimationTreeNodeEditorPlugin *p_editor) {207ERR_FAIL_COND(p_editor->get_parent());208editor_base->add_child(p_editor);209editors.push_back(p_editor);210p_editor->set_h_size_flags(SIZE_EXPAND_FILL);211p_editor->set_v_size_flags(SIZE_EXPAND_FILL);212p_editor->hide();213}214215void AnimationTreeEditor::remove_plugin(AnimationTreeNodeEditorPlugin *p_editor) {216ERR_FAIL_COND(p_editor->get_parent() != editor_base);217editor_base->remove_child(p_editor);218editors.erase(p_editor);219}220221String AnimationTreeEditor::get_base_path() {222String path = Animation::PARAMETERS_BASE_PATH;223for (int i = 0; i < edited_path.size(); i++) {224path += edited_path[i] + "/";225}226return path;227}228229bool AnimationTreeEditor::can_edit(const Ref<AnimationNode> &p_node) const {230for (int i = 0; i < editors.size(); i++) {231if (editors[i]->can_edit(p_node)) {232return true;233}234}235return false;236}237238Vector<String> AnimationTreeEditor::get_animation_list() {239// This can be called off the main thread due to resource preview generation. Quit early in that case.240if (!singleton->tree || !Thread::is_main_thread() || !singleton->is_visible()) {241// When tree is empty, singleton not in the main thread.242return Vector<String>();243}244245AnimationTree *tree = singleton->tree;246if (!tree) {247return Vector<String>();248}249250List<StringName> anims;251tree->get_animation_list(&anims);252Vector<String> ret;253for (const StringName &E : anims) {254ret.push_back(E);255}256257return ret;258}259260AnimationTreeEditor::AnimationTreeEditor() {261singleton = this;262AnimationNodeAnimation::get_editable_animation_list = get_animation_list;263264set_name(TTRC("AnimationTree"));265set_icon_name("AnimationTreeDock");266set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_animation_tree_bottom_panel", TTRC("Toggle AnimationTree Dock")));267set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);268set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL | EditorDock::DOCK_LAYOUT_FLOATING);269set_global(false);270set_transient(true);271272VBoxContainer *main_vbox_container = memnew(VBoxContainer);273add_child(main_vbox_container);274275path_edit = memnew(ScrollContainer);276path_edit->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);277main_vbox_container->add_child(path_edit);278279path_hb = memnew(HBoxContainer);280path_hb->add_child(memnew(Label(TTR("Path:"))));281path_edit->add_child(path_hb);282283main_vbox_container->add_child(memnew(HSeparator));284285editor_base = memnew(MarginContainer);286editor_base->set_v_size_flags(SIZE_EXPAND_FILL);287main_vbox_container->add_child(editor_base);288289add_plugin(memnew(AnimationNodeBlendTreeEditor));290add_plugin(memnew(AnimationNodeBlendSpace1DEditor));291add_plugin(memnew(AnimationNodeBlendSpace2DEditor));292add_plugin(memnew(AnimationNodeStateMachineEditor));293}294295void AnimationTreeEditorPlugin::edit(Object *p_object) {296anim_tree_editor->edit(Object::cast_to<AnimationTree>(p_object));297}298299bool AnimationTreeEditorPlugin::handles(Object *p_object) const {300return p_object->is_class("AnimationTree");301}302303void AnimationTreeEditorPlugin::make_visible(bool p_visible) {304if (p_visible) {305anim_tree_editor->make_visible();306} else {307anim_tree_editor->close();308}309310anim_tree_editor->set_process(p_visible);311}312313AnimationTreeEditorPlugin::AnimationTreeEditorPlugin() {314anim_tree_editor = memnew(AnimationTreeEditor);315anim_tree_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);316EditorDockManager::get_singleton()->add_dock(anim_tree_editor);317anim_tree_editor->close();318}319320321