multi_node_edit.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_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. String name = p_name;
  41. if (name == "scripts") { // script set is intercepted at object level (check Variant Object::get() ) ,so use a different name
  42. name = "script";
  43. }
  44. Node *node_path_target = nullptr;
  45. if (p_value.get_type() == Variant::NODE_PATH && p_value != NodePath()) {
  46. node_path_target = es->get_node(p_value);
  47. }
  48. UndoRedo *ur = EditorNode::get_undo_redo();
  49. ur->create_action(TTR("MultiNode Set") + " " + String(name), UndoRedo::MERGE_ENDS);
  50. for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
  51. if (!es->has_node(E->get()))
  52. continue;
  53. Node *n = es->get_node(E->get());
  54. if (!n)
  55. continue;
  56. if (p_value.get_type() == Variant::NODE_PATH) {
  57. NodePath path;
  58. if (node_path_target) {
  59. path = n->get_path_to(node_path_target);
  60. }
  61. ur->add_do_property(n, name, path);
  62. } else {
  63. Variant new_value;
  64. if (p_field == "") {
  65. // whole value
  66. new_value = p_value;
  67. } else {
  68. // only one field
  69. new_value = fieldwise_assign(n->get(name), p_value, p_field);
  70. }
  71. ur->add_do_property(n, name, new_value);
  72. }
  73. ur->add_undo_property(n, name, n->get(name));
  74. }
  75. ur->add_do_method(EditorNode::get_singleton()->get_inspector(), "refresh");
  76. ur->add_undo_method(EditorNode::get_singleton()->get_inspector(), "refresh");
  77. ur->commit_action();
  78. return true;
  79. }
  80. bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {
  81. Node *es = EditorNode::get_singleton()->get_edited_scene();
  82. if (!es)
  83. return false;
  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 List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
  89. if (!es->has_node(E->get()))
  90. continue;
  91. const Node *n = es->get_node(E->get());
  92. if (!n)
  93. continue;
  94. bool found;
  95. r_ret = n->get(name, &found);
  96. if (found)
  97. return true;
  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. int nc = 0;
  107. List<PLData *> data_list;
  108. for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
  109. if (!es->has_node(E->get()))
  110. continue;
  111. Node *n = es->get_node(E->get());
  112. if (!n)
  113. continue;
  114. List<PropertyInfo> plist;
  115. n->get_property_list(&plist, true);
  116. for (List<PropertyInfo>::Element *F = plist.front(); F; F = F->next()) {
  117. if (F->get().name == "script")
  118. continue; //added later manually, since this is intercepted before being set (check Variant Object::get() )
  119. if (!usage.has(F->get().name)) {
  120. PLData pld;
  121. pld.uses = 0;
  122. pld.info = F->get();
  123. usage[F->get().name] = pld;
  124. data_list.push_back(usage.getptr(F->get().name));
  125. }
  126. // Make sure only properties with the same exact PropertyInfo data will appear
  127. if (usage[F->get().name].info == F->get())
  128. usage[F->get().name].uses++;
  129. }
  130. nc++;
  131. }
  132. for (List<PLData *>::Element *E = data_list.front(); E; E = E->next()) {
  133. if (nc == E->get()->uses) {
  134. p_list->push_back(E->get()->info);
  135. }
  136. }
  137. p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
  138. }
  139. void MultiNodeEdit::clear_nodes() {
  140. nodes.clear();
  141. }
  142. void MultiNodeEdit::add_node(const NodePath &p_node) {
  143. nodes.push_back(p_node);
  144. }
  145. int MultiNodeEdit::get_node_count() const {
  146. return nodes.size();
  147. }
  148. NodePath MultiNodeEdit::get_node(int p_index) const {
  149. ERR_FAIL_INDEX_V(p_index, nodes.size(), NodePath());
  150. return nodes[p_index];
  151. }
  152. void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
  153. _set_impl(p_property, p_value, p_field);
  154. }
  155. MultiNodeEdit::MultiNodeEdit() {
  156. }