2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* multi_node_edit.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* 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"
|
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;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (name == "scripts") { // script set is intercepted at object level (check Variant Object::get() ) ,so use a different name
|
|
|
|
name = "script";
|
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);
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:08:25 +02:00
|
|
|
UndoRedo *ur = EditorNode::get_undo_redo();
|
2015-08-25 05:08:45 +02:00
|
|
|
|
2018-11-19 01:43:22 +01:00
|
|
|
ur->create_action(TTR("MultiNode Set") + " " + String(name), UndoRedo::MERGE_ENDS);
|
2021-07-16 05:45:57 +02:00
|
|
|
for (const NodePath &E : nodes) {
|
|
|
|
if (!es->has_node(E)) {
|
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
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
Node *n = es->get_node(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
|
|
|
}
|
2021-11-17 21:08:55 +01:00
|
|
|
ur->add_do_method(InspectorDock::get_inspector_singleton(), "refresh");
|
|
|
|
ur->add_undo_method(InspectorDock::get_inspector_singleton(), "refresh");
|
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;
|
|
|
|
if (name == "scripts") { // script set is intercepted at object level (check Variant Object::get() ) ,so use a different name
|
|
|
|
name = "script";
|
2016-06-19 15:12:35 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
for (const NodePath &E : nodes) {
|
|
|
|
if (!es->has_node(E)) {
|
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
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
const Node *n = es->get_node(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) {
|
|
|
|
if (!es->has_node(E)) {
|
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
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
Node *n = es->get_node(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
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const PropertyInfo &F : plist) {
|
2021-07-16 05:45:57 +02:00
|
|
|
if (F.name == "script") {
|
2016-06-19 15:12:35 +02:00
|
|
|
continue; //added later manually, since this is intercepted before being set (check Variant Object::get() )
|
2020-05-14 16:41:43 +02: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;
|
|
|
|
usage[F.name] = pld;
|
|
|
|
data_list.push_back(usage.getptr(F.name));
|
2015-08-25 05:08:45 +02:00
|
|
|
}
|
|
|
|
|
2019-07-05 22:30:01 +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
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
MultiNodeEdit::MultiNodeEdit() {
|
2015-08-25 05:08:45 +02:00
|
|
|
}
|