multi_node_edit.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**************************************************************************/
  2. /* multi_node_edit.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "multi_node_edit.h"
  31. #include "core/math/math_fieldwise.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. bool MultiNodeEdit::_set(const StringName &p_name, const Variant &p_value) {
  35. return _set_impl(p_name, p_value, "");
  36. }
  37. bool MultiNodeEdit::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field, bool p_undo_redo) {
  38. Node *es = EditorNode::get_singleton()->get_edited_scene();
  39. if (!es) {
  40. return false;
  41. }
  42. String name = p_name;
  43. if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.
  44. name = "script";
  45. } else if (name.begins_with("Metadata/")) {
  46. name = name.replace_first("Metadata/", "metadata/");
  47. }
  48. Node *node_path_target = nullptr;
  49. if (p_value.get_type() == Variant::NODE_PATH && p_value != NodePath()) {
  50. node_path_target = es->get_node(p_value);
  51. }
  52. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  53. if (p_undo_redo) {
  54. ur->create_action(vformat(TTR("Set %s on %d nodes"), name, get_node_count()), UndoRedo::MERGE_ENDS);
  55. }
  56. for (const NodePath &E : nodes) {
  57. Node *n = es->get_node_or_null(E);
  58. if (!n) {
  59. continue;
  60. }
  61. Variant new_value;
  62. if (p_value.get_type() == Variant::NODE_PATH) {
  63. NodePath path;
  64. if (node_path_target) {
  65. path = n->get_path_to(node_path_target);
  66. }
  67. if (p_undo_redo) {
  68. ur->add_do_property(n, name, path);
  69. } else {
  70. n->set(name, path);
  71. }
  72. } else {
  73. if (p_field.is_empty()) {
  74. // whole value
  75. new_value = p_value;
  76. } else {
  77. // only one field
  78. new_value = fieldwise_assign(n->get(name), p_value, p_field);
  79. }
  80. if (p_undo_redo) {
  81. ur->add_do_property(n, name, new_value);
  82. } else {
  83. n->set(name, new_value);
  84. }
  85. }
  86. if (p_undo_redo) {
  87. ur->add_undo_property(n, name, n->get(name));
  88. Variant old_value = n->get(p_name);
  89. Variant::Type type = old_value.get_type();
  90. if ((type == Variant::OBJECT || type == Variant::ARRAY || type == Variant::DICTIONARY) && old_value != new_value) {
  91. ur->add_do_method(EditorNode::get_singleton(), "update_node_reference", old_value, n, true);
  92. ur->add_do_method(EditorNode::get_singleton(), "update_node_reference", new_value, n, false);
  93. // Perhaps an inefficient way of updating the resource count.
  94. // 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.
  95. ur->add_undo_method(EditorNode::get_singleton(), "update_node_reference", new_value, n, true);
  96. ur->add_undo_method(EditorNode::get_singleton(), "update_node_reference", old_value, n, false);
  97. }
  98. }
  99. }
  100. if (p_undo_redo) {
  101. ur->commit_action();
  102. }
  103. return true;
  104. }
  105. bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {
  106. Node *es = EditorNode::get_singleton()->get_edited_scene();
  107. if (!es) {
  108. return false;
  109. }
  110. String name = p_name;
  111. if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.
  112. name = "script";
  113. } else if (name.begins_with("Metadata/")) {
  114. name = name.replace_first("Metadata/", "metadata/");
  115. }
  116. for (const NodePath &E : nodes) {
  117. const Node *n = es->get_node_or_null(E);
  118. if (!n) {
  119. continue;
  120. }
  121. bool found;
  122. r_ret = n->get(name, &found);
  123. if (found) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {
  130. HashMap<String, PLData> usage;
  131. Node *es = EditorNode::get_singleton()->get_edited_scene();
  132. if (!es) {
  133. return;
  134. }
  135. int nc = 0;
  136. List<PLData *> data_list;
  137. for (const NodePath &E : nodes) {
  138. Node *n = es->get_node_or_null(E);
  139. if (!n) {
  140. continue;
  141. }
  142. List<PropertyInfo> plist;
  143. n->get_property_list(&plist, true);
  144. for (PropertyInfo F : plist) {
  145. if (F.name == "script") {
  146. continue; // Added later manually, since this is intercepted before being set (check Variant Object::get()).
  147. } else if (F.name.begins_with("metadata/")) {
  148. F.name = F.name.replace_first("metadata/", "Metadata/"); // Trick to not get actual metadata edited from MultiNodeEdit.
  149. }
  150. PLData *usage_data = usage.getptr(F.name);
  151. if (!usage_data) {
  152. PLData pld;
  153. pld.uses = 0;
  154. pld.info = F;
  155. pld.info.name = F.name;
  156. HashMap<String, MultiNodeEdit::PLData>::Iterator I = usage.insert(F.name, pld);
  157. usage_data = &I->value;
  158. data_list.push_back(usage_data);
  159. }
  160. // Make sure only properties with the same exact PropertyInfo data will appear.
  161. if (usage_data->info == F) {
  162. usage_data->uses++;
  163. }
  164. }
  165. nc++;
  166. }
  167. for (const PLData *E : data_list) {
  168. if (nc == E->uses) {
  169. p_list->push_back(E->info);
  170. }
  171. }
  172. p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
  173. }
  174. String MultiNodeEdit::_get_editor_name() const {
  175. return vformat(TTR("%s (%d Selected)"), get_edited_class_name(), get_node_count());
  176. }
  177. bool MultiNodeEdit::_property_can_revert(const StringName &p_name) const {
  178. Node *es = EditorNode::get_singleton()->get_edited_scene();
  179. if (!es) {
  180. return false;
  181. }
  182. if (ClassDB::has_property(get_edited_class_name(), p_name)) {
  183. for (const NodePath &E : nodes) {
  184. Node *node = es->get_node_or_null(E);
  185. if (node) {
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. // Don't show the revert button if the edited class doesn't have the property.
  192. return false;
  193. }
  194. bool MultiNodeEdit::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  195. Node *es = EditorNode::get_singleton()->get_edited_scene();
  196. if (!es) {
  197. return false;
  198. }
  199. for (const NodePath &E : nodes) {
  200. Node *node = es->get_node_or_null(E);
  201. if (!node) {
  202. continue;
  203. }
  204. r_property = ClassDB::class_get_default_property_value(node->get_class_name(), p_name);
  205. return true;
  206. }
  207. return false;
  208. }
  209. void MultiNodeEdit::_queue_notify_property_list_changed() {
  210. if (notify_property_list_changed_pending) {
  211. return;
  212. }
  213. notify_property_list_changed_pending = true;
  214. callable_mp(this, &MultiNodeEdit::_notify_property_list_changed).call_deferred();
  215. }
  216. void MultiNodeEdit::_notify_property_list_changed() {
  217. notify_property_list_changed_pending = false;
  218. notify_property_list_changed();
  219. }
  220. void MultiNodeEdit::add_node(const NodePath &p_node) {
  221. nodes.push_back(p_node);
  222. Node *es = EditorNode::get_singleton()->get_edited_scene();
  223. if (es) {
  224. Node *node = es->get_node_or_null(p_node);
  225. if (node) {
  226. node->connect(CoreStringName(property_list_changed), callable_mp(this, &MultiNodeEdit::_queue_notify_property_list_changed));
  227. }
  228. }
  229. }
  230. int MultiNodeEdit::get_node_count() const {
  231. return nodes.size();
  232. }
  233. NodePath MultiNodeEdit::get_node(int p_index) const {
  234. ERR_FAIL_INDEX_V(p_index, get_node_count(), NodePath());
  235. return nodes[p_index];
  236. }
  237. StringName MultiNodeEdit::get_edited_class_name() const {
  238. Node *es = EditorNode::get_singleton()->get_edited_scene();
  239. if (!es) {
  240. return SNAME("Node");
  241. }
  242. // Get the class name of the first node.
  243. StringName class_name;
  244. for (const NodePath &E : nodes) {
  245. Node *node = es->get_node_or_null(E);
  246. if (!node) {
  247. continue;
  248. }
  249. class_name = node->get_class_name();
  250. break;
  251. }
  252. if (class_name == StringName()) {
  253. return SNAME("Node");
  254. }
  255. bool check_again = true;
  256. while (check_again) {
  257. check_again = false;
  258. if (class_name == SNAME("Node") || class_name == StringName()) {
  259. // All nodes inherit from Node, so no need to continue checking.
  260. return SNAME("Node");
  261. }
  262. // Check that all nodes inherit from class_name.
  263. for (const NodePath &E : nodes) {
  264. Node *node = es->get_node_or_null(E);
  265. if (!node) {
  266. continue;
  267. }
  268. const StringName node_class_name = node->get_class_name();
  269. if (class_name == node_class_name || ClassDB::is_parent_class(node_class_name, class_name)) {
  270. // class_name is the same or a parent of the node's class.
  271. continue;
  272. }
  273. // class_name is not a parent of the node's class, so check again with the parent class.
  274. class_name = ClassDB::get_parent_class(class_name);
  275. check_again = true;
  276. break;
  277. }
  278. }
  279. return class_name;
  280. }
  281. void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
  282. // Ignore the field with arrays and dictionaries, as they are passed whole when edited.
  283. Variant::Type type = p_value.get_type();
  284. if (type == Variant::ARRAY || type == Variant::DICTIONARY) {
  285. _set_impl(p_property, p_value, "");
  286. } else {
  287. _set_impl(p_property, p_value, p_field);
  288. }
  289. }
  290. void MultiNodeEdit::_bind_methods() {
  291. ClassDB::bind_method("_hide_script_from_inspector", &MultiNodeEdit::_hide_script_from_inspector);
  292. ClassDB::bind_method("_hide_metadata_from_inspector", &MultiNodeEdit::_hide_metadata_from_inspector);
  293. ClassDB::bind_method("_get_editor_name", &MultiNodeEdit::_get_editor_name);
  294. }