2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* multi_node_edit.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-01-05 00:50:27 +01:00
|
|
|
|
2015-08-25 05:08:45 +02:00
|
|
|
#include "multi_node_edit.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2018-09-22 22:31:56 +02:00
|
|
|
#include "core/math/math_fieldwise.h"
|
2022-02-12 02:46:22 +01:00
|
|
|
#include "editor/editor_node.h"
|
2022-03-25 18:06:46 +01:00
|
|
|
#include "editor/editor_undo_redo_manager.h"
|
2015-08-25 05:08:45 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool MultiNodeEdit::_set(const StringName &p_name, const Variant &p_value) {
|
2017-03-02 10:42:05 +01:00
|
|
|
return _set_impl(p_name, p_value, "");
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool MultiNodeEdit::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field) {
|
2015-08-25 05:08:45 +02:00
|
|
|
Node *es = EditorNode::get_singleton()->get_edited_scene();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!es) {
|
2015-08-25 05:08:45 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-08-25 05:08:45 +02:00
|
|
|
|
2016-06-19 15:12:35 +02:00
|
|
|
String name = p_name;
|
|
|
|
|
2022-06-14 20:34:17 +02:00
|
|
|
if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.
|
2017-03-05 16:44:50 +01:00
|
|
|
name = "script";
|
2023-01-13 19:10:19 +01:00
|
|
|
} else if (name.begins_with("Metadata/")) {
|
|
|
|
name = name.replace_first("Metadata/", "metadata/");
|
2016-06-19 15:12:35 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 17:17:30 +02:00
|
|
|
Node *node_path_target = nullptr;
|
|
|
|
if (p_value.get_type() == Variant::NODE_PATH && p_value != NodePath()) {
|
|
|
|
node_path_target = es->get_node(p_value);
|
|
|
|
}
|
|
|
|
|
2022-12-23 23:53:16 +01:00
|
|
|
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
|
2015-08-25 05:08:45 +02:00
|
|
|
|
2022-06-14 20:34:17 +02:00
|
|
|
ur->create_action(vformat(TTR("Set %s on %d nodes"), name, get_node_count()), UndoRedo::MERGE_ENDS);
|
2021-07-16 05:45:57 +02:00
|
|
|
for (const NodePath &E : nodes) {
|
2022-06-14 20:34:17 +02:00
|
|
|
Node *n = es->get_node_or_null(E);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!n) {
|
2015-08-25 05:08:45 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-08-25 05:08:45 +02:00
|
|
|
|
2016-10-04 15:49:11 +02:00
|
|
|
if (p_value.get_type() == Variant::NODE_PATH) {
|
2021-08-22 17:17:30 +02:00
|
|
|
NodePath path;
|
|
|
|
if (node_path_target) {
|
|
|
|
path = n->get_path_to(node_path_target);
|
|
|
|
}
|
|
|
|
ur->add_do_property(n, name, path);
|
2016-10-04 15:49:11 +02:00
|
|
|
} else {
|
2017-03-02 10:42:05 +01:00
|
|
|
Variant new_value;
|
2021-12-09 10:42:46 +01:00
|
|
|
if (p_field.is_empty()) {
|
2017-03-02 10:42:05 +01:00
|
|
|
// whole value
|
2017-03-05 16:44:50 +01:00
|
|
|
new_value = p_value;
|
2017-03-02 10:42:05 +01:00
|
|
|
} else {
|
|
|
|
// only one field
|
2017-03-05 16:44:50 +01:00
|
|
|
new_value = fieldwise_assign(n->get(name), p_value, p_field);
|
2017-03-02 10:42:05 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
ur->add_do_property(n, name, new_value);
|
2016-10-04 15:49:11 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ur->add_undo_property(n, name, n->get(name));
|
2015-08-25 05:08:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ur->commit_action();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {
|
2015-08-25 05:08:45 +02:00
|
|
|
Node *es = EditorNode::get_singleton()->get_edited_scene();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!es) {
|
2015-08-25 05:08:45 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-08-25 05:08:45 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String name = p_name;
|
2022-06-14 20:34:17 +02:00
|
|
|
if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.
|
2017-03-05 16:44:50 +01:00
|
|
|
name = "script";
|
2023-01-13 19:10:19 +01:00
|
|
|
} else if (name.begins_with("Metadata/")) {
|
|
|
|
name = name.replace_first("Metadata/", "metadata/");
|
2016-06-19 15:12:35 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
for (const NodePath &E : nodes) {
|
2022-06-14 20:34:17 +02:00
|
|
|
const Node *n = es->get_node_or_null(E);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!n) {
|
2015-08-25 05:08:45 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-08-25 05:08:45 +02:00
|
|
|
|
|
|
|
bool found;
|
2017-03-05 16:44:50 +01:00
|
|
|
r_ret = n->get(name, &found);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (found) {
|
2015-08-25 05:08:45 +02:00
|
|
|
return true;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-08-25 05:08:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
HashMap<String, PLData> usage;
|
2015-08-25 05:08:45 +02:00
|
|
|
|
|
|
|
Node *es = EditorNode::get_singleton()->get_edited_scene();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!es) {
|
2015-08-25 05:08:45 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-08-25 05:08:45 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int nc = 0;
|
2015-08-25 05:08:45 +02:00
|
|
|
|
2019-11-22 08:35:03 +01:00
|
|
|
List<PLData *> data_list;
|
2015-08-25 05:08:45 +02:00
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
for (const NodePath &E : nodes) {
|
2022-06-14 20:34:17 +02:00
|
|
|
Node *n = es->get_node_or_null(E);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!n) {
|
2015-08-25 05:08:45 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-08-25 05:08:45 +02:00
|
|
|
|
|
|
|
List<PropertyInfo> plist;
|
2017-03-05 16:44:50 +01:00
|
|
|
n->get_property_list(&plist, true);
|
2015-08-25 05:08:45 +02:00
|
|
|
|
2023-01-13 19:10:19 +01:00
|
|
|
for (PropertyInfo F : plist) {
|
2021-07-16 05:45:57 +02:00
|
|
|
if (F.name == "script") {
|
2022-06-14 20:34:17 +02:00
|
|
|
continue; // Added later manually, since this is intercepted before being set (check Variant Object::get()).
|
2023-01-13 19:10:19 +01:00
|
|
|
} else if (F.name.begins_with("metadata/")) {
|
|
|
|
F.name = F.name.replace_first("metadata/", "Metadata/"); // Trick to not get actual metadata edited from MultiNodeEdit.
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2023-01-13 19:10:19 +01:00
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
if (!usage.has(F.name)) {
|
2015-08-25 05:08:45 +02:00
|
|
|
PLData pld;
|
2017-03-05 16:44:50 +01:00
|
|
|
pld.uses = 0;
|
2021-07-16 05:45:57 +02:00
|
|
|
pld.info = F;
|
2023-01-13 19:10:19 +01:00
|
|
|
pld.info.name = F.name;
|
2021-07-16 05:45:57 +02:00
|
|
|
usage[F.name] = pld;
|
|
|
|
data_list.push_back(usage.getptr(F.name));
|
2015-08-25 05:08:45 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 20:34:17 +02:00
|
|
|
// Make sure only properties with the same exact PropertyInfo data will appear.
|
2021-07-16 05:45:57 +02:00
|
|
|
if (usage[F.name].info == F) {
|
|
|
|
usage[F.name].uses++;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-08-25 05:08:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nc++;
|
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const PLData *E : data_list) {
|
2021-07-16 05:45:57 +02:00
|
|
|
if (nc == E->uses) {
|
|
|
|
p_list->push_back(E->info);
|
2015-08-25 05:08:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
|
2015-08-25 05:08:45 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 20:34:17 +02:00
|
|
|
String MultiNodeEdit::_get_editor_name() const {
|
|
|
|
return vformat(TTR("%s (%d Selected)"), get_edited_class_name(), get_node_count());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MultiNodeEdit::_property_can_revert(const StringName &p_name) const {
|
|
|
|
Node *es = EditorNode::get_singleton()->get_edited_scene();
|
|
|
|
if (!es) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassDB::has_property(get_edited_class_name(), p_name)) {
|
|
|
|
StringName class_name;
|
|
|
|
for (const NodePath &E : nodes) {
|
|
|
|
Node *node = es->get_node_or_null(E);
|
|
|
|
if (!node) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
class_name = node->get_class_name();
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant default_value = ClassDB::class_get_default_property_value(class_name, p_name);
|
|
|
|
for (const NodePath &E : nodes) {
|
|
|
|
Node *node = es->get_node_or_null(E);
|
|
|
|
if (!node) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->get(p_name) != default_value) {
|
|
|
|
// A node that doesn't have the default value has been found, so show the revert button.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't show the revert button if the edited class doesn't have the property.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MultiNodeEdit::_property_get_revert(const StringName &p_name, Variant &r_property) const {
|
|
|
|
Node *es = EditorNode::get_singleton()->get_edited_scene();
|
|
|
|
if (!es) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const NodePath &E : nodes) {
|
|
|
|
Node *node = es->get_node_or_null(E);
|
|
|
|
if (!node) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_property = ClassDB::class_get_default_property_value(node->get_class_name(), p_name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void MultiNodeEdit::add_node(const NodePath &p_node) {
|
2015-08-25 05:08:45 +02:00
|
|
|
nodes.push_back(p_node);
|
|
|
|
}
|
|
|
|
|
2019-11-10 09:37:51 +01:00
|
|
|
int MultiNodeEdit::get_node_count() const {
|
|
|
|
return nodes.size();
|
|
|
|
}
|
2017-03-02 10:42:05 +01:00
|
|
|
|
2019-11-10 09:37:51 +01:00
|
|
|
NodePath MultiNodeEdit::get_node(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, nodes.size(), NodePath());
|
|
|
|
return nodes[p_index];
|
|
|
|
}
|
|
|
|
|
2022-06-14 20:34:17 +02:00
|
|
|
StringName MultiNodeEdit::get_edited_class_name() const {
|
|
|
|
Node *es = EditorNode::get_singleton()->get_edited_scene();
|
|
|
|
if (!es) {
|
|
|
|
return SNAME("Node");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the class name of the first node.
|
|
|
|
StringName class_name;
|
|
|
|
for (const NodePath &E : nodes) {
|
|
|
|
Node *node = es->get_node_or_null(E);
|
|
|
|
if (!node) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
class_name = node->get_class_name();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (class_name == StringName()) {
|
|
|
|
return SNAME("Node");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool check_again = true;
|
|
|
|
while (check_again) {
|
|
|
|
check_again = false;
|
|
|
|
|
|
|
|
if (class_name == SNAME("Node") || class_name == StringName()) {
|
|
|
|
// All nodes inherit from Node, so no need to continue checking.
|
|
|
|
return SNAME("Node");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all nodes inherit from class_name.
|
|
|
|
for (const NodePath &E : nodes) {
|
|
|
|
Node *node = es->get_node_or_null(E);
|
|
|
|
if (!node) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StringName node_class_name = node->get_class_name();
|
|
|
|
if (class_name == node_class_name || ClassDB::is_parent_class(node_class_name, class_name)) {
|
|
|
|
// class_name is the same or a parent of the node's class.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// class_name is not a parent of the node's class, so check again with the parent class.
|
|
|
|
class_name = ClassDB::get_parent_class(class_name);
|
|
|
|
check_again = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return class_name;
|
|
|
|
}
|
|
|
|
|
2019-11-10 09:37:51 +01:00
|
|
|
void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
|
2017-03-02 10:42:05 +01:00
|
|
|
_set_impl(p_property, p_value, p_field);
|
|
|
|
}
|
|
|
|
|
2022-06-14 20:34:17 +02:00
|
|
|
void MultiNodeEdit::_bind_methods() {
|
|
|
|
ClassDB::bind_method("_hide_script_from_inspector", &MultiNodeEdit::_hide_script_from_inspector);
|
|
|
|
ClassDB::bind_method("_hide_metadata_from_inspector", &MultiNodeEdit::_hide_metadata_from_inspector);
|
|
|
|
ClassDB::bind_method("_get_editor_name", &MultiNodeEdit::_get_editor_name);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
MultiNodeEdit::MultiNodeEdit() {
|
2015-08-25 05:08:45 +02:00
|
|
|
}
|