scene_tree_dock.cpp 123 KB

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