scene_tree_dock.cpp 125 KB

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