multi_node_edit.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. StringName class_name;
  158. for (const NodePath &E : nodes) {
  159. Node *node = es->get_node_or_null(E);
  160. if (!node) {
  161. continue;
  162. }
  163. class_name = node->get_class_name();
  164. }
  165. Variant default_value = ClassDB::class_get_default_property_value(class_name, p_name);
  166. for (const NodePath &E : nodes) {
  167. Node *node = es->get_node_or_null(E);
  168. if (!node) {
  169. continue;
  170. }
  171. if (node->get(p_name) != default_value) {
  172. // A node that doesn't have the default value has been found, so show the revert button.
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. // Don't show the revert button if the edited class doesn't have the property.
  179. return false;
  180. }
  181. bool MultiNodeEdit::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  182. Node *es = EditorNode::get_singleton()->get_edited_scene();
  183. if (!es) {
  184. return false;
  185. }
  186. for (const NodePath &E : nodes) {
  187. Node *node = es->get_node_or_null(E);
  188. if (!node) {
  189. continue;
  190. }
  191. r_property = ClassDB::class_get_default_property_value(node->get_class_name(), p_name);
  192. return true;
  193. }
  194. return false;
  195. }
  196. void MultiNodeEdit::add_node(const NodePath &p_node) {
  197. nodes.push_back(p_node);
  198. }
  199. int MultiNodeEdit::get_node_count() const {
  200. return nodes.size();
  201. }
  202. NodePath MultiNodeEdit::get_node(int p_index) const {
  203. ERR_FAIL_INDEX_V(p_index, get_node_count(), NodePath());
  204. return nodes[p_index];
  205. }
  206. StringName MultiNodeEdit::get_edited_class_name() const {
  207. Node *es = EditorNode::get_singleton()->get_edited_scene();
  208. if (!es) {
  209. return SNAME("Node");
  210. }
  211. // Get the class name of the first node.
  212. StringName class_name;
  213. for (const NodePath &E : nodes) {
  214. Node *node = es->get_node_or_null(E);
  215. if (!node) {
  216. continue;
  217. }
  218. class_name = node->get_class_name();
  219. break;
  220. }
  221. if (class_name == StringName()) {
  222. return SNAME("Node");
  223. }
  224. bool check_again = true;
  225. while (check_again) {
  226. check_again = false;
  227. if (class_name == SNAME("Node") || class_name == StringName()) {
  228. // All nodes inherit from Node, so no need to continue checking.
  229. return SNAME("Node");
  230. }
  231. // Check that all nodes inherit from class_name.
  232. for (const NodePath &E : nodes) {
  233. Node *node = es->get_node_or_null(E);
  234. if (!node) {
  235. continue;
  236. }
  237. const StringName node_class_name = node->get_class_name();
  238. if (class_name == node_class_name || ClassDB::is_parent_class(node_class_name, class_name)) {
  239. // class_name is the same or a parent of the node's class.
  240. continue;
  241. }
  242. // class_name is not a parent of the node's class, so check again with the parent class.
  243. class_name = ClassDB::get_parent_class(class_name);
  244. check_again = true;
  245. break;
  246. }
  247. }
  248. return class_name;
  249. }
  250. void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
  251. _set_impl(p_property, p_value, p_field);
  252. }
  253. void MultiNodeEdit::_bind_methods() {
  254. ClassDB::bind_method("_hide_script_from_inspector", &MultiNodeEdit::_hide_script_from_inspector);
  255. ClassDB::bind_method("_hide_metadata_from_inspector", &MultiNodeEdit::_hide_metadata_from_inspector);
  256. ClassDB::bind_method("_get_editor_name", &MultiNodeEdit::_get_editor_name);
  257. }
  258. MultiNodeEdit::MultiNodeEdit() {
  259. }