Path: blob/master/editor/animation/animation_tree_editor_plugin.cpp
9896 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/editor_node.h"37#include "editor/gui/editor_bottom_panel.h"38#include "editor/settings/editor_command_palette.h"39#include "editor/themes/editor_scale.h"40#include "scene/animation/animation_blend_tree.h"41#include "scene/gui/button.h"42#include "scene/gui/margin_container.h"43#include "scene/gui/scroll_container.h"44#include "scene/gui/separator.h"4546void AnimationTreeEditor::edit(AnimationTree *p_tree) {47if (p_tree && !p_tree->is_connected("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed))) {48p_tree->connect("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed), CONNECT_DEFERRED);49}5051if (tree == p_tree) {52return;53}5455if (tree && tree->is_connected("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed))) {56tree->disconnect("animation_list_changed", callable_mp(this, &AnimationTreeEditor::_animation_list_changed));57}5859tree = p_tree;6061Vector<String> path;62if (tree) {63edit_path(path);64}65}6667void AnimationTreeEditor::_node_removed(Node *p_node) {68if (p_node == tree) {69tree = nullptr;70_clear_editors();71}72}7374void AnimationTreeEditor::_path_button_pressed(int p_path) {75edited_path.clear();76for (int i = 0; i <= p_path; i++) {77edited_path.push_back(button_path[i]);78}79}8081void AnimationTreeEditor::_animation_list_changed() {82AnimationNodeBlendTreeEditor *bte = AnimationNodeBlendTreeEditor::get_singleton();83if (bte) {84bte->update_graph();85}86}8788void AnimationTreeEditor::_update_path() {89while (path_hb->get_child_count() > 1) {90memdelete(path_hb->get_child(1));91}9293Ref<ButtonGroup> group;94group.instantiate();9596Button *b = memnew(Button);97b->set_text(TTR("Root"));98b->set_toggle_mode(true);99b->set_button_group(group);100b->set_pressed(true);101b->set_focus_mode(FOCUS_ACCESSIBILITY);102b->connect(SceneStringName(pressed), callable_mp(this, &AnimationTreeEditor::_path_button_pressed).bind(-1));103path_hb->add_child(b);104for (int i = 0; i < button_path.size(); i++) {105b = memnew(Button);106b->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);107b->set_text(button_path[i]);108b->set_toggle_mode(true);109b->set_button_group(group);110path_hb->add_child(b);111b->set_pressed(true);112b->set_focus_mode(FOCUS_ACCESSIBILITY);113b->connect(SceneStringName(pressed), callable_mp(this, &AnimationTreeEditor::_path_button_pressed).bind(i));114}115}116117void AnimationTreeEditor::edit_path(const Vector<String> &p_path) {118button_path.clear();119120Ref<AnimationNode> node = tree->get_root_animation_node();121122if (node.is_valid()) {123current_root = node->get_instance_id();124125for (int i = 0; i < p_path.size(); i++) {126Ref<AnimationNode> child = node->get_child_by_name(p_path[i]);127ERR_BREAK(child.is_null());128node = child;129button_path.push_back(p_path[i]);130}131132edited_path = button_path;133134for (int i = 0; i < editors.size(); i++) {135if (editors[i]->can_edit(node)) {136editors[i]->edit(node);137editors[i]->show();138} else {139editors[i]->edit(Ref<AnimationNode>());140editors[i]->hide();141}142}143} else {144current_root = ObjectID();145edited_path = button_path;146for (int i = 0; i < editors.size(); i++) {147editors[i]->edit(Ref<AnimationNode>());148editors[i]->hide();149}150}151152_update_path();153}154155void AnimationTreeEditor::_clear_editors() {156button_path.clear();157current_root = ObjectID();158edited_path = button_path;159for (int i = 0; i < editors.size(); i++) {160editors[i]->edit(Ref<AnimationNode>());161editors[i]->hide();162}163_update_path();164}165166Vector<String> AnimationTreeEditor::get_edited_path() const {167return button_path;168}169170void AnimationTreeEditor::enter_editor(const String &p_path) {171Vector<String> path = edited_path;172path.push_back(p_path);173edit_path(path);174}175176void AnimationTreeEditor::_notification(int p_what) {177switch (p_what) {178case NOTIFICATION_ENTER_TREE: {179get_tree()->connect("node_removed", callable_mp(this, &AnimationTreeEditor::_node_removed));180} break;181case NOTIFICATION_PROCESS: {182ObjectID root;183if (tree && tree->get_root_animation_node().is_valid()) {184root = tree->get_root_animation_node()->get_instance_id();185}186187if (root != current_root) {188edit_path(Vector<String>());189}190191if (button_path.size() != edited_path.size()) {192edit_path(edited_path);193}194} break;195case NOTIFICATION_EXIT_TREE: {196get_tree()->disconnect("node_removed", callable_mp(this, &AnimationTreeEditor::_node_removed));197} break;198}199}200201AnimationTreeEditor *AnimationTreeEditor::singleton = nullptr;202203void AnimationTreeEditor::add_plugin(AnimationTreeNodeEditorPlugin *p_editor) {204ERR_FAIL_COND(p_editor->get_parent());205editor_base->add_child(p_editor);206editors.push_back(p_editor);207p_editor->set_h_size_flags(SIZE_EXPAND_FILL);208p_editor->set_v_size_flags(SIZE_EXPAND_FILL);209p_editor->hide();210}211212void AnimationTreeEditor::remove_plugin(AnimationTreeNodeEditorPlugin *p_editor) {213ERR_FAIL_COND(p_editor->get_parent() != editor_base);214editor_base->remove_child(p_editor);215editors.erase(p_editor);216}217218String AnimationTreeEditor::get_base_path() {219String path = Animation::PARAMETERS_BASE_PATH;220for (int i = 0; i < edited_path.size(); i++) {221path += edited_path[i] + "/";222}223return path;224}225226bool AnimationTreeEditor::can_edit(const Ref<AnimationNode> &p_node) const {227for (int i = 0; i < editors.size(); i++) {228if (editors[i]->can_edit(p_node)) {229return true;230}231}232return false;233}234235Vector<String> AnimationTreeEditor::get_animation_list() {236// This can be called off the main thread due to resource preview generation. Quit early in that case.237if (!singleton->tree || !Thread::is_main_thread() || !singleton->is_visible()) {238// When tree is empty, singleton not in the main thread.239return Vector<String>();240}241242AnimationTree *tree = singleton->tree;243if (!tree) {244return Vector<String>();245}246247List<StringName> anims;248tree->get_animation_list(&anims);249Vector<String> ret;250for (const StringName &E : anims) {251ret.push_back(E);252}253254return ret;255}256257AnimationTreeEditor::AnimationTreeEditor() {258AnimationNodeAnimation::get_editable_animation_list = get_animation_list;259path_edit = memnew(ScrollContainer);260add_child(path_edit);261path_edit->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);262path_hb = memnew(HBoxContainer);263path_edit->add_child(path_hb);264path_hb->add_child(memnew(Label(TTR("Path:"))));265266add_child(memnew(HSeparator));267268singleton = this;269editor_base = memnew(MarginContainer);270editor_base->set_v_size_flags(SIZE_EXPAND_FILL);271add_child(editor_base);272273add_plugin(memnew(AnimationNodeBlendTreeEditor));274add_plugin(memnew(AnimationNodeBlendSpace1DEditor));275add_plugin(memnew(AnimationNodeBlendSpace2DEditor));276add_plugin(memnew(AnimationNodeStateMachineEditor));277}278279void AnimationTreeEditorPlugin::edit(Object *p_object) {280anim_tree_editor->edit(Object::cast_to<AnimationTree>(p_object));281}282283bool AnimationTreeEditorPlugin::handles(Object *p_object) const {284return p_object->is_class("AnimationTree");285}286287void AnimationTreeEditorPlugin::make_visible(bool p_visible) {288if (p_visible) {289//editor->hide_animation_player_editors();290//editor->animation_panel_make_visible(true);291button->show();292EditorNode::get_bottom_panel()->make_item_visible(anim_tree_editor);293anim_tree_editor->set_process(true);294} else {295if (anim_tree_editor->is_visible_in_tree()) {296EditorNode::get_bottom_panel()->hide_bottom_panel();297}298button->hide();299anim_tree_editor->set_process(false);300}301}302303AnimationTreeEditorPlugin::AnimationTreeEditorPlugin() {304anim_tree_editor = memnew(AnimationTreeEditor);305anim_tree_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);306307button = EditorNode::get_bottom_panel()->add_item(TTRC("AnimationTree"), anim_tree_editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_animation_tree_bottom_panel", TTRC("Toggle AnimationTree Bottom Panel")));308button->hide();309}310311312