editor_sub_scene.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**************************************************************************/
  2. /* editor_sub_scene.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 "editor_sub_scene.h"
  31. #include "editor/editor_node.h"
  32. #include "scene/gui/margin_container.h"
  33. #include "scene/resources/packed_scene.h"
  34. void EditorSubScene::_path_selected(const String &p_path) {
  35. path->set_text(p_path);
  36. _path_changed(p_path);
  37. }
  38. void EditorSubScene::_path_changed(const String &p_path) {
  39. tree->clear();
  40. if (scene) {
  41. memdelete(scene);
  42. scene = nullptr;
  43. }
  44. if (p_path == "") {
  45. return;
  46. }
  47. Ref<PackedScene> ps = ResourceLoader::load(p_path, "PackedScene");
  48. if (ps.is_null()) {
  49. return;
  50. }
  51. scene = ps->instance();
  52. if (!scene) {
  53. return;
  54. }
  55. _fill_tree(scene, nullptr);
  56. }
  57. void EditorSubScene::_path_browse() {
  58. file_dialog->popup_centered_ratio();
  59. }
  60. void EditorSubScene::_notification(int p_what) {
  61. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  62. if (is_visible() && scene == nullptr) {
  63. _path_browse();
  64. }
  65. }
  66. }
  67. void EditorSubScene::_fill_tree(Node *p_node, TreeItem *p_parent) {
  68. TreeItem *it = tree->create_item(p_parent);
  69. it->set_metadata(0, p_node);
  70. it->set_text(0, p_node->get_name());
  71. it->set_editable(0, false);
  72. it->set_selectable(0, true);
  73. it->set_icon(0, EditorNode::get_singleton()->get_object_icon(p_node, "Node"));
  74. for (int i = 0; i < p_node->get_child_count(); i++) {
  75. Node *c = p_node->get_child(i);
  76. if (c->get_owner() != scene) {
  77. continue;
  78. }
  79. _fill_tree(c, it);
  80. }
  81. }
  82. void EditorSubScene::_selected_changed() {
  83. TreeItem *item = tree->get_selected();
  84. ERR_FAIL_COND(!item);
  85. Node *n = item->get_metadata(0);
  86. if (!n || !selection.find(n)) {
  87. selection.clear();
  88. is_root = false;
  89. }
  90. }
  91. void EditorSubScene::_item_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  92. if (!is_root) {
  93. TreeItem *item = Object::cast_to<TreeItem>(p_object);
  94. ERR_FAIL_COND(!item);
  95. Node *n = item->get_metadata(0);
  96. if (!n) {
  97. return;
  98. }
  99. if (p_selected) {
  100. if (n == scene) {
  101. is_root = true;
  102. selection.clear();
  103. }
  104. selection.push_back(n);
  105. } else {
  106. List<Node *>::Element *E = selection.find(n);
  107. if (E) {
  108. selection.erase(E);
  109. }
  110. }
  111. }
  112. }
  113. void EditorSubScene::_remove_selection_child(Node *p_node) {
  114. if (p_node->get_child_count() > 0) {
  115. for (int i = 0; i < p_node->get_child_count(); i++) {
  116. Node *c = p_node->get_child(i);
  117. List<Node *>::Element *E = selection.find(c);
  118. if (E) {
  119. selection.move_to_back(E);
  120. selection.pop_back();
  121. }
  122. if (c->get_child_count() > 0) {
  123. _remove_selection_child(c);
  124. }
  125. }
  126. }
  127. }
  128. void EditorSubScene::ok_pressed() {
  129. if (selection.size() <= 0) {
  130. return;
  131. }
  132. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  133. Node *c = E->get();
  134. _remove_selection_child(c);
  135. }
  136. emit_signal("subscene_selected");
  137. hide();
  138. clear();
  139. }
  140. void EditorSubScene::_reown(Node *p_node, List<Node *> *p_to_reown) {
  141. if (p_node == scene) {
  142. scene->set_filename("");
  143. p_to_reown->push_back(p_node);
  144. } else if (p_node->get_owner() == scene) {
  145. p_to_reown->push_back(p_node);
  146. }
  147. for (int i = 0; i < p_node->get_child_count(); i++) {
  148. Node *c = p_node->get_child(i);
  149. _reown(c, p_to_reown);
  150. }
  151. }
  152. void EditorSubScene::move(Node *p_new_parent, Node *p_new_owner) {
  153. if (!scene) {
  154. return;
  155. }
  156. if (selection.size() <= 0) {
  157. return;
  158. }
  159. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  160. Node *selnode = E->get();
  161. if (!selnode) {
  162. return;
  163. }
  164. List<Node *> to_reown;
  165. _reown(selnode, &to_reown);
  166. if (selnode != scene) {
  167. selnode->get_parent()->remove_child(selnode);
  168. }
  169. p_new_parent->add_child(selnode);
  170. for (List<Node *>::Element *F = to_reown.front(); F; F = F->next()) {
  171. F->get()->set_owner(p_new_owner);
  172. }
  173. }
  174. if (!is_root) {
  175. memdelete(scene);
  176. }
  177. scene = nullptr;
  178. //return selnode;
  179. }
  180. void EditorSubScene::clear() {
  181. path->set_text("");
  182. _path_changed("");
  183. }
  184. void EditorSubScene::_bind_methods() {
  185. ClassDB::bind_method(D_METHOD("_path_selected"), &EditorSubScene::_path_selected);
  186. ClassDB::bind_method(D_METHOD("_path_changed"), &EditorSubScene::_path_changed);
  187. ClassDB::bind_method(D_METHOD("_path_browse"), &EditorSubScene::_path_browse);
  188. ClassDB::bind_method(D_METHOD("_item_multi_selected"), &EditorSubScene::_item_multi_selected);
  189. ClassDB::bind_method(D_METHOD("_selected_changed"), &EditorSubScene::_selected_changed);
  190. ADD_SIGNAL(MethodInfo("subscene_selected"));
  191. }
  192. EditorSubScene::EditorSubScene() {
  193. scene = nullptr;
  194. is_root = false;
  195. set_title(TTR("Select Node(s) to Import"));
  196. set_hide_on_ok(false);
  197. VBoxContainer *vb = memnew(VBoxContainer);
  198. add_child(vb);
  199. //set_child_rect(vb);
  200. HBoxContainer *hb = memnew(HBoxContainer);
  201. path = memnew(LineEdit);
  202. path->connect("text_entered", this, "_path_changed");
  203. hb->add_child(path);
  204. path->set_h_size_flags(SIZE_EXPAND_FILL);
  205. Button *b = memnew(Button);
  206. b->set_text(TTR("Browse"));
  207. hb->add_child(b);
  208. b->connect("pressed", this, "_path_browse");
  209. vb->add_margin_child(TTR("Scene Path:"), hb);
  210. tree = memnew(Tree);
  211. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  212. vb->add_margin_child(TTR("Import From Node:"), tree, true);
  213. tree->set_select_mode(Tree::SELECT_MULTI);
  214. tree->connect("multi_selected", this, "_item_multi_selected");
  215. //tree->connect("nothing_selected", this, "_deselect_items");
  216. tree->connect("cell_selected", this, "_selected_changed");
  217. tree->connect("item_activated", this, "_ok", make_binds(), CONNECT_DEFERRED);
  218. file_dialog = memnew(EditorFileDialog);
  219. List<String> extensions;
  220. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
  221. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  222. file_dialog->add_filter("*." + E->get());
  223. }
  224. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  225. add_child(file_dialog);
  226. file_dialog->connect("file_selected", this, "_path_selected");
  227. }