multi_node_edit.cpp 5.8 KB

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