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