scene_tree_dock.cpp 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  1. /*************************************************************************/
  2. /* scene_tree_dock.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 "scene_tree_dock.h"
  31. #include "core/io/resource_saver.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/project_settings.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/multi_node_edit.h"
  37. #include "editor/plugins/animation_player_editor_plugin.h"
  38. #include "editor/plugins/canvas_item_editor_plugin.h"
  39. #include "editor/plugins/script_editor_plugin.h"
  40. #include "editor/plugins/spatial_editor_plugin.h"
  41. #include "editor/script_editor_debugger.h"
  42. #include "scene/main/viewport.h"
  43. #include "scene/resources/packed_scene.h"
  44. void SceneTreeDock::_nodes_drag_begin() {
  45. if (restore_script_editor_on_drag) {
  46. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  47. restore_script_editor_on_drag = false;
  48. }
  49. }
  50. void SceneTreeDock::_quick_open() {
  51. Vector<String> files = quick_open->get_selected_files();
  52. for (int i = 0; i < files.size(); i++) {
  53. instance(files[i]);
  54. }
  55. }
  56. void SceneTreeDock::_input(Ref<InputEvent> p_event) {
  57. Ref<InputEventMouseButton> mb = p_event;
  58. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  59. restore_script_editor_on_drag = false; //lost chance
  60. }
  61. }
  62. void SceneTreeDock::_unhandled_key_input(Ref<InputEvent> p_event) {
  63. if (get_viewport()->get_modal_stack_top())
  64. return; //ignore because of modal window
  65. if (get_focus_owner() && get_focus_owner()->is_text_field())
  66. return;
  67. if (!p_event->is_pressed() || p_event->is_echo())
  68. return;
  69. if (ED_IS_SHORTCUT("scene_tree/batch_rename", p_event)) {
  70. _tool_selected(TOOL_BATCH_RENAME);
  71. } else if (ED_IS_SHORTCUT("scene_tree/rename", p_event)) {
  72. _tool_selected(TOOL_RENAME);
  73. } else if (ED_IS_SHORTCUT("scene_tree/add_child_node", p_event)) {
  74. _tool_selected(TOOL_NEW);
  75. } else if (ED_IS_SHORTCUT("scene_tree/instance_scene", p_event)) {
  76. _tool_selected(TOOL_INSTANCE);
  77. } else if (ED_IS_SHORTCUT("scene_tree/change_node_type", p_event)) {
  78. _tool_selected(TOOL_REPLACE);
  79. } else if (ED_IS_SHORTCUT("scene_tree/duplicate", p_event)) {
  80. _tool_selected(TOOL_DUPLICATE);
  81. } else if (ED_IS_SHORTCUT("scene_tree/attach_script", p_event)) {
  82. _tool_selected(TOOL_ATTACH_SCRIPT);
  83. } else if (ED_IS_SHORTCUT("scene_tree/clear_script", p_event)) {
  84. _tool_selected(TOOL_CLEAR_SCRIPT);
  85. } else if (ED_IS_SHORTCUT("scene_tree/move_up", p_event)) {
  86. _tool_selected(TOOL_MOVE_UP);
  87. } else if (ED_IS_SHORTCUT("scene_tree/move_down", p_event)) {
  88. _tool_selected(TOOL_MOVE_DOWN);
  89. } else if (ED_IS_SHORTCUT("scene_tree/reparent", p_event)) {
  90. _tool_selected(TOOL_REPARENT);
  91. } else if (ED_IS_SHORTCUT("scene_tree/merge_from_scene", p_event)) {
  92. _tool_selected(TOOL_MERGE_FROM_SCENE);
  93. } else if (ED_IS_SHORTCUT("scene_tree/save_branch_as_scene", p_event)) {
  94. _tool_selected(TOOL_NEW_SCENE_FROM);
  95. } else if (ED_IS_SHORTCUT("scene_tree/delete_no_confirm", p_event)) {
  96. _tool_selected(TOOL_ERASE, true);
  97. } else if (ED_IS_SHORTCUT("scene_tree/copy_node_path", p_event)) {
  98. _tool_selected(TOOL_COPY_NODE_PATH);
  99. } else if (ED_IS_SHORTCUT("scene_tree/delete", p_event)) {
  100. _tool_selected(TOOL_ERASE);
  101. }
  102. }
  103. void SceneTreeDock::instance(const String &p_file) {
  104. Node *parent = scene_tree->get_selected();
  105. if (!parent) {
  106. parent = edited_scene;
  107. };
  108. if (!edited_scene) {
  109. current_option = -1;
  110. accept->set_text(TTR("No parent to instance a child at."));
  111. accept->popup_centered_minsize();
  112. return;
  113. };
  114. ERR_FAIL_COND(!parent);
  115. Vector<String> scenes;
  116. scenes.push_back(p_file);
  117. _perform_instance_scenes(scenes, parent, -1);
  118. }
  119. void SceneTreeDock::instance_scenes(const Vector<String> &p_files, Node *p_parent) {
  120. Node *parent = p_parent;
  121. if (!parent) {
  122. parent = scene_tree->get_selected();
  123. }
  124. if (!parent || !edited_scene) {
  125. accept->set_text(TTR("No parent to instance the scenes at."));
  126. accept->popup_centered_minsize();
  127. return;
  128. };
  129. _perform_instance_scenes(p_files, parent, -1);
  130. }
  131. void SceneTreeDock::_perform_instance_scenes(const Vector<String> &p_files, Node *parent, int p_pos) {
  132. ERR_FAIL_COND(!parent);
  133. Vector<Node *> instances;
  134. bool error = false;
  135. for (int i = 0; i < p_files.size(); i++) {
  136. Ref<PackedScene> sdata = ResourceLoader::load(p_files[i]);
  137. if (!sdata.is_valid()) {
  138. current_option = -1;
  139. accept->set_text(vformat(TTR("Error loading scene from %s"), p_files[i]));
  140. accept->popup_centered_minsize();
  141. error = true;
  142. break;
  143. }
  144. Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  145. if (!instanced_scene) {
  146. current_option = -1;
  147. accept->set_text(vformat(TTR("Error instancing scene from %s"), p_files[i]));
  148. accept->popup_centered_minsize();
  149. error = true;
  150. break;
  151. }
  152. if (edited_scene->get_filename() != "") {
  153. if (_cyclical_dependency_exists(edited_scene->get_filename(), instanced_scene)) {
  154. accept->set_text(vformat(TTR("Cannot instance the scene '%s' because the current scene exists within one of its nodes."), p_files[i]));
  155. accept->popup_centered_minsize();
  156. error = true;
  157. break;
  158. }
  159. }
  160. instanced_scene->set_filename(ProjectSettings::get_singleton()->localize_path(p_files[i]));
  161. instances.push_back(instanced_scene);
  162. }
  163. if (error) {
  164. for (int i = 0; i < instances.size(); i++) {
  165. memdelete(instances[i]);
  166. }
  167. return;
  168. }
  169. editor_data->get_undo_redo().create_action(TTR("Instance Scene(s)"));
  170. for (int i = 0; i < instances.size(); i++) {
  171. Node *instanced_scene = instances[i];
  172. editor_data->get_undo_redo().add_do_method(parent, "add_child", instanced_scene);
  173. if (p_pos >= 0) {
  174. editor_data->get_undo_redo().add_do_method(parent, "move_child", instanced_scene, p_pos + i);
  175. }
  176. editor_data->get_undo_redo().add_do_method(instanced_scene, "set_owner", edited_scene);
  177. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  178. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", instanced_scene);
  179. editor_data->get_undo_redo().add_do_reference(instanced_scene);
  180. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", instanced_scene);
  181. String new_name = parent->validate_child_name(instanced_scene);
  182. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  183. editor_data->get_undo_redo().add_do_method(sed, "live_debug_instance_node", edited_scene->get_path_to(parent), p_files[i], new_name);
  184. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)).plus_file(new_name)));
  185. }
  186. editor_data->get_undo_redo().commit_action();
  187. }
  188. void SceneTreeDock::_replace_with_branch_scene(const String &p_file, Node *base) {
  189. Ref<PackedScene> sdata = ResourceLoader::load(p_file);
  190. if (!sdata.is_valid()) {
  191. accept->set_text(vformat(TTR("Error loading scene from %s"), p_file));
  192. accept->popup_centered_minsize();
  193. return;
  194. }
  195. Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  196. if (!instanced_scene) {
  197. accept->set_text(vformat(TTR("Error instancing scene from %s"), p_file));
  198. accept->popup_centered_minsize();
  199. return;
  200. }
  201. Node *parent = base->get_parent();
  202. int pos = base->get_index();
  203. parent->remove_child(base);
  204. parent->add_child(instanced_scene);
  205. parent->move_child(instanced_scene, pos);
  206. instanced_scene->set_owner(edited_scene);
  207. editor_selection->clear();
  208. editor_selection->add_node(instanced_scene);
  209. scene_tree->set_selected(instanced_scene);
  210. // Delete the node as late as possible because before another one is selected
  211. // an editor plugin could be referencing it to do something with it before
  212. // switching to another (or to none); and since some steps of changing the
  213. // editor state are deferred, the safest thing is to do this is as the last
  214. // step of this function and also by enqueing instead of memdelete()-ing it here
  215. base->queue_delete();
  216. }
  217. bool SceneTreeDock::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
  218. int childCount = p_desired_node->get_child_count();
  219. if (p_desired_node->get_filename() == p_target_scene_path) {
  220. return true;
  221. }
  222. for (int i = 0; i < childCount; i++) {
  223. Node *child = p_desired_node->get_child(i);
  224. if (_cyclical_dependency_exists(p_target_scene_path, child)) {
  225. return true;
  226. }
  227. }
  228. return false;
  229. }
  230. void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
  231. current_option = p_tool;
  232. switch (p_tool) {
  233. case TOOL_BATCH_RENAME: {
  234. if (!profile_allow_editing) {
  235. break;
  236. }
  237. Tree *tree = scene_tree->get_scene_tree();
  238. if (tree->is_anything_selected()) {
  239. rename_dialog->popup_centered();
  240. }
  241. } break;
  242. case TOOL_RENAME: {
  243. if (!profile_allow_editing) {
  244. break;
  245. }
  246. Tree *tree = scene_tree->get_scene_tree();
  247. if (tree->is_anything_selected()) {
  248. tree->grab_focus();
  249. tree->edit_selected();
  250. }
  251. } break;
  252. case TOOL_NEW: {
  253. if (!profile_allow_editing) {
  254. break;
  255. }
  256. String preferred = "";
  257. Node *current_edited_scene_root = EditorNode::get_singleton()->get_edited_scene();
  258. if (current_edited_scene_root) {
  259. if (ClassDB::is_parent_class(current_edited_scene_root->get_class_name(), "Node2D"))
  260. preferred = "Node2D";
  261. else if (ClassDB::is_parent_class(current_edited_scene_root->get_class_name(), "Spatial"))
  262. preferred = "Spatial";
  263. }
  264. create_dialog->set_preferred_search_result_type(preferred);
  265. create_dialog->popup_create(true);
  266. } break;
  267. case TOOL_INSTANCE: {
  268. if (!profile_allow_editing) {
  269. break;
  270. }
  271. Node *scene = edited_scene;
  272. if (!scene) {
  273. EditorNode::get_singleton()->new_inherited_scene();
  274. break;
  275. }
  276. quick_open->popup_dialog("PackedScene", true);
  277. quick_open->set_title(TTR("Instance Child Scene"));
  278. } break;
  279. case TOOL_REPLACE: {
  280. if (!profile_allow_editing) {
  281. break;
  282. }
  283. create_dialog->popup_create(false, true);
  284. } break;
  285. case TOOL_ATTACH_SCRIPT: {
  286. if (!profile_allow_script_editing) {
  287. break;
  288. }
  289. List<Node *> selection = editor_selection->get_selected_node_list();
  290. if (selection.empty())
  291. break;
  292. Node *selected = scene_tree->get_selected();
  293. if (!selected)
  294. selected = selection.front()->get();
  295. Ref<Script> existing = selected->get_script();
  296. String path = selected->get_filename();
  297. if (path == "") {
  298. String root_path = editor_data->get_edited_scene_root()->get_filename();
  299. if (root_path == "") {
  300. path = String("res://").plus_file(selected->get_name());
  301. } else {
  302. path = root_path.get_base_dir().plus_file(selected->get_name());
  303. }
  304. }
  305. String inherits = selected->get_class();
  306. if (existing.is_valid()) {
  307. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  308. ScriptLanguage *l = ScriptServer::get_language(i);
  309. if (l->get_type() == existing->get_class()) {
  310. String name = l->get_global_class_name(existing->get_path());
  311. if (ScriptServer::is_global_class(name) && EDITOR_GET("interface/editors/derive_script_globals_by_name").operator bool()) {
  312. inherits = name;
  313. } else if (l->can_inherit_from_file()) {
  314. inherits = "\"" + existing->get_path() + "\"";
  315. }
  316. break;
  317. }
  318. }
  319. }
  320. script_create_dialog->config(inherits, path);
  321. script_create_dialog->popup_centered();
  322. } break;
  323. case TOOL_CLEAR_SCRIPT: {
  324. if (!profile_allow_script_editing) {
  325. break;
  326. }
  327. Array selection = editor_selection->get_selected_nodes();
  328. if (selection.empty())
  329. return;
  330. editor_data->get_undo_redo().create_action(TTR("Clear Script"));
  331. editor_data->get_undo_redo().add_do_method(editor, "push_item", (Script *)NULL);
  332. for (int i = 0; i < selection.size(); i++) {
  333. Node *n = Object::cast_to<Node>(selection[i]);
  334. Ref<Script> existing = n->get_script();
  335. if (existing.is_valid()) {
  336. const RefPtr empty;
  337. editor_data->get_undo_redo().add_do_method(n, "set_script", empty);
  338. editor_data->get_undo_redo().add_undo_method(n, "set_script", existing);
  339. }
  340. }
  341. editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
  342. editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
  343. editor_data->get_undo_redo().commit_action();
  344. } break;
  345. case TOOL_MOVE_UP:
  346. case TOOL_MOVE_DOWN: {
  347. if (!profile_allow_editing) {
  348. break;
  349. }
  350. if (!scene_tree->get_selected())
  351. break;
  352. if (scene_tree->get_selected() == edited_scene) {
  353. current_option = -1;
  354. accept->set_text(TTR("This operation can't be done on the tree root."));
  355. accept->popup_centered_minsize();
  356. break;
  357. }
  358. if (!_validate_no_foreign())
  359. break;
  360. bool MOVING_DOWN = (p_tool == TOOL_MOVE_DOWN);
  361. bool MOVING_UP = !MOVING_DOWN;
  362. Node *common_parent = scene_tree->get_selected()->get_parent();
  363. List<Node *> selection = editor_selection->get_selected_node_list();
  364. selection.sort_custom<Node::Comparator>(); // sort by index
  365. if (MOVING_DOWN)
  366. selection.invert();
  367. int lowest_id = common_parent->get_child_count() - 1;
  368. int highest_id = 0;
  369. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  370. int index = E->get()->get_index();
  371. if (index > highest_id) highest_id = index;
  372. if (index < lowest_id) lowest_id = index;
  373. if (E->get()->get_parent() != common_parent)
  374. common_parent = NULL;
  375. }
  376. if (!common_parent || (MOVING_DOWN && highest_id >= common_parent->get_child_count() - MOVING_DOWN) || (MOVING_UP && lowest_id == 0))
  377. break; // one or more nodes can not be moved
  378. if (selection.size() == 1) editor_data->get_undo_redo().create_action(TTR("Move Node In Parent"));
  379. if (selection.size() > 1) editor_data->get_undo_redo().create_action(TTR("Move Nodes In Parent"));
  380. for (int i = 0; i < selection.size(); i++) {
  381. Node *top_node = selection[i];
  382. Node *bottom_node = selection[selection.size() - 1 - i];
  383. ERR_FAIL_COND(!top_node->get_parent());
  384. ERR_FAIL_COND(!bottom_node->get_parent());
  385. int bottom_node_pos = bottom_node->get_index();
  386. int top_node_pos_next = top_node->get_index() + (MOVING_DOWN ? 1 : -1);
  387. editor_data->get_undo_redo().add_do_method(top_node->get_parent(), "move_child", top_node, top_node_pos_next);
  388. editor_data->get_undo_redo().add_undo_method(bottom_node->get_parent(), "move_child", bottom_node, bottom_node_pos);
  389. }
  390. editor_data->get_undo_redo().commit_action();
  391. } break;
  392. case TOOL_DUPLICATE: {
  393. if (!profile_allow_editing) {
  394. break;
  395. }
  396. if (!edited_scene)
  397. break;
  398. if (editor_selection->is_selected(edited_scene)) {
  399. current_option = -1;
  400. accept->set_text(TTR("This operation can't be done on the tree root."));
  401. accept->popup_centered_minsize();
  402. break;
  403. }
  404. if (!_validate_no_foreign())
  405. break;
  406. List<Node *> selection = editor_selection->get_selected_node_list();
  407. if (selection.size() == 0)
  408. break;
  409. editor_data->get_undo_redo().create_action(TTR("Duplicate Node(s)"));
  410. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  411. Node *dupsingle = NULL;
  412. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  413. Node *node = E->get();
  414. Node *parent = node->get_parent();
  415. List<Node *> owned;
  416. node->get_owned_by(node->get_owner(), &owned);
  417. Map<const Node *, Node *> duplimap;
  418. Node *dup = node->duplicate_from_editor(duplimap);
  419. ERR_CONTINUE(!dup);
  420. if (selection.size() == 1)
  421. dupsingle = dup;
  422. dup->set_name(parent->validate_child_name(dup));
  423. editor_data->get_undo_redo().add_do_method(parent, "add_child_below_node", node, dup);
  424. for (List<Node *>::Element *F = owned.front(); F; F = F->next()) {
  425. if (!duplimap.has(F->get())) {
  426. continue;
  427. }
  428. Node *d = duplimap[F->get()];
  429. editor_data->get_undo_redo().add_do_method(d, "set_owner", node->get_owner());
  430. }
  431. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", dup);
  432. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", dup);
  433. editor_data->get_undo_redo().add_do_reference(dup);
  434. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  435. editor_data->get_undo_redo().add_do_method(sed, "live_debug_duplicate_node", edited_scene->get_path_to(node), dup->get_name());
  436. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)).plus_file(dup->get_name())));
  437. }
  438. editor_data->get_undo_redo().commit_action();
  439. if (dupsingle)
  440. editor->push_item(dupsingle);
  441. } break;
  442. case TOOL_REPARENT: {
  443. if (!profile_allow_editing) {
  444. break;
  445. }
  446. if (!scene_tree->get_selected())
  447. break;
  448. if (editor_selection->is_selected(edited_scene)) {
  449. current_option = -1;
  450. accept->set_text(TTR("This operation can't be done on the tree root."));
  451. accept->popup_centered_minsize();
  452. break;
  453. }
  454. if (!_validate_no_foreign())
  455. break;
  456. List<Node *> nodes = editor_selection->get_selected_node_list();
  457. Set<Node *> nodeset;
  458. for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
  459. nodeset.insert(E->get());
  460. }
  461. reparent_dialog->popup_centered_ratio();
  462. reparent_dialog->set_current(nodeset);
  463. } break;
  464. case TOOL_MAKE_ROOT: {
  465. if (!profile_allow_editing) {
  466. break;
  467. }
  468. List<Node *> nodes = editor_selection->get_selected_node_list();
  469. ERR_FAIL_COND(nodes.size() != 1);
  470. Node *node = nodes.front()->get();
  471. Node *root = get_tree()->get_edited_scene_root();
  472. if (node == root)
  473. return;
  474. //check that from node to root, all owners are right
  475. if (root->get_scene_inherited_state().is_valid()) {
  476. accept->set_text(TTR("Can't reparent nodes in inherited scenes, order of nodes can't change."));
  477. accept->popup_centered_minsize();
  478. return;
  479. }
  480. if (node->get_owner() != root) {
  481. accept->set_text(TTR("Node must belong to the edited scene to become root."));
  482. accept->popup_centered_minsize();
  483. return;
  484. }
  485. if (node->get_filename() != String()) {
  486. accept->set_text(TTR("Instantiated scenes can't become root"));
  487. accept->popup_centered_minsize();
  488. return;
  489. }
  490. editor_data->get_undo_redo().create_action(TTR("Make node as Root"));
  491. editor_data->get_undo_redo().add_do_method(node->get_parent(), "remove_child", node);
  492. editor_data->get_undo_redo().add_do_method(root->get_parent(), "remove_child", root);
  493. editor_data->get_undo_redo().add_do_method(node, "add_child", root);
  494. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", node);
  495. editor_data->get_undo_redo().add_do_method(node, "set_filename", root->get_filename());
  496. editor_data->get_undo_redo().add_do_method(root, "set_filename", String());
  497. editor_data->get_undo_redo().add_do_method(node, "set_owner", (Object *)NULL);
  498. editor_data->get_undo_redo().add_do_method(root, "set_owner", node);
  499. _node_replace_owner(root, root, node, MODE_DO);
  500. editor_data->get_undo_redo().add_undo_method(root, "set_filename", root->get_filename());
  501. editor_data->get_undo_redo().add_undo_method(node, "set_filename", String());
  502. editor_data->get_undo_redo().add_undo_method(node, "remove_child", root);
  503. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", root);
  504. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "add_child", node);
  505. editor_data->get_undo_redo().add_undo_method(root, "set_owner", (Object *)NULL);
  506. editor_data->get_undo_redo().add_undo_method(node, "set_owner", root);
  507. _node_replace_owner(root, root, root, MODE_UNDO);
  508. editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
  509. editor_data->get_undo_redo().add_undo_method(scene_tree, "update_tree");
  510. editor_data->get_undo_redo().add_undo_reference(root);
  511. editor_data->get_undo_redo().commit_action();
  512. } break;
  513. case TOOL_MULTI_EDIT: {
  514. if (!profile_allow_editing) {
  515. break;
  516. }
  517. Node *root = EditorNode::get_singleton()->get_edited_scene();
  518. if (!root)
  519. break;
  520. Ref<MultiNodeEdit> mne = memnew(MultiNodeEdit);
  521. for (const Map<Node *, Object *>::Element *E = EditorNode::get_singleton()->get_editor_selection()->get_selection().front(); E; E = E->next()) {
  522. mne->add_node(root->get_path_to(E->key()));
  523. }
  524. EditorNode::get_singleton()->push_item(mne.ptr());
  525. } break;
  526. case TOOL_ERASE: {
  527. if (!profile_allow_editing) {
  528. break;
  529. }
  530. List<Node *> remove_list = editor_selection->get_selected_node_list();
  531. if (remove_list.empty())
  532. return;
  533. if (!_validate_no_foreign())
  534. break;
  535. if (p_confirm_override) {
  536. _delete_confirm();
  537. } else {
  538. delete_dialog->set_text(TTR("Delete Node(s)?"));
  539. delete_dialog->popup_centered_minsize();
  540. }
  541. } break;
  542. case TOOL_MERGE_FROM_SCENE: {
  543. if (!profile_allow_editing) {
  544. break;
  545. }
  546. EditorNode::get_singleton()->merge_from_scene();
  547. } break;
  548. case TOOL_NEW_SCENE_FROM: {
  549. if (!profile_allow_editing) {
  550. break;
  551. }
  552. Node *scene = editor_data->get_edited_scene_root();
  553. if (!scene) {
  554. accept->set_text(TTR("This operation can't be done without a scene."));
  555. accept->popup_centered_minsize();
  556. break;
  557. }
  558. List<Node *> selection = editor_selection->get_selected_node_list();
  559. if (selection.size() != 1) {
  560. accept->set_text(TTR("This operation requires a single selected node."));
  561. accept->popup_centered_minsize();
  562. break;
  563. }
  564. Node *tocopy = selection.front()->get();
  565. if (tocopy == scene) {
  566. accept->set_text(TTR("Can not perform with the root node."));
  567. accept->popup_centered_minsize();
  568. break;
  569. }
  570. if (tocopy != editor_data->get_edited_scene_root() && tocopy->get_filename() != "") {
  571. accept->set_text(TTR("This operation can't be done on instanced scenes."));
  572. accept->popup_centered_minsize();
  573. break;
  574. }
  575. new_scene_from_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  576. List<String> extensions;
  577. Ref<PackedScene> sd = memnew(PackedScene);
  578. ResourceSaver::get_recognized_extensions(sd, &extensions);
  579. new_scene_from_dialog->clear_filters();
  580. for (int i = 0; i < extensions.size(); i++) {
  581. new_scene_from_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  582. }
  583. String existing;
  584. if (extensions.size()) {
  585. String root_name(tocopy->get_name());
  586. existing = root_name + "." + extensions.front()->get().to_lower();
  587. }
  588. new_scene_from_dialog->set_current_path(existing);
  589. new_scene_from_dialog->popup_centered_ratio();
  590. new_scene_from_dialog->set_title(TTR("Save New Scene As..."));
  591. } break;
  592. case TOOL_COPY_NODE_PATH: {
  593. List<Node *> selection = editor_selection->get_selected_node_list();
  594. List<Node *>::Element *e = selection.front();
  595. if (e) {
  596. Node *node = e->get();
  597. if (node) {
  598. Node *root = EditorNode::get_singleton()->get_edited_scene();
  599. NodePath path = root->get_path().rel_path_to(node->get_path());
  600. OS::get_singleton()->set_clipboard(path);
  601. }
  602. }
  603. } break;
  604. case TOOL_OPEN_DOCUMENTATION: {
  605. List<Node *> selection = editor_selection->get_selected_node_list();
  606. for (int i = 0; i < selection.size(); i++) {
  607. ScriptEditor::get_singleton()->goto_help("class_name:" + selection[i]->get_class());
  608. }
  609. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  610. } break;
  611. case TOOL_SCENE_EDITABLE_CHILDREN: {
  612. if (!profile_allow_editing) {
  613. break;
  614. }
  615. List<Node *> selection = editor_selection->get_selected_node_list();
  616. List<Node *>::Element *e = selection.front();
  617. if (e) {
  618. Node *node = e->get();
  619. if (node) {
  620. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(node);
  621. if (editable) {
  622. editable_instance_remove_dialog->set_text(TTR("Disabling \"editable_instance\" will cause all properties of the node to be reverted to their default."));
  623. editable_instance_remove_dialog->popup_centered_minsize();
  624. break;
  625. }
  626. _toggle_editable_children();
  627. }
  628. }
  629. } break;
  630. case TOOL_SCENE_USE_PLACEHOLDER: {
  631. if (!profile_allow_editing) {
  632. break;
  633. }
  634. List<Node *> selection = editor_selection->get_selected_node_list();
  635. List<Node *>::Element *e = selection.front();
  636. if (e) {
  637. Node *node = e->get();
  638. if (node) {
  639. bool placeholder = node->get_scene_instance_load_placeholder();
  640. placeholder = !placeholder;
  641. int editable_item_idx = menu->get_item_idx_from_text(TTR("Editable Children"));
  642. int placeholder_item_idx = menu->get_item_idx_from_text(TTR("Load As Placeholder"));
  643. if (placeholder)
  644. EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(node, false);
  645. node->set_scene_instance_load_placeholder(placeholder);
  646. menu->set_item_checked(editable_item_idx, false);
  647. menu->set_item_checked(placeholder_item_idx, placeholder);
  648. scene_tree->update_tree();
  649. }
  650. }
  651. } break;
  652. case TOOL_SCENE_MAKE_LOCAL: {
  653. if (!profile_allow_editing) {
  654. break;
  655. }
  656. List<Node *> selection = editor_selection->get_selected_node_list();
  657. List<Node *>::Element *e = selection.front();
  658. if (e) {
  659. Node *node = e->get();
  660. if (node) {
  661. Node *root = EditorNode::get_singleton()->get_edited_scene();
  662. UndoRedo *undo_redo = &editor_data->get_undo_redo();
  663. if (!root)
  664. break;
  665. ERR_FAIL_COND(node->get_filename() == String());
  666. undo_redo->create_action(TTR("Make Local"));
  667. undo_redo->add_do_method(node, "set_filename", "");
  668. undo_redo->add_undo_method(node, "set_filename", node->get_filename());
  669. _node_replace_owner(node, node, root);
  670. undo_redo->add_do_method(scene_tree, "update_tree");
  671. undo_redo->add_undo_method(scene_tree, "update_tree");
  672. undo_redo->commit_action();
  673. }
  674. }
  675. } break;
  676. case TOOL_SCENE_OPEN: {
  677. List<Node *> selection = editor_selection->get_selected_node_list();
  678. List<Node *>::Element *e = selection.front();
  679. if (e) {
  680. Node *node = e->get();
  681. if (node) {
  682. scene_tree->emit_signal("open", node->get_filename());
  683. }
  684. }
  685. } break;
  686. case TOOL_SCENE_CLEAR_INHERITANCE: {
  687. if (!profile_allow_editing) {
  688. break;
  689. }
  690. clear_inherit_confirm->popup_centered_minsize();
  691. } break;
  692. case TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM: {
  693. if (!profile_allow_editing) {
  694. break;
  695. }
  696. List<Node *> selection = editor_selection->get_selected_node_list();
  697. List<Node *>::Element *e = selection.front();
  698. if (e) {
  699. Node *node = e->get();
  700. if (node) {
  701. node->set_scene_inherited_state(Ref<SceneState>());
  702. scene_tree->update_tree();
  703. EditorNode::get_singleton()->get_inspector()->update_tree();
  704. }
  705. }
  706. } break;
  707. case TOOL_SCENE_OPEN_INHERITED: {
  708. List<Node *> selection = editor_selection->get_selected_node_list();
  709. List<Node *>::Element *e = selection.front();
  710. if (e) {
  711. Node *node = e->get();
  712. if (node && node->get_scene_inherited_state().is_valid()) {
  713. scene_tree->emit_signal("open", node->get_scene_inherited_state()->get_path());
  714. }
  715. }
  716. } break;
  717. case TOOL_CREATE_2D_SCENE:
  718. case TOOL_CREATE_3D_SCENE:
  719. case TOOL_CREATE_USER_INTERFACE:
  720. case TOOL_CREATE_FAVORITE: {
  721. Node *new_node = NULL;
  722. if (TOOL_CREATE_FAVORITE == p_tool) {
  723. String name = selected_favorite_root.get_slicec(' ', 0);
  724. if (ScriptServer::is_global_class(name)) {
  725. new_node = Object::cast_to<Node>(ClassDB::instance(ScriptServer::get_global_class_native_base(name)));
  726. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(name), "Script");
  727. if (new_node && script.is_valid()) {
  728. new_node->set_script(script.get_ref_ptr());
  729. new_node->set_name(name);
  730. }
  731. } else {
  732. new_node = Object::cast_to<Node>(ClassDB::instance(selected_favorite_root));
  733. }
  734. if (!new_node) {
  735. ERR_EXPLAIN("Creating root from favorite '" + selected_favorite_root + "' failed. Creating 'Node' instead.");
  736. new_node = memnew(Node);
  737. }
  738. } else {
  739. switch (p_tool) {
  740. case TOOL_CREATE_2D_SCENE: new_node = memnew(Node2D); break;
  741. case TOOL_CREATE_3D_SCENE: new_node = memnew(Spatial); break;
  742. case TOOL_CREATE_USER_INTERFACE: {
  743. Control *node = memnew(Control);
  744. node->set_anchors_and_margins_preset(PRESET_WIDE); //more useful for resizable UIs.
  745. new_node = node;
  746. } break;
  747. }
  748. }
  749. editor_data->get_undo_redo().create_action(TTR("New Scene Root"));
  750. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", new_node);
  751. editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
  752. editor_data->get_undo_redo().add_do_reference(new_node);
  753. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", (Object *)NULL);
  754. editor_data->get_undo_redo().commit_action();
  755. editor->edit_node(new_node);
  756. editor_selection->clear();
  757. editor_selection->add_node(new_node);
  758. } break;
  759. default: {
  760. if (p_tool >= EDIT_SUBRESOURCE_BASE) {
  761. int idx = p_tool - EDIT_SUBRESOURCE_BASE;
  762. ERR_FAIL_INDEX(idx, subresources.size());
  763. Object *obj = ObjectDB::get_instance(subresources[idx]);
  764. ERR_FAIL_COND(!obj);
  765. editor->push_item(obj);
  766. }
  767. }
  768. }
  769. }
  770. void SceneTreeDock::_notification(int p_what) {
  771. switch (p_what) {
  772. case NOTIFICATION_READY: {
  773. if (!first_enter)
  774. break;
  775. first_enter = false;
  776. EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", this, "_feature_profile_changed");
  777. CanvasItemEditorPlugin *canvas_item_plugin = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
  778. if (canvas_item_plugin) {
  779. canvas_item_plugin->get_canvas_item_editor()->connect("item_lock_status_changed", scene_tree, "_update_tree");
  780. canvas_item_plugin->get_canvas_item_editor()->connect("item_group_status_changed", scene_tree, "_update_tree");
  781. scene_tree->connect("node_changed", canvas_item_plugin->get_canvas_item_editor()->get_viewport_control(), "update");
  782. }
  783. SpatialEditorPlugin *spatial_editor_plugin = Object::cast_to<SpatialEditorPlugin>(editor_data->get_editor("3D"));
  784. spatial_editor_plugin->get_spatial_editor()->connect("item_lock_status_changed", scene_tree, "_update_tree");
  785. spatial_editor_plugin->get_spatial_editor()->connect("item_group_status_changed", scene_tree, "_update_tree");
  786. button_add->set_icon(get_icon("Add", "EditorIcons"));
  787. button_instance->set_icon(get_icon("Instance", "EditorIcons"));
  788. button_create_script->set_icon(get_icon("ScriptCreate", "EditorIcons"));
  789. button_clear_script->set_icon(get_icon("ScriptRemove", "EditorIcons"));
  790. filter->set_right_icon(get_icon("Search", "EditorIcons"));
  791. filter->set_clear_button_enabled(true);
  792. EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", this, "_selection_changed");
  793. // create_root_dialog
  794. HBoxContainer *top_row = memnew(HBoxContainer);
  795. top_row->set_name("NodeShortcutsTopRow");
  796. top_row->set_h_size_flags(SIZE_EXPAND_FILL);
  797. top_row->add_child(memnew(Label(TTR("Create Root Node:"))));
  798. top_row->add_spacer();
  799. ToolButton *node_shortcuts_toggle = memnew(ToolButton);
  800. node_shortcuts_toggle->set_name("NodeShortcutsToggle");
  801. node_shortcuts_toggle->set_icon(get_icon("Favorites", "EditorIcons"));
  802. node_shortcuts_toggle->set_toggle_mode(true);
  803. node_shortcuts_toggle->set_pressed(EDITOR_GET("_use_favorites_root_selection"));
  804. node_shortcuts_toggle->set_anchors_and_margins_preset(Control::PRESET_CENTER_RIGHT);
  805. node_shortcuts_toggle->connect("pressed", this, "_update_create_root_dialog");
  806. top_row->add_child(node_shortcuts_toggle);
  807. create_root_dialog->add_child(top_row);
  808. VBoxContainer *node_shortcuts = memnew(VBoxContainer);
  809. node_shortcuts->set_name("NodeShortcuts");
  810. VBoxContainer *beginner_node_shortcuts = memnew(VBoxContainer);
  811. beginner_node_shortcuts->set_name("BeginnerNodeShortcuts");
  812. node_shortcuts->add_child(beginner_node_shortcuts);
  813. Button *button_2d = memnew(Button);
  814. beginner_node_shortcuts->add_child(button_2d);
  815. button_2d->set_text(TTR("2D Scene"));
  816. button_2d->set_icon(get_icon("Node2D", "EditorIcons"));
  817. button_2d->connect("pressed", this, "_tool_selected", make_binds(TOOL_CREATE_2D_SCENE, false));
  818. Button *button_3d = memnew(Button);
  819. beginner_node_shortcuts->add_child(button_3d);
  820. button_3d->set_text(TTR("3D Scene"));
  821. button_3d->set_icon(get_icon("Spatial", "EditorIcons"));
  822. button_3d->connect("pressed", this, "_tool_selected", make_binds(TOOL_CREATE_3D_SCENE, false));
  823. Button *button_ui = memnew(Button);
  824. beginner_node_shortcuts->add_child(button_ui);
  825. button_ui->set_text(TTR("User Interface"));
  826. button_ui->set_icon(get_icon("Control", "EditorIcons"));
  827. button_ui->connect("pressed", this, "_tool_selected", make_binds(TOOL_CREATE_USER_INTERFACE, false));
  828. VBoxContainer *favorite_node_shortcuts = memnew(VBoxContainer);
  829. favorite_node_shortcuts->set_name("FavoriteNodeShortcuts");
  830. node_shortcuts->add_child(favorite_node_shortcuts);
  831. Button *button_custom = memnew(Button);
  832. node_shortcuts->add_child(button_custom);
  833. button_custom->set_text(TTR("Other Node"));
  834. button_custom->set_icon(get_icon("Add", "EditorIcons"));
  835. button_custom->connect("pressed", this, "_tool_selected", make_binds(TOOL_NEW, false));
  836. node_shortcuts->add_spacer();
  837. create_root_dialog->add_child(node_shortcuts);
  838. _update_create_root_dialog();
  839. } break;
  840. case NOTIFICATION_ENTER_TREE: {
  841. clear_inherit_confirm->connect("confirmed", this, "_tool_selected", varray(TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM));
  842. } break;
  843. case NOTIFICATION_EXIT_TREE: {
  844. clear_inherit_confirm->disconnect("confirmed", this, "_tool_selected");
  845. } break;
  846. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  847. button_add->set_icon(get_icon("Add", "EditorIcons"));
  848. button_instance->set_icon(get_icon("Instance", "EditorIcons"));
  849. button_create_script->set_icon(get_icon("ScriptCreate", "EditorIcons"));
  850. button_clear_script->set_icon(get_icon("ScriptRemove", "EditorIcons"));
  851. filter->set_right_icon(get_icon("Search", "EditorIcons"));
  852. filter->set_clear_button_enabled(true);
  853. } break;
  854. case NOTIFICATION_PROCESS: {
  855. bool show_create_root = bool(EDITOR_GET("interface/editors/show_scene_tree_root_selection")) && get_tree()->get_edited_scene_root() == NULL;
  856. if (show_create_root != create_root_dialog->is_visible_in_tree()) {
  857. if (show_create_root) {
  858. create_root_dialog->show();
  859. scene_tree->hide();
  860. } else {
  861. create_root_dialog->hide();
  862. scene_tree->show();
  863. }
  864. }
  865. } break;
  866. }
  867. }
  868. void SceneTreeDock::_node_replace_owner(Node *p_base, Node *p_node, Node *p_root, ReplaceOwnerMode p_mode) {
  869. if (p_node->get_owner() == p_base && p_node != p_root) {
  870. UndoRedo *undo_redo = &editor_data->get_undo_redo();
  871. switch (p_mode) {
  872. case MODE_BIDI: {
  873. undo_redo->add_do_method(p_node, "set_owner", p_root);
  874. undo_redo->add_undo_method(p_node, "set_owner", p_base);
  875. } break;
  876. case MODE_DO: {
  877. undo_redo->add_do_method(p_node, "set_owner", p_root);
  878. } break;
  879. case MODE_UNDO: {
  880. undo_redo->add_undo_method(p_node, "set_owner", p_root);
  881. } break;
  882. }
  883. }
  884. for (int i = 0; i < p_node->get_child_count(); i++) {
  885. _node_replace_owner(p_base, p_node->get_child(i), p_root, p_mode);
  886. }
  887. }
  888. void SceneTreeDock::_load_request(const String &p_path) {
  889. editor->open_request(p_path);
  890. }
  891. void SceneTreeDock::_script_open_request(const Ref<Script> &p_script) {
  892. editor->edit_resource(p_script);
  893. }
  894. void SceneTreeDock::_node_selected() {
  895. Node *node = scene_tree->get_selected();
  896. if (!node) {
  897. editor->push_item(NULL);
  898. return;
  899. }
  900. if (ScriptEditor::get_singleton()->is_visible_in_tree()) {
  901. restore_script_editor_on_drag = true;
  902. }
  903. editor->push_item(node);
  904. }
  905. void SceneTreeDock::_node_renamed() {
  906. _node_selected();
  907. }
  908. void SceneTreeDock::_set_owners(Node *p_owner, const Array &p_nodes) {
  909. for (int i = 0; i < p_nodes.size(); i++) {
  910. Node *n = Object::cast_to<Node>(p_nodes[i]);
  911. if (!n)
  912. continue;
  913. n->set_owner(p_owner);
  914. }
  915. }
  916. void SceneTreeDock::_fill_path_renames(Vector<StringName> base_path, Vector<StringName> new_base_path, Node *p_node, List<Pair<NodePath, NodePath> > *p_renames) {
  917. base_path.push_back(p_node->get_name());
  918. if (new_base_path.size())
  919. new_base_path.push_back(p_node->get_name());
  920. NodePath from(base_path, true);
  921. NodePath to;
  922. if (new_base_path.size())
  923. to = NodePath(new_base_path, true);
  924. Pair<NodePath, NodePath> npp;
  925. npp.first = from;
  926. npp.second = to;
  927. p_renames->push_back(npp);
  928. for (int i = 0; i < p_node->get_child_count(); i++) {
  929. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), p_renames);
  930. }
  931. }
  932. void SceneTreeDock::fill_path_renames(Node *p_node, Node *p_new_parent, List<Pair<NodePath, NodePath> > *p_renames) {
  933. if (!bool(EDITOR_DEF("editors/animation/autorename_animation_tracks", true)))
  934. return;
  935. Vector<StringName> base_path;
  936. Node *n = p_node->get_parent();
  937. while (n) {
  938. base_path.push_back(n->get_name());
  939. n = n->get_parent();
  940. }
  941. base_path.invert();
  942. Vector<StringName> new_base_path;
  943. if (p_new_parent) {
  944. n = p_new_parent;
  945. while (n) {
  946. new_base_path.push_back(n->get_name());
  947. n = n->get_parent();
  948. }
  949. new_base_path.invert();
  950. }
  951. _fill_path_renames(base_path, new_base_path, p_node, p_renames);
  952. }
  953. void SceneTreeDock::perform_node_renames(Node *p_base, List<Pair<NodePath, NodePath> > *p_renames, Map<Ref<Animation>, Set<int> > *r_rem_anims) {
  954. Map<Ref<Animation>, Set<int> > rem_anims;
  955. if (!r_rem_anims)
  956. r_rem_anims = &rem_anims;
  957. if (!p_base) {
  958. p_base = edited_scene;
  959. }
  960. if (!p_base)
  961. return;
  962. // Renaming node paths used in script instances
  963. if (p_base->get_script_instance()) {
  964. ScriptInstance *si = p_base->get_script_instance();
  965. if (si) {
  966. List<PropertyInfo> properties;
  967. si->get_property_list(&properties);
  968. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  969. String propertyname = E->get().name;
  970. Variant p = p_base->get(propertyname);
  971. if (p.get_type() == Variant::NODE_PATH) {
  972. // Goes through all paths to check if its matching
  973. for (List<Pair<NodePath, NodePath> >::Element *F = p_renames->front(); F; F = F->next()) {
  974. NodePath root_path = p_base->get_path();
  975. NodePath rel_path_old = root_path.rel_path_to(F->get().first);
  976. NodePath rel_path_new = F->get().second;
  977. // if not empty, get new relative path
  978. if (F->get().second != NodePath()) {
  979. rel_path_new = root_path.rel_path_to(F->get().second);
  980. }
  981. // if old path detected, then it needs to be replaced with the new one
  982. if (p == rel_path_old) {
  983. editor_data->get_undo_redo().add_do_property(p_base, propertyname, rel_path_new);
  984. editor_data->get_undo_redo().add_undo_property(p_base, propertyname, rel_path_old);
  985. p_base->set(propertyname, rel_path_new);
  986. break;
  987. }
  988. }
  989. }
  990. }
  991. }
  992. }
  993. bool autorename_animation_tracks = bool(EDITOR_DEF("editors/animation/autorename_animation_tracks", true));
  994. if (autorename_animation_tracks && Object::cast_to<AnimationPlayer>(p_base)) {
  995. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_base);
  996. List<StringName> anims;
  997. ap->get_animation_list(&anims);
  998. Node *root = ap->get_node(ap->get_root());
  999. if (root) {
  1000. NodePath root_path = root->get_path();
  1001. NodePath new_root_path = root_path;
  1002. for (List<Pair<NodePath, NodePath> >::Element *E = p_renames->front(); E; E = E->next()) {
  1003. if (E->get().first == root_path) {
  1004. new_root_path = E->get().second;
  1005. break;
  1006. }
  1007. }
  1008. if (new_root_path != NodePath()) {
  1009. //will not be erased
  1010. for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
  1011. Ref<Animation> anim = ap->get_animation(E->get());
  1012. if (!r_rem_anims->has(anim)) {
  1013. r_rem_anims->insert(anim, Set<int>());
  1014. Set<int> &ran = r_rem_anims->find(anim)->get();
  1015. for (int i = 0; i < anim->get_track_count(); i++)
  1016. ran.insert(i);
  1017. }
  1018. Set<int> &ran = r_rem_anims->find(anim)->get();
  1019. if (anim.is_null())
  1020. continue;
  1021. for (int i = 0; i < anim->get_track_count(); i++) {
  1022. NodePath track_np = anim->track_get_path(i);
  1023. Node *n = root->get_node(track_np);
  1024. if (!n) {
  1025. continue;
  1026. }
  1027. NodePath old_np = n->get_path();
  1028. if (!ran.has(i))
  1029. continue; //channel was removed
  1030. for (List<Pair<NodePath, NodePath> >::Element *F = p_renames->front(); F; F = F->next()) {
  1031. if (F->get().first == old_np) {
  1032. if (F->get().second == NodePath()) {
  1033. //will be erased
  1034. int idx = 0;
  1035. Set<int>::Element *EI = ran.front();
  1036. ERR_FAIL_COND(!EI); //bug
  1037. while (EI->get() != i) {
  1038. idx++;
  1039. EI = EI->next();
  1040. ERR_FAIL_COND(!EI); //another bug
  1041. }
  1042. editor_data->get_undo_redo().add_do_method(anim.ptr(), "remove_track", idx);
  1043. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "add_track", anim->track_get_type(i), idx);
  1044. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", idx, track_np);
  1045. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_interpolation_type", idx, anim->track_get_interpolation_type(i));
  1046. for (int j = 0; j < anim->track_get_key_count(i); j++) {
  1047. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_insert_key", idx, anim->track_get_key_time(i, j), anim->track_get_key_value(i, j), anim->track_get_key_transition(i, j));
  1048. }
  1049. ran.erase(i); //byebye channel
  1050. } else {
  1051. //will be renamed
  1052. NodePath rel_path = new_root_path.rel_path_to(F->get().second);
  1053. NodePath new_path = NodePath(rel_path.get_names(), track_np.get_subnames(), false);
  1054. if (new_path == track_np)
  1055. continue; //bleh
  1056. editor_data->get_undo_redo().add_do_method(anim.ptr(), "track_set_path", i, new_path);
  1057. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", i, track_np);
  1058. }
  1059. }
  1060. }
  1061. }
  1062. }
  1063. }
  1064. }
  1065. }
  1066. for (int i = 0; i < p_base->get_child_count(); i++)
  1067. perform_node_renames(p_base->get_child(i), p_renames, r_rem_anims);
  1068. }
  1069. void SceneTreeDock::_node_prerenamed(Node *p_node, const String &p_new_name) {
  1070. List<Pair<NodePath, NodePath> > path_renames;
  1071. Vector<StringName> base_path;
  1072. Node *n = p_node->get_parent();
  1073. while (n) {
  1074. base_path.push_back(n->get_name());
  1075. n = n->get_parent();
  1076. }
  1077. base_path.invert();
  1078. Vector<StringName> new_base_path = base_path;
  1079. base_path.push_back(p_node->get_name());
  1080. new_base_path.push_back(p_new_name);
  1081. Pair<NodePath, NodePath> npp;
  1082. npp.first = NodePath(base_path, true);
  1083. npp.second = NodePath(new_base_path, true);
  1084. path_renames.push_back(npp);
  1085. for (int i = 0; i < p_node->get_child_count(); i++)
  1086. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), &path_renames);
  1087. perform_node_renames(NULL, &path_renames);
  1088. }
  1089. bool SceneTreeDock::_validate_no_foreign() {
  1090. List<Node *> selection = editor_selection->get_selected_node_list();
  1091. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1092. if (E->get() != edited_scene && E->get()->get_owner() != edited_scene) {
  1093. accept->set_text(TTR("Can't operate on nodes from a foreign scene!"));
  1094. accept->popup_centered_minsize();
  1095. return false;
  1096. }
  1097. // When edited_scene inherits from another one the root Node will be the parent Scene,
  1098. // we don't want to consider that Node a foreign one otherwise we would not be able to
  1099. // delete it
  1100. if (edited_scene->get_scene_inherited_state().is_valid() && edited_scene == E->get()) {
  1101. continue;
  1102. }
  1103. if (edited_scene->get_scene_inherited_state().is_valid() && edited_scene->get_scene_inherited_state()->find_node_by_path(edited_scene->get_path_to(E->get())) >= 0) {
  1104. accept->set_text(TTR("Can't operate on nodes the current scene inherits from!"));
  1105. accept->popup_centered_minsize();
  1106. return false;
  1107. }
  1108. }
  1109. return true;
  1110. }
  1111. void SceneTreeDock::_node_reparent(NodePath p_path, bool p_keep_global_xform) {
  1112. Node *new_parent = scene_root->get_node(p_path);
  1113. ERR_FAIL_COND(!new_parent);
  1114. List<Node *> selection = editor_selection->get_selected_node_list();
  1115. if (selection.empty())
  1116. return; //nothing to reparent
  1117. Vector<Node *> nodes;
  1118. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1119. nodes.push_back(E->get());
  1120. }
  1121. _do_reparent(new_parent, -1, nodes, p_keep_global_xform);
  1122. }
  1123. void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, Vector<Node *> p_nodes, bool p_keep_global_xform) {
  1124. Node *new_parent = p_new_parent;
  1125. ERR_FAIL_COND(!new_parent);
  1126. Node *validate = new_parent;
  1127. while (validate) {
  1128. if (p_nodes.find(validate) != -1) {
  1129. ERR_EXPLAIN("Selection changed at some point.. can't reparent");
  1130. ERR_FAIL();
  1131. return;
  1132. }
  1133. validate = validate->get_parent();
  1134. }
  1135. //ok all valid
  1136. if (p_nodes.size() == 0)
  1137. return; //nothing to reparent
  1138. //sort by tree order, so re-adding is easy
  1139. p_nodes.sort_custom<Node::Comparator>();
  1140. editor_data->get_undo_redo().create_action(TTR("Reparent Node"));
  1141. List<Pair<NodePath, NodePath> > path_renames;
  1142. Vector<StringName> former_names;
  1143. int inc = 0;
  1144. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1145. //no undo for now, sorry
  1146. Node *node = p_nodes[ni];
  1147. fill_path_renames(node, new_parent, &path_renames);
  1148. former_names.push_back(node->get_name());
  1149. List<Node *> owned;
  1150. node->get_owned_by(node->get_owner(), &owned);
  1151. Array owners;
  1152. for (List<Node *>::Element *E = owned.front(); E; E = E->next()) {
  1153. owners.push_back(E->get());
  1154. }
  1155. if (new_parent == node->get_parent() && node->get_index() < p_position_in_parent + ni) {
  1156. //if child will generate a gap when moved, adjust
  1157. inc--;
  1158. }
  1159. editor_data->get_undo_redo().add_do_method(node->get_parent(), "remove_child", node);
  1160. editor_data->get_undo_redo().add_do_method(new_parent, "add_child", node);
  1161. if (p_position_in_parent >= 0)
  1162. editor_data->get_undo_redo().add_do_method(new_parent, "move_child", node, p_position_in_parent + inc);
  1163. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  1164. String old_name = former_names[ni];
  1165. String new_name = new_parent->validate_child_name(node);
  1166. // name was modified, fix the path renames
  1167. if (old_name.casecmp_to(new_name) != 0) {
  1168. // Fix the to name to have the new name
  1169. NodePath old_new_name = path_renames[ni].second;
  1170. NodePath new_path;
  1171. Vector<StringName> unfixed_new_names = old_new_name.get_names();
  1172. Vector<StringName> fixed_new_names;
  1173. // Get last name and replace with fixed new name
  1174. for (int a = 0; a < (unfixed_new_names.size() - 1); a++) {
  1175. fixed_new_names.push_back(unfixed_new_names[a]);
  1176. }
  1177. fixed_new_names.push_back(new_name);
  1178. NodePath fixed_node_path = NodePath(fixed_new_names, true);
  1179. path_renames[ni].second = fixed_node_path;
  1180. }
  1181. editor_data->get_undo_redo().add_do_method(sed, "live_debug_reparent_node", edited_scene->get_path_to(node), edited_scene->get_path_to(new_parent), new_name, p_position_in_parent + inc);
  1182. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_reparent_node", NodePath(String(edited_scene->get_path_to(new_parent)).plus_file(new_name)), edited_scene->get_path_to(node->get_parent()), node->get_name(), node->get_index());
  1183. if (p_keep_global_xform) {
  1184. if (Object::cast_to<Node2D>(node))
  1185. editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Node2D>(node)->get_global_transform());
  1186. if (Object::cast_to<Spatial>(node))
  1187. editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Spatial>(node)->get_global_transform());
  1188. if (Object::cast_to<Control>(node))
  1189. editor_data->get_undo_redo().add_do_method(node, "set_global_position", Object::cast_to<Control>(node)->get_global_position());
  1190. }
  1191. editor_data->get_undo_redo().add_do_method(this, "_set_owners", edited_scene, owners);
  1192. if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == node)
  1193. editor_data->get_undo_redo().add_do_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", node);
  1194. editor_data->get_undo_redo().add_undo_method(new_parent, "remove_child", node);
  1195. editor_data->get_undo_redo().add_undo_method(node, "set_name", former_names[ni]);
  1196. inc++;
  1197. }
  1198. //add and move in a second step.. (so old order is preserved)
  1199. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1200. Node *node = p_nodes[ni];
  1201. List<Node *> owned;
  1202. node->get_owned_by(node->get_owner(), &owned);
  1203. Array owners;
  1204. for (List<Node *>::Element *E = owned.front(); E; E = E->next()) {
  1205. owners.push_back(E->get());
  1206. }
  1207. int child_pos = node->get_position_in_parent();
  1208. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "add_child", node);
  1209. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "move_child", node, child_pos);
  1210. editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
  1211. if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == node)
  1212. editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", node);
  1213. if (p_keep_global_xform) {
  1214. if (Object::cast_to<Node2D>(node))
  1215. editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Node2D>(node)->get_transform());
  1216. if (Object::cast_to<Spatial>(node))
  1217. editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Spatial>(node)->get_transform());
  1218. if (Object::cast_to<Control>(node))
  1219. editor_data->get_undo_redo().add_undo_method(node, "set_position", Object::cast_to<Control>(node)->get_position());
  1220. }
  1221. }
  1222. perform_node_renames(NULL, &path_renames);
  1223. editor_data->get_undo_redo().commit_action();
  1224. }
  1225. void SceneTreeDock::_script_created(Ref<Script> p_script) {
  1226. List<Node *> selected = editor_selection->get_selected_node_list();
  1227. if (selected.empty())
  1228. return;
  1229. editor_data->get_undo_redo().create_action(TTR("Attach Script"));
  1230. for (List<Node *>::Element *E = selected.front(); E; E = E->next()) {
  1231. Ref<Script> existing = E->get()->get_script();
  1232. editor_data->get_undo_redo().add_do_method(E->get(), "set_script", p_script.get_ref_ptr());
  1233. editor_data->get_undo_redo().add_undo_method(E->get(), "set_script", existing);
  1234. editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
  1235. editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
  1236. }
  1237. editor_data->get_undo_redo().commit_action();
  1238. editor->push_item(p_script.operator->());
  1239. _update_script_button();
  1240. }
  1241. void SceneTreeDock::_toggle_editable_children() {
  1242. List<Node *> selection = editor_selection->get_selected_node_list();
  1243. List<Node *>::Element *e = selection.front();
  1244. if (e) {
  1245. Node *node = e->get();
  1246. if (node) {
  1247. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(node);
  1248. int editable_item_idx = menu->get_item_idx_from_text(TTR("Editable Children"));
  1249. int placeholder_item_idx = menu->get_item_idx_from_text(TTR("Load As Placeholder"));
  1250. editable = !editable;
  1251. EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(node, editable);
  1252. menu->set_item_checked(editable_item_idx, editable);
  1253. if (editable) {
  1254. node->set_scene_instance_load_placeholder(false);
  1255. menu->set_item_checked(placeholder_item_idx, false);
  1256. }
  1257. SpatialEditor::get_singleton()->update_all_gizmos(node);
  1258. scene_tree->update_tree();
  1259. }
  1260. }
  1261. }
  1262. void SceneTreeDock::_delete_confirm() {
  1263. List<Node *> remove_list = editor_selection->get_selected_node_list();
  1264. if (remove_list.empty())
  1265. return;
  1266. editor->get_editor_plugins_over()->make_visible(false);
  1267. editor_data->get_undo_redo().create_action(TTR("Remove Node(s)"));
  1268. bool entire_scene = false;
  1269. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  1270. if (E->get() == edited_scene) {
  1271. entire_scene = true;
  1272. }
  1273. }
  1274. if (entire_scene) {
  1275. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", (Object *)NULL);
  1276. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", edited_scene);
  1277. editor_data->get_undo_redo().add_undo_method(edited_scene, "set_owner", edited_scene->get_owner());
  1278. editor_data->get_undo_redo().add_undo_method(scene_tree, "update_tree");
  1279. editor_data->get_undo_redo().add_undo_reference(edited_scene);
  1280. } else {
  1281. remove_list.sort_custom<Node::Comparator>(); //sort nodes to keep positions
  1282. List<Pair<NodePath, NodePath> > path_renames;
  1283. //delete from animation
  1284. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  1285. Node *n = E->get();
  1286. if (!n->is_inside_tree() || !n->get_parent())
  1287. continue;
  1288. fill_path_renames(n, NULL, &path_renames);
  1289. }
  1290. perform_node_renames(NULL, &path_renames);
  1291. //delete for read
  1292. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  1293. Node *n = E->get();
  1294. if (!n->is_inside_tree() || !n->get_parent())
  1295. continue;
  1296. List<Node *> owned;
  1297. n->get_owned_by(n->get_owner(), &owned);
  1298. Array owners;
  1299. for (List<Node *>::Element *F = owned.front(); F; F = F->next()) {
  1300. owners.push_back(F->get());
  1301. }
  1302. editor_data->get_undo_redo().add_do_method(n->get_parent(), "remove_child", n);
  1303. editor_data->get_undo_redo().add_undo_method(n->get_parent(), "add_child", n);
  1304. editor_data->get_undo_redo().add_undo_method(n->get_parent(), "move_child", n, n->get_index());
  1305. if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == n)
  1306. editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", n);
  1307. editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
  1308. editor_data->get_undo_redo().add_undo_reference(n);
  1309. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  1310. editor_data->get_undo_redo().add_do_method(sed, "live_debug_remove_and_keep_node", edited_scene->get_path_to(n), n->get_instance_id());
  1311. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_restore_node", n->get_instance_id(), edited_scene->get_path_to(n->get_parent()), n->get_index());
  1312. }
  1313. }
  1314. editor_data->get_undo_redo().commit_action();
  1315. // hack, force 2d editor viewport to refresh after deletion
  1316. if (CanvasItemEditor *editor = CanvasItemEditor::get_singleton())
  1317. editor->get_viewport_control()->update();
  1318. editor->push_item(NULL);
  1319. // Fixes the EditorHistory from still offering deleted notes
  1320. EditorHistory *editor_history = EditorNode::get_singleton()->get_editor_history();
  1321. editor_history->cleanup_history();
  1322. EditorNode::get_singleton()->get_inspector_dock()->call("_prepare_history");
  1323. }
  1324. void SceneTreeDock::_update_script_button() {
  1325. if (!profile_allow_script_editing) {
  1326. button_create_script->hide();
  1327. button_clear_script->hide();
  1328. } else if (EditorNode::get_singleton()->get_editor_selection()->get_selection().size() == 0) {
  1329. button_create_script->hide();
  1330. button_clear_script->hide();
  1331. } else if (EditorNode::get_singleton()->get_editor_selection()->get_selection().size() == 1) {
  1332. Node *n = EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list()[0];
  1333. if (n->get_script().is_null()) {
  1334. button_create_script->show();
  1335. button_clear_script->hide();
  1336. } else {
  1337. button_create_script->hide();
  1338. button_clear_script->show();
  1339. }
  1340. } else {
  1341. button_create_script->hide();
  1342. Array selection = editor_selection->get_selected_nodes();
  1343. for (int i = 0; i < selection.size(); i++) {
  1344. Node *n = Object::cast_to<Node>(selection[i]);
  1345. if (!n->get_script().is_null()) {
  1346. button_clear_script->show();
  1347. return;
  1348. }
  1349. }
  1350. button_clear_script->hide();
  1351. }
  1352. }
  1353. void SceneTreeDock::_selection_changed() {
  1354. int selection_size = EditorNode::get_singleton()->get_editor_selection()->get_selection().size();
  1355. if (selection_size > 1) {
  1356. //automatically turn on multi-edit
  1357. _tool_selected(TOOL_MULTI_EDIT);
  1358. } else if (selection_size == 1) {
  1359. editor->push_item(EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list()[0]);
  1360. } else {
  1361. editor->push_item(NULL);
  1362. }
  1363. _update_script_button();
  1364. }
  1365. void SceneTreeDock::_create() {
  1366. if (current_option == TOOL_NEW) {
  1367. Node *parent = NULL;
  1368. if (edited_scene) {
  1369. // If root exists in edited scene
  1370. parent = scene_tree->get_selected();
  1371. if (!parent)
  1372. parent = edited_scene;
  1373. } else {
  1374. // If no root exist in edited scene
  1375. parent = scene_root;
  1376. ERR_FAIL_COND(!parent);
  1377. }
  1378. Object *c = create_dialog->instance_selected();
  1379. ERR_FAIL_COND(!c);
  1380. Node *child = Object::cast_to<Node>(c);
  1381. ERR_FAIL_COND(!child);
  1382. editor_data->get_undo_redo().create_action(TTR("Create Node"));
  1383. if (edited_scene) {
  1384. editor_data->get_undo_redo().add_do_method(parent, "add_child", child);
  1385. editor_data->get_undo_redo().add_do_method(child, "set_owner", edited_scene);
  1386. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  1387. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", child);
  1388. editor_data->get_undo_redo().add_do_reference(child);
  1389. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", child);
  1390. String new_name = parent->validate_child_name(child);
  1391. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  1392. editor_data->get_undo_redo().add_do_method(sed, "live_debug_create_node", edited_scene->get_path_to(parent), child->get_class(), new_name);
  1393. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)).plus_file(new_name)));
  1394. } else {
  1395. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", child);
  1396. editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
  1397. editor_data->get_undo_redo().add_do_reference(child);
  1398. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", (Object *)NULL);
  1399. }
  1400. editor_data->get_undo_redo().commit_action();
  1401. editor->push_item(c);
  1402. editor_selection->clear();
  1403. editor_selection->add_node(child);
  1404. if (Object::cast_to<Control>(c)) {
  1405. //make editor more comfortable, so some controls don't appear super shrunk
  1406. Control *ct = Object::cast_to<Control>(c);
  1407. Size2 ms = ct->get_minimum_size();
  1408. if (ms.width < 4)
  1409. ms.width = 40;
  1410. if (ms.height < 4)
  1411. ms.height = 40;
  1412. ct->set_size(ms);
  1413. }
  1414. } else if (current_option == TOOL_REPLACE) {
  1415. List<Node *> selection = editor_selection->get_selected_node_list();
  1416. ERR_FAIL_COND(selection.size() <= 0);
  1417. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1418. Node *n = E->get();
  1419. ERR_FAIL_COND(!n);
  1420. Object *c = create_dialog->instance_selected();
  1421. ERR_FAIL_COND(!c);
  1422. Node *newnode = Object::cast_to<Node>(c);
  1423. ERR_FAIL_COND(!newnode);
  1424. replace_node(n, newnode);
  1425. }
  1426. }
  1427. scene_tree->get_scene_tree()->call_deferred("grab_focus");
  1428. }
  1429. void SceneTreeDock::replace_node(Node *p_node, Node *p_by_node, bool p_keep_properties) {
  1430. Node *n = p_node;
  1431. Node *newnode = p_by_node;
  1432. if (p_keep_properties) {
  1433. Node *default_oldnode = Object::cast_to<Node>(ClassDB::instance(n->get_class()));
  1434. List<PropertyInfo> pinfo;
  1435. n->get_property_list(&pinfo);
  1436. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1437. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  1438. continue;
  1439. if (E->get().name == "__meta__")
  1440. continue;
  1441. if (default_oldnode->get(E->get().name) != n->get(E->get().name)) {
  1442. newnode->set(E->get().name, n->get(E->get().name));
  1443. }
  1444. }
  1445. memdelete(default_oldnode);
  1446. }
  1447. editor->push_item(NULL);
  1448. //reconnect signals
  1449. List<MethodInfo> sl;
  1450. n->get_signal_list(&sl);
  1451. for (List<MethodInfo>::Element *E = sl.front(); E; E = E->next()) {
  1452. List<Object::Connection> cl;
  1453. n->get_signal_connection_list(E->get().name, &cl);
  1454. for (List<Object::Connection>::Element *F = cl.front(); F; F = F->next()) {
  1455. Object::Connection &c = F->get();
  1456. if (!(c.flags & Object::CONNECT_PERSIST))
  1457. continue;
  1458. newnode->connect(c.signal, c.target, c.method, c.binds, Object::CONNECT_PERSIST);
  1459. }
  1460. }
  1461. String newname = n->get_name();
  1462. List<Node *> to_erase;
  1463. for (int i = 0; i < n->get_child_count(); i++) {
  1464. if (n->get_child(i)->get_owner() == NULL && n->is_owned_by_parent()) {
  1465. to_erase.push_back(n->get_child(i));
  1466. }
  1467. }
  1468. n->replace_by(newnode, true);
  1469. if (n == edited_scene) {
  1470. edited_scene = newnode;
  1471. editor->set_edited_scene(newnode);
  1472. newnode->set_editable_instances(n->get_editable_instances());
  1473. }
  1474. //small hack to make collisionshapes and other kind of nodes to work
  1475. for (int i = 0; i < newnode->get_child_count(); i++) {
  1476. Node *c = newnode->get_child(i);
  1477. c->call("set_transform", c->call("get_transform"));
  1478. }
  1479. editor_data->get_undo_redo().clear_history();
  1480. newnode->set_name(newname);
  1481. editor->push_item(newnode);
  1482. memdelete(n);
  1483. while (to_erase.front()) {
  1484. memdelete(to_erase.front()->get());
  1485. to_erase.pop_front();
  1486. }
  1487. }
  1488. void SceneTreeDock::set_edited_scene(Node *p_scene) {
  1489. edited_scene = p_scene;
  1490. }
  1491. void SceneTreeDock::set_selected(Node *p_node, bool p_emit_selected) {
  1492. scene_tree->set_selected(p_node, p_emit_selected);
  1493. }
  1494. void SceneTreeDock::import_subscene() {
  1495. Size2 popup_size = Size2(500, 800) * editor_get_scale();
  1496. Size2 window_size = get_viewport_rect().size;
  1497. popup_size.x = MIN(window_size.x * 0.8, popup_size.x);
  1498. popup_size.y = MIN(window_size.y * 0.8, popup_size.y);
  1499. import_subscene_dialog->popup_centered(popup_size);
  1500. }
  1501. void SceneTreeDock::_import_subscene() {
  1502. Node *parent = scene_tree->get_selected();
  1503. if (!parent) {
  1504. parent = editor_data->get_edited_scene_root();
  1505. ERR_FAIL_COND(!parent);
  1506. }
  1507. import_subscene_dialog->move(parent, edited_scene);
  1508. editor_data->get_undo_redo().clear_history(); //no undo for now..
  1509. }
  1510. void SceneTreeDock::_new_scene_from(String p_file) {
  1511. List<Node *> selection = editor_selection->get_selected_node_list();
  1512. if (selection.size() != 1) {
  1513. accept->set_text(TTR("This operation requires a single selected node."));
  1514. accept->popup_centered_minsize();
  1515. return;
  1516. }
  1517. if (EditorNode::get_singleton()->is_scene_open(p_file)) {
  1518. accept->set_text(TTR("Can't overwrite scene that is still open!"));
  1519. accept->popup_centered_minsize();
  1520. return;
  1521. }
  1522. Node *base = selection.front()->get();
  1523. Map<Node *, Node *> reown;
  1524. reown[editor_data->get_edited_scene_root()] = base;
  1525. Node *copy = base->duplicate_and_reown(reown);
  1526. if (copy) {
  1527. Ref<PackedScene> sdata = memnew(PackedScene);
  1528. Error err = sdata->pack(copy);
  1529. memdelete(copy);
  1530. if (err != OK) {
  1531. accept->set_text(TTR("Couldn't save new scene. Likely dependencies (instances) couldn't be satisfied."));
  1532. accept->popup_centered_minsize();
  1533. return;
  1534. }
  1535. int flg = 0;
  1536. if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources"))
  1537. flg |= ResourceSaver::FLAG_COMPRESS;
  1538. err = ResourceSaver::save(p_file, sdata, flg);
  1539. if (err != OK) {
  1540. accept->set_text(TTR("Error saving scene."));
  1541. accept->popup_centered_minsize();
  1542. return;
  1543. }
  1544. _replace_with_branch_scene(p_file, base);
  1545. } else {
  1546. accept->set_text(TTR("Error duplicating scene to save it."));
  1547. accept->popup_centered_minsize();
  1548. return;
  1549. }
  1550. }
  1551. static bool _is_node_visible(Node *p_node) {
  1552. if (!p_node->get_owner())
  1553. return false;
  1554. if (p_node->get_owner() != EditorNode::get_singleton()->get_edited_scene() && !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node->get_owner()))
  1555. return false;
  1556. return true;
  1557. }
  1558. static bool _has_visible_children(Node *p_node) {
  1559. bool collapsed = p_node->is_displayed_folded();
  1560. if (collapsed)
  1561. return false;
  1562. for (int i = 0; i < p_node->get_child_count(); i++) {
  1563. Node *child = p_node->get_child(i);
  1564. if (!_is_node_visible(child))
  1565. continue;
  1566. return true;
  1567. }
  1568. return false;
  1569. }
  1570. void SceneTreeDock::_normalize_drop(Node *&to_node, int &to_pos, int p_type) {
  1571. to_pos = -1;
  1572. if (p_type == -1) {
  1573. //drop at above selected node
  1574. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  1575. to_node = NULL;
  1576. ERR_EXPLAIN("Cannot perform drop above the root node!");
  1577. ERR_FAIL();
  1578. }
  1579. to_pos = to_node->get_index();
  1580. to_node = to_node->get_parent();
  1581. } else if (p_type == 1) {
  1582. //drop at below selected node
  1583. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  1584. //if at lower sibling of root node
  1585. to_pos = 0; //just insert at beginning of root node
  1586. return;
  1587. }
  1588. Node *lower_sibling = NULL;
  1589. if (_has_visible_children(to_node)) {
  1590. to_pos = 0;
  1591. } else {
  1592. for (int i = to_node->get_index() + 1; i < to_node->get_parent()->get_child_count(); i++) {
  1593. Node *c = to_node->get_parent()->get_child(i);
  1594. if (_is_node_visible(c)) {
  1595. lower_sibling = c;
  1596. break;
  1597. }
  1598. }
  1599. if (lower_sibling) {
  1600. to_pos = lower_sibling->get_index();
  1601. }
  1602. to_node = to_node->get_parent();
  1603. }
  1604. }
  1605. }
  1606. void SceneTreeDock::_files_dropped(Vector<String> p_files, NodePath p_to, int p_type) {
  1607. Node *node = get_node(p_to);
  1608. ERR_FAIL_COND(!node);
  1609. int to_pos = -1;
  1610. _normalize_drop(node, to_pos, p_type);
  1611. _perform_instance_scenes(p_files, node, to_pos);
  1612. }
  1613. void SceneTreeDock::_script_dropped(String p_file, NodePath p_to) {
  1614. Ref<Script> scr = ResourceLoader::load(p_file);
  1615. ERR_FAIL_COND(!scr.is_valid());
  1616. Node *n = get_node(p_to);
  1617. if (n) {
  1618. editor_data->get_undo_redo().create_action(TTR("Attach Script"));
  1619. editor_data->get_undo_redo().add_do_method(n, "set_script", scr);
  1620. editor_data->get_undo_redo().add_undo_method(n, "set_script", n->get_script());
  1621. editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
  1622. editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
  1623. editor_data->get_undo_redo().commit_action();
  1624. }
  1625. }
  1626. void SceneTreeDock::_nodes_dragged(Array p_nodes, NodePath p_to, int p_type) {
  1627. Vector<Node *> nodes;
  1628. Node *to_node;
  1629. for (int i = 0; i < p_nodes.size(); i++) {
  1630. Node *n = get_node((p_nodes[i]));
  1631. if (n) {
  1632. nodes.push_back(n);
  1633. }
  1634. }
  1635. if (nodes.size() == 0)
  1636. return;
  1637. to_node = get_node(p_to);
  1638. if (!to_node)
  1639. return;
  1640. int to_pos = -1;
  1641. _normalize_drop(to_node, to_pos, p_type);
  1642. _do_reparent(to_node, to_pos, nodes, true);
  1643. }
  1644. void SceneTreeDock::_add_children_to_popup(Object *p_obj, int p_depth) {
  1645. if (p_depth > 8)
  1646. return;
  1647. List<PropertyInfo> pinfo;
  1648. p_obj->get_property_list(&pinfo);
  1649. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1650. if (!(E->get().usage & PROPERTY_USAGE_EDITOR))
  1651. continue;
  1652. if (E->get().hint != PROPERTY_HINT_RESOURCE_TYPE)
  1653. continue;
  1654. Variant value = p_obj->get(E->get().name);
  1655. if (value.get_type() != Variant::OBJECT)
  1656. continue;
  1657. Object *obj = value;
  1658. if (!obj)
  1659. continue;
  1660. Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
  1661. if (menu->get_item_count() == 0) {
  1662. menu->add_submenu_item(TTR("Sub-Resources"), "Sub-Resources");
  1663. }
  1664. int index = menu_subresources->get_item_count();
  1665. menu_subresources->add_icon_item(icon, E->get().name.capitalize(), EDIT_SUBRESOURCE_BASE + subresources.size());
  1666. menu_subresources->set_item_h_offset(index, p_depth * 10 * EDSCALE);
  1667. subresources.push_back(obj->get_instance_id());
  1668. _add_children_to_popup(obj, p_depth + 1);
  1669. }
  1670. }
  1671. void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
  1672. if (!EditorNode::get_singleton()->get_edited_scene()) {
  1673. menu->clear();
  1674. if (profile_allow_editing) {
  1675. menu->add_icon_shortcut(get_icon("Add", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  1676. menu->add_icon_shortcut(get_icon("Instance", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANCE);
  1677. }
  1678. menu->set_size(Size2(1, 1));
  1679. menu->set_position(p_menu_pos);
  1680. menu->popup();
  1681. return;
  1682. }
  1683. List<Node *> selection = editor_selection->get_selected_node_list();
  1684. if (selection.size() == 0)
  1685. return;
  1686. menu->clear();
  1687. Ref<Script> existing_script;
  1688. if (selection.size() == 1) {
  1689. Node *selected = selection[0];
  1690. if (profile_allow_editing) {
  1691. subresources.clear();
  1692. menu_subresources->clear();
  1693. menu_subresources->set_size(Size2(1, 1));
  1694. _add_children_to_popup(selection.front()->get(), 0);
  1695. if (menu->get_item_count() > 0)
  1696. menu->add_separator();
  1697. menu->add_icon_shortcut(get_icon("Add", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  1698. menu->add_icon_shortcut(get_icon("Instance", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANCE);
  1699. menu->add_separator();
  1700. }
  1701. existing_script = selected->get_script();
  1702. }
  1703. if (profile_allow_script_editing) {
  1704. if (selection.size() == 1) {
  1705. if (!existing_script.is_valid()) {
  1706. menu->add_icon_shortcut(get_icon("ScriptCreate", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/attach_script"), TOOL_ATTACH_SCRIPT);
  1707. } else {
  1708. menu->add_icon_shortcut(get_icon("ScriptExtend", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/extend_script"), TOOL_ATTACH_SCRIPT);
  1709. }
  1710. }
  1711. if (selection.size() > 1 || existing_script.is_valid()) {
  1712. menu->add_icon_shortcut(get_icon("ScriptRemove", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/clear_script"), TOOL_CLEAR_SCRIPT);
  1713. }
  1714. menu->add_separator();
  1715. }
  1716. if (profile_allow_editing) {
  1717. if (selection.size() == 1) {
  1718. menu->add_icon_shortcut(get_icon("Rename", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/rename"), TOOL_RENAME);
  1719. }
  1720. menu->add_icon_shortcut(get_icon("Reload", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/change_node_type"), TOOL_REPLACE);
  1721. if (scene_tree->get_selected() != edited_scene) {
  1722. menu->add_separator();
  1723. menu->add_icon_shortcut(get_icon("MoveUp", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/move_up"), TOOL_MOVE_UP);
  1724. menu->add_icon_shortcut(get_icon("MoveDown", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/move_down"), TOOL_MOVE_DOWN);
  1725. menu->add_icon_shortcut(get_icon("Duplicate", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/duplicate"), TOOL_DUPLICATE);
  1726. menu->add_icon_shortcut(get_icon("Reparent", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/reparent"), TOOL_REPARENT);
  1727. menu->add_icon_shortcut(get_icon("NewRoot", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/make_root"), TOOL_MAKE_ROOT);
  1728. }
  1729. }
  1730. if (selection.size() == 1) {
  1731. if (profile_allow_editing) {
  1732. menu->add_separator();
  1733. menu->add_icon_shortcut(get_icon("Blend", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/merge_from_scene"), TOOL_MERGE_FROM_SCENE);
  1734. menu->add_icon_shortcut(get_icon("CreateNewSceneFrom", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/save_branch_as_scene"), TOOL_NEW_SCENE_FROM);
  1735. menu->add_separator();
  1736. }
  1737. menu->add_icon_shortcut(get_icon("CopyNodePath", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/copy_node_path"), TOOL_COPY_NODE_PATH);
  1738. bool is_external = (selection[0]->get_filename() != "");
  1739. if (is_external) {
  1740. bool is_inherited = selection[0]->get_scene_inherited_state() != NULL;
  1741. bool is_top_level = selection[0]->get_owner() == NULL;
  1742. if (is_inherited && is_top_level) {
  1743. menu->add_separator();
  1744. if (profile_allow_editing) {
  1745. menu->add_item(TTR("Clear Inheritance"), TOOL_SCENE_CLEAR_INHERITANCE);
  1746. }
  1747. menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Open in Editor"), TOOL_SCENE_OPEN_INHERITED);
  1748. } else if (!is_top_level) {
  1749. menu->add_separator();
  1750. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(selection[0]);
  1751. bool placeholder = selection[0]->get_scene_instance_load_placeholder();
  1752. if (profile_allow_editing) {
  1753. menu->add_check_item(TTR("Editable Children"), TOOL_SCENE_EDITABLE_CHILDREN);
  1754. menu->add_check_item(TTR("Load As Placeholder"), TOOL_SCENE_USE_PLACEHOLDER);
  1755. menu->add_item(TTR("Make Local"), TOOL_SCENE_MAKE_LOCAL);
  1756. }
  1757. menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Open in Editor"), TOOL_SCENE_OPEN);
  1758. if (profile_allow_editing) {
  1759. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Editable Children")), editable);
  1760. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Load As Placeholder")), placeholder);
  1761. }
  1762. }
  1763. }
  1764. }
  1765. if (profile_allow_editing && selection.size() > 1) {
  1766. //this is not a commonly used action, it makes no sense for it to be where it was nor always present.
  1767. menu->add_separator();
  1768. menu->add_icon_shortcut(get_icon("Rename", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/batch_rename"), TOOL_BATCH_RENAME);
  1769. }
  1770. menu->add_separator();
  1771. menu->add_icon_item(get_icon("Help", "EditorIcons"), TTR("Open Documentation"), TOOL_OPEN_DOCUMENTATION);
  1772. if (profile_allow_editing) {
  1773. menu->add_separator();
  1774. menu->add_icon_shortcut(get_icon("Remove", "EditorIcons"), ED_SHORTCUT("scene_tree/delete", TTR("Delete Node(s)"), KEY_DELETE), TOOL_ERASE);
  1775. }
  1776. menu->set_size(Size2(1, 1));
  1777. menu->set_position(p_menu_pos);
  1778. menu->popup();
  1779. }
  1780. void SceneTreeDock::_filter_changed(const String &p_filter) {
  1781. scene_tree->set_filter(p_filter);
  1782. }
  1783. String SceneTreeDock::get_filter() {
  1784. return filter->get_text();
  1785. }
  1786. void SceneTreeDock::set_filter(const String &p_filter) {
  1787. filter->set_text(p_filter);
  1788. scene_tree->set_filter(p_filter);
  1789. }
  1790. void SceneTreeDock::_focus_node() {
  1791. Node *node = scene_tree->get_selected();
  1792. ERR_FAIL_COND(!node);
  1793. if (node->is_class("CanvasItem")) {
  1794. CanvasItemEditorPlugin *editor = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
  1795. editor->get_canvas_item_editor()->focus_selection();
  1796. } else {
  1797. SpatialEditorPlugin *editor = Object::cast_to<SpatialEditorPlugin>(editor_data->get_editor("3D"));
  1798. editor->get_spatial_editor()->get_editor_viewport(0)->focus_selection();
  1799. }
  1800. }
  1801. void SceneTreeDock::open_script_dialog(Node *p_for_node) {
  1802. scene_tree->set_selected(p_for_node, false);
  1803. _tool_selected(TOOL_ATTACH_SCRIPT);
  1804. }
  1805. void SceneTreeDock::add_remote_tree_editor(Control *p_remote) {
  1806. ERR_FAIL_COND(remote_tree != NULL);
  1807. add_child(p_remote);
  1808. remote_tree = p_remote;
  1809. remote_tree->hide();
  1810. }
  1811. void SceneTreeDock::show_remote_tree() {
  1812. _remote_tree_selected();
  1813. }
  1814. void SceneTreeDock::hide_remote_tree() {
  1815. _local_tree_selected();
  1816. }
  1817. void SceneTreeDock::show_tab_buttons() {
  1818. button_hb->show();
  1819. }
  1820. void SceneTreeDock::hide_tab_buttons() {
  1821. button_hb->hide();
  1822. }
  1823. void SceneTreeDock::_remote_tree_selected() {
  1824. scene_tree->hide();
  1825. if (remote_tree)
  1826. remote_tree->show();
  1827. edit_remote->set_pressed(true);
  1828. edit_local->set_pressed(false);
  1829. emit_signal("remote_tree_selected");
  1830. }
  1831. void SceneTreeDock::_local_tree_selected() {
  1832. scene_tree->show();
  1833. if (remote_tree)
  1834. remote_tree->hide();
  1835. edit_remote->set_pressed(false);
  1836. edit_local->set_pressed(true);
  1837. }
  1838. void SceneTreeDock::_update_create_root_dialog() {
  1839. BaseButton *toggle = Object::cast_to<BaseButton>(create_root_dialog->get_node(String("NodeShortcutsTopRow/NodeShortcutsToggle")));
  1840. Node *node_shortcuts = create_root_dialog->get_node(String("NodeShortcuts"));
  1841. if (!toggle || !node_shortcuts)
  1842. return;
  1843. Control *beginner_nodes = Object::cast_to<Control>(node_shortcuts->get_node(String("BeginnerNodeShortcuts")));
  1844. Control *favorite_nodes = Object::cast_to<Control>(node_shortcuts->get_node(String("FavoriteNodeShortcuts")));
  1845. if (!beginner_nodes || !favorite_nodes)
  1846. return;
  1847. EditorSettings::get_singleton()->set_setting("_use_favorites_root_selection", toggle->is_pressed());
  1848. EditorSettings::get_singleton()->save();
  1849. if (toggle->is_pressed()) {
  1850. for (int i = 0; i < favorite_nodes->get_child_count(); i++) {
  1851. favorite_nodes->get_child(i)->queue_delete();
  1852. }
  1853. FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("favorites.Node"), FileAccess::READ);
  1854. if (f) {
  1855. while (!f->eof_reached()) {
  1856. String l = f->get_line().strip_edges();
  1857. if (l != String()) {
  1858. Button *button = memnew(Button);
  1859. favorite_nodes->add_child(button);
  1860. button->set_text(TTR(l));
  1861. String name = l.get_slicec(' ', 0);
  1862. if (ScriptServer::is_global_class(name))
  1863. name = ScriptServer::get_global_class_native_base(name);
  1864. button->set_icon(EditorNode::get_singleton()->get_class_icon(name));
  1865. button->connect("pressed", this, "_favorite_root_selected", make_binds(l));
  1866. }
  1867. }
  1868. memdelete(f);
  1869. }
  1870. if (!favorite_nodes->is_visible_in_tree()) {
  1871. favorite_nodes->show();
  1872. beginner_nodes->hide();
  1873. }
  1874. } else {
  1875. if (!beginner_nodes->is_visible_in_tree()) {
  1876. beginner_nodes->show();
  1877. favorite_nodes->hide();
  1878. }
  1879. }
  1880. }
  1881. void SceneTreeDock::_favorite_root_selected(const String &p_class) {
  1882. selected_favorite_root = p_class;
  1883. _tool_selected(TOOL_CREATE_FAVORITE, false);
  1884. }
  1885. void SceneTreeDock::_feature_profile_changed() {
  1886. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  1887. if (profile.is_valid()) {
  1888. profile_allow_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCENE_TREE);
  1889. profile_allow_script_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCRIPT);
  1890. button_add->set_visible(profile_allow_editing);
  1891. button_instance->set_visible(profile_allow_editing);
  1892. scene_tree->set_can_rename(profile_allow_editing);
  1893. } else {
  1894. button_add->set_visible(true);
  1895. button_instance->set_visible(true);
  1896. scene_tree->set_can_rename(true);
  1897. profile_allow_editing = true;
  1898. profile_allow_script_editing = true;
  1899. }
  1900. _update_script_button();
  1901. }
  1902. void SceneTreeDock::_bind_methods() {
  1903. ClassDB::bind_method(D_METHOD("_tool_selected"), &SceneTreeDock::_tool_selected, DEFVAL(false));
  1904. ClassDB::bind_method(D_METHOD("_create"), &SceneTreeDock::_create);
  1905. ClassDB::bind_method(D_METHOD("_node_reparent"), &SceneTreeDock::_node_reparent);
  1906. ClassDB::bind_method(D_METHOD("_set_owners"), &SceneTreeDock::_set_owners);
  1907. ClassDB::bind_method(D_METHOD("_node_selected"), &SceneTreeDock::_node_selected);
  1908. ClassDB::bind_method(D_METHOD("_node_renamed"), &SceneTreeDock::_node_renamed);
  1909. ClassDB::bind_method(D_METHOD("_script_created"), &SceneTreeDock::_script_created);
  1910. ClassDB::bind_method(D_METHOD("_load_request"), &SceneTreeDock::_load_request);
  1911. ClassDB::bind_method(D_METHOD("_script_open_request"), &SceneTreeDock::_script_open_request);
  1912. ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &SceneTreeDock::_unhandled_key_input);
  1913. ClassDB::bind_method(D_METHOD("_input"), &SceneTreeDock::_input);
  1914. ClassDB::bind_method(D_METHOD("_nodes_drag_begin"), &SceneTreeDock::_nodes_drag_begin);
  1915. ClassDB::bind_method(D_METHOD("_delete_confirm"), &SceneTreeDock::_delete_confirm);
  1916. ClassDB::bind_method(D_METHOD("_toggle_editable_children"), &SceneTreeDock::_toggle_editable_children);
  1917. ClassDB::bind_method(D_METHOD("_node_prerenamed"), &SceneTreeDock::_node_prerenamed);
  1918. ClassDB::bind_method(D_METHOD("_import_subscene"), &SceneTreeDock::_import_subscene);
  1919. ClassDB::bind_method(D_METHOD("_selection_changed"), &SceneTreeDock::_selection_changed);
  1920. ClassDB::bind_method(D_METHOD("_new_scene_from"), &SceneTreeDock::_new_scene_from);
  1921. ClassDB::bind_method(D_METHOD("_nodes_dragged"), &SceneTreeDock::_nodes_dragged);
  1922. ClassDB::bind_method(D_METHOD("_files_dropped"), &SceneTreeDock::_files_dropped);
  1923. ClassDB::bind_method(D_METHOD("_quick_open"), &SceneTreeDock::_quick_open);
  1924. ClassDB::bind_method(D_METHOD("_script_dropped"), &SceneTreeDock::_script_dropped);
  1925. ClassDB::bind_method(D_METHOD("_tree_rmb"), &SceneTreeDock::_tree_rmb);
  1926. ClassDB::bind_method(D_METHOD("_filter_changed"), &SceneTreeDock::_filter_changed);
  1927. ClassDB::bind_method(D_METHOD("_focus_node"), &SceneTreeDock::_focus_node);
  1928. ClassDB::bind_method(D_METHOD("_remote_tree_selected"), &SceneTreeDock::_remote_tree_selected);
  1929. ClassDB::bind_method(D_METHOD("_local_tree_selected"), &SceneTreeDock::_local_tree_selected);
  1930. ClassDB::bind_method(D_METHOD("_update_script_button"), &SceneTreeDock::_update_script_button);
  1931. ClassDB::bind_method(D_METHOD("_favorite_root_selected"), &SceneTreeDock::_favorite_root_selected);
  1932. ClassDB::bind_method(D_METHOD("_update_create_root_dialog"), &SceneTreeDock::_update_create_root_dialog);
  1933. ClassDB::bind_method(D_METHOD("_feature_profile_changed"), &SceneTreeDock::_feature_profile_changed);
  1934. ClassDB::bind_method(D_METHOD("instance"), &SceneTreeDock::instance);
  1935. ADD_SIGNAL(MethodInfo("remote_tree_selected"));
  1936. }
  1937. SceneTreeDock::SceneTreeDock(EditorNode *p_editor, Node *p_scene_root, EditorSelection *p_editor_selection, EditorData &p_editor_data) {
  1938. set_name("Scene");
  1939. editor = p_editor;
  1940. edited_scene = NULL;
  1941. editor_data = &p_editor_data;
  1942. editor_selection = p_editor_selection;
  1943. scene_root = p_scene_root;
  1944. VBoxContainer *vbc = this;
  1945. HBoxContainer *filter_hbc = memnew(HBoxContainer);
  1946. filter_hbc->add_constant_override("separate", 0);
  1947. ED_SHORTCUT("scene_tree/rename", TTR("Rename"));
  1948. ED_SHORTCUT("scene_tree/batch_rename", TTR("Batch Rename"), KEY_MASK_CMD | KEY_F2);
  1949. ED_SHORTCUT("scene_tree/add_child_node", TTR("Add Child Node"), KEY_MASK_CMD | KEY_A);
  1950. ED_SHORTCUT("scene_tree/instance_scene", TTR("Instance Child Scene"));
  1951. ED_SHORTCUT("scene_tree/change_node_type", TTR("Change Type"));
  1952. ED_SHORTCUT("scene_tree/attach_script", TTR("Attach Script"));
  1953. ED_SHORTCUT("scene_tree/extend_script", TTR("Extend Script"));
  1954. ED_SHORTCUT("scene_tree/clear_script", TTR("Clear Script"));
  1955. ED_SHORTCUT("scene_tree/move_up", TTR("Move Up"), KEY_MASK_CMD | KEY_UP);
  1956. ED_SHORTCUT("scene_tree/move_down", TTR("Move Down"), KEY_MASK_CMD | KEY_DOWN);
  1957. ED_SHORTCUT("scene_tree/duplicate", TTR("Duplicate"), KEY_MASK_CMD | KEY_D);
  1958. ED_SHORTCUT("scene_tree/reparent", TTR("Reparent"));
  1959. ED_SHORTCUT("scene_tree/make_root", TTR("Make Scene Root"));
  1960. ED_SHORTCUT("scene_tree/merge_from_scene", TTR("Merge From Scene"));
  1961. ED_SHORTCUT("scene_tree/save_branch_as_scene", TTR("Save Branch as Scene"));
  1962. ED_SHORTCUT("scene_tree/copy_node_path", TTR("Copy Node Path"), KEY_MASK_CMD | KEY_C);
  1963. ED_SHORTCUT("scene_tree/delete_no_confirm", TTR("Delete (No Confirm)"), KEY_MASK_SHIFT | KEY_DELETE);
  1964. ED_SHORTCUT("scene_tree/delete", TTR("Delete"), KEY_DELETE);
  1965. button_add = memnew(ToolButton);
  1966. button_add->connect("pressed", this, "_tool_selected", make_binds(TOOL_NEW, false));
  1967. button_add->set_tooltip(TTR("Add/Create a New Node"));
  1968. button_add->set_shortcut(ED_GET_SHORTCUT("scene_tree/add_child_node"));
  1969. filter_hbc->add_child(button_add);
  1970. button_instance = memnew(ToolButton);
  1971. button_instance->connect("pressed", this, "_tool_selected", make_binds(TOOL_INSTANCE, false));
  1972. button_instance->set_tooltip(TTR("Instance a scene file as a Node. Creates an inherited scene if no root node exists."));
  1973. button_instance->set_shortcut(ED_GET_SHORTCUT("scene_tree/instance_scene"));
  1974. filter_hbc->add_child(button_instance);
  1975. vbc->add_child(filter_hbc);
  1976. filter = memnew(LineEdit);
  1977. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  1978. filter->set_placeholder(TTR("Filter nodes"));
  1979. filter_hbc->add_child(filter);
  1980. filter->add_constant_override("minimum_spaces", 0);
  1981. filter->connect("text_changed", this, "_filter_changed");
  1982. button_create_script = memnew(ToolButton);
  1983. button_create_script->connect("pressed", this, "_tool_selected", make_binds(TOOL_ATTACH_SCRIPT, false));
  1984. button_create_script->set_tooltip(TTR("Attach a new or existing script for the selected node."));
  1985. button_create_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/attach_script"));
  1986. filter_hbc->add_child(button_create_script);
  1987. button_create_script->hide();
  1988. button_clear_script = memnew(ToolButton);
  1989. button_clear_script->connect("pressed", this, "_tool_selected", make_binds(TOOL_CLEAR_SCRIPT, false));
  1990. button_clear_script->set_tooltip(TTR("Clear a script for the selected node."));
  1991. button_clear_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/clear_script"));
  1992. filter_hbc->add_child(button_clear_script);
  1993. button_clear_script->hide();
  1994. button_hb = memnew(HBoxContainer);
  1995. vbc->add_child(button_hb);
  1996. edit_remote = memnew(ToolButton);
  1997. button_hb->add_child(edit_remote);
  1998. edit_remote->set_h_size_flags(SIZE_EXPAND_FILL);
  1999. edit_remote->set_text(TTR("Remote"));
  2000. edit_remote->set_toggle_mode(true);
  2001. edit_remote->connect("pressed", this, "_remote_tree_selected");
  2002. edit_local = memnew(ToolButton);
  2003. button_hb->add_child(edit_local);
  2004. edit_local->set_h_size_flags(SIZE_EXPAND_FILL);
  2005. edit_local->set_text(TTR("Local"));
  2006. edit_local->set_toggle_mode(true);
  2007. edit_local->connect("pressed", this, "_local_tree_selected");
  2008. remote_tree = NULL;
  2009. button_hb->hide();
  2010. create_root_dialog = memnew(VBoxContainer);
  2011. vbc->add_child(create_root_dialog);
  2012. create_root_dialog->hide();
  2013. scene_tree = memnew(SceneTreeEditor(false, true, true));
  2014. vbc->add_child(scene_tree);
  2015. scene_tree->set_v_size_flags(SIZE_EXPAND | SIZE_FILL);
  2016. scene_tree->connect("rmb_pressed", this, "_tree_rmb");
  2017. scene_tree->connect("node_selected", this, "_node_selected", varray(), CONNECT_DEFERRED);
  2018. scene_tree->connect("node_renamed", this, "_node_renamed", varray(), CONNECT_DEFERRED);
  2019. scene_tree->connect("node_prerename", this, "_node_prerenamed");
  2020. scene_tree->connect("open", this, "_load_request");
  2021. scene_tree->connect("open_script", this, "_script_open_request");
  2022. scene_tree->connect("nodes_rearranged", this, "_nodes_dragged");
  2023. scene_tree->connect("files_dropped", this, "_files_dropped");
  2024. scene_tree->connect("script_dropped", this, "_script_dropped");
  2025. scene_tree->connect("nodes_dragged", this, "_nodes_drag_begin");
  2026. scene_tree->get_scene_tree()->connect("item_double_clicked", this, "_focus_node");
  2027. scene_tree->set_undo_redo(&editor_data->get_undo_redo());
  2028. scene_tree->set_editor_selection(editor_selection);
  2029. create_dialog = memnew(CreateDialog);
  2030. create_dialog->set_base_type("Node");
  2031. add_child(create_dialog);
  2032. create_dialog->connect("create", this, "_create");
  2033. create_dialog->connect("favorites_updated", this, "_update_create_root_dialog");
  2034. rename_dialog = memnew(RenameDialog(scene_tree, &editor_data->get_undo_redo()));
  2035. add_child(rename_dialog);
  2036. script_create_dialog = memnew(ScriptCreateDialog);
  2037. script_create_dialog->set_inheritance_base_type("Node");
  2038. add_child(script_create_dialog);
  2039. script_create_dialog->connect("script_created", this, "_script_created");
  2040. reparent_dialog = memnew(ReparentDialog);
  2041. add_child(reparent_dialog);
  2042. reparent_dialog->connect("reparent", this, "_node_reparent");
  2043. accept = memnew(AcceptDialog);
  2044. add_child(accept);
  2045. quick_open = memnew(EditorQuickOpen);
  2046. add_child(quick_open);
  2047. quick_open->connect("quick_open", this, "_quick_open");
  2048. set_process_unhandled_key_input(true);
  2049. delete_dialog = memnew(ConfirmationDialog);
  2050. add_child(delete_dialog);
  2051. delete_dialog->connect("confirmed", this, "_delete_confirm");
  2052. editable_instance_remove_dialog = memnew(ConfirmationDialog);
  2053. add_child(editable_instance_remove_dialog);
  2054. editable_instance_remove_dialog->connect("confirmed", this, "_toggle_editable_children");
  2055. import_subscene_dialog = memnew(EditorSubScene);
  2056. add_child(import_subscene_dialog);
  2057. import_subscene_dialog->connect("subscene_selected", this, "_import_subscene");
  2058. new_scene_from_dialog = memnew(EditorFileDialog);
  2059. new_scene_from_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  2060. add_child(new_scene_from_dialog);
  2061. new_scene_from_dialog->connect("file_selected", this, "_new_scene_from");
  2062. menu = memnew(PopupMenu);
  2063. add_child(menu);
  2064. menu->connect("id_pressed", this, "_tool_selected");
  2065. menu->set_hide_on_window_lose_focus(true);
  2066. menu_subresources = memnew(PopupMenu);
  2067. menu_subresources->set_name("Sub-Resources");
  2068. menu_subresources->connect("id_pressed", this, "_tool_selected");
  2069. menu->add_child(menu_subresources);
  2070. first_enter = true;
  2071. restore_script_editor_on_drag = false;
  2072. clear_inherit_confirm = memnew(ConfirmationDialog);
  2073. clear_inherit_confirm->set_text(TTR("Clear Inheritance? (No Undo!)"));
  2074. clear_inherit_confirm->get_ok()->set_text(TTR("Clear"));
  2075. add_child(clear_inherit_confirm);
  2076. set_process_input(true);
  2077. set_process(true);
  2078. profile_allow_editing = true;
  2079. profile_allow_script_editing = true;
  2080. EDITOR_DEF("interface/editors/show_scene_tree_root_selection", true);
  2081. EDITOR_DEF("_use_favorites_root_selection", false);
  2082. }