editor_sub_scene.cpp 7.5 KB

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