editor_sub_scene.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*************************************************************************/
  2. /* editor_sub_scene.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "editor_sub_scene.h"
  30. #include "scene/gui/margin_container.h"
  31. #include "scene/resources/packed_scene.h"
  32. void EditorSubScene::_path_selected(const String& p_path) {
  33. path->set_text(p_path);
  34. _path_changed(p_path);
  35. }
  36. void EditorSubScene::_path_changed(const String& p_path) {
  37. tree->clear();
  38. if (scene) {
  39. memdelete(scene);
  40. scene=NULL;
  41. }
  42. if (p_path=="")
  43. return;
  44. Ref<PackedScene> ps = ResourceLoader::load(p_path,"PackedScene");
  45. if (ps.is_null())
  46. return;
  47. scene = ps->instance();
  48. if (!scene)
  49. return;
  50. _fill_tree(scene,NULL);
  51. }
  52. void EditorSubScene::_path_browse() {
  53. file_dialog->popup_centered_ratio();
  54. }
  55. void EditorSubScene::_notification(int p_what) {
  56. if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  57. if (!is_visible()) {
  58. }
  59. }
  60. }
  61. void EditorSubScene::_fill_tree(Node* p_node,TreeItem *p_parent) {
  62. TreeItem *it = tree->create_item(p_parent);
  63. it->set_metadata(0,p_node);
  64. it->set_text(0,p_node->get_name());
  65. it->set_editable(0,false);
  66. it->set_selectable(0,true);
  67. if (has_icon(p_node->get_type(),"EditorIcons")) {
  68. it->set_icon(0,get_icon(p_node->get_type(),"EditorIcons"));
  69. }
  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::ok_pressed() {
  78. TreeItem *s = tree->get_selected();
  79. if (!s)
  80. return;
  81. Node *selnode = s->get_metadata(0);
  82. if (!selnode)
  83. return;
  84. emit_signal("subscene_selected");
  85. hide();
  86. clear();
  87. }
  88. void EditorSubScene::_reown(Node* p_node,List<Node*> *p_to_reown) {
  89. if (p_node==scene) {
  90. scene->set_filename("");
  91. p_to_reown->push_back(p_node);
  92. } else if (p_node->get_owner()==scene){
  93. p_to_reown->push_back(p_node);
  94. }
  95. for(int i=0;i<p_node->get_child_count();i++) {
  96. Node *c=p_node->get_child(i);
  97. _reown(c,p_to_reown);
  98. }
  99. }
  100. void EditorSubScene::move(Node* p_new_parent, Node* p_new_owner) {
  101. if (!scene) {
  102. return;
  103. }
  104. TreeItem *s = tree->get_selected();
  105. if (!s) {
  106. return;
  107. }
  108. Node *selnode = s->get_metadata(0);
  109. if (!selnode) {
  110. return;
  111. }
  112. List<Node*> to_reown;
  113. _reown(selnode,&to_reown);
  114. if (selnode!=scene) {
  115. selnode->get_parent()->remove_child(selnode);
  116. }
  117. p_new_parent->add_child(selnode);
  118. for (List<Node*>::Element *E=to_reown.front();E;E=E->next()) {
  119. E->get()->set_owner(p_new_owner);
  120. }
  121. if (selnode!=scene) {
  122. memdelete(scene);
  123. }
  124. scene=NULL;
  125. //return selnode;
  126. }
  127. void EditorSubScene::clear() {
  128. path->set_text("");
  129. _path_changed("");
  130. }
  131. void EditorSubScene::_bind_methods() {
  132. ObjectTypeDB::bind_method(_MD("_path_selected"),&EditorSubScene::_path_selected);
  133. ObjectTypeDB::bind_method(_MD("_path_changed"),&EditorSubScene::_path_changed);
  134. ObjectTypeDB::bind_method(_MD("_path_browse"),&EditorSubScene::_path_browse);
  135. ADD_SIGNAL( MethodInfo("subscene_selected"));
  136. }
  137. EditorSubScene::EditorSubScene() {
  138. set_title("Select Sub-Scene..");
  139. VBoxContainer *vb = memnew( VBoxContainer );
  140. add_child(vb);
  141. set_child_rect(vb);
  142. HBoxContainer *hb = memnew( HBoxContainer );
  143. path = memnew( LineEdit );
  144. path->connect("text_entered",this,"_path_changed");
  145. hb->add_child(path);
  146. path->set_h_size_flags(SIZE_EXPAND_FILL);
  147. Button *b = memnew( Button );
  148. b->set_text(" .. ");
  149. hb->add_child(b);
  150. b->connect("pressed",this,"_path_browse");
  151. vb->add_margin_child("Scene Path:",hb);
  152. tree = memnew( Tree );
  153. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  154. vb->add_margin_child("Import From Node:",tree)->set_v_size_flags(SIZE_EXPAND_FILL);
  155. file_dialog = memnew( FileDialog );
  156. List<String> extensions;
  157. ResourceLoader::get_recognized_extensions_for_type("PackedScene",&extensions);
  158. for(List<String>::Element *E = extensions.front();E;E=E->next() ) {
  159. file_dialog->add_filter("*."+E->get());
  160. }
  161. file_dialog->set_mode(FileDialog::MODE_OPEN_FILE);
  162. add_child(file_dialog);
  163. file_dialog->connect("file_selected",this,"_path_selected");
  164. scene=NULL;
  165. set_hide_on_ok(false);
  166. }