multi_node_edit.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_node.h"
  33. bool MultiNodeEdit::_set(const StringName &p_name, const Variant &p_value) {
  34. return _set_impl(p_name, p_value, "");
  35. }
  36. bool MultiNodeEdit::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field) {
  37. Node *es = EditorNode::get_singleton()->get_edited_scene();
  38. if (!es) {
  39. return false;
  40. }
  41. String name = p_name;
  42. if (name == "scripts") { // script set is intercepted at object level (check Variant Object::get() ) ,so use a different name
  43. name = "script";
  44. }
  45. Node *node_path_target = nullptr;
  46. if (p_value.get_type() == Variant::NODE_PATH && p_value != NodePath()) {
  47. node_path_target = es->get_node(p_value);
  48. }
  49. UndoRedo *ur = EditorNode::get_undo_redo();
  50. ur->create_action(TTR("MultiNode Set") + " " + String(name), UndoRedo::MERGE_ENDS);
  51. for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
  52. if (!es->has_node(E->get())) {
  53. continue;
  54. }
  55. Node *n = es->get_node(E->get());
  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 == "") {
  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->add_do_method(EditorNode::get_singleton()->get_inspector(), "refresh");
  79. ur->add_undo_method(EditorNode::get_singleton()->get_inspector(), "refresh");
  80. ur->commit_action();
  81. return true;
  82. }
  83. bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {
  84. Node *es = EditorNode::get_singleton()->get_edited_scene();
  85. if (!es) {
  86. return false;
  87. }
  88. String name = p_name;
  89. if (name == "scripts") { // script set is intercepted at object level (check Variant Object::get() ) ,so use a different name
  90. name = "script";
  91. }
  92. for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
  93. if (!es->has_node(E->get())) {
  94. continue;
  95. }
  96. const Node *n = es->get_node(E->get());
  97. if (!n) {
  98. continue;
  99. }
  100. bool found;
  101. r_ret = n->get(name, &found);
  102. if (found) {
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {
  109. HashMap<String, PLData> usage;
  110. Node *es = EditorNode::get_singleton()->get_edited_scene();
  111. if (!es) {
  112. return;
  113. }
  114. int nc = 0;
  115. List<PLData *> data_list;
  116. for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
  117. if (!es->has_node(E->get())) {
  118. continue;
  119. }
  120. Node *n = es->get_node(E->get());
  121. if (!n) {
  122. continue;
  123. }
  124. List<PropertyInfo> plist;
  125. n->get_property_list(&plist, true);
  126. for (List<PropertyInfo>::Element *F = plist.front(); F; F = F->next()) {
  127. if (F->get().name == "script") {
  128. continue; //added later manually, since this is intercepted before being set (check Variant Object::get() )
  129. }
  130. if (!usage.has(F->get().name)) {
  131. PLData pld;
  132. pld.uses = 0;
  133. pld.info = F->get();
  134. usage[F->get().name] = pld;
  135. data_list.push_back(usage.getptr(F->get().name));
  136. }
  137. // Make sure only properties with the same exact PropertyInfo data will appear
  138. if (usage[F->get().name].info == F->get()) {
  139. usage[F->get().name].uses++;
  140. }
  141. }
  142. nc++;
  143. }
  144. for (List<PLData *>::Element *E = data_list.front(); E; E = E->next()) {
  145. if (nc == E->get()->uses) {
  146. p_list->push_back(E->get()->info);
  147. }
  148. }
  149. p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
  150. }
  151. void MultiNodeEdit::clear_nodes() {
  152. nodes.clear();
  153. }
  154. void MultiNodeEdit::add_node(const NodePath &p_node) {
  155. nodes.push_back(p_node);
  156. }
  157. int MultiNodeEdit::get_node_count() const {
  158. return nodes.size();
  159. }
  160. NodePath MultiNodeEdit::get_node(int p_index) const {
  161. ERR_FAIL_INDEX_V(p_index, nodes.size(), NodePath());
  162. return nodes[p_index];
  163. }
  164. StringName MultiNodeEdit::get_edited_class_name() const {
  165. Node *es = EditorNode::get_singleton()->get_edited_scene();
  166. if (!es) {
  167. return StringName("Node");
  168. }
  169. // Get the class name of the first node.
  170. StringName class_name;
  171. for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
  172. Node *node = es->get_node_or_null(E->get());
  173. if (!node) {
  174. continue;
  175. }
  176. class_name = node->get_class_name();
  177. break;
  178. }
  179. if (class_name == StringName()) {
  180. return StringName("Node");
  181. }
  182. bool check_again = true;
  183. while (check_again) {
  184. check_again = false;
  185. if (class_name == StringName("Node") || class_name == StringName()) {
  186. // All nodes inherit from Node, so no need to continue checking.
  187. return StringName("Node");
  188. }
  189. // Check that all nodes inherit from class_name.
  190. for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
  191. Node *node = es->get_node_or_null(E->get());
  192. if (!node) {
  193. continue;
  194. }
  195. const StringName node_class_name = node->get_class_name();
  196. if (class_name == node_class_name || ClassDB::is_parent_class(node_class_name, class_name)) {
  197. // class_name is the same or a parent of the node's class.
  198. continue;
  199. }
  200. // class_name is not a parent of the node's class, so check again with the parent class.
  201. class_name = ClassDB::get_parent_class(class_name);
  202. check_again = true;
  203. break;
  204. }
  205. }
  206. return class_name;
  207. }
  208. void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
  209. _set_impl(p_property, p_value, p_field);
  210. }
  211. MultiNodeEdit::MultiNodeEdit() {
  212. }