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
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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();
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!es) {
|
2015-08-25 03:08:45 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +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);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!es->has_node(E->get())) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Node *n = es->get_node(E->get());
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!n) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +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;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_field == "") {
|
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
|
|
|
}
|
2018-05-15 20:12:35 +00:00
|
|
|
ur->add_do_method(EditorNode::get_singleton()->get_inspector(), "refresh");
|
|
|
|
ur->add_undo_method(EditorNode::get_singleton()->get_inspector(), "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();
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!es) {
|
2015-08-25 03:08:45 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +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
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!es->has_node(E->get())) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const Node *n = es->get_node(E->get());
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!n) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +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);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (found) {
|
2015-08-25 03:08:45 +00:00
|
|
|
return true;
|
2021-05-05 10:44:11 +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();
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!es) {
|
2015-08-25 03:08:45 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +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
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!es->has_node(E->get())) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Node *n = es->get_node(E->get());
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!n) {
|
2015-08-25 03:08:45 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +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
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<PropertyInfo>::Element *F = plist.front(); F; F = F->next()) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (F->get().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() )
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
if (!usage.has(F->get().name)) {
|
|
|
|
PLData pld;
|
2017-03-05 15:44:50 +00:00
|
|
|
pld.uses = 0;
|
|
|
|
pld.info = F->get();
|
|
|
|
usage[F->get().name] = pld;
|
2019-11-22 07:35:03 +00:00
|
|
|
data_list.push_back(usage.getptr(F->get().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-05-05 10:44:11 +00:00
|
|
|
if (usage[F->get().name].info == F->get()) {
|
2019-07-05 20:30:01 +00:00
|
|
|
usage[F->get().name].uses++;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-08-25 03:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nc++;
|
|
|
|
}
|
|
|
|
|
2019-11-22 07:35:03 +00:00
|
|
|
for (List<PLData *>::Element *E = data_list.front(); E; E = E->next()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (nc == E->get()->uses) {
|
2015-08-25 03:08:45 +00:00
|
|
|
p_list->push_back(E->get()->info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void MultiNodeEdit::clear_nodes() {
|
|
|
|
nodes.clear();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|