editor_sub_scene.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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-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 "editor_sub_scene.h"
  31. #include "scene/gui/margin_container.h"
  32. #include "scene/resources/packed_scene.h"
  33. void EditorSubScene::_path_selected(const String &p_path) {
  34. path->set_text(p_path);
  35. _path_changed(p_path);
  36. }
  37. void EditorSubScene::_path_changed(const String &p_path) {
  38. tree->clear();
  39. if (scene) {
  40. memdelete(scene);
  41. scene = NULL;
  42. }
  43. if (p_path == "")
  44. return;
  45. Ref<PackedScene> ps = ResourceLoader::load(p_path, "PackedScene");
  46. if (ps.is_null())
  47. return;
  48. scene = ps->instance();
  49. if (!scene)
  50. return;
  51. _fill_tree(scene, NULL);
  52. }
  53. void EditorSubScene::_path_browse() {
  54. file_dialog->popup_centered_ratio();
  55. }
  56. void EditorSubScene::_notification(int p_what) {
  57. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  58. if (!is_visible()) {
  59. }
  60. }
  61. }
  62. void EditorSubScene::_fill_tree(Node *p_node, TreeItem *p_parent) {
  63. TreeItem *it = tree->create_item(p_parent);
  64. it->set_metadata(0, p_node);
  65. it->set_text(0, p_node->get_name());
  66. it->set_editable(0, false);
  67. it->set_selectable(0, true);
  68. if (has_icon(p_node->get_type(), "EditorIcons")) {
  69. it->set_icon(0, get_icon(p_node->get_type(), "EditorIcons"));
  70. }
  71. for (int i = 0; i < p_node->get_child_count(); i++) {
  72. Node *c = p_node->get_child(i);
  73. if (c->get_owner() != scene)
  74. continue;
  75. _fill_tree(c, it);
  76. }
  77. }
  78. void EditorSubScene::ok_pressed() {
  79. TreeItem *s = tree->get_selected();
  80. if (!s)
  81. return;
  82. Node *selnode = s->get_metadata(0);
  83. if (!selnode)
  84. return;
  85. emit_signal("subscene_selected");
  86. hide();
  87. clear();
  88. }
  89. void EditorSubScene::_reown(Node *p_node, List<Node *> *p_to_reown) {
  90. if (p_node == scene) {
  91. scene->set_filename("");
  92. p_to_reown->push_back(p_node);
  93. } else if (p_node->get_owner() == scene) {
  94. p_to_reown->push_back(p_node);
  95. }
  96. for (int i = 0; i < p_node->get_child_count(); i++) {
  97. Node *c = p_node->get_child(i);
  98. _reown(c, p_to_reown);
  99. }
  100. }
  101. void EditorSubScene::move(Node *p_new_parent, Node *p_new_owner) {
  102. if (!scene) {
  103. return;
  104. }
  105. TreeItem *s = tree->get_selected();
  106. if (!s) {
  107. return;
  108. }
  109. Node *selnode = s->get_metadata(0);
  110. if (!selnode) {
  111. return;
  112. }
  113. List<Node *> to_reown;
  114. _reown(selnode, &to_reown);
  115. if (selnode != scene) {
  116. selnode->get_parent()->remove_child(selnode);
  117. }
  118. p_new_parent->add_child(selnode);
  119. for (List<Node *>::Element *E = to_reown.front(); E; E = E->next()) {
  120. E->get()->set_owner(p_new_owner);
  121. }
  122. if (selnode != scene) {
  123. memdelete(scene);
  124. }
  125. scene = NULL;
  126. //return selnode;
  127. }
  128. void EditorSubScene::clear() {
  129. path->set_text("");
  130. _path_changed("");
  131. }
  132. void EditorSubScene::_bind_methods() {
  133. ObjectTypeDB::bind_method(_MD("_path_selected"), &EditorSubScene::_path_selected);
  134. ObjectTypeDB::bind_method(_MD("_path_changed"), &EditorSubScene::_path_changed);
  135. ObjectTypeDB::bind_method(_MD("_path_browse"), &EditorSubScene::_path_browse);
  136. ADD_SIGNAL(MethodInfo("subscene_selected"));
  137. }
  138. EditorSubScene::EditorSubScene() {
  139. scene = NULL;
  140. set_title(TTR("Select Node(s) to Import"));
  141. set_hide_on_ok(false);
  142. VBoxContainer *vb = memnew(VBoxContainer);
  143. add_child(vb);
  144. set_child_rect(vb);
  145. HBoxContainer *hb = memnew(HBoxContainer);
  146. path = memnew(LineEdit);
  147. path->connect("text_entered", this, "_path_changed");
  148. hb->add_child(path);
  149. path->set_h_size_flags(SIZE_EXPAND_FILL);
  150. Button *b = memnew(Button);
  151. b->set_text(" .. ");
  152. hb->add_child(b);
  153. b->connect("pressed", this, "_path_browse");
  154. vb->add_margin_child(TTR("Scene Path:"), hb);
  155. tree = memnew(Tree);
  156. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  157. vb->add_margin_child(TTR("Import From Node:"), tree, true);
  158. tree->connect("item_activated", this, "_ok", make_binds(), CONNECT_DEFERRED);
  159. file_dialog = memnew(EditorFileDialog);
  160. List<String> extensions;
  161. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
  162. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  163. file_dialog->add_filter("*." + E->get());
  164. }
  165. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  166. add_child(file_dialog);
  167. file_dialog->connect("file_selected", this, "_path_selected");
  168. }