2018-08-29 22:38:13 +02: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 00:05:11 +02:00
|
|
|
#include "root_motion_editor_plugin.h"
|
2023-07-20 17:34:06 +02:00
|
|
|
|
2018-06-27 00:05:11 +02:00
|
|
|
#include "editor/editor_node.h"
|
2024-01-15 13:14:55 +01:00
|
|
|
#include "editor/themes/editor_scale.h"
|
2024-01-05 18:21:38 +01:00
|
|
|
#include "scene/3d/skeleton_3d.h"
|
2023-07-20 17:34:06 +02:00
|
|
|
#include "scene/animation/animation_mixer.h"
|
2023-04-07 18:59:49 +02:00
|
|
|
#include "scene/gui/button.h"
|
|
|
|
#include "scene/gui/dialogs.h"
|
2022-11-19 12:45:49 +01:00
|
|
|
#include "scene/gui/tree.h"
|
2020-03-04 02:51:12 +01:00
|
|
|
#include "scene/main/window.h"
|
2018-06-27 00:05:11 +02:00
|
|
|
|
|
|
|
void EditorPropertyRootMotion::_confirmed() {
|
|
|
|
TreeItem *ti = filters->get_selected();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!ti) {
|
2018-06-27 00:05:11 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-06-27 00:05:11 +02:00
|
|
|
|
|
|
|
NodePath path = ti->get_metadata(0);
|
2019-01-18 17:01:24 +01:00
|
|
|
emit_changed(get_edited_property(), path);
|
2018-06-27 00:05:11 +02:00
|
|
|
update_property();
|
|
|
|
filter_dialog->hide(); //may come from activated
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyRootMotion::_node_assign() {
|
2023-07-20 17:34:06 +02: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 00:05:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-20 17:34:06 +02:00
|
|
|
Node *base = mixer->get_node(mixer->get_root_node());
|
2018-06-27 00:05:11 +02:00
|
|
|
|
|
|
|
if (!base) {
|
2023-07-20 17:34:06 +02:00
|
|
|
EditorNode::get_singleton()->show_warning(TTR("AnimationMixer has no valid root node path, so unable to retrieve track names."));
|
2018-06-27 00:05:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-19 17:00:06 +02:00
|
|
|
HashSet<String> paths;
|
2018-06-27 00:05:11 +02:00
|
|
|
{
|
|
|
|
List<StringName> animations;
|
2023-07-20 17:34:06 +02:00
|
|
|
mixer->get_animation_list(&animations);
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const StringName &E : animations) {
|
2023-07-20 17:34:06 +02:00
|
|
|
Ref<Animation> anim = mixer->get_animation(E);
|
2018-06-27 00:05:11 +02:00
|
|
|
for (int i = 0; i < anim->get_track_count(); i++) {
|
2022-11-18 08:49:20 +01:00
|
|
|
String pathname = anim->track_get_path(i).get_concatenated_names();
|
|
|
|
if (!paths.has(pathname)) {
|
|
|
|
paths.insert(pathname);
|
|
|
|
}
|
2018-06-27 00:05:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filters->clear();
|
|
|
|
TreeItem *root = filters->create_item();
|
|
|
|
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<String, TreeItem *> parenthood;
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-05-19 01:43:40 +02:00
|
|
|
for (const String &E : paths) {
|
|
|
|
NodePath path = E;
|
2020-04-02 01:20:12 +02:00
|
|
|
TreeItem *ti = nullptr;
|
2018-06-27 00:05:11 +02:00
|
|
|
String accum;
|
|
|
|
for (int i = 0; i < path.get_name_count(); i++) {
|
|
|
|
String name = path.get_name(i);
|
2021-12-09 10:42:46 +01:00
|
|
|
if (!accum.is_empty()) {
|
2018-06-27 00:05:11 +02: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-02 23:40:51 +02:00
|
|
|
ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));
|
2018-06-27 00:05:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ti = parenthood[accum];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
Node *node = nullptr;
|
2018-06-27 00:05:11 +02:00
|
|
|
if (base->has_node(accum)) {
|
|
|
|
node = base->get_node(accum);
|
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!node) {
|
2018-09-13 03:38:39 +02:00
|
|
|
continue; //no node, can't edit
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-11-18 08:49:20 +01:00
|
|
|
Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
|
|
|
|
if (skeleton) {
|
|
|
|
HashMap<int, TreeItem *> items;
|
|
|
|
items.insert(-1, ti);
|
2024-01-28 01:11:43 +01:00
|
|
|
Ref<Texture> bone_icon = get_editor_theme_icon(SNAME("Bone"));
|
2022-11-18 08:49:20 +01: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 00:05:11 +02:00
|
|
|
}
|
|
|
|
|
2022-11-18 08:49:20 +01:00
|
|
|
const int parent_idx = skeleton->get_bone_parent(current_bone_idx);
|
|
|
|
TreeItem *parent_item = items.find(parent_idx)->value;
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-11-18 08:49:20 +01:00
|
|
|
TreeItem *joint_item = filters->create_item(parent_item);
|
|
|
|
items.insert(current_bone_idx, joint_item);
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-11-18 08:49:20 +01: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 00:05:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filters->ensure_cursor_is_visible();
|
2023-07-20 17:34:06 +02:00
|
|
|
filter_dialog->popup_centered(Size2(500, 500) * EDSCALE);
|
2018-06-27 00:05:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyRootMotion::_node_clear() {
|
2019-01-18 17:01:24 +01:00
|
|
|
emit_changed(get_edited_property(), NodePath());
|
2018-06-27 00:05:11 +02:00
|
|
|
update_property();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyRootMotion::update_property() {
|
2023-05-26 09:13:24 +02:00
|
|
|
NodePath p = get_edited_property_value();
|
2022-08-25 12:42:17 +02:00
|
|
|
assign->set_tooltip_text(p);
|
2018-06-27 00:05:11 +02:00
|
|
|
if (p == NodePath()) {
|
2019-06-11 20:43:37 +02:00
|
|
|
assign->set_icon(Ref<Texture2D>());
|
2019-01-07 16:57:52 +01:00
|
|
|
assign->set_text(TTR("Assign..."));
|
2018-06-27 00:05:11 +02:00
|
|
|
assign->set_flat(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-18 08:49:20 +01:00
|
|
|
assign->set_icon(Ref<Texture2D>());
|
|
|
|
assign->set_text(p);
|
2018-06-27 00:05:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyRootMotion::setup(const NodePath &p_base_hint) {
|
|
|
|
base_hint = p_base_hint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPropertyRootMotion::_notification(int p_what) {
|
2022-02-16 03:44:22 +01:00
|
|
|
switch (p_what) {
|
2022-08-29 11:04:31 +02:00
|
|
|
case NOTIFICATION_ENTER_TREE:
|
2022-02-16 03:44:22 +01:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2023-08-13 02:33:39 +02:00
|
|
|
Ref<Texture2D> t = get_editor_theme_icon(SNAME("Clear"));
|
2022-02-16 03:44:22 +01:00
|
|
|
clear->set_icon(t);
|
|
|
|
} break;
|
2018-06-27 00:05:11 +02: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-21 18:28:45 +01:00
|
|
|
assign->connect("pressed", callable_mp(this, &EditorPropertyRootMotion::_node_assign));
|
2018-06-27 00:05:11 +02:00
|
|
|
hbc->add_child(assign);
|
|
|
|
|
|
|
|
clear = memnew(Button);
|
2020-02-21 18:28:45 +01:00
|
|
|
clear->connect("pressed", callable_mp(this, &EditorPropertyRootMotion::_node_clear));
|
2018-06-27 00:05:11 +02:00
|
|
|
hbc->add_child(clear);
|
|
|
|
|
|
|
|
filter_dialog = memnew(ConfirmationDialog);
|
|
|
|
add_child(filter_dialog);
|
|
|
|
filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
|
2020-02-21 18:28:45 +01:00
|
|
|
filter_dialog->connect("confirmed", callable_mp(this, &EditorPropertyRootMotion::_confirmed));
|
2018-06-27 00:05:11 +02:00
|
|
|
|
|
|
|
filters = memnew(Tree);
|
|
|
|
filter_dialog->add_child(filters);
|
2024-03-17 09:28:18 +01:00
|
|
|
filters->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
|
2018-06-27 00:05:11 +02:00
|
|
|
filters->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
filters->set_hide_root(true);
|
2020-02-21 18:28:45 +01:00
|
|
|
filters->connect("item_activated", callable_mp(this, &EditorPropertyRootMotion::_confirmed));
|
2018-06-27 00:05:11 +02:00
|
|
|
//filters->connect("item_edited", this, "_filter_edited");
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2018-06-27 00:05:11 +02:00
|
|
|
//////////////////////////
|
|
|
|
|
|
|
|
bool EditorInspectorRootMotionPlugin::can_handle(Object *p_object) {
|
2021-11-10 15:49:19 +01:00
|
|
|
return true; // Can handle everything.
|
2018-06-27 00:05:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-31 19:08:46 +01: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 17:34:06 +02:00
|
|
|
if (p_path == "root_motion_track" && p_object->is_class("AnimationMixer") && p_type == Variant::NODE_PATH) {
|
2018-06-27 00:05:11 +02:00
|
|
|
EditorPropertyRootMotion *editor = memnew(EditorPropertyRootMotion);
|
|
|
|
add_property_editor(p_path, editor);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:49:19 +01:00
|
|
|
return false;
|
2018-06-27 00:05:11 +02:00
|
|
|
}
|