scene_tree_dock.cpp 119 KB

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