2
0

scene_tree_dock.cpp 114 KB

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