2018-08-30 04:38:13 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* root_motion_editor_plugin.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2018-06-27 06:05:11 +08:00
|
|
|
#include "root_motion_editor_plugin.h"
|
2023-07-20 23:34:06 +08:00
|
|
|
|
2018-06-27 06:05:11 +08:00
|
|
|
#include "editor/editor_node.h"
|
2024-01-15 20:14:55 +08:00
|
|
|
#include "editor/themes/editor_scale.h"
|
2024-01-06 01:21:38 +08:00
|
|
|
#include "scene/3d/skeleton_3d.h"
|
2023-07-20 23:34:06 +08:00
|
|
|
#include "scene/animation/animation_mixer.h"
|
2023-04-08 00:59:49 +08:00
|
|
|
#include "scene/gui/button.h"
|
|
|
|
#include "scene/gui/dialogs.h"
|
2022-11-19 19:45:49 +08:00
|
|
|
#include "scene/gui/tree.h"
|
2020-03-04 09:51:12 +08:00
|
|
|
#include "scene/main/window.h"
|
2018-06-27 06:05:11 +08:00
|
|
|
|
|
|
|
void EditorPropertyRootMotion::_confirmed() {
|
|
|
|
TreeItem *ti = filters->get_selected();
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!ti) {
|
2018-06-27 06:05:11 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-06-27 06:05:11 +08:00
|
|
|
|
|
|
|
NodePath path = ti->get_metadata(0);
|
2019-01-19 00:01:24 +08:00
|
|
|
emit_changed(get_edited_property(), path);
|
2018-06-27 06:05:11 +08:00
|
|
|
update_property();
|
|
|
|
filter_dialog->hide(); //may come from activated
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyRootMotion::_node_assign() {
|
2023-07-20 23:34:06 +08:00
|
|
|
AnimationMixer *mixer = Object::cast_to<AnimationMixer>(get_edited_object());
|
|
|
|
if (!mixer) {
|
|
|
|
EditorNode::get_singleton()->show_warning(TTR("Path to AnimationMixer is invalid"));
|
2018-06-27 06:05:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:34:06 +08:00
|
|
|
Node *base = mixer->get_node(mixer->get_root_node());
|
2018-06-27 06:05:11 +08:00
|
|
|
|
|
|
|
if (!base) {
|
2023-07-20 23:34:06 +08:00
|
|
|
EditorNode::get_singleton()->show_warning(TTR("AnimationMixer has no valid root node path, so unable to retrieve track names."));
|
2018-06-27 06:05:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-19 23:00:06 +08:00
|
|
|
HashSet<String> paths;
|
2018-06-27 06:05:11 +08:00
|
|
|
{
|
|
|
|
List<StringName> animations;
|
2023-07-20 23:34:06 +08:00
|
|
|
mixer->get_animation_list(&animations);
|
2018-06-27 06:05:11 +08:00
|
|
|
|
2021-07-24 21:46:25 +08:00
|
|
|
for (const StringName &E : animations) {
|
2023-07-20 23:34:06 +08:00
|
|
|
Ref<Animation> anim = mixer->get_animation(E);
|
2018-06-27 06:05:11 +08:00
|
|
|
for (int i = 0; i < anim->get_track_count(); i++) {
|
2022-11-18 15:49:20 +08:00
|
|
|
String pathname = anim->track_get_path(i).get_concatenated_names();
|
|
|
|
if (!paths.has(pathname)) {
|
|
|
|
paths.insert(pathname);
|
|
|
|
}
|
2018-06-27 06:05:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filters->clear();
|
|
|
|
TreeItem *root = filters->create_item();
|
|
|
|
|
2022-05-13 21:04:37 +08:00
|
|
|
HashMap<String, TreeItem *> parenthood;
|
2018-06-27 06:05:11 +08:00
|
|
|
|
2022-05-19 07:43:40 +08:00
|
|
|
for (const String &E : paths) {
|
|
|
|
NodePath path = E;
|
2020-04-02 07:20:12 +08:00
|
|
|
TreeItem *ti = nullptr;
|
2018-06-27 06:05:11 +08:00
|
|
|
String accum;
|
|
|
|
for (int i = 0; i < path.get_name_count(); i++) {
|
|
|
|
String name = path.get_name(i);
|
2021-12-09 17:42:46 +08:00
|
|
|
if (!accum.is_empty()) {
|
2018-06-27 06:05:11 +08:00
|
|
|
accum += "/";
|
|
|
|
}
|
|
|
|
accum += name;
|
|
|
|
if (!parenthood.has(accum)) {
|
|
|
|
if (ti) {
|
|
|
|
ti = filters->create_item(ti);
|
|
|
|
} else {
|
|
|
|
ti = filters->create_item(root);
|
|
|
|
}
|
|
|
|
parenthood[accum] = ti;
|
|
|
|
ti->set_text(0, name);
|
|
|
|
ti->set_selectable(0, false);
|
|
|
|
ti->set_editable(0, false);
|
|
|
|
|
|
|
|
if (base->has_node(accum)) {
|
|
|
|
Node *node = base->get_node(accum);
|
2018-09-03 05:40:51 +08:00
|
|
|
ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));
|
2018-06-27 06:05:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ti = parenthood[accum];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
Node *node = nullptr;
|
2018-06-27 06:05:11 +08:00
|
|
|
if (base->has_node(accum)) {
|
|
|
|
node = base->get_node(accum);
|
|
|
|
}
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!node) {
|
2018-09-13 09:38:39 +08:00
|
|
|
continue; //no node, can't edit
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-06-27 06:05:11 +08:00
|
|
|
|
2022-11-18 15:49:20 +08:00
|
|
|
Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
|
|
|
|
if (skeleton) {
|
|
|
|
HashMap<int, TreeItem *> items;
|
|
|
|
items.insert(-1, ti);
|
2024-01-28 08:11:43 +08:00
|
|
|
Ref<Texture> bone_icon = get_editor_theme_icon(SNAME("Bone"));
|
2022-11-18 15:49:20 +08:00
|
|
|
Vector<int> bones_to_process = skeleton->get_parentless_bones();
|
|
|
|
while (bones_to_process.size() > 0) {
|
|
|
|
int current_bone_idx = bones_to_process[0];
|
|
|
|
bones_to_process.erase(current_bone_idx);
|
|
|
|
|
|
|
|
Vector<int> current_bone_child_bones = skeleton->get_bone_children(current_bone_idx);
|
|
|
|
int child_bone_size = current_bone_child_bones.size();
|
|
|
|
for (int i = 0; i < child_bone_size; i++) {
|
|
|
|
bones_to_process.push_back(current_bone_child_bones[i]);
|
2018-06-27 06:05:11 +08:00
|
|
|
}
|
|
|
|
|
2022-11-18 15:49:20 +08:00
|
|
|
const int parent_idx = skeleton->get_bone_parent(current_bone_idx);
|
|
|
|
TreeItem *parent_item = items.find(parent_idx)->value;
|
2018-06-27 06:05:11 +08:00
|
|
|
|
2022-11-18 15:49:20 +08:00
|
|
|
TreeItem *joint_item = filters->create_item(parent_item);
|
|
|
|
items.insert(current_bone_idx, joint_item);
|
2018-06-27 06:05:11 +08:00
|
|
|
|
2022-11-18 15:49:20 +08:00
|
|
|
joint_item->set_text(0, skeleton->get_bone_name(current_bone_idx));
|
|
|
|
joint_item->set_icon(0, bone_icon);
|
|
|
|
joint_item->set_selectable(0, true);
|
|
|
|
joint_item->set_metadata(0, accum + ":" + skeleton->get_bone_name(current_bone_idx));
|
|
|
|
joint_item->set_collapsed(true);
|
2018-06-27 06:05:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filters->ensure_cursor_is_visible();
|
2023-07-20 23:34:06 +08:00
|
|
|
filter_dialog->popup_centered(Size2(500, 500) * EDSCALE);
|
2018-06-27 06:05:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyRootMotion::_node_clear() {
|
2019-01-19 00:01:24 +08:00
|
|
|
emit_changed(get_edited_property(), NodePath());
|
2018-06-27 06:05:11 +08:00
|
|
|
update_property();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyRootMotion::update_property() {
|
2023-05-26 15:13:24 +08:00
|
|
|
NodePath p = get_edited_property_value();
|
2022-08-25 18:42:17 +08:00
|
|
|
assign->set_tooltip_text(p);
|
2018-06-27 06:05:11 +08:00
|
|
|
if (p == NodePath()) {
|
2019-06-12 02:43:37 +08:00
|
|
|
assign->set_icon(Ref<Texture2D>());
|
2019-01-07 23:57:52 +08:00
|
|
|
assign->set_text(TTR("Assign..."));
|
2018-06-27 06:05:11 +08:00
|
|
|
assign->set_flat(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-18 15:49:20 +08:00
|
|
|
assign->set_icon(Ref<Texture2D>());
|
|
|
|
assign->set_text(p);
|
2018-06-27 06:05:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyRootMotion::setup(const NodePath &p_base_hint) {
|
|
|
|
base_hint = p_base_hint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyRootMotion::_notification(int p_what) {
|
2022-02-16 10:44:22 +08:00
|
|
|
switch (p_what) {
|
2022-08-29 17:04:31 +08:00
|
|
|
case NOTIFICATION_ENTER_TREE:
|
2022-02-16 10:44:22 +08:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2023-08-13 08:33:39 +08:00
|
|
|
Ref<Texture2D> t = get_editor_theme_icon(SNAME("Clear"));
|
2022-02-16 10:44:22 +08:00
|
|
|
clear->set_icon(t);
|
|
|
|
} break;
|
2018-06-27 06:05:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyRootMotion::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorPropertyRootMotion::EditorPropertyRootMotion() {
|
|
|
|
HBoxContainer *hbc = memnew(HBoxContainer);
|
|
|
|
add_child(hbc);
|
|
|
|
assign = memnew(Button);
|
|
|
|
assign->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
assign->set_clip_text(true);
|
2020-02-22 01:28:45 +08:00
|
|
|
assign->connect("pressed", callable_mp(this, &EditorPropertyRootMotion::_node_assign));
|
2018-06-27 06:05:11 +08:00
|
|
|
hbc->add_child(assign);
|
|
|
|
|
|
|
|
clear = memnew(Button);
|
2020-02-22 01:28:45 +08:00
|
|
|
clear->connect("pressed", callable_mp(this, &EditorPropertyRootMotion::_node_clear));
|
2018-06-27 06:05:11 +08:00
|
|
|
hbc->add_child(clear);
|
|
|
|
|
|
|
|
filter_dialog = memnew(ConfirmationDialog);
|
|
|
|
add_child(filter_dialog);
|
|
|
|
filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
|
2020-02-22 01:28:45 +08:00
|
|
|
filter_dialog->connect("confirmed", callable_mp(this, &EditorPropertyRootMotion::_confirmed));
|
2018-06-27 06:05:11 +08:00
|
|
|
|
|
|
|
filters = memnew(Tree);
|
|
|
|
filter_dialog->add_child(filters);
|
2024-03-17 16:28:18 +08:00
|
|
|
filters->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
|
2018-06-27 06:05:11 +08:00
|
|
|
filters->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
filters->set_hide_root(true);
|
2020-02-22 01:28:45 +08:00
|
|
|
filters->connect("item_activated", callable_mp(this, &EditorPropertyRootMotion::_confirmed));
|
2018-06-27 06:05:11 +08:00
|
|
|
//filters->connect("item_edited", this, "_filter_edited");
|
|
|
|
}
|
2020-05-14 20:29:06 +08:00
|
|
|
|
2018-06-27 06:05:11 +08:00
|
|
|
//////////////////////////
|
|
|
|
|
|
|
|
bool EditorInspectorRootMotionPlugin::can_handle(Object *p_object) {
|
2021-11-10 22:49:19 +08:00
|
|
|
return true; // Can handle everything.
|
2018-06-27 06:05:11 +08:00
|
|
|
}
|
|
|
|
|
2023-02-01 02:08:46 +08:00
|
|
|
bool EditorInspectorRootMotionPlugin::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
|
2023-07-20 23:34:06 +08:00
|
|
|
if (p_path == "root_motion_track" && p_object->is_class("AnimationMixer") && p_type == Variant::NODE_PATH) {
|
2018-06-27 06:05:11 +08:00
|
|
|
EditorPropertyRootMotion *editor = memnew(EditorPropertyRootMotion);
|
|
|
|
add_property_editor(p_path, editor);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-10 22:49:19 +08:00
|
|
|
return false;
|
2018-06-27 06:05:11 +08:00
|
|
|
}
|