Path: blob/master/editor/inspector/multi_node_edit.cpp
21000 views
/**************************************************************************/1/* multi_node_edit.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "multi_node_edit.h"3132#include "core/math/math_fieldwise.h"33#include "editor/editor_node.h"34#include "editor/editor_undo_redo_manager.h"3536bool MultiNodeEdit::_set(const StringName &p_name, const Variant &p_value) {37return _set_impl(p_name, p_value, "");38}3940bool MultiNodeEdit::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field, bool p_undo_redo) {41Node *es = EditorNode::get_singleton()->get_edited_scene();42if (!es) {43return false;44}4546String name = p_name;4748if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.49name = "script";50} else if (name.begins_with("Metadata/")) {51name = name.replace_first("Metadata/", "metadata/");52}5354Node *node_path_target = nullptr;55if (p_value.get_type() == Variant::NODE_PATH && p_value != NodePath()) {56node_path_target = es->get_node(p_value);57}5859EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();6061if (p_undo_redo) {62ur->create_action(vformat(TTR("Set %s on %d nodes"), name, get_node_count()), UndoRedo::MERGE_ENDS);63}6465for (const NodePath &E : nodes) {66Node *n = es->get_node_or_null(E);67if (!n) {68continue;69}7071Variant new_value;72if (p_value.get_type() == Variant::NODE_PATH) {73NodePath path;74if (node_path_target) {75path = n->get_path_to(node_path_target);76}7778if (p_undo_redo) {79ur->add_do_property(n, name, path);80} else {81n->set(name, path);82}83} else {84if (p_field.is_empty()) {85// whole value86new_value = p_value;87} else {88// only one field89new_value = fieldwise_assign(n->get(name), p_value, p_field);90}9192if (p_undo_redo) {93ur->add_do_property(n, name, new_value);94} else {95n->set(name, new_value);96}97}9899if (p_undo_redo) {100ur->add_undo_property(n, name, n->get(name));101Variant old_value = n->get(p_name);102Variant::Type type = old_value.get_type();103if ((type == Variant::OBJECT || type == Variant::ARRAY || type == Variant::DICTIONARY) && old_value != new_value) {104ur->add_do_method(EditorNode::get_singleton(), "update_node_reference", old_value, n, true);105ur->add_do_method(EditorNode::get_singleton(), "update_node_reference", new_value, n, false);106// Perhaps an inefficient way of updating the resource count.107// We could go in depth and check which Resource values changed/got removed and which ones stayed the same, but this is more readable at the moment.108ur->add_undo_method(EditorNode::get_singleton(), "update_node_reference", new_value, n, true);109ur->add_undo_method(EditorNode::get_singleton(), "update_node_reference", old_value, n, false);110}111}112}113114if (p_undo_redo) {115ur->commit_action();116}117return true;118}119120bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {121Node *es = EditorNode::get_singleton()->get_edited_scene();122if (!es) {123return false;124}125126String name = p_name;127if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.128name = "script";129} else if (name.begins_with("Metadata/")) {130name = name.replace_first("Metadata/", "metadata/");131}132133for (const NodePath &E : nodes) {134const Node *n = es->get_node_or_null(E);135if (!n) {136continue;137}138139bool found;140r_ret = n->get(name, &found);141if (found) {142return true;143}144}145146return false;147}148149void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {150HashMap<String, PLData> usage;151152Node *es = EditorNode::get_singleton()->get_edited_scene();153if (!es) {154return;155}156157int nc = 0;158159List<PLData *> data_list;160161for (const NodePath &E : nodes) {162Node *n = es->get_node_or_null(E);163if (!n) {164continue;165}166167List<PropertyInfo> plist;168n->get_property_list(&plist, true);169170for (PropertyInfo F : plist) {171if (F.name == "script") {172continue; // Added later manually, since this is intercepted before being set (check Variant Object::get()).173} else if (F.name.begins_with("metadata/")) {174F.name = F.name.replace_first("metadata/", "Metadata/"); // Trick to not get actual metadata edited from MultiNodeEdit.175}176177PLData *usage_data = usage.getptr(F.name);178if (!usage_data) {179PLData pld;180pld.uses = 0;181pld.info = F;182pld.info.name = F.name;183HashMap<String, MultiNodeEdit::PLData>::Iterator I = usage.insert(F.name, pld);184usage_data = &I->value;185data_list.push_back(usage_data);186}187188// Make sure only properties with the same exact PropertyInfo data will appear.189if (usage_data->info == F) {190usage_data->uses++;191}192}193194nc++;195}196197for (const PLData *E : data_list) {198if (nc == E->uses) {199p_list->push_back(E->info);200}201}202203p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, Script::get_class_static()));204}205206String MultiNodeEdit::_get_editor_name() const {207return vformat(TTR("%s (%d Selected)"), get_edited_class_name(), get_node_count());208}209210bool MultiNodeEdit::_property_can_revert(const StringName &p_name) const {211Node *es = EditorNode::get_singleton()->get_edited_scene();212if (!es) {213return false;214}215216if (ClassDB::has_property(get_edited_class_name(), p_name)) {217for (const NodePath &E : nodes) {218Node *node = es->get_node_or_null(E);219if (node) {220return true;221}222}223224return false;225}226227// Don't show the revert button if the edited class doesn't have the property.228return false;229}230231bool MultiNodeEdit::_property_get_revert(const StringName &p_name, Variant &r_property) const {232Node *es = EditorNode::get_singleton()->get_edited_scene();233if (!es) {234return false;235}236237for (const NodePath &E : nodes) {238Node *node = es->get_node_or_null(E);239if (!node) {240continue;241}242243r_property = ClassDB::class_get_default_property_value(node->get_class_name(), p_name);244return true;245}246247return false;248}249250void MultiNodeEdit::_queue_notify_property_list_changed() {251if (notify_property_list_changed_pending) {252return;253}254notify_property_list_changed_pending = true;255callable_mp(this, &MultiNodeEdit::_notify_property_list_changed).call_deferred();256}257258void MultiNodeEdit::_notify_property_list_changed() {259notify_property_list_changed_pending = false;260notify_property_list_changed();261}262263void MultiNodeEdit::add_node(const NodePath &p_node) {264nodes.push_back(p_node);265266Node *es = EditorNode::get_singleton()->get_edited_scene();267if (es) {268Node *node = es->get_node_or_null(p_node);269if (node) {270node->connect(CoreStringName(property_list_changed), callable_mp(this, &MultiNodeEdit::_queue_notify_property_list_changed));271}272}273}274275int MultiNodeEdit::get_node_count() const {276return nodes.size();277}278279NodePath MultiNodeEdit::get_node(int p_index) const {280ERR_FAIL_INDEX_V(p_index, get_node_count(), NodePath());281return nodes[p_index];282}283284StringName MultiNodeEdit::get_edited_class_name() const {285Node *es = EditorNode::get_singleton()->get_edited_scene();286if (!es) {287return SNAME("Node");288}289290// Get the class name of the first node.291StringName class_name;292for (const NodePath &E : nodes) {293Node *node = es->get_node_or_null(E);294if (!node) {295continue;296}297298class_name = node->get_class_name();299break;300}301302if (class_name == StringName()) {303return SNAME("Node");304}305306bool check_again = true;307while (check_again) {308check_again = false;309310if (class_name == SNAME("Node") || class_name == StringName()) {311// All nodes inherit from Node, so no need to continue checking.312return SNAME("Node");313}314315// Check that all nodes inherit from class_name.316for (const NodePath &E : nodes) {317Node *node = es->get_node_or_null(E);318if (!node) {319continue;320}321322const StringName node_class_name = node->get_class_name();323if (class_name == node_class_name || ClassDB::is_parent_class(node_class_name, class_name)) {324// class_name is the same or a parent of the node's class.325continue;326}327328// class_name is not a parent of the node's class, so check again with the parent class.329class_name = ClassDB::get_parent_class(class_name);330check_again = true;331break;332}333}334335return class_name;336}337338void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {339// Ignore the field with arrays and dictionaries, as they are passed whole when edited.340Variant::Type type = p_value.get_type();341if (type == Variant::ARRAY || type == Variant::DICTIONARY) {342_set_impl(p_property, p_value, "");343} else {344_set_impl(p_property, p_value, p_field);345}346}347348void MultiNodeEdit::_bind_methods() {349ClassDB::bind_method("_hide_script_from_inspector", &MultiNodeEdit::_hide_script_from_inspector);350ClassDB::bind_method("_hide_metadata_from_inspector", &MultiNodeEdit::_hide_metadata_from_inspector);351ClassDB::bind_method("_get_editor_name", &MultiNodeEdit::_get_editor_name);352}353354355