123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270 |
- /*************************************************************************/
- /* scene_tree_dock.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "scene_tree_dock.h"
- #include "core/io/resource_saver.h"
- #include "core/os/input.h"
- #include "core/os/keyboard.h"
- #include "core/project_settings.h"
- #include "editor/editor_feature_profile.h"
- #include "editor/editor_node.h"
- #include "editor/editor_scale.h"
- #include "editor/editor_settings.h"
- #include "editor/multi_node_edit.h"
- #include "editor/plugins/animation_player_editor_plugin.h"
- #include "editor/plugins/canvas_item_editor_plugin.h"
- #include "editor/plugins/script_editor_plugin.h"
- #include "editor/plugins/spatial_editor_plugin.h"
- #include "editor/script_editor_debugger.h"
- #include "scene/main/viewport.h"
- #include "scene/resources/packed_scene.h"
- void SceneTreeDock::_nodes_drag_begin() {
- if (restore_script_editor_on_drag) {
- EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
- restore_script_editor_on_drag = false;
- }
- }
- void SceneTreeDock::_quick_open() {
- instance_scenes(quick_open->get_selected_files(), scene_tree->get_selected());
- }
- void SceneTreeDock::_input(Ref<InputEvent> p_event) {
- Ref<InputEventMouseButton> mb = p_event;
- if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
- restore_script_editor_on_drag = false; //lost chance
- }
- }
- void SceneTreeDock::_unhandled_key_input(Ref<InputEvent> p_event) {
- if (get_viewport()->get_modal_stack_top())
- return; //ignore because of modal window
- if (get_focus_owner() && get_focus_owner()->is_text_field())
- return;
- if (!p_event->is_pressed() || p_event->is_echo())
- return;
- if (ED_IS_SHORTCUT("scene_tree/batch_rename", p_event)) {
- _tool_selected(TOOL_BATCH_RENAME);
- } else if (ED_IS_SHORTCUT("scene_tree/rename", p_event)) {
- _tool_selected(TOOL_RENAME);
- } else if (ED_IS_SHORTCUT("scene_tree/add_child_node", p_event)) {
- _tool_selected(TOOL_NEW);
- } else if (ED_IS_SHORTCUT("scene_tree/instance_scene", p_event)) {
- _tool_selected(TOOL_INSTANCE);
- } else if (ED_IS_SHORTCUT("scene_tree/expand_collapse_all", p_event)) {
- _tool_selected(TOOL_EXPAND_COLLAPSE);
- } else if (ED_IS_SHORTCUT("scene_tree/cut_node", p_event)) {
- _tool_selected(TOOL_CUT);
- } else if (ED_IS_SHORTCUT("scene_tree/copy_node", p_event)) {
- _tool_selected(TOOL_COPY);
- } else if (ED_IS_SHORTCUT("scene_tree/paste_node", p_event)) {
- _tool_selected(TOOL_PASTE);
- } else if (ED_IS_SHORTCUT("scene_tree/change_node_type", p_event)) {
- _tool_selected(TOOL_REPLACE);
- } else if (ED_IS_SHORTCUT("scene_tree/duplicate", p_event)) {
- _tool_selected(TOOL_DUPLICATE);
- } else if (ED_IS_SHORTCUT("scene_tree/attach_script", p_event)) {
- _tool_selected(TOOL_ATTACH_SCRIPT);
- } else if (ED_IS_SHORTCUT("scene_tree/detach_script", p_event)) {
- _tool_selected(TOOL_DETACH_SCRIPT);
- } else if (ED_IS_SHORTCUT("scene_tree/move_up", p_event)) {
- _tool_selected(TOOL_MOVE_UP);
- } else if (ED_IS_SHORTCUT("scene_tree/move_down", p_event)) {
- _tool_selected(TOOL_MOVE_DOWN);
- } else if (ED_IS_SHORTCUT("scene_tree/reparent", p_event)) {
- _tool_selected(TOOL_REPARENT);
- } else if (ED_IS_SHORTCUT("scene_tree/merge_from_scene", p_event)) {
- _tool_selected(TOOL_MERGE_FROM_SCENE);
- } else if (ED_IS_SHORTCUT("scene_tree/save_branch_as_scene", p_event)) {
- _tool_selected(TOOL_NEW_SCENE_FROM);
- } else if (ED_IS_SHORTCUT("scene_tree/delete_no_confirm", p_event)) {
- _tool_selected(TOOL_ERASE, true);
- } else if (ED_IS_SHORTCUT("scene_tree/copy_node_path", p_event)) {
- _tool_selected(TOOL_COPY_NODE_PATH);
- } else if (ED_IS_SHORTCUT("scene_tree/delete", p_event)) {
- _tool_selected(TOOL_ERASE);
- }
- }
- void SceneTreeDock::instance(const String &p_file) {
- Vector<String> scenes;
- scenes.push_back(p_file);
- instance_scenes(scenes, scene_tree->get_selected());
- }
- void SceneTreeDock::instance_scenes(const Vector<String> &p_files, Node *p_parent) {
- Node *parent = p_parent;
- if (!parent) {
- parent = scene_tree->get_selected();
- }
- if (!parent || !edited_scene) {
- if (p_files.size() == 1) {
- accept->set_text(TTR("No parent to instance a child at."));
- } else {
- accept->set_text(TTR("No parent to instance the scenes at."));
- }
- accept->popup_centered_minsize();
- return;
- };
- _perform_instance_scenes(p_files, parent, -1);
- }
- void SceneTreeDock::_perform_instance_scenes(const Vector<String> &p_files, Node *parent, int p_pos) {
- ERR_FAIL_COND(!parent);
- Vector<Node *> instances;
- bool error = false;
- for (int i = 0; i < p_files.size(); i++) {
- Ref<PackedScene> sdata = ResourceLoader::load(p_files[i]);
- if (!sdata.is_valid()) {
- current_option = -1;
- accept->set_text(vformat(TTR("Error loading scene from %s"), p_files[i]));
- accept->popup_centered_minsize();
- error = true;
- break;
- }
- Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
- if (!instanced_scene) {
- current_option = -1;
- accept->set_text(vformat(TTR("Error instancing scene from %s"), p_files[i]));
- accept->popup_centered_minsize();
- error = true;
- break;
- }
- if (edited_scene->get_filename() != "") {
- if (_cyclical_dependency_exists(edited_scene->get_filename(), instanced_scene)) {
- accept->set_text(vformat(TTR("Cannot instance the scene '%s' because the current scene exists within one of its nodes."), p_files[i]));
- accept->popup_centered_minsize();
- error = true;
- break;
- }
- }
- instanced_scene->set_filename(ProjectSettings::get_singleton()->localize_path(p_files[i]));
- instances.push_back(instanced_scene);
- }
- if (error) {
- for (int i = 0; i < instances.size(); i++) {
- memdelete(instances[i]);
- }
- return;
- }
- editor_data->get_undo_redo().create_action(TTR("Instance Scene(s)"));
- for (int i = 0; i < instances.size(); i++) {
- Node *instanced_scene = instances[i];
- editor_data->get_undo_redo().add_do_method(parent, "add_child", instanced_scene);
- if (p_pos >= 0) {
- editor_data->get_undo_redo().add_do_method(parent, "move_child", instanced_scene, p_pos + i);
- }
- editor_data->get_undo_redo().add_do_method(instanced_scene, "set_owner", edited_scene);
- editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
- editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", instanced_scene);
- editor_data->get_undo_redo().add_do_reference(instanced_scene);
- editor_data->get_undo_redo().add_undo_method(parent, "remove_child", instanced_scene);
- String new_name = parent->validate_child_name(instanced_scene);
- ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
- editor_data->get_undo_redo().add_do_method(sed, "live_debug_instance_node", edited_scene->get_path_to(parent), p_files[i], new_name);
- editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)).plus_file(new_name)));
- }
- editor_data->get_undo_redo().commit_action();
- editor->push_item(instances[instances.size() - 1]);
- }
- void SceneTreeDock::_replace_with_branch_scene(const String &p_file, Node *base) {
- Ref<PackedScene> sdata = ResourceLoader::load(p_file);
- if (!sdata.is_valid()) {
- accept->set_text(vformat(TTR("Error loading scene from %s"), p_file));
- accept->popup_centered_minsize();
- return;
- }
- Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
- if (!instanced_scene) {
- accept->set_text(vformat(TTR("Error instancing scene from %s"), p_file));
- accept->popup_centered_minsize();
- return;
- }
- UndoRedo *undo_redo = editor->get_undo_redo();
- undo_redo->create_action(TTR("Replace with Branch Scene"));
- Node *parent = base->get_parent();
- int pos = base->get_index();
- undo_redo->add_do_method(parent, "remove_child", base);
- undo_redo->add_undo_method(parent, "remove_child", instanced_scene);
- undo_redo->add_do_method(parent, "add_child", instanced_scene);
- undo_redo->add_undo_method(parent, "add_child", base);
- undo_redo->add_do_method(parent, "move_child", instanced_scene, pos);
- undo_redo->add_undo_method(parent, "move_child", base, pos);
- List<Node *> owned;
- base->get_owned_by(base->get_owner(), &owned);
- Array owners;
- for (List<Node *>::Element *F = owned.front(); F; F = F->next()) {
- owners.push_back(F->get());
- }
- undo_redo->add_do_method(instanced_scene, "set_owner", edited_scene);
- undo_redo->add_undo_method(this, "_set_owners", edited_scene, owners);
- undo_redo->add_do_method(editor_selection, "clear");
- undo_redo->add_undo_method(editor_selection, "clear");
- undo_redo->add_do_method(editor_selection, "add_node", instanced_scene);
- undo_redo->add_undo_method(editor_selection, "add_node", base);
- undo_redo->add_do_property(scene_tree, "set_selected", instanced_scene);
- undo_redo->add_undo_property(scene_tree, "set_selected", base);
- undo_redo->add_do_reference(instanced_scene);
- undo_redo->add_undo_reference(base);
- undo_redo->commit_action();
- }
- bool SceneTreeDock::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
- int childCount = p_desired_node->get_child_count();
- if (_track_inherit(p_target_scene_path, p_desired_node)) {
- return true;
- }
- for (int i = 0; i < childCount; i++) {
- Node *child = p_desired_node->get_child(i);
- if (_cyclical_dependency_exists(p_target_scene_path, child)) {
- return true;
- }
- }
- return false;
- }
- bool SceneTreeDock::_track_inherit(const String &p_target_scene_path, Node *p_desired_node) {
- Node *p = p_desired_node;
- bool result = false;
- Vector<Node *> instances;
- while (true) {
- if (p->get_filename() == p_target_scene_path) {
- result = true;
- break;
- }
- Ref<SceneState> ss = p->get_scene_inherited_state();
- if (ss.is_valid()) {
- String path = ss->get_path();
- Ref<PackedScene> data = ResourceLoader::load(path);
- if (data.is_valid()) {
- p = data->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
- if (!p)
- continue;
- instances.push_back(p);
- } else
- break;
- } else
- break;
- }
- for (int i = 0; i < instances.size(); i++) {
- memdelete(instances[i]);
- }
- return result;
- }
- void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
- current_option = p_tool;
- switch (p_tool) {
- case TOOL_BATCH_RENAME: {
- if (!profile_allow_editing) {
- break;
- }
- if (editor_selection->get_selected_node_list().size() > 1) {
- rename_dialog->popup_centered();
- }
- } break;
- case TOOL_RENAME: {
- if (!profile_allow_editing) {
- break;
- }
- Tree *tree = scene_tree->get_scene_tree();
- if (tree->is_anything_selected()) {
- tree->grab_focus();
- tree->edit_selected();
- }
- } break;
- case TOOL_NEW:
- case TOOL_REPARENT_TO_NEW_NODE: {
- if (!profile_allow_editing) {
- break;
- }
- // Prefer nodes that inherit from the current scene root.
- Node *current_edited_scene_root = EditorNode::get_singleton()->get_edited_scene();
- if (current_edited_scene_root) {
- String root_class = current_edited_scene_root->get_class_name();
- static Vector<String> preferred_types;
- if (preferred_types.empty()) {
- preferred_types.push_back("Control");
- preferred_types.push_back("Node2D");
- preferred_types.push_back("Spatial");
- }
- for (int i = 0; i < preferred_types.size(); i++) {
- if (ClassDB::is_parent_class(root_class, preferred_types[i])) {
- create_dialog->set_preferred_search_result_type(preferred_types[i]);
- break;
- }
- }
- }
- create_dialog->popup_create(true);
- } break;
- case TOOL_INSTANCE: {
- if (!profile_allow_editing) {
- break;
- }
- Node *scene = edited_scene;
- if (!scene) {
- EditorNode::get_singleton()->new_inherited_scene();
- break;
- }
- quick_open->popup_dialog("PackedScene", true);
- quick_open->set_title(TTR("Instance Child Scene"));
- } break;
- case TOOL_EXPAND_COLLAPSE: {
- if (!scene_tree->get_selected())
- break;
- Tree *tree = scene_tree->get_scene_tree();
- TreeItem *selected_item = tree->get_selected();
- if (!selected_item)
- selected_item = tree->get_root();
- bool collapsed = _is_collapsed_recursive(selected_item);
- _set_collapsed_recursive(selected_item, !collapsed);
- tree->ensure_cursor_is_visible();
- } break;
- case TOOL_CUT:
- case TOOL_COPY: {
- if (!edited_scene || !_validate_no_foreign()) {
- break;
- }
- List<Node *> selection = editor_selection->get_selected_node_list();
- if (selection.size() == 0) {
- break;
- }
- if (!node_clipboard.empty()) {
- _clear_clipboard();
- }
- clipboard_source_scene = editor->get_edited_scene()->get_filename();
- selection.sort_custom<Node::Comparator>();
- for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
- Node *node = E->get();
- Map<const Node *, Node *> duplimap;
- Node *dup = node->duplicate_from_editor(duplimap);
- ERR_CONTINUE(!dup);
- node_clipboard.push_back(dup);
- }
- if (p_tool == TOOL_CUT) {
- _delete_confirm(true);
- }
- } break;
- case TOOL_PASTE: {
- if (node_clipboard.empty() || !edited_scene) {
- break;
- }
- bool has_cycle = false;
- if (edited_scene->get_filename() != String()) {
- for (List<Node *>::Element *E = node_clipboard.front(); E; E = E->next()) {
- if (edited_scene->get_filename() == E->get()->get_filename()) {
- has_cycle = true;
- break;
- }
- }
- }
- if (has_cycle) {
- current_option = -1;
- accept->set_text(TTR("Can't paste root node into the same scene."));
- accept->popup_centered();
- break;
- }
- Node *paste_parent = edited_scene;
- List<Node *> selection = editor_selection->get_selected_node_list();
- if (selection.size() > 0) {
- paste_parent = selection.back()->get();
- }
- Node *owner = paste_parent->get_owner();
- if (!owner) {
- owner = paste_parent;
- }
- editor_data->get_undo_redo().create_action(TTR("Paste Node(s)"));
- editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
- Map<RES, RES> resource_remap;
- String target_scene = editor->get_edited_scene()->get_filename();
- if (target_scene != clipboard_source_scene) {
- if (!clipboard_resource_remap.has(target_scene)) {
- Map<RES, RES> remap;
- for (List<Node *>::Element *E = node_clipboard.front(); E; E = E->next()) {
- _create_remap_for_node(E->get(), remap);
- }
- clipboard_resource_remap[target_scene] = remap;
- }
- resource_remap = clipboard_resource_remap[target_scene];
- }
- for (List<Node *>::Element *E = node_clipboard.front(); E; E = E->next()) {
- Node *node = E->get();
- Map<const Node *, Node *> duplimap;
- Node *dup = node->duplicate_from_editor(duplimap, resource_remap);
- ERR_CONTINUE(!dup);
- editor_data->get_undo_redo().add_do_method(paste_parent, "add_child", dup);
- for (Map<const Node *, Node *>::Element *E2 = duplimap.front(); E2; E2 = E2->next()) {
- Node *d = E2->value();
- editor_data->get_undo_redo().add_do_method(d, "set_owner", owner);
- }
- editor_data->get_undo_redo().add_do_method(dup, "set_owner", owner);
- editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", dup);
- editor_data->get_undo_redo().add_undo_method(paste_parent, "remove_child", dup);
- editor_data->get_undo_redo().add_do_reference(dup);
- if (node_clipboard.size() == 1) {
- editor_data->get_undo_redo().add_do_method(editor, "push_item", dup);
- }
- }
- editor_data->get_undo_redo().commit_action();
- } break;
- case TOOL_REPLACE: {
- if (!profile_allow_editing) {
- break;
- }
- if (!_validate_no_foreign()) {
- break;
- }
- if (!_validate_no_instance()) {
- break;
- }
- Node *selected = scene_tree->get_selected();
- if (!selected && !editor_selection->get_selected_node_list().empty())
- selected = editor_selection->get_selected_node_list().front()->get();
- if (selected)
- create_dialog->popup_create(false, true, selected->get_class());
- } break;
- case TOOL_EXTEND_SCRIPT: {
- attach_script_to_selected(true);
- } break;
- case TOOL_ATTACH_SCRIPT: {
- attach_script_to_selected(false);
- } break;
- case TOOL_DETACH_SCRIPT: {
- if (!profile_allow_script_editing) {
- break;
- }
- Array selection = editor_selection->get_selected_nodes();
- if (selection.empty())
- return;
- editor_data->get_undo_redo().create_action(TTR("Detach Script"));
- editor_data->get_undo_redo().add_do_method(editor, "push_item", (Script *)NULL);
- for (int i = 0; i < selection.size(); i++) {
- Node *n = Object::cast_to<Node>(selection[i]);
- Ref<Script> existing = n->get_script();
- Ref<Script> empty = EditorNode::get_singleton()->get_object_custom_type_base(n);
- if (existing != empty) {
- editor_data->get_undo_redo().add_do_method(n, "set_script", empty);
- editor_data->get_undo_redo().add_undo_method(n, "set_script", existing);
- }
- }
- editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
- editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
- editor_data->get_undo_redo().commit_action();
- } break;
- case TOOL_MOVE_UP:
- case TOOL_MOVE_DOWN: {
- if (!profile_allow_editing) {
- break;
- }
- if (!scene_tree->get_selected())
- break;
- if (scene_tree->get_selected() == edited_scene) {
- current_option = -1;
- accept->set_text(TTR("This operation can't be done on the tree root."));
- accept->popup_centered_minsize();
- break;
- }
- if (!_validate_no_foreign())
- break;
- bool MOVING_DOWN = (p_tool == TOOL_MOVE_DOWN);
- bool MOVING_UP = !MOVING_DOWN;
- Node *common_parent = scene_tree->get_selected()->get_parent();
- List<Node *> selection = editor_selection->get_selected_node_list();
- selection.sort_custom<Node::Comparator>(); // sort by index
- if (MOVING_DOWN)
- selection.invert();
- int lowest_id = common_parent->get_child_count() - 1;
- int highest_id = 0;
- for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
- int index = E->get()->get_index();
- if (index > highest_id) highest_id = index;
- if (index < lowest_id) lowest_id = index;
- if (E->get()->get_parent() != common_parent)
- common_parent = NULL;
- }
- if (!common_parent || (MOVING_DOWN && highest_id >= common_parent->get_child_count() - MOVING_DOWN) || (MOVING_UP && lowest_id == 0))
- break; // one or more nodes can not be moved
- if (selection.size() == 1) editor_data->get_undo_redo().create_action(TTR("Move Node In Parent"));
- if (selection.size() > 1) editor_data->get_undo_redo().create_action(TTR("Move Nodes In Parent"));
- for (int i = 0; i < selection.size(); i++) {
- Node *top_node = selection[i];
- Node *bottom_node = selection[selection.size() - 1 - i];
- ERR_FAIL_COND(!top_node->get_parent());
- ERR_FAIL_COND(!bottom_node->get_parent());
- int bottom_node_pos = bottom_node->get_index();
- int top_node_pos_next = top_node->get_index() + (MOVING_DOWN ? 1 : -1);
- editor_data->get_undo_redo().add_do_method(top_node->get_parent(), "move_child", top_node, top_node_pos_next);
- editor_data->get_undo_redo().add_undo_method(bottom_node->get_parent(), "move_child", bottom_node, bottom_node_pos);
- }
- editor_data->get_undo_redo().commit_action();
- } break;
- case TOOL_DUPLICATE: {
- if (!profile_allow_editing) {
- break;
- }
- if (!edited_scene)
- break;
- if (editor_selection->is_selected(edited_scene)) {
- current_option = -1;
- accept->set_text(TTR("This operation can't be done on the tree root."));
- accept->popup_centered_minsize();
- break;
- }
- if (!_validate_no_foreign())
- break;
- List<Node *> selection = editor_selection->get_selected_node_list();
- if (selection.size() == 0)
- break;
- editor_data->get_undo_redo().create_action(TTR("Duplicate Node(s)"));
- editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
- Node *dupsingle = NULL;
- List<Node *> editable_children;
- selection.sort_custom<Node::Comparator>();
- Node *add_below_node = selection.back()->get();
- for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
- Node *node = E->get();
- Node *parent = node->get_parent();
- List<Node *> owned;
- node->get_owned_by(node->get_owner(), &owned);
- Map<const Node *, Node *> duplimap;
- Node *dup = node->duplicate_from_editor(duplimap);
- if (EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(node))
- editable_children.push_back(dup);
- ERR_CONTINUE(!dup);
- if (selection.size() == 1)
- dupsingle = dup;
- dup->set_name(parent->validate_child_name(dup));
- editor_data->get_undo_redo().add_do_method(parent, "add_child_below_node", add_below_node, dup);
- for (List<Node *>::Element *F = owned.front(); F; F = F->next()) {
- if (!duplimap.has(F->get())) {
- continue;
- }
- Node *d = duplimap[F->get()];
- editor_data->get_undo_redo().add_do_method(d, "set_owner", node->get_owner());
- }
- editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", dup);
- editor_data->get_undo_redo().add_undo_method(parent, "remove_child", dup);
- editor_data->get_undo_redo().add_do_reference(dup);
- ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
- editor_data->get_undo_redo().add_do_method(sed, "live_debug_duplicate_node", edited_scene->get_path_to(node), dup->get_name());
- editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)).plus_file(dup->get_name())));
- add_below_node = dup;
- }
- editor_data->get_undo_redo().commit_action();
- if (dupsingle)
- editor->push_item(dupsingle);
- for (List<Node *>::Element *E = editable_children.back(); E; E = E->prev())
- _toggle_editable_children(E->get());
- } break;
- case TOOL_REPARENT: {
- if (!profile_allow_editing) {
- break;
- }
- if (!scene_tree->get_selected())
- break;
- if (editor_selection->is_selected(edited_scene)) {
- current_option = -1;
- accept->set_text(TTR("This operation can't be done on the tree root."));
- accept->popup_centered_minsize();
- break;
- }
- if (!_validate_no_foreign())
- break;
- List<Node *> nodes = editor_selection->get_selected_node_list();
- Set<Node *> nodeset;
- for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
- nodeset.insert(E->get());
- }
- reparent_dialog->popup_centered_ratio();
- reparent_dialog->set_current(nodeset);
- } break;
- case TOOL_MAKE_ROOT: {
- if (!profile_allow_editing) {
- break;
- }
- List<Node *> nodes = editor_selection->get_selected_node_list();
- ERR_FAIL_COND(nodes.size() != 1);
- Node *node = nodes.front()->get();
- Node *root = get_tree()->get_edited_scene_root();
- if (node == root)
- return;
- //check that from node to root, all owners are right
- if (root->get_scene_inherited_state().is_valid()) {
- accept->set_text(TTR("Can't reparent nodes in inherited scenes, order of nodes can't change."));
- accept->popup_centered_minsize();
- return;
- }
- if (node->get_owner() != root) {
- accept->set_text(TTR("Node must belong to the edited scene to become root."));
- accept->popup_centered_minsize();
- return;
- }
- if (node->get_filename() != String()) {
- accept->set_text(TTR("Instantiated scenes can't become root"));
- accept->popup_centered_minsize();
- return;
- }
- editor_data->get_undo_redo().create_action(TTR("Make node as Root"));
- editor_data->get_undo_redo().add_do_method(node->get_parent(), "remove_child", node);
- editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", node);
- editor_data->get_undo_redo().add_do_method(node, "add_child", root);
- editor_data->get_undo_redo().add_do_method(node, "set_filename", root->get_filename());
- editor_data->get_undo_redo().add_do_method(root, "set_filename", String());
- editor_data->get_undo_redo().add_do_method(node, "set_owner", (Object *)NULL);
- editor_data->get_undo_redo().add_do_method(root, "set_owner", node);
- _node_replace_owner(root, root, node, MODE_DO);
- editor_data->get_undo_redo().add_undo_method(root, "set_filename", root->get_filename());
- editor_data->get_undo_redo().add_undo_method(node, "set_filename", String());
- editor_data->get_undo_redo().add_undo_method(node, "remove_child", root);
- editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", root);
- editor_data->get_undo_redo().add_undo_method(node->get_parent(), "add_child", node);
- editor_data->get_undo_redo().add_undo_method(node->get_parent(), "move_child", node, node->get_index());
- editor_data->get_undo_redo().add_undo_method(root, "set_owner", (Object *)NULL);
- editor_data->get_undo_redo().add_undo_method(node, "set_owner", root);
- _node_replace_owner(root, root, root, MODE_UNDO);
- editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
- editor_data->get_undo_redo().add_undo_method(scene_tree, "update_tree");
- editor_data->get_undo_redo().commit_action();
- } break;
- case TOOL_MULTI_EDIT: {
- if (!profile_allow_editing) {
- break;
- }
- Node *root = EditorNode::get_singleton()->get_edited_scene();
- if (!root)
- break;
- Ref<MultiNodeEdit> mne = memnew(MultiNodeEdit);
- for (const Map<Node *, Object *>::Element *E = EditorNode::get_singleton()->get_editor_selection()->get_selection().front(); E; E = E->next()) {
- mne->add_node(root->get_path_to(E->key()));
- }
- EditorNode::get_singleton()->push_item(mne.ptr());
- } break;
- case TOOL_ERASE: {
- if (!profile_allow_editing) {
- break;
- }
- List<Node *> remove_list = editor_selection->get_selected_node_list();
- if (remove_list.empty())
- return;
- if (!_validate_no_foreign())
- break;
- if (p_confirm_override) {
- _delete_confirm();
- } else {
- String msg;
- if (remove_list.size() > 1) {
- bool any_children = false;
- for (int i = 0; !any_children && i < remove_list.size(); i++) {
- any_children = remove_list[i]->get_child_count() > 0;
- }
- msg = vformat(any_children ? TTR("Delete %d nodes and any children?") : TTR("Delete %d nodes?"), remove_list.size());
- } else {
- Node *node = remove_list[0];
- if (node == editor_data->get_edited_scene_root()) {
- msg = vformat(TTR("Delete the root node \"%s\"?"), node->get_name());
- } else if (node->get_filename() == "" && node->get_child_count() > 0) {
- // Display this message only for non-instanced scenes
- msg = vformat(TTR("Delete node \"%s\" and its children?"), node->get_name());
- } else {
- msg = vformat(TTR("Delete node \"%s\"?"), node->get_name());
- }
- }
- delete_dialog->set_text(msg);
- // Resize the dialog to its minimum size.
- // This prevents the dialog from being too wide after displaying
- // a deletion confirmation for a node with a long name.
- delete_dialog->set_size(Size2());
- delete_dialog->popup_centered_minsize();
- }
- } break;
- case TOOL_MERGE_FROM_SCENE: {
- if (!profile_allow_editing) {
- break;
- }
- EditorNode::get_singleton()->merge_from_scene();
- } break;
- case TOOL_NEW_SCENE_FROM: {
- if (!profile_allow_editing) {
- break;
- }
- Node *scene = editor_data->get_edited_scene_root();
- if (!scene) {
- accept->set_text(TTR("This operation can't be done without a scene."));
- accept->popup_centered_minsize();
- break;
- }
- List<Node *> selection = editor_selection->get_selected_node_list();
- if (selection.size() != 1) {
- accept->set_text(TTR("This operation requires a single selected node."));
- accept->popup_centered_minsize();
- break;
- }
- Node *tocopy = selection.front()->get();
- if (tocopy == scene) {
- accept->set_text(TTR("Can not perform with the root node."));
- accept->popup_centered_minsize();
- break;
- }
- if (tocopy != editor_data->get_edited_scene_root() && tocopy->get_filename() != "") {
- accept->set_text(TTR("This operation can't be done on instanced scenes."));
- accept->popup_centered_minsize();
- break;
- }
- new_scene_from_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
- List<String> extensions;
- Ref<PackedScene> sd = memnew(PackedScene);
- ResourceSaver::get_recognized_extensions(sd, &extensions);
- new_scene_from_dialog->clear_filters();
- for (int i = 0; i < extensions.size(); i++) {
- new_scene_from_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
- }
- String existing;
- if (extensions.size()) {
- String root_name(tocopy->get_name());
- existing = root_name + "." + extensions.front()->get().to_lower();
- }
- new_scene_from_dialog->set_current_path(existing);
- new_scene_from_dialog->popup_centered_ratio();
- new_scene_from_dialog->set_title(TTR("Save New Scene As..."));
- } break;
- case TOOL_COPY_NODE_PATH: {
- List<Node *> selection = editor_selection->get_selected_node_list();
- List<Node *>::Element *e = selection.front();
- if (e) {
- Node *node = e->get();
- if (node) {
- Node *root = EditorNode::get_singleton()->get_edited_scene();
- NodePath path = root->get_path().rel_path_to(node->get_path());
- OS::get_singleton()->set_clipboard(path);
- }
- }
- } break;
- case TOOL_OPEN_DOCUMENTATION: {
- List<Node *> selection = editor_selection->get_selected_node_list();
- for (int i = 0; i < selection.size(); i++) {
- ScriptEditor::get_singleton()->goto_help("class_name:" + selection[i]->get_class());
- }
- EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
- } break;
- case TOOL_SCENE_EDITABLE_CHILDREN: {
- if (!profile_allow_editing) {
- break;
- }
- List<Node *> selection = editor_selection->get_selected_node_list();
- List<Node *>::Element *e = selection.front();
- if (e) {
- Node *node = e->get();
- if (node) {
- bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(node);
- if (editable) {
- editable_instance_remove_dialog->set_text(TTR("Disabling \"editable_instance\" will cause all properties of the node to be reverted to their default."));
- editable_instance_remove_dialog->popup_centered_minsize();
- break;
- }
- _toggle_editable_children(node);
- }
- }
- } break;
- case TOOL_SCENE_USE_PLACEHOLDER: {
- if (!profile_allow_editing) {
- break;
- }
- List<Node *> selection = editor_selection->get_selected_node_list();
- List<Node *>::Element *e = selection.front();
- if (e) {
- Node *node = e->get();
- if (node) {
- bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(node);
- bool placeholder = node->get_scene_instance_load_placeholder();
- // Fire confirmation dialog when children are editable.
- if (editable && !placeholder) {
- 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."));
- placeholder_editable_instance_remove_dialog->popup_centered_minsize();
- break;
- }
- placeholder = !placeholder;
- if (placeholder)
- EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(node, false);
- node->set_scene_instance_load_placeholder(placeholder);
- scene_tree->update_tree();
- }
- }
- } break;
- case TOOL_SCENE_MAKE_LOCAL: {
- if (!profile_allow_editing) {
- break;
- }
- List<Node *> selection = editor_selection->get_selected_node_list();
- List<Node *>::Element *e = selection.front();
- if (e) {
- Node *node = e->get();
- if (node) {
- Node *root = EditorNode::get_singleton()->get_edited_scene();
- UndoRedo *undo_redo = &editor_data->get_undo_redo();
- if (!root)
- break;
- ERR_FAIL_COND(node->get_filename() == String());
- undo_redo->create_action(TTR("Make Local"));
- undo_redo->add_do_method(node, "set_filename", "");
- undo_redo->add_undo_method(node, "set_filename", node->get_filename());
- _node_replace_owner(node, node, root);
- undo_redo->add_do_method(scene_tree, "update_tree");
- undo_redo->add_undo_method(scene_tree, "update_tree");
- undo_redo->commit_action();
- }
- }
- } break;
- case TOOL_SCENE_OPEN: {
- List<Node *> selection = editor_selection->get_selected_node_list();
- List<Node *>::Element *e = selection.front();
- if (e) {
- Node *node = e->get();
- if (node) {
- scene_tree->emit_signal("open", node->get_filename());
- }
- }
- } break;
- case TOOL_SCENE_CLEAR_INHERITANCE: {
- if (!profile_allow_editing) {
- break;
- }
- clear_inherit_confirm->popup_centered_minsize();
- } break;
- case TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM: {
- if (!profile_allow_editing) {
- break;
- }
- List<Node *> selection = editor_selection->get_selected_node_list();
- List<Node *>::Element *e = selection.front();
- if (e) {
- Node *node = e->get();
- if (node) {
- node->set_scene_inherited_state(Ref<SceneState>());
- scene_tree->update_tree();
- EditorNode::get_singleton()->get_inspector()->update_tree();
- }
- }
- } break;
- case TOOL_SCENE_OPEN_INHERITED: {
- List<Node *> selection = editor_selection->get_selected_node_list();
- List<Node *>::Element *e = selection.front();
- if (e) {
- Node *node = e->get();
- if (node && node->get_scene_inherited_state().is_valid()) {
- scene_tree->emit_signal("open", node->get_scene_inherited_state()->get_path());
- }
- }
- } break;
- case TOOL_CREATE_2D_SCENE:
- case TOOL_CREATE_3D_SCENE:
- case TOOL_CREATE_USER_INTERFACE:
- case TOOL_CREATE_FAVORITE: {
- Node *new_node = NULL;
- if (TOOL_CREATE_FAVORITE == p_tool) {
- String name = selected_favorite_root.get_slicec(' ', 0);
- if (ScriptServer::is_global_class(name)) {
- new_node = Object::cast_to<Node>(ClassDB::instance(ScriptServer::get_global_class_native_base(name)));
- Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(name), "Script");
- if (new_node && script.is_valid()) {
- new_node->set_script(script.get_ref_ptr());
- new_node->set_name(name);
- }
- } else {
- new_node = Object::cast_to<Node>(ClassDB::instance(selected_favorite_root));
- }
- if (!new_node) {
- new_node = memnew(Node);
- ERR_PRINTS("Creating root from favorite '" + selected_favorite_root + "' failed. Creating 'Node' instead.");
- }
- } else {
- switch (p_tool) {
- case TOOL_CREATE_2D_SCENE: new_node = memnew(Node2D); break;
- case TOOL_CREATE_3D_SCENE: new_node = memnew(Spatial); break;
- case TOOL_CREATE_USER_INTERFACE: {
- Control *node = memnew(Control);
- node->set_anchors_and_margins_preset(PRESET_WIDE); //more useful for resizable UIs.
- new_node = node;
- } break;
- }
- }
- editor_data->get_undo_redo().create_action(TTR("New Scene Root"));
- editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", new_node);
- editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
- editor_data->get_undo_redo().add_do_reference(new_node);
- editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", (Object *)NULL);
- editor_data->get_undo_redo().commit_action();
- editor->edit_node(new_node);
- editor_selection->clear();
- editor_selection->add_node(new_node);
- } break;
- default: {
- if (p_tool >= EDIT_SUBRESOURCE_BASE) {
- int idx = p_tool - EDIT_SUBRESOURCE_BASE;
- ERR_FAIL_INDEX(idx, subresources.size());
- Object *obj = ObjectDB::get_instance(subresources[idx]);
- ERR_FAIL_COND(!obj);
- editor->push_item(obj);
- }
- }
- }
- }
- void SceneTreeDock::_node_collapsed(Object *p_obj) {
- TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
- if (!ti)
- return;
- if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
- _set_collapsed_recursive(ti, ti->is_collapsed());
- }
- }
- void SceneTreeDock::_notification(int p_what) {
- switch (p_what) {
- case NOTIFICATION_READY: {
- if (!first_enter)
- break;
- first_enter = false;
- EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", this, "_feature_profile_changed");
- CanvasItemEditorPlugin *canvas_item_plugin = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
- if (canvas_item_plugin) {
- canvas_item_plugin->get_canvas_item_editor()->connect("item_lock_status_changed", scene_tree, "_update_tree");
- canvas_item_plugin->get_canvas_item_editor()->connect("item_group_status_changed", scene_tree, "_update_tree");
- scene_tree->connect("node_changed", canvas_item_plugin->get_canvas_item_editor()->get_viewport_control(), "update");
- }
- SpatialEditorPlugin *spatial_editor_plugin = Object::cast_to<SpatialEditorPlugin>(editor_data->get_editor("3D"));
- spatial_editor_plugin->get_spatial_editor()->connect("item_lock_status_changed", scene_tree, "_update_tree");
- spatial_editor_plugin->get_spatial_editor()->connect("item_group_status_changed", scene_tree, "_update_tree");
- button_add->set_icon(get_icon("Add", "EditorIcons"));
- button_instance->set_icon(get_icon("Instance", "EditorIcons"));
- button_create_script->set_icon(get_icon("ScriptCreate", "EditorIcons"));
- button_detach_script->set_icon(get_icon("ScriptRemove", "EditorIcons"));
- filter->set_right_icon(get_icon("Search", "EditorIcons"));
- filter->set_clear_button_enabled(true);
- EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", this, "_selection_changed");
- scene_tree->get_scene_tree()->connect("item_collapsed", this, "_node_collapsed");
- // create_root_dialog
- HBoxContainer *top_row = memnew(HBoxContainer);
- top_row->set_name("NodeShortcutsTopRow");
- top_row->set_h_size_flags(SIZE_EXPAND_FILL);
- top_row->add_child(memnew(Label(TTR("Create Root Node:"))));
- top_row->add_spacer();
- ToolButton *node_shortcuts_toggle = memnew(ToolButton);
- node_shortcuts_toggle->set_name("NodeShortcutsToggle");
- node_shortcuts_toggle->set_icon(get_icon("Favorites", "EditorIcons"));
- node_shortcuts_toggle->set_toggle_mode(true);
- node_shortcuts_toggle->set_pressed(EDITOR_GET("_use_favorites_root_selection"));
- node_shortcuts_toggle->set_anchors_and_margins_preset(Control::PRESET_CENTER_RIGHT);
- node_shortcuts_toggle->connect("pressed", this, "_update_create_root_dialog");
- top_row->add_child(node_shortcuts_toggle);
- create_root_dialog->add_child(top_row);
- VBoxContainer *node_shortcuts = memnew(VBoxContainer);
- node_shortcuts->set_name("NodeShortcuts");
- VBoxContainer *beginner_node_shortcuts = memnew(VBoxContainer);
- beginner_node_shortcuts->set_name("BeginnerNodeShortcuts");
- node_shortcuts->add_child(beginner_node_shortcuts);
- button_2d = memnew(Button);
- beginner_node_shortcuts->add_child(button_2d);
- button_2d->set_text(TTR("2D Scene"));
- button_2d->set_icon(get_icon("Node2D", "EditorIcons"));
- button_2d->connect("pressed", this, "_tool_selected", make_binds(TOOL_CREATE_2D_SCENE, false));
- button_3d = memnew(Button);
- beginner_node_shortcuts->add_child(button_3d);
- button_3d->set_text(TTR("3D Scene"));
- button_3d->set_icon(get_icon("Spatial", "EditorIcons"));
- button_3d->connect("pressed", this, "_tool_selected", make_binds(TOOL_CREATE_3D_SCENE, false));
- button_ui = memnew(Button);
- beginner_node_shortcuts->add_child(button_ui);
- button_ui->set_text(TTR("User Interface"));
- button_ui->set_icon(get_icon("Control", "EditorIcons"));
- button_ui->connect("pressed", this, "_tool_selected", make_binds(TOOL_CREATE_USER_INTERFACE, false));
- VBoxContainer *favorite_node_shortcuts = memnew(VBoxContainer);
- favorite_node_shortcuts->set_name("FavoriteNodeShortcuts");
- node_shortcuts->add_child(favorite_node_shortcuts);
- button_custom = memnew(Button);
- node_shortcuts->add_child(button_custom);
- button_custom->set_text(TTR("Other Node"));
- button_custom->set_icon(get_icon("Add", "EditorIcons"));
- button_custom->connect("pressed", this, "_tool_selected", make_binds(TOOL_NEW, false));
- node_shortcuts->add_spacer();
- create_root_dialog->add_child(node_shortcuts);
- _update_create_root_dialog();
- } break;
- case NOTIFICATION_ENTER_TREE: {
- clear_inherit_confirm->connect("confirmed", this, "_tool_selected", varray(TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM));
- } break;
- case NOTIFICATION_EXIT_TREE: {
- clear_inherit_confirm->disconnect("confirmed", this, "_tool_selected");
- } break;
- case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
- button_add->set_icon(get_icon("Add", "EditorIcons"));
- button_instance->set_icon(get_icon("Instance", "EditorIcons"));
- button_create_script->set_icon(get_icon("ScriptCreate", "EditorIcons"));
- button_detach_script->set_icon(get_icon("ScriptRemove", "EditorIcons"));
- button_2d->set_icon(get_icon("Node2D", "EditorIcons"));
- button_3d->set_icon(get_icon("Spatial", "EditorIcons"));
- button_ui->set_icon(get_icon("Control", "EditorIcons"));
- button_custom->set_icon(get_icon("Add", "EditorIcons"));
- filter->set_right_icon(get_icon("Search", "EditorIcons"));
- filter->set_clear_button_enabled(true);
- } break;
- case NOTIFICATION_PROCESS: {
- bool show_create_root = bool(EDITOR_GET("interface/editors/show_scene_tree_root_selection")) && get_tree()->get_edited_scene_root() == NULL;
- if (show_create_root != create_root_dialog->is_visible_in_tree() && !remote_tree->is_visible()) {
- if (show_create_root) {
- create_root_dialog->show();
- scene_tree->hide();
- } else {
- create_root_dialog->hide();
- scene_tree->show();
- }
- }
- } break;
- }
- }
- void SceneTreeDock::_node_replace_owner(Node *p_base, Node *p_node, Node *p_root, ReplaceOwnerMode p_mode) {
- if (p_node->get_owner() == p_base && p_node != p_root) {
- UndoRedo *undo_redo = &editor_data->get_undo_redo();
- switch (p_mode) {
- case MODE_BIDI: {
- undo_redo->add_do_method(p_node, "set_owner", p_root);
- undo_redo->add_undo_method(p_node, "set_owner", p_base);
- } break;
- case MODE_DO: {
- undo_redo->add_do_method(p_node, "set_owner", p_root);
- } break;
- case MODE_UNDO: {
- undo_redo->add_undo_method(p_node, "set_owner", p_root);
- } break;
- }
- }
- for (int i = 0; i < p_node->get_child_count(); i++) {
- _node_replace_owner(p_base, p_node->get_child(i), p_root, p_mode);
- }
- }
- void SceneTreeDock::_load_request(const String &p_path) {
- editor->open_request(p_path);
- }
- void SceneTreeDock::_script_open_request(const Ref<Script> &p_script) {
- editor->edit_resource(p_script);
- }
- void SceneTreeDock::_node_selected() {
- Node *node = scene_tree->get_selected();
- if (!node) {
- return;
- }
- if (ScriptEditor::get_singleton()->is_visible_in_tree()) {
- restore_script_editor_on_drag = true;
- }
- editor->push_item(node);
- }
- void SceneTreeDock::_node_renamed() {
- _node_selected();
- }
- void SceneTreeDock::_set_owners(Node *p_owner, const Array &p_nodes) {
- for (int i = 0; i < p_nodes.size(); i++) {
- Node *n = Object::cast_to<Node>(p_nodes[i]);
- if (!n)
- continue;
- n->set_owner(p_owner);
- }
- }
- void SceneTreeDock::_fill_path_renames(Vector<StringName> base_path, Vector<StringName> new_base_path, Node *p_node, List<Pair<NodePath, NodePath> > *p_renames) {
- base_path.push_back(p_node->get_name());
- if (new_base_path.size())
- new_base_path.push_back(p_node->get_name());
- NodePath from(base_path, true);
- NodePath to;
- if (new_base_path.size())
- to = NodePath(new_base_path, true);
- Pair<NodePath, NodePath> npp;
- npp.first = from;
- npp.second = to;
- p_renames->push_back(npp);
- for (int i = 0; i < p_node->get_child_count(); i++) {
- _fill_path_renames(base_path, new_base_path, p_node->get_child(i), p_renames);
- }
- }
- void SceneTreeDock::fill_path_renames(Node *p_node, Node *p_new_parent, List<Pair<NodePath, NodePath> > *p_renames) {
- Vector<StringName> base_path;
- Node *n = p_node->get_parent();
- while (n) {
- base_path.push_back(n->get_name());
- n = n->get_parent();
- }
- base_path.invert();
- Vector<StringName> new_base_path;
- if (p_new_parent) {
- n = p_new_parent;
- while (n) {
- new_base_path.push_back(n->get_name());
- n = n->get_parent();
- }
- new_base_path.invert();
- }
- _fill_path_renames(base_path, new_base_path, p_node, p_renames);
- }
- void SceneTreeDock::perform_node_renames(Node *p_base, List<Pair<NodePath, NodePath> > *p_renames, Map<Ref<Animation>, Set<int> > *r_rem_anims) {
- Map<Ref<Animation>, Set<int> > rem_anims;
- if (!r_rem_anims)
- r_rem_anims = &rem_anims;
- if (!p_base) {
- p_base = edited_scene;
- }
- if (!p_base)
- return;
- // Renaming node paths used in script instances
- if (p_base->get_script_instance()) {
- ScriptInstance *si = p_base->get_script_instance();
- if (si) {
- List<PropertyInfo> properties;
- si->get_property_list(&properties);
- NodePath root_path = p_base->get_path();
- for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
- String propertyname = E->get().name;
- Variant p = p_base->get(propertyname);
- if (p.get_type() == Variant::NODE_PATH) {
- NodePath root_path_new = root_path;
- for (List<Pair<NodePath, NodePath> >::Element *F = p_renames->front(); F; F = F->next()) {
- if (root_path == F->get().first) {
- root_path_new = F->get().second;
- break;
- }
- }
- // Goes through all paths to check if its matching
- for (List<Pair<NodePath, NodePath> >::Element *F = p_renames->front(); F; F = F->next()) {
- NodePath rel_path_old = root_path.rel_path_to(F->get().first);
- // if old path detected, then it needs to be replaced with the new one
- if (p == rel_path_old) {
- NodePath rel_path_new = F->get().second;
- // if not empty, get new relative path
- if (!rel_path_new.is_empty()) {
- rel_path_new = root_path_new.rel_path_to(F->get().second);
- }
- editor_data->get_undo_redo().add_do_property(p_base, propertyname, rel_path_new);
- editor_data->get_undo_redo().add_undo_property(p_base, propertyname, rel_path_old);
- p_base->set(propertyname, rel_path_new);
- break;
- }
- // update the node itself if it has a valid node path and has not been deleted
- if (root_path == F->get().first && p != NodePath() && F->get().second != NodePath()) {
- NodePath abs_path = NodePath(String(root_path).plus_file(p)).simplified();
- NodePath rel_path_new = F->get().second.rel_path_to(abs_path);
- editor_data->get_undo_redo().add_do_property(p_base, propertyname, rel_path_new);
- editor_data->get_undo_redo().add_undo_property(p_base, propertyname, p);
- p_base->set(propertyname, rel_path_new);
- }
- }
- }
- }
- }
- }
- bool autorename_animation_tracks = bool(EDITOR_DEF("editors/animation/autorename_animation_tracks", true));
- if (autorename_animation_tracks && Object::cast_to<AnimationPlayer>(p_base)) {
- AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_base);
- List<StringName> anims;
- ap->get_animation_list(&anims);
- Node *root = ap->get_node(ap->get_root());
- if (root) {
- NodePath root_path = root->get_path();
- NodePath new_root_path = root_path;
- for (List<Pair<NodePath, NodePath> >::Element *E = p_renames->front(); E; E = E->next()) {
- if (E->get().first == root_path) {
- new_root_path = E->get().second;
- break;
- }
- }
- if (new_root_path != NodePath()) {
- //will not be erased
- for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
- Ref<Animation> anim = ap->get_animation(E->get());
- if (!r_rem_anims->has(anim)) {
- r_rem_anims->insert(anim, Set<int>());
- Set<int> &ran = r_rem_anims->find(anim)->get();
- for (int i = 0; i < anim->get_track_count(); i++)
- ran.insert(i);
- }
- Set<int> &ran = r_rem_anims->find(anim)->get();
- if (anim.is_null())
- continue;
- for (int i = 0; i < anim->get_track_count(); i++) {
- NodePath track_np = anim->track_get_path(i);
- Node *n = root->get_node(track_np);
- if (!n) {
- continue;
- }
- NodePath old_np = n->get_path();
- if (!ran.has(i))
- continue; //channel was removed
- for (List<Pair<NodePath, NodePath> >::Element *F = p_renames->front(); F; F = F->next()) {
- if (F->get().first == old_np) {
- if (F->get().second == NodePath()) {
- //will be erased
- int idx = 0;
- Set<int>::Element *EI = ran.front();
- ERR_FAIL_COND(!EI); //bug
- while (EI->get() != i) {
- idx++;
- EI = EI->next();
- ERR_FAIL_COND(!EI); //another bug
- }
- editor_data->get_undo_redo().add_do_method(anim.ptr(), "remove_track", idx);
- editor_data->get_undo_redo().add_undo_method(anim.ptr(), "add_track", anim->track_get_type(i), idx);
- editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", idx, track_np);
- editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_interpolation_type", idx, anim->track_get_interpolation_type(i));
- for (int j = 0; j < anim->track_get_key_count(i); j++) {
- 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));
- }
- ran.erase(i); //byebye channel
- } else {
- //will be renamed
- NodePath rel_path = new_root_path.rel_path_to(F->get().second);
- NodePath new_path = NodePath(rel_path.get_names(), track_np.get_subnames(), false);
- if (new_path == track_np)
- continue; //bleh
- editor_data->get_undo_redo().add_do_method(anim.ptr(), "track_set_path", i, new_path);
- editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", i, track_np);
- }
- }
- }
- }
- }
- }
- }
- }
- for (int i = 0; i < p_base->get_child_count(); i++)
- perform_node_renames(p_base->get_child(i), p_renames, r_rem_anims);
- }
- void SceneTreeDock::_node_prerenamed(Node *p_node, const String &p_new_name) {
- List<Pair<NodePath, NodePath> > path_renames;
- Vector<StringName> base_path;
- Node *n = p_node->get_parent();
- while (n) {
- base_path.push_back(n->get_name());
- n = n->get_parent();
- }
- base_path.invert();
- Vector<StringName> new_base_path = base_path;
- base_path.push_back(p_node->get_name());
- new_base_path.push_back(p_new_name);
- Pair<NodePath, NodePath> npp;
- npp.first = NodePath(base_path, true);
- npp.second = NodePath(new_base_path, true);
- path_renames.push_back(npp);
- for (int i = 0; i < p_node->get_child_count(); i++)
- _fill_path_renames(base_path, new_base_path, p_node->get_child(i), &path_renames);
- perform_node_renames(NULL, &path_renames);
- }
- bool SceneTreeDock::_validate_no_foreign() {
- List<Node *> selection = editor_selection->get_selected_node_list();
- for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
- if (E->get() != edited_scene && E->get()->get_owner() != edited_scene) {
- accept->set_text(TTR("Can't operate on nodes from a foreign scene!"));
- accept->popup_centered_minsize();
- return false;
- }
- // When edited_scene inherits from another one the root Node will be the parent Scene,
- // we don't want to consider that Node a foreign one otherwise we would not be able to
- // delete it.
- if (edited_scene->get_scene_inherited_state().is_valid() && edited_scene == E->get()) {
- continue;
- }
- 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) {
- accept->set_text(TTR("Can't operate on nodes the current scene inherits from!"));
- accept->popup_centered_minsize();
- return false;
- }
- }
- return true;
- }
- bool SceneTreeDock::_validate_no_instance() {
- List<Node *> selection = editor_selection->get_selected_node_list();
- for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
- if (E->get() != edited_scene && E->get()->get_filename() != "") {
- accept->set_text(TTR("This operation can't be done on instanced scenes."));
- accept->popup_centered();
- return false;
- }
- }
- return true;
- }
- void SceneTreeDock::_node_reparent(NodePath p_path, bool p_keep_global_xform) {
- Node *new_parent = scene_root->get_node(p_path);
- ERR_FAIL_COND(!new_parent);
- List<Node *> selection = editor_selection->get_selected_node_list();
- if (selection.empty())
- return; // Nothing to reparent.
- Vector<Node *> nodes;
- for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
- nodes.push_back(E->get());
- }
- _do_reparent(new_parent, -1, nodes, p_keep_global_xform);
- }
- void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, Vector<Node *> p_nodes, bool p_keep_global_xform) {
- Node *new_parent = p_new_parent;
- ERR_FAIL_COND(!new_parent);
- if (p_nodes.size() == 0)
- return; // Nothing to reparent.
- p_nodes.sort_custom<Node::Comparator>(); //Makes result reliable.
- bool no_change = true;
- for (int ni = 0; ni < p_nodes.size(); ni++) {
- if (p_nodes[ni] == p_new_parent)
- return; // Attempt to reparent to itself.
- if (p_nodes[ni]->get_parent() != p_new_parent || p_position_in_parent + ni != p_nodes[ni]->get_position_in_parent())
- no_change = false;
- }
- if (no_change)
- return; // Position and parent didn't change.
- Node *validate = new_parent;
- while (validate) {
- ERR_FAIL_COND_MSG(p_nodes.find(validate) != -1, "Selection changed at some point. Can't reparent.");
- validate = validate->get_parent();
- }
- // Sort by tree order, so re-adding is easy.
- p_nodes.sort_custom<Node::Comparator>();
- editor_data->get_undo_redo().create_action(TTR("Reparent Node"));
- List<Pair<NodePath, NodePath> > path_renames;
- Vector<StringName> former_names;
- int inc = 0;
- for (int ni = 0; ni < p_nodes.size(); ni++) {
- // No undo implemented for this yet.
- Node *node = p_nodes[ni];
- fill_path_renames(node, new_parent, &path_renames);
- former_names.push_back(node->get_name());
- List<Node *> owned;
- node->get_owned_by(node->get_owner(), &owned);
- Array owners;
- for (List<Node *>::Element *E = owned.front(); E; E = E->next()) {
- owners.push_back(E->get());
- }
- if (new_parent == node->get_parent() && node->get_index() < p_position_in_parent + ni)
- inc--; // If the child will generate a gap when moved, adjust.
- editor_data->get_undo_redo().add_do_method(node->get_parent(), "remove_child", node);
- editor_data->get_undo_redo().add_do_method(new_parent, "add_child", node);
- if (p_position_in_parent >= 0)
- editor_data->get_undo_redo().add_do_method(new_parent, "move_child", node, p_position_in_parent + inc);
- ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
- String old_name = former_names[ni];
- String new_name = new_parent->validate_child_name(node);
- // Name was modified, fix the path renames.
- if (old_name.casecmp_to(new_name) != 0) {
- // Fix the to name to have the new name.
- NodePath old_new_name = path_renames[ni].second;
- NodePath new_path;
- Vector<StringName> unfixed_new_names = old_new_name.get_names();
- Vector<StringName> fixed_new_names;
- // Get last name and replace with fixed new name.
- for (int a = 0; a < (unfixed_new_names.size() - 1); a++) {
- fixed_new_names.push_back(unfixed_new_names[a]);
- }
- fixed_new_names.push_back(new_name);
- NodePath fixed_node_path = NodePath(fixed_new_names, true);
- path_renames[ni].second = fixed_node_path;
- }
- editor_data->get_undo_redo().add_do_method(sed, "live_debug_reparent_node", edited_scene->get_path_to(node), edited_scene->get_path_to(new_parent), new_name, p_position_in_parent + inc);
- editor_data->get_undo_redo().add_undo_method(sed, "live_debug_reparent_node", NodePath(String(edited_scene->get_path_to(new_parent)).plus_file(new_name)), edited_scene->get_path_to(node->get_parent()), node->get_name(), node->get_index());
- if (p_keep_global_xform) {
- if (Object::cast_to<Node2D>(node))
- editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Node2D>(node)->get_global_transform());
- if (Object::cast_to<Spatial>(node))
- editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Spatial>(node)->get_global_transform());
- if (Object::cast_to<Control>(node))
- editor_data->get_undo_redo().add_do_method(node, "set_global_position", Object::cast_to<Control>(node)->get_global_position());
- }
- editor_data->get_undo_redo().add_do_method(this, "_set_owners", edited_scene, owners);
- if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == node)
- editor_data->get_undo_redo().add_do_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", node);
- editor_data->get_undo_redo().add_undo_method(new_parent, "remove_child", node);
- editor_data->get_undo_redo().add_undo_method(node, "set_name", former_names[ni]);
- inc++;
- }
- // Add and move in a second step (so old order is preserved).
- for (int ni = 0; ni < p_nodes.size(); ni++) {
- Node *node = p_nodes[ni];
- List<Node *> owned;
- node->get_owned_by(node->get_owner(), &owned);
- Array owners;
- for (List<Node *>::Element *E = owned.front(); E; E = E->next()) {
- owners.push_back(E->get());
- }
- int child_pos = node->get_position_in_parent();
- editor_data->get_undo_redo().add_undo_method(node->get_parent(), "add_child", node);
- editor_data->get_undo_redo().add_undo_method(node->get_parent(), "move_child", node, child_pos);
- editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
- if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == node)
- editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", node);
- if (p_keep_global_xform) {
- if (Object::cast_to<Node2D>(node))
- editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Node2D>(node)->get_transform());
- if (Object::cast_to<Spatial>(node))
- editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Spatial>(node)->get_transform());
- if (Object::cast_to<Control>(node))
- editor_data->get_undo_redo().add_undo_method(node, "set_position", Object::cast_to<Control>(node)->get_position());
- }
- }
- perform_node_renames(NULL, &path_renames);
- editor_data->get_undo_redo().commit_action();
- }
- bool SceneTreeDock::_is_collapsed_recursive(TreeItem *p_item) const {
- bool is_branch_collapsed = false;
- List<TreeItem *> needs_check;
- needs_check.push_back(p_item);
- while (!needs_check.empty()) {
- TreeItem *item = needs_check.back()->get();
- needs_check.pop_back();
- TreeItem *child = item->get_children();
- is_branch_collapsed = item->is_collapsed() && child;
- if (is_branch_collapsed) {
- break;
- }
- while (child) {
- needs_check.push_back(child);
- child = child->get_next();
- }
- }
- return is_branch_collapsed;
- }
- void SceneTreeDock::_set_collapsed_recursive(TreeItem *p_item, bool p_collapsed) {
- List<TreeItem *> to_collapse;
- to_collapse.push_back(p_item);
- while (!to_collapse.empty()) {
- TreeItem *item = to_collapse.back()->get();
- to_collapse.pop_back();
- item->set_collapsed(p_collapsed);
- TreeItem *child = item->get_children();
- while (child) {
- to_collapse.push_back(child);
- child = child->get_next();
- }
- }
- }
- void SceneTreeDock::_script_created(Ref<Script> p_script) {
- List<Node *> selected = editor_selection->get_selected_node_list();
- if (selected.empty())
- return;
- editor_data->get_undo_redo().create_action(TTR("Attach Script"));
- for (List<Node *>::Element *E = selected.front(); E; E = E->next()) {
- Ref<Script> existing = E->get()->get_script();
- editor_data->get_undo_redo().add_do_method(E->get(), "set_script", p_script.get_ref_ptr());
- editor_data->get_undo_redo().add_undo_method(E->get(), "set_script", existing);
- editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
- editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
- }
- editor_data->get_undo_redo().commit_action();
- editor->push_item(p_script.operator->());
- _update_script_button();
- }
- void SceneTreeDock::_script_creation_closed() {
- script_create_dialog->disconnect("script_created", this, "_script_created");
- }
- void SceneTreeDock::_toggle_editable_children_from_selection() {
- List<Node *> selection = editor_selection->get_selected_node_list();
- List<Node *>::Element *e = selection.front();
- if (e) {
- _toggle_editable_children(e->get());
- }
- }
- void SceneTreeDock::_toggle_placeholder_from_selection() {
- List<Node *> selection = editor_selection->get_selected_node_list();
- List<Node *>::Element *e = selection.front();
- if (e) {
- Node *node = e->get();
- if (node) {
- _toggle_editable_children(node);
- bool placeholder = node->get_scene_instance_load_placeholder();
- placeholder = !placeholder;
- node->set_scene_instance_load_placeholder(placeholder);
- scene_tree->update_tree();
- }
- }
- }
- void SceneTreeDock::_toggle_editable_children(Node *p_node) {
- if (p_node) {
- bool editable = !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node);
- EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(p_node, editable);
- if (editable)
- p_node->set_scene_instance_load_placeholder(false);
- SpatialEditor::get_singleton()->update_all_gizmos(p_node);
- scene_tree->update_tree();
- }
- }
- void SceneTreeDock::_delete_confirm(bool p_cut) {
- List<Node *> remove_list = editor_selection->get_selected_node_list();
- if (remove_list.empty())
- return;
- editor->get_editor_plugins_over()->make_visible(false);
- if (p_cut) {
- editor_data->get_undo_redo().create_action(TTR("Cut Node(s)"));
- } else {
- editor_data->get_undo_redo().create_action(TTR("Remove Node(s)"));
- }
- bool entire_scene = false;
- for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
- if (E->get() == edited_scene) {
- entire_scene = true;
- }
- }
- if (entire_scene) {
- editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", (Object *)NULL);
- editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", edited_scene);
- editor_data->get_undo_redo().add_undo_method(edited_scene, "set_owner", edited_scene->get_owner());
- editor_data->get_undo_redo().add_undo_method(scene_tree, "update_tree");
- editor_data->get_undo_redo().add_undo_reference(edited_scene);
- } else {
- remove_list.sort_custom<Node::Comparator>(); //sort nodes to keep positions
- List<Pair<NodePath, NodePath> > path_renames;
- //delete from animation
- for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
- Node *n = E->get();
- if (!n->is_inside_tree() || !n->get_parent())
- continue;
- fill_path_renames(n, NULL, &path_renames);
- }
- perform_node_renames(NULL, &path_renames);
- //delete for read
- for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
- Node *n = E->get();
- if (!n->is_inside_tree() || !n->get_parent())
- continue;
- List<Node *> owned;
- n->get_owned_by(n->get_owner(), &owned);
- Array owners;
- for (List<Node *>::Element *F = owned.front(); F; F = F->next()) {
- owners.push_back(F->get());
- }
- editor_data->get_undo_redo().add_do_method(n->get_parent(), "remove_child", n);
- editor_data->get_undo_redo().add_undo_method(n->get_parent(), "add_child", n);
- editor_data->get_undo_redo().add_undo_method(n->get_parent(), "move_child", n, n->get_index());
- if (AnimationPlayerEditor::singleton->get_track_editor()->get_root() == n)
- editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::singleton->get_track_editor(), "set_root", n);
- editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
- editor_data->get_undo_redo().add_undo_reference(n);
- ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
- editor_data->get_undo_redo().add_do_method(sed, "live_debug_remove_and_keep_node", edited_scene->get_path_to(n), n->get_instance_id());
- editor_data->get_undo_redo().add_undo_method(sed, "live_debug_restore_node", n->get_instance_id(), edited_scene->get_path_to(n->get_parent()), n->get_index());
- }
- }
- editor_data->get_undo_redo().commit_action();
- // hack, force 2d editor viewport to refresh after deletion
- if (CanvasItemEditor *editor = CanvasItemEditor::get_singleton())
- editor->get_viewport_control()->update();
- editor->push_item(NULL);
- // Fixes the EditorHistory from still offering deleted notes
- EditorHistory *editor_history = EditorNode::get_singleton()->get_editor_history();
- editor_history->cleanup_history();
- EditorNode::get_singleton()->get_inspector_dock()->call("_prepare_history");
- }
- void SceneTreeDock::_update_script_button() {
- if (!profile_allow_script_editing) {
- button_create_script->hide();
- button_detach_script->hide();
- } else if (EditorNode::get_singleton()->get_editor_selection()->get_selection().size() == 0) {
- button_create_script->hide();
- button_detach_script->hide();
- } else if (EditorNode::get_singleton()->get_editor_selection()->get_selection().size() == 1) {
- Node *n = EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list()[0];
- if (n->get_script().is_null()) {
- button_create_script->show();
- button_detach_script->hide();
- } else {
- button_create_script->hide();
- button_detach_script->show();
- }
- } else {
- button_create_script->hide();
- Array selection = editor_selection->get_selected_nodes();
- for (int i = 0; i < selection.size(); i++) {
- Node *n = Object::cast_to<Node>(selection[i]);
- if (!n->get_script().is_null()) {
- button_detach_script->show();
- return;
- }
- }
- button_detach_script->hide();
- }
- }
- void SceneTreeDock::_selection_changed() {
- int selection_size = EditorNode::get_singleton()->get_editor_selection()->get_selection().size();
- if (selection_size > 1) {
- //automatically turn on multi-edit
- _tool_selected(TOOL_MULTI_EDIT);
- } else if (selection_size == 0) {
- editor->push_item(NULL);
- }
- _update_script_button();
- }
- Node *SceneTreeDock::_get_selection_group_tail(Node *p_node, List<Node *> p_list) {
- Node *tail = p_node;
- Node *parent = tail->get_parent();
- for (int i = p_node->get_position_in_parent(); i < parent->get_child_count(); i++) {
- Node *sibling = parent->get_child(i);
- if (p_list.find(sibling))
- tail = sibling;
- else
- break;
- }
- return tail;
- }
- void SceneTreeDock::_do_create(Node *p_parent) {
- Variant c = create_dialog->instance_selected();
- ERR_FAIL_COND(!c);
- Node *child = Object::cast_to<Node>(c);
- ERR_FAIL_COND(!child);
- editor_data->get_undo_redo().create_action(TTR("Create Node"));
- if (edited_scene) {
- editor_data->get_undo_redo().add_do_method(p_parent, "add_child", child);
- editor_data->get_undo_redo().add_do_method(child, "set_owner", edited_scene);
- editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
- editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", child);
- editor_data->get_undo_redo().add_do_reference(child);
- editor_data->get_undo_redo().add_undo_method(p_parent, "remove_child", child);
- String new_name = p_parent->validate_child_name(child);
- ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
- editor_data->get_undo_redo().add_do_method(sed, "live_debug_create_node", edited_scene->get_path_to(p_parent), child->get_class(), new_name);
- editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(p_parent)).plus_file(new_name)));
- } else {
- editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", child);
- editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
- editor_data->get_undo_redo().add_do_reference(child);
- editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", (Object *)NULL);
- }
- editor_data->get_undo_redo().commit_action();
- editor->push_item(c);
- editor_selection->clear();
- editor_selection->add_node(child);
- if (Object::cast_to<Control>(c)) {
- //make editor more comfortable, so some controls don't appear super shrunk
- Control *ct = Object::cast_to<Control>(c);
- Size2 ms = ct->get_minimum_size();
- if (ms.width < 4)
- ms.width = 40;
- if (ms.height < 4)
- ms.height = 40;
- ct->set_size(ms);
- }
- }
- void SceneTreeDock::_create() {
- if (current_option == TOOL_NEW) {
- Node *parent = NULL;
- if (edited_scene) {
- // If root exists in edited scene
- parent = scene_tree->get_selected();
- if (!parent)
- parent = edited_scene;
- } else {
- // If no root exist in edited scene
- parent = scene_root;
- ERR_FAIL_COND(!parent);
- }
- _do_create(parent);
- } else if (current_option == TOOL_REPLACE) {
- List<Node *> selection = editor_selection->get_selected_node_list();
- ERR_FAIL_COND(selection.size() <= 0);
- UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
- ur->create_action(TTR("Change type of node(s)"));
- for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
- Node *n = E->get();
- ERR_FAIL_COND(!n);
- Variant c = create_dialog->instance_selected();
- ERR_FAIL_COND(!c);
- Node *newnode = Object::cast_to<Node>(c);
- ERR_FAIL_COND(!newnode);
- ur->add_do_method(this, "replace_node", n, newnode, true, false);
- ur->add_do_reference(newnode);
- ur->add_undo_method(this, "replace_node", newnode, n, false, false);
- ur->add_undo_reference(n);
- }
- ur->commit_action();
- } else if (current_option == TOOL_REPARENT_TO_NEW_NODE) {
- List<Node *> selection = editor_selection->get_selected_node_list();
- ERR_FAIL_COND(selection.size() <= 0);
- // Find top level node in selection
- bool only_one_top_node = true;
- Node *first = selection.front()->get();
- ERR_FAIL_COND(!first);
- int smaller_path_to_top = first->get_path_to(scene_root).get_name_count();
- Node *top_node = first;
- for (List<Node *>::Element *E = selection.front()->next(); E; E = E->next()) {
- Node *n = E->get();
- ERR_FAIL_COND(!n);
- int path_length = n->get_path_to(scene_root).get_name_count();
- if (top_node != n) {
- if (smaller_path_to_top > path_length) {
- top_node = n;
- smaller_path_to_top = path_length;
- only_one_top_node = true;
- } else if (smaller_path_to_top == path_length) {
- if (only_one_top_node && top_node->get_parent() != n->get_parent())
- only_one_top_node = false;
- }
- }
- }
- Node *parent = NULL;
- if (only_one_top_node)
- parent = top_node->get_parent();
- else
- parent = top_node->get_parent()->get_parent();
- _do_create(parent);
- Vector<Node *> nodes;
- for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
- nodes.push_back(E->get());
- }
- // This works because editor_selection was cleared and populated with last created node in _do_create()
- Node *last_created = editor_selection->get_selected_node_list().front()->get();
- _do_reparent(last_created, -1, nodes, true);
- }
- scene_tree->get_scene_tree()->call_deferred("grab_focus");
- }
- void SceneTreeDock::replace_node(Node *p_node, Node *p_by_node, bool p_keep_properties, bool p_remove_old) {
- Node *n = p_node;
- Node *newnode = p_by_node;
- if (p_keep_properties) {
- Node *default_oldnode = Object::cast_to<Node>(ClassDB::instance(n->get_class()));
- List<PropertyInfo> pinfo;
- n->get_property_list(&pinfo);
- for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
- continue;
- if (E->get().name == "__meta__") {
- Dictionary metadata = n->get(E->get().name);
- if (metadata.has("_editor_description_")) {
- newnode->set_meta("_editor_description_", metadata["_editor_description_"]);
- }
- if (Object::cast_to<CanvasItem>(newnode) || Object::cast_to<Spatial>(newnode)) {
- if (metadata.has("_edit_group_") && metadata["_edit_group_"]) {
- newnode->set_meta("_edit_group_", true);
- }
- if (metadata.has("_edit_lock_") && metadata["_edit_lock_"]) {
- newnode->set_meta("_edit_lock_", true);
- }
- }
- continue;
- }
- if (default_oldnode->get(E->get().name) != n->get(E->get().name)) {
- newnode->set(E->get().name, n->get(E->get().name));
- }
- }
- memdelete(default_oldnode);
- }
- editor->push_item(NULL);
- //reconnect signals
- List<MethodInfo> sl;
- n->get_signal_list(&sl);
- for (List<MethodInfo>::Element *E = sl.front(); E; E = E->next()) {
- List<Object::Connection> cl;
- n->get_signal_connection_list(E->get().name, &cl);
- for (List<Object::Connection>::Element *F = cl.front(); F; F = F->next()) {
- Object::Connection &c = F->get();
- if (!(c.flags & Object::CONNECT_PERSIST))
- continue;
- newnode->connect(c.signal, c.target, c.method, c.binds, Object::CONNECT_PERSIST);
- }
- }
- String newname = n->get_name();
- List<Node *> to_erase;
- for (int i = 0; i < n->get_child_count(); i++) {
- if (n->get_child(i)->get_owner() == NULL && n->is_owned_by_parent()) {
- to_erase.push_back(n->get_child(i));
- }
- }
- n->replace_by(newnode, true);
- if (n == edited_scene) {
- edited_scene = newnode;
- editor->set_edited_scene(newnode);
- }
- //small hack to make collisionshapes and other kind of nodes to work
- for (int i = 0; i < newnode->get_child_count(); i++) {
- Node *c = newnode->get_child(i);
- c->call("set_transform", c->call("get_transform"));
- }
- //p_remove_old was added to support undo
- if (p_remove_old)
- editor_data->get_undo_redo().clear_history();
- newnode->set_name(newname);
- editor->push_item(newnode);
- if (p_remove_old) {
- memdelete(n);
- while (to_erase.front()) {
- memdelete(to_erase.front()->get());
- to_erase.pop_front();
- }
- }
- }
- void SceneTreeDock::set_edited_scene(Node *p_scene) {
- edited_scene = p_scene;
- }
- void SceneTreeDock::set_selected(Node *p_node, bool p_emit_selected) {
- scene_tree->set_selected(p_node, p_emit_selected);
- }
- void SceneTreeDock::import_subscene() {
- import_subscene_dialog->popup_centered_clamped(Size2(500, 800) * EDSCALE, 0.8);
- }
- void SceneTreeDock::_import_subscene() {
- Node *parent = scene_tree->get_selected();
- if (!parent) {
- parent = editor_data->get_edited_scene_root();
- ERR_FAIL_COND(!parent);
- }
- import_subscene_dialog->move(parent, edited_scene);
- editor_data->get_undo_redo().clear_history(); //no undo for now..
- }
- void SceneTreeDock::_new_scene_from(String p_file) {
- List<Node *> selection = editor_selection->get_selected_node_list();
- if (selection.size() != 1) {
- accept->set_text(TTR("This operation requires a single selected node."));
- accept->popup_centered_minsize();
- return;
- }
- if (EditorNode::get_singleton()->is_scene_open(p_file)) {
- accept->set_text(TTR("Can't overwrite scene that is still open!"));
- accept->popup_centered_minsize();
- return;
- }
- Node *base = selection.front()->get();
- Map<Node *, Node *> reown;
- reown[editor_data->get_edited_scene_root()] = base;
- Node *copy = base->duplicate_and_reown(reown);
- if (copy) {
- Ref<PackedScene> sdata = memnew(PackedScene);
- Error err = sdata->pack(copy);
- memdelete(copy);
- if (err != OK) {
- accept->set_text(TTR("Couldn't save new scene. Likely dependencies (instances) couldn't be satisfied."));
- accept->popup_centered_minsize();
- return;
- }
- int flg = 0;
- if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources"))
- flg |= ResourceSaver::FLAG_COMPRESS;
- err = ResourceSaver::save(p_file, sdata, flg);
- if (err != OK) {
- accept->set_text(TTR("Error saving scene."));
- accept->popup_centered_minsize();
- return;
- }
- _replace_with_branch_scene(p_file, base);
- } else {
- accept->set_text(TTR("Error duplicating scene to save it."));
- accept->popup_centered_minsize();
- return;
- }
- }
- static bool _is_node_visible(Node *p_node) {
- if (!p_node->get_owner())
- return false;
- if (p_node->get_owner() != EditorNode::get_singleton()->get_edited_scene() && !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node->get_owner()))
- return false;
- return true;
- }
- static bool _has_visible_children(Node *p_node) {
- bool collapsed = p_node->is_displayed_folded();
- if (collapsed)
- return false;
- for (int i = 0; i < p_node->get_child_count(); i++) {
- Node *child = p_node->get_child(i);
- if (!_is_node_visible(child))
- continue;
- return true;
- }
- return false;
- }
- void SceneTreeDock::_normalize_drop(Node *&to_node, int &to_pos, int p_type) {
- to_pos = -1;
- if (p_type == -1) {
- //drop at above selected node
- if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
- to_node = NULL;
- ERR_FAIL_MSG("Cannot perform drop above the root node!");
- }
- to_pos = to_node->get_index();
- to_node = to_node->get_parent();
- } else if (p_type == 1) {
- //drop at below selected node
- if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
- //if at lower sibling of root node
- to_pos = 0; //just insert at beginning of root node
- return;
- }
- Node *lower_sibling = NULL;
- if (_has_visible_children(to_node)) {
- to_pos = 0;
- } else {
- for (int i = to_node->get_index() + 1; i < to_node->get_parent()->get_child_count(); i++) {
- Node *c = to_node->get_parent()->get_child(i);
- if (_is_node_visible(c)) {
- lower_sibling = c;
- break;
- }
- }
- if (lower_sibling) {
- to_pos = lower_sibling->get_index();
- }
- to_node = to_node->get_parent();
- }
- }
- }
- void SceneTreeDock::_files_dropped(Vector<String> p_files, NodePath p_to, int p_type) {
- Node *node = get_node(p_to);
- ERR_FAIL_COND(!node);
- int to_pos = -1;
- _normalize_drop(node, to_pos, p_type);
- _perform_instance_scenes(p_files, node, to_pos);
- }
- void SceneTreeDock::_script_dropped(String p_file, NodePath p_to) {
- Ref<Script> scr = ResourceLoader::load(p_file);
- ERR_FAIL_COND(!scr.is_valid());
- Node *n = get_node(p_to);
- if (n) {
- editor_data->get_undo_redo().create_action(TTR("Attach Script"));
- editor_data->get_undo_redo().add_do_method(n, "set_script", scr);
- editor_data->get_undo_redo().add_undo_method(n, "set_script", n->get_script());
- editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
- editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
- editor_data->get_undo_redo().commit_action();
- }
- }
- void SceneTreeDock::_nodes_dragged(Array p_nodes, NodePath p_to, int p_type) {
- List<Node *> selection = editor_selection->get_selected_node_list();
- if (selection.empty())
- return; //nothing to reparent
- Node *to_node = get_node(p_to);
- if (!to_node)
- return;
- Vector<Node *> nodes;
- for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
- nodes.push_back(E->get());
- }
- int to_pos = -1;
- _normalize_drop(to_node, to_pos, p_type);
- _do_reparent(to_node, to_pos, nodes, !Input::get_singleton()->is_key_pressed(KEY_SHIFT));
- }
- void SceneTreeDock::_add_children_to_popup(Object *p_obj, int p_depth) {
- if (p_depth > 8)
- return;
- List<PropertyInfo> pinfo;
- p_obj->get_property_list(&pinfo);
- for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
- if (!(E->get().usage & PROPERTY_USAGE_EDITOR))
- continue;
- if (E->get().hint != PROPERTY_HINT_RESOURCE_TYPE)
- continue;
- Variant value = p_obj->get(E->get().name);
- if (value.get_type() != Variant::OBJECT)
- continue;
- Object *obj = value;
- if (!obj)
- continue;
- Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
- if (menu->get_item_count() == 0) {
- menu->add_submenu_item(TTR("Sub-Resources"), "Sub-Resources");
- }
- int index = menu_subresources->get_item_count();
- menu_subresources->add_icon_item(icon, E->get().name.capitalize(), EDIT_SUBRESOURCE_BASE + subresources.size());
- menu_subresources->set_item_h_offset(index, p_depth * 10 * EDSCALE);
- subresources.push_back(obj->get_instance_id());
- _add_children_to_popup(obj, p_depth + 1);
- }
- }
- void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
- if (!EditorNode::get_singleton()->get_edited_scene()) {
- menu->clear();
- if (profile_allow_editing) {
- menu->add_icon_shortcut(get_icon("Add", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
- menu->add_icon_shortcut(get_icon("Instance", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANCE);
- }
- menu->set_size(Size2(1, 1));
- menu->set_position(p_menu_pos);
- menu->popup();
- return;
- }
- List<Node *> selection = editor_selection->get_selected_node_list();
- List<Node *> full_selection = editor_selection->get_full_selected_node_list(); // Above method only returns nodes with common parent.
- if (selection.size() == 0)
- return;
- menu->clear();
- Ref<Script> existing_script;
- bool exisiting_script_removable = true;
- if (selection.size() == 1) {
- Node *selected = selection[0];
- if (profile_allow_editing) {
- subresources.clear();
- menu_subresources->clear();
- menu_subresources->set_size(Size2(1, 1));
- _add_children_to_popup(selection.front()->get(), 0);
- if (menu->get_item_count() > 0)
- menu->add_separator();
- menu->add_icon_shortcut(get_icon("Add", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
- menu->add_icon_shortcut(get_icon("Instance", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANCE);
- }
- menu->add_icon_shortcut(get_icon("Collapse", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/expand_collapse_all"), TOOL_EXPAND_COLLAPSE);
- menu->add_separator();
- existing_script = selected->get_script();
- if (EditorNode::get_singleton()->get_object_custom_type_base(selected) == existing_script) {
- exisiting_script_removable = false;
- }
- }
- if (profile_allow_editing) {
- menu->add_shortcut(ED_GET_SHORTCUT("scene_tree/cut_node"), TOOL_CUT);
- menu->add_shortcut(ED_GET_SHORTCUT("scene_tree/copy_node"), TOOL_COPY);
- if (selection.size() == 1 && !node_clipboard.empty()) {
- menu->add_shortcut(ED_GET_SHORTCUT("scene_tree/paste_node"), TOOL_PASTE);
- }
- menu->add_separator();
- }
- if (profile_allow_script_editing) {
- bool add_separator = false;
- if (full_selection.size() == 1) {
- add_separator = true;
- menu->add_icon_shortcut(get_icon("ScriptCreate", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/attach_script"), TOOL_ATTACH_SCRIPT);
- if (existing_script.is_valid()) {
- menu->add_icon_shortcut(get_icon("ScriptExtend", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/extend_script"), TOOL_EXTEND_SCRIPT);
- }
- }
- if (existing_script.is_valid() && exisiting_script_removable) {
- add_separator = true;
- menu->add_icon_shortcut(get_icon("ScriptRemove", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/detach_script"), TOOL_DETACH_SCRIPT);
- } else if (full_selection.size() > 1) {
- bool script_exists = false;
- for (List<Node *>::Element *E = full_selection.front(); E; E = E->next()) {
- if (!E->get()->get_script().is_null()) {
- script_exists = true;
- break;
- }
- }
- if (script_exists) {
- add_separator = true;
- menu->add_icon_shortcut(get_icon("ScriptRemove", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/detach_script"), TOOL_DETACH_SCRIPT);
- }
- }
- if (add_separator && profile_allow_editing) {
- menu->add_separator();
- }
- }
- if (profile_allow_editing) {
- if (full_selection.size() == 1) {
- menu->add_icon_shortcut(get_icon("Rename", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/rename"), TOOL_RENAME);
- }
- bool can_replace = true;
- for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
- if (E->get() != edited_scene && (E->get()->get_owner() != edited_scene || E->get()->get_filename() != "")) {
- can_replace = false;
- break;
- }
- }
- if (can_replace) {
- menu->add_icon_shortcut(get_icon("Reload", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/change_node_type"), TOOL_REPLACE);
- }
- if (scene_tree->get_selected() != edited_scene) {
- menu->add_separator();
- menu->add_icon_shortcut(get_icon("MoveUp", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/move_up"), TOOL_MOVE_UP);
- menu->add_icon_shortcut(get_icon("MoveDown", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/move_down"), TOOL_MOVE_DOWN);
- menu->add_icon_shortcut(get_icon("Duplicate", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/duplicate"), TOOL_DUPLICATE);
- menu->add_icon_shortcut(get_icon("Reparent", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/reparent"), TOOL_REPARENT);
- menu->add_icon_shortcut(get_icon("ReparentToNewNode", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/reparent_to_new_node"), TOOL_REPARENT_TO_NEW_NODE);
- if (selection.size() == 1) {
- menu->add_icon_shortcut(get_icon("NewRoot", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/make_root"), TOOL_MAKE_ROOT);
- }
- }
- }
- if (selection.size() == 1) {
- if (profile_allow_editing) {
- menu->add_separator();
- menu->add_icon_shortcut(get_icon("Blend", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/merge_from_scene"), TOOL_MERGE_FROM_SCENE);
- menu->add_icon_shortcut(get_icon("CreateNewSceneFrom", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/save_branch_as_scene"), TOOL_NEW_SCENE_FROM);
- }
- if (full_selection.size() == 1) {
- menu->add_separator();
- menu->add_icon_shortcut(get_icon("CopyNodePath", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/copy_node_path"), TOOL_COPY_NODE_PATH);
- }
- bool is_external = (selection[0]->get_filename() != "");
- if (is_external) {
- bool is_inherited = selection[0]->get_scene_inherited_state() != NULL;
- bool is_top_level = selection[0]->get_owner() == NULL;
- if (is_inherited && is_top_level) {
- menu->add_separator();
- if (profile_allow_editing) {
- menu->add_item(TTR("Clear Inheritance"), TOOL_SCENE_CLEAR_INHERITANCE);
- }
- menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Open in Editor"), TOOL_SCENE_OPEN_INHERITED);
- } else if (!is_top_level) {
- menu->add_separator();
- bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(selection[0]);
- bool placeholder = selection[0]->get_scene_instance_load_placeholder();
- if (profile_allow_editing) {
- menu->add_check_item(TTR("Editable Children"), TOOL_SCENE_EDITABLE_CHILDREN);
- menu->add_check_item(TTR("Load As Placeholder"), TOOL_SCENE_USE_PLACEHOLDER);
- menu->add_item(TTR("Make Local"), TOOL_SCENE_MAKE_LOCAL);
- }
- menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Open in Editor"), TOOL_SCENE_OPEN);
- if (profile_allow_editing) {
- menu->set_item_checked(menu->get_item_idx_from_text(TTR("Editable Children")), editable);
- menu->set_item_checked(menu->get_item_idx_from_text(TTR("Load As Placeholder")), placeholder);
- }
- }
- }
- }
- if (profile_allow_editing && selection.size() > 1) {
- //this is not a commonly used action, it makes no sense for it to be where it was nor always present.
- menu->add_separator();
- menu->add_icon_shortcut(get_icon("Rename", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/batch_rename"), TOOL_BATCH_RENAME);
- }
- menu->add_separator();
- menu->add_icon_item(get_icon("Help", "EditorIcons"), TTR("Open Documentation"), TOOL_OPEN_DOCUMENTATION);
- if (profile_allow_editing) {
- menu->add_separator();
- menu->add_icon_shortcut(get_icon("Remove", "EditorIcons"), ED_SHORTCUT("scene_tree/delete", TTR("Delete Node(s)"), KEY_DELETE), TOOL_ERASE);
- }
- menu->set_size(Size2(1, 1));
- menu->set_position(p_menu_pos);
- menu->popup();
- }
- void SceneTreeDock::_filter_changed(const String &p_filter) {
- scene_tree->set_filter(p_filter);
- }
- String SceneTreeDock::get_filter() {
- return filter->get_text();
- }
- void SceneTreeDock::set_filter(const String &p_filter) {
- filter->set_text(p_filter);
- scene_tree->set_filter(p_filter);
- }
- void SceneTreeDock::_focus_node() {
- Node *node = scene_tree->get_selected();
- ERR_FAIL_COND(!node);
- if (node->is_class("CanvasItem")) {
- CanvasItemEditorPlugin *editor = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
- editor->get_canvas_item_editor()->focus_selection();
- } else {
- SpatialEditorPlugin *editor = Object::cast_to<SpatialEditorPlugin>(editor_data->get_editor("3D"));
- editor->get_spatial_editor()->get_editor_viewport(0)->focus_selection();
- }
- }
- void SceneTreeDock::attach_script_to_selected(bool p_extend) {
- if (ScriptServer::get_language_count() == 0) {
- 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."));
- return;
- }
- if (!profile_allow_script_editing) {
- return;
- }
- List<Node *> selection = editor_selection->get_selected_node_list();
- if (selection.empty())
- return;
- Node *selected = scene_tree->get_selected();
- if (!selected)
- selected = selection.front()->get();
- Ref<Script> existing = selected->get_script();
- String path = selected->get_filename();
- if (path == "") {
- String root_path = editor_data->get_edited_scene_root()->get_filename();
- if (root_path == "") {
- path = String("res://").plus_file(selected->get_name());
- } else {
- path = root_path.get_base_dir().plus_file(selected->get_name());
- }
- }
- String inherits = selected->get_class();
- if (p_extend && existing.is_valid()) {
- for (int i = 0; i < ScriptServer::get_language_count(); i++) {
- ScriptLanguage *l = ScriptServer::get_language(i);
- if (l->get_type() == existing->get_class()) {
- String name = l->get_global_class_name(existing->get_path());
- if (ScriptServer::is_global_class(name) && EDITOR_GET("interface/editors/derive_script_globals_by_name").operator bool()) {
- inherits = name;
- } else if (l->can_inherit_from_file()) {
- inherits = "\"" + existing->get_path() + "\"";
- }
- break;
- }
- }
- }
- script_create_dialog->connect("script_created", this, "_script_created");
- script_create_dialog->connect("popup_hide", this, "_script_creation_closed", varray(), CONNECT_ONESHOT);
- script_create_dialog->set_inheritance_base_type("Node");
- script_create_dialog->config(inherits, path);
- script_create_dialog->popup_centered();
- }
- void SceneTreeDock::open_script_dialog(Node *p_for_node, bool p_extend) {
- scene_tree->set_selected(p_for_node, false);
- if (p_extend) {
- _tool_selected(TOOL_EXTEND_SCRIPT);
- } else {
- _tool_selected(TOOL_ATTACH_SCRIPT);
- }
- }
- void SceneTreeDock::add_remote_tree_editor(Control *p_remote) {
- ERR_FAIL_COND(remote_tree != NULL);
- add_child(p_remote);
- remote_tree = p_remote;
- remote_tree->hide();
- }
- void SceneTreeDock::show_remote_tree() {
- _remote_tree_selected();
- }
- void SceneTreeDock::hide_remote_tree() {
- _local_tree_selected();
- }
- void SceneTreeDock::show_tab_buttons() {
- button_hb->show();
- }
- void SceneTreeDock::hide_tab_buttons() {
- button_hb->hide();
- }
- void SceneTreeDock::_remote_tree_selected() {
- scene_tree->hide();
- create_root_dialog->hide();
- if (remote_tree)
- remote_tree->show();
- edit_remote->set_pressed(true);
- edit_local->set_pressed(false);
- emit_signal("remote_tree_selected");
- }
- void SceneTreeDock::_local_tree_selected() {
- if (!bool(EDITOR_GET("interface/editors/show_scene_tree_root_selection")) || get_tree()->get_edited_scene_root() != nullptr) {
- scene_tree->show();
- }
- if (remote_tree)
- remote_tree->hide();
- edit_remote->set_pressed(false);
- edit_local->set_pressed(true);
- }
- void SceneTreeDock::_update_create_root_dialog() {
- BaseButton *toggle = Object::cast_to<BaseButton>(create_root_dialog->get_node(String("NodeShortcutsTopRow/NodeShortcutsToggle")));
- Node *node_shortcuts = create_root_dialog->get_node(String("NodeShortcuts"));
- if (!toggle || !node_shortcuts)
- return;
- Control *beginner_nodes = Object::cast_to<Control>(node_shortcuts->get_node(String("BeginnerNodeShortcuts")));
- Control *favorite_nodes = Object::cast_to<Control>(node_shortcuts->get_node(String("FavoriteNodeShortcuts")));
- if (!beginner_nodes || !favorite_nodes)
- return;
- EditorSettings::get_singleton()->set_setting("_use_favorites_root_selection", toggle->is_pressed());
- EditorSettings::get_singleton()->save();
- if (toggle->is_pressed()) {
- for (int i = 0; i < favorite_nodes->get_child_count(); i++) {
- favorite_nodes->get_child(i)->queue_delete();
- }
- FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("favorites.Node"), FileAccess::READ);
- if (f) {
- while (!f->eof_reached()) {
- String l = f->get_line().strip_edges();
- if (l != String()) {
- Button *button = memnew(Button);
- favorite_nodes->add_child(button);
- button->set_text(TTR(l));
- String name = l.get_slicec(' ', 0);
- if (ScriptServer::is_global_class(name))
- name = ScriptServer::get_global_class_native_base(name);
- button->set_icon(EditorNode::get_singleton()->get_class_icon(name));
- button->connect("pressed", this, "_favorite_root_selected", make_binds(l));
- }
- }
- memdelete(f);
- }
- if (!favorite_nodes->is_visible_in_tree()) {
- favorite_nodes->show();
- beginner_nodes->hide();
- }
- } else {
- if (!beginner_nodes->is_visible_in_tree()) {
- beginner_nodes->show();
- favorite_nodes->hide();
- }
- }
- }
- void SceneTreeDock::_favorite_root_selected(const String &p_class) {
- selected_favorite_root = p_class;
- _tool_selected(TOOL_CREATE_FAVORITE, false);
- }
- void SceneTreeDock::_feature_profile_changed() {
- Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
- if (profile.is_valid()) {
- profile_allow_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCENE_TREE);
- profile_allow_script_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCRIPT);
- bool profile_allow_3d = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D);
- button_3d->set_visible(profile_allow_3d);
- button_add->set_visible(profile_allow_editing);
- button_instance->set_visible(profile_allow_editing);
- scene_tree->set_can_rename(profile_allow_editing);
- } else {
- button_3d->set_visible(true);
- button_add->set_visible(true);
- button_instance->set_visible(true);
- scene_tree->set_can_rename(true);
- profile_allow_editing = true;
- profile_allow_script_editing = true;
- }
- _update_script_button();
- }
- void SceneTreeDock::_clear_clipboard() {
- for (List<Node *>::Element *E = node_clipboard.front(); E; E = E->next()) {
- memdelete(E->get());
- }
- node_clipboard.clear();
- clipboard_resource_remap.clear();
- }
- void SceneTreeDock::_create_remap_for_node(Node *p_node, Map<RES, RES> &r_remap) {
- List<PropertyInfo> props;
- p_node->get_property_list(&props);
- for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
- continue;
- }
- Variant v = p_node->get(E->get().name);
- if (v.is_ref()) {
- RES res = v;
- if (res.is_valid()) {
- if ((res->get_path() == "" || res->get_path().find("::") > -1) && !r_remap.has(res)) {
- _create_remap_for_resource(res, r_remap);
- }
- }
- }
- }
- for (int i = 0; i < p_node->get_child_count(); i++) {
- _create_remap_for_node(p_node->get_child(i), r_remap);
- }
- }
- void SceneTreeDock::_create_remap_for_resource(RES p_resource, Map<RES, RES> &r_remap) {
- r_remap[p_resource] = p_resource->duplicate();
- List<PropertyInfo> props;
- p_resource->get_property_list(&props);
- for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
- continue;
- }
- Variant v = p_resource->get(E->get().name);
- if (v.is_ref()) {
- RES res = v;
- if (res.is_valid()) {
- if ((res->get_path() == "" || res->get_path().find("::") > -1) && !r_remap.has(res)) {
- _create_remap_for_resource(res, r_remap);
- }
- }
- }
- }
- }
- void SceneTreeDock::_bind_methods() {
- ClassDB::bind_method(D_METHOD("_tool_selected"), &SceneTreeDock::_tool_selected, DEFVAL(false));
- ClassDB::bind_method(D_METHOD("_create"), &SceneTreeDock::_create);
- ClassDB::bind_method(D_METHOD("_node_reparent"), &SceneTreeDock::_node_reparent);
- ClassDB::bind_method(D_METHOD("_set_owners"), &SceneTreeDock::_set_owners);
- ClassDB::bind_method(D_METHOD("_node_selected"), &SceneTreeDock::_node_selected);
- ClassDB::bind_method(D_METHOD("_node_renamed"), &SceneTreeDock::_node_renamed);
- ClassDB::bind_method(D_METHOD("_script_created"), &SceneTreeDock::_script_created);
- ClassDB::bind_method(D_METHOD("_script_creation_closed"), &SceneTreeDock::_script_creation_closed);
- ClassDB::bind_method(D_METHOD("_load_request"), &SceneTreeDock::_load_request);
- ClassDB::bind_method(D_METHOD("_script_open_request"), &SceneTreeDock::_script_open_request);
- ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &SceneTreeDock::_unhandled_key_input);
- ClassDB::bind_method(D_METHOD("_input"), &SceneTreeDock::_input);
- ClassDB::bind_method(D_METHOD("_nodes_drag_begin"), &SceneTreeDock::_nodes_drag_begin);
- ClassDB::bind_method(D_METHOD("_delete_confirm"), &SceneTreeDock::_delete_confirm);
- ClassDB::bind_method(D_METHOD("_toggle_editable_children_from_selection"), &SceneTreeDock::_toggle_editable_children_from_selection);
- ClassDB::bind_method(D_METHOD("_toggle_placeholder_from_selection"), &SceneTreeDock::_toggle_placeholder_from_selection);
- ClassDB::bind_method(D_METHOD("_node_prerenamed"), &SceneTreeDock::_node_prerenamed);
- ClassDB::bind_method(D_METHOD("_import_subscene"), &SceneTreeDock::_import_subscene);
- ClassDB::bind_method(D_METHOD("_selection_changed"), &SceneTreeDock::_selection_changed);
- ClassDB::bind_method(D_METHOD("_node_collapsed"), &SceneTreeDock::_node_collapsed);
- ClassDB::bind_method(D_METHOD("_new_scene_from"), &SceneTreeDock::_new_scene_from);
- ClassDB::bind_method(D_METHOD("_nodes_dragged"), &SceneTreeDock::_nodes_dragged);
- ClassDB::bind_method(D_METHOD("_files_dropped"), &SceneTreeDock::_files_dropped);
- ClassDB::bind_method(D_METHOD("_quick_open"), &SceneTreeDock::_quick_open);
- ClassDB::bind_method(D_METHOD("_script_dropped"), &SceneTreeDock::_script_dropped);
- ClassDB::bind_method(D_METHOD("_tree_rmb"), &SceneTreeDock::_tree_rmb);
- ClassDB::bind_method(D_METHOD("_filter_changed"), &SceneTreeDock::_filter_changed);
- ClassDB::bind_method(D_METHOD("_focus_node"), &SceneTreeDock::_focus_node);
- ClassDB::bind_method(D_METHOD("_remote_tree_selected"), &SceneTreeDock::_remote_tree_selected);
- ClassDB::bind_method(D_METHOD("_local_tree_selected"), &SceneTreeDock::_local_tree_selected);
- ClassDB::bind_method(D_METHOD("_update_script_button"), &SceneTreeDock::_update_script_button);
- ClassDB::bind_method(D_METHOD("_favorite_root_selected"), &SceneTreeDock::_favorite_root_selected);
- ClassDB::bind_method(D_METHOD("_update_create_root_dialog"), &SceneTreeDock::_update_create_root_dialog);
- ClassDB::bind_method(D_METHOD("_feature_profile_changed"), &SceneTreeDock::_feature_profile_changed);
- ClassDB::bind_method(D_METHOD("instance"), &SceneTreeDock::instance);
- ClassDB::bind_method(D_METHOD("get_tree_editor"), &SceneTreeDock::get_tree_editor);
- ClassDB::bind_method(D_METHOD("replace_node"), &SceneTreeDock::replace_node);
- ADD_SIGNAL(MethodInfo("remote_tree_selected"));
- }
- SceneTreeDock::SceneTreeDock(EditorNode *p_editor, Node *p_scene_root, EditorSelection *p_editor_selection, EditorData &p_editor_data) {
- set_name("Scene");
- editor = p_editor;
- edited_scene = NULL;
- editor_data = &p_editor_data;
- editor_selection = p_editor_selection;
- scene_root = p_scene_root;
- VBoxContainer *vbc = this;
- HBoxContainer *filter_hbc = memnew(HBoxContainer);
- filter_hbc->add_constant_override("separate", 0);
- ED_SHORTCUT("scene_tree/rename", TTR("Rename"), KEY_F2);
- ED_SHORTCUT("scene_tree/batch_rename", TTR("Batch Rename"), KEY_MASK_SHIFT | KEY_F2);
- ED_SHORTCUT("scene_tree/add_child_node", TTR("Add Child Node"), KEY_MASK_CMD | KEY_A);
- ED_SHORTCUT("scene_tree/instance_scene", TTR("Instance Child Scene"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_A);
- ED_SHORTCUT("scene_tree/expand_collapse_all", TTR("Expand/Collapse All"));
- ED_SHORTCUT("scene_tree/cut_node", TTR("Cut"), KEY_MASK_CMD | KEY_X);
- ED_SHORTCUT("scene_tree/copy_node", TTR("Copy"), KEY_MASK_CMD | KEY_C);
- ED_SHORTCUT("scene_tree/paste_node", TTR("Paste"), KEY_MASK_CMD | KEY_V);
- ED_SHORTCUT("scene_tree/change_node_type", TTR("Change Type"));
- ED_SHORTCUT("scene_tree/attach_script", TTR("Attach Script"));
- ED_SHORTCUT("scene_tree/extend_script", TTR("Extend Script"));
- ED_SHORTCUT("scene_tree/detach_script", TTR("Detach Script"));
- ED_SHORTCUT("scene_tree/move_up", TTR("Move Up"), KEY_MASK_CMD | KEY_UP);
- ED_SHORTCUT("scene_tree/move_down", TTR("Move Down"), KEY_MASK_CMD | KEY_DOWN);
- ED_SHORTCUT("scene_tree/duplicate", TTR("Duplicate"), KEY_MASK_CMD | KEY_D);
- ED_SHORTCUT("scene_tree/reparent", TTR("Reparent"));
- ED_SHORTCUT("scene_tree/reparent_to_new_node", TTR("Reparent to New Node"));
- ED_SHORTCUT("scene_tree/make_root", TTR("Make Scene Root"));
- ED_SHORTCUT("scene_tree/merge_from_scene", TTR("Merge From Scene"));
- ED_SHORTCUT("scene_tree/save_branch_as_scene", TTR("Save Branch as Scene"));
- ED_SHORTCUT("scene_tree/copy_node_path", TTR("Copy Node Path"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_C);
- ED_SHORTCUT("scene_tree/delete_no_confirm", TTR("Delete (No Confirm)"), KEY_MASK_SHIFT | KEY_DELETE);
- ED_SHORTCUT("scene_tree/delete", TTR("Delete"), KEY_DELETE);
- button_add = memnew(ToolButton);
- button_add->connect("pressed", this, "_tool_selected", make_binds(TOOL_NEW, false));
- button_add->set_tooltip(TTR("Add/Create a New Node."));
- button_add->set_shortcut(ED_GET_SHORTCUT("scene_tree/add_child_node"));
- filter_hbc->add_child(button_add);
- button_instance = memnew(ToolButton);
- button_instance->connect("pressed", this, "_tool_selected", make_binds(TOOL_INSTANCE, false));
- button_instance->set_tooltip(TTR("Instance a scene file as a Node. Creates an inherited scene if no root node exists."));
- button_instance->set_shortcut(ED_GET_SHORTCUT("scene_tree/instance_scene"));
- filter_hbc->add_child(button_instance);
- vbc->add_child(filter_hbc);
- filter = memnew(LineEdit);
- filter->set_h_size_flags(SIZE_EXPAND_FILL);
- filter->set_placeholder(TTR("Filter nodes"));
- filter_hbc->add_child(filter);
- filter->add_constant_override("minimum_spaces", 0);
- filter->connect("text_changed", this, "_filter_changed");
- button_create_script = memnew(ToolButton);
- button_create_script->connect("pressed", this, "_tool_selected", make_binds(TOOL_ATTACH_SCRIPT, false));
- button_create_script->set_tooltip(TTR("Attach a new or existing script to the selected node."));
- button_create_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/attach_script"));
- filter_hbc->add_child(button_create_script);
- button_create_script->hide();
- button_detach_script = memnew(ToolButton);
- button_detach_script->connect("pressed", this, "_tool_selected", make_binds(TOOL_DETACH_SCRIPT, false));
- button_detach_script->set_tooltip(TTR("Detach the script from the selected node."));
- button_detach_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/detach_script"));
- filter_hbc->add_child(button_detach_script);
- button_detach_script->hide();
- button_hb = memnew(HBoxContainer);
- vbc->add_child(button_hb);
- edit_remote = memnew(ToolButton);
- button_hb->add_child(edit_remote);
- edit_remote->set_h_size_flags(SIZE_EXPAND_FILL);
- edit_remote->set_text(TTR("Remote"));
- edit_remote->set_toggle_mode(true);
- edit_remote->set_tooltip(TTR("If selected, the Remote scene tree dock will cause the project to stutter every time it updates.\nSwitch back to the Local scene tree dock to improve performance."));
- edit_remote->connect("pressed", this, "_remote_tree_selected");
- edit_local = memnew(ToolButton);
- button_hb->add_child(edit_local);
- edit_local->set_h_size_flags(SIZE_EXPAND_FILL);
- edit_local->set_text(TTR("Local"));
- edit_local->set_toggle_mode(true);
- edit_local->connect("pressed", this, "_local_tree_selected");
- remote_tree = NULL;
- button_hb->hide();
- create_root_dialog = memnew(VBoxContainer);
- vbc->add_child(create_root_dialog);
- create_root_dialog->hide();
- scene_tree = memnew(SceneTreeEditor(false, true, true));
- vbc->add_child(scene_tree);
- scene_tree->set_v_size_flags(SIZE_EXPAND | SIZE_FILL);
- scene_tree->connect("rmb_pressed", this, "_tree_rmb");
- scene_tree->connect("node_selected", this, "_node_selected", varray(), CONNECT_DEFERRED);
- scene_tree->connect("node_renamed", this, "_node_renamed", varray(), CONNECT_DEFERRED);
- scene_tree->connect("node_prerename", this, "_node_prerenamed");
- scene_tree->connect("open", this, "_load_request");
- scene_tree->connect("open_script", this, "_script_open_request");
- scene_tree->connect("nodes_rearranged", this, "_nodes_dragged");
- scene_tree->connect("files_dropped", this, "_files_dropped");
- scene_tree->connect("script_dropped", this, "_script_dropped");
- scene_tree->connect("nodes_dragged", this, "_nodes_drag_begin");
- scene_tree->get_scene_tree()->connect("item_double_clicked", this, "_focus_node");
- scene_tree->set_undo_redo(&editor_data->get_undo_redo());
- scene_tree->set_editor_selection(editor_selection);
- create_dialog = memnew(CreateDialog);
- create_dialog->set_base_type("Node");
- add_child(create_dialog);
- create_dialog->connect("create", this, "_create");
- create_dialog->connect("favorites_updated", this, "_update_create_root_dialog");
- rename_dialog = memnew(RenameDialog(scene_tree, &editor_data->get_undo_redo()));
- add_child(rename_dialog);
- script_create_dialog = memnew(ScriptCreateDialog);
- script_create_dialog->set_inheritance_base_type("Node");
- add_child(script_create_dialog);
- reparent_dialog = memnew(ReparentDialog);
- add_child(reparent_dialog);
- reparent_dialog->connect("reparent", this, "_node_reparent");
- accept = memnew(AcceptDialog);
- add_child(accept);
- quick_open = memnew(EditorQuickOpen);
- add_child(quick_open);
- quick_open->connect("quick_open", this, "_quick_open");
- set_process_unhandled_key_input(true);
- delete_dialog = memnew(ConfirmationDialog);
- add_child(delete_dialog);
- delete_dialog->connect("confirmed", this, "_delete_confirm", varray(false));
- editable_instance_remove_dialog = memnew(ConfirmationDialog);
- add_child(editable_instance_remove_dialog);
- editable_instance_remove_dialog->connect("confirmed", this, "_toggle_editable_children_from_selection");
- placeholder_editable_instance_remove_dialog = memnew(ConfirmationDialog);
- add_child(placeholder_editable_instance_remove_dialog);
- placeholder_editable_instance_remove_dialog->connect("confirmed", this, "_toggle_placeholder_from_selection");
- import_subscene_dialog = memnew(EditorSubScene);
- add_child(import_subscene_dialog);
- import_subscene_dialog->connect("subscene_selected", this, "_import_subscene");
- new_scene_from_dialog = memnew(EditorFileDialog);
- new_scene_from_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
- add_child(new_scene_from_dialog);
- new_scene_from_dialog->connect("file_selected", this, "_new_scene_from");
- menu = memnew(PopupMenu);
- add_child(menu);
- menu->connect("id_pressed", this, "_tool_selected");
- menu->set_hide_on_window_lose_focus(true);
- menu_subresources = memnew(PopupMenu);
- menu_subresources->set_name("Sub-Resources");
- menu_subresources->connect("id_pressed", this, "_tool_selected");
- menu->add_child(menu_subresources);
- first_enter = true;
- restore_script_editor_on_drag = false;
- clear_inherit_confirm = memnew(ConfirmationDialog);
- clear_inherit_confirm->set_text(TTR("Clear Inheritance? (No Undo!)"));
- clear_inherit_confirm->get_ok()->set_text(TTR("Clear"));
- add_child(clear_inherit_confirm);
- set_process_input(true);
- set_process(true);
- profile_allow_editing = true;
- profile_allow_script_editing = true;
- EDITOR_DEF("interface/editors/show_scene_tree_root_selection", true);
- EDITOR_DEF("interface/editors/derive_script_globals_by_name", true);
- EDITOR_DEF("_use_favorites_root_selection", false);
- }
- SceneTreeDock::~SceneTreeDock() {
- if (!node_clipboard.empty()) {
- _clear_clipboard();
- }
- }
|