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_create_root = bool(EDITOR_GET("interface/editors/show_scene_tree_root_selection")) && get_tree()->get_edited_scene_root() == nullptr;
  1197. if (show_create_root != create_root_dialog->is_visible_in_tree() && !remote_tree->is_visible()) {
  1198. if (show_create_root) {
  1199. create_root_dialog->show();
  1200. scene_tree->hide();
  1201. } else {
  1202. create_root_dialog->hide();
  1203. scene_tree->show();
  1204. }
  1205. }
  1206. } break;
  1207. }
  1208. }
  1209. void SceneTreeDock::_node_replace_owner(Node *p_base, Node *p_node, Node *p_root, ReplaceOwnerMode p_mode) {
  1210. if (p_node->get_owner() == p_base && p_node != p_root) {
  1211. UndoRedo *undo_redo = &editor_data->get_undo_redo();
  1212. switch (p_mode) {
  1213. case MODE_BIDI: {
  1214. 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;
  1215. if (is_unique) {
  1216. // Will create a unique name conflict. Disable before setting owner.
  1217. undo_redo->add_do_method(p_node, "set_unique_name_in_owner", false);
  1218. }
  1219. undo_redo->add_do_method(p_node, "set_owner", p_root);
  1220. undo_redo->add_undo_method(p_node, "set_owner", p_base);
  1221. if (is_unique) {
  1222. // Will create a unique name conflict. Enable after setting owner.
  1223. undo_redo->add_undo_method(p_node, "set_unique_name_in_owner", true);
  1224. }
  1225. } break;
  1226. case MODE_DO: {
  1227. undo_redo->add_do_method(p_node, "set_owner", p_root);
  1228. } break;
  1229. case MODE_UNDO: {
  1230. undo_redo->add_undo_method(p_node, "set_owner", p_root);
  1231. } break;
  1232. }
  1233. }
  1234. for (int i = 0; i < p_node->get_child_count(); i++) {
  1235. _node_replace_owner(p_base, p_node->get_child(i), p_root, p_mode);
  1236. }
  1237. }
  1238. void SceneTreeDock::_load_request(const String &p_path) {
  1239. editor->open_request(p_path);
  1240. }
  1241. void SceneTreeDock::_script_open_request(const Ref<Script> &p_script) {
  1242. editor->edit_resource(p_script);
  1243. }
  1244. void SceneTreeDock::_push_item(Object *p_object) {
  1245. editor->push_item(p_object);
  1246. }
  1247. void SceneTreeDock::_handle_select(Node *p_node) {
  1248. if (tree_clicked) {
  1249. pending_click_select = p_node;
  1250. } else {
  1251. _push_item(p_node);
  1252. }
  1253. }
  1254. void SceneTreeDock::_node_selected() {
  1255. Node *node = scene_tree->get_selected();
  1256. if (!node) {
  1257. return;
  1258. }
  1259. _handle_select(node);
  1260. }
  1261. void SceneTreeDock::_node_renamed() {
  1262. _node_selected();
  1263. }
  1264. void SceneTreeDock::_set_owners(Node *p_owner, const Array &p_nodes) {
  1265. for (int i = 0; i < p_nodes.size(); i++) {
  1266. Node *n = Object::cast_to<Node>(p_nodes[i]);
  1267. if (!n) {
  1268. continue;
  1269. }
  1270. n->set_owner(p_owner);
  1271. }
  1272. }
  1273. void SceneTreeDock::_fill_path_renames(Vector<StringName> base_path, Vector<StringName> new_base_path, Node *p_node, Map<Node *, NodePath> *p_renames) {
  1274. base_path.push_back(p_node->get_name());
  1275. if (new_base_path.size()) {
  1276. new_base_path.push_back(p_node->get_name());
  1277. }
  1278. NodePath new_path;
  1279. if (new_base_path.size()) {
  1280. new_path = NodePath(new_base_path, true);
  1281. }
  1282. p_renames->insert(p_node, new_path);
  1283. for (int i = 0; i < p_node->get_child_count(); i++) {
  1284. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), p_renames);
  1285. }
  1286. }
  1287. void SceneTreeDock::fill_path_renames(Node *p_node, Node *p_new_parent, Map<Node *, NodePath> *p_renames) {
  1288. Vector<StringName> base_path;
  1289. Node *n = p_node->get_parent();
  1290. while (n) {
  1291. base_path.push_back(n->get_name());
  1292. n = n->get_parent();
  1293. }
  1294. base_path.invert();
  1295. Vector<StringName> new_base_path;
  1296. if (p_new_parent) {
  1297. n = p_new_parent;
  1298. while (n) {
  1299. new_base_path.push_back(n->get_name());
  1300. n = n->get_parent();
  1301. }
  1302. new_base_path.invert();
  1303. }
  1304. _fill_path_renames(base_path, new_base_path, p_node, p_renames);
  1305. }
  1306. bool SceneTreeDock::_update_node_path(Node *p_root_node, NodePath &r_node_path, Map<Node *, NodePath> *p_renames) const {
  1307. Node *target_node = p_root_node->get_node_or_null(r_node_path);
  1308. 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)) + "'");
  1309. // Try to find the target node in modified node paths.
  1310. Map<Node *, NodePath>::Element *found_node_path = p_renames->find(target_node);
  1311. if (found_node_path) {
  1312. Map<Node *, NodePath>::Element *found_root_path = p_renames->find(p_root_node);
  1313. NodePath root_path_new = found_root_path ? found_root_path->get() : p_root_node->get_path();
  1314. r_node_path = root_path_new.rel_path_to(found_node_path->get());
  1315. return true;
  1316. }
  1317. // Update the path if the base node has changed and has not been deleted.
  1318. Map<Node *, NodePath>::Element *found_root_path = p_renames->find(p_root_node);
  1319. if (found_root_path) {
  1320. NodePath root_path_new = found_root_path->get();
  1321. if (!root_path_new.is_empty()) {
  1322. NodePath old_abs_path = NodePath(String(p_root_node->get_path()).plus_file(r_node_path));
  1323. old_abs_path.simplify();
  1324. r_node_path = root_path_new.rel_path_to(old_abs_path);
  1325. }
  1326. return true;
  1327. }
  1328. return false;
  1329. }
  1330. bool SceneTreeDock::_check_node_path_recursive(Node *p_root_node, Variant &r_variant, Map<Node *, NodePath> *p_renames) const {
  1331. switch (r_variant.get_type()) {
  1332. case Variant::NODE_PATH: {
  1333. NodePath node_path = r_variant;
  1334. if (!node_path.is_empty() && _update_node_path(p_root_node, node_path, p_renames)) {
  1335. r_variant = node_path;
  1336. return true;
  1337. }
  1338. } break;
  1339. case Variant::ARRAY: {
  1340. Array a = r_variant;
  1341. bool updated = false;
  1342. for (int i = 0; i < a.size(); i++) {
  1343. Variant value = a[i];
  1344. if (_check_node_path_recursive(p_root_node, value, p_renames)) {
  1345. if (!updated) {
  1346. a = a.duplicate(); // Need to duplicate for undo-redo to work.
  1347. updated = true;
  1348. }
  1349. a[i] = value;
  1350. }
  1351. }
  1352. if (updated) {
  1353. r_variant = a;
  1354. return true;
  1355. }
  1356. } break;
  1357. case Variant::DICTIONARY: {
  1358. Dictionary d = r_variant;
  1359. bool updated = false;
  1360. for (int i = 0; i < d.size(); i++) {
  1361. Variant value = d.get_value_at_index(i);
  1362. if (_check_node_path_recursive(p_root_node, value, p_renames)) {
  1363. if (!updated) {
  1364. d = d.duplicate(); // Need to duplicate for undo-redo to work.
  1365. updated = true;
  1366. }
  1367. d[d.get_key_at_index(i)] = value;
  1368. }
  1369. }
  1370. if (updated) {
  1371. r_variant = d;
  1372. return true;
  1373. }
  1374. } break;
  1375. default: {
  1376. }
  1377. }
  1378. return false;
  1379. }
  1380. void SceneTreeDock::perform_node_renames(Node *p_base, Map<Node *, NodePath> *p_renames, Map<Ref<Animation>, Set<int>> *r_rem_anims) {
  1381. Map<Ref<Animation>, Set<int>> rem_anims;
  1382. if (!r_rem_anims) {
  1383. r_rem_anims = &rem_anims;
  1384. }
  1385. if (!p_base) {
  1386. p_base = edited_scene;
  1387. }
  1388. if (!p_base) {
  1389. return;
  1390. }
  1391. // No renaming if base node is deleted.
  1392. Map<Node *, NodePath>::Element *found_base_path = p_renames->find(p_base);
  1393. if (found_base_path && found_base_path->get().is_empty()) {
  1394. return;
  1395. }
  1396. // Renaming node paths used in node properties.
  1397. List<PropertyInfo> properties;
  1398. p_base->get_property_list(&properties);
  1399. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  1400. if (!(E->get().usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR))) {
  1401. continue;
  1402. }
  1403. String propertyname = E->get().name;
  1404. Variant old_variant = p_base->get(propertyname);
  1405. Variant updated_variant = old_variant;
  1406. if (_check_node_path_recursive(p_base, updated_variant, p_renames)) {
  1407. editor_data->get_undo_redo().add_do_property(p_base, propertyname, updated_variant);
  1408. editor_data->get_undo_redo().add_undo_property(p_base, propertyname, old_variant);
  1409. p_base->set(propertyname, updated_variant);
  1410. }
  1411. }
  1412. bool autorename_animation_tracks = bool(EDITOR_DEF("editors/animation/autorename_animation_tracks", true));
  1413. if (autorename_animation_tracks && Object::cast_to<AnimationPlayer>(p_base)) {
  1414. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_base);
  1415. List<StringName> anims;
  1416. ap->get_animation_list(&anims);
  1417. Node *root = ap->get_node(ap->get_root());
  1418. if (root) {
  1419. Map<Node *, NodePath>::Element *found_root_path = p_renames->find(root);
  1420. NodePath new_root_path = found_root_path ? found_root_path->get() : root->get_path();
  1421. if (!new_root_path.is_empty()) { // No renaming if root node is deleted.
  1422. for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
  1423. Ref<Animation> anim = ap->get_animation(E->get());
  1424. if (!r_rem_anims->has(anim)) {
  1425. r_rem_anims->insert(anim, Set<int>());
  1426. Set<int> &ran = r_rem_anims->find(anim)->get();
  1427. for (int i = 0; i < anim->get_track_count(); i++) {
  1428. ran.insert(i);
  1429. }
  1430. }
  1431. Set<int> &ran = r_rem_anims->find(anim)->get();
  1432. if (anim.is_null()) {
  1433. continue;
  1434. }
  1435. for (int i = 0; i < anim->get_track_count(); i++) {
  1436. NodePath track_np = anim->track_get_path(i);
  1437. Node *n = root->get_node(track_np);
  1438. if (!n) {
  1439. continue;
  1440. }
  1441. if (!ran.has(i)) {
  1442. continue; //channel was removed
  1443. }
  1444. Map<Node *, NodePath>::Element *found_path = p_renames->find(n);
  1445. if (found_path) {
  1446. if (found_path->get() == NodePath()) {
  1447. //will be erased
  1448. int idx = 0;
  1449. Set<int>::Element *EI = ran.front();
  1450. ERR_FAIL_COND(!EI); //bug
  1451. while (EI->get() != i) {
  1452. idx++;
  1453. EI = EI->next();
  1454. ERR_FAIL_COND(!EI); //another bug
  1455. }
  1456. editor_data->get_undo_redo().add_do_method(anim.ptr(), "remove_track", idx);
  1457. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "add_track", anim->track_get_type(i), idx);
  1458. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", idx, track_np);
  1459. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_interpolation_type", idx, anim->track_get_interpolation_type(i));
  1460. for (int j = 0; j < anim->track_get_key_count(i); j++) {
  1461. 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));
  1462. }
  1463. ran.erase(i); //byebye channel
  1464. } else {
  1465. //will be renamed
  1466. NodePath rel_path = new_root_path.rel_path_to(found_path->get());
  1467. NodePath new_path = NodePath(rel_path.get_names(), track_np.get_subnames(), false);
  1468. if (new_path == track_np) {
  1469. continue; //bleh
  1470. }
  1471. editor_data->get_undo_redo().add_do_method(anim.ptr(), "track_set_path", i, new_path);
  1472. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", i, track_np);
  1473. }
  1474. }
  1475. }
  1476. }
  1477. }
  1478. }
  1479. }
  1480. for (int i = 0; i < p_base->get_child_count(); i++) {
  1481. perform_node_renames(p_base->get_child(i), p_renames, r_rem_anims);
  1482. }
  1483. }
  1484. void SceneTreeDock::_node_prerenamed(Node *p_node, const String &p_new_name) {
  1485. Map<Node *, NodePath> path_renames;
  1486. Vector<StringName> base_path;
  1487. Node *n = p_node->get_parent();
  1488. while (n) {
  1489. base_path.push_back(n->get_name());
  1490. n = n->get_parent();
  1491. }
  1492. base_path.invert();
  1493. Vector<StringName> new_base_path = base_path;
  1494. base_path.push_back(p_node->get_name());
  1495. new_base_path.push_back(p_new_name);
  1496. NodePath new_path(new_base_path, true);
  1497. path_renames[p_node] = new_path;
  1498. for (int i = 0; i < p_node->get_child_count(); i++) {
  1499. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), &path_renames);
  1500. }
  1501. perform_node_renames(nullptr, &path_renames);
  1502. }
  1503. bool SceneTreeDock::_validate_no_foreign() {
  1504. List<Node *> selection = editor_selection->get_selected_node_list();
  1505. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1506. if (E->get() != edited_scene && E->get()->get_owner() != edited_scene) {
  1507. accept->set_text(TTR("Can't operate on nodes from a foreign scene!"));
  1508. accept->popup_centered_minsize();
  1509. return false;
  1510. }
  1511. // When edited_scene inherits from another one the root Node will be the parent Scene,
  1512. // we don't want to consider that Node a foreign one otherwise we would not be able to
  1513. // delete it.
  1514. if (edited_scene->get_scene_inherited_state().is_valid() && edited_scene == E->get()) {
  1515. continue;
  1516. }
  1517. 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) {
  1518. accept->set_text(TTR("Can't operate on nodes the current scene inherits from!"));
  1519. accept->popup_centered_minsize();
  1520. return false;
  1521. }
  1522. }
  1523. return true;
  1524. }
  1525. bool SceneTreeDock::_validate_no_instance() {
  1526. List<Node *> selection = editor_selection->get_selected_node_list();
  1527. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1528. if (E->get() != edited_scene && E->get()->get_filename() != "") {
  1529. accept->set_text(TTR("This operation can't be done on instanced scenes."));
  1530. accept->popup_centered();
  1531. return false;
  1532. }
  1533. }
  1534. return true;
  1535. }
  1536. void SceneTreeDock::_node_reparent(NodePath p_path, bool p_keep_global_xform) {
  1537. Node *new_parent = scene_root->get_node(p_path);
  1538. ERR_FAIL_COND(!new_parent);
  1539. List<Node *> selection = editor_selection->get_selected_node_list();
  1540. if (selection.empty()) {
  1541. return; // Nothing to reparent.
  1542. }
  1543. Vector<Node *> nodes;
  1544. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1545. nodes.push_back(E->get());
  1546. }
  1547. _do_reparent(new_parent, -1, nodes, p_keep_global_xform);
  1548. }
  1549. void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, Vector<Node *> p_nodes, bool p_keep_global_xform) {
  1550. Node *new_parent = p_new_parent;
  1551. ERR_FAIL_COND(!new_parent);
  1552. if (p_nodes.size() == 0) {
  1553. return; // Nothing to reparent.
  1554. }
  1555. p_nodes.sort_custom<Node::Comparator>(); //Makes result reliable.
  1556. bool no_change = true;
  1557. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1558. if (p_nodes[ni] == p_new_parent) {
  1559. return; // Attempt to reparent to itself.
  1560. }
  1561. if (p_nodes[ni]->get_parent() != p_new_parent || p_position_in_parent + ni != p_nodes[ni]->get_position_in_parent()) {
  1562. no_change = false;
  1563. }
  1564. }
  1565. if (no_change) {
  1566. return; // Position and parent didn't change.
  1567. }
  1568. Node *validate = new_parent;
  1569. while (validate) {
  1570. ERR_FAIL_COND_MSG(p_nodes.find(validate) != -1, "Selection changed at some point. Can't reparent.");
  1571. validate = validate->get_parent();
  1572. }
  1573. // Sort by tree order, so re-adding is easy.
  1574. p_nodes.sort_custom<Node::Comparator>();
  1575. editor_data->get_undo_redo().create_action(TTR("Reparent Node"));
  1576. Map<Node *, NodePath> path_renames;
  1577. Vector<StringName> former_names;
  1578. int inc = 0;
  1579. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1580. // No undo implemented for this yet.
  1581. Node *node = p_nodes[ni];
  1582. fill_path_renames(node, new_parent, &path_renames);
  1583. former_names.push_back(node->get_name());
  1584. List<Node *> owned;
  1585. node->get_owned_by(node->get_owner(), &owned);
  1586. Array owners;
  1587. for (List<Node *>::Element *E = owned.front(); E; E = E->next()) {
  1588. owners.push_back(E->get());
  1589. }
  1590. if (new_parent == node->get_parent() && node->get_index() < p_position_in_parent + ni) {
  1591. inc--; // If the child will generate a gap when moved, adjust.
  1592. }
  1593. editor_data->get_undo_redo().add_do_method(node->get_parent(), "remove_child", node);
  1594. editor_data->get_undo_redo().add_do_method(new_parent, "add_child", node);
  1595. if (p_position_in_parent >= 0) {
  1596. editor_data->get_undo_redo().add_do_method(new_parent, "move_child", node, p_position_in_parent + inc);
  1597. }
  1598. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  1599. String old_name = former_names[ni];
  1600. String new_name = new_parent->validate_child_name(node);
  1601. // Name was modified, fix the path renames.
  1602. if (old_name.casecmp_to(new_name) != 0) {
  1603. // Fix the to name to have the new name.
  1604. Map<Node *, NodePath>::Element *found_path = path_renames.find(node);
  1605. if (found_path) {
  1606. NodePath old_new_name = found_path->get();
  1607. Vector<StringName> unfixed_new_names = old_new_name.get_names();
  1608. Vector<StringName> fixed_new_names;
  1609. // Get last name and replace with fixed new name.
  1610. for (int a = 0; a < (unfixed_new_names.size() - 1); a++) {
  1611. fixed_new_names.push_back(unfixed_new_names[a]);
  1612. }
  1613. fixed_new_names.push_back(new_name);
  1614. NodePath fixed_node_path = NodePath(fixed_new_names, true);
  1615. path_renames[node] = fixed_node_path;
  1616. } else {
  1617. ERR_PRINT("Internal error. Can't find renamed path for node '" + node->get_path() + "'");
  1618. }
  1619. }
  1620. 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);
  1621. 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());
  1622. if (p_keep_global_xform) {
  1623. if (Object::cast_to<Node2D>(node)) {
  1624. editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Node2D>(node)->get_global_transform());
  1625. }
  1626. if (Object::cast_to<Spatial>(node)) {
  1627. editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Spatial>(node)->get_global_transform());
  1628. }
  1629. if (Object::cast_to<Control>(node)) {
  1630. editor_data->get_undo_redo().add_do_method(node, "set_global_position", Object::cast_to<Control>(node)->get_global_position());
  1631. }
  1632. }
  1633. editor_data->get_undo_redo().add_do_method(this, "_set_owners", edited_scene, owners);
  1634. if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == node) {
  1635. editor_data->get_undo_redo().add_do_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", node);
  1636. }
  1637. editor_data->get_undo_redo().add_undo_method(new_parent, "remove_child", node);
  1638. editor_data->get_undo_redo().add_undo_method(node, "set_name", former_names[ni]);
  1639. inc++;
  1640. }
  1641. // Add and move in a second step (so old order is preserved).
  1642. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1643. Node *node = p_nodes[ni];
  1644. List<Node *> owned;
  1645. node->get_owned_by(node->get_owner(), &owned);
  1646. Array owners;
  1647. for (List<Node *>::Element *E = owned.front(); E; E = E->next()) {
  1648. owners.push_back(E->get());
  1649. }
  1650. int child_pos = node->get_position_in_parent();
  1651. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "add_child", node);
  1652. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "move_child", node, child_pos);
  1653. editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
  1654. if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == node) {
  1655. editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", node);
  1656. }
  1657. if (p_keep_global_xform) {
  1658. if (Object::cast_to<Node2D>(node)) {
  1659. editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Node2D>(node)->get_transform());
  1660. }
  1661. if (Object::cast_to<Spatial>(node)) {
  1662. editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Spatial>(node)->get_transform());
  1663. }
  1664. if (Object::cast_to<Control>(node)) {
  1665. editor_data->get_undo_redo().add_undo_method(node, "set_position", Object::cast_to<Control>(node)->get_position());
  1666. }
  1667. }
  1668. }
  1669. perform_node_renames(nullptr, &path_renames);
  1670. editor_data->get_undo_redo().commit_action();
  1671. }
  1672. bool SceneTreeDock::_is_collapsed_recursive(TreeItem *p_item) const {
  1673. bool is_branch_collapsed = false;
  1674. List<TreeItem *> needs_check;
  1675. needs_check.push_back(p_item);
  1676. while (!needs_check.empty()) {
  1677. TreeItem *item = needs_check.back()->get();
  1678. needs_check.pop_back();
  1679. TreeItem *child = item->get_children();
  1680. is_branch_collapsed = item->is_collapsed() && child;
  1681. if (is_branch_collapsed) {
  1682. break;
  1683. }
  1684. while (child) {
  1685. needs_check.push_back(child);
  1686. child = child->get_next();
  1687. }
  1688. }
  1689. return is_branch_collapsed;
  1690. }
  1691. void SceneTreeDock::_set_collapsed_recursive(TreeItem *p_item, bool p_collapsed) {
  1692. List<TreeItem *> to_collapse;
  1693. to_collapse.push_back(p_item);
  1694. while (!to_collapse.empty()) {
  1695. TreeItem *item = to_collapse.back()->get();
  1696. to_collapse.pop_back();
  1697. item->set_collapsed(p_collapsed);
  1698. TreeItem *child = item->get_children();
  1699. while (child) {
  1700. to_collapse.push_back(child);
  1701. child = child->get_next();
  1702. }
  1703. }
  1704. }
  1705. void SceneTreeDock::_script_created(Ref<Script> p_script) {
  1706. List<Node *> selected = editor_selection->get_selected_node_list();
  1707. if (selected.empty()) {
  1708. return;
  1709. }
  1710. InspectorDock *inspector_dock = EditorNode::get_singleton()->get_inspector_dock();
  1711. UndoRedo &undo_redo = editor_data->get_undo_redo();
  1712. undo_redo.create_action(TTR("Attach Script"));
  1713. for (List<Node *>::Element *E = selected.front(); E; E = E->next()) {
  1714. Node *node = E->get();
  1715. Ref<Script> existing = node->get_script();
  1716. undo_redo.add_do_method(inspector_dock, "store_script_properties", node);
  1717. undo_redo.add_undo_method(inspector_dock, "store_script_properties", node);
  1718. undo_redo.add_do_method(node, "set_script", p_script.get_ref_ptr());
  1719. undo_redo.add_undo_method(node, "set_script", existing);
  1720. undo_redo.add_do_method(inspector_dock, "apply_script_properties", node);
  1721. undo_redo.add_undo_method(inspector_dock, "apply_script_properties", node);
  1722. undo_redo.add_do_method(this, "_update_script_button");
  1723. undo_redo.add_undo_method(this, "_update_script_button");
  1724. }
  1725. undo_redo.commit_action();
  1726. _push_item(p_script.operator->());
  1727. _update_script_button();
  1728. }
  1729. void SceneTreeDock::_script_creation_closed() {
  1730. script_create_dialog->disconnect("script_created", this, "_script_created");
  1731. }
  1732. void SceneTreeDock::_toggle_editable_children_from_selection() {
  1733. List<Node *> selection = editor_selection->get_selected_node_list();
  1734. List<Node *>::Element *e = selection.front();
  1735. if (e) {
  1736. _toggle_editable_children(e->get());
  1737. }
  1738. }
  1739. void SceneTreeDock::_toggle_placeholder_from_selection() {
  1740. List<Node *> selection = editor_selection->get_selected_node_list();
  1741. List<Node *>::Element *e = selection.front();
  1742. if (e) {
  1743. Node *node = e->get();
  1744. if (node) {
  1745. _toggle_editable_children(node);
  1746. bool placeholder = node->get_scene_instance_load_placeholder();
  1747. placeholder = !placeholder;
  1748. node->set_scene_instance_load_placeholder(placeholder);
  1749. scene_tree->update_tree();
  1750. }
  1751. }
  1752. }
  1753. void SceneTreeDock::_toggle_editable_children(Node *p_node) {
  1754. if (p_node) {
  1755. bool editable = !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node);
  1756. EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(p_node, editable);
  1757. if (editable) {
  1758. p_node->set_scene_instance_load_placeholder(false);
  1759. }
  1760. SpatialEditor::get_singleton()->update_all_gizmos(p_node);
  1761. scene_tree->update_tree();
  1762. }
  1763. }
  1764. void SceneTreeDock::_delete_confirm(bool p_cut) {
  1765. List<Node *> remove_list = editor_selection->get_selected_node_list();
  1766. if (remove_list.empty()) {
  1767. return;
  1768. }
  1769. editor->get_editor_plugins_over()->make_visible(false);
  1770. if (p_cut) {
  1771. editor_data->get_undo_redo().create_action(TTR("Cut Node(s)"));
  1772. } else {
  1773. editor_data->get_undo_redo().create_action(TTR("Remove Node(s)"));
  1774. }
  1775. bool entire_scene = false;
  1776. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  1777. if (E->get() == edited_scene) {
  1778. entire_scene = true;
  1779. }
  1780. }
  1781. if (entire_scene) {
  1782. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", (Object *)nullptr);
  1783. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", edited_scene);
  1784. editor_data->get_undo_redo().add_undo_method(edited_scene, "set_owner", edited_scene->get_owner());
  1785. editor_data->get_undo_redo().add_undo_method(scene_tree, "update_tree");
  1786. editor_data->get_undo_redo().add_undo_reference(edited_scene);
  1787. } else {
  1788. remove_list.sort_custom<Node::Comparator>(); //sort nodes to keep positions
  1789. Map<Node *, NodePath> path_renames;
  1790. //delete from animation
  1791. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  1792. Node *n = E->get();
  1793. if (!n->is_inside_tree() || !n->get_parent()) {
  1794. continue;
  1795. }
  1796. fill_path_renames(n, nullptr, &path_renames);
  1797. }
  1798. perform_node_renames(nullptr, &path_renames);
  1799. //delete for read
  1800. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  1801. Node *n = E->get();
  1802. if (!n->is_inside_tree() || !n->get_parent()) {
  1803. continue;
  1804. }
  1805. List<Node *> owned;
  1806. n->get_owned_by(n->get_owner(), &owned);
  1807. Array owners;
  1808. for (List<Node *>::Element *F = owned.front(); F; F = F->next()) {
  1809. owners.push_back(F->get());
  1810. }
  1811. editor_data->get_undo_redo().add_do_method(n->get_parent(), "remove_child", n);
  1812. editor_data->get_undo_redo().add_undo_method(n->get_parent(), "add_child", n);
  1813. editor_data->get_undo_redo().add_undo_method(n->get_parent(), "move_child", n, n->get_index());
  1814. if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == n) {
  1815. editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", n);
  1816. }
  1817. editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
  1818. editor_data->get_undo_redo().add_undo_reference(n);
  1819. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  1820. 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());
  1821. 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());
  1822. }
  1823. }
  1824. editor_data->get_undo_redo().commit_action();
  1825. // hack, force 2d editor viewport to refresh after deletion
  1826. if (CanvasItemEditor *editor = CanvasItemEditor::get_singleton()) {
  1827. editor->get_viewport_control()->update();
  1828. }
  1829. _push_item(nullptr);
  1830. // Fixes the EditorHistory from still offering deleted notes
  1831. EditorHistory *editor_history = EditorNode::get_singleton()->get_editor_history();
  1832. editor_history->cleanup_history();
  1833. EditorNode::get_singleton()->get_inspector_dock()->call("_prepare_history");
  1834. }
  1835. void SceneTreeDock::_update_script_button() {
  1836. if (!profile_allow_script_editing) {
  1837. button_create_script->hide();
  1838. button_detach_script->hide();
  1839. } else if (editor_selection->get_selection().size() == 0) {
  1840. button_create_script->hide();
  1841. button_detach_script->hide();
  1842. } else if (editor_selection->get_selection().size() == 1) {
  1843. Node *n = editor_selection->get_selected_node_list()[0];
  1844. if (n->get_script().is_null()) {
  1845. button_create_script->show();
  1846. button_detach_script->hide();
  1847. } else {
  1848. button_create_script->hide();
  1849. button_detach_script->show();
  1850. }
  1851. } else {
  1852. button_create_script->hide();
  1853. Array selection = editor_selection->get_selected_nodes();
  1854. for (int i = 0; i < selection.size(); i++) {
  1855. Node *n = Object::cast_to<Node>(selection[i]);
  1856. if (!n->get_script().is_null()) {
  1857. button_detach_script->show();
  1858. return;
  1859. }
  1860. }
  1861. button_detach_script->hide();
  1862. }
  1863. }
  1864. void SceneTreeDock::_selection_changed() {
  1865. int selection_size = editor_selection->get_selection().size();
  1866. if (selection_size > 1) {
  1867. //automatically turn on multi-edit
  1868. _tool_selected(TOOL_MULTI_EDIT);
  1869. } else if (selection_size == 1) {
  1870. _handle_select(editor_selection->get_selection().front()->key());
  1871. } else if (selection_size == 0) {
  1872. _push_item(nullptr);
  1873. }
  1874. _update_script_button();
  1875. }
  1876. Node *SceneTreeDock::_get_selection_group_tail(Node *p_node, List<Node *> p_list) {
  1877. Node *tail = p_node;
  1878. Node *parent = tail->get_parent();
  1879. for (int i = p_node->get_position_in_parent(); i < parent->get_child_count(); i++) {
  1880. Node *sibling = parent->get_child(i);
  1881. if (p_list.find(sibling)) {
  1882. tail = sibling;
  1883. } else {
  1884. break;
  1885. }
  1886. }
  1887. return tail;
  1888. }
  1889. void SceneTreeDock::_do_create(Node *p_parent) {
  1890. Variant c = create_dialog->instance_selected();
  1891. ERR_FAIL_COND(!c);
  1892. Node *child = Object::cast_to<Node>(c);
  1893. ERR_FAIL_COND(!child);
  1894. editor_data->get_undo_redo().create_action(TTR("Create Node"));
  1895. if (edited_scene) {
  1896. editor_data->get_undo_redo().add_do_method(p_parent, "add_child", child);
  1897. editor_data->get_undo_redo().add_do_method(child, "set_owner", edited_scene);
  1898. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  1899. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", child);
  1900. editor_data->get_undo_redo().add_do_reference(child);
  1901. editor_data->get_undo_redo().add_undo_method(p_parent, "remove_child", child);
  1902. String new_name = p_parent->validate_child_name(child);
  1903. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  1904. 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);
  1905. 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)));
  1906. } else {
  1907. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", child);
  1908. editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
  1909. editor_data->get_undo_redo().add_do_reference(child);
  1910. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", (Object *)nullptr);
  1911. }
  1912. editor_data->get_undo_redo().commit_action();
  1913. _push_item(c);
  1914. editor_selection->clear();
  1915. editor_selection->add_node(child);
  1916. if (Object::cast_to<Control>(c)) {
  1917. //make editor more comfortable, so some controls don't appear super shrunk
  1918. Control *ct = Object::cast_to<Control>(c);
  1919. Size2 ms = ct->get_minimum_size();
  1920. if (ms.width < 4) {
  1921. ms.width = 40;
  1922. }
  1923. if (ms.height < 4) {
  1924. ms.height = 40;
  1925. }
  1926. ct->set_size(ms);
  1927. }
  1928. emit_signal("node_created", c);
  1929. }
  1930. void SceneTreeDock::_create() {
  1931. if (current_option == TOOL_NEW) {
  1932. Node *parent = nullptr;
  1933. if (edited_scene) {
  1934. // If root exists in edited scene
  1935. parent = scene_tree->get_selected();
  1936. if (!parent) {
  1937. parent = edited_scene;
  1938. }
  1939. } else {
  1940. // If no root exist in edited scene
  1941. parent = scene_root;
  1942. ERR_FAIL_COND(!parent);
  1943. }
  1944. _do_create(parent);
  1945. } else if (current_option == TOOL_REPLACE) {
  1946. List<Node *> selection = editor_selection->get_selected_node_list();
  1947. ERR_FAIL_COND(selection.size() <= 0);
  1948. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  1949. ur->create_action(TTR("Change type of node(s)"));
  1950. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1951. Node *n = E->get();
  1952. ERR_FAIL_COND(!n);
  1953. Variant c = create_dialog->instance_selected();
  1954. ERR_FAIL_COND(!c);
  1955. Node *newnode = Object::cast_to<Node>(c);
  1956. ERR_FAIL_COND(!newnode);
  1957. ur->add_do_method(this, "replace_node", n, newnode, true, false);
  1958. ur->add_do_reference(newnode);
  1959. ur->add_undo_method(this, "replace_node", newnode, n, false, false);
  1960. ur->add_undo_reference(n);
  1961. }
  1962. ur->commit_action();
  1963. } else if (current_option == TOOL_REPARENT_TO_NEW_NODE) {
  1964. List<Node *> selection = editor_selection->get_selected_node_list();
  1965. ERR_FAIL_COND(selection.size() <= 0);
  1966. // Find top level node in selection
  1967. bool only_one_top_node = true;
  1968. Node *first = selection.front()->get();
  1969. ERR_FAIL_COND(!first);
  1970. int smaller_path_to_top = first->get_path_to(scene_root).get_name_count();
  1971. Node *top_node = first;
  1972. for (List<Node *>::Element *E = selection.front()->next(); E; E = E->next()) {
  1973. Node *n = E->get();
  1974. ERR_FAIL_COND(!n);
  1975. int path_length = n->get_path_to(scene_root).get_name_count();
  1976. if (top_node != n) {
  1977. if (smaller_path_to_top > path_length) {
  1978. top_node = n;
  1979. smaller_path_to_top = path_length;
  1980. only_one_top_node = true;
  1981. } else if (smaller_path_to_top == path_length) {
  1982. if (only_one_top_node && top_node->get_parent() != n->get_parent()) {
  1983. only_one_top_node = false;
  1984. }
  1985. }
  1986. }
  1987. }
  1988. Node *parent = nullptr;
  1989. if (only_one_top_node) {
  1990. parent = top_node->get_parent();
  1991. } else {
  1992. parent = top_node->get_parent()->get_parent();
  1993. }
  1994. _do_create(parent);
  1995. Vector<Node *> nodes;
  1996. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1997. nodes.push_back(E->get());
  1998. }
  1999. // This works because editor_selection was cleared and populated with last created node in _do_create()
  2000. Node *last_created = editor_selection->get_selected_node_list().front()->get();
  2001. _do_reparent(last_created, -1, nodes, true);
  2002. }
  2003. scene_tree->get_scene_tree()->call_deferred("grab_focus");
  2004. }
  2005. void SceneTreeDock::replace_node(Node *p_node, Node *p_by_node, bool p_keep_properties, bool p_remove_old) {
  2006. Node *n = p_node;
  2007. Node *newnode = p_by_node;
  2008. if (p_keep_properties) {
  2009. Node *default_oldnode = Object::cast_to<Node>(ClassDB::instance(n->get_class()));
  2010. List<PropertyInfo> pinfo;
  2011. n->get_property_list(&pinfo);
  2012. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  2013. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  2014. continue;
  2015. }
  2016. if (E->get().name == "__meta__") {
  2017. Dictionary metadata = n->get(E->get().name);
  2018. if (metadata.has("_editor_description_")) {
  2019. newnode->set_meta("_editor_description_", metadata["_editor_description_"]);
  2020. }
  2021. if (Object::cast_to<CanvasItem>(newnode) || Object::cast_to<Spatial>(newnode)) {
  2022. if (metadata.has("_edit_group_") && metadata["_edit_group_"]) {
  2023. newnode->set_meta("_edit_group_", true);
  2024. }
  2025. if (metadata.has("_edit_lock_") && metadata["_edit_lock_"]) {
  2026. newnode->set_meta("_edit_lock_", true);
  2027. }
  2028. }
  2029. continue;
  2030. }
  2031. if (default_oldnode->get(E->get().name) != n->get(E->get().name)) {
  2032. newnode->set(E->get().name, n->get(E->get().name));
  2033. }
  2034. }
  2035. memdelete(default_oldnode);
  2036. }
  2037. _push_item(nullptr);
  2038. //reconnect signals
  2039. List<MethodInfo> sl;
  2040. n->get_signal_list(&sl);
  2041. for (List<MethodInfo>::Element *E = sl.front(); E; E = E->next()) {
  2042. List<Object::Connection> cl;
  2043. n->get_signal_connection_list(E->get().name, &cl);
  2044. for (List<Object::Connection>::Element *F = cl.front(); F; F = F->next()) {
  2045. Object::Connection &c = F->get();
  2046. if (!(c.flags & Object::CONNECT_PERSIST)) {
  2047. continue;
  2048. }
  2049. newnode->connect(c.signal, c.target, c.method, c.binds, Object::CONNECT_PERSIST);
  2050. }
  2051. }
  2052. String newname = n->get_name();
  2053. List<Node *> to_erase;
  2054. for (int i = 0; i < n->get_child_count(); i++) {
  2055. if (n->get_child(i)->get_owner() == nullptr && n->is_owned_by_parent()) {
  2056. to_erase.push_back(n->get_child(i));
  2057. }
  2058. }
  2059. n->replace_by(newnode, true);
  2060. if (n == edited_scene) {
  2061. edited_scene = newnode;
  2062. editor->set_edited_scene(newnode);
  2063. }
  2064. //small hack to make collisionshapes and other kind of nodes to work
  2065. for (int i = 0; i < newnode->get_child_count(); i++) {
  2066. Node *c = newnode->get_child(i);
  2067. c->call("set_transform", c->call("get_transform"));
  2068. }
  2069. //p_remove_old was added to support undo
  2070. if (p_remove_old) {
  2071. editor_data->get_undo_redo().clear_history();
  2072. }
  2073. newnode->set_name(newname);
  2074. _push_item(newnode);
  2075. if (p_remove_old) {
  2076. memdelete(n);
  2077. while (to_erase.front()) {
  2078. memdelete(to_erase.front()->get());
  2079. to_erase.pop_front();
  2080. }
  2081. }
  2082. }
  2083. void SceneTreeDock::set_edited_scene(Node *p_scene) {
  2084. edited_scene = p_scene;
  2085. }
  2086. void SceneTreeDock::set_selected(Node *p_node, bool p_emit_selected) {
  2087. scene_tree->set_selected(p_node, p_emit_selected);
  2088. }
  2089. void SceneTreeDock::import_subscene() {
  2090. import_subscene_dialog->popup_centered_clamped(Size2(500, 800) * EDSCALE, 0.8);
  2091. }
  2092. void SceneTreeDock::_import_subscene() {
  2093. Node *parent = scene_tree->get_selected();
  2094. if (!parent) {
  2095. parent = editor_data->get_edited_scene_root();
  2096. ERR_FAIL_COND(!parent);
  2097. }
  2098. import_subscene_dialog->move(parent, edited_scene);
  2099. editor_data->get_undo_redo().clear_history(); //no undo for now..
  2100. }
  2101. void SceneTreeDock::_new_scene_from(String p_file) {
  2102. List<Node *> selection = editor_selection->get_selected_node_list();
  2103. if (selection.size() != 1) {
  2104. accept->set_text(TTR("This operation requires a single selected node."));
  2105. accept->popup_centered_minsize();
  2106. return;
  2107. }
  2108. if (EditorNode::get_singleton()->is_scene_open(p_file)) {
  2109. accept->set_text(TTR("Can't overwrite scene that is still open!"));
  2110. accept->popup_centered_minsize();
  2111. return;
  2112. }
  2113. Node *base = selection.front()->get();
  2114. Map<Node *, Node *> reown;
  2115. reown[editor_data->get_edited_scene_root()] = base;
  2116. Node *copy = base->duplicate_and_reown(reown);
  2117. if (copy) {
  2118. Ref<PackedScene> sdata = memnew(PackedScene);
  2119. Error err = sdata->pack(copy);
  2120. memdelete(copy);
  2121. if (err != OK) {
  2122. accept->set_text(TTR("Couldn't save new scene. Likely dependencies (instances) couldn't be satisfied."));
  2123. accept->popup_centered_minsize();
  2124. return;
  2125. }
  2126. int flg = 0;
  2127. if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources")) {
  2128. flg |= ResourceSaver::FLAG_COMPRESS;
  2129. }
  2130. err = ResourceSaver::save(p_file, sdata, flg);
  2131. if (err != OK) {
  2132. accept->set_text(TTR("Error saving scene."));
  2133. accept->popup_centered_minsize();
  2134. return;
  2135. }
  2136. _replace_with_branch_scene(p_file, base);
  2137. } else {
  2138. accept->set_text(TTR("Error duplicating scene to save it."));
  2139. accept->popup_centered_minsize();
  2140. return;
  2141. }
  2142. }
  2143. static bool _is_node_visible(Node *p_node) {
  2144. if (!p_node->get_owner()) {
  2145. return false;
  2146. }
  2147. if (p_node->get_owner() != EditorNode::get_singleton()->get_edited_scene() && !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node->get_owner())) {
  2148. return false;
  2149. }
  2150. return true;
  2151. }
  2152. static bool _has_visible_children(Node *p_node) {
  2153. bool collapsed = p_node->is_displayed_folded();
  2154. if (collapsed) {
  2155. return false;
  2156. }
  2157. for (int i = 0; i < p_node->get_child_count(); i++) {
  2158. Node *child = p_node->get_child(i);
  2159. if (!_is_node_visible(child)) {
  2160. continue;
  2161. }
  2162. return true;
  2163. }
  2164. return false;
  2165. }
  2166. void SceneTreeDock::_normalize_drop(Node *&to_node, int &to_pos, int p_type) {
  2167. to_pos = -1;
  2168. if (p_type == -1) {
  2169. //drop at above selected node
  2170. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  2171. to_node = nullptr;
  2172. ERR_FAIL_MSG("Cannot perform drop above the root node!");
  2173. }
  2174. to_pos = to_node->get_index();
  2175. to_node = to_node->get_parent();
  2176. } else if (p_type == 1) {
  2177. //drop at below selected node
  2178. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  2179. //if at lower sibling of root node
  2180. to_pos = 0; //just insert at beginning of root node
  2181. return;
  2182. }
  2183. Node *lower_sibling = nullptr;
  2184. if (_has_visible_children(to_node)) {
  2185. to_pos = 0;
  2186. } else {
  2187. for (int i = to_node->get_index() + 1; i < to_node->get_parent()->get_child_count(); i++) {
  2188. Node *c = to_node->get_parent()->get_child(i);
  2189. if (_is_node_visible(c)) {
  2190. lower_sibling = c;
  2191. break;
  2192. }
  2193. }
  2194. if (lower_sibling) {
  2195. to_pos = lower_sibling->get_index();
  2196. }
  2197. to_node = to_node->get_parent();
  2198. }
  2199. }
  2200. }
  2201. void SceneTreeDock::_files_dropped(Vector<String> p_files, NodePath p_to, int p_type) {
  2202. Node *node = get_node(p_to);
  2203. ERR_FAIL_COND(!node);
  2204. if (scene_tree->get_scene_tree()->get_drop_mode_flags() & Tree::DROP_MODE_INBETWEEN) {
  2205. // Dropped PackedScene, instance it.
  2206. int to_pos = -1;
  2207. _normalize_drop(node, to_pos, p_type);
  2208. _perform_instance_scenes(p_files, node, to_pos);
  2209. } else {
  2210. String res_path = p_files[0];
  2211. StringName res_type = EditorFileSystem::get_singleton()->get_file_type(res_path);
  2212. List<String> valid_properties;
  2213. List<PropertyInfo> pinfo;
  2214. node->get_property_list(&pinfo);
  2215. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  2216. PropertyInfo &p = E->get();
  2217. if (!(p.usage & PROPERTY_USAGE_EDITOR) || !(p.usage & PROPERTY_USAGE_STORAGE) || p.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  2218. continue;
  2219. }
  2220. Vector<String> valid_types = p.hint_string.split(",");
  2221. for (int i = 0; i < valid_types.size(); i++) {
  2222. String prop_type = valid_types[i];
  2223. 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)) {
  2224. valid_properties.push_back(p.name);
  2225. break;
  2226. }
  2227. }
  2228. }
  2229. if (valid_properties.size() > 1) {
  2230. property_drop_node = node;
  2231. resource_drop_path = res_path;
  2232. const EditorPropertyNameProcessor::Style style = EditorNode::get_singleton()->get_inspector_dock()->get_property_name_style();
  2233. menu_properties->clear();
  2234. for (List<String>::Element *E = valid_properties.front(); E; E = E->next()) {
  2235. String &p = E->get();
  2236. menu_properties->add_item(EditorPropertyNameProcessor::get_singleton()->process_name(p, style));
  2237. menu_properties->set_item_metadata(menu_properties->get_item_count() - 1, p);
  2238. }
  2239. menu_properties->set_size(Size2(1, 1));
  2240. menu_properties->set_position(get_global_mouse_position());
  2241. menu_properties->popup();
  2242. } else if (!valid_properties.empty()) {
  2243. _perform_property_drop(node, valid_properties[0], ResourceLoader::load(res_path));
  2244. }
  2245. }
  2246. }
  2247. void SceneTreeDock::_script_dropped(String p_file, NodePath p_to) {
  2248. Ref<Script> scr = ResourceLoader::load(p_file);
  2249. ERR_FAIL_COND(!scr.is_valid());
  2250. Node *n = get_node(p_to);
  2251. if (n) {
  2252. InspectorDock *inspector_dock = EditorNode::get_singleton()->get_inspector_dock();
  2253. UndoRedo &undo_redo = editor_data->get_undo_redo();
  2254. undo_redo.create_action(TTR("Attach Script"));
  2255. undo_redo.add_do_method(inspector_dock, "store_script_properties", n);
  2256. undo_redo.add_undo_method(inspector_dock, "store_script_properties", n);
  2257. undo_redo.add_do_method(n, "set_script", scr);
  2258. undo_redo.add_undo_method(n, "set_script", n->get_script());
  2259. undo_redo.add_do_method(inspector_dock, "apply_script_properties", n);
  2260. undo_redo.add_undo_method(inspector_dock, "apply_script_properties", n);
  2261. undo_redo.add_do_method(this, "_update_script_button");
  2262. undo_redo.add_undo_method(this, "_update_script_button");
  2263. undo_redo.commit_action();
  2264. }
  2265. }
  2266. void SceneTreeDock::_nodes_dragged(Array p_nodes, NodePath p_to, int p_type) {
  2267. List<Node *> selection = editor_selection->get_selected_node_list();
  2268. if (selection.empty()) {
  2269. return; //nothing to reparent
  2270. }
  2271. Node *to_node = get_node(p_to);
  2272. if (!to_node) {
  2273. return;
  2274. }
  2275. Vector<Node *> nodes;
  2276. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  2277. nodes.push_back(E->get());
  2278. }
  2279. int to_pos = -1;
  2280. _normalize_drop(to_node, to_pos, p_type);
  2281. _do_reparent(to_node, to_pos, nodes, !Input::get_singleton()->is_key_pressed(KEY_SHIFT));
  2282. }
  2283. void SceneTreeDock::_add_children_to_popup(Object *p_obj, int p_depth) {
  2284. if (p_depth > 8) {
  2285. return;
  2286. }
  2287. List<PropertyInfo> pinfo;
  2288. p_obj->get_property_list(&pinfo);
  2289. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  2290. if (!(E->get().usage & PROPERTY_USAGE_EDITOR)) {
  2291. continue;
  2292. }
  2293. if (E->get().hint != PROPERTY_HINT_RESOURCE_TYPE) {
  2294. continue;
  2295. }
  2296. Variant value = p_obj->get(E->get().name);
  2297. if (value.get_type() != Variant::OBJECT) {
  2298. continue;
  2299. }
  2300. Object *obj = value;
  2301. if (!obj) {
  2302. continue;
  2303. }
  2304. Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
  2305. if (menu->get_item_count() == 0) {
  2306. menu->add_submenu_item(TTR("Sub-Resources"), "Sub-Resources");
  2307. }
  2308. int index = menu_subresources->get_item_count();
  2309. menu_subresources->add_icon_item(icon, E->get().name.capitalize(), EDIT_SUBRESOURCE_BASE + subresources.size());
  2310. menu_subresources->set_item_h_offset(index, p_depth * 10 * EDSCALE);
  2311. subresources.push_back(obj->get_instance_id());
  2312. _add_children_to_popup(obj, p_depth + 1);
  2313. }
  2314. }
  2315. void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
  2316. if (!EditorNode::get_singleton()->get_edited_scene()) {
  2317. menu->clear();
  2318. if (profile_allow_editing) {
  2319. menu->add_icon_shortcut(get_icon("Add", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  2320. menu->add_icon_shortcut(get_icon("Instance", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANCE);
  2321. }
  2322. menu->set_size(Size2(1, 1));
  2323. menu->set_position(p_menu_pos);
  2324. menu->popup();
  2325. return;
  2326. }
  2327. List<Node *> selection = editor_selection->get_selected_node_list();
  2328. List<Node *> full_selection = editor_selection->get_full_selected_node_list(); // Above method only returns nodes with common parent.
  2329. if (selection.size() == 0) {
  2330. return;
  2331. }
  2332. menu->clear();
  2333. Ref<Script> existing_script;
  2334. bool exisiting_script_removable = true;
  2335. if (selection.size() == 1) {
  2336. Node *selected = selection[0];
  2337. if (profile_allow_editing) {
  2338. subresources.clear();
  2339. menu_subresources->clear();
  2340. menu_subresources->set_size(Size2(1, 1));
  2341. _add_children_to_popup(selection.front()->get(), 0);
  2342. if (menu->get_item_count() > 0) {
  2343. menu->add_separator();
  2344. }
  2345. menu->add_icon_shortcut(get_icon("Add", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  2346. menu->add_icon_shortcut(get_icon("Instance", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANCE);
  2347. }
  2348. menu->add_icon_shortcut(get_icon("Collapse", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/expand_collapse_all"), TOOL_EXPAND_COLLAPSE);
  2349. menu->add_separator();
  2350. existing_script = selected->get_script();
  2351. if (EditorNode::get_singleton()->get_object_custom_type_base(selected) == existing_script) {
  2352. exisiting_script_removable = false;
  2353. }
  2354. }
  2355. if (profile_allow_editing) {
  2356. menu->add_shortcut(ED_GET_SHORTCUT("scene_tree/cut_node"), TOOL_CUT);
  2357. menu->add_shortcut(ED_GET_SHORTCUT("scene_tree/copy_node"), TOOL_COPY);
  2358. if (selection.size() == 1 && !node_clipboard.empty()) {
  2359. menu->add_shortcut(ED_GET_SHORTCUT("scene_tree/paste_node"), TOOL_PASTE);
  2360. }
  2361. menu->add_separator();
  2362. }
  2363. if (profile_allow_script_editing) {
  2364. bool add_separator = false;
  2365. if (full_selection.size() == 1) {
  2366. add_separator = true;
  2367. menu->add_icon_shortcut(get_icon("ScriptCreate", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/attach_script"), TOOL_ATTACH_SCRIPT);
  2368. if (existing_script.is_valid()) {
  2369. menu->add_icon_shortcut(get_icon("ScriptExtend", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/extend_script"), TOOL_EXTEND_SCRIPT);
  2370. }
  2371. }
  2372. if (existing_script.is_valid() && exisiting_script_removable) {
  2373. add_separator = true;
  2374. menu->add_icon_shortcut(get_icon("ScriptRemove", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/detach_script"), TOOL_DETACH_SCRIPT);
  2375. } else if (full_selection.size() > 1) {
  2376. bool script_exists = false;
  2377. for (List<Node *>::Element *E = full_selection.front(); E; E = E->next()) {
  2378. if (!E->get()->get_script().is_null()) {
  2379. script_exists = true;
  2380. break;
  2381. }
  2382. }
  2383. if (script_exists) {
  2384. add_separator = true;
  2385. menu->add_icon_shortcut(get_icon("ScriptRemove", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/detach_script"), TOOL_DETACH_SCRIPT);
  2386. }
  2387. }
  2388. if (add_separator && profile_allow_editing) {
  2389. menu->add_separator();
  2390. }
  2391. }
  2392. if (profile_allow_editing) {
  2393. // Allow multi-toggling scene unique names but only if all selected nodes are owned by the edited scene root.
  2394. bool all_owned = true;
  2395. for (List<Node *>::Element *e = full_selection.front(); e; e = e->next()) {
  2396. Node *node = e->get();
  2397. if (node->get_owner() != EditorNode::get_singleton()->get_edited_scene()) {
  2398. all_owned = false;
  2399. break;
  2400. }
  2401. }
  2402. if (all_owned) {
  2403. Node *node = full_selection[0];
  2404. menu->add_icon_shortcut(get_icon("SceneUniqueName", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/toggle_unique_name"), TOOL_TOGGLE_SCENE_UNIQUE_NAME);
  2405. 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"));
  2406. }
  2407. }
  2408. if (profile_allow_editing) {
  2409. bool add_separator = false;
  2410. if (full_selection.size() == 1) {
  2411. add_separator = true;
  2412. menu->add_icon_shortcut(get_icon("Rename", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/rename"), TOOL_RENAME);
  2413. }
  2414. bool can_replace = true;
  2415. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  2416. if (E->get() != edited_scene && (E->get()->get_owner() != edited_scene || E->get()->get_filename() != "")) {
  2417. can_replace = false;
  2418. break;
  2419. }
  2420. }
  2421. if (can_replace) {
  2422. add_separator = true;
  2423. menu->add_icon_shortcut(get_icon("Reload", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/change_node_type"), TOOL_REPLACE);
  2424. }
  2425. if (scene_tree->get_selected() != edited_scene) {
  2426. if (add_separator) {
  2427. menu->add_separator();
  2428. }
  2429. menu->add_icon_shortcut(get_icon("MoveUp", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/move_up"), TOOL_MOVE_UP);
  2430. menu->add_icon_shortcut(get_icon("MoveDown", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/move_down"), TOOL_MOVE_DOWN);
  2431. menu->add_icon_shortcut(get_icon("Duplicate", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/duplicate"), TOOL_DUPLICATE);
  2432. menu->add_icon_shortcut(get_icon("Reparent", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/reparent"), TOOL_REPARENT);
  2433. menu->add_icon_shortcut(get_icon("ReparentToNewNode", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/reparent_to_new_node"), TOOL_REPARENT_TO_NEW_NODE);
  2434. if (selection.size() == 1) {
  2435. menu->add_icon_shortcut(get_icon("NewRoot", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/make_root"), TOOL_MAKE_ROOT);
  2436. }
  2437. }
  2438. }
  2439. if (selection.size() == 1) {
  2440. if (profile_allow_editing) {
  2441. menu->add_separator();
  2442. menu->add_icon_shortcut(get_icon("Blend", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/merge_from_scene"), TOOL_MERGE_FROM_SCENE);
  2443. menu->add_icon_shortcut(get_icon("CreateNewSceneFrom", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/save_branch_as_scene"), TOOL_NEW_SCENE_FROM);
  2444. }
  2445. if (full_selection.size() == 1) {
  2446. menu->add_separator();
  2447. menu->add_icon_shortcut(get_icon("CopyNodePath", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/copy_node_path"), TOOL_COPY_NODE_PATH);
  2448. }
  2449. bool is_external = (selection[0]->get_filename() != "");
  2450. if (is_external) {
  2451. bool is_inherited = selection[0]->get_scene_inherited_state() != nullptr;
  2452. bool is_top_level = selection[0]->get_owner() == nullptr;
  2453. if (is_inherited && is_top_level) {
  2454. menu->add_separator();
  2455. if (profile_allow_editing) {
  2456. menu->add_item(TTR("Clear Inheritance"), TOOL_SCENE_CLEAR_INHERITANCE);
  2457. }
  2458. menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Open in Editor"), TOOL_SCENE_OPEN_INHERITED);
  2459. } else if (!is_top_level) {
  2460. menu->add_separator();
  2461. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(selection[0]);
  2462. bool placeholder = selection[0]->get_scene_instance_load_placeholder();
  2463. if (profile_allow_editing) {
  2464. menu->add_check_item(TTR("Editable Children"), TOOL_SCENE_EDITABLE_CHILDREN);
  2465. menu->add_check_item(TTR("Load As Placeholder"), TOOL_SCENE_USE_PLACEHOLDER);
  2466. menu->add_item(TTR("Make Local"), TOOL_SCENE_MAKE_LOCAL);
  2467. }
  2468. menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Open in Editor"), TOOL_SCENE_OPEN);
  2469. if (profile_allow_editing) {
  2470. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Editable Children")), editable);
  2471. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Load As Placeholder")), placeholder);
  2472. }
  2473. }
  2474. }
  2475. }
  2476. #ifdef MODULE_REGEX_ENABLED
  2477. if (profile_allow_editing && selection.size() > 1) {
  2478. //this is not a commonly used action, it makes no sense for it to be where it was nor always present.
  2479. menu->add_separator();
  2480. menu->add_icon_shortcut(get_icon("Rename", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/batch_rename"), TOOL_BATCH_RENAME);
  2481. }
  2482. #endif // MODULE_REGEX_ENABLED
  2483. menu->add_separator();
  2484. menu->add_icon_item(get_icon("Help", "EditorIcons"), TTR("Open Documentation"), TOOL_OPEN_DOCUMENTATION);
  2485. if (profile_allow_editing) {
  2486. menu->add_separator();
  2487. menu->add_icon_shortcut(get_icon("Remove", "EditorIcons"), ED_SHORTCUT("scene_tree/delete", TTR("Delete Node(s)"), KEY_DELETE), TOOL_ERASE);
  2488. }
  2489. menu->set_size(Size2(1, 1));
  2490. menu->set_position(p_menu_pos);
  2491. menu->popup();
  2492. }
  2493. void SceneTreeDock::_update_filter_menu() {
  2494. _append_filter_options_to(filter->get_menu());
  2495. }
  2496. void SceneTreeDock::_filter_changed(const String &p_filter) {
  2497. scene_tree->set_filter(p_filter);
  2498. String warning = scene_tree->get_filter_term_warning();
  2499. if (!warning.empty()) {
  2500. filter->add_icon_override("clear", get_icon("NodeWarning", "EditorIcons"));
  2501. filter->set_tooltip(warning);
  2502. } else {
  2503. filter->remove_icon_override("clear");
  2504. filter->set_tooltip("");
  2505. }
  2506. }
  2507. void SceneTreeDock::_filter_gui_input(const Ref<InputEvent> &p_event) {
  2508. Ref<InputEventMouseButton> mb = p_event;
  2509. if (mb.is_null()) {
  2510. return;
  2511. }
  2512. if (mb->is_pressed() && mb->get_button_index() == BUTTON_MIDDLE) {
  2513. filter_quick_menu->clear();
  2514. _append_filter_options_to(filter_quick_menu, false);
  2515. filter_quick_menu->set_position(get_global_position() + get_local_mouse_position());
  2516. filter_quick_menu->popup();
  2517. filter_quick_menu->grab_focus();
  2518. accept_event();
  2519. }
  2520. }
  2521. void SceneTreeDock::_filter_option_selected(int p_option) {
  2522. String filter_parameter;
  2523. switch (p_option) {
  2524. case FILTER_BY_TYPE: {
  2525. filter_parameter = "type";
  2526. } break;
  2527. case FILTER_BY_GROUP: {
  2528. filter_parameter = "group";
  2529. } break;
  2530. }
  2531. if (!filter_parameter.empty()) {
  2532. set_filter((get_filter() + " " + filter_parameter + ":").strip_edges());
  2533. filter->set_cursor_position(filter->get_text().length());
  2534. filter->grab_focus();
  2535. }
  2536. }
  2537. void SceneTreeDock::_append_filter_options_to(PopupMenu *p_menu, bool p_include_separator) {
  2538. if (p_include_separator) {
  2539. p_menu->add_separator();
  2540. }
  2541. p_menu->add_item(TTR("Filter by Type"), FILTER_BY_TYPE);
  2542. p_menu->add_item(TTR("Filter by Group"), FILTER_BY_GROUP);
  2543. p_menu->set_item_icon(p_menu->get_item_index(FILTER_BY_TYPE), get_icon("Node", "EditorIcons"));
  2544. p_menu->set_item_icon(p_menu->get_item_index(FILTER_BY_GROUP), get_icon("Groups", "EditorIcons"));
  2545. p_menu->set_item_tooltip(p_menu->get_item_index(FILTER_BY_TYPE), TTR("Selects all Nodes of the given type."));
  2546. 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."));
  2547. }
  2548. String SceneTreeDock::get_filter() {
  2549. return filter->get_text();
  2550. }
  2551. void SceneTreeDock::set_filter(const String &p_filter) {
  2552. filter->set_text(p_filter);
  2553. scene_tree->set_filter(p_filter);
  2554. }
  2555. void SceneTreeDock::save_branch_to_file(String p_directory) {
  2556. new_scene_from_dialog->set_current_dir(p_directory);
  2557. _tool_selected(TOOL_NEW_SCENE_FROM);
  2558. }
  2559. void SceneTreeDock::_focus_node() {
  2560. Node *node = scene_tree->get_selected();
  2561. ERR_FAIL_COND(!node);
  2562. if (node->is_class("CanvasItem")) {
  2563. CanvasItemEditorPlugin *editor = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
  2564. editor->get_canvas_item_editor()->focus_selection();
  2565. } else {
  2566. SpatialEditorPlugin *editor = Object::cast_to<SpatialEditorPlugin>(editor_data->get_editor("3D"));
  2567. editor->get_spatial_editor()->get_editor_viewport(0)->focus_selection();
  2568. }
  2569. }
  2570. void SceneTreeDock::attach_script_to_selected(bool p_extend) {
  2571. if (ScriptServer::get_language_count() == 0) {
  2572. 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."));
  2573. return;
  2574. }
  2575. if (!profile_allow_script_editing) {
  2576. return;
  2577. }
  2578. List<Node *> selection = editor_selection->get_selected_node_list();
  2579. if (selection.empty()) {
  2580. return;
  2581. }
  2582. Node *selected = scene_tree->get_selected();
  2583. if (!selected) {
  2584. selected = selection.front()->get();
  2585. }
  2586. Ref<Script> existing = selected->get_script();
  2587. String path = selected->get_filename();
  2588. if (path == "") {
  2589. String root_path = editor_data->get_edited_scene_root()->get_filename();
  2590. if (root_path == "") {
  2591. path = String("res://").plus_file(selected->get_name());
  2592. } else {
  2593. path = root_path.get_base_dir().plus_file(selected->get_name());
  2594. }
  2595. }
  2596. String inherits = selected->get_class();
  2597. if (p_extend && existing.is_valid()) {
  2598. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  2599. ScriptLanguage *l = ScriptServer::get_language(i);
  2600. if (l->get_type() == existing->get_class()) {
  2601. String name = l->get_global_class_name(existing->get_path());
  2602. if (ScriptServer::is_global_class(name) && EDITOR_GET("interface/editors/derive_script_globals_by_name").operator bool()) {
  2603. inherits = name;
  2604. } else if (l->can_inherit_from_file()) {
  2605. inherits = "\"" + existing->get_path() + "\"";
  2606. }
  2607. break;
  2608. }
  2609. }
  2610. }
  2611. script_create_dialog->connect("script_created", this, "_script_created");
  2612. script_create_dialog->connect("popup_hide", this, "_script_creation_closed", varray(), CONNECT_ONESHOT);
  2613. script_create_dialog->set_inheritance_base_type("Node");
  2614. script_create_dialog->config(inherits, path);
  2615. script_create_dialog->popup_centered();
  2616. }
  2617. void SceneTreeDock::open_script_dialog(Node *p_for_node, bool p_extend) {
  2618. scene_tree->set_selected(p_for_node, false);
  2619. if (p_extend) {
  2620. _tool_selected(TOOL_EXTEND_SCRIPT);
  2621. } else {
  2622. _tool_selected(TOOL_ATTACH_SCRIPT);
  2623. }
  2624. }
  2625. void SceneTreeDock::open_add_child_dialog() {
  2626. create_dialog->set_base_type("CanvasItem");
  2627. _tool_selected(TOOL_NEW, true);
  2628. reset_create_dialog = true;
  2629. }
  2630. void SceneTreeDock::open_instance_child_dialog() {
  2631. _tool_selected(TOOL_INSTANCE, true);
  2632. }
  2633. void SceneTreeDock::add_remote_tree_editor(Control *p_remote) {
  2634. ERR_FAIL_COND(remote_tree != nullptr);
  2635. add_child(p_remote);
  2636. remote_tree = p_remote;
  2637. remote_tree->hide();
  2638. }
  2639. void SceneTreeDock::show_remote_tree() {
  2640. _remote_tree_selected();
  2641. }
  2642. void SceneTreeDock::hide_remote_tree() {
  2643. _local_tree_selected();
  2644. }
  2645. void SceneTreeDock::show_tab_buttons() {
  2646. button_hb->show();
  2647. }
  2648. void SceneTreeDock::hide_tab_buttons() {
  2649. button_hb->hide();
  2650. }
  2651. void SceneTreeDock::_remote_tree_selected() {
  2652. scene_tree->hide();
  2653. create_root_dialog->hide();
  2654. if (remote_tree) {
  2655. remote_tree->show();
  2656. }
  2657. edit_remote->set_pressed(true);
  2658. edit_local->set_pressed(false);
  2659. emit_signal("remote_tree_selected");
  2660. }
  2661. void SceneTreeDock::_local_tree_selected() {
  2662. if (!bool(EDITOR_GET("interface/editors/show_scene_tree_root_selection")) || get_tree()->get_edited_scene_root() != nullptr) {
  2663. scene_tree->show();
  2664. }
  2665. if (remote_tree) {
  2666. remote_tree->hide();
  2667. }
  2668. edit_remote->set_pressed(false);
  2669. edit_local->set_pressed(true);
  2670. }
  2671. void SceneTreeDock::_update_create_root_dialog() {
  2672. BaseButton *toggle = Object::cast_to<BaseButton>(create_root_dialog->get_node(String("NodeShortcutsTopRow/NodeShortcutsToggle")));
  2673. Node *node_shortcuts = create_root_dialog->get_node(String("NodeShortcutsScrollContainer/NodeShortcuts"));
  2674. if (!toggle || !node_shortcuts) {
  2675. return;
  2676. }
  2677. Control *beginner_nodes = Object::cast_to<Control>(node_shortcuts->get_node(String("BeginnerNodeShortcuts")));
  2678. Control *favorite_nodes = Object::cast_to<Control>(node_shortcuts->get_node(String("FavoriteNodeShortcuts")));
  2679. if (!beginner_nodes || !favorite_nodes) {
  2680. return;
  2681. }
  2682. EditorSettings::get_singleton()->set_setting("_use_favorites_root_selection", toggle->is_pressed());
  2683. EditorSettings::get_singleton()->save();
  2684. if (toggle->is_pressed()) {
  2685. for (int i = 0; i < favorite_nodes->get_child_count(); i++) {
  2686. favorite_nodes->get_child(i)->queue_delete();
  2687. }
  2688. FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("favorites.Node"), FileAccess::READ);
  2689. if (f) {
  2690. while (!f->eof_reached()) {
  2691. String l = f->get_line().strip_edges();
  2692. if (l != String()) {
  2693. Button *button = memnew(Button);
  2694. favorite_nodes->add_child(button);
  2695. button->set_text(TTR(l));
  2696. button->set_clip_text(true);
  2697. String name = l.get_slicec(' ', 0);
  2698. if (ScriptServer::is_global_class(name)) {
  2699. name = ScriptServer::get_global_class_native_base(name);
  2700. }
  2701. button->set_icon(EditorNode::get_singleton()->get_class_icon(name));
  2702. button->connect("pressed", this, "_favorite_root_selected", make_binds(l));
  2703. }
  2704. }
  2705. memdelete(f);
  2706. }
  2707. if (!favorite_nodes->is_visible_in_tree()) {
  2708. favorite_nodes->show();
  2709. beginner_nodes->hide();
  2710. }
  2711. } else {
  2712. if (!beginner_nodes->is_visible_in_tree()) {
  2713. beginner_nodes->show();
  2714. favorite_nodes->hide();
  2715. }
  2716. }
  2717. }
  2718. void SceneTreeDock::_favorite_root_selected(const String &p_class) {
  2719. selected_favorite_root = p_class;
  2720. _tool_selected(TOOL_CREATE_FAVORITE, false);
  2721. }
  2722. void SceneTreeDock::_feature_profile_changed() {
  2723. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  2724. if (profile.is_valid()) {
  2725. profile_allow_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCENE_TREE);
  2726. profile_allow_script_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCRIPT);
  2727. bool profile_allow_3d = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D);
  2728. button_3d->set_visible(profile_allow_3d);
  2729. button_add->set_visible(profile_allow_editing);
  2730. button_instance->set_visible(profile_allow_editing);
  2731. scene_tree->set_can_rename(profile_allow_editing);
  2732. } else {
  2733. button_3d->set_visible(true);
  2734. button_add->set_visible(true);
  2735. button_instance->set_visible(true);
  2736. scene_tree->set_can_rename(true);
  2737. profile_allow_editing = true;
  2738. profile_allow_script_editing = true;
  2739. }
  2740. _update_script_button();
  2741. }
  2742. void SceneTreeDock::_clear_clipboard() {
  2743. for (List<Node *>::Element *E = node_clipboard.front(); E; E = E->next()) {
  2744. memdelete(E->get());
  2745. }
  2746. node_clipboard.clear();
  2747. clipboard_resource_remap.clear();
  2748. }
  2749. void SceneTreeDock::_create_remap_for_node(Node *p_node, Map<RES, RES> &r_remap) {
  2750. List<PropertyInfo> props;
  2751. p_node->get_property_list(&props);
  2752. Vector<SceneState::PackState> states_stack;
  2753. bool states_stack_ready = false;
  2754. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2755. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  2756. continue;
  2757. }
  2758. Variant v = p_node->get(E->get().name);
  2759. if (v.is_ref()) {
  2760. RES res = v;
  2761. if (res.is_valid()) {
  2762. if (!states_stack_ready) {
  2763. states_stack = PropertyUtils::get_node_states_stack(p_node);
  2764. states_stack_ready = true;
  2765. }
  2766. bool is_valid_default = false;
  2767. Variant orig = PropertyUtils::get_property_default_value(p_node, E->get().name, &is_valid_default, &states_stack);
  2768. if (is_valid_default && !PropertyUtils::is_property_value_different(v, orig)) {
  2769. continue;
  2770. }
  2771. if ((res->get_path() == "" || res->get_path().find("::") > -1) && !r_remap.has(res)) {
  2772. _create_remap_for_resource(res, r_remap);
  2773. }
  2774. }
  2775. }
  2776. }
  2777. for (int i = 0; i < p_node->get_child_count(); i++) {
  2778. _create_remap_for_node(p_node->get_child(i), r_remap);
  2779. }
  2780. }
  2781. void SceneTreeDock::_create_remap_for_resource(RES p_resource, Map<RES, RES> &r_remap) {
  2782. r_remap[p_resource] = p_resource->duplicate();
  2783. List<PropertyInfo> props;
  2784. p_resource->get_property_list(&props);
  2785. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2786. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  2787. continue;
  2788. }
  2789. Variant v = p_resource->get(E->get().name);
  2790. if (v.is_ref()) {
  2791. RES res = v;
  2792. if (res.is_valid()) {
  2793. if ((res->get_path() == "" || res->get_path().find("::") > -1) && !r_remap.has(res)) {
  2794. _create_remap_for_resource(res, r_remap);
  2795. }
  2796. }
  2797. }
  2798. }
  2799. }
  2800. void SceneTreeDock::_bind_methods() {
  2801. ClassDB::bind_method(D_METHOD("_tool_selected"), &SceneTreeDock::_tool_selected, DEFVAL(false));
  2802. ClassDB::bind_method(D_METHOD("_create"), &SceneTreeDock::_create);
  2803. ClassDB::bind_method(D_METHOD("_node_reparent"), &SceneTreeDock::_node_reparent);
  2804. ClassDB::bind_method(D_METHOD("_set_owners"), &SceneTreeDock::_set_owners);
  2805. ClassDB::bind_method(D_METHOD("_node_selected"), &SceneTreeDock::_node_selected);
  2806. ClassDB::bind_method(D_METHOD("_node_renamed"), &SceneTreeDock::_node_renamed);
  2807. ClassDB::bind_method(D_METHOD("_script_created"), &SceneTreeDock::_script_created);
  2808. ClassDB::bind_method(D_METHOD("_script_creation_closed"), &SceneTreeDock::_script_creation_closed);
  2809. ClassDB::bind_method(D_METHOD("_load_request"), &SceneTreeDock::_load_request);
  2810. ClassDB::bind_method(D_METHOD("_script_open_request"), &SceneTreeDock::_script_open_request);
  2811. ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &SceneTreeDock::_unhandled_key_input);
  2812. ClassDB::bind_method(D_METHOD("_input"), &SceneTreeDock::_input);
  2813. ClassDB::bind_method(D_METHOD("_nodes_drag_begin"), &SceneTreeDock::_nodes_drag_begin);
  2814. ClassDB::bind_method(D_METHOD("_delete_confirm"), &SceneTreeDock::_delete_confirm);
  2815. ClassDB::bind_method(D_METHOD("_toggle_editable_children_from_selection"), &SceneTreeDock::_toggle_editable_children_from_selection);
  2816. ClassDB::bind_method(D_METHOD("_toggle_placeholder_from_selection"), &SceneTreeDock::_toggle_placeholder_from_selection);
  2817. ClassDB::bind_method(D_METHOD("_node_prerenamed"), &SceneTreeDock::_node_prerenamed);
  2818. ClassDB::bind_method(D_METHOD("_import_subscene"), &SceneTreeDock::_import_subscene);
  2819. ClassDB::bind_method(D_METHOD("_selection_changed"), &SceneTreeDock::_selection_changed);
  2820. ClassDB::bind_method(D_METHOD("_node_collapsed"), &SceneTreeDock::_node_collapsed);
  2821. ClassDB::bind_method(D_METHOD("_new_scene_from"), &SceneTreeDock::_new_scene_from);
  2822. ClassDB::bind_method(D_METHOD("_nodes_dragged"), &SceneTreeDock::_nodes_dragged);
  2823. ClassDB::bind_method(D_METHOD("_files_dropped"), &SceneTreeDock::_files_dropped);
  2824. ClassDB::bind_method(D_METHOD("_property_selected"), &SceneTreeDock::_property_selected);
  2825. ClassDB::bind_method(D_METHOD("_quick_open"), &SceneTreeDock::_quick_open);
  2826. ClassDB::bind_method(D_METHOD("_script_dropped"), &SceneTreeDock::_script_dropped);
  2827. ClassDB::bind_method(D_METHOD("_tree_rmb"), &SceneTreeDock::_tree_rmb);
  2828. ClassDB::bind_method(D_METHOD("_filter_changed"), &SceneTreeDock::_filter_changed);
  2829. ClassDB::bind_method(D_METHOD("_filter_gui_input"), &SceneTreeDock::_filter_gui_input);
  2830. ClassDB::bind_method(D_METHOD("_update_filter_menu"), &SceneTreeDock::_update_filter_menu);
  2831. ClassDB::bind_method(D_METHOD("_filter_option_selected"), &SceneTreeDock::_filter_option_selected);
  2832. ClassDB::bind_method(D_METHOD("_focus_node"), &SceneTreeDock::_focus_node);
  2833. ClassDB::bind_method(D_METHOD("_remote_tree_selected"), &SceneTreeDock::_remote_tree_selected);
  2834. ClassDB::bind_method(D_METHOD("_local_tree_selected"), &SceneTreeDock::_local_tree_selected);
  2835. ClassDB::bind_method(D_METHOD("_update_script_button"), &SceneTreeDock::_update_script_button);
  2836. ClassDB::bind_method(D_METHOD("_favorite_root_selected"), &SceneTreeDock::_favorite_root_selected);
  2837. ClassDB::bind_method(D_METHOD("_update_create_root_dialog"), &SceneTreeDock::_update_create_root_dialog);
  2838. ClassDB::bind_method(D_METHOD("_feature_profile_changed"), &SceneTreeDock::_feature_profile_changed);
  2839. ClassDB::bind_method(D_METHOD("instance"), &SceneTreeDock::instance);
  2840. ClassDB::bind_method(D_METHOD("get_tree_editor"), &SceneTreeDock::get_tree_editor);
  2841. ClassDB::bind_method(D_METHOD("replace_node"), &SceneTreeDock::replace_node);
  2842. ADD_SIGNAL(MethodInfo("remote_tree_selected"));
  2843. ADD_SIGNAL(MethodInfo("add_node_used"));
  2844. ADD_SIGNAL(MethodInfo("node_created", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2845. }
  2846. SceneTreeDock::SceneTreeDock(EditorNode *p_editor, Node *p_scene_root, EditorSelection *p_editor_selection, EditorData &p_editor_data) {
  2847. set_name("Scene");
  2848. editor = p_editor;
  2849. edited_scene = nullptr;
  2850. editor_data = &p_editor_data;
  2851. editor_selection = p_editor_selection;
  2852. scene_root = p_scene_root;
  2853. pending_click_select = nullptr;
  2854. tree_clicked = false;
  2855. VBoxContainer *vbc = this;
  2856. HBoxContainer *filter_hbc = memnew(HBoxContainer);
  2857. filter_hbc->add_constant_override("separate", 0);
  2858. #ifdef OSX_ENABLED
  2859. ED_SHORTCUT("scene_tree/rename", TTR("Rename"), KEY_ENTER);
  2860. ED_SHORTCUT("scene_tree/batch_rename", TTR("Batch Rename"), KEY_MASK_SHIFT | KEY_ENTER);
  2861. #else
  2862. ED_SHORTCUT("scene_tree/rename", TTR("Rename"), KEY_F2);
  2863. ED_SHORTCUT("scene_tree/batch_rename", TTR("Batch Rename"), KEY_MASK_SHIFT | KEY_F2);
  2864. #endif
  2865. ED_SHORTCUT("scene_tree/add_child_node", TTR("Add Child Node"), KEY_MASK_CMD | KEY_A);
  2866. ED_SHORTCUT("scene_tree/instance_scene", TTR("Instance Child Scene"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_A);
  2867. ED_SHORTCUT("scene_tree/expand_collapse_all", TTR("Expand/Collapse All"));
  2868. ED_SHORTCUT("scene_tree/cut_node", TTR("Cut"), KEY_MASK_CMD | KEY_X);
  2869. ED_SHORTCUT("scene_tree/copy_node", TTR("Copy"), KEY_MASK_CMD | KEY_C);
  2870. ED_SHORTCUT("scene_tree/paste_node", TTR("Paste"), KEY_MASK_CMD | KEY_V);
  2871. ED_SHORTCUT("scene_tree/change_node_type", TTR("Change Type"));
  2872. ED_SHORTCUT("scene_tree/attach_script", TTR("Attach Script"));
  2873. ED_SHORTCUT("scene_tree/extend_script", TTR("Extend Script"));
  2874. ED_SHORTCUT("scene_tree/detach_script", TTR("Detach Script"));
  2875. ED_SHORTCUT("scene_tree/move_up", TTR("Move Up"), KEY_MASK_CMD | KEY_UP);
  2876. ED_SHORTCUT("scene_tree/move_down", TTR("Move Down"), KEY_MASK_CMD | KEY_DOWN);
  2877. ED_SHORTCUT("scene_tree/duplicate", TTR("Duplicate"), KEY_MASK_CMD | KEY_D);
  2878. ED_SHORTCUT("scene_tree/reparent", TTR("Reparent"));
  2879. ED_SHORTCUT("scene_tree/reparent_to_new_node", TTR("Reparent to New Node"));
  2880. ED_SHORTCUT("scene_tree/make_root", TTR("Make Scene Root"));
  2881. ED_SHORTCUT("scene_tree/merge_from_scene", TTR("Merge From Scene"));
  2882. ED_SHORTCUT("scene_tree/save_branch_as_scene", TTR("Save Branch as Scene"));
  2883. ED_SHORTCUT("scene_tree/copy_node_path", TTR("Copy Node Path"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_C);
  2884. ED_SHORTCUT("scene_tree/toggle_unique_name", TTR("Toggle Access as Unique Name"));
  2885. ED_SHORTCUT("scene_tree/delete_no_confirm", TTR("Delete (No Confirm)"), KEY_MASK_SHIFT | KEY_DELETE);
  2886. ED_SHORTCUT("scene_tree/delete", TTR("Delete"), KEY_DELETE);
  2887. button_add = memnew(ToolButton);
  2888. button_add->connect("pressed", this, "_tool_selected", make_binds(TOOL_NEW, false));
  2889. button_add->set_tooltip(TTR("Add/Create a New Node."));
  2890. button_add->set_shortcut(ED_GET_SHORTCUT("scene_tree/add_child_node"));
  2891. filter_hbc->add_child(button_add);
  2892. button_instance = memnew(ToolButton);
  2893. button_instance->connect("pressed", this, "_tool_selected", make_binds(TOOL_INSTANCE, false));
  2894. button_instance->set_tooltip(TTR("Instance a scene file as a Node. Creates an inherited scene if no root node exists."));
  2895. button_instance->set_shortcut(ED_GET_SHORTCUT("scene_tree/instance_scene"));
  2896. filter_hbc->add_child(button_instance);
  2897. vbc->add_child(filter_hbc);
  2898. // The "Filter Nodes" text input above the Scene Tree Editor.
  2899. filter = memnew(LineEdit);
  2900. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2901. filter->set_placeholder(TTR("Filter nodes"));
  2902. filter_hbc->add_child(filter);
  2903. filter->add_constant_override("minimum_spaces", 0);
  2904. filter->connect("text_changed", this, "_filter_changed");
  2905. filter->connect("gui_input", this, "_filter_gui_input");
  2906. filter->get_menu()->connect("id_pressed", this, "_filter_option_selected");
  2907. call_deferred("_update_filter_menu");
  2908. filter_quick_menu = memnew(PopupMenu);
  2909. filter_quick_menu->connect("id_pressed", this, "_filter_option_selected");
  2910. filter->add_child(filter_quick_menu);
  2911. button_create_script = memnew(ToolButton);
  2912. button_create_script->connect("pressed", this, "_tool_selected", make_binds(TOOL_ATTACH_SCRIPT, false));
  2913. button_create_script->set_tooltip(TTR("Attach a new or existing script to the selected node."));
  2914. button_create_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/attach_script"));
  2915. filter_hbc->add_child(button_create_script);
  2916. button_create_script->hide();
  2917. button_detach_script = memnew(ToolButton);
  2918. button_detach_script->connect("pressed", this, "_tool_selected", make_binds(TOOL_DETACH_SCRIPT, false));
  2919. button_detach_script->set_tooltip(TTR("Detach the script from the selected node."));
  2920. button_detach_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/detach_script"));
  2921. filter_hbc->add_child(button_detach_script);
  2922. button_detach_script->hide();
  2923. button_hb = memnew(HBoxContainer);
  2924. vbc->add_child(button_hb);
  2925. edit_remote = memnew(ToolButton);
  2926. button_hb->add_child(edit_remote);
  2927. edit_remote->set_h_size_flags(SIZE_EXPAND_FILL);
  2928. edit_remote->set_text(TTR("Remote"));
  2929. edit_remote->set_toggle_mode(true);
  2930. 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."));
  2931. edit_remote->connect("pressed", this, "_remote_tree_selected");
  2932. edit_local = memnew(ToolButton);
  2933. button_hb->add_child(edit_local);
  2934. edit_local->set_h_size_flags(SIZE_EXPAND_FILL);
  2935. edit_local->set_text(TTR("Local"));
  2936. edit_local->set_toggle_mode(true);
  2937. edit_local->connect("pressed", this, "_local_tree_selected");
  2938. remote_tree = nullptr;
  2939. button_hb->hide();
  2940. create_root_dialog = memnew(VBoxContainer);
  2941. vbc->add_child(create_root_dialog);
  2942. create_root_dialog->set_v_size_flags(SIZE_EXPAND_FILL);
  2943. create_root_dialog->hide();
  2944. scene_tree = memnew(SceneTreeEditor(false, true, true));
  2945. vbc->add_child(scene_tree);
  2946. scene_tree->set_v_size_flags(SIZE_EXPAND | SIZE_FILL);
  2947. scene_tree->connect("rmb_pressed", this, "_tree_rmb");
  2948. scene_tree->connect("node_selected", this, "_node_selected", varray(), CONNECT_DEFERRED);
  2949. scene_tree->connect("node_renamed", this, "_node_renamed", varray(), CONNECT_DEFERRED);
  2950. scene_tree->connect("node_prerename", this, "_node_prerenamed");
  2951. scene_tree->connect("open", this, "_load_request");
  2952. scene_tree->connect("open_script", this, "_script_open_request");
  2953. scene_tree->connect("nodes_rearranged", this, "_nodes_dragged");
  2954. scene_tree->connect("files_dropped", this, "_files_dropped");
  2955. scene_tree->connect("script_dropped", this, "_script_dropped");
  2956. scene_tree->connect("nodes_dragged", this, "_nodes_drag_begin");
  2957. scene_tree->get_scene_tree()->connect("item_double_clicked", this, "_focus_node");
  2958. scene_tree->set_undo_redo(&editor_data->get_undo_redo());
  2959. scene_tree->set_editor_selection(editor_selection);
  2960. create_dialog = memnew(CreateDialog);
  2961. create_dialog->set_base_type("Node");
  2962. add_child(create_dialog);
  2963. create_dialog->connect("create", this, "_create");
  2964. create_dialog->connect("favorites_updated", this, "_update_create_root_dialog");
  2965. #ifdef MODULE_REGEX_ENABLED
  2966. rename_dialog = memnew(RenameDialog(scene_tree, &editor_data->get_undo_redo()));
  2967. add_child(rename_dialog);
  2968. #endif // MODULE_REGEX_ENABLED
  2969. script_create_dialog = memnew(ScriptCreateDialog);
  2970. script_create_dialog->set_inheritance_base_type("Node");
  2971. add_child(script_create_dialog);
  2972. reparent_dialog = memnew(ReparentDialog);
  2973. add_child(reparent_dialog);
  2974. reparent_dialog->connect("reparent", this, "_node_reparent");
  2975. accept = memnew(AcceptDialog);
  2976. add_child(accept);
  2977. quick_open = memnew(EditorQuickOpen);
  2978. add_child(quick_open);
  2979. quick_open->connect("quick_open", this, "_quick_open");
  2980. set_process_unhandled_key_input(true);
  2981. delete_dialog = memnew(ConfirmationDialog);
  2982. add_child(delete_dialog);
  2983. delete_dialog->connect("confirmed", this, "_delete_confirm", varray(false));
  2984. editable_instance_remove_dialog = memnew(ConfirmationDialog);
  2985. add_child(editable_instance_remove_dialog);
  2986. editable_instance_remove_dialog->connect("confirmed", this, "_toggle_editable_children_from_selection");
  2987. placeholder_editable_instance_remove_dialog = memnew(ConfirmationDialog);
  2988. add_child(placeholder_editable_instance_remove_dialog);
  2989. placeholder_editable_instance_remove_dialog->connect("confirmed", this, "_toggle_placeholder_from_selection");
  2990. import_subscene_dialog = memnew(EditorSubScene);
  2991. add_child(import_subscene_dialog);
  2992. import_subscene_dialog->connect("subscene_selected", this, "_import_subscene");
  2993. new_scene_from_dialog = memnew(EditorFileDialog);
  2994. new_scene_from_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  2995. add_child(new_scene_from_dialog);
  2996. new_scene_from_dialog->connect("file_selected", this, "_new_scene_from");
  2997. menu = memnew(PopupMenu);
  2998. add_child(menu);
  2999. menu->connect("id_pressed", this, "_tool_selected");
  3000. menu->set_hide_on_window_lose_focus(true);
  3001. menu_subresources = memnew(PopupMenu);
  3002. menu_subresources->set_name("Sub-Resources");
  3003. menu_subresources->connect("id_pressed", this, "_tool_selected");
  3004. menu->add_child(menu_subresources);
  3005. first_enter = true;
  3006. menu_properties = memnew(PopupMenu);
  3007. add_child(menu_properties);
  3008. menu_properties->connect("id_pressed", this, "_property_selected");
  3009. clear_inherit_confirm = memnew(ConfirmationDialog);
  3010. clear_inherit_confirm->set_text(TTR("Clear Inheritance? (No Undo!)"));
  3011. clear_inherit_confirm->get_ok()->set_text(TTR("Clear"));
  3012. add_child(clear_inherit_confirm);
  3013. set_process_input(true);
  3014. set_process(true);
  3015. profile_allow_editing = true;
  3016. profile_allow_script_editing = true;
  3017. EDITOR_DEF("interface/editors/show_scene_tree_root_selection", true);
  3018. EDITOR_DEF("interface/editors/derive_script_globals_by_name", true);
  3019. EDITOR_DEF("_use_favorites_root_selection", false);
  3020. }
  3021. SceneTreeDock::~SceneTreeDock() {
  3022. if (!node_clipboard.empty()) {
  3023. _clear_clipboard();
  3024. }
  3025. }