2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* multi_node_edit.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 12:46:12 +00: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-04 23:50:27 +00:00
|
|
|
|
2015-08-25 03:08:45 +00:00
|
|
|
#include "multi_node_edit.h"
|
2017-01-16 07:04:19 +00:00
|
|
|
|
2018-09-22 20:31:56 +00:00
|
|
|
#include "core/math/math_fieldwise.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "editor_node.h"
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool MultiNodeEdit::_set(const StringName &p_name, const Variant &p_value) {
|
2017-03-02 09:42:05 +00:00
|
|
|
return _set_impl(p_name, p_value, "");
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool MultiNodeEdit::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field) {
|
2015-08-25 03:08:45 +00:00
|
|
|
Node *es = EditorNode::get_singleton()->get_edited_scene();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!es) {
|
2015-08-25 03:08:45 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2016-06-19 13:12:35 +00:00
|
|
|
String name = p_name;
|
|
|
|
|
2017-03-05 15:44:50 +00: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 13:12:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-22 15:17:30 +00: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 13:08:25 +00:00
|
|
|
UndoRedo *ur = EditorNode::get_undo_redo();
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2018-11-19 00:43:22 +00:00
|
|
|
ur->create_action(TTR("MultiNode Set") + " " + String(name), UndoRedo::MERGE_ENDS);
|
2021-07-16 03:45:57 +00:00
|
|
|
for (const NodePath &E : nodes) {
|
|
|
|
if (!es->has_node(E)) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2021-07-16 03:45:57 +00:00
|
|
|
Node *n = es->get_node(E);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!n) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2016-10-04 13:49:11 +00:00
|
|
|
if (p_value.get_type() == Variant::NODE_PATH) {
|
2021-08-22 15:17:30 +00: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 13:49:11 +00:00
|
|
|
} else {
|
2017-03-02 09:42:05 +00:00
|
|
|
Variant new_value;
|
2021-12-09 09:42:46 +00:00
|
|
|
if (p_field.is_empty()) {
|
2017-03-02 09:42:05 +00:00
|
|
|
// whole value
|
2017-03-05 15:44:50 +00:00
|
|
|
new_value = p_value;
|
2017-03-02 09:42:05 +00:00
|
|
|
} else {
|
|
|
|
// only one field
|
2017-03-05 15:44:50 +00:00
|
|
|
new_value = fieldwise_assign(n->get(name), p_value, p_field);
|
2017-03-02 09:42:05 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
ur->add_do_property(n, name, new_value);
|
2016-10-04 13:49:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ur->add_undo_property(n, name, n->get(name));
|
2015-08-25 03:08:45 +00:00
|
|
|
}
|
2021-11-17 20:08:55 +00:00
|
|
|
ur->add_do_method(InspectorDock::get_inspector_singleton(), "refresh");
|
|
|
|
ur->add_undo_method(InspectorDock::get_inspector_singleton(), "refresh");
|
2015-08-25 03:08:45 +00:00
|
|
|
|
|
|
|
ur->commit_action();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {
|
2015-08-25 03:08:45 +00:00
|
|
|
Node *es = EditorNode::get_singleton()->get_edited_scene();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!es) {
|
2015-08-25 03:08:45 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00: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 13:12:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 03:45:57 +00:00
|
|
|
for (const NodePath &E : nodes) {
|
|
|
|
if (!es->has_node(E)) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2021-07-16 03:45:57 +00:00
|
|
|
const Node *n = es->get_node(E);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!n) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
|
|
|
bool found;
|
2017-03-05 15:44:50 +00:00
|
|
|
r_ret = n->get(name, &found);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (found) {
|
2015-08-25 03:08:45 +00:00
|
|
|
return true;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
HashMap<String, PLData> usage;
|
2015-08-25 03:08:45 +00:00
|
|
|
|
|
|
|
Node *es = EditorNode::get_singleton()->get_edited_scene();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!es) {
|
2015-08-25 03:08:45 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int nc = 0;
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2019-11-22 07:35:03 +00:00
|
|
|
List<PLData *> data_list;
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2021-07-16 03:45:57 +00:00
|
|
|
for (const NodePath &E : nodes) {
|
|
|
|
if (!es->has_node(E)) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2021-07-16 03:45:57 +00:00
|
|
|
Node *n = es->get_node(E);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!n) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
|
|
|
List<PropertyInfo> plist;
|
2017-03-05 15:44:50 +00:00
|
|
|
n->get_property_list(&plist, true);
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2021-07-24 13:46:25 +00:00
|
|
|
for (const PropertyInfo &F : plist) {
|
2021-07-16 03:45:57 +00:00
|
|
|
if (F.name == "script") {
|
2016-06-19 13:12:35 +00:00
|
|
|
continue; //added later manually, since this is intercepted before being set (check Variant Object::get() )
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-16 03:45:57 +00:00
|
|
|
if (!usage.has(F.name)) {
|
2015-08-25 03:08:45 +00:00
|
|
|
PLData pld;
|
2017-03-05 15:44:50 +00:00
|
|
|
pld.uses = 0;
|
2021-07-16 03:45:57 +00:00
|
|
|
pld.info = F;
|
|
|
|
usage[F.name] = pld;
|
|
|
|
data_list.push_back(usage.getptr(F.name));
|
2015-08-25 03:08:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 20:30:01 +00:00
|
|
|
// Make sure only properties with the same exact PropertyInfo data will appear
|
2021-07-16 03:45:57 +00:00
|
|
|
if (usage[F.name].info == F) {
|
|
|
|
usage[F.name].uses++;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nc++;
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:46:25 +00:00
|
|
|
for (const PLData *E : data_list) {
|
2021-07-16 03:45:57 +00:00
|
|
|
if (nc == E->uses) {
|
|
|
|
p_list->push_back(E->info);
|
2015-08-25 03:08:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
|
2015-08-25 03:08:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MultiNodeEdit::add_node(const NodePath &p_node) {
|
2015-08-25 03:08:45 +00:00
|
|
|
nodes.push_back(p_node);
|
|
|
|
}
|
|
|
|
|
2019-11-10 08:37:51 +00:00
|
|
|
int MultiNodeEdit::get_node_count() const {
|
|
|
|
return nodes.size();
|
|
|
|
}
|
2017-03-02 09:42:05 +00:00
|
|
|
|
2019-11-10 08:37:51 +00: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 09:42:05 +00:00
|
|
|
_set_impl(p_property, p_value, p_field);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
MultiNodeEdit::MultiNodeEdit() {
|
2015-08-25 03:08:45 +00:00
|
|
|
}
|