Path: blob/master/editor/inspector/multi_node_edit.cpp
9903 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) {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();6061ur->create_action(vformat(TTR("Set %s on %d nodes"), name, get_node_count()), UndoRedo::MERGE_ENDS);62for (const NodePath &E : nodes) {63Node *n = es->get_node_or_null(E);64if (!n) {65continue;66}6768if (p_value.get_type() == Variant::NODE_PATH) {69NodePath path;70if (node_path_target) {71path = n->get_path_to(node_path_target);72}73ur->add_do_property(n, name, path);74} else {75Variant new_value;76if (p_field.is_empty()) {77// whole value78new_value = p_value;79} else {80// only one field81new_value = fieldwise_assign(n->get(name), p_value, p_field);82}83ur->add_do_property(n, name, new_value);84}8586ur->add_undo_property(n, name, n->get(name));87}8889ur->commit_action();90return true;91}9293bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {94Node *es = EditorNode::get_singleton()->get_edited_scene();95if (!es) {96return false;97}9899String name = p_name;100if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.101name = "script";102} else if (name.begins_with("Metadata/")) {103name = name.replace_first("Metadata/", "metadata/");104}105106for (const NodePath &E : nodes) {107const Node *n = es->get_node_or_null(E);108if (!n) {109continue;110}111112bool found;113r_ret = n->get(name, &found);114if (found) {115return true;116}117}118119return false;120}121122void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {123HashMap<String, PLData> usage;124125Node *es = EditorNode::get_singleton()->get_edited_scene();126if (!es) {127return;128}129130int nc = 0;131132List<PLData *> data_list;133134for (const NodePath &E : nodes) {135Node *n = es->get_node_or_null(E);136if (!n) {137continue;138}139140List<PropertyInfo> plist;141n->get_property_list(&plist, true);142143for (PropertyInfo F : plist) {144if (F.name == "script") {145continue; // Added later manually, since this is intercepted before being set (check Variant Object::get()).146} else if (F.name.begins_with("metadata/")) {147F.name = F.name.replace_first("metadata/", "Metadata/"); // Trick to not get actual metadata edited from MultiNodeEdit.148}149150if (!usage.has(F.name)) {151PLData pld;152pld.uses = 0;153pld.info = F;154pld.info.name = F.name;155usage[F.name] = pld;156data_list.push_back(usage.getptr(F.name));157}158159// Make sure only properties with the same exact PropertyInfo data will appear.160if (usage[F.name].info == F) {161usage[F.name].uses++;162}163}164165nc++;166}167168for (const PLData *E : data_list) {169if (nc == E->uses) {170p_list->push_back(E->info);171}172}173174p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, "Script"));175}176177String MultiNodeEdit::_get_editor_name() const {178return vformat(TTR("%s (%d Selected)"), get_edited_class_name(), get_node_count());179}180181bool MultiNodeEdit::_property_can_revert(const StringName &p_name) const {182Node *es = EditorNode::get_singleton()->get_edited_scene();183if (!es) {184return false;185}186187if (ClassDB::has_property(get_edited_class_name(), p_name)) {188for (const NodePath &E : nodes) {189Node *node = es->get_node_or_null(E);190if (node) {191return true;192}193}194195return false;196}197198// Don't show the revert button if the edited class doesn't have the property.199return false;200}201202bool MultiNodeEdit::_property_get_revert(const StringName &p_name, Variant &r_property) const {203Node *es = EditorNode::get_singleton()->get_edited_scene();204if (!es) {205return false;206}207208for (const NodePath &E : nodes) {209Node *node = es->get_node_or_null(E);210if (!node) {211continue;212}213214r_property = ClassDB::class_get_default_property_value(node->get_class_name(), p_name);215return true;216}217218return false;219}220221void MultiNodeEdit::_queue_notify_property_list_changed() {222if (notify_property_list_changed_pending) {223return;224}225notify_property_list_changed_pending = true;226callable_mp(this, &MultiNodeEdit::_notify_property_list_changed).call_deferred();227}228229void MultiNodeEdit::_notify_property_list_changed() {230notify_property_list_changed_pending = false;231notify_property_list_changed();232}233234void MultiNodeEdit::add_node(const NodePath &p_node) {235nodes.push_back(p_node);236237Node *es = EditorNode::get_singleton()->get_edited_scene();238if (es) {239Node *node = es->get_node_or_null(p_node);240if (node) {241node->connect(CoreStringName(property_list_changed), callable_mp(this, &MultiNodeEdit::_queue_notify_property_list_changed));242}243}244}245246int MultiNodeEdit::get_node_count() const {247return nodes.size();248}249250NodePath MultiNodeEdit::get_node(int p_index) const {251ERR_FAIL_INDEX_V(p_index, get_node_count(), NodePath());252return nodes[p_index];253}254255StringName MultiNodeEdit::get_edited_class_name() const {256Node *es = EditorNode::get_singleton()->get_edited_scene();257if (!es) {258return SNAME("Node");259}260261// Get the class name of the first node.262StringName class_name;263for (const NodePath &E : nodes) {264Node *node = es->get_node_or_null(E);265if (!node) {266continue;267}268269class_name = node->get_class_name();270break;271}272273if (class_name == StringName()) {274return SNAME("Node");275}276277bool check_again = true;278while (check_again) {279check_again = false;280281if (class_name == SNAME("Node") || class_name == StringName()) {282// All nodes inherit from Node, so no need to continue checking.283return SNAME("Node");284}285286// Check that all nodes inherit from class_name.287for (const NodePath &E : nodes) {288Node *node = es->get_node_or_null(E);289if (!node) {290continue;291}292293const StringName node_class_name = node->get_class_name();294if (class_name == node_class_name || ClassDB::is_parent_class(node_class_name, class_name)) {295// class_name is the same or a parent of the node's class.296continue;297}298299// class_name is not a parent of the node's class, so check again with the parent class.300class_name = ClassDB::get_parent_class(class_name);301check_again = true;302break;303}304}305306return class_name;307}308309void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {310_set_impl(p_property, p_value, p_field);311}312313void MultiNodeEdit::_bind_methods() {314ClassDB::bind_method("_hide_script_from_inspector", &MultiNodeEdit::_hide_script_from_inspector);315ClassDB::bind_method("_hide_metadata_from_inspector", &MultiNodeEdit::_hide_metadata_from_inspector);316ClassDB::bind_method("_get_editor_name", &MultiNodeEdit::_get_editor_name);317}318319320