multi_node_edit.cpp 8.8 KB

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