multi_node_edit.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(TTR("MultiNode Set") + " " + String(name), UndoRedo::MERGE_ENDS);
  52. for (const NodePath &E : nodes) {
  53. if (!es->has_node(E)) {
  54. continue;
  55. }
  56. Node *n = es->get_node(E);
  57. if (!n) {
  58. continue;
  59. }
  60. if (p_value.get_type() == Variant::NODE_PATH) {
  61. NodePath path;
  62. if (node_path_target) {
  63. path = n->get_path_to(node_path_target);
  64. }
  65. ur->add_do_property(n, name, path);
  66. } else {
  67. Variant new_value;
  68. if (p_field.is_empty()) {
  69. // whole value
  70. new_value = p_value;
  71. } else {
  72. // only one field
  73. new_value = fieldwise_assign(n->get(name), p_value, p_field);
  74. }
  75. ur->add_do_property(n, name, new_value);
  76. }
  77. ur->add_undo_property(n, name, n->get(name));
  78. }
  79. ur->commit_action();
  80. return true;
  81. }
  82. bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {
  83. Node *es = EditorNode::get_singleton()->get_edited_scene();
  84. if (!es) {
  85. return false;
  86. }
  87. String name = p_name;
  88. if (name == "scripts") { // script set is intercepted at object level (check Variant Object::get() ) ,so use a different name
  89. name = "script";
  90. }
  91. for (const NodePath &E : nodes) {
  92. if (!es->has_node(E)) {
  93. continue;
  94. }
  95. const Node *n = es->get_node(E);
  96. if (!n) {
  97. continue;
  98. }
  99. bool found;
  100. r_ret = n->get(name, &found);
  101. if (found) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {
  108. HashMap<String, PLData> usage;
  109. Node *es = EditorNode::get_singleton()->get_edited_scene();
  110. if (!es) {
  111. return;
  112. }
  113. int nc = 0;
  114. List<PLData *> data_list;
  115. for (const NodePath &E : nodes) {
  116. if (!es->has_node(E)) {
  117. continue;
  118. }
  119. Node *n = es->get_node(E);
  120. if (!n) {
  121. continue;
  122. }
  123. List<PropertyInfo> plist;
  124. n->get_property_list(&plist, true);
  125. for (const PropertyInfo &F : plist) {
  126. if (F.name == "script") {
  127. continue; //added later manually, since this is intercepted before being set (check Variant Object::get() )
  128. }
  129. if (!usage.has(F.name)) {
  130. PLData pld;
  131. pld.uses = 0;
  132. pld.info = F;
  133. usage[F.name] = pld;
  134. data_list.push_back(usage.getptr(F.name));
  135. }
  136. // Make sure only properties with the same exact PropertyInfo data will appear
  137. if (usage[F.name].info == F) {
  138. usage[F.name].uses++;
  139. }
  140. }
  141. nc++;
  142. }
  143. for (const PLData *E : data_list) {
  144. if (nc == E->uses) {
  145. p_list->push_back(E->info);
  146. }
  147. }
  148. p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
  149. }
  150. void MultiNodeEdit::add_node(const NodePath &p_node) {
  151. nodes.push_back(p_node);
  152. }
  153. int MultiNodeEdit::get_node_count() const {
  154. return nodes.size();
  155. }
  156. NodePath MultiNodeEdit::get_node(int p_index) const {
  157. ERR_FAIL_INDEX_V(p_index, nodes.size(), NodePath());
  158. return nodes[p_index];
  159. }
  160. void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
  161. _set_impl(p_property, p_value, p_field);
  162. }
  163. MultiNodeEdit::MultiNodeEdit() {
  164. }