scene_tree_dock.cpp 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/project_settings.h"
  35. #include "editor/editor_feature_profile.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_scale.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/multi_node_edit.h"
  40. #include "editor/plugins/animation_player_editor_plugin.h"
  41. #include "editor/plugins/canvas_item_editor_plugin.h"
  42. #include "editor/plugins/script_editor_plugin.h"
  43. #include "editor/plugins/spatial_editor_plugin.h"
  44. #include "editor/script_editor_debugger.h"
  45. #include "scene/main/viewport.h"
  46. #include "scene/resources/packed_scene.h"
  47. #include "modules/modules_enabled.gen.h" // For regex.
  48. void SceneTreeDock::_nodes_drag_begin() {
  49. if (restore_script_editor_on_drag) {
  50. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  51. restore_script_editor_on_drag = false;
  52. }
  53. }
  54. void SceneTreeDock::_quick_open() {
  55. instance_scenes(quick_open->get_selected_files(), scene_tree->get_selected());
  56. }
  57. void SceneTreeDock::_input(Ref<InputEvent> p_event) {
  58. Ref<InputEventMouseButton> mb = p_event;
  59. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  60. restore_script_editor_on_drag = false; //lost chance
  61. }
  62. }
  63. void SceneTreeDock::_unhandled_key_input(Ref<InputEvent> p_event) {
  64. ERR_FAIL_COND(p_event.is_null());
  65. if (get_viewport()->get_modal_stack_top()) {
  66. return; //ignore because of modal window
  67. }
  68. if (get_focus_owner() && get_focus_owner()->is_text_field()) {
  69. return;
  70. }
  71. if (!p_event->is_pressed() || p_event->is_echo()) {
  72. return;
  73. }
  74. if (ED_IS_SHORTCUT("scene_tree/rename", p_event)) {
  75. _tool_selected(TOOL_RENAME);
  76. #ifdef MODULE_REGEX_ENABLED
  77. } else if (ED_IS_SHORTCUT("scene_tree/batch_rename", p_event)) {
  78. _tool_selected(TOOL_BATCH_RENAME);
  79. #endif // MODULE_REGEX_ENABLED
  80. } else if (ED_IS_SHORTCUT("scene_tree/add_child_node", p_event)) {
  81. _tool_selected(TOOL_NEW);
  82. } else if (ED_IS_SHORTCUT("scene_tree/instance_scene", p_event)) {
  83. _tool_selected(TOOL_INSTANCE);
  84. } else if (ED_IS_SHORTCUT("scene_tree/expand_collapse_all", p_event)) {
  85. _tool_selected(TOOL_EXPAND_COLLAPSE);
  86. } else if (ED_IS_SHORTCUT("scene_tree/cut_node", p_event)) {
  87. _tool_selected(TOOL_CUT);
  88. } else if (ED_IS_SHORTCUT("scene_tree/copy_node", p_event)) {
  89. _tool_selected(TOOL_COPY);
  90. } else if (ED_IS_SHORTCUT("scene_tree/paste_node", p_event)) {
  91. _tool_selected(TOOL_PASTE);
  92. } else if (ED_IS_SHORTCUT("scene_tree/change_node_type", p_event)) {
  93. _tool_selected(TOOL_REPLACE);
  94. } else if (ED_IS_SHORTCUT("scene_tree/duplicate", p_event)) {
  95. _tool_selected(TOOL_DUPLICATE);
  96. } else if (ED_IS_SHORTCUT("scene_tree/attach_script", p_event)) {
  97. _tool_selected(TOOL_ATTACH_SCRIPT);
  98. } else if (ED_IS_SHORTCUT("scene_tree/detach_script", p_event)) {
  99. _tool_selected(TOOL_DETACH_SCRIPT);
  100. } else if (ED_IS_SHORTCUT("scene_tree/move_up", p_event)) {
  101. _tool_selected(TOOL_MOVE_UP);
  102. } else if (ED_IS_SHORTCUT("scene_tree/move_down", p_event)) {
  103. _tool_selected(TOOL_MOVE_DOWN);
  104. } else if (ED_IS_SHORTCUT("scene_tree/reparent", p_event)) {
  105. _tool_selected(TOOL_REPARENT);
  106. } else if (ED_IS_SHORTCUT("scene_tree/merge_from_scene", p_event)) {
  107. _tool_selected(TOOL_MERGE_FROM_SCENE);
  108. } else if (ED_IS_SHORTCUT("scene_tree/save_branch_as_scene", p_event)) {
  109. _tool_selected(TOOL_NEW_SCENE_FROM);
  110. } else if (ED_IS_SHORTCUT("scene_tree/delete_no_confirm", p_event)) {
  111. _tool_selected(TOOL_ERASE, true);
  112. } else if (ED_IS_SHORTCUT("scene_tree/copy_node_path", p_event)) {
  113. _tool_selected(TOOL_COPY_NODE_PATH);
  114. } else if (ED_IS_SHORTCUT("scene_tree/delete", p_event)) {
  115. _tool_selected(TOOL_ERASE);
  116. }
  117. }
  118. void SceneTreeDock::instance(const String &p_file) {
  119. Vector<String> scenes;
  120. scenes.push_back(p_file);
  121. instance_scenes(scenes, scene_tree->get_selected());
  122. }
  123. void SceneTreeDock::instance_scenes(const Vector<String> &p_files, Node *p_parent) {
  124. Node *parent = p_parent;
  125. if (!parent) {
  126. parent = scene_tree->get_selected();
  127. }
  128. if (!parent) {
  129. parent = edited_scene;
  130. }
  131. if (!parent) {
  132. if (p_files.size() == 1) {
  133. accept->set_text(TTR("No parent to instance a child at."));
  134. } else {
  135. accept->set_text(TTR("No parent to instance the scenes at."));
  136. }
  137. accept->popup_centered_minsize();
  138. return;
  139. };
  140. _perform_instance_scenes(p_files, parent, -1);
  141. }
  142. void SceneTreeDock::_perform_instance_scenes(const Vector<String> &p_files, Node *parent, int p_pos) {
  143. ERR_FAIL_COND(!parent);
  144. Vector<Node *> instances;
  145. bool error = false;
  146. for (int i = 0; i < p_files.size(); i++) {
  147. Ref<PackedScene> sdata = ResourceLoader::load(p_files[i]);
  148. if (!sdata.is_valid()) {
  149. current_option = -1;
  150. accept->set_text(vformat(TTR("Error loading scene from %s"), p_files[i]));
  151. accept->popup_centered_minsize();
  152. error = true;
  153. break;
  154. }
  155. Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  156. if (!instanced_scene) {
  157. current_option = -1;
  158. accept->set_text(vformat(TTR("Error instancing scene from %s"), p_files[i]));
  159. accept->popup_centered_minsize();
  160. error = true;
  161. break;
  162. }
  163. if (edited_scene->get_filename() != "") {
  164. if (_cyclical_dependency_exists(edited_scene->get_filename(), instanced_scene)) {
  165. accept->set_text(vformat(TTR("Cannot instance the scene '%s' because the current scene exists within one of its nodes."), p_files[i]));
  166. accept->popup_centered_minsize();
  167. error = true;
  168. break;
  169. }
  170. }
  171. instanced_scene->set_filename(ProjectSettings::get_singleton()->localize_path(p_files[i]));
  172. instances.push_back(instanced_scene);
  173. }
  174. if (error) {
  175. for (int i = 0; i < instances.size(); i++) {
  176. memdelete(instances[i]);
  177. }
  178. return;
  179. }
  180. editor_data->get_undo_redo().create_action(TTR("Instance Scene(s)"));
  181. for (int i = 0; i < instances.size(); i++) {
  182. Node *instanced_scene = instances[i];
  183. editor_data->get_undo_redo().add_do_method(parent, "add_child", instanced_scene);
  184. if (p_pos >= 0) {
  185. editor_data->get_undo_redo().add_do_method(parent, "move_child", instanced_scene, p_pos + i);
  186. }
  187. editor_data->get_undo_redo().add_do_method(instanced_scene, "set_owner", edited_scene);
  188. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  189. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", instanced_scene);
  190. editor_data->get_undo_redo().add_do_reference(instanced_scene);
  191. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", instanced_scene);
  192. String new_name = parent->validate_child_name(instanced_scene);
  193. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  194. editor_data->get_undo_redo().add_do_method(sed, "live_debug_instance_node", edited_scene->get_path_to(parent), p_files[i], new_name);
  195. 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)));
  196. }
  197. editor_data->get_undo_redo().commit_action();
  198. editor->push_item(instances[instances.size() - 1]);
  199. for (int i = 0; i < instances.size(); i++) {
  200. emit_signal("node_created", instances[i]);
  201. }
  202. }
  203. void SceneTreeDock::_replace_with_branch_scene(const String &p_file, Node *base) {
  204. Ref<PackedScene> sdata = ResourceLoader::load(p_file);
  205. if (!sdata.is_valid()) {
  206. accept->set_text(vformat(TTR("Error loading scene from %s"), p_file));
  207. accept->popup_centered_minsize();
  208. return;
  209. }
  210. Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  211. if (!instanced_scene) {
  212. accept->set_text(vformat(TTR("Error instancing scene from %s"), p_file));
  213. accept->popup_centered_minsize();
  214. return;
  215. }
  216. UndoRedo *undo_redo = editor->get_undo_redo();
  217. undo_redo->create_action(TTR("Replace with Branch Scene"));
  218. Node *parent = base->get_parent();
  219. int pos = base->get_index();
  220. undo_redo->add_do_method(parent, "remove_child", base);
  221. undo_redo->add_undo_method(parent, "remove_child", instanced_scene);
  222. undo_redo->add_do_method(parent, "add_child", instanced_scene);
  223. undo_redo->add_undo_method(parent, "add_child", base);
  224. undo_redo->add_do_method(parent, "move_child", instanced_scene, pos);
  225. undo_redo->add_undo_method(parent, "move_child", base, pos);
  226. List<Node *> owned;
  227. base->get_owned_by(base->get_owner(), &owned);
  228. Array owners;
  229. for (List<Node *>::Element *F = owned.front(); F; F = F->next()) {
  230. owners.push_back(F->get());
  231. }
  232. undo_redo->add_do_method(instanced_scene, "set_owner", edited_scene);
  233. undo_redo->add_undo_method(this, "_set_owners", edited_scene, owners);
  234. undo_redo->add_do_method(editor_selection, "clear");
  235. undo_redo->add_undo_method(editor_selection, "clear");
  236. undo_redo->add_do_method(editor_selection, "add_node", instanced_scene);
  237. undo_redo->add_undo_method(editor_selection, "add_node", base);
  238. undo_redo->add_do_property(scene_tree, "set_selected", instanced_scene);
  239. undo_redo->add_undo_property(scene_tree, "set_selected", base);
  240. undo_redo->add_do_reference(instanced_scene);
  241. undo_redo->add_undo_reference(base);
  242. undo_redo->commit_action();
  243. }
  244. bool SceneTreeDock::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
  245. int childCount = p_desired_node->get_child_count();
  246. if (_track_inherit(p_target_scene_path, p_desired_node)) {
  247. return true;
  248. }
  249. for (int i = 0; i < childCount; i++) {
  250. Node *child = p_desired_node->get_child(i);
  251. if (_cyclical_dependency_exists(p_target_scene_path, child)) {
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. bool SceneTreeDock::_track_inherit(const String &p_target_scene_path, Node *p_desired_node) {
  258. Node *p = p_desired_node;
  259. bool result = false;
  260. Vector<Node *> instances;
  261. while (true) {
  262. if (p->get_filename() == p_target_scene_path) {
  263. result = true;
  264. break;
  265. }
  266. Ref<SceneState> ss = p->get_scene_inherited_state();
  267. if (ss.is_valid()) {
  268. String path = ss->get_path();
  269. Ref<PackedScene> data = ResourceLoader::load(path);
  270. if (data.is_valid()) {
  271. p = data->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  272. if (!p) {
  273. continue;
  274. }
  275. instances.push_back(p);
  276. } else {
  277. break;
  278. }
  279. } else {
  280. break;
  281. }
  282. }
  283. for (int i = 0; i < instances.size(); i++) {
  284. memdelete(instances[i]);
  285. }
  286. return result;
  287. }
  288. void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
  289. current_option = p_tool;
  290. switch (p_tool) {
  291. #ifdef MODULE_REGEX_ENABLED
  292. case TOOL_BATCH_RENAME: {
  293. if (!profile_allow_editing) {
  294. break;
  295. }
  296. if (editor_selection->get_selected_node_list().size() > 1) {
  297. rename_dialog->popup_centered();
  298. }
  299. } break;
  300. #endif // MODULE_REGEX_ENABLED
  301. case TOOL_RENAME: {
  302. if (!profile_allow_editing) {
  303. break;
  304. }
  305. Tree *tree = scene_tree->get_scene_tree();
  306. if (tree->is_anything_selected()) {
  307. tree->grab_focus();
  308. tree->edit_selected();
  309. }
  310. } break;
  311. case TOOL_NEW:
  312. case TOOL_REPARENT_TO_NEW_NODE: {
  313. if (!profile_allow_editing) {
  314. break;
  315. }
  316. if (reset_create_dialog && !p_confirm_override) {
  317. create_dialog->set_base_type("Node");
  318. reset_create_dialog = false;
  319. }
  320. // Prefer nodes that inherit from the current scene root.
  321. Node *current_edited_scene_root = EditorNode::get_singleton()->get_edited_scene();
  322. if (current_edited_scene_root) {
  323. String root_class = current_edited_scene_root->get_class_name();
  324. static Vector<String> preferred_types;
  325. if (preferred_types.empty()) {
  326. preferred_types.push_back("Control");
  327. preferred_types.push_back("Node2D");
  328. preferred_types.push_back("Spatial");
  329. }
  330. for (int i = 0; i < preferred_types.size(); i++) {
  331. if (ClassDB::is_parent_class(root_class, preferred_types[i])) {
  332. create_dialog->set_preferred_search_result_type(preferred_types[i]);
  333. break;
  334. }
  335. }
  336. }
  337. create_dialog->popup_create(true);
  338. if (!p_confirm_override) {
  339. emit_signal("add_node_used");
  340. }
  341. } break;
  342. case TOOL_INSTANCE: {
  343. if (!profile_allow_editing) {
  344. break;
  345. }
  346. Node *scene = edited_scene;
  347. if (!scene) {
  348. EditorNode::get_singleton()->new_inherited_scene();
  349. break;
  350. }
  351. quick_open->popup_dialog("PackedScene", true);
  352. quick_open->set_title(TTR("Instance Child Scene"));
  353. if (!p_confirm_override) {
  354. emit_signal("add_node_used");
  355. }
  356. } break;
  357. case TOOL_EXPAND_COLLAPSE: {
  358. if (!scene_tree->get_selected()) {
  359. break;
  360. }
  361. Tree *tree = scene_tree->get_scene_tree();
  362. TreeItem *selected_item = tree->get_selected();
  363. if (!selected_item) {
  364. selected_item = tree->get_root();
  365. }
  366. bool collapsed = _is_collapsed_recursive(selected_item);
  367. _set_collapsed_recursive(selected_item, !collapsed);
  368. tree->ensure_cursor_is_visible();
  369. } break;
  370. case TOOL_CUT:
  371. case TOOL_COPY: {
  372. if (!edited_scene || !_validate_no_foreign()) {
  373. break;
  374. }
  375. List<Node *> selection = editor_selection->get_selected_node_list();
  376. if (selection.size() == 0) {
  377. break;
  378. }
  379. if (!node_clipboard.empty()) {
  380. _clear_clipboard();
  381. }
  382. clipboard_source_scene = editor->get_edited_scene()->get_filename();
  383. selection.sort_custom<Node::Comparator>();
  384. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  385. Node *node = E->get();
  386. Map<const Node *, Node *> duplimap;
  387. Node *dup = node->duplicate_from_editor(duplimap);
  388. ERR_CONTINUE(!dup);
  389. node_clipboard.push_back(dup);
  390. }
  391. if (p_tool == TOOL_CUT) {
  392. _delete_confirm(true);
  393. }
  394. } break;
  395. case TOOL_PASTE: {
  396. if (node_clipboard.empty() || !edited_scene) {
  397. break;
  398. }
  399. bool has_cycle = false;
  400. if (edited_scene->get_filename() != String()) {
  401. for (List<Node *>::Element *E = node_clipboard.front(); E; E = E->next()) {
  402. if (edited_scene->get_filename() == E->get()->get_filename()) {
  403. has_cycle = true;
  404. break;
  405. }
  406. }
  407. }
  408. if (has_cycle) {
  409. current_option = -1;
  410. accept->set_text(TTR("Can't paste root node into the same scene."));
  411. accept->popup_centered();
  412. break;
  413. }
  414. Node *paste_parent = edited_scene;
  415. List<Node *> selection = editor_selection->get_selected_node_list();
  416. if (selection.size() > 0) {
  417. paste_parent = selection.back()->get();
  418. }
  419. Node *owner = paste_parent->get_owner();
  420. if (!owner) {
  421. owner = paste_parent;
  422. }
  423. editor_data->get_undo_redo().create_action(TTR("Paste Node(s)"));
  424. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  425. Map<RES, RES> resource_remap;
  426. String target_scene = editor->get_edited_scene()->get_filename();
  427. if (target_scene != clipboard_source_scene) {
  428. if (!clipboard_resource_remap.has(target_scene)) {
  429. Map<RES, RES> remap;
  430. for (List<Node *>::Element *E = node_clipboard.front(); E; E = E->next()) {
  431. _create_remap_for_node(E->get(), remap);
  432. }
  433. clipboard_resource_remap[target_scene] = remap;
  434. }
  435. resource_remap = clipboard_resource_remap[target_scene];
  436. }
  437. for (List<Node *>::Element *E = node_clipboard.front(); E; E = E->next()) {
  438. Node *node = E->get();
  439. Map<const Node *, Node *> duplimap;
  440. Node *dup = node->duplicate_from_editor(duplimap, resource_remap);
  441. ERR_CONTINUE(!dup);
  442. editor_data->get_undo_redo().add_do_method(paste_parent, "add_child", dup);
  443. for (Map<const Node *, Node *>::Element *E2 = duplimap.front(); E2; E2 = E2->next()) {
  444. Node *d = E2->value();
  445. editor_data->get_undo_redo().add_do_method(d, "set_owner", owner);
  446. }
  447. editor_data->get_undo_redo().add_do_method(dup, "set_owner", owner);
  448. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", dup);
  449. editor_data->get_undo_redo().add_undo_method(paste_parent, "remove_child", dup);
  450. editor_data->get_undo_redo().add_do_reference(dup);
  451. if (node_clipboard.size() == 1) {
  452. editor_data->get_undo_redo().add_do_method(editor, "push_item", dup);
  453. }
  454. }
  455. editor_data->get_undo_redo().commit_action();
  456. } break;
  457. case TOOL_REPLACE: {
  458. if (!profile_allow_editing) {
  459. break;
  460. }
  461. if (!_validate_no_foreign()) {
  462. break;
  463. }
  464. if (!_validate_no_instance()) {
  465. break;
  466. }
  467. if (reset_create_dialog) {
  468. create_dialog->set_base_type("Node");
  469. reset_create_dialog = false;
  470. }
  471. Node *selected = scene_tree->get_selected();
  472. if (!selected && !editor_selection->get_selected_node_list().empty()) {
  473. selected = editor_selection->get_selected_node_list().front()->get();
  474. }
  475. if (selected) {
  476. create_dialog->popup_create(false, true, selected->get_class());
  477. }
  478. } break;
  479. case TOOL_EXTEND_SCRIPT: {
  480. attach_script_to_selected(true);
  481. } break;
  482. case TOOL_ATTACH_SCRIPT: {
  483. attach_script_to_selected(false);
  484. } break;
  485. case TOOL_DETACH_SCRIPT: {
  486. if (!profile_allow_script_editing) {
  487. break;
  488. }
  489. Array selection = editor_selection->get_selected_nodes();
  490. if (selection.empty()) {
  491. return;
  492. }
  493. editor_data->get_undo_redo().create_action(TTR("Detach Script"));
  494. editor_data->get_undo_redo().add_do_method(editor, "push_item", (Script *)nullptr);
  495. for (int i = 0; i < selection.size(); i++) {
  496. Node *n = Object::cast_to<Node>(selection[i]);
  497. Ref<Script> existing = n->get_script();
  498. Ref<Script> empty = EditorNode::get_singleton()->get_object_custom_type_base(n);
  499. if (existing != empty) {
  500. editor_data->get_undo_redo().add_do_method(n, "set_script", empty);
  501. editor_data->get_undo_redo().add_undo_method(n, "set_script", existing);
  502. }
  503. }
  504. editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
  505. editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
  506. editor_data->get_undo_redo().commit_action();
  507. } break;
  508. case TOOL_MOVE_UP:
  509. case TOOL_MOVE_DOWN: {
  510. if (!profile_allow_editing) {
  511. break;
  512. }
  513. if (!scene_tree->get_selected()) {
  514. break;
  515. }
  516. if (scene_tree->get_selected() == edited_scene) {
  517. current_option = -1;
  518. accept->set_text(TTR("This operation can't be done on the tree root."));
  519. accept->popup_centered_minsize();
  520. break;
  521. }
  522. if (!_validate_no_foreign()) {
  523. break;
  524. }
  525. bool MOVING_DOWN = (p_tool == TOOL_MOVE_DOWN);
  526. bool MOVING_UP = !MOVING_DOWN;
  527. Node *common_parent = scene_tree->get_selected()->get_parent();
  528. List<Node *> selection = editor_selection->get_selected_node_list();
  529. selection.sort_custom<Node::Comparator>(); // sort by index
  530. if (MOVING_DOWN) {
  531. selection.invert();
  532. }
  533. int lowest_id = common_parent->get_child_count() - 1;
  534. int highest_id = 0;
  535. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  536. int index = E->get()->get_index();
  537. if (index > highest_id) {
  538. highest_id = index;
  539. }
  540. if (index < lowest_id) {
  541. lowest_id = index;
  542. }
  543. if (E->get()->get_parent() != common_parent) {
  544. common_parent = nullptr;
  545. }
  546. }
  547. if (!common_parent || (MOVING_DOWN && highest_id >= common_parent->get_child_count() - MOVING_DOWN) || (MOVING_UP && lowest_id == 0)) {
  548. break; // one or more nodes can not be moved
  549. }
  550. if (selection.size() == 1) {
  551. editor_data->get_undo_redo().create_action(TTR("Move Node In Parent"));
  552. }
  553. if (selection.size() > 1) {
  554. editor_data->get_undo_redo().create_action(TTR("Move Nodes In Parent"));
  555. }
  556. for (int i = 0; i < selection.size(); i++) {
  557. Node *top_node = selection[i];
  558. Node *bottom_node = selection[selection.size() - 1 - i];
  559. ERR_FAIL_COND(!top_node->get_parent());
  560. ERR_FAIL_COND(!bottom_node->get_parent());
  561. int bottom_node_pos = bottom_node->get_index();
  562. int top_node_pos_next = top_node->get_index() + (MOVING_DOWN ? 1 : -1);
  563. editor_data->get_undo_redo().add_do_method(top_node->get_parent(), "move_child", top_node, top_node_pos_next);
  564. editor_data->get_undo_redo().add_undo_method(bottom_node->get_parent(), "move_child", bottom_node, bottom_node_pos);
  565. }
  566. editor_data->get_undo_redo().commit_action();
  567. } break;
  568. case TOOL_DUPLICATE: {
  569. if (!profile_allow_editing) {
  570. break;
  571. }
  572. if (!edited_scene) {
  573. break;
  574. }
  575. if (editor_selection->is_selected(edited_scene)) {
  576. current_option = -1;
  577. accept->set_text(TTR("This operation can't be done on the tree root."));
  578. accept->popup_centered_minsize();
  579. break;
  580. }
  581. if (!_validate_no_foreign()) {
  582. break;
  583. }
  584. List<Node *> selection = editor_selection->get_selected_node_list();
  585. if (selection.size() == 0) {
  586. break;
  587. }
  588. editor_data->get_undo_redo().create_action(TTR("Duplicate Node(s)"));
  589. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  590. Node *dupsingle = nullptr;
  591. selection.sort_custom<Node::Comparator>();
  592. Node *add_below_node = selection.back()->get();
  593. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  594. Node *node = E->get();
  595. Node *parent = node->get_parent();
  596. List<Node *> owned;
  597. node->get_owned_by(node->get_owner(), &owned);
  598. Map<const Node *, Node *> duplimap;
  599. Node *dup = node->duplicate_from_editor(duplimap);
  600. ERR_CONTINUE(!dup);
  601. if (selection.size() == 1) {
  602. dupsingle = dup;
  603. }
  604. dup->set_name(parent->validate_child_name(dup));
  605. editor_data->get_undo_redo().add_do_method(parent, "add_child_below_node", add_below_node, dup);
  606. for (List<Node *>::Element *F = owned.front(); F; F = F->next()) {
  607. if (!duplimap.has(F->get())) {
  608. continue;
  609. }
  610. Node *d = duplimap[F->get()];
  611. editor_data->get_undo_redo().add_do_method(d, "set_owner", node->get_owner());
  612. }
  613. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", dup);
  614. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", dup);
  615. editor_data->get_undo_redo().add_do_reference(dup);
  616. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  617. editor_data->get_undo_redo().add_do_method(sed, "live_debug_duplicate_node", edited_scene->get_path_to(node), dup->get_name());
  618. 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())));
  619. add_below_node = dup;
  620. }
  621. editor_data->get_undo_redo().commit_action();
  622. if (dupsingle) {
  623. editor->push_item(dupsingle);
  624. }
  625. } break;
  626. case TOOL_REPARENT: {
  627. if (!profile_allow_editing) {
  628. break;
  629. }
  630. if (!scene_tree->get_selected()) {
  631. break;
  632. }
  633. if (editor_selection->is_selected(edited_scene)) {
  634. current_option = -1;
  635. accept->set_text(TTR("This operation can't be done on the tree root."));
  636. accept->popup_centered_minsize();
  637. break;
  638. }
  639. if (!_validate_no_foreign()) {
  640. break;
  641. }
  642. List<Node *> nodes = editor_selection->get_selected_node_list();
  643. Set<Node *> nodeset;
  644. for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
  645. nodeset.insert(E->get());
  646. }
  647. reparent_dialog->popup_centered_ratio();
  648. reparent_dialog->set_current(nodeset);
  649. } break;
  650. case TOOL_MAKE_ROOT: {
  651. if (!profile_allow_editing) {
  652. break;
  653. }
  654. List<Node *> nodes = editor_selection->get_selected_node_list();
  655. ERR_FAIL_COND(nodes.size() != 1);
  656. Node *node = nodes.front()->get();
  657. Node *root = get_tree()->get_edited_scene_root();
  658. if (node == root) {
  659. return;
  660. }
  661. //check that from node to root, all owners are right
  662. if (root->get_scene_inherited_state().is_valid()) {
  663. accept->set_text(TTR("Can't reparent nodes in inherited scenes, order of nodes can't change."));
  664. accept->popup_centered_minsize();
  665. return;
  666. }
  667. if (node->get_owner() != root) {
  668. accept->set_text(TTR("Node must belong to the edited scene to become root."));
  669. accept->popup_centered_minsize();
  670. return;
  671. }
  672. if (node->get_filename() != String()) {
  673. accept->set_text(TTR("Instantiated scenes can't become root"));
  674. accept->popup_centered_minsize();
  675. return;
  676. }
  677. editor_data->get_undo_redo().create_action(TTR("Make node as Root"));
  678. editor_data->get_undo_redo().add_do_method(node->get_parent(), "remove_child", node);
  679. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", node);
  680. editor_data->get_undo_redo().add_do_method(node, "add_child", root);
  681. editor_data->get_undo_redo().add_do_method(node, "set_filename", root->get_filename());
  682. editor_data->get_undo_redo().add_do_method(root, "set_filename", String());
  683. editor_data->get_undo_redo().add_do_method(node, "set_owner", (Object *)nullptr);
  684. editor_data->get_undo_redo().add_do_method(root, "set_owner", node);
  685. _node_replace_owner(root, root, node, MODE_DO);
  686. editor_data->get_undo_redo().add_undo_method(root, "set_filename", root->get_filename());
  687. editor_data->get_undo_redo().add_undo_method(node, "set_filename", String());
  688. editor_data->get_undo_redo().add_undo_method(node, "remove_child", root);
  689. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", root);
  690. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "add_child", node);
  691. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "move_child", node, node->get_index());
  692. editor_data->get_undo_redo().add_undo_method(root, "set_owner", (Object *)nullptr);
  693. editor_data->get_undo_redo().add_undo_method(node, "set_owner", root);
  694. _node_replace_owner(root, root, root, MODE_UNDO);
  695. editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
  696. editor_data->get_undo_redo().add_undo_method(scene_tree, "update_tree");
  697. editor_data->get_undo_redo().commit_action();
  698. } break;
  699. case TOOL_MULTI_EDIT: {
  700. if (!profile_allow_editing) {
  701. break;
  702. }
  703. Node *root = EditorNode::get_singleton()->get_edited_scene();
  704. if (!root) {
  705. break;
  706. }
  707. Ref<MultiNodeEdit> mne = memnew(MultiNodeEdit);
  708. for (const Map<Node *, Object *>::Element *E = editor_selection->get_selection().front(); E; E = E->next()) {
  709. mne->add_node(root->get_path_to(E->key()));
  710. }
  711. EditorNode::get_singleton()->push_item(mne.ptr());
  712. } break;
  713. case TOOL_ERASE: {
  714. if (!profile_allow_editing) {
  715. break;
  716. }
  717. List<Node *> remove_list = editor_selection->get_selected_node_list();
  718. if (remove_list.empty()) {
  719. return;
  720. }
  721. if (!_validate_no_foreign()) {
  722. break;
  723. }
  724. if (p_confirm_override) {
  725. _delete_confirm();
  726. } else {
  727. String msg;
  728. if (remove_list.size() > 1) {
  729. bool any_children = false;
  730. for (int i = 0; !any_children && i < remove_list.size(); i++) {
  731. any_children = remove_list[i]->get_child_count() > 0;
  732. }
  733. msg = vformat(any_children ? TTR("Delete %d nodes and any children?") : TTR("Delete %d nodes?"), remove_list.size());
  734. } else {
  735. Node *node = remove_list[0];
  736. if (node == editor_data->get_edited_scene_root()) {
  737. msg = vformat(TTR("Delete the root node \"%s\"?"), node->get_name());
  738. } else if (node->get_filename() == "" && node->get_child_count() > 0) {
  739. // Display this message only for non-instanced scenes
  740. msg = vformat(TTR("Delete node \"%s\" and its children?"), node->get_name());
  741. } else {
  742. msg = vformat(TTR("Delete node \"%s\"?"), node->get_name());
  743. }
  744. }
  745. delete_dialog->set_text(msg);
  746. // Resize the dialog to its minimum size.
  747. // This prevents the dialog from being too wide after displaying
  748. // a deletion confirmation for a node with a long name.
  749. delete_dialog->set_size(Size2());
  750. delete_dialog->popup_centered_minsize();
  751. }
  752. } break;
  753. case TOOL_MERGE_FROM_SCENE: {
  754. if (!profile_allow_editing) {
  755. break;
  756. }
  757. EditorNode::get_singleton()->merge_from_scene();
  758. } break;
  759. case TOOL_NEW_SCENE_FROM: {
  760. if (!profile_allow_editing) {
  761. break;
  762. }
  763. Node *scene = editor_data->get_edited_scene_root();
  764. if (!scene) {
  765. accept->set_text(TTR("Saving the branch as a scene requires having a scene open in the editor."));
  766. accept->popup_centered_minsize();
  767. break;
  768. }
  769. List<Node *> selection = editor_selection->get_selected_node_list();
  770. if (selection.size() != 1) {
  771. accept->set_text(vformat(TTR("Saving the branch as a scene requires selecting only one node, but you have selected %d nodes."), selection.size()));
  772. accept->popup_centered_minsize();
  773. break;
  774. }
  775. Node *tocopy = selection.front()->get();
  776. if (tocopy == scene) {
  777. accept->set_text(TTR("Can't save the root node branch as an instanced scene.\nTo create an editable copy of the current scene, duplicate it using the FileSystem dock context menu\nor create an inherited scene using Scene > New Inherited Scene... instead."));
  778. accept->popup_centered_minsize();
  779. break;
  780. }
  781. if (tocopy != editor_data->get_edited_scene_root() && tocopy->get_filename() != "") {
  782. accept->set_text(TTR("Can't save the branch of an already instanced scene.\nTo create a variation of a scene, you can make an inherited scene based on the instanced scene using Scene > New Inherited Scene... instead."));
  783. accept->popup_centered_minsize();
  784. break;
  785. }
  786. new_scene_from_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  787. List<String> extensions;
  788. Ref<PackedScene> sd = memnew(PackedScene);
  789. ResourceSaver::get_recognized_extensions(sd, &extensions);
  790. new_scene_from_dialog->clear_filters();
  791. for (int i = 0; i < extensions.size(); i++) {
  792. new_scene_from_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  793. }
  794. String existing;
  795. if (extensions.size()) {
  796. String root_name(tocopy->get_name());
  797. existing = root_name + "." + extensions.front()->get().to_lower();
  798. }
  799. new_scene_from_dialog->set_current_path(existing);
  800. new_scene_from_dialog->popup_centered_ratio();
  801. new_scene_from_dialog->set_title(TTR("Save New Scene As..."));
  802. } break;
  803. case TOOL_COPY_NODE_PATH: {
  804. List<Node *> selection = editor_selection->get_selected_node_list();
  805. List<Node *>::Element *e = selection.front();
  806. if (e) {
  807. Node *node = e->get();
  808. if (node) {
  809. Node *root = EditorNode::get_singleton()->get_edited_scene();
  810. NodePath path = root->get_path().rel_path_to(node->get_path());
  811. OS::get_singleton()->set_clipboard(path);
  812. }
  813. }
  814. } break;
  815. case TOOL_OPEN_DOCUMENTATION: {
  816. List<Node *> selection = editor_selection->get_selected_node_list();
  817. for (int i = 0; i < selection.size(); i++) {
  818. ScriptEditor::get_singleton()->goto_help("class_name:" + selection[i]->get_class());
  819. }
  820. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  821. } break;
  822. case TOOL_SCENE_EDITABLE_CHILDREN: {
  823. if (!profile_allow_editing) {
  824. break;
  825. }
  826. List<Node *> selection = editor_selection->get_selected_node_list();
  827. List<Node *>::Element *e = selection.front();
  828. if (e) {
  829. Node *node = e->get();
  830. if (node) {
  831. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(node);
  832. if (editable) {
  833. editable_instance_remove_dialog->set_text(TTR("Disabling \"editable_instance\" will cause all properties of the node to be reverted to their default."));
  834. editable_instance_remove_dialog->popup_centered_minsize();
  835. break;
  836. }
  837. _toggle_editable_children(node);
  838. }
  839. }
  840. } break;
  841. case TOOL_SCENE_USE_PLACEHOLDER: {
  842. if (!profile_allow_editing) {
  843. break;
  844. }
  845. List<Node *> selection = editor_selection->get_selected_node_list();
  846. List<Node *>::Element *e = selection.front();
  847. if (e) {
  848. Node *node = e->get();
  849. if (node) {
  850. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(node);
  851. bool placeholder = node->get_scene_instance_load_placeholder();
  852. // Fire confirmation dialog when children are editable.
  853. if (editable && !placeholder) {
  854. placeholder_editable_instance_remove_dialog->set_text(TTR("Enabling \"Load As Placeholder\" will disable \"Editable Children\" and cause all properties of the node to be reverted to their default."));
  855. placeholder_editable_instance_remove_dialog->popup_centered_minsize();
  856. break;
  857. }
  858. placeholder = !placeholder;
  859. if (placeholder) {
  860. EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(node, false);
  861. }
  862. node->set_scene_instance_load_placeholder(placeholder);
  863. scene_tree->update_tree();
  864. }
  865. }
  866. } break;
  867. case TOOL_SCENE_MAKE_LOCAL: {
  868. if (!profile_allow_editing) {
  869. break;
  870. }
  871. List<Node *> selection = editor_selection->get_selected_node_list();
  872. List<Node *>::Element *e = selection.front();
  873. if (e) {
  874. Node *node = e->get();
  875. if (node) {
  876. Node *root = EditorNode::get_singleton()->get_edited_scene();
  877. UndoRedo *undo_redo = &editor_data->get_undo_redo();
  878. if (!root) {
  879. break;
  880. }
  881. ERR_FAIL_COND(node->get_filename() == String());
  882. undo_redo->create_action(TTR("Make Local"));
  883. undo_redo->add_do_method(node, "set_filename", "");
  884. undo_redo->add_undo_method(node, "set_filename", node->get_filename());
  885. _node_replace_owner(node, node, root);
  886. undo_redo->add_do_method(scene_tree, "update_tree");
  887. undo_redo->add_undo_method(scene_tree, "update_tree");
  888. undo_redo->commit_action();
  889. }
  890. }
  891. } break;
  892. case TOOL_SCENE_OPEN: {
  893. List<Node *> selection = editor_selection->get_selected_node_list();
  894. List<Node *>::Element *e = selection.front();
  895. if (e) {
  896. Node *node = e->get();
  897. if (node) {
  898. scene_tree->emit_signal("open", node->get_filename());
  899. }
  900. }
  901. } break;
  902. case TOOL_SCENE_CLEAR_INHERITANCE: {
  903. if (!profile_allow_editing) {
  904. break;
  905. }
  906. clear_inherit_confirm->popup_centered_minsize();
  907. } break;
  908. case TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM: {
  909. if (!profile_allow_editing) {
  910. break;
  911. }
  912. List<Node *> selection = editor_selection->get_selected_node_list();
  913. List<Node *>::Element *e = selection.front();
  914. if (e) {
  915. Node *node = e->get();
  916. if (node) {
  917. node->set_scene_inherited_state(Ref<SceneState>());
  918. scene_tree->update_tree();
  919. EditorNode::get_singleton()->get_inspector()->update_tree();
  920. }
  921. }
  922. } break;
  923. case TOOL_SCENE_OPEN_INHERITED: {
  924. List<Node *> selection = editor_selection->get_selected_node_list();
  925. List<Node *>::Element *e = selection.front();
  926. if (e) {
  927. Node *node = e->get();
  928. if (node && node->get_scene_inherited_state().is_valid()) {
  929. scene_tree->emit_signal("open", node->get_scene_inherited_state()->get_path());
  930. }
  931. }
  932. } break;
  933. case TOOL_CREATE_2D_SCENE:
  934. case TOOL_CREATE_3D_SCENE:
  935. case TOOL_CREATE_USER_INTERFACE:
  936. case TOOL_CREATE_FAVORITE: {
  937. Node *new_node = nullptr;
  938. if (TOOL_CREATE_FAVORITE == p_tool) {
  939. String name = selected_favorite_root.get_slicec(' ', 0);
  940. if (ScriptServer::is_global_class(name)) {
  941. new_node = Object::cast_to<Node>(ClassDB::instance(ScriptServer::get_global_class_native_base(name)));
  942. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(name), "Script");
  943. if (new_node && script.is_valid()) {
  944. new_node->set_script(script.get_ref_ptr());
  945. new_node->set_name(name);
  946. }
  947. } else {
  948. new_node = Object::cast_to<Node>(ClassDB::instance(selected_favorite_root));
  949. }
  950. if (!new_node) {
  951. new_node = memnew(Node);
  952. ERR_PRINT("Creating root from favorite '" + selected_favorite_root + "' failed. Creating 'Node' instead.");
  953. }
  954. } else {
  955. switch (p_tool) {
  956. case TOOL_CREATE_2D_SCENE:
  957. new_node = memnew(Node2D);
  958. break;
  959. case TOOL_CREATE_3D_SCENE:
  960. new_node = memnew(Spatial);
  961. break;
  962. case TOOL_CREATE_USER_INTERFACE: {
  963. Control *node = memnew(Control);
  964. node->set_anchors_and_margins_preset(PRESET_WIDE); //more useful for resizable UIs.
  965. new_node = node;
  966. } break;
  967. }
  968. }
  969. editor_data->get_undo_redo().create_action(TTR("New Scene Root"));
  970. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", new_node);
  971. editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
  972. editor_data->get_undo_redo().add_do_reference(new_node);
  973. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", (Object *)nullptr);
  974. editor_data->get_undo_redo().commit_action();
  975. editor->edit_node(new_node);
  976. editor_selection->clear();
  977. editor_selection->add_node(new_node);
  978. scene_tree->get_scene_tree()->grab_focus();
  979. } break;
  980. default: {
  981. if (p_tool >= EDIT_SUBRESOURCE_BASE) {
  982. int idx = p_tool - EDIT_SUBRESOURCE_BASE;
  983. ERR_FAIL_INDEX(idx, subresources.size());
  984. Object *obj = ObjectDB::get_instance(subresources[idx]);
  985. ERR_FAIL_COND(!obj);
  986. editor->push_item(obj);
  987. }
  988. }
  989. }
  990. }
  991. void SceneTreeDock::_property_selected(int p_idx) {
  992. ERR_FAIL_NULL(property_drop_node);
  993. _perform_property_drop(property_drop_node, menu_properties->get_item_metadata(p_idx), ResourceLoader::load(resource_drop_path));
  994. property_drop_node = nullptr;
  995. }
  996. void SceneTreeDock::_perform_property_drop(Node *p_node, String p_property, RES p_res) {
  997. editor_data->get_undo_redo().create_action(vformat(TTR("Set %s"), p_property));
  998. editor_data->get_undo_redo().add_do_property(p_node, p_property, p_res);
  999. editor_data->get_undo_redo().add_do_method(p_node, "property_list_changed_notify");
  1000. editor_data->get_undo_redo().add_undo_property(p_node, p_property, p_node->get(p_property));
  1001. editor_data->get_undo_redo().add_undo_method(p_node, "property_list_changed_notify");
  1002. editor_data->get_undo_redo().commit_action();
  1003. }
  1004. void SceneTreeDock::_node_collapsed(Object *p_obj) {
  1005. TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
  1006. if (!ti) {
  1007. return;
  1008. }
  1009. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1010. _set_collapsed_recursive(ti, ti->is_collapsed());
  1011. }
  1012. }
  1013. void SceneTreeDock::_notification(int p_what) {
  1014. switch (p_what) {
  1015. case NOTIFICATION_READY: {
  1016. if (!first_enter) {
  1017. break;
  1018. }
  1019. first_enter = false;
  1020. EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", this, "_feature_profile_changed");
  1021. CanvasItemEditorPlugin *canvas_item_plugin = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
  1022. if (canvas_item_plugin) {
  1023. canvas_item_plugin->get_canvas_item_editor()->connect("item_lock_status_changed", scene_tree, "_update_tree");
  1024. canvas_item_plugin->get_canvas_item_editor()->connect("item_group_status_changed", scene_tree, "_update_tree");
  1025. scene_tree->connect("node_changed", canvas_item_plugin->get_canvas_item_editor()->get_viewport_control(), "update");
  1026. }
  1027. SpatialEditorPlugin *spatial_editor_plugin = Object::cast_to<SpatialEditorPlugin>(editor_data->get_editor("3D"));
  1028. spatial_editor_plugin->get_spatial_editor()->connect("item_lock_status_changed", scene_tree, "_update_tree");
  1029. spatial_editor_plugin->get_spatial_editor()->connect("item_group_status_changed", scene_tree, "_update_tree");
  1030. button_add->set_icon(get_icon("Add", "EditorIcons"));
  1031. button_instance->set_icon(get_icon("Instance", "EditorIcons"));
  1032. button_create_script->set_icon(get_icon("ScriptCreate", "EditorIcons"));
  1033. button_detach_script->set_icon(get_icon("ScriptRemove", "EditorIcons"));
  1034. filter->set_right_icon(get_icon("Search", "EditorIcons"));
  1035. filter->set_clear_button_enabled(true);
  1036. EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", this, "_selection_changed");
  1037. scene_tree->get_scene_tree()->connect("item_collapsed", this, "_node_collapsed");
  1038. // create_root_dialog
  1039. HBoxContainer *top_row = memnew(HBoxContainer);
  1040. top_row->set_name("NodeShortcutsTopRow");
  1041. top_row->set_h_size_flags(SIZE_EXPAND_FILL);
  1042. top_row->add_child(memnew(Label(TTR("Create Root Node:"))));
  1043. top_row->add_spacer();
  1044. ToolButton *node_shortcuts_toggle = memnew(ToolButton);
  1045. node_shortcuts_toggle->set_name("NodeShortcutsToggle");
  1046. node_shortcuts_toggle->set_icon(get_icon("Favorites", "EditorIcons"));
  1047. node_shortcuts_toggle->set_toggle_mode(true);
  1048. node_shortcuts_toggle->set_pressed(EDITOR_GET("_use_favorites_root_selection"));
  1049. node_shortcuts_toggle->set_anchors_and_margins_preset(Control::PRESET_CENTER_RIGHT);
  1050. node_shortcuts_toggle->connect("pressed", this, "_update_create_root_dialog");
  1051. top_row->add_child(node_shortcuts_toggle);
  1052. create_root_dialog->add_child(top_row);
  1053. VBoxContainer *node_shortcuts = memnew(VBoxContainer);
  1054. node_shortcuts->set_name("NodeShortcuts");
  1055. VBoxContainer *beginner_node_shortcuts = memnew(VBoxContainer);
  1056. beginner_node_shortcuts->set_name("BeginnerNodeShortcuts");
  1057. node_shortcuts->add_child(beginner_node_shortcuts);
  1058. button_2d = memnew(Button);
  1059. beginner_node_shortcuts->add_child(button_2d);
  1060. button_2d->set_text(TTR("2D Scene"));
  1061. button_2d->set_icon(get_icon("Node2D", "EditorIcons"));
  1062. button_2d->connect("pressed", this, "_tool_selected", make_binds(TOOL_CREATE_2D_SCENE, false));
  1063. button_3d = memnew(Button);
  1064. beginner_node_shortcuts->add_child(button_3d);
  1065. button_3d->set_text(TTR("3D Scene"));
  1066. button_3d->set_icon(get_icon("Spatial", "EditorIcons"));
  1067. button_3d->connect("pressed", this, "_tool_selected", make_binds(TOOL_CREATE_3D_SCENE, false));
  1068. button_ui = memnew(Button);
  1069. beginner_node_shortcuts->add_child(button_ui);
  1070. button_ui->set_text(TTR("User Interface"));
  1071. button_ui->set_icon(get_icon("Control", "EditorIcons"));
  1072. button_ui->connect("pressed", this, "_tool_selected", make_binds(TOOL_CREATE_USER_INTERFACE, false));
  1073. VBoxContainer *favorite_node_shortcuts = memnew(VBoxContainer);
  1074. favorite_node_shortcuts->set_name("FavoriteNodeShortcuts");
  1075. node_shortcuts->add_child(favorite_node_shortcuts);
  1076. button_custom = memnew(Button);
  1077. node_shortcuts->add_child(button_custom);
  1078. button_custom->set_text(TTR("Other Node"));
  1079. button_custom->set_icon(get_icon("Add", "EditorIcons"));
  1080. button_custom->connect("pressed", this, "_tool_selected", make_binds(TOOL_NEW, false));
  1081. node_shortcuts->add_spacer();
  1082. create_root_dialog->add_child(node_shortcuts);
  1083. _update_create_root_dialog();
  1084. } break;
  1085. case NOTIFICATION_ENTER_TREE: {
  1086. clear_inherit_confirm->connect("confirmed", this, "_tool_selected", varray(TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM));
  1087. } break;
  1088. case NOTIFICATION_EXIT_TREE: {
  1089. clear_inherit_confirm->disconnect("confirmed", this, "_tool_selected");
  1090. } break;
  1091. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1092. button_add->set_icon(get_icon("Add", "EditorIcons"));
  1093. button_instance->set_icon(get_icon("Instance", "EditorIcons"));
  1094. button_create_script->set_icon(get_icon("ScriptCreate", "EditorIcons"));
  1095. button_detach_script->set_icon(get_icon("ScriptRemove", "EditorIcons"));
  1096. button_2d->set_icon(get_icon("Node2D", "EditorIcons"));
  1097. button_3d->set_icon(get_icon("Spatial", "EditorIcons"));
  1098. button_ui->set_icon(get_icon("Control", "EditorIcons"));
  1099. button_custom->set_icon(get_icon("Add", "EditorIcons"));
  1100. filter->set_right_icon(get_icon("Search", "EditorIcons"));
  1101. filter->set_clear_button_enabled(true);
  1102. } break;
  1103. case NOTIFICATION_PROCESS: {
  1104. bool show_create_root = bool(EDITOR_GET("interface/editors/show_scene_tree_root_selection")) && get_tree()->get_edited_scene_root() == nullptr;
  1105. if (show_create_root != create_root_dialog->is_visible_in_tree() && !remote_tree->is_visible()) {
  1106. if (show_create_root) {
  1107. create_root_dialog->show();
  1108. scene_tree->hide();
  1109. } else {
  1110. create_root_dialog->hide();
  1111. scene_tree->show();
  1112. }
  1113. }
  1114. } break;
  1115. }
  1116. }
  1117. void SceneTreeDock::_node_replace_owner(Node *p_base, Node *p_node, Node *p_root, ReplaceOwnerMode p_mode) {
  1118. if (p_node->get_owner() == p_base && p_node != p_root) {
  1119. UndoRedo *undo_redo = &editor_data->get_undo_redo();
  1120. switch (p_mode) {
  1121. case MODE_BIDI: {
  1122. undo_redo->add_do_method(p_node, "set_owner", p_root);
  1123. undo_redo->add_undo_method(p_node, "set_owner", p_base);
  1124. } break;
  1125. case MODE_DO: {
  1126. undo_redo->add_do_method(p_node, "set_owner", p_root);
  1127. } break;
  1128. case MODE_UNDO: {
  1129. undo_redo->add_undo_method(p_node, "set_owner", p_root);
  1130. } break;
  1131. }
  1132. }
  1133. for (int i = 0; i < p_node->get_child_count(); i++) {
  1134. _node_replace_owner(p_base, p_node->get_child(i), p_root, p_mode);
  1135. }
  1136. }
  1137. void SceneTreeDock::_load_request(const String &p_path) {
  1138. editor->open_request(p_path);
  1139. }
  1140. void SceneTreeDock::_script_open_request(const Ref<Script> &p_script) {
  1141. editor->edit_resource(p_script);
  1142. }
  1143. void SceneTreeDock::_node_selected() {
  1144. Node *node = scene_tree->get_selected();
  1145. if (!node) {
  1146. return;
  1147. }
  1148. if (ScriptEditor::get_singleton()->is_visible_in_tree()) {
  1149. restore_script_editor_on_drag = true;
  1150. }
  1151. editor->push_item(node);
  1152. }
  1153. void SceneTreeDock::_node_renamed() {
  1154. _node_selected();
  1155. }
  1156. void SceneTreeDock::_set_owners(Node *p_owner, const Array &p_nodes) {
  1157. for (int i = 0; i < p_nodes.size(); i++) {
  1158. Node *n = Object::cast_to<Node>(p_nodes[i]);
  1159. if (!n) {
  1160. continue;
  1161. }
  1162. n->set_owner(p_owner);
  1163. }
  1164. }
  1165. void SceneTreeDock::_fill_path_renames(Vector<StringName> base_path, Vector<StringName> new_base_path, Node *p_node, Map<Node *, NodePath> *p_renames) {
  1166. base_path.push_back(p_node->get_name());
  1167. if (new_base_path.size()) {
  1168. new_base_path.push_back(p_node->get_name());
  1169. }
  1170. NodePath new_path;
  1171. if (new_base_path.size()) {
  1172. new_path = NodePath(new_base_path, true);
  1173. }
  1174. p_renames->insert(p_node, new_path);
  1175. for (int i = 0; i < p_node->get_child_count(); i++) {
  1176. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), p_renames);
  1177. }
  1178. }
  1179. void SceneTreeDock::fill_path_renames(Node *p_node, Node *p_new_parent, Map<Node *, NodePath> *p_renames) {
  1180. Vector<StringName> base_path;
  1181. Node *n = p_node->get_parent();
  1182. while (n) {
  1183. base_path.push_back(n->get_name());
  1184. n = n->get_parent();
  1185. }
  1186. base_path.invert();
  1187. Vector<StringName> new_base_path;
  1188. if (p_new_parent) {
  1189. n = p_new_parent;
  1190. while (n) {
  1191. new_base_path.push_back(n->get_name());
  1192. n = n->get_parent();
  1193. }
  1194. new_base_path.invert();
  1195. }
  1196. _fill_path_renames(base_path, new_base_path, p_node, p_renames);
  1197. }
  1198. bool SceneTreeDock::_update_node_path(Node *p_root_node, NodePath &r_node_path, Map<Node *, NodePath> *p_renames) const {
  1199. Node *target_node = p_root_node->get_node_or_null(r_node_path);
  1200. ERR_FAIL_NULL_V_MSG(target_node, false, "Found invalid node path '" + String(r_node_path) + "' on node '" + String(scene_root->get_path_to(p_root_node)) + "'");
  1201. // Try to find the target node in modified node paths.
  1202. Map<Node *, NodePath>::Element *found_node_path = p_renames->find(target_node);
  1203. if (found_node_path) {
  1204. Map<Node *, NodePath>::Element *found_root_path = p_renames->find(p_root_node);
  1205. NodePath root_path_new = found_root_path ? found_root_path->get() : p_root_node->get_path();
  1206. r_node_path = root_path_new.rel_path_to(found_node_path->get());
  1207. return true;
  1208. }
  1209. // Update the path if the base node has changed and has not been deleted.
  1210. Map<Node *, NodePath>::Element *found_root_path = p_renames->find(p_root_node);
  1211. if (found_root_path) {
  1212. NodePath root_path_new = found_root_path->get();
  1213. if (!root_path_new.is_empty()) {
  1214. NodePath old_abs_path = NodePath(String(p_root_node->get_path()).plus_file(r_node_path));
  1215. old_abs_path.simplify();
  1216. r_node_path = root_path_new.rel_path_to(old_abs_path);
  1217. }
  1218. return true;
  1219. }
  1220. return false;
  1221. }
  1222. bool SceneTreeDock::_check_node_path_recursive(Node *p_root_node, Variant &r_variant, Map<Node *, NodePath> *p_renames) const {
  1223. switch (r_variant.get_type()) {
  1224. case Variant::NODE_PATH: {
  1225. NodePath node_path = r_variant;
  1226. if (!node_path.is_empty() && _update_node_path(p_root_node, node_path, p_renames)) {
  1227. r_variant = node_path;
  1228. return true;
  1229. }
  1230. } break;
  1231. case Variant::ARRAY: {
  1232. Array a = r_variant;
  1233. bool updated = false;
  1234. for (int i = 0; i < a.size(); i++) {
  1235. Variant value = a[i];
  1236. if (_check_node_path_recursive(p_root_node, value, p_renames)) {
  1237. if (!updated) {
  1238. a = a.duplicate(); // Need to duplicate for undo-redo to work.
  1239. updated = true;
  1240. }
  1241. a[i] = value;
  1242. }
  1243. }
  1244. if (updated) {
  1245. r_variant = a;
  1246. return true;
  1247. }
  1248. } break;
  1249. case Variant::DICTIONARY: {
  1250. Dictionary d = r_variant;
  1251. bool updated = false;
  1252. for (int i = 0; i < d.size(); i++) {
  1253. Variant value = d.get_value_at_index(i);
  1254. if (_check_node_path_recursive(p_root_node, value, p_renames)) {
  1255. if (!updated) {
  1256. d = d.duplicate(); // Need to duplicate for undo-redo to work.
  1257. updated = true;
  1258. }
  1259. d[d.get_key_at_index(i)] = value;
  1260. }
  1261. }
  1262. if (updated) {
  1263. r_variant = d;
  1264. return true;
  1265. }
  1266. } break;
  1267. default: {
  1268. }
  1269. }
  1270. return false;
  1271. }
  1272. void SceneTreeDock::perform_node_renames(Node *p_base, Map<Node *, NodePath> *p_renames, Map<Ref<Animation>, Set<int>> *r_rem_anims) {
  1273. Map<Ref<Animation>, Set<int>> rem_anims;
  1274. if (!r_rem_anims) {
  1275. r_rem_anims = &rem_anims;
  1276. }
  1277. if (!p_base) {
  1278. p_base = edited_scene;
  1279. }
  1280. if (!p_base) {
  1281. return;
  1282. }
  1283. // No renaming if base node is deleted.
  1284. Map<Node *, NodePath>::Element *found_base_path = p_renames->find(p_base);
  1285. if (found_base_path && found_base_path->get().is_empty()) {
  1286. return;
  1287. }
  1288. // Renaming node paths used in node properties.
  1289. List<PropertyInfo> properties;
  1290. p_base->get_property_list(&properties);
  1291. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  1292. if (!(E->get().usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR))) {
  1293. continue;
  1294. }
  1295. String propertyname = E->get().name;
  1296. Variant old_variant = p_base->get(propertyname);
  1297. Variant updated_variant = old_variant;
  1298. if (_check_node_path_recursive(p_base, updated_variant, p_renames)) {
  1299. editor_data->get_undo_redo().add_do_property(p_base, propertyname, updated_variant);
  1300. editor_data->get_undo_redo().add_undo_property(p_base, propertyname, old_variant);
  1301. p_base->set(propertyname, updated_variant);
  1302. }
  1303. }
  1304. bool autorename_animation_tracks = bool(EDITOR_DEF("editors/animation/autorename_animation_tracks", true));
  1305. if (autorename_animation_tracks && Object::cast_to<AnimationPlayer>(p_base)) {
  1306. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_base);
  1307. List<StringName> anims;
  1308. ap->get_animation_list(&anims);
  1309. Node *root = ap->get_node(ap->get_root());
  1310. if (root) {
  1311. Map<Node *, NodePath>::Element *found_root_path = p_renames->find(root);
  1312. NodePath new_root_path = found_root_path ? found_root_path->get() : root->get_path();
  1313. if (!new_root_path.is_empty()) { // No renaming if root node is deleted.
  1314. for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
  1315. Ref<Animation> anim = ap->get_animation(E->get());
  1316. if (!r_rem_anims->has(anim)) {
  1317. r_rem_anims->insert(anim, Set<int>());
  1318. Set<int> &ran = r_rem_anims->find(anim)->get();
  1319. for (int i = 0; i < anim->get_track_count(); i++) {
  1320. ran.insert(i);
  1321. }
  1322. }
  1323. Set<int> &ran = r_rem_anims->find(anim)->get();
  1324. if (anim.is_null()) {
  1325. continue;
  1326. }
  1327. for (int i = 0; i < anim->get_track_count(); i++) {
  1328. NodePath track_np = anim->track_get_path(i);
  1329. Node *n = root->get_node(track_np);
  1330. if (!n) {
  1331. continue;
  1332. }
  1333. if (!ran.has(i)) {
  1334. continue; //channel was removed
  1335. }
  1336. Map<Node *, NodePath>::Element *found_path = p_renames->find(n);
  1337. if (found_path) {
  1338. if (found_path->get() == NodePath()) {
  1339. //will be erased
  1340. int idx = 0;
  1341. Set<int>::Element *EI = ran.front();
  1342. ERR_FAIL_COND(!EI); //bug
  1343. while (EI->get() != i) {
  1344. idx++;
  1345. EI = EI->next();
  1346. ERR_FAIL_COND(!EI); //another bug
  1347. }
  1348. editor_data->get_undo_redo().add_do_method(anim.ptr(), "remove_track", idx);
  1349. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "add_track", anim->track_get_type(i), idx);
  1350. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", idx, track_np);
  1351. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_interpolation_type", idx, anim->track_get_interpolation_type(i));
  1352. for (int j = 0; j < anim->track_get_key_count(i); j++) {
  1353. 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));
  1354. }
  1355. ran.erase(i); //byebye channel
  1356. } else {
  1357. //will be renamed
  1358. NodePath rel_path = new_root_path.rel_path_to(found_path->get());
  1359. NodePath new_path = NodePath(rel_path.get_names(), track_np.get_subnames(), false);
  1360. if (new_path == track_np) {
  1361. continue; //bleh
  1362. }
  1363. editor_data->get_undo_redo().add_do_method(anim.ptr(), "track_set_path", i, new_path);
  1364. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", i, track_np);
  1365. }
  1366. }
  1367. }
  1368. }
  1369. }
  1370. }
  1371. }
  1372. for (int i = 0; i < p_base->get_child_count(); i++) {
  1373. perform_node_renames(p_base->get_child(i), p_renames, r_rem_anims);
  1374. }
  1375. }
  1376. void SceneTreeDock::_node_prerenamed(Node *p_node, const String &p_new_name) {
  1377. Map<Node *, NodePath> path_renames;
  1378. Vector<StringName> base_path;
  1379. Node *n = p_node->get_parent();
  1380. while (n) {
  1381. base_path.push_back(n->get_name());
  1382. n = n->get_parent();
  1383. }
  1384. base_path.invert();
  1385. Vector<StringName> new_base_path = base_path;
  1386. base_path.push_back(p_node->get_name());
  1387. new_base_path.push_back(p_new_name);
  1388. NodePath new_path(new_base_path, true);
  1389. path_renames[p_node] = new_path;
  1390. for (int i = 0; i < p_node->get_child_count(); i++) {
  1391. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), &path_renames);
  1392. }
  1393. perform_node_renames(nullptr, &path_renames);
  1394. }
  1395. bool SceneTreeDock::_validate_no_foreign() {
  1396. List<Node *> selection = editor_selection->get_selected_node_list();
  1397. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1398. if (E->get() != edited_scene && E->get()->get_owner() != edited_scene) {
  1399. accept->set_text(TTR("Can't operate on nodes from a foreign scene!"));
  1400. accept->popup_centered_minsize();
  1401. return false;
  1402. }
  1403. // When edited_scene inherits from another one the root Node will be the parent Scene,
  1404. // we don't want to consider that Node a foreign one otherwise we would not be able to
  1405. // delete it.
  1406. if (edited_scene->get_scene_inherited_state().is_valid() && edited_scene == E->get()) {
  1407. continue;
  1408. }
  1409. 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) {
  1410. accept->set_text(TTR("Can't operate on nodes the current scene inherits from!"));
  1411. accept->popup_centered_minsize();
  1412. return false;
  1413. }
  1414. }
  1415. return true;
  1416. }
  1417. bool SceneTreeDock::_validate_no_instance() {
  1418. List<Node *> selection = editor_selection->get_selected_node_list();
  1419. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1420. if (E->get() != edited_scene && E->get()->get_filename() != "") {
  1421. accept->set_text(TTR("This operation can't be done on instanced scenes."));
  1422. accept->popup_centered();
  1423. return false;
  1424. }
  1425. }
  1426. return true;
  1427. }
  1428. void SceneTreeDock::_node_reparent(NodePath p_path, bool p_keep_global_xform) {
  1429. Node *new_parent = scene_root->get_node(p_path);
  1430. ERR_FAIL_COND(!new_parent);
  1431. List<Node *> selection = editor_selection->get_selected_node_list();
  1432. if (selection.empty()) {
  1433. return; // Nothing to reparent.
  1434. }
  1435. Vector<Node *> nodes;
  1436. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1437. nodes.push_back(E->get());
  1438. }
  1439. _do_reparent(new_parent, -1, nodes, p_keep_global_xform);
  1440. }
  1441. void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, Vector<Node *> p_nodes, bool p_keep_global_xform) {
  1442. Node *new_parent = p_new_parent;
  1443. ERR_FAIL_COND(!new_parent);
  1444. if (p_nodes.size() == 0) {
  1445. return; // Nothing to reparent.
  1446. }
  1447. p_nodes.sort_custom<Node::Comparator>(); //Makes result reliable.
  1448. bool no_change = true;
  1449. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1450. if (p_nodes[ni] == p_new_parent) {
  1451. return; // Attempt to reparent to itself.
  1452. }
  1453. if (p_nodes[ni]->get_parent() != p_new_parent || p_position_in_parent + ni != p_nodes[ni]->get_position_in_parent()) {
  1454. no_change = false;
  1455. }
  1456. }
  1457. if (no_change) {
  1458. return; // Position and parent didn't change.
  1459. }
  1460. Node *validate = new_parent;
  1461. while (validate) {
  1462. ERR_FAIL_COND_MSG(p_nodes.find(validate) != -1, "Selection changed at some point. Can't reparent.");
  1463. validate = validate->get_parent();
  1464. }
  1465. // Sort by tree order, so re-adding is easy.
  1466. p_nodes.sort_custom<Node::Comparator>();
  1467. editor_data->get_undo_redo().create_action(TTR("Reparent Node"));
  1468. Map<Node *, NodePath> path_renames;
  1469. Vector<StringName> former_names;
  1470. int inc = 0;
  1471. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1472. // No undo implemented for this yet.
  1473. Node *node = p_nodes[ni];
  1474. fill_path_renames(node, new_parent, &path_renames);
  1475. former_names.push_back(node->get_name());
  1476. List<Node *> owned;
  1477. node->get_owned_by(node->get_owner(), &owned);
  1478. Array owners;
  1479. for (List<Node *>::Element *E = owned.front(); E; E = E->next()) {
  1480. owners.push_back(E->get());
  1481. }
  1482. if (new_parent == node->get_parent() && node->get_index() < p_position_in_parent + ni) {
  1483. inc--; // If the child will generate a gap when moved, adjust.
  1484. }
  1485. editor_data->get_undo_redo().add_do_method(node->get_parent(), "remove_child", node);
  1486. editor_data->get_undo_redo().add_do_method(new_parent, "add_child", node);
  1487. if (p_position_in_parent >= 0) {
  1488. editor_data->get_undo_redo().add_do_method(new_parent, "move_child", node, p_position_in_parent + inc);
  1489. }
  1490. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  1491. String old_name = former_names[ni];
  1492. String new_name = new_parent->validate_child_name(node);
  1493. // Name was modified, fix the path renames.
  1494. if (old_name.casecmp_to(new_name) != 0) {
  1495. // Fix the to name to have the new name.
  1496. Map<Node *, NodePath>::Element *found_path = path_renames.find(node);
  1497. if (found_path) {
  1498. NodePath old_new_name = found_path->get();
  1499. Vector<StringName> unfixed_new_names = old_new_name.get_names();
  1500. Vector<StringName> fixed_new_names;
  1501. // Get last name and replace with fixed new name.
  1502. for (int a = 0; a < (unfixed_new_names.size() - 1); a++) {
  1503. fixed_new_names.push_back(unfixed_new_names[a]);
  1504. }
  1505. fixed_new_names.push_back(new_name);
  1506. NodePath fixed_node_path = NodePath(fixed_new_names, true);
  1507. path_renames[node] = fixed_node_path;
  1508. } else {
  1509. ERR_PRINT("Internal error. Can't find renamed path for node '" + node->get_path() + "'");
  1510. }
  1511. }
  1512. 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);
  1513. 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());
  1514. if (p_keep_global_xform) {
  1515. if (Object::cast_to<Node2D>(node)) {
  1516. editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Node2D>(node)->get_global_transform());
  1517. }
  1518. if (Object::cast_to<Spatial>(node)) {
  1519. editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Spatial>(node)->get_global_transform());
  1520. }
  1521. if (Object::cast_to<Control>(node)) {
  1522. editor_data->get_undo_redo().add_do_method(node, "set_global_position", Object::cast_to<Control>(node)->get_global_position());
  1523. }
  1524. }
  1525. editor_data->get_undo_redo().add_do_method(this, "_set_owners", edited_scene, owners);
  1526. if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == node) {
  1527. editor_data->get_undo_redo().add_do_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", node);
  1528. }
  1529. editor_data->get_undo_redo().add_undo_method(new_parent, "remove_child", node);
  1530. editor_data->get_undo_redo().add_undo_method(node, "set_name", former_names[ni]);
  1531. inc++;
  1532. }
  1533. // Add and move in a second step (so old order is preserved).
  1534. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1535. Node *node = p_nodes[ni];
  1536. List<Node *> owned;
  1537. node->get_owned_by(node->get_owner(), &owned);
  1538. Array owners;
  1539. for (List<Node *>::Element *E = owned.front(); E; E = E->next()) {
  1540. owners.push_back(E->get());
  1541. }
  1542. int child_pos = node->get_position_in_parent();
  1543. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "add_child", node);
  1544. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "move_child", node, child_pos);
  1545. editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
  1546. if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == node) {
  1547. editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", node);
  1548. }
  1549. if (p_keep_global_xform) {
  1550. if (Object::cast_to<Node2D>(node)) {
  1551. editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Node2D>(node)->get_transform());
  1552. }
  1553. if (Object::cast_to<Spatial>(node)) {
  1554. editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Spatial>(node)->get_transform());
  1555. }
  1556. if (Object::cast_to<Control>(node)) {
  1557. editor_data->get_undo_redo().add_undo_method(node, "set_position", Object::cast_to<Control>(node)->get_position());
  1558. }
  1559. }
  1560. }
  1561. perform_node_renames(nullptr, &path_renames);
  1562. editor_data->get_undo_redo().commit_action();
  1563. }
  1564. bool SceneTreeDock::_is_collapsed_recursive(TreeItem *p_item) const {
  1565. bool is_branch_collapsed = false;
  1566. List<TreeItem *> needs_check;
  1567. needs_check.push_back(p_item);
  1568. while (!needs_check.empty()) {
  1569. TreeItem *item = needs_check.back()->get();
  1570. needs_check.pop_back();
  1571. TreeItem *child = item->get_children();
  1572. is_branch_collapsed = item->is_collapsed() && child;
  1573. if (is_branch_collapsed) {
  1574. break;
  1575. }
  1576. while (child) {
  1577. needs_check.push_back(child);
  1578. child = child->get_next();
  1579. }
  1580. }
  1581. return is_branch_collapsed;
  1582. }
  1583. void SceneTreeDock::_set_collapsed_recursive(TreeItem *p_item, bool p_collapsed) {
  1584. List<TreeItem *> to_collapse;
  1585. to_collapse.push_back(p_item);
  1586. while (!to_collapse.empty()) {
  1587. TreeItem *item = to_collapse.back()->get();
  1588. to_collapse.pop_back();
  1589. item->set_collapsed(p_collapsed);
  1590. TreeItem *child = item->get_children();
  1591. while (child) {
  1592. to_collapse.push_back(child);
  1593. child = child->get_next();
  1594. }
  1595. }
  1596. }
  1597. void SceneTreeDock::_script_created(Ref<Script> p_script) {
  1598. List<Node *> selected = editor_selection->get_selected_node_list();
  1599. if (selected.empty()) {
  1600. return;
  1601. }
  1602. editor_data->get_undo_redo().create_action(TTR("Attach Script"));
  1603. for (List<Node *>::Element *E = selected.front(); E; E = E->next()) {
  1604. Ref<Script> existing = E->get()->get_script();
  1605. editor_data->get_undo_redo().add_do_method(E->get(), "set_script", p_script.get_ref_ptr());
  1606. editor_data->get_undo_redo().add_undo_method(E->get(), "set_script", existing);
  1607. editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
  1608. editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
  1609. }
  1610. editor_data->get_undo_redo().commit_action();
  1611. editor->push_item(p_script.operator->());
  1612. _update_script_button();
  1613. }
  1614. void SceneTreeDock::_script_creation_closed() {
  1615. script_create_dialog->disconnect("script_created", this, "_script_created");
  1616. }
  1617. void SceneTreeDock::_toggle_editable_children_from_selection() {
  1618. List<Node *> selection = editor_selection->get_selected_node_list();
  1619. List<Node *>::Element *e = selection.front();
  1620. if (e) {
  1621. _toggle_editable_children(e->get());
  1622. }
  1623. }
  1624. void SceneTreeDock::_toggle_placeholder_from_selection() {
  1625. List<Node *> selection = editor_selection->get_selected_node_list();
  1626. List<Node *>::Element *e = selection.front();
  1627. if (e) {
  1628. Node *node = e->get();
  1629. if (node) {
  1630. _toggle_editable_children(node);
  1631. bool placeholder = node->get_scene_instance_load_placeholder();
  1632. placeholder = !placeholder;
  1633. node->set_scene_instance_load_placeholder(placeholder);
  1634. scene_tree->update_tree();
  1635. }
  1636. }
  1637. }
  1638. void SceneTreeDock::_toggle_editable_children(Node *p_node) {
  1639. if (p_node) {
  1640. bool editable = !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node);
  1641. EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(p_node, editable);
  1642. if (editable) {
  1643. p_node->set_scene_instance_load_placeholder(false);
  1644. }
  1645. SpatialEditor::get_singleton()->update_all_gizmos(p_node);
  1646. scene_tree->update_tree();
  1647. }
  1648. }
  1649. void SceneTreeDock::_delete_confirm(bool p_cut) {
  1650. List<Node *> remove_list = editor_selection->get_selected_node_list();
  1651. if (remove_list.empty()) {
  1652. return;
  1653. }
  1654. editor->get_editor_plugins_over()->make_visible(false);
  1655. if (p_cut) {
  1656. editor_data->get_undo_redo().create_action(TTR("Cut Node(s)"));
  1657. } else {
  1658. editor_data->get_undo_redo().create_action(TTR("Remove Node(s)"));
  1659. }
  1660. bool entire_scene = false;
  1661. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  1662. if (E->get() == edited_scene) {
  1663. entire_scene = true;
  1664. }
  1665. }
  1666. if (entire_scene) {
  1667. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", (Object *)nullptr);
  1668. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", edited_scene);
  1669. editor_data->get_undo_redo().add_undo_method(edited_scene, "set_owner", edited_scene->get_owner());
  1670. editor_data->get_undo_redo().add_undo_method(scene_tree, "update_tree");
  1671. editor_data->get_undo_redo().add_undo_reference(edited_scene);
  1672. } else {
  1673. remove_list.sort_custom<Node::Comparator>(); //sort nodes to keep positions
  1674. Map<Node *, NodePath> path_renames;
  1675. //delete from animation
  1676. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  1677. Node *n = E->get();
  1678. if (!n->is_inside_tree() || !n->get_parent()) {
  1679. continue;
  1680. }
  1681. fill_path_renames(n, nullptr, &path_renames);
  1682. }
  1683. perform_node_renames(nullptr, &path_renames);
  1684. //delete for read
  1685. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  1686. Node *n = E->get();
  1687. if (!n->is_inside_tree() || !n->get_parent()) {
  1688. continue;
  1689. }
  1690. List<Node *> owned;
  1691. n->get_owned_by(n->get_owner(), &owned);
  1692. Array owners;
  1693. for (List<Node *>::Element *F = owned.front(); F; F = F->next()) {
  1694. owners.push_back(F->get());
  1695. }
  1696. editor_data->get_undo_redo().add_do_method(n->get_parent(), "remove_child", n);
  1697. editor_data->get_undo_redo().add_undo_method(n->get_parent(), "add_child", n);
  1698. editor_data->get_undo_redo().add_undo_method(n->get_parent(), "move_child", n, n->get_index());
  1699. if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == n) {
  1700. editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", n);
  1701. }
  1702. editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
  1703. editor_data->get_undo_redo().add_undo_reference(n);
  1704. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  1705. 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());
  1706. 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());
  1707. }
  1708. }
  1709. editor_data->get_undo_redo().commit_action();
  1710. // hack, force 2d editor viewport to refresh after deletion
  1711. if (CanvasItemEditor *editor = CanvasItemEditor::get_singleton()) {
  1712. editor->get_viewport_control()->update();
  1713. }
  1714. editor->push_item(nullptr);
  1715. // Fixes the EditorHistory from still offering deleted notes
  1716. EditorHistory *editor_history = EditorNode::get_singleton()->get_editor_history();
  1717. editor_history->cleanup_history();
  1718. EditorNode::get_singleton()->get_inspector_dock()->call("_prepare_history");
  1719. }
  1720. void SceneTreeDock::_update_script_button() {
  1721. if (!profile_allow_script_editing) {
  1722. button_create_script->hide();
  1723. button_detach_script->hide();
  1724. } else if (editor_selection->get_selection().size() == 0) {
  1725. button_create_script->hide();
  1726. button_detach_script->hide();
  1727. } else if (editor_selection->get_selection().size() == 1) {
  1728. Node *n = editor_selection->get_selected_node_list()[0];
  1729. if (n->get_script().is_null()) {
  1730. button_create_script->show();
  1731. button_detach_script->hide();
  1732. } else {
  1733. button_create_script->hide();
  1734. button_detach_script->show();
  1735. }
  1736. } else {
  1737. button_create_script->hide();
  1738. Array selection = editor_selection->get_selected_nodes();
  1739. for (int i = 0; i < selection.size(); i++) {
  1740. Node *n = Object::cast_to<Node>(selection[i]);
  1741. if (!n->get_script().is_null()) {
  1742. button_detach_script->show();
  1743. return;
  1744. }
  1745. }
  1746. button_detach_script->hide();
  1747. }
  1748. }
  1749. void SceneTreeDock::_selection_changed() {
  1750. int selection_size = editor_selection->get_selection().size();
  1751. if (selection_size > 1) {
  1752. //automatically turn on multi-edit
  1753. _tool_selected(TOOL_MULTI_EDIT);
  1754. } else if (selection_size == 1) {
  1755. editor->push_item(editor_selection->get_selection().front()->key());
  1756. } else if (selection_size == 0) {
  1757. editor->push_item(nullptr);
  1758. }
  1759. _update_script_button();
  1760. }
  1761. Node *SceneTreeDock::_get_selection_group_tail(Node *p_node, List<Node *> p_list) {
  1762. Node *tail = p_node;
  1763. Node *parent = tail->get_parent();
  1764. for (int i = p_node->get_position_in_parent(); i < parent->get_child_count(); i++) {
  1765. Node *sibling = parent->get_child(i);
  1766. if (p_list.find(sibling)) {
  1767. tail = sibling;
  1768. } else {
  1769. break;
  1770. }
  1771. }
  1772. return tail;
  1773. }
  1774. void SceneTreeDock::_do_create(Node *p_parent) {
  1775. Variant c = create_dialog->instance_selected();
  1776. ERR_FAIL_COND(!c);
  1777. Node *child = Object::cast_to<Node>(c);
  1778. ERR_FAIL_COND(!child);
  1779. editor_data->get_undo_redo().create_action(TTR("Create Node"));
  1780. if (edited_scene) {
  1781. editor_data->get_undo_redo().add_do_method(p_parent, "add_child", child);
  1782. editor_data->get_undo_redo().add_do_method(child, "set_owner", edited_scene);
  1783. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  1784. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", child);
  1785. editor_data->get_undo_redo().add_do_reference(child);
  1786. editor_data->get_undo_redo().add_undo_method(p_parent, "remove_child", child);
  1787. String new_name = p_parent->validate_child_name(child);
  1788. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  1789. editor_data->get_undo_redo().add_do_method(sed, "live_debug_create_node", edited_scene->get_path_to(p_parent), child->get_class(), new_name);
  1790. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(p_parent)).plus_file(new_name)));
  1791. } else {
  1792. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", child);
  1793. editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
  1794. editor_data->get_undo_redo().add_do_reference(child);
  1795. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", (Object *)nullptr);
  1796. }
  1797. editor_data->get_undo_redo().commit_action();
  1798. editor->push_item(c);
  1799. editor_selection->clear();
  1800. editor_selection->add_node(child);
  1801. if (Object::cast_to<Control>(c)) {
  1802. //make editor more comfortable, so some controls don't appear super shrunk
  1803. Control *ct = Object::cast_to<Control>(c);
  1804. Size2 ms = ct->get_minimum_size();
  1805. if (ms.width < 4) {
  1806. ms.width = 40;
  1807. }
  1808. if (ms.height < 4) {
  1809. ms.height = 40;
  1810. }
  1811. ct->set_size(ms);
  1812. }
  1813. emit_signal("node_created", c);
  1814. }
  1815. void SceneTreeDock::_create() {
  1816. if (current_option == TOOL_NEW) {
  1817. Node *parent = nullptr;
  1818. if (edited_scene) {
  1819. // If root exists in edited scene
  1820. parent = scene_tree->get_selected();
  1821. if (!parent) {
  1822. parent = edited_scene;
  1823. }
  1824. } else {
  1825. // If no root exist in edited scene
  1826. parent = scene_root;
  1827. ERR_FAIL_COND(!parent);
  1828. }
  1829. _do_create(parent);
  1830. } else if (current_option == TOOL_REPLACE) {
  1831. List<Node *> selection = editor_selection->get_selected_node_list();
  1832. ERR_FAIL_COND(selection.size() <= 0);
  1833. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  1834. ur->create_action(TTR("Change type of node(s)"));
  1835. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1836. Node *n = E->get();
  1837. ERR_FAIL_COND(!n);
  1838. Variant c = create_dialog->instance_selected();
  1839. ERR_FAIL_COND(!c);
  1840. Node *newnode = Object::cast_to<Node>(c);
  1841. ERR_FAIL_COND(!newnode);
  1842. ur->add_do_method(this, "replace_node", n, newnode, true, false);
  1843. ur->add_do_reference(newnode);
  1844. ur->add_undo_method(this, "replace_node", newnode, n, false, false);
  1845. ur->add_undo_reference(n);
  1846. }
  1847. ur->commit_action();
  1848. } else if (current_option == TOOL_REPARENT_TO_NEW_NODE) {
  1849. List<Node *> selection = editor_selection->get_selected_node_list();
  1850. ERR_FAIL_COND(selection.size() <= 0);
  1851. // Find top level node in selection
  1852. bool only_one_top_node = true;
  1853. Node *first = selection.front()->get();
  1854. ERR_FAIL_COND(!first);
  1855. int smaller_path_to_top = first->get_path_to(scene_root).get_name_count();
  1856. Node *top_node = first;
  1857. for (List<Node *>::Element *E = selection.front()->next(); E; E = E->next()) {
  1858. Node *n = E->get();
  1859. ERR_FAIL_COND(!n);
  1860. int path_length = n->get_path_to(scene_root).get_name_count();
  1861. if (top_node != n) {
  1862. if (smaller_path_to_top > path_length) {
  1863. top_node = n;
  1864. smaller_path_to_top = path_length;
  1865. only_one_top_node = true;
  1866. } else if (smaller_path_to_top == path_length) {
  1867. if (only_one_top_node && top_node->get_parent() != n->get_parent()) {
  1868. only_one_top_node = false;
  1869. }
  1870. }
  1871. }
  1872. }
  1873. Node *parent = nullptr;
  1874. if (only_one_top_node) {
  1875. parent = top_node->get_parent();
  1876. } else {
  1877. parent = top_node->get_parent()->get_parent();
  1878. }
  1879. _do_create(parent);
  1880. Vector<Node *> nodes;
  1881. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1882. nodes.push_back(E->get());
  1883. }
  1884. // This works because editor_selection was cleared and populated with last created node in _do_create()
  1885. Node *last_created = editor_selection->get_selected_node_list().front()->get();
  1886. _do_reparent(last_created, -1, nodes, true);
  1887. }
  1888. scene_tree->get_scene_tree()->grab_focus();
  1889. }
  1890. void SceneTreeDock::replace_node(Node *p_node, Node *p_by_node, bool p_keep_properties, bool p_remove_old) {
  1891. Node *n = p_node;
  1892. Node *newnode = p_by_node;
  1893. if (p_keep_properties) {
  1894. Node *default_oldnode = Object::cast_to<Node>(ClassDB::instance(n->get_class()));
  1895. List<PropertyInfo> pinfo;
  1896. n->get_property_list(&pinfo);
  1897. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1898. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  1899. continue;
  1900. }
  1901. if (E->get().name == "__meta__") {
  1902. Dictionary metadata = n->get(E->get().name);
  1903. if (metadata.has("_editor_description_")) {
  1904. newnode->set_meta("_editor_description_", metadata["_editor_description_"]);
  1905. }
  1906. if (Object::cast_to<CanvasItem>(newnode) || Object::cast_to<Spatial>(newnode)) {
  1907. if (metadata.has("_edit_group_") && metadata["_edit_group_"]) {
  1908. newnode->set_meta("_edit_group_", true);
  1909. }
  1910. if (metadata.has("_edit_lock_") && metadata["_edit_lock_"]) {
  1911. newnode->set_meta("_edit_lock_", true);
  1912. }
  1913. }
  1914. continue;
  1915. }
  1916. if (default_oldnode->get(E->get().name) != n->get(E->get().name)) {
  1917. newnode->set(E->get().name, n->get(E->get().name));
  1918. }
  1919. }
  1920. memdelete(default_oldnode);
  1921. }
  1922. editor->push_item(nullptr);
  1923. //reconnect signals
  1924. List<MethodInfo> sl;
  1925. n->get_signal_list(&sl);
  1926. for (List<MethodInfo>::Element *E = sl.front(); E; E = E->next()) {
  1927. List<Object::Connection> cl;
  1928. n->get_signal_connection_list(E->get().name, &cl);
  1929. for (List<Object::Connection>::Element *F = cl.front(); F; F = F->next()) {
  1930. Object::Connection &c = F->get();
  1931. if (!(c.flags & Object::CONNECT_PERSIST)) {
  1932. continue;
  1933. }
  1934. newnode->connect(c.signal, c.target, c.method, c.binds, Object::CONNECT_PERSIST);
  1935. }
  1936. }
  1937. String newname = n->get_name();
  1938. List<Node *> to_erase;
  1939. for (int i = 0; i < n->get_child_count(); i++) {
  1940. if (n->get_child(i)->get_owner() == nullptr && n->is_owned_by_parent()) {
  1941. to_erase.push_back(n->get_child(i));
  1942. }
  1943. }
  1944. n->replace_by(newnode, true);
  1945. if (n == edited_scene) {
  1946. edited_scene = newnode;
  1947. editor->set_edited_scene(newnode);
  1948. }
  1949. //small hack to make collisionshapes and other kind of nodes to work
  1950. for (int i = 0; i < newnode->get_child_count(); i++) {
  1951. Node *c = newnode->get_child(i);
  1952. c->call("set_transform", c->call("get_transform"));
  1953. }
  1954. //p_remove_old was added to support undo
  1955. if (p_remove_old) {
  1956. editor_data->get_undo_redo().clear_history();
  1957. }
  1958. newnode->set_name(newname);
  1959. editor->push_item(newnode);
  1960. if (p_remove_old) {
  1961. memdelete(n);
  1962. while (to_erase.front()) {
  1963. memdelete(to_erase.front()->get());
  1964. to_erase.pop_front();
  1965. }
  1966. }
  1967. }
  1968. void SceneTreeDock::set_edited_scene(Node *p_scene) {
  1969. edited_scene = p_scene;
  1970. }
  1971. void SceneTreeDock::set_selected(Node *p_node, bool p_emit_selected) {
  1972. scene_tree->set_selected(p_node, p_emit_selected);
  1973. }
  1974. void SceneTreeDock::import_subscene() {
  1975. import_subscene_dialog->popup_centered_clamped(Size2(500, 800) * EDSCALE, 0.8);
  1976. }
  1977. void SceneTreeDock::_import_subscene() {
  1978. Node *parent = scene_tree->get_selected();
  1979. if (!parent) {
  1980. parent = editor_data->get_edited_scene_root();
  1981. ERR_FAIL_COND(!parent);
  1982. }
  1983. import_subscene_dialog->move(parent, edited_scene);
  1984. editor_data->get_undo_redo().clear_history(); //no undo for now..
  1985. }
  1986. void SceneTreeDock::_new_scene_from(String p_file) {
  1987. List<Node *> selection = editor_selection->get_selected_node_list();
  1988. if (selection.size() != 1) {
  1989. accept->set_text(TTR("This operation requires a single selected node."));
  1990. accept->popup_centered_minsize();
  1991. return;
  1992. }
  1993. if (EditorNode::get_singleton()->is_scene_open(p_file)) {
  1994. accept->set_text(TTR("Can't overwrite scene that is still open!"));
  1995. accept->popup_centered_minsize();
  1996. return;
  1997. }
  1998. Node *base = selection.front()->get();
  1999. Map<Node *, Node *> reown;
  2000. reown[editor_data->get_edited_scene_root()] = base;
  2001. Node *copy = base->duplicate_and_reown(reown);
  2002. if (copy) {
  2003. Ref<PackedScene> sdata = memnew(PackedScene);
  2004. Error err = sdata->pack(copy);
  2005. memdelete(copy);
  2006. if (err != OK) {
  2007. accept->set_text(TTR("Couldn't save new scene. Likely dependencies (instances) couldn't be satisfied."));
  2008. accept->popup_centered_minsize();
  2009. return;
  2010. }
  2011. int flg = 0;
  2012. if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources")) {
  2013. flg |= ResourceSaver::FLAG_COMPRESS;
  2014. }
  2015. err = ResourceSaver::save(p_file, sdata, flg);
  2016. if (err != OK) {
  2017. accept->set_text(TTR("Error saving scene."));
  2018. accept->popup_centered_minsize();
  2019. return;
  2020. }
  2021. _replace_with_branch_scene(p_file, base);
  2022. } else {
  2023. accept->set_text(TTR("Error duplicating scene to save it."));
  2024. accept->popup_centered_minsize();
  2025. return;
  2026. }
  2027. }
  2028. static bool _is_node_visible(Node *p_node) {
  2029. if (!p_node->get_owner()) {
  2030. return false;
  2031. }
  2032. if (p_node->get_owner() != EditorNode::get_singleton()->get_edited_scene() && !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node->get_owner())) {
  2033. return false;
  2034. }
  2035. return true;
  2036. }
  2037. static bool _has_visible_children(Node *p_node) {
  2038. bool collapsed = p_node->is_displayed_folded();
  2039. if (collapsed) {
  2040. return false;
  2041. }
  2042. for (int i = 0; i < p_node->get_child_count(); i++) {
  2043. Node *child = p_node->get_child(i);
  2044. if (!_is_node_visible(child)) {
  2045. continue;
  2046. }
  2047. return true;
  2048. }
  2049. return false;
  2050. }
  2051. void SceneTreeDock::_normalize_drop(Node *&to_node, int &to_pos, int p_type) {
  2052. to_pos = -1;
  2053. if (p_type == -1) {
  2054. //drop at above selected node
  2055. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  2056. to_node = nullptr;
  2057. ERR_FAIL_MSG("Cannot perform drop above the root node!");
  2058. }
  2059. to_pos = to_node->get_index();
  2060. to_node = to_node->get_parent();
  2061. } else if (p_type == 1) {
  2062. //drop at below selected node
  2063. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  2064. //if at lower sibling of root node
  2065. to_pos = 0; //just insert at beginning of root node
  2066. return;
  2067. }
  2068. Node *lower_sibling = nullptr;
  2069. if (_has_visible_children(to_node)) {
  2070. to_pos = 0;
  2071. } else {
  2072. for (int i = to_node->get_index() + 1; i < to_node->get_parent()->get_child_count(); i++) {
  2073. Node *c = to_node->get_parent()->get_child(i);
  2074. if (_is_node_visible(c)) {
  2075. lower_sibling = c;
  2076. break;
  2077. }
  2078. }
  2079. if (lower_sibling) {
  2080. to_pos = lower_sibling->get_index();
  2081. }
  2082. to_node = to_node->get_parent();
  2083. }
  2084. }
  2085. }
  2086. void SceneTreeDock::_files_dropped(Vector<String> p_files, NodePath p_to, int p_type) {
  2087. Node *node = get_node(p_to);
  2088. ERR_FAIL_COND(!node);
  2089. if (scene_tree->get_scene_tree()->get_drop_mode_flags() & Tree::DROP_MODE_INBETWEEN) {
  2090. // Dropped PackedScene, instance it.
  2091. int to_pos = -1;
  2092. _normalize_drop(node, to_pos, p_type);
  2093. _perform_instance_scenes(p_files, node, to_pos);
  2094. } else {
  2095. String res_path = p_files[0];
  2096. StringName res_type = EditorFileSystem::get_singleton()->get_file_type(res_path);
  2097. List<String> valid_properties;
  2098. List<PropertyInfo> pinfo;
  2099. node->get_property_list(&pinfo);
  2100. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  2101. PropertyInfo &p = E->get();
  2102. if (!(p.usage & PROPERTY_USAGE_EDITOR) || !(p.usage & PROPERTY_USAGE_STORAGE) || p.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  2103. continue;
  2104. }
  2105. Vector<String> valid_types = p.hint_string.split(",");
  2106. for (int i = 0; i < valid_types.size(); i++) {
  2107. String prop_type = valid_types[i];
  2108. if (res_type == prop_type || ClassDB::is_parent_class(res_type, prop_type) || EditorNode::get_editor_data().script_class_is_parent(res_type, prop_type)) {
  2109. valid_properties.push_back(p.name);
  2110. break;
  2111. }
  2112. }
  2113. }
  2114. if (valid_properties.size() > 1) {
  2115. property_drop_node = node;
  2116. resource_drop_path = res_path;
  2117. bool capitalize = bool(EDITOR_GET("interface/inspector/capitalize_properties"));
  2118. menu_properties->clear();
  2119. for (List<String>::Element *E = valid_properties.front(); E; E = E->next()) {
  2120. String &p = E->get();
  2121. menu_properties->add_item(capitalize ? p.capitalize() : p);
  2122. menu_properties->set_item_metadata(menu_properties->get_item_count() - 1, p);
  2123. }
  2124. menu_properties->set_size(Size2(1, 1));
  2125. menu_properties->set_position(get_global_mouse_position());
  2126. menu_properties->popup();
  2127. } else if (!valid_properties.empty()) {
  2128. _perform_property_drop(node, valid_properties[0], ResourceLoader::load(res_path));
  2129. }
  2130. }
  2131. }
  2132. void SceneTreeDock::_script_dropped(String p_file, NodePath p_to) {
  2133. Ref<Script> scr = ResourceLoader::load(p_file);
  2134. ERR_FAIL_COND(!scr.is_valid());
  2135. Node *n = get_node(p_to);
  2136. if (n) {
  2137. editor_data->get_undo_redo().create_action(TTR("Attach Script"));
  2138. editor_data->get_undo_redo().add_do_method(n, "set_script", scr);
  2139. editor_data->get_undo_redo().add_undo_method(n, "set_script", n->get_script());
  2140. editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
  2141. editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
  2142. editor_data->get_undo_redo().commit_action();
  2143. }
  2144. }
  2145. void SceneTreeDock::_nodes_dragged(Array p_nodes, NodePath p_to, int p_type) {
  2146. List<Node *> selection = editor_selection->get_selected_node_list();
  2147. if (selection.empty()) {
  2148. return; //nothing to reparent
  2149. }
  2150. Node *to_node = get_node(p_to);
  2151. if (!to_node) {
  2152. return;
  2153. }
  2154. Vector<Node *> nodes;
  2155. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  2156. nodes.push_back(E->get());
  2157. }
  2158. int to_pos = -1;
  2159. _normalize_drop(to_node, to_pos, p_type);
  2160. _do_reparent(to_node, to_pos, nodes, !Input::get_singleton()->is_key_pressed(KEY_SHIFT));
  2161. }
  2162. void SceneTreeDock::_add_children_to_popup(Object *p_obj, int p_depth) {
  2163. if (p_depth > 8) {
  2164. return;
  2165. }
  2166. List<PropertyInfo> pinfo;
  2167. p_obj->get_property_list(&pinfo);
  2168. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  2169. if (!(E->get().usage & PROPERTY_USAGE_EDITOR)) {
  2170. continue;
  2171. }
  2172. if (E->get().hint != PROPERTY_HINT_RESOURCE_TYPE) {
  2173. continue;
  2174. }
  2175. Variant value = p_obj->get(E->get().name);
  2176. if (value.get_type() != Variant::OBJECT) {
  2177. continue;
  2178. }
  2179. Object *obj = value;
  2180. if (!obj) {
  2181. continue;
  2182. }
  2183. Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
  2184. if (menu->get_item_count() == 0) {
  2185. menu->add_submenu_item(TTR("Sub-Resources"), "Sub-Resources");
  2186. }
  2187. int index = menu_subresources->get_item_count();
  2188. menu_subresources->add_icon_item(icon, E->get().name.capitalize(), EDIT_SUBRESOURCE_BASE + subresources.size());
  2189. menu_subresources->set_item_h_offset(index, p_depth * 10 * EDSCALE);
  2190. subresources.push_back(obj->get_instance_id());
  2191. _add_children_to_popup(obj, p_depth + 1);
  2192. }
  2193. }
  2194. void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
  2195. if (!EditorNode::get_singleton()->get_edited_scene()) {
  2196. menu->clear();
  2197. if (profile_allow_editing) {
  2198. menu->add_icon_shortcut(get_icon("Add", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  2199. menu->add_icon_shortcut(get_icon("Instance", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANCE);
  2200. }
  2201. menu->set_size(Size2(1, 1));
  2202. menu->set_position(p_menu_pos);
  2203. menu->popup();
  2204. return;
  2205. }
  2206. List<Node *> selection = editor_selection->get_selected_node_list();
  2207. List<Node *> full_selection = editor_selection->get_full_selected_node_list(); // Above method only returns nodes with common parent.
  2208. if (selection.size() == 0) {
  2209. return;
  2210. }
  2211. menu->clear();
  2212. Ref<Script> existing_script;
  2213. bool exisiting_script_removable = true;
  2214. if (selection.size() == 1) {
  2215. Node *selected = selection[0];
  2216. if (profile_allow_editing) {
  2217. subresources.clear();
  2218. menu_subresources->clear();
  2219. menu_subresources->set_size(Size2(1, 1));
  2220. _add_children_to_popup(selection.front()->get(), 0);
  2221. if (menu->get_item_count() > 0) {
  2222. menu->add_separator();
  2223. }
  2224. menu->add_icon_shortcut(get_icon("Add", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  2225. menu->add_icon_shortcut(get_icon("Instance", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANCE);
  2226. }
  2227. menu->add_icon_shortcut(get_icon("Collapse", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/expand_collapse_all"), TOOL_EXPAND_COLLAPSE);
  2228. menu->add_separator();
  2229. existing_script = selected->get_script();
  2230. if (EditorNode::get_singleton()->get_object_custom_type_base(selected) == existing_script) {
  2231. exisiting_script_removable = false;
  2232. }
  2233. }
  2234. if (profile_allow_editing) {
  2235. menu->add_shortcut(ED_GET_SHORTCUT("scene_tree/cut_node"), TOOL_CUT);
  2236. menu->add_shortcut(ED_GET_SHORTCUT("scene_tree/copy_node"), TOOL_COPY);
  2237. if (selection.size() == 1 && !node_clipboard.empty()) {
  2238. menu->add_shortcut(ED_GET_SHORTCUT("scene_tree/paste_node"), TOOL_PASTE);
  2239. }
  2240. menu->add_separator();
  2241. }
  2242. if (profile_allow_script_editing) {
  2243. bool add_separator = false;
  2244. if (full_selection.size() == 1) {
  2245. add_separator = true;
  2246. menu->add_icon_shortcut(get_icon("ScriptCreate", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/attach_script"), TOOL_ATTACH_SCRIPT);
  2247. if (existing_script.is_valid()) {
  2248. menu->add_icon_shortcut(get_icon("ScriptExtend", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/extend_script"), TOOL_EXTEND_SCRIPT);
  2249. }
  2250. }
  2251. if (existing_script.is_valid() && exisiting_script_removable) {
  2252. add_separator = true;
  2253. menu->add_icon_shortcut(get_icon("ScriptRemove", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/detach_script"), TOOL_DETACH_SCRIPT);
  2254. } else if (full_selection.size() > 1) {
  2255. bool script_exists = false;
  2256. for (List<Node *>::Element *E = full_selection.front(); E; E = E->next()) {
  2257. if (!E->get()->get_script().is_null()) {
  2258. script_exists = true;
  2259. break;
  2260. }
  2261. }
  2262. if (script_exists) {
  2263. add_separator = true;
  2264. menu->add_icon_shortcut(get_icon("ScriptRemove", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/detach_script"), TOOL_DETACH_SCRIPT);
  2265. }
  2266. }
  2267. if (add_separator && profile_allow_editing) {
  2268. menu->add_separator();
  2269. }
  2270. }
  2271. if (profile_allow_editing) {
  2272. if (full_selection.size() == 1) {
  2273. menu->add_icon_shortcut(get_icon("Rename", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/rename"), TOOL_RENAME);
  2274. }
  2275. bool can_replace = true;
  2276. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  2277. if (E->get() != edited_scene && (E->get()->get_owner() != edited_scene || E->get()->get_filename() != "")) {
  2278. can_replace = false;
  2279. break;
  2280. }
  2281. }
  2282. if (can_replace) {
  2283. menu->add_icon_shortcut(get_icon("Reload", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/change_node_type"), TOOL_REPLACE);
  2284. }
  2285. if (scene_tree->get_selected() != edited_scene) {
  2286. menu->add_separator();
  2287. menu->add_icon_shortcut(get_icon("MoveUp", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/move_up"), TOOL_MOVE_UP);
  2288. menu->add_icon_shortcut(get_icon("MoveDown", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/move_down"), TOOL_MOVE_DOWN);
  2289. menu->add_icon_shortcut(get_icon("Duplicate", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/duplicate"), TOOL_DUPLICATE);
  2290. menu->add_icon_shortcut(get_icon("Reparent", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/reparent"), TOOL_REPARENT);
  2291. menu->add_icon_shortcut(get_icon("ReparentToNewNode", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/reparent_to_new_node"), TOOL_REPARENT_TO_NEW_NODE);
  2292. if (selection.size() == 1) {
  2293. menu->add_icon_shortcut(get_icon("NewRoot", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/make_root"), TOOL_MAKE_ROOT);
  2294. }
  2295. }
  2296. }
  2297. if (selection.size() == 1) {
  2298. if (profile_allow_editing) {
  2299. menu->add_separator();
  2300. menu->add_icon_shortcut(get_icon("Blend", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/merge_from_scene"), TOOL_MERGE_FROM_SCENE);
  2301. menu->add_icon_shortcut(get_icon("CreateNewSceneFrom", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/save_branch_as_scene"), TOOL_NEW_SCENE_FROM);
  2302. }
  2303. if (full_selection.size() == 1) {
  2304. menu->add_separator();
  2305. menu->add_icon_shortcut(get_icon("CopyNodePath", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/copy_node_path"), TOOL_COPY_NODE_PATH);
  2306. }
  2307. bool is_external = (selection[0]->get_filename() != "");
  2308. if (is_external) {
  2309. bool is_inherited = selection[0]->get_scene_inherited_state() != nullptr;
  2310. bool is_top_level = selection[0]->get_owner() == nullptr;
  2311. if (is_inherited && is_top_level) {
  2312. menu->add_separator();
  2313. if (profile_allow_editing) {
  2314. menu->add_item(TTR("Clear Inheritance"), TOOL_SCENE_CLEAR_INHERITANCE);
  2315. }
  2316. menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Open in Editor"), TOOL_SCENE_OPEN_INHERITED);
  2317. } else if (!is_top_level) {
  2318. menu->add_separator();
  2319. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(selection[0]);
  2320. bool placeholder = selection[0]->get_scene_instance_load_placeholder();
  2321. if (profile_allow_editing) {
  2322. menu->add_check_item(TTR("Editable Children"), TOOL_SCENE_EDITABLE_CHILDREN);
  2323. menu->add_check_item(TTR("Load As Placeholder"), TOOL_SCENE_USE_PLACEHOLDER);
  2324. menu->add_item(TTR("Make Local"), TOOL_SCENE_MAKE_LOCAL);
  2325. }
  2326. menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Open in Editor"), TOOL_SCENE_OPEN);
  2327. if (profile_allow_editing) {
  2328. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Editable Children")), editable);
  2329. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Load As Placeholder")), placeholder);
  2330. }
  2331. }
  2332. }
  2333. }
  2334. #ifdef MODULE_REGEX_ENABLED
  2335. if (profile_allow_editing && selection.size() > 1) {
  2336. //this is not a commonly used action, it makes no sense for it to be where it was nor always present.
  2337. menu->add_separator();
  2338. menu->add_icon_shortcut(get_icon("Rename", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/batch_rename"), TOOL_BATCH_RENAME);
  2339. }
  2340. #endif // MODULE_REGEX_ENABLED
  2341. menu->add_separator();
  2342. menu->add_icon_item(get_icon("Help", "EditorIcons"), TTR("Open Documentation"), TOOL_OPEN_DOCUMENTATION);
  2343. if (profile_allow_editing) {
  2344. menu->add_separator();
  2345. menu->add_icon_shortcut(get_icon("Remove", "EditorIcons"), ED_SHORTCUT("scene_tree/delete", TTR("Delete Node(s)"), KEY_DELETE), TOOL_ERASE);
  2346. }
  2347. menu->set_size(Size2(1, 1));
  2348. menu->set_position(p_menu_pos);
  2349. menu->popup();
  2350. }
  2351. void SceneTreeDock::_filter_changed(const String &p_filter) {
  2352. scene_tree->set_filter(p_filter);
  2353. }
  2354. String SceneTreeDock::get_filter() {
  2355. return filter->get_text();
  2356. }
  2357. void SceneTreeDock::set_filter(const String &p_filter) {
  2358. filter->set_text(p_filter);
  2359. scene_tree->set_filter(p_filter);
  2360. }
  2361. void SceneTreeDock::save_branch_to_file(String p_directory) {
  2362. new_scene_from_dialog->set_current_dir(p_directory);
  2363. _tool_selected(TOOL_NEW_SCENE_FROM);
  2364. }
  2365. void SceneTreeDock::_focus_node() {
  2366. Node *node = scene_tree->get_selected();
  2367. ERR_FAIL_COND(!node);
  2368. if (node->is_class("CanvasItem")) {
  2369. CanvasItemEditorPlugin *editor = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
  2370. editor->get_canvas_item_editor()->focus_selection();
  2371. } else {
  2372. SpatialEditorPlugin *editor = Object::cast_to<SpatialEditorPlugin>(editor_data->get_editor("3D"));
  2373. editor->get_spatial_editor()->get_editor_viewport(0)->focus_selection();
  2374. }
  2375. }
  2376. void SceneTreeDock::attach_script_to_selected(bool p_extend) {
  2377. if (ScriptServer::get_language_count() == 0) {
  2378. EditorNode::get_singleton()->show_warning(TTR("Cannot attach a script: there are no languages registered.\nThis is probably because this editor was built with all language modules disabled."));
  2379. return;
  2380. }
  2381. if (!profile_allow_script_editing) {
  2382. return;
  2383. }
  2384. List<Node *> selection = editor_selection->get_selected_node_list();
  2385. if (selection.empty()) {
  2386. return;
  2387. }
  2388. Node *selected = scene_tree->get_selected();
  2389. if (!selected) {
  2390. selected = selection.front()->get();
  2391. }
  2392. Ref<Script> existing = selected->get_script();
  2393. String path = selected->get_filename();
  2394. if (path == "") {
  2395. String root_path = editor_data->get_edited_scene_root()->get_filename();
  2396. if (root_path == "") {
  2397. path = String("res://").plus_file(selected->get_name());
  2398. } else {
  2399. path = root_path.get_base_dir().plus_file(selected->get_name());
  2400. }
  2401. }
  2402. String inherits = selected->get_class();
  2403. if (p_extend && existing.is_valid()) {
  2404. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  2405. ScriptLanguage *l = ScriptServer::get_language(i);
  2406. if (l->get_type() == existing->get_class()) {
  2407. String name = l->get_global_class_name(existing->get_path());
  2408. if (ScriptServer::is_global_class(name) && EDITOR_GET("interface/editors/derive_script_globals_by_name").operator bool()) {
  2409. inherits = name;
  2410. } else if (l->can_inherit_from_file()) {
  2411. inherits = "\"" + existing->get_path() + "\"";
  2412. }
  2413. break;
  2414. }
  2415. }
  2416. }
  2417. script_create_dialog->connect("script_created", this, "_script_created");
  2418. script_create_dialog->connect("popup_hide", this, "_script_creation_closed", varray(), CONNECT_ONESHOT);
  2419. script_create_dialog->set_inheritance_base_type("Node");
  2420. script_create_dialog->config(inherits, path);
  2421. script_create_dialog->popup_centered();
  2422. }
  2423. void SceneTreeDock::open_script_dialog(Node *p_for_node, bool p_extend) {
  2424. scene_tree->set_selected(p_for_node, false);
  2425. if (p_extend) {
  2426. _tool_selected(TOOL_EXTEND_SCRIPT);
  2427. } else {
  2428. _tool_selected(TOOL_ATTACH_SCRIPT);
  2429. }
  2430. }
  2431. void SceneTreeDock::open_add_child_dialog() {
  2432. create_dialog->set_base_type("CanvasItem");
  2433. _tool_selected(TOOL_NEW, true);
  2434. reset_create_dialog = true;
  2435. }
  2436. void SceneTreeDock::open_instance_child_dialog() {
  2437. _tool_selected(TOOL_INSTANCE, true);
  2438. }
  2439. void SceneTreeDock::add_remote_tree_editor(Control *p_remote) {
  2440. ERR_FAIL_COND(remote_tree != nullptr);
  2441. add_child(p_remote);
  2442. remote_tree = p_remote;
  2443. remote_tree->hide();
  2444. }
  2445. void SceneTreeDock::show_remote_tree() {
  2446. _remote_tree_selected();
  2447. }
  2448. void SceneTreeDock::hide_remote_tree() {
  2449. _local_tree_selected();
  2450. }
  2451. void SceneTreeDock::show_tab_buttons() {
  2452. button_hb->show();
  2453. }
  2454. void SceneTreeDock::hide_tab_buttons() {
  2455. button_hb->hide();
  2456. }
  2457. void SceneTreeDock::_remote_tree_selected() {
  2458. scene_tree->hide();
  2459. create_root_dialog->hide();
  2460. if (remote_tree) {
  2461. remote_tree->show();
  2462. }
  2463. edit_remote->set_pressed(true);
  2464. edit_local->set_pressed(false);
  2465. emit_signal("remote_tree_selected");
  2466. }
  2467. void SceneTreeDock::_local_tree_selected() {
  2468. if (!bool(EDITOR_GET("interface/editors/show_scene_tree_root_selection")) || get_tree()->get_edited_scene_root() != nullptr) {
  2469. scene_tree->show();
  2470. }
  2471. if (remote_tree) {
  2472. remote_tree->hide();
  2473. }
  2474. edit_remote->set_pressed(false);
  2475. edit_local->set_pressed(true);
  2476. }
  2477. void SceneTreeDock::_update_create_root_dialog() {
  2478. BaseButton *toggle = Object::cast_to<BaseButton>(create_root_dialog->get_node(String("NodeShortcutsTopRow/NodeShortcutsToggle")));
  2479. Node *node_shortcuts = create_root_dialog->get_node(String("NodeShortcuts"));
  2480. if (!toggle || !node_shortcuts) {
  2481. return;
  2482. }
  2483. Control *beginner_nodes = Object::cast_to<Control>(node_shortcuts->get_node(String("BeginnerNodeShortcuts")));
  2484. Control *favorite_nodes = Object::cast_to<Control>(node_shortcuts->get_node(String("FavoriteNodeShortcuts")));
  2485. if (!beginner_nodes || !favorite_nodes) {
  2486. return;
  2487. }
  2488. EditorSettings::get_singleton()->set_setting("_use_favorites_root_selection", toggle->is_pressed());
  2489. EditorSettings::get_singleton()->save();
  2490. if (toggle->is_pressed()) {
  2491. for (int i = 0; i < favorite_nodes->get_child_count(); i++) {
  2492. favorite_nodes->get_child(i)->queue_delete();
  2493. }
  2494. FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("favorites.Node"), FileAccess::READ);
  2495. if (f) {
  2496. while (!f->eof_reached()) {
  2497. String l = f->get_line().strip_edges();
  2498. if (l != String()) {
  2499. Button *button = memnew(Button);
  2500. favorite_nodes->add_child(button);
  2501. button->set_text(TTR(l));
  2502. String name = l.get_slicec(' ', 0);
  2503. if (ScriptServer::is_global_class(name)) {
  2504. name = ScriptServer::get_global_class_native_base(name);
  2505. }
  2506. button->set_icon(EditorNode::get_singleton()->get_class_icon(name));
  2507. button->connect("pressed", this, "_favorite_root_selected", make_binds(l));
  2508. }
  2509. }
  2510. memdelete(f);
  2511. }
  2512. if (!favorite_nodes->is_visible_in_tree()) {
  2513. favorite_nodes->show();
  2514. beginner_nodes->hide();
  2515. }
  2516. } else {
  2517. if (!beginner_nodes->is_visible_in_tree()) {
  2518. beginner_nodes->show();
  2519. favorite_nodes->hide();
  2520. }
  2521. }
  2522. }
  2523. void SceneTreeDock::_favorite_root_selected(const String &p_class) {
  2524. selected_favorite_root = p_class;
  2525. _tool_selected(TOOL_CREATE_FAVORITE, false);
  2526. }
  2527. void SceneTreeDock::_feature_profile_changed() {
  2528. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  2529. if (profile.is_valid()) {
  2530. profile_allow_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCENE_TREE);
  2531. profile_allow_script_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCRIPT);
  2532. bool profile_allow_3d = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D);
  2533. button_3d->set_visible(profile_allow_3d);
  2534. button_add->set_visible(profile_allow_editing);
  2535. button_instance->set_visible(profile_allow_editing);
  2536. scene_tree->set_can_rename(profile_allow_editing);
  2537. } else {
  2538. button_3d->set_visible(true);
  2539. button_add->set_visible(true);
  2540. button_instance->set_visible(true);
  2541. scene_tree->set_can_rename(true);
  2542. profile_allow_editing = true;
  2543. profile_allow_script_editing = true;
  2544. }
  2545. _update_script_button();
  2546. }
  2547. void SceneTreeDock::_clear_clipboard() {
  2548. for (List<Node *>::Element *E = node_clipboard.front(); E; E = E->next()) {
  2549. memdelete(E->get());
  2550. }
  2551. node_clipboard.clear();
  2552. clipboard_resource_remap.clear();
  2553. }
  2554. void SceneTreeDock::_create_remap_for_node(Node *p_node, Map<RES, RES> &r_remap) {
  2555. List<PropertyInfo> props;
  2556. p_node->get_property_list(&props);
  2557. bool is_instanced = EditorPropertyRevert::may_node_be_in_instance(p_node);
  2558. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2559. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  2560. continue;
  2561. }
  2562. Variant v = p_node->get(E->get().name);
  2563. if (v.is_ref()) {
  2564. RES res = v;
  2565. if (res.is_valid()) {
  2566. if (is_instanced) {
  2567. Variant orig;
  2568. if (EditorPropertyRevert::get_instanced_node_original_property(p_node, E->get().name, orig)) {
  2569. if (!EditorPropertyRevert::is_node_property_different(p_node, v, orig)) {
  2570. continue;
  2571. }
  2572. }
  2573. }
  2574. if ((res->get_path() == "" || res->get_path().find("::") > -1) && !r_remap.has(res)) {
  2575. _create_remap_for_resource(res, r_remap);
  2576. }
  2577. }
  2578. }
  2579. }
  2580. for (int i = 0; i < p_node->get_child_count(); i++) {
  2581. _create_remap_for_node(p_node->get_child(i), r_remap);
  2582. }
  2583. }
  2584. void SceneTreeDock::_create_remap_for_resource(RES p_resource, Map<RES, RES> &r_remap) {
  2585. r_remap[p_resource] = p_resource->duplicate();
  2586. List<PropertyInfo> props;
  2587. p_resource->get_property_list(&props);
  2588. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2589. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  2590. continue;
  2591. }
  2592. Variant v = p_resource->get(E->get().name);
  2593. if (v.is_ref()) {
  2594. RES res = v;
  2595. if (res.is_valid()) {
  2596. if ((res->get_path() == "" || res->get_path().find("::") > -1) && !r_remap.has(res)) {
  2597. _create_remap_for_resource(res, r_remap);
  2598. }
  2599. }
  2600. }
  2601. }
  2602. }
  2603. void SceneTreeDock::_bind_methods() {
  2604. ClassDB::bind_method(D_METHOD("_tool_selected"), &SceneTreeDock::_tool_selected, DEFVAL(false));
  2605. ClassDB::bind_method(D_METHOD("_create"), &SceneTreeDock::_create);
  2606. ClassDB::bind_method(D_METHOD("_node_reparent"), &SceneTreeDock::_node_reparent);
  2607. ClassDB::bind_method(D_METHOD("_set_owners"), &SceneTreeDock::_set_owners);
  2608. ClassDB::bind_method(D_METHOD("_node_selected"), &SceneTreeDock::_node_selected);
  2609. ClassDB::bind_method(D_METHOD("_node_renamed"), &SceneTreeDock::_node_renamed);
  2610. ClassDB::bind_method(D_METHOD("_script_created"), &SceneTreeDock::_script_created);
  2611. ClassDB::bind_method(D_METHOD("_script_creation_closed"), &SceneTreeDock::_script_creation_closed);
  2612. ClassDB::bind_method(D_METHOD("_load_request"), &SceneTreeDock::_load_request);
  2613. ClassDB::bind_method(D_METHOD("_script_open_request"), &SceneTreeDock::_script_open_request);
  2614. ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &SceneTreeDock::_unhandled_key_input);
  2615. ClassDB::bind_method(D_METHOD("_input"), &SceneTreeDock::_input);
  2616. ClassDB::bind_method(D_METHOD("_nodes_drag_begin"), &SceneTreeDock::_nodes_drag_begin);
  2617. ClassDB::bind_method(D_METHOD("_delete_confirm"), &SceneTreeDock::_delete_confirm);
  2618. ClassDB::bind_method(D_METHOD("_toggle_editable_children_from_selection"), &SceneTreeDock::_toggle_editable_children_from_selection);
  2619. ClassDB::bind_method(D_METHOD("_toggle_placeholder_from_selection"), &SceneTreeDock::_toggle_placeholder_from_selection);
  2620. ClassDB::bind_method(D_METHOD("_node_prerenamed"), &SceneTreeDock::_node_prerenamed);
  2621. ClassDB::bind_method(D_METHOD("_import_subscene"), &SceneTreeDock::_import_subscene);
  2622. ClassDB::bind_method(D_METHOD("_selection_changed"), &SceneTreeDock::_selection_changed);
  2623. ClassDB::bind_method(D_METHOD("_node_collapsed"), &SceneTreeDock::_node_collapsed);
  2624. ClassDB::bind_method(D_METHOD("_new_scene_from"), &SceneTreeDock::_new_scene_from);
  2625. ClassDB::bind_method(D_METHOD("_nodes_dragged"), &SceneTreeDock::_nodes_dragged);
  2626. ClassDB::bind_method(D_METHOD("_files_dropped"), &SceneTreeDock::_files_dropped);
  2627. ClassDB::bind_method(D_METHOD("_property_selected"), &SceneTreeDock::_property_selected);
  2628. ClassDB::bind_method(D_METHOD("_quick_open"), &SceneTreeDock::_quick_open);
  2629. ClassDB::bind_method(D_METHOD("_script_dropped"), &SceneTreeDock::_script_dropped);
  2630. ClassDB::bind_method(D_METHOD("_tree_rmb"), &SceneTreeDock::_tree_rmb);
  2631. ClassDB::bind_method(D_METHOD("_filter_changed"), &SceneTreeDock::_filter_changed);
  2632. ClassDB::bind_method(D_METHOD("_focus_node"), &SceneTreeDock::_focus_node);
  2633. ClassDB::bind_method(D_METHOD("_remote_tree_selected"), &SceneTreeDock::_remote_tree_selected);
  2634. ClassDB::bind_method(D_METHOD("_local_tree_selected"), &SceneTreeDock::_local_tree_selected);
  2635. ClassDB::bind_method(D_METHOD("_update_script_button"), &SceneTreeDock::_update_script_button);
  2636. ClassDB::bind_method(D_METHOD("_favorite_root_selected"), &SceneTreeDock::_favorite_root_selected);
  2637. ClassDB::bind_method(D_METHOD("_update_create_root_dialog"), &SceneTreeDock::_update_create_root_dialog);
  2638. ClassDB::bind_method(D_METHOD("_feature_profile_changed"), &SceneTreeDock::_feature_profile_changed);
  2639. ClassDB::bind_method(D_METHOD("instance"), &SceneTreeDock::instance);
  2640. ClassDB::bind_method(D_METHOD("get_tree_editor"), &SceneTreeDock::get_tree_editor);
  2641. ClassDB::bind_method(D_METHOD("replace_node"), &SceneTreeDock::replace_node);
  2642. ADD_SIGNAL(MethodInfo("remote_tree_selected"));
  2643. ADD_SIGNAL(MethodInfo("add_node_used"));
  2644. ADD_SIGNAL(MethodInfo("node_created", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2645. }
  2646. SceneTreeDock::SceneTreeDock(EditorNode *p_editor, Node *p_scene_root, EditorSelection *p_editor_selection, EditorData &p_editor_data) {
  2647. set_name("Scene");
  2648. editor = p_editor;
  2649. edited_scene = nullptr;
  2650. editor_data = &p_editor_data;
  2651. editor_selection = p_editor_selection;
  2652. scene_root = p_scene_root;
  2653. VBoxContainer *vbc = this;
  2654. HBoxContainer *filter_hbc = memnew(HBoxContainer);
  2655. filter_hbc->add_constant_override("separate", 0);
  2656. ED_SHORTCUT("scene_tree/rename", TTR("Rename"), KEY_F2);
  2657. ED_SHORTCUT("scene_tree/batch_rename", TTR("Batch Rename"), KEY_MASK_SHIFT | KEY_F2);
  2658. ED_SHORTCUT("scene_tree/add_child_node", TTR("Add Child Node"), KEY_MASK_CMD | KEY_A);
  2659. ED_SHORTCUT("scene_tree/instance_scene", TTR("Instance Child Scene"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_A);
  2660. ED_SHORTCUT("scene_tree/expand_collapse_all", TTR("Expand/Collapse All"));
  2661. ED_SHORTCUT("scene_tree/cut_node", TTR("Cut"), KEY_MASK_CMD | KEY_X);
  2662. ED_SHORTCUT("scene_tree/copy_node", TTR("Copy"), KEY_MASK_CMD | KEY_C);
  2663. ED_SHORTCUT("scene_tree/paste_node", TTR("Paste"), KEY_MASK_CMD | KEY_V);
  2664. ED_SHORTCUT("scene_tree/change_node_type", TTR("Change Type"));
  2665. ED_SHORTCUT("scene_tree/attach_script", TTR("Attach Script"));
  2666. ED_SHORTCUT("scene_tree/extend_script", TTR("Extend Script"));
  2667. ED_SHORTCUT("scene_tree/detach_script", TTR("Detach Script"));
  2668. ED_SHORTCUT("scene_tree/move_up", TTR("Move Up"), KEY_MASK_CMD | KEY_UP);
  2669. ED_SHORTCUT("scene_tree/move_down", TTR("Move Down"), KEY_MASK_CMD | KEY_DOWN);
  2670. ED_SHORTCUT("scene_tree/duplicate", TTR("Duplicate"), KEY_MASK_CMD | KEY_D);
  2671. ED_SHORTCUT("scene_tree/reparent", TTR("Reparent"));
  2672. ED_SHORTCUT("scene_tree/reparent_to_new_node", TTR("Reparent to New Node"));
  2673. ED_SHORTCUT("scene_tree/make_root", TTR("Make Scene Root"));
  2674. ED_SHORTCUT("scene_tree/merge_from_scene", TTR("Merge From Scene"));
  2675. ED_SHORTCUT("scene_tree/save_branch_as_scene", TTR("Save Branch as Scene"));
  2676. ED_SHORTCUT("scene_tree/copy_node_path", TTR("Copy Node Path"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_C);
  2677. ED_SHORTCUT("scene_tree/delete_no_confirm", TTR("Delete (No Confirm)"), KEY_MASK_SHIFT | KEY_DELETE);
  2678. ED_SHORTCUT("scene_tree/delete", TTR("Delete"), KEY_DELETE);
  2679. button_add = memnew(ToolButton);
  2680. button_add->connect("pressed", this, "_tool_selected", make_binds(TOOL_NEW, false));
  2681. button_add->set_tooltip(TTR("Add/Create a New Node."));
  2682. button_add->set_shortcut(ED_GET_SHORTCUT("scene_tree/add_child_node"));
  2683. filter_hbc->add_child(button_add);
  2684. button_instance = memnew(ToolButton);
  2685. button_instance->connect("pressed", this, "_tool_selected", make_binds(TOOL_INSTANCE, false));
  2686. button_instance->set_tooltip(TTR("Instance a scene file as a Node. Creates an inherited scene if no root node exists."));
  2687. button_instance->set_shortcut(ED_GET_SHORTCUT("scene_tree/instance_scene"));
  2688. filter_hbc->add_child(button_instance);
  2689. vbc->add_child(filter_hbc);
  2690. filter = memnew(LineEdit);
  2691. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2692. filter->set_placeholder(TTR("Filter nodes"));
  2693. filter_hbc->add_child(filter);
  2694. filter->add_constant_override("minimum_spaces", 0);
  2695. filter->connect("text_changed", this, "_filter_changed");
  2696. button_create_script = memnew(ToolButton);
  2697. button_create_script->connect("pressed", this, "_tool_selected", make_binds(TOOL_ATTACH_SCRIPT, false));
  2698. button_create_script->set_tooltip(TTR("Attach a new or existing script to the selected node."));
  2699. button_create_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/attach_script"));
  2700. filter_hbc->add_child(button_create_script);
  2701. button_create_script->hide();
  2702. button_detach_script = memnew(ToolButton);
  2703. button_detach_script->connect("pressed", this, "_tool_selected", make_binds(TOOL_DETACH_SCRIPT, false));
  2704. button_detach_script->set_tooltip(TTR("Detach the script from the selected node."));
  2705. button_detach_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/detach_script"));
  2706. filter_hbc->add_child(button_detach_script);
  2707. button_detach_script->hide();
  2708. button_hb = memnew(HBoxContainer);
  2709. vbc->add_child(button_hb);
  2710. edit_remote = memnew(ToolButton);
  2711. button_hb->add_child(edit_remote);
  2712. edit_remote->set_h_size_flags(SIZE_EXPAND_FILL);
  2713. edit_remote->set_text(TTR("Remote"));
  2714. edit_remote->set_toggle_mode(true);
  2715. edit_remote->set_tooltip(TTR("If selected, the Remote scene tree dock will cause the project to stutter every time it updates.\nSwitch back to the Local scene tree dock to improve performance."));
  2716. edit_remote->connect("pressed", this, "_remote_tree_selected");
  2717. edit_local = memnew(ToolButton);
  2718. button_hb->add_child(edit_local);
  2719. edit_local->set_h_size_flags(SIZE_EXPAND_FILL);
  2720. edit_local->set_text(TTR("Local"));
  2721. edit_local->set_toggle_mode(true);
  2722. edit_local->connect("pressed", this, "_local_tree_selected");
  2723. remote_tree = nullptr;
  2724. button_hb->hide();
  2725. create_root_dialog = memnew(VBoxContainer);
  2726. vbc->add_child(create_root_dialog);
  2727. create_root_dialog->hide();
  2728. scene_tree = memnew(SceneTreeEditor(false, true, true));
  2729. vbc->add_child(scene_tree);
  2730. scene_tree->set_v_size_flags(SIZE_EXPAND | SIZE_FILL);
  2731. scene_tree->connect("rmb_pressed", this, "_tree_rmb");
  2732. scene_tree->connect("node_selected", this, "_node_selected", varray(), CONNECT_DEFERRED);
  2733. scene_tree->connect("node_renamed", this, "_node_renamed", varray(), CONNECT_DEFERRED);
  2734. scene_tree->connect("node_prerename", this, "_node_prerenamed");
  2735. scene_tree->connect("open", this, "_load_request");
  2736. scene_tree->connect("open_script", this, "_script_open_request");
  2737. scene_tree->connect("nodes_rearranged", this, "_nodes_dragged");
  2738. scene_tree->connect("files_dropped", this, "_files_dropped");
  2739. scene_tree->connect("script_dropped", this, "_script_dropped");
  2740. scene_tree->connect("nodes_dragged", this, "_nodes_drag_begin");
  2741. scene_tree->get_scene_tree()->connect("item_double_clicked", this, "_focus_node");
  2742. scene_tree->set_undo_redo(&editor_data->get_undo_redo());
  2743. scene_tree->set_editor_selection(editor_selection);
  2744. create_dialog = memnew(CreateDialog);
  2745. create_dialog->set_base_type("Node");
  2746. add_child(create_dialog);
  2747. create_dialog->connect("create", this, "_create");
  2748. create_dialog->connect("favorites_updated", this, "_update_create_root_dialog");
  2749. #ifdef MODULE_REGEX_ENABLED
  2750. rename_dialog = memnew(RenameDialog(scene_tree, &editor_data->get_undo_redo()));
  2751. add_child(rename_dialog);
  2752. #endif // MODULE_REGEX_ENABLED
  2753. script_create_dialog = memnew(ScriptCreateDialog);
  2754. script_create_dialog->set_inheritance_base_type("Node");
  2755. add_child(script_create_dialog);
  2756. reparent_dialog = memnew(ReparentDialog);
  2757. add_child(reparent_dialog);
  2758. reparent_dialog->connect("reparent", this, "_node_reparent");
  2759. accept = memnew(AcceptDialog);
  2760. add_child(accept);
  2761. quick_open = memnew(EditorQuickOpen);
  2762. add_child(quick_open);
  2763. quick_open->connect("quick_open", this, "_quick_open");
  2764. set_process_unhandled_key_input(true);
  2765. delete_dialog = memnew(ConfirmationDialog);
  2766. add_child(delete_dialog);
  2767. delete_dialog->connect("confirmed", this, "_delete_confirm", varray(false));
  2768. editable_instance_remove_dialog = memnew(ConfirmationDialog);
  2769. add_child(editable_instance_remove_dialog);
  2770. editable_instance_remove_dialog->connect("confirmed", this, "_toggle_editable_children_from_selection");
  2771. placeholder_editable_instance_remove_dialog = memnew(ConfirmationDialog);
  2772. add_child(placeholder_editable_instance_remove_dialog);
  2773. placeholder_editable_instance_remove_dialog->connect("confirmed", this, "_toggle_placeholder_from_selection");
  2774. import_subscene_dialog = memnew(EditorSubScene);
  2775. add_child(import_subscene_dialog);
  2776. import_subscene_dialog->connect("subscene_selected", this, "_import_subscene");
  2777. new_scene_from_dialog = memnew(EditorFileDialog);
  2778. new_scene_from_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  2779. add_child(new_scene_from_dialog);
  2780. new_scene_from_dialog->connect("file_selected", this, "_new_scene_from");
  2781. menu = memnew(PopupMenu);
  2782. add_child(menu);
  2783. menu->connect("id_pressed", this, "_tool_selected");
  2784. menu->set_hide_on_window_lose_focus(true);
  2785. menu_subresources = memnew(PopupMenu);
  2786. menu_subresources->set_name("Sub-Resources");
  2787. menu_subresources->connect("id_pressed", this, "_tool_selected");
  2788. menu->add_child(menu_subresources);
  2789. first_enter = true;
  2790. restore_script_editor_on_drag = false;
  2791. menu_properties = memnew(PopupMenu);
  2792. add_child(menu_properties);
  2793. menu_properties->connect("id_pressed", this, "_property_selected");
  2794. clear_inherit_confirm = memnew(ConfirmationDialog);
  2795. clear_inherit_confirm->set_text(TTR("Clear Inheritance? (No Undo!)"));
  2796. clear_inherit_confirm->get_ok()->set_text(TTR("Clear"));
  2797. add_child(clear_inherit_confirm);
  2798. set_process_input(true);
  2799. set_process(true);
  2800. profile_allow_editing = true;
  2801. profile_allow_script_editing = true;
  2802. EDITOR_DEF("interface/editors/show_scene_tree_root_selection", true);
  2803. EDITOR_DEF("interface/editors/derive_script_globals_by_name", true);
  2804. EDITOR_DEF("_use_favorites_root_selection", false);
  2805. }
  2806. SceneTreeDock::~SceneTreeDock() {
  2807. if (!node_clipboard.empty()) {
  2808. _clear_clipboard();
  2809. }
  2810. }