multi_node_edit.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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) {
  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. ur->create_action(vformat(TTR("Set %s on %d nodes"), name, get_node_count()), UndoRedo::MERGE_ENDS);
  54. for (const NodePath &E : nodes) {
  55. Node *n = es->get_node_or_null(E);
  56. if (!n) {
  57. continue;
  58. }
  59. if (p_value.get_type() == Variant::NODE_PATH) {
  60. NodePath path;
  61. if (node_path_target) {
  62. path = n->get_path_to(node_path_target);
  63. }
  64. ur->add_do_property(n, name, path);
  65. } else {
  66. Variant new_value;
  67. if (p_field.is_empty()) {
  68. // whole value
  69. new_value = p_value;
  70. } else {
  71. // only one field
  72. new_value = fieldwise_assign(n->get(name), p_value, p_field);
  73. }
  74. ur->add_do_property(n, name, new_value);
  75. }
  76. ur->add_undo_property(n, name, n->get(name));
  77. }
  78. ur->commit_action();
  79. return true;
  80. }
  81. bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {
  82. Node *es = EditorNode::get_singleton()->get_edited_scene();
  83. if (!es) {
  84. return false;
  85. }
  86. String name = p_name;
  87. if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.
  88. name = "script";
  89. } else if (name.begins_with("Metadata/")) {
  90. name = name.replace_first("Metadata/", "metadata/");
  91. }
  92. for (const NodePath &E : nodes) {
  93. const Node *n = es->get_node_or_null(E);
  94. if (!n) {
  95. continue;
  96. }
  97. bool found;
  98. r_ret = n->get(name, &found);
  99. if (found) {
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {
  106. HashMap<String, PLData> usage;
  107. Node *es = EditorNode::get_singleton()->get_edited_scene();
  108. if (!es) {
  109. return;
  110. }
  111. int nc = 0;
  112. List<PLData *> data_list;
  113. for (const NodePath &E : nodes) {
  114. Node *n = es->get_node_or_null(E);
  115. if (!n) {
  116. continue;
  117. }
  118. List<PropertyInfo> plist;
  119. n->get_property_list(&plist, true);
  120. for (PropertyInfo F : plist) {
  121. if (F.name == "script") {
  122. continue; // Added later manually, since this is intercepted before being set (check Variant Object::get()).
  123. } else if (F.name.begins_with("metadata/")) {
  124. F.name = F.name.replace_first("metadata/", "Metadata/"); // Trick to not get actual metadata edited from MultiNodeEdit.
  125. }
  126. if (!usage.has(F.name)) {
  127. PLData pld;
  128. pld.uses = 0;
  129. pld.info = F;
  130. pld.info.name = F.name;
  131. usage[F.name] = pld;
  132. data_list.push_back(usage.getptr(F.name));
  133. }
  134. // Make sure only properties with the same exact PropertyInfo data will appear.
  135. if (usage[F.name].info == F) {
  136. usage[F.name].uses++;
  137. }
  138. }
  139. nc++;
  140. }
  141. for (const PLData *E : data_list) {
  142. if (nc == E->uses) {
  143. p_list->push_back(E->info);
  144. }
  145. }
  146. p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
  147. }
  148. String MultiNodeEdit::_get_editor_name() const {
  149. return vformat(TTR("%s (%d Selected)"), get_edited_class_name(), get_node_count());
  150. }
  151. bool MultiNodeEdit::_property_can_revert(const StringName &p_name) const {
  152. Node *es = EditorNode::get_singleton()->get_edited_scene();
  153. if (!es) {
  154. return false;
  155. }
  156. if (ClassDB::has_property(get_edited_class_name(), p_name)) {
  157. for (const NodePath &E : nodes) {
  158. Node *node = es->get_node_or_null(E);
  159. if (node) {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. // Don't show the revert button if the edited class doesn't have the property.
  166. return false;
  167. }
  168. bool MultiNodeEdit::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  169. Node *es = EditorNode::get_singleton()->get_edited_scene();
  170. if (!es) {
  171. return false;
  172. }
  173. for (const NodePath &E : nodes) {
  174. Node *node = es->get_node_or_null(E);
  175. if (!node) {
  176. continue;
  177. }
  178. r_property = ClassDB::class_get_default_property_value(node->get_class_name(), p_name);
  179. return true;
  180. }
  181. return false;
  182. }
  183. void MultiNodeEdit::add_node(const NodePath &p_node) {
  184. nodes.push_back(p_node);
  185. }
  186. int MultiNodeEdit::get_node_count() const {
  187. return nodes.size();
  188. }
  189. NodePath MultiNodeEdit::get_node(int p_index) const {
  190. ERR_FAIL_INDEX_V(p_index, get_node_count(), NodePath());
  191. return nodes[p_index];
  192. }
  193. StringName MultiNodeEdit::get_edited_class_name() const {
  194. Node *es = EditorNode::get_singleton()->get_edited_scene();
  195. if (!es) {
  196. return SNAME("Node");
  197. }
  198. // Get the class name of the first node.
  199. StringName class_name;
  200. for (const NodePath &E : nodes) {
  201. Node *node = es->get_node_or_null(E);
  202. if (!node) {
  203. continue;
  204. }
  205. class_name = node->get_class_name();
  206. break;
  207. }
  208. if (class_name == StringName()) {
  209. return SNAME("Node");
  210. }
  211. bool check_again = true;
  212. while (check_again) {
  213. check_again = false;
  214. if (class_name == SNAME("Node") || class_name == StringName()) {
  215. // All nodes inherit from Node, so no need to continue checking.
  216. return SNAME("Node");
  217. }
  218. // Check that all nodes inherit from class_name.
  219. for (const NodePath &E : nodes) {
  220. Node *node = es->get_node_or_null(E);
  221. if (!node) {
  222. continue;
  223. }
  224. const StringName node_class_name = node->get_class_name();
  225. if (class_name == node_class_name || ClassDB::is_parent_class(node_class_name, class_name)) {
  226. // class_name is the same or a parent of the node's class.
  227. continue;
  228. }
  229. // class_name is not a parent of the node's class, so check again with the parent class.
  230. class_name = ClassDB::get_parent_class(class_name);
  231. check_again = true;
  232. break;
  233. }
  234. }
  235. return class_name;
  236. }
  237. void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
  238. _set_impl(p_property, p_value, p_field);
  239. }
  240. void MultiNodeEdit::_bind_methods() {
  241. ClassDB::bind_method("_hide_script_from_inspector", &MultiNodeEdit::_hide_script_from_inspector);
  242. ClassDB::bind_method("_hide_metadata_from_inspector", &MultiNodeEdit::_hide_metadata_from_inspector);
  243. ClassDB::bind_method("_get_editor_name", &MultiNodeEdit::_get_editor_name);
  244. }
  245. MultiNodeEdit::MultiNodeEdit() {
  246. }