scene_tree_dock.cpp 105 KB

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