multi_node_edit.cpp 6.1 KB

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