script_editor_plugin.cpp 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559
  1. /*************************************************************************/
  2. /* script_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "script_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/os/file_access.h"
  33. #include "core/os/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "core/project_settings.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/find_in_files.h"
  40. #include "editor/node_dock.h"
  41. #include "editor/plugins/shader_editor_plugin.h"
  42. #include "editor/script_editor_debugger.h"
  43. #include "scene/main/viewport.h"
  44. #include "script_text_editor.h"
  45. /*** SCRIPT EDITOR ****/
  46. void ScriptEditorBase::_bind_methods() {
  47. ADD_SIGNAL(MethodInfo("name_changed"));
  48. ADD_SIGNAL(MethodInfo("edited_script_changed"));
  49. ADD_SIGNAL(MethodInfo("request_help", PropertyInfo(Variant::STRING, "topic")));
  50. ADD_SIGNAL(MethodInfo("request_open_script_at_line", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::INT, "line")));
  51. ADD_SIGNAL(MethodInfo("request_save_history"));
  52. ADD_SIGNAL(MethodInfo("go_to_help", PropertyInfo(Variant::STRING, "what")));
  53. // TODO This signal is no use for VisualScript...
  54. ADD_SIGNAL(MethodInfo("search_in_files_requested", PropertyInfo(Variant::STRING, "text")));
  55. }
  56. static bool _is_built_in_script(Script *p_script) {
  57. String path = p_script->get_path();
  58. return path.find("::") != -1;
  59. }
  60. class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache {
  61. struct Cache {
  62. uint64_t time_loaded;
  63. RES cache;
  64. };
  65. Map<String, Cache> cached;
  66. public:
  67. uint64_t max_time_cache;
  68. int max_cache_size;
  69. void cleanup() {
  70. List<Map<String, Cache>::Element *> to_clean;
  71. Map<String, Cache>::Element *I = cached.front();
  72. while (I) {
  73. if ((OS::get_singleton()->get_ticks_msec() - I->get().time_loaded) > max_time_cache) {
  74. to_clean.push_back(I);
  75. }
  76. I = I->next();
  77. }
  78. while (to_clean.front()) {
  79. cached.erase(to_clean.front()->get());
  80. to_clean.pop_front();
  81. }
  82. }
  83. virtual RES get_cached_resource(const String &p_path) {
  84. Map<String, Cache>::Element *E = cached.find(p_path);
  85. if (!E) {
  86. Cache c;
  87. c.cache = ResourceLoader::load(p_path);
  88. E = cached.insert(p_path, c);
  89. }
  90. E->get().time_loaded = OS::get_singleton()->get_ticks_msec();
  91. if (cached.size() > max_cache_size) {
  92. uint64_t older;
  93. Map<String, Cache>::Element *O = cached.front();
  94. older = O->get().time_loaded;
  95. Map<String, Cache>::Element *I = O;
  96. while (I) {
  97. if (I->get().time_loaded < older) {
  98. older = I->get().time_loaded;
  99. O = I;
  100. }
  101. I = I->next();
  102. }
  103. if (O != E) { //should never happen..
  104. cached.erase(O);
  105. }
  106. }
  107. return E->get().cache;
  108. }
  109. EditorScriptCodeCompletionCache() {
  110. max_cache_size = 128;
  111. max_time_cache = 5 * 60 * 1000; //minutes, five
  112. }
  113. virtual ~EditorScriptCodeCompletionCache() {}
  114. };
  115. void ScriptEditorQuickOpen::popup_dialog(const Vector<String> &p_functions, bool p_dontclear) {
  116. popup_centered_ratio(0.6);
  117. if (p_dontclear)
  118. search_box->select_all();
  119. else
  120. search_box->clear();
  121. search_box->grab_focus();
  122. functions = p_functions;
  123. _update_search();
  124. }
  125. void ScriptEditorQuickOpen::_text_changed(const String &p_newtext) {
  126. _update_search();
  127. }
  128. void ScriptEditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
  129. Ref<InputEventKey> k = p_ie;
  130. if (k.is_valid() && (k->get_scancode() == KEY_UP ||
  131. k->get_scancode() == KEY_DOWN ||
  132. k->get_scancode() == KEY_PAGEUP ||
  133. k->get_scancode() == KEY_PAGEDOWN)) {
  134. search_options->call("_gui_input", k);
  135. search_box->accept_event();
  136. }
  137. }
  138. void ScriptEditorQuickOpen::_update_search() {
  139. search_options->clear();
  140. TreeItem *root = search_options->create_item();
  141. for (int i = 0; i < functions.size(); i++) {
  142. String file = functions[i];
  143. if ((search_box->get_text() == "" || file.findn(search_box->get_text()) != -1)) {
  144. TreeItem *ti = search_options->create_item(root);
  145. ti->set_text(0, file);
  146. if (root->get_children() == ti)
  147. ti->select(0);
  148. }
  149. }
  150. get_ok()->set_disabled(root->get_children() == NULL);
  151. }
  152. void ScriptEditorQuickOpen::_confirmed() {
  153. TreeItem *ti = search_options->get_selected();
  154. if (!ti)
  155. return;
  156. int line = ti->get_text(0).get_slice(":", 1).to_int();
  157. emit_signal("goto_line", line - 1);
  158. hide();
  159. }
  160. void ScriptEditorQuickOpen::_notification(int p_what) {
  161. switch (p_what) {
  162. case NOTIFICATION_ENTER_TREE: {
  163. connect("confirmed", this, "_confirmed");
  164. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  165. search_box->set_clear_button_enabled(true);
  166. } break;
  167. case NOTIFICATION_EXIT_TREE: {
  168. disconnect("confirmed", this, "_confirmed");
  169. } break;
  170. }
  171. }
  172. void ScriptEditorQuickOpen::_bind_methods() {
  173. ClassDB::bind_method(D_METHOD("_text_changed"), &ScriptEditorQuickOpen::_text_changed);
  174. ClassDB::bind_method(D_METHOD("_confirmed"), &ScriptEditorQuickOpen::_confirmed);
  175. ClassDB::bind_method(D_METHOD("_sbox_input"), &ScriptEditorQuickOpen::_sbox_input);
  176. ADD_SIGNAL(MethodInfo("goto_line", PropertyInfo(Variant::INT, "line")));
  177. }
  178. ScriptEditorQuickOpen::ScriptEditorQuickOpen() {
  179. VBoxContainer *vbc = memnew(VBoxContainer);
  180. add_child(vbc);
  181. search_box = memnew(LineEdit);
  182. vbc->add_margin_child(TTR("Search:"), search_box);
  183. search_box->connect("text_changed", this, "_text_changed");
  184. search_box->connect("gui_input", this, "_sbox_input");
  185. search_options = memnew(Tree);
  186. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  187. get_ok()->set_text(TTR("Open"));
  188. get_ok()->set_disabled(true);
  189. register_text_enter(search_box);
  190. set_hide_on_ok(false);
  191. search_options->connect("item_activated", this, "_confirmed");
  192. search_options->set_hide_root(true);
  193. }
  194. /////////////////////////////////
  195. ScriptEditor *ScriptEditor::script_editor = NULL;
  196. /*** SCRIPT EDITOR ******/
  197. String ScriptEditor::_get_debug_tooltip(const String &p_text, Node *_se) {
  198. String val = debugger->get_var_value(p_text);
  199. if (val != String()) {
  200. return p_text + ": " + val;
  201. } else {
  202. return String();
  203. }
  204. }
  205. void ScriptEditor::_breaked(bool p_breaked, bool p_can_debug) {
  206. if (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor"))) {
  207. return;
  208. }
  209. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug));
  210. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug));
  211. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked);
  212. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked);
  213. for (int i = 0; i < tab_container->get_child_count(); i++) {
  214. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  215. if (!se) {
  216. continue;
  217. }
  218. se->set_debugger_active(p_breaked);
  219. }
  220. }
  221. void ScriptEditor::_show_debugger(bool p_show) {
  222. //debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), p_show);
  223. }
  224. void ScriptEditor::_script_created(Ref<Script> p_script) {
  225. editor->push_item(p_script.operator->());
  226. }
  227. void ScriptEditor::_goto_script_line2(int p_line) {
  228. ScriptEditorBase *current = _get_current_editor();
  229. if (current)
  230. current->goto_line(p_line);
  231. }
  232. void ScriptEditor::_goto_script_line(REF p_script, int p_line) {
  233. Ref<Script> script = Object::cast_to<Script>(*p_script);
  234. if (script.is_valid() && (script->has_source_code() || script->get_path().is_resource_file())) {
  235. if (edit(p_script, p_line, 0)) {
  236. editor->push_item(p_script.ptr());
  237. ScriptEditorBase *current = _get_current_editor();
  238. if (ScriptTextEditor *script_text_editor = Object::cast_to<ScriptTextEditor>(current)) {
  239. script_text_editor->goto_line_centered(p_line);
  240. } else if (current) {
  241. current->goto_line(p_line, true);
  242. }
  243. }
  244. }
  245. }
  246. void ScriptEditor::_set_execution(REF p_script, int p_line) {
  247. Ref<Script> script = Object::cast_to<Script>(*p_script);
  248. if (script.is_valid() && (script->has_source_code() || script->get_path().is_resource_file())) {
  249. for (int i = 0; i < tab_container->get_child_count(); i++) {
  250. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  251. if (!se)
  252. continue;
  253. if ((script != NULL && se->get_edited_resource() == p_script) || se->get_edited_resource()->get_path() == script->get_path()) {
  254. se->set_executing_line(p_line);
  255. }
  256. }
  257. }
  258. }
  259. void ScriptEditor::_clear_execution(REF p_script) {
  260. Ref<Script> script = Object::cast_to<Script>(*p_script);
  261. if (script.is_valid() && (script->has_source_code() || script->get_path().is_resource_file())) {
  262. for (int i = 0; i < tab_container->get_child_count(); i++) {
  263. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  264. if (!se)
  265. continue;
  266. if ((script != NULL && se->get_edited_resource() == p_script) || se->get_edited_resource()->get_path() == script->get_path()) {
  267. se->clear_executing_line();
  268. }
  269. }
  270. }
  271. }
  272. ScriptEditorBase *ScriptEditor::_get_current_editor() const {
  273. int selected = tab_container->get_current_tab();
  274. if (selected < 0 || selected >= tab_container->get_child_count())
  275. return NULL;
  276. return Object::cast_to<ScriptEditorBase>(tab_container->get_child(selected));
  277. }
  278. void ScriptEditor::_update_history_arrows() {
  279. script_back->set_disabled(history_pos <= 0);
  280. script_forward->set_disabled(history_pos >= history.size() - 1);
  281. }
  282. void ScriptEditor::_save_history() {
  283. if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) {
  284. Node *n = tab_container->get_current_tab_control();
  285. if (Object::cast_to<ScriptEditorBase>(n)) {
  286. history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_edit_state();
  287. }
  288. if (Object::cast_to<EditorHelp>(n)) {
  289. history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();
  290. }
  291. }
  292. history.resize(history_pos + 1);
  293. ScriptHistory sh;
  294. sh.control = tab_container->get_current_tab_control();
  295. sh.state = Variant();
  296. history.push_back(sh);
  297. history_pos++;
  298. _update_history_arrows();
  299. }
  300. void ScriptEditor::_go_to_tab(int p_idx) {
  301. ScriptEditorBase *current = _get_current_editor();
  302. if (current) {
  303. if (current->is_unsaved()) {
  304. current->apply_code();
  305. }
  306. }
  307. Control *c = Object::cast_to<Control>(tab_container->get_child(p_idx));
  308. if (!c)
  309. return;
  310. if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) {
  311. Node *n = tab_container->get_current_tab_control();
  312. if (Object::cast_to<ScriptEditorBase>(n)) {
  313. history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_edit_state();
  314. }
  315. if (Object::cast_to<EditorHelp>(n)) {
  316. history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();
  317. }
  318. }
  319. history.resize(history_pos + 1);
  320. ScriptHistory sh;
  321. sh.control = c;
  322. sh.state = Variant();
  323. history.push_back(sh);
  324. history_pos++;
  325. tab_container->set_current_tab(p_idx);
  326. c = tab_container->get_current_tab_control();
  327. if (Object::cast_to<ScriptEditorBase>(c)) {
  328. script_name_label->set_text(Object::cast_to<ScriptEditorBase>(c)->get_name());
  329. script_icon->set_texture(Object::cast_to<ScriptEditorBase>(c)->get_icon());
  330. if (is_visible_in_tree())
  331. Object::cast_to<ScriptEditorBase>(c)->ensure_focus();
  332. Ref<Script> script = Object::cast_to<ScriptEditorBase>(c)->get_edited_resource();
  333. if (script != NULL) {
  334. notify_script_changed(script);
  335. }
  336. Object::cast_to<ScriptEditorBase>(c)->validate();
  337. }
  338. if (Object::cast_to<EditorHelp>(c)) {
  339. script_name_label->set_text(Object::cast_to<EditorHelp>(c)->get_class());
  340. script_icon->set_texture(get_icon("Help", "EditorIcons"));
  341. if (is_visible_in_tree())
  342. Object::cast_to<EditorHelp>(c)->set_focused();
  343. }
  344. c->set_meta("__editor_pass", ++edit_pass);
  345. _update_history_arrows();
  346. _update_script_colors();
  347. _update_members_overview();
  348. _update_help_overview();
  349. _update_selected_editor_menu();
  350. _update_members_overview_visibility();
  351. _update_help_overview_visibility();
  352. }
  353. void ScriptEditor::_add_recent_script(String p_path) {
  354. if (p_path.empty()) {
  355. return;
  356. }
  357. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
  358. if (rc.find(p_path) != -1) {
  359. rc.erase(p_path);
  360. }
  361. rc.push_front(p_path);
  362. if (rc.size() > 10) {
  363. rc.resize(10);
  364. }
  365. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", rc);
  366. _update_recent_scripts();
  367. }
  368. void ScriptEditor::_update_recent_scripts() {
  369. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
  370. recent_scripts->clear();
  371. recent_scripts->add_shortcut(ED_SHORTCUT("script_editor/open_recent", TTR("Open Recent")));
  372. recent_scripts->add_separator();
  373. String path;
  374. for (int i = 0; i < rc.size(); i++) {
  375. path = rc[i];
  376. recent_scripts->add_item(path.replace("res://", ""));
  377. }
  378. recent_scripts->add_separator();
  379. recent_scripts->add_shortcut(ED_SHORTCUT("script_editor/clear_recent", TTR("Clear Recent Files")));
  380. recent_scripts->set_as_minsize();
  381. }
  382. void ScriptEditor::_open_recent_script(int p_idx) {
  383. // clear button
  384. if (p_idx == recent_scripts->get_item_count() - 1) {
  385. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", Array());
  386. call_deferred("_update_recent_scripts");
  387. return;
  388. }
  389. // take two for the open recent button
  390. if (p_idx > 0) {
  391. p_idx -= 2;
  392. }
  393. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
  394. ERR_FAIL_INDEX(p_idx, rc.size());
  395. String path = rc[p_idx];
  396. // if its not on disk its a help file or deleted
  397. if (FileAccess::exists(path)) {
  398. List<String> extensions;
  399. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  400. if (extensions.find(path.get_extension())) {
  401. Ref<Script> script = ResourceLoader::load(path);
  402. if (script.is_valid()) {
  403. edit(script, true);
  404. return;
  405. }
  406. }
  407. Error err;
  408. Ref<TextFile> text_file = _load_text_file(path, &err);
  409. if (text_file.is_valid()) {
  410. edit(text_file, true);
  411. return;
  412. }
  413. // if it's a path then it's most likely a deleted file not help
  414. } else if (path.find("::") != -1) {
  415. // built-in script
  416. Ref<Script> script = ResourceLoader::load(path);
  417. if (script.is_valid()) {
  418. edit(script, true);
  419. return;
  420. }
  421. } else if (!path.is_resource_file()) {
  422. _help_class_open(path);
  423. return;
  424. }
  425. rc.remove(p_idx);
  426. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", rc);
  427. _update_recent_scripts();
  428. _show_error_dialog(path);
  429. }
  430. void ScriptEditor::_show_error_dialog(String p_path) {
  431. error_dialog->set_text(vformat(TTR("Can't open '%s'. The file could have been moved or deleted."), p_path));
  432. error_dialog->popup_centered_minsize();
  433. }
  434. void ScriptEditor::_close_tab(int p_idx, bool p_save, bool p_history_back) {
  435. int selected = p_idx;
  436. if (selected < 0 || selected >= tab_container->get_child_count())
  437. return;
  438. Node *tselected = tab_container->get_child(selected);
  439. ScriptEditorBase *current = Object::cast_to<ScriptEditorBase>(tab_container->get_child(selected));
  440. if (current) {
  441. if (p_save) {
  442. apply_scripts();
  443. }
  444. Ref<Script> script = current->get_edited_resource();
  445. if (script != NULL) {
  446. previous_scripts.push_back(script->get_path());
  447. notify_script_close(script);
  448. }
  449. }
  450. // roll back to previous tab
  451. if (p_history_back) {
  452. _history_back();
  453. }
  454. //remove from history
  455. history.resize(history_pos + 1);
  456. for (int i = 0; i < history.size(); i++) {
  457. if (history[i].control == tselected) {
  458. history.remove(i);
  459. i--;
  460. history_pos--;
  461. }
  462. }
  463. if (history_pos >= history.size()) {
  464. history_pos = history.size() - 1;
  465. }
  466. int idx = tab_container->get_current_tab();
  467. if (current) {
  468. current->clear_edit_menu();
  469. }
  470. memdelete(tselected);
  471. if (idx >= tab_container->get_child_count())
  472. idx = tab_container->get_child_count() - 1;
  473. if (idx >= 0) {
  474. if (history_pos >= 0) {
  475. idx = history[history_pos].control->get_index();
  476. }
  477. tab_container->set_current_tab(idx);
  478. } else {
  479. _update_selected_editor_menu();
  480. }
  481. _update_history_arrows();
  482. _update_script_names();
  483. _update_members_overview_visibility();
  484. _update_help_overview_visibility();
  485. _save_layout();
  486. }
  487. void ScriptEditor::_close_current_tab() {
  488. _close_tab(tab_container->get_current_tab());
  489. }
  490. void ScriptEditor::_close_discard_current_tab(const String &p_str) {
  491. _close_tab(tab_container->get_current_tab(), false);
  492. erase_tab_confirm->hide();
  493. }
  494. void ScriptEditor::_close_docs_tab() {
  495. int child_count = tab_container->get_child_count();
  496. for (int i = child_count - 1; i >= 0; i--) {
  497. EditorHelp *se = Object::cast_to<EditorHelp>(tab_container->get_child(i));
  498. if (se) {
  499. _close_tab(i, true, false);
  500. }
  501. }
  502. }
  503. void ScriptEditor::_copy_script_path() {
  504. ScriptEditorBase *se = _get_current_editor();
  505. RES script = se->get_edited_resource();
  506. OS::get_singleton()->set_clipboard(script->get_path());
  507. }
  508. void ScriptEditor::_close_other_tabs() {
  509. int child_count = tab_container->get_child_count();
  510. int current_idx = tab_container->get_current_tab();
  511. for (int i = child_count - 1; i >= 0; i--) {
  512. if (i == current_idx) {
  513. continue;
  514. }
  515. tab_container->set_current_tab(i);
  516. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  517. if (se) {
  518. // Maybe there are unsaved changes
  519. if (se->is_unsaved()) {
  520. _ask_close_current_unsaved_tab(se);
  521. continue;
  522. }
  523. }
  524. _close_current_tab();
  525. }
  526. }
  527. void ScriptEditor::_close_all_tabs() {
  528. int child_count = tab_container->get_child_count();
  529. for (int i = child_count - 1; i >= 0; i--) {
  530. tab_container->set_current_tab(i);
  531. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  532. if (se) {
  533. // Maybe there are unsaved changes
  534. if (se->is_unsaved()) {
  535. _ask_close_current_unsaved_tab(se);
  536. continue;
  537. }
  538. }
  539. _close_current_tab();
  540. }
  541. }
  542. void ScriptEditor::_ask_close_current_unsaved_tab(ScriptEditorBase *current) {
  543. erase_tab_confirm->set_text(TTR("Close and save changes?") + "\n\"" + current->get_name() + "\"");
  544. erase_tab_confirm->popup_centered_minsize();
  545. }
  546. void ScriptEditor::_resave_scripts(const String &p_str) {
  547. apply_scripts();
  548. for (int i = 0; i < tab_container->get_child_count(); i++) {
  549. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  550. if (!se)
  551. continue;
  552. RES script = se->get_edited_resource();
  553. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1)
  554. continue; //internal script, who cares
  555. if (trim_trailing_whitespace_on_save) {
  556. se->trim_trailing_whitespace();
  557. }
  558. se->insert_final_newline();
  559. if (convert_indent_on_save) {
  560. if (use_space_indentation) {
  561. se->convert_indent_to_spaces();
  562. } else {
  563. se->convert_indent_to_tabs();
  564. }
  565. }
  566. Ref<TextFile> text_file = script;
  567. if (text_file != NULL) {
  568. se->apply_code();
  569. _save_text_file(text_file, text_file->get_path());
  570. break;
  571. } else {
  572. editor->save_resource(script);
  573. }
  574. se->tag_saved_version();
  575. }
  576. disk_changed->hide();
  577. }
  578. void ScriptEditor::_reload_scripts() {
  579. for (int i = 0; i < tab_container->get_child_count(); i++) {
  580. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  581. if (!se) {
  582. continue;
  583. }
  584. RES edited_res = se->get_edited_resource();
  585. if (edited_res->get_path() == "" || edited_res->get_path().find("local://") != -1 || edited_res->get_path().find("::") != -1) {
  586. continue; //internal script, who cares
  587. }
  588. uint64_t last_date = edited_res->get_last_modified_time();
  589. uint64_t date = FileAccess::get_modified_time(edited_res->get_path());
  590. if (last_date == date) {
  591. continue;
  592. }
  593. Ref<Script> script = edited_res;
  594. if (script != NULL) {
  595. Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_class(), true);
  596. ERR_CONTINUE(!rel_script.is_valid());
  597. script->set_source_code(rel_script->get_source_code());
  598. script->set_last_modified_time(rel_script->get_last_modified_time());
  599. script->reload();
  600. }
  601. Ref<TextFile> text_file = edited_res;
  602. if (text_file != NULL) {
  603. Error err;
  604. Ref<TextFile> rel_text_file = _load_text_file(text_file->get_path(), &err);
  605. ERR_CONTINUE(!rel_text_file.is_valid());
  606. text_file->set_text(rel_text_file->get_text());
  607. text_file->set_last_modified_time(rel_text_file->get_last_modified_time());
  608. }
  609. se->reload_text();
  610. }
  611. disk_changed->hide();
  612. _update_script_names();
  613. }
  614. void ScriptEditor::_res_saved_callback(const Ref<Resource> &p_res) {
  615. for (int i = 0; i < tab_container->get_child_count(); i++) {
  616. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  617. if (!se) {
  618. continue;
  619. }
  620. RES script = se->get_edited_resource();
  621. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
  622. continue; //internal script, who cares
  623. }
  624. if (script == p_res) {
  625. se->tag_saved_version();
  626. }
  627. }
  628. _update_script_names();
  629. if (!pending_auto_reload && auto_reload_running_scripts) {
  630. call_deferred("_live_auto_reload_running_scripts");
  631. pending_auto_reload = true;
  632. }
  633. }
  634. void ScriptEditor::_live_auto_reload_running_scripts() {
  635. pending_auto_reload = false;
  636. debugger->reload_scripts();
  637. }
  638. bool ScriptEditor::_test_script_times_on_disk(RES p_for_script) {
  639. disk_changed_list->clear();
  640. TreeItem *r = disk_changed_list->create_item();
  641. disk_changed_list->set_hide_root(true);
  642. bool need_ask = false;
  643. bool need_reload = false;
  644. bool use_autoreload = bool(EDITOR_DEF("text_editor/files/auto_reload_scripts_on_external_change", false));
  645. for (int i = 0; i < tab_container->get_child_count(); i++) {
  646. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  647. if (se) {
  648. RES edited_res = se->get_edited_resource();
  649. if (p_for_script.is_valid() && edited_res.is_valid() && p_for_script != edited_res)
  650. continue;
  651. if (edited_res->get_path() == "" || edited_res->get_path().find("local://") != -1 || edited_res->get_path().find("::") != -1)
  652. continue; //internal script, who cares
  653. uint64_t last_date = edited_res->get_last_modified_time();
  654. uint64_t date = FileAccess::get_modified_time(edited_res->get_path());
  655. if (last_date != date) {
  656. TreeItem *ti = disk_changed_list->create_item(r);
  657. ti->set_text(0, edited_res->get_path().get_file());
  658. if (!use_autoreload || se->is_unsaved()) {
  659. need_ask = true;
  660. }
  661. need_reload = true;
  662. }
  663. }
  664. }
  665. if (need_reload) {
  666. if (!need_ask) {
  667. script_editor->_reload_scripts();
  668. need_reload = false;
  669. } else {
  670. disk_changed->call_deferred("popup_centered_ratio", 0.5);
  671. }
  672. }
  673. return need_reload;
  674. }
  675. void ScriptEditor::_file_dialog_action(String p_file) {
  676. switch (file_dialog_option) {
  677. case FILE_NEW_TEXTFILE: {
  678. Error err;
  679. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  680. if (err) {
  681. memdelete(file);
  682. editor->show_warning(TTR("Error writing TextFile:") + "\n" + p_file, TTR("Error!"));
  683. break;
  684. }
  685. file->close();
  686. memdelete(file);
  687. FALLTHROUGH;
  688. }
  689. case FILE_OPEN: {
  690. List<String> extensions;
  691. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  692. if (extensions.find(p_file.get_extension())) {
  693. Ref<Script> scr = ResourceLoader::load(p_file);
  694. if (!scr.is_valid()) {
  695. editor->show_warning(TTR("Could not load file at:") + "\n\n" + p_file, TTR("Error!"));
  696. file_dialog_option = -1;
  697. return;
  698. }
  699. edit(scr);
  700. file_dialog_option = -1;
  701. return;
  702. }
  703. Error error;
  704. Ref<TextFile> text_file = _load_text_file(p_file, &error);
  705. if (error != OK) {
  706. editor->show_warning(TTR("Could not load file at:") + "\n\n" + p_file, TTR("Error!"));
  707. }
  708. if (text_file.is_valid()) {
  709. edit(text_file);
  710. file_dialog_option = -1;
  711. return;
  712. }
  713. } break;
  714. case FILE_SAVE_AS: {
  715. ScriptEditorBase *current = _get_current_editor();
  716. String path = ProjectSettings::get_singleton()->localize_path(p_file);
  717. Error err = _save_text_file(current->get_edited_resource(), path);
  718. if (err != OK) {
  719. editor->show_accept(TTR("Error saving file!"), TTR("OK"));
  720. return;
  721. }
  722. ((Resource *)current->get_edited_resource().ptr())->set_path(path);
  723. _update_script_names();
  724. } break;
  725. case THEME_SAVE_AS: {
  726. if (!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) {
  727. editor->show_warning(TTR("Error while saving theme."), TTR("Error Saving"));
  728. }
  729. } break;
  730. case THEME_IMPORT: {
  731. if (!EditorSettings::get_singleton()->import_text_editor_theme(p_file)) {
  732. editor->show_warning(TTR("Error importing theme."), TTR("Error Importing"));
  733. }
  734. } break;
  735. }
  736. file_dialog_option = -1;
  737. }
  738. Ref<Script> ScriptEditor::_get_current_script() {
  739. ScriptEditorBase *current = _get_current_editor();
  740. if (current) {
  741. Ref<Script> script = current->get_edited_resource();
  742. return script != NULL ? script : NULL;
  743. } else {
  744. return NULL;
  745. }
  746. }
  747. Array ScriptEditor::_get_open_scripts() const {
  748. Array ret;
  749. Vector<Ref<Script> > scripts = get_open_scripts();
  750. int scrits_amount = scripts.size();
  751. for (int idx_script = 0; idx_script < scrits_amount; idx_script++) {
  752. ret.push_back(scripts[idx_script]);
  753. }
  754. return ret;
  755. }
  756. void ScriptEditor::_menu_option(int p_option) {
  757. switch (p_option) {
  758. case FILE_NEW: {
  759. script_create_dialog->config("Node", "new_script");
  760. script_create_dialog->popup_centered(Size2(300, 300) * EDSCALE);
  761. } break;
  762. case FILE_NEW_TEXTFILE: {
  763. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  764. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  765. file_dialog_option = FILE_NEW_TEXTFILE;
  766. file_dialog->clear_filters();
  767. file_dialog->popup_centered_ratio();
  768. file_dialog->set_title(TTR("New TextFile..."));
  769. } break;
  770. case FILE_OPEN: {
  771. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  772. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  773. file_dialog_option = FILE_OPEN;
  774. List<String> extensions;
  775. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  776. file_dialog->clear_filters();
  777. for (int i = 0; i < extensions.size(); i++) {
  778. file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  779. }
  780. file_dialog->popup_centered_ratio();
  781. file_dialog->set_title(TTR("Open File"));
  782. return;
  783. } break;
  784. case FILE_REOPEN_CLOSED: {
  785. if (previous_scripts.empty())
  786. return;
  787. String path = previous_scripts.back()->get();
  788. previous_scripts.pop_back();
  789. List<String> extensions;
  790. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  791. bool built_in = !path.is_resource_file();
  792. if (extensions.find(path.get_extension()) || built_in) {
  793. if (built_in) {
  794. String scene_path = path.get_slice("::", 0);
  795. if (!EditorNode::get_singleton()->is_scene_open(scene_path)) {
  796. EditorNode::get_singleton()->load_scene(scene_path);
  797. script_editor->call_deferred("_menu_option", p_option);
  798. previous_scripts.push_back(path); //repeat the operation
  799. return;
  800. }
  801. }
  802. Ref<Script> scr = ResourceLoader::load(path);
  803. if (!scr.is_valid()) {
  804. editor->show_warning(TTR("Could not load file at:") + "\n\n" + path, TTR("Error!"));
  805. file_dialog_option = -1;
  806. return;
  807. }
  808. edit(scr);
  809. file_dialog_option = -1;
  810. return;
  811. } else {
  812. Error error;
  813. Ref<TextFile> text_file = _load_text_file(path, &error);
  814. if (error != OK)
  815. editor->show_warning(TTR("Could not load file at:") + "\n\n" + path, TTR("Error!"));
  816. if (text_file.is_valid()) {
  817. edit(text_file);
  818. file_dialog_option = -1;
  819. return;
  820. }
  821. }
  822. } break;
  823. case FILE_SAVE_ALL: {
  824. if (_test_script_times_on_disk())
  825. return;
  826. save_all_scripts();
  827. } break;
  828. case SEARCH_IN_FILES: {
  829. _on_find_in_files_requested("");
  830. } break;
  831. case SEARCH_HELP: {
  832. help_search_dialog->popup_dialog();
  833. } break;
  834. case SEARCH_WEBSITE: {
  835. OS::get_singleton()->shell_open("https://docs.godotengine.org/");
  836. } break;
  837. case REQUEST_DOCS: {
  838. OS::get_singleton()->shell_open("https://github.com/godotengine/godot-docs/issues/new");
  839. } break;
  840. case WINDOW_NEXT: {
  841. _history_forward();
  842. } break;
  843. case WINDOW_PREV: {
  844. _history_back();
  845. } break;
  846. case WINDOW_SORT: {
  847. _sort_list_on_update = true;
  848. _update_script_names();
  849. } break;
  850. case DEBUG_SHOW: {
  851. if (debugger) {
  852. bool visible = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW));
  853. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW), !visible);
  854. if (visible)
  855. debugger->hide();
  856. else
  857. debugger->show();
  858. }
  859. } break;
  860. case DEBUG_SHOW_KEEP_OPEN: {
  861. bool visible = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN));
  862. if (debugger)
  863. debugger->set_hide_on_stop(visible);
  864. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN), !visible);
  865. } break;
  866. case DEBUG_WITH_EXTERNAL_EDITOR: {
  867. bool debug_with_external_editor = !debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR));
  868. debugger->set_debug_with_external_editor(debug_with_external_editor);
  869. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR), debug_with_external_editor);
  870. } break;
  871. case TOGGLE_SCRIPTS_PANEL: {
  872. list_split->set_visible(!list_split->is_visible());
  873. }
  874. }
  875. ScriptEditorBase *current = _get_current_editor();
  876. if (current) {
  877. switch (p_option) {
  878. case FILE_SAVE: {
  879. if (_test_script_times_on_disk())
  880. return;
  881. if (trim_trailing_whitespace_on_save)
  882. current->trim_trailing_whitespace();
  883. current->insert_final_newline();
  884. if (convert_indent_on_save) {
  885. if (use_space_indentation) {
  886. current->convert_indent_to_spaces();
  887. } else {
  888. current->convert_indent_to_tabs();
  889. }
  890. }
  891. Ref<TextFile> text_file = current->get_edited_resource();
  892. if (text_file != NULL) {
  893. current->apply_code();
  894. _save_text_file(text_file, text_file->get_path());
  895. break;
  896. }
  897. editor->save_resource(current->get_edited_resource());
  898. } break;
  899. case FILE_SAVE_AS: {
  900. if (trim_trailing_whitespace_on_save)
  901. current->trim_trailing_whitespace();
  902. current->insert_final_newline();
  903. if (convert_indent_on_save) {
  904. if (use_space_indentation) {
  905. current->convert_indent_to_spaces();
  906. } else {
  907. current->convert_indent_to_tabs();
  908. }
  909. }
  910. Ref<TextFile> text_file = current->get_edited_resource();
  911. if (text_file != NULL) {
  912. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  913. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  914. file_dialog_option = FILE_SAVE_AS;
  915. List<String> extensions;
  916. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  917. file_dialog->clear_filters();
  918. file_dialog->set_current_dir(text_file->get_path().get_base_dir());
  919. file_dialog->set_current_file(text_file->get_path().get_file());
  920. file_dialog->popup_centered_ratio();
  921. file_dialog->set_title(TTR("Save File As..."));
  922. break;
  923. }
  924. editor->push_item(Object::cast_to<Object>(current->get_edited_resource().ptr()));
  925. editor->save_resource_as(current->get_edited_resource());
  926. } break;
  927. case FILE_TOOL_RELOAD:
  928. case FILE_TOOL_RELOAD_SOFT: {
  929. current->reload(p_option == FILE_TOOL_RELOAD_SOFT);
  930. } break;
  931. case FILE_RUN: {
  932. Ref<Script> scr = current->get_edited_resource();
  933. if (scr == NULL || scr.is_null()) {
  934. EditorNode::get_singleton()->show_warning("Can't obtain the script for running.");
  935. break;
  936. }
  937. current->apply_code();
  938. Error err = scr->reload(false); //hard reload script before running always
  939. if (err != OK) {
  940. EditorNode::get_singleton()->show_warning("Script failed reloading, check console for errors.");
  941. return;
  942. }
  943. if (!scr->is_tool()) {
  944. EditorNode::get_singleton()->show_warning("Script is not in tool mode, will not be able to run.");
  945. return;
  946. }
  947. if (!ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorScript")) {
  948. EditorNode::get_singleton()->show_warning("To run this script, it must inherit EditorScript and be set to tool mode.");
  949. return;
  950. }
  951. Ref<EditorScript> es = memnew(EditorScript);
  952. es->set_script(scr.get_ref_ptr());
  953. es->set_editor(EditorNode::get_singleton());
  954. es->_run();
  955. EditorNode::get_undo_redo()->clear_history();
  956. } break;
  957. case FILE_CLOSE: {
  958. if (current->is_unsaved()) {
  959. _ask_close_current_unsaved_tab(current);
  960. } else {
  961. _close_current_tab();
  962. }
  963. } break;
  964. case FILE_COPY_PATH: {
  965. _copy_script_path();
  966. } break;
  967. case SHOW_IN_FILE_SYSTEM: {
  968. RES script = current->get_edited_resource();
  969. FileSystemDock *file_system_dock = EditorNode::get_singleton()->get_filesystem_dock();
  970. file_system_dock->navigate_to_path(script->get_path());
  971. // Ensure that the FileSystem dock is visible.
  972. TabContainer *tab_container = (TabContainer *)file_system_dock->get_parent_control();
  973. tab_container->set_current_tab(file_system_dock->get_position_in_parent());
  974. } break;
  975. case CLOSE_DOCS: {
  976. _close_docs_tab();
  977. } break;
  978. case CLOSE_OTHER_TABS: {
  979. _close_other_tabs();
  980. } break;
  981. case CLOSE_ALL: {
  982. _close_all_tabs();
  983. } break;
  984. case DEBUG_NEXT: {
  985. if (debugger)
  986. debugger->debug_next();
  987. } break;
  988. case DEBUG_STEP: {
  989. if (debugger)
  990. debugger->debug_step();
  991. } break;
  992. case DEBUG_BREAK: {
  993. if (debugger)
  994. debugger->debug_break();
  995. } break;
  996. case DEBUG_CONTINUE: {
  997. if (debugger)
  998. debugger->debug_continue();
  999. } break;
  1000. case WINDOW_MOVE_UP: {
  1001. if (tab_container->get_current_tab() > 0) {
  1002. tab_container->move_child(current, tab_container->get_current_tab() - 1);
  1003. tab_container->set_current_tab(tab_container->get_current_tab() - 1);
  1004. _update_script_names();
  1005. }
  1006. } break;
  1007. case WINDOW_MOVE_DOWN: {
  1008. if (tab_container->get_current_tab() < tab_container->get_child_count() - 1) {
  1009. tab_container->move_child(current, tab_container->get_current_tab() + 1);
  1010. tab_container->set_current_tab(tab_container->get_current_tab() + 1);
  1011. _update_script_names();
  1012. }
  1013. } break;
  1014. default: {
  1015. if (p_option >= WINDOW_SELECT_BASE) {
  1016. tab_container->set_current_tab(p_option - WINDOW_SELECT_BASE);
  1017. _update_script_names();
  1018. }
  1019. }
  1020. }
  1021. } else {
  1022. EditorHelp *help = Object::cast_to<EditorHelp>(tab_container->get_current_tab_control());
  1023. if (help) {
  1024. switch (p_option) {
  1025. case HELP_SEARCH_FIND: {
  1026. help->popup_search();
  1027. } break;
  1028. case HELP_SEARCH_FIND_NEXT: {
  1029. help->search_again();
  1030. } break;
  1031. case FILE_CLOSE: {
  1032. _close_current_tab();
  1033. } break;
  1034. case CLOSE_DOCS: {
  1035. _close_docs_tab();
  1036. } break;
  1037. case CLOSE_OTHER_TABS: {
  1038. _close_other_tabs();
  1039. } break;
  1040. case CLOSE_ALL: {
  1041. _close_all_tabs();
  1042. } break;
  1043. case WINDOW_MOVE_UP: {
  1044. if (tab_container->get_current_tab() > 0) {
  1045. tab_container->move_child(help, tab_container->get_current_tab() - 1);
  1046. tab_container->set_current_tab(tab_container->get_current_tab() - 1);
  1047. _update_script_names();
  1048. }
  1049. } break;
  1050. case WINDOW_MOVE_DOWN: {
  1051. if (tab_container->get_current_tab() < tab_container->get_child_count() - 1) {
  1052. tab_container->move_child(help, tab_container->get_current_tab() + 1);
  1053. tab_container->set_current_tab(tab_container->get_current_tab() + 1);
  1054. _update_script_names();
  1055. }
  1056. } break;
  1057. }
  1058. }
  1059. }
  1060. }
  1061. void ScriptEditor::_theme_option(int p_option) {
  1062. switch (p_option) {
  1063. case THEME_IMPORT: {
  1064. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  1065. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1066. file_dialog_option = THEME_IMPORT;
  1067. file_dialog->clear_filters();
  1068. file_dialog->add_filter("*.tet");
  1069. file_dialog->popup_centered_ratio();
  1070. file_dialog->set_title(TTR("Import Theme"));
  1071. } break;
  1072. case THEME_RELOAD: {
  1073. EditorSettings::get_singleton()->load_text_editor_theme();
  1074. } break;
  1075. case THEME_SAVE: {
  1076. if (EditorSettings::get_singleton()->is_default_text_editor_theme()) {
  1077. ScriptEditor::_show_save_theme_as_dialog();
  1078. } else if (!EditorSettings::get_singleton()->save_text_editor_theme()) {
  1079. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  1080. }
  1081. } break;
  1082. case THEME_SAVE_AS: {
  1083. ScriptEditor::_show_save_theme_as_dialog();
  1084. } break;
  1085. }
  1086. }
  1087. void ScriptEditor::_show_save_theme_as_dialog() {
  1088. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  1089. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1090. file_dialog_option = THEME_SAVE_AS;
  1091. file_dialog->clear_filters();
  1092. file_dialog->add_filter("*.tet");
  1093. file_dialog->set_current_path(EditorSettings::get_singleton()->get_text_editor_themes_dir().plus_file(EditorSettings::get_singleton()->get("text_editor/theme/color_theme")));
  1094. file_dialog->popup_centered_ratio();
  1095. file_dialog->set_title(TTR("Save Theme As..."));
  1096. }
  1097. void ScriptEditor::_tab_changed(int p_which) {
  1098. ensure_select_current();
  1099. }
  1100. void ScriptEditor::_notification(int p_what) {
  1101. switch (p_what) {
  1102. case NOTIFICATION_ENTER_TREE: {
  1103. editor->connect("play_pressed", this, "_editor_play");
  1104. editor->connect("pause_pressed", this, "_editor_pause");
  1105. editor->connect("stop_pressed", this, "_editor_stop");
  1106. editor->connect("script_add_function_request", this, "_add_callback");
  1107. editor->connect("resource_saved", this, "_res_saved_callback");
  1108. script_list->connect("item_selected", this, "_script_selected");
  1109. members_overview->connect("item_selected", this, "_members_overview_selected");
  1110. help_overview->connect("item_selected", this, "_help_overview_selected");
  1111. script_split->connect("dragged", this, "_script_split_dragged");
  1112. autosave_timer->connect("timeout", this, "_autosave_scripts");
  1113. {
  1114. float autosave_time = EditorSettings::get_singleton()->get("text_editor/files/autosave_interval_secs");
  1115. if (autosave_time > 0) {
  1116. autosave_timer->set_wait_time(autosave_time);
  1117. autosave_timer->start();
  1118. } else {
  1119. autosave_timer->stop();
  1120. }
  1121. }
  1122. EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
  1123. help_search->set_icon(get_icon("HelpSearch", "EditorIcons"));
  1124. site_search->set_icon(get_icon("Instance", "EditorIcons"));
  1125. request_docs->set_icon(get_icon("Issue", "EditorIcons"));
  1126. script_forward->set_icon(get_icon("Forward", "EditorIcons"));
  1127. script_back->set_icon(get_icon("Back", "EditorIcons"));
  1128. members_overview_alphabeta_sort_button->set_icon(get_icon("Sort", "EditorIcons"));
  1129. filter_scripts->set_right_icon(get_icon("Search", "EditorIcons"));
  1130. filter_methods->set_right_icon(get_icon("Search", "EditorIcons"));
  1131. } break;
  1132. case NOTIFICATION_READY: {
  1133. get_tree()->connect("tree_changed", this, "_tree_changed");
  1134. editor->get_inspector_dock()->connect("request_help", this, "_request_help");
  1135. editor->connect("request_help_search", this, "_help_search");
  1136. } break;
  1137. case NOTIFICATION_EXIT_TREE: {
  1138. editor->disconnect("play_pressed", this, "_editor_play");
  1139. editor->disconnect("pause_pressed", this, "_editor_pause");
  1140. editor->disconnect("stop_pressed", this, "_editor_stop");
  1141. } break;
  1142. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  1143. _test_script_times_on_disk();
  1144. _update_modified_scripts_for_external_editor();
  1145. } break;
  1146. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1147. help_search->set_icon(get_icon("HelpSearch", "EditorIcons"));
  1148. site_search->set_icon(get_icon("Instance", "EditorIcons"));
  1149. script_forward->set_icon(get_icon("Forward", "EditorIcons"));
  1150. script_back->set_icon(get_icon("Back", "EditorIcons"));
  1151. members_overview_alphabeta_sort_button->set_icon(get_icon("Sort", "EditorIcons"));
  1152. filename->add_style_override("normal", editor->get_gui_base()->get_stylebox("normal", "LineEdit"));
  1153. recent_scripts->set_as_minsize();
  1154. } break;
  1155. case CanvasItem::NOTIFICATION_VISIBILITY_CHANGED: {
  1156. if (is_visible()) {
  1157. find_in_files_button->show();
  1158. } else {
  1159. if (find_in_files->is_visible_in_tree()) {
  1160. editor->hide_bottom_panel();
  1161. }
  1162. find_in_files_button->hide();
  1163. }
  1164. } break;
  1165. default:
  1166. break;
  1167. }
  1168. }
  1169. bool ScriptEditor::can_take_away_focus() const {
  1170. ScriptEditorBase *current = _get_current_editor();
  1171. if (current)
  1172. return current->can_lose_focus_on_node_selection();
  1173. else
  1174. return true;
  1175. }
  1176. void ScriptEditor::close_builtin_scripts_from_scene(const String &p_scene) {
  1177. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1178. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1179. if (se) {
  1180. Ref<Script> script = se->get_edited_resource();
  1181. if (script == NULL || !script.is_valid())
  1182. continue;
  1183. if (script->get_path().find("::") != -1 && script->get_path().begins_with(p_scene)) { //is an internal script and belongs to scene being closed
  1184. _close_tab(i);
  1185. i--;
  1186. }
  1187. }
  1188. }
  1189. }
  1190. void ScriptEditor::edited_scene_changed() {
  1191. _update_modified_scripts_for_external_editor();
  1192. }
  1193. void ScriptEditor::notify_script_close(const Ref<Script> &p_script) {
  1194. emit_signal("script_close", p_script);
  1195. }
  1196. void ScriptEditor::notify_script_changed(const Ref<Script> &p_script) {
  1197. emit_signal("editor_script_changed", p_script);
  1198. }
  1199. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  1200. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1201. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1202. if (!se)
  1203. continue;
  1204. Ref<Script> script = se->get_edited_resource();
  1205. if (script == NULL) {
  1206. continue;
  1207. }
  1208. List<int> bpoints;
  1209. se->get_breakpoints(&bpoints);
  1210. String base = script->get_path();
  1211. ERR_CONTINUE(base.begins_with("local://") || base == "");
  1212. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  1213. p_breakpoints->push_back(base + ":" + itos(E->get() + 1));
  1214. }
  1215. }
  1216. }
  1217. void ScriptEditor::ensure_focus_current() {
  1218. if (!is_inside_tree())
  1219. return;
  1220. ScriptEditorBase *current = _get_current_editor();
  1221. if (current)
  1222. current->ensure_focus();
  1223. }
  1224. void ScriptEditor::_members_overview_selected(int p_idx) {
  1225. ScriptEditorBase *se = _get_current_editor();
  1226. if (!se) {
  1227. return;
  1228. }
  1229. // Go to the member's line and reset the cursor column. We can't change scroll_position
  1230. // directly until we have gone to the line first, since code might be folded.
  1231. se->goto_line(members_overview->get_item_metadata(p_idx));
  1232. Dictionary state = se->get_edit_state();
  1233. state["column"] = 0;
  1234. state["scroll_position"] = members_overview->get_item_metadata(p_idx);
  1235. se->set_edit_state(state);
  1236. }
  1237. void ScriptEditor::_help_overview_selected(int p_idx) {
  1238. Node *current = tab_container->get_child(tab_container->get_current_tab());
  1239. EditorHelp *se = Object::cast_to<EditorHelp>(current);
  1240. if (!se) {
  1241. return;
  1242. }
  1243. se->scroll_to_section(help_overview->get_item_metadata(p_idx));
  1244. }
  1245. void ScriptEditor::_script_selected(int p_idx) {
  1246. grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing
  1247. _go_to_tab(script_list->get_item_metadata(p_idx));
  1248. grab_focus_block = false;
  1249. }
  1250. void ScriptEditor::ensure_select_current() {
  1251. if (tab_container->get_child_count() && tab_container->get_current_tab() >= 0) {
  1252. ScriptEditorBase *se = _get_current_editor();
  1253. if (se) {
  1254. if (!grab_focus_block && is_visible_in_tree())
  1255. se->ensure_focus();
  1256. }
  1257. }
  1258. _update_selected_editor_menu();
  1259. }
  1260. void ScriptEditor::_find_scripts(Node *p_base, Node *p_current, Set<Ref<Script> > &used) {
  1261. if (p_current != p_base && p_current->get_owner() != p_base)
  1262. return;
  1263. if (p_current->get_script_instance()) {
  1264. Ref<Script> scr = p_current->get_script();
  1265. if (scr.is_valid())
  1266. used.insert(scr);
  1267. }
  1268. for (int i = 0; i < p_current->get_child_count(); i++) {
  1269. _find_scripts(p_base, p_current->get_child(i), used);
  1270. }
  1271. }
  1272. struct _ScriptEditorItemData {
  1273. String name;
  1274. String sort_key;
  1275. Ref<Texture> icon;
  1276. int index;
  1277. String tooltip;
  1278. bool used;
  1279. int category;
  1280. Node *ref;
  1281. bool operator<(const _ScriptEditorItemData &id) const {
  1282. if (category == id.category) {
  1283. if (sort_key == id.sort_key) {
  1284. return index < id.index;
  1285. } else {
  1286. return sort_key < id.sort_key;
  1287. }
  1288. } else {
  1289. return category < id.category;
  1290. }
  1291. }
  1292. };
  1293. void ScriptEditor::_update_members_overview_visibility() {
  1294. ScriptEditorBase *se = _get_current_editor();
  1295. if (!se) {
  1296. members_overview_alphabeta_sort_button->set_visible(false);
  1297. members_overview->set_visible(false);
  1298. overview_vbox->set_visible(false);
  1299. return;
  1300. }
  1301. if (members_overview_enabled && se->show_members_overview()) {
  1302. members_overview_alphabeta_sort_button->set_visible(true);
  1303. members_overview->set_visible(true);
  1304. overview_vbox->set_visible(true);
  1305. } else {
  1306. members_overview_alphabeta_sort_button->set_visible(false);
  1307. members_overview->set_visible(false);
  1308. overview_vbox->set_visible(false);
  1309. }
  1310. }
  1311. void ScriptEditor::_toggle_members_overview_alpha_sort(bool p_alphabetic_sort) {
  1312. EditorSettings::get_singleton()->set("text_editor/tools/sort_members_outline_alphabetically", p_alphabetic_sort);
  1313. _update_members_overview();
  1314. }
  1315. void ScriptEditor::_update_members_overview() {
  1316. members_overview->clear();
  1317. ScriptEditorBase *se = _get_current_editor();
  1318. if (!se) {
  1319. return;
  1320. }
  1321. Vector<String> functions = se->get_functions();
  1322. if (EditorSettings::get_singleton()->get("text_editor/tools/sort_members_outline_alphabetically")) {
  1323. functions.sort();
  1324. }
  1325. for (int i = 0; i < functions.size(); i++) {
  1326. String filter = filter_methods->get_text();
  1327. String name = functions[i].get_slice(":", 0);
  1328. if (filter == "" || filter.is_subsequence_ofi(name)) {
  1329. members_overview->add_item(name);
  1330. members_overview->set_item_metadata(members_overview->get_item_count() - 1, functions[i].get_slice(":", 1).to_int() - 1);
  1331. }
  1332. }
  1333. String path = se->get_edited_resource()->get_path();
  1334. bool built_in = !path.is_resource_file();
  1335. String name = built_in ? path.get_file() : se->get_name();
  1336. filename->set_text(name);
  1337. }
  1338. void ScriptEditor::_update_help_overview_visibility() {
  1339. int selected = tab_container->get_current_tab();
  1340. if (selected < 0 || selected >= tab_container->get_child_count()) {
  1341. help_overview->set_visible(false);
  1342. return;
  1343. }
  1344. Node *current = tab_container->get_child(tab_container->get_current_tab());
  1345. EditorHelp *se = Object::cast_to<EditorHelp>(current);
  1346. if (!se) {
  1347. help_overview->set_visible(false);
  1348. return;
  1349. }
  1350. if (help_overview_enabled) {
  1351. members_overview_alphabeta_sort_button->set_visible(false);
  1352. help_overview->set_visible(true);
  1353. overview_vbox->set_visible(true);
  1354. filename->set_text(se->get_name());
  1355. } else {
  1356. help_overview->set_visible(false);
  1357. overview_vbox->set_visible(false);
  1358. }
  1359. }
  1360. void ScriptEditor::_update_help_overview() {
  1361. help_overview->clear();
  1362. int selected = tab_container->get_current_tab();
  1363. if (selected < 0 || selected >= tab_container->get_child_count())
  1364. return;
  1365. Node *current = tab_container->get_child(tab_container->get_current_tab());
  1366. EditorHelp *se = Object::cast_to<EditorHelp>(current);
  1367. if (!se) {
  1368. return;
  1369. }
  1370. Vector<Pair<String, int> > sections = se->get_sections();
  1371. for (int i = 0; i < sections.size(); i++) {
  1372. help_overview->add_item(sections[i].first);
  1373. help_overview->set_item_metadata(i, sections[i].second);
  1374. }
  1375. }
  1376. void ScriptEditor::_update_script_colors() {
  1377. bool script_temperature_enabled = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_enabled");
  1378. bool highlight_current = EditorSettings::get_singleton()->get("text_editor/open_scripts/highlight_current_script");
  1379. int hist_size = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_history_size");
  1380. Color hot_color = get_color("accent_color", "Editor");
  1381. Color cold_color = get_color("font_color", "Editor");
  1382. for (int i = 0; i < script_list->get_item_count(); i++) {
  1383. int c = script_list->get_item_metadata(i);
  1384. Node *n = tab_container->get_child(c);
  1385. if (!n)
  1386. continue;
  1387. script_list->set_item_custom_bg_color(i, Color(0, 0, 0, 0));
  1388. bool current = tab_container->get_current_tab() == c;
  1389. if (current && highlight_current) {
  1390. script_list->set_item_custom_bg_color(i, EditorSettings::get_singleton()->get("text_editor/open_scripts/current_script_background_color"));
  1391. } else if (script_temperature_enabled) {
  1392. if (!n->has_meta("__editor_pass")) {
  1393. continue;
  1394. }
  1395. int pass = n->get_meta("__editor_pass");
  1396. int h = edit_pass - pass;
  1397. if (h > hist_size) {
  1398. continue;
  1399. }
  1400. int non_zero_hist_size = (hist_size == 0) ? 1 : hist_size;
  1401. float v = Math::ease((edit_pass - pass) / float(non_zero_hist_size), 0.4);
  1402. script_list->set_item_custom_fg_color(i, hot_color.linear_interpolate(cold_color, v));
  1403. }
  1404. }
  1405. }
  1406. void ScriptEditor::_update_script_names() {
  1407. if (restoring_layout)
  1408. return;
  1409. Set<Ref<Script> > used;
  1410. Node *edited = EditorNode::get_singleton()->get_edited_scene();
  1411. if (edited) {
  1412. _find_scripts(edited, edited, used);
  1413. }
  1414. script_list->clear();
  1415. bool split_script_help = EditorSettings::get_singleton()->get("text_editor/open_scripts/group_help_pages");
  1416. ScriptSortBy sort_by = (ScriptSortBy)(int)EditorSettings::get_singleton()->get("text_editor/open_scripts/sort_scripts_by");
  1417. ScriptListName display_as = (ScriptListName)(int)EditorSettings::get_singleton()->get("text_editor/open_scripts/list_script_names_as");
  1418. Vector<_ScriptEditorItemData> sedata;
  1419. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1420. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1421. if (se) {
  1422. Ref<Texture> icon = se->get_icon();
  1423. String path = se->get_edited_resource()->get_path();
  1424. bool built_in = !path.is_resource_file();
  1425. String name;
  1426. if (built_in) {
  1427. name = path.get_file();
  1428. String resource_name = se->get_edited_resource()->get_name();
  1429. if (resource_name != "") {
  1430. name = name.substr(0, name.find("::", 0) + 2) + resource_name;
  1431. }
  1432. } else {
  1433. name = se->get_name();
  1434. }
  1435. _ScriptEditorItemData sd;
  1436. sd.icon = icon;
  1437. sd.name = name;
  1438. sd.tooltip = path;
  1439. sd.index = i;
  1440. sd.used = used.has(se->get_edited_resource());
  1441. sd.category = 0;
  1442. sd.ref = se;
  1443. switch (sort_by) {
  1444. case SORT_BY_NAME: {
  1445. sd.sort_key = name.to_lower();
  1446. } break;
  1447. case SORT_BY_PATH: {
  1448. sd.sort_key = path;
  1449. } break;
  1450. case SORT_BY_NONE: {
  1451. sd.sort_key = "";
  1452. } break;
  1453. }
  1454. switch (display_as) {
  1455. case DISPLAY_NAME: {
  1456. sd.name = name;
  1457. } break;
  1458. case DISPLAY_DIR_AND_NAME: {
  1459. if (!path.get_base_dir().get_file().empty()) {
  1460. sd.name = path.get_base_dir().get_file().plus_file(name);
  1461. } else {
  1462. sd.name = name;
  1463. }
  1464. } break;
  1465. case DISPLAY_FULL_PATH: {
  1466. sd.name = path;
  1467. } break;
  1468. }
  1469. sedata.push_back(sd);
  1470. }
  1471. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_child(i));
  1472. if (eh) {
  1473. String name = eh->get_class();
  1474. Ref<Texture> icon = get_icon("Help", "EditorIcons");
  1475. String tooltip = vformat(TTR("%s Class Reference"), name);
  1476. _ScriptEditorItemData sd;
  1477. sd.icon = icon;
  1478. sd.name = name;
  1479. sd.sort_key = name.to_lower();
  1480. sd.tooltip = tooltip;
  1481. sd.index = i;
  1482. sd.used = false;
  1483. sd.category = split_script_help ? 1 : 0;
  1484. sd.ref = eh;
  1485. sedata.push_back(sd);
  1486. }
  1487. }
  1488. if (_sort_list_on_update && !sedata.empty()) {
  1489. sedata.sort();
  1490. // change actual order of tab_container so that the order can be rearranged by user
  1491. int cur_tab = tab_container->get_current_tab();
  1492. int prev_tab = tab_container->get_previous_tab();
  1493. int new_cur_tab = -1;
  1494. int new_prev_tab = -1;
  1495. for (int i = 0; i < sedata.size(); i++) {
  1496. tab_container->move_child(sedata[i].ref, i);
  1497. if (new_prev_tab == -1 && sedata[i].index == prev_tab) {
  1498. new_prev_tab = i;
  1499. }
  1500. if (new_cur_tab == -1 && sedata[i].index == cur_tab) {
  1501. new_cur_tab = i;
  1502. }
  1503. }
  1504. tab_container->set_current_tab(new_prev_tab);
  1505. tab_container->set_current_tab(new_cur_tab);
  1506. _sort_list_on_update = false;
  1507. }
  1508. Vector<_ScriptEditorItemData> sedata_filtered;
  1509. for (int i = 0; i < sedata.size(); i++) {
  1510. String filter = filter_scripts->get_text();
  1511. if (filter == "" || filter.is_subsequence_ofi(sedata[i].name)) {
  1512. sedata_filtered.push_back(sedata[i]);
  1513. }
  1514. }
  1515. for (int i = 0; i < sedata_filtered.size(); i++) {
  1516. script_list->add_item(sedata_filtered[i].name, sedata_filtered[i].icon);
  1517. int index = script_list->get_item_count() - 1;
  1518. script_list->set_item_tooltip(index, sedata_filtered[i].tooltip);
  1519. script_list->set_item_metadata(index, sedata_filtered[i].index); /* Saving as metadata the script's index in the tab container and not the filtered one */
  1520. if (sedata_filtered[i].used) {
  1521. script_list->set_item_custom_bg_color(index, Color(88 / 255.0, 88 / 255.0, 60 / 255.0));
  1522. }
  1523. if (tab_container->get_current_tab() == sedata_filtered[i].index) {
  1524. script_list->select(index);
  1525. script_name_label->set_text(sedata_filtered[i].name);
  1526. script_icon->set_texture(sedata_filtered[i].icon);
  1527. }
  1528. }
  1529. if (!waiting_update_names) {
  1530. _update_members_overview();
  1531. _update_help_overview();
  1532. } else {
  1533. waiting_update_names = false;
  1534. }
  1535. _update_members_overview_visibility();
  1536. _update_help_overview_visibility();
  1537. _update_script_colors();
  1538. file_menu->get_popup()->set_item_disabled(file_menu->get_popup()->get_item_index(FILE_REOPEN_CLOSED), previous_scripts.empty());
  1539. }
  1540. void ScriptEditor::_update_script_connections() {
  1541. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1542. ScriptTextEditor *ste = Object::cast_to<ScriptTextEditor>(tab_container->get_child(i));
  1543. if (!ste) {
  1544. continue;
  1545. }
  1546. ste->_update_connected_methods();
  1547. }
  1548. }
  1549. Ref<TextFile> ScriptEditor::_load_text_file(const String &p_path, Error *r_error) {
  1550. if (r_error) {
  1551. *r_error = ERR_FILE_CANT_OPEN;
  1552. }
  1553. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1554. String path = ResourceLoader::path_remap(local_path);
  1555. TextFile *text_file = memnew(TextFile);
  1556. Ref<TextFile> text_res(text_file);
  1557. Error err = text_file->load_text(path);
  1558. ERR_FAIL_COND_V(err != OK, RES());
  1559. text_file->set_file_path(local_path);
  1560. text_file->set_path(local_path, true);
  1561. if (ResourceLoader::get_timestamp_on_load()) {
  1562. text_file->set_last_modified_time(FileAccess::get_modified_time(path));
  1563. }
  1564. if (r_error) {
  1565. *r_error = OK;
  1566. }
  1567. return text_res;
  1568. }
  1569. Error ScriptEditor::_save_text_file(Ref<TextFile> p_text_file, const String &p_path) {
  1570. Ref<TextFile> sqscr = p_text_file;
  1571. ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER);
  1572. String source = sqscr->get_text();
  1573. Error err;
  1574. FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1575. if (err) {
  1576. ERR_FAIL_COND_V(err, err);
  1577. }
  1578. file->store_string(source);
  1579. if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
  1580. memdelete(file);
  1581. return ERR_CANT_CREATE;
  1582. }
  1583. file->close();
  1584. memdelete(file);
  1585. if (ResourceSaver::get_timestamp_on_save()) {
  1586. p_text_file->set_last_modified_time(FileAccess::get_modified_time(p_path));
  1587. }
  1588. _res_saved_callback(sqscr);
  1589. return OK;
  1590. }
  1591. bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_grab_focus) {
  1592. if (p_resource.is_null())
  1593. return false;
  1594. Ref<Script> script = p_resource;
  1595. // refuse to open built-in if scene is not loaded
  1596. // see if already has it
  1597. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/files/open_dominant_script_on_scene_change");
  1598. const bool should_open = open_dominant || !EditorNode::get_singleton()->is_changing_scene();
  1599. if (script != NULL && script->get_language()->overrides_external_editor()) {
  1600. if (should_open) {
  1601. Error err = script->get_language()->open_in_external_editor(script, p_line >= 0 ? p_line : 0, p_col);
  1602. if (err != OK)
  1603. ERR_PRINT("Couldn't open script in the overridden external text editor");
  1604. }
  1605. return false;
  1606. }
  1607. if ((debugger->get_dump_stack_script() != p_resource || debugger->get_debug_with_external_editor()) &&
  1608. p_resource->get_path().is_resource_file() &&
  1609. p_resource->get_class_name() != StringName("VisualScript") &&
  1610. bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor"))) {
  1611. String path = EditorSettings::get_singleton()->get("text_editor/external/exec_path");
  1612. String flags = EditorSettings::get_singleton()->get("text_editor/external/exec_flags");
  1613. List<String> args;
  1614. bool has_file_flag = false;
  1615. String script_path = ProjectSettings::get_singleton()->globalize_path(p_resource->get_path());
  1616. if (flags.size()) {
  1617. String project_path = ProjectSettings::get_singleton()->get_resource_path();
  1618. flags = flags.replacen("{line}", itos(p_line > 0 ? p_line : 0));
  1619. flags = flags.replacen("{col}", itos(p_col));
  1620. flags = flags.strip_edges().replace("\\\\", "\\");
  1621. int from = 0;
  1622. int num_chars = 0;
  1623. bool inside_quotes = false;
  1624. for (int i = 0; i < flags.size(); i++) {
  1625. if (flags[i] == '"' && (!i || flags[i - 1] != '\\')) {
  1626. if (!inside_quotes) {
  1627. from++;
  1628. }
  1629. inside_quotes = !inside_quotes;
  1630. } else if (flags[i] == '\0' || (!inside_quotes && flags[i] == ' ')) {
  1631. String arg = flags.substr(from, num_chars);
  1632. if (arg.find("{file}") != -1) {
  1633. has_file_flag = true;
  1634. }
  1635. // do path replacement here, else there will be issues with spaces and quotes
  1636. arg = arg.replacen("{project}", project_path);
  1637. arg = arg.replacen("{file}", script_path);
  1638. args.push_back(arg);
  1639. from = i + 1;
  1640. num_chars = 0;
  1641. } else {
  1642. num_chars++;
  1643. }
  1644. }
  1645. }
  1646. // Default to passing script path if no {file} flag is specified.
  1647. if (!has_file_flag) {
  1648. args.push_back(script_path);
  1649. }
  1650. Error err = OS::get_singleton()->execute(path, args, false);
  1651. if (err == OK)
  1652. return false;
  1653. WARN_PRINT("Couldn't open external text editor, using internal");
  1654. }
  1655. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1656. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1657. if (!se)
  1658. continue;
  1659. if ((script != NULL && se->get_edited_resource() == p_resource) || se->get_edited_resource()->get_path() == p_resource->get_path()) {
  1660. if (should_open) {
  1661. if (tab_container->get_current_tab() != i) {
  1662. _go_to_tab(i);
  1663. _update_script_names();
  1664. }
  1665. if (is_visible_in_tree())
  1666. se->ensure_focus();
  1667. if (p_line > 0) {
  1668. se->goto_line(p_line - 1);
  1669. }
  1670. }
  1671. _update_script_names();
  1672. script_list->ensure_current_is_visible();
  1673. return true;
  1674. }
  1675. }
  1676. // doesn't have it, make a new one
  1677. ScriptEditorBase *se = NULL;
  1678. for (int i = script_editor_func_count - 1; i >= 0; i--) {
  1679. se = script_editor_funcs[i](p_resource);
  1680. if (se)
  1681. break;
  1682. }
  1683. ERR_FAIL_COND_V(!se, false);
  1684. if (p_resource->get_class_name() != StringName("VisualScript")) {
  1685. bool highlighter_set = false;
  1686. for (int i = 0; i < syntax_highlighters_func_count; i++) {
  1687. SyntaxHighlighter *highlighter = syntax_highlighters_funcs[i]();
  1688. se->add_syntax_highlighter(highlighter);
  1689. if (script != NULL && !highlighter_set) {
  1690. List<String> languages = highlighter->get_supported_languages();
  1691. if (languages.find(script->get_language()->get_name())) {
  1692. se->set_syntax_highlighter(highlighter);
  1693. highlighter_set = true;
  1694. }
  1695. }
  1696. }
  1697. }
  1698. tab_container->add_child(se);
  1699. se->set_edited_resource(p_resource);
  1700. se->set_tooltip_request_func("_get_debug_tooltip", this);
  1701. if (se->get_edit_menu()) {
  1702. se->get_edit_menu()->hide();
  1703. menu_hb->add_child(se->get_edit_menu());
  1704. menu_hb->move_child(se->get_edit_menu(), 1);
  1705. }
  1706. if (p_grab_focus) {
  1707. _go_to_tab(tab_container->get_tab_count() - 1);
  1708. }
  1709. _sort_list_on_update = true;
  1710. _update_script_names();
  1711. _save_layout();
  1712. se->connect("name_changed", this, "_update_script_names");
  1713. se->connect("edited_script_changed", this, "_script_changed");
  1714. se->connect("request_help", this, "_help_search");
  1715. se->connect("request_open_script_at_line", this, "_goto_script_line");
  1716. se->connect("go_to_help", this, "_help_class_goto");
  1717. se->connect("request_save_history", this, "_save_history");
  1718. se->connect("search_in_files_requested", this, "_on_find_in_files_requested");
  1719. //test for modification, maybe the script was not edited but was loaded
  1720. _test_script_times_on_disk(p_resource);
  1721. _update_modified_scripts_for_external_editor(p_resource);
  1722. if (p_line > 0) {
  1723. se->goto_line(p_line - 1);
  1724. }
  1725. notify_script_changed(p_resource);
  1726. _add_recent_script(p_resource->get_path());
  1727. return true;
  1728. }
  1729. void ScriptEditor::save_all_scripts() {
  1730. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1731. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1732. if (!se)
  1733. continue;
  1734. if (convert_indent_on_save) {
  1735. if (use_space_indentation) {
  1736. se->convert_indent_to_spaces();
  1737. } else {
  1738. se->convert_indent_to_tabs();
  1739. }
  1740. }
  1741. if (trim_trailing_whitespace_on_save) {
  1742. se->trim_trailing_whitespace();
  1743. }
  1744. se->insert_final_newline();
  1745. if (!se->is_unsaved())
  1746. continue;
  1747. RES edited_res = se->get_edited_resource();
  1748. if (edited_res.is_valid()) {
  1749. se->apply_code();
  1750. }
  1751. if (edited_res->get_path() != "" && edited_res->get_path().find("local://") == -1 && edited_res->get_path().find("::") == -1) {
  1752. Ref<TextFile> text_file = edited_res;
  1753. if (text_file != NULL) {
  1754. _save_text_file(text_file, text_file->get_path());
  1755. continue;
  1756. }
  1757. editor->save_resource(edited_res); //external script, save it
  1758. }
  1759. }
  1760. _update_script_names();
  1761. EditorFileSystem::get_singleton()->update_script_classes();
  1762. }
  1763. void ScriptEditor::apply_scripts() const {
  1764. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1765. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1766. if (!se)
  1767. continue;
  1768. se->apply_code();
  1769. }
  1770. }
  1771. void ScriptEditor::open_script_create_dialog(const String &p_base_name, const String &p_base_path) {
  1772. _menu_option(FILE_NEW);
  1773. script_create_dialog->config(p_base_name, p_base_path);
  1774. }
  1775. void ScriptEditor::_editor_play() {
  1776. debugger->start();
  1777. debug_menu->get_popup()->grab_focus();
  1778. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1779. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  1780. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false);
  1781. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  1782. }
  1783. void ScriptEditor::_editor_pause() {
  1784. }
  1785. void ScriptEditor::_editor_stop() {
  1786. debugger->stop();
  1787. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1788. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  1789. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true);
  1790. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  1791. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1792. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1793. if (!se) {
  1794. continue;
  1795. }
  1796. se->set_debugger_active(false);
  1797. }
  1798. }
  1799. void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const PoolStringArray &p_args) {
  1800. ERR_FAIL_COND(!p_obj);
  1801. Ref<Script> script = p_obj->get_script();
  1802. ERR_FAIL_COND(!script.is_valid());
  1803. editor->push_item(script.ptr());
  1804. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1805. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1806. if (!se)
  1807. continue;
  1808. if (se->get_edited_resource() != script)
  1809. continue;
  1810. se->add_callback(p_function, p_args);
  1811. _go_to_tab(i);
  1812. script_list->select(script_list->find_metadata(i));
  1813. break;
  1814. }
  1815. }
  1816. void ScriptEditor::_save_layout() {
  1817. if (restoring_layout) {
  1818. return;
  1819. }
  1820. editor->save_layout();
  1821. }
  1822. void ScriptEditor::_editor_settings_changed() {
  1823. trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/files/trim_trailing_whitespace_on_save");
  1824. convert_indent_on_save = EditorSettings::get_singleton()->get("text_editor/indent/convert_indent_on_save");
  1825. use_space_indentation = EditorSettings::get_singleton()->get("text_editor/indent/type");
  1826. members_overview_enabled = EditorSettings::get_singleton()->get("text_editor/open_scripts/show_members_overview");
  1827. help_overview_enabled = EditorSettings::get_singleton()->get("text_editor/help/show_help_index");
  1828. _update_members_overview_visibility();
  1829. _update_help_overview_visibility();
  1830. float autosave_time = EditorSettings::get_singleton()->get("text_editor/files/autosave_interval_secs");
  1831. if (autosave_time > 0) {
  1832. autosave_timer->set_wait_time(autosave_time);
  1833. autosave_timer->start();
  1834. } else {
  1835. autosave_timer->stop();
  1836. }
  1837. if (current_theme == "") {
  1838. current_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
  1839. } else if (current_theme != EditorSettings::get_singleton()->get("text_editor/theme/color_theme")) {
  1840. current_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
  1841. EditorSettings::get_singleton()->load_text_editor_theme();
  1842. }
  1843. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1844. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1845. if (!se)
  1846. continue;
  1847. se->update_settings();
  1848. }
  1849. _update_script_colors();
  1850. _update_script_names();
  1851. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/files/auto_reload_and_parse_scripts_on_save", true));
  1852. }
  1853. void ScriptEditor::_autosave_scripts() {
  1854. save_all_scripts();
  1855. }
  1856. void ScriptEditor::_tree_changed() {
  1857. if (waiting_update_names)
  1858. return;
  1859. waiting_update_names = true;
  1860. call_deferred("_update_script_names");
  1861. call_deferred("_update_script_connections");
  1862. }
  1863. void ScriptEditor::_script_split_dragged(float) {
  1864. _save_layout();
  1865. }
  1866. Variant ScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1867. if (tab_container->get_child_count() == 0)
  1868. return Variant();
  1869. Node *cur_node = tab_container->get_child(tab_container->get_current_tab());
  1870. HBoxContainer *drag_preview = memnew(HBoxContainer);
  1871. String preview_name = "";
  1872. Ref<Texture> preview_icon;
  1873. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(cur_node);
  1874. if (se) {
  1875. preview_name = se->get_name();
  1876. preview_icon = se->get_icon();
  1877. }
  1878. EditorHelp *eh = Object::cast_to<EditorHelp>(cur_node);
  1879. if (eh) {
  1880. preview_name = eh->get_class();
  1881. preview_icon = get_icon("Help", "EditorIcons");
  1882. }
  1883. if (!preview_icon.is_null()) {
  1884. TextureRect *tf = memnew(TextureRect);
  1885. tf->set_texture(preview_icon);
  1886. drag_preview->add_child(tf);
  1887. }
  1888. Label *label = memnew(Label(preview_name));
  1889. drag_preview->add_child(label);
  1890. set_drag_preview(drag_preview);
  1891. Dictionary drag_data;
  1892. drag_data["type"] = "script_list_element"; // using a custom type because node caused problems when dragging to scene tree
  1893. drag_data["script_list_element"] = cur_node;
  1894. return drag_data;
  1895. }
  1896. bool ScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1897. Dictionary d = p_data;
  1898. if (!d.has("type"))
  1899. return false;
  1900. if (String(d["type"]) == "script_list_element") {
  1901. Node *node = d["script_list_element"];
  1902. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  1903. if (se) {
  1904. return true;
  1905. }
  1906. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  1907. if (eh) {
  1908. return true;
  1909. }
  1910. }
  1911. if (String(d["type"]) == "nodes") {
  1912. Array nodes = d["nodes"];
  1913. if (nodes.size() == 0)
  1914. return false;
  1915. Node *node = get_node((nodes[0]));
  1916. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  1917. if (se) {
  1918. return true;
  1919. }
  1920. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  1921. if (eh) {
  1922. return true;
  1923. }
  1924. }
  1925. if (String(d["type"]) == "files") {
  1926. Vector<String> files = d["files"];
  1927. if (files.size() == 0)
  1928. return false; //weird
  1929. for (int i = 0; i < files.size(); i++) {
  1930. String file = files[i];
  1931. if (file == "" || !FileAccess::exists(file))
  1932. continue;
  1933. Ref<Script> scr = ResourceLoader::load(file);
  1934. if (scr.is_valid()) {
  1935. return true;
  1936. }
  1937. }
  1938. return true;
  1939. }
  1940. return false;
  1941. }
  1942. void ScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1943. if (!can_drop_data_fw(p_point, p_data, p_from))
  1944. return;
  1945. Dictionary d = p_data;
  1946. if (!d.has("type"))
  1947. return;
  1948. if (String(d["type"]) == "script_list_element") {
  1949. Node *node = d["script_list_element"];
  1950. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  1951. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  1952. if (se || eh) {
  1953. int new_index = script_list->get_item_metadata(script_list->get_item_at_position(p_point));
  1954. tab_container->move_child(node, new_index);
  1955. tab_container->set_current_tab(new_index);
  1956. _update_script_names();
  1957. }
  1958. }
  1959. if (String(d["type"]) == "nodes") {
  1960. Array nodes = d["nodes"];
  1961. if (nodes.size() == 0)
  1962. return;
  1963. Node *node = get_node(nodes[0]);
  1964. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  1965. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  1966. if (se || eh) {
  1967. int new_index = script_list->get_item_metadata(script_list->get_item_at_position(p_point));
  1968. tab_container->move_child(node, new_index);
  1969. tab_container->set_current_tab(new_index);
  1970. _update_script_names();
  1971. }
  1972. }
  1973. if (String(d["type"]) == "files") {
  1974. Vector<String> files = d["files"];
  1975. int new_index = script_list->get_item_metadata(script_list->get_item_at_position(p_point));
  1976. int num_tabs_before = tab_container->get_child_count();
  1977. for (int i = 0; i < files.size(); i++) {
  1978. String file = files[i];
  1979. if (file == "" || !FileAccess::exists(file))
  1980. continue;
  1981. Ref<Script> scr = ResourceLoader::load(file);
  1982. if (scr.is_valid()) {
  1983. edit(scr);
  1984. if (tab_container->get_child_count() > num_tabs_before) {
  1985. tab_container->move_child(tab_container->get_child(tab_container->get_child_count() - 1), new_index);
  1986. num_tabs_before = tab_container->get_child_count();
  1987. } else { /* Maybe script was already open */
  1988. tab_container->move_child(tab_container->get_child(tab_container->get_current_tab()), new_index);
  1989. }
  1990. }
  1991. }
  1992. tab_container->set_current_tab(new_index);
  1993. _update_script_names();
  1994. }
  1995. }
  1996. void ScriptEditor::_unhandled_input(const Ref<InputEvent> &p_event) {
  1997. if (!is_visible_in_tree() || !p_event->is_pressed() || p_event->is_echo())
  1998. return;
  1999. if (ED_IS_SHORTCUT("script_editor/next_script", p_event)) {
  2000. int next_tab = script_list->get_current() + 1;
  2001. next_tab %= script_list->get_item_count();
  2002. _go_to_tab(script_list->get_item_metadata(next_tab));
  2003. _update_script_names();
  2004. }
  2005. if (ED_IS_SHORTCUT("script_editor/prev_script", p_event)) {
  2006. int next_tab = script_list->get_current() - 1;
  2007. next_tab = next_tab >= 0 ? next_tab : script_list->get_item_count() - 1;
  2008. _go_to_tab(script_list->get_item_metadata(next_tab));
  2009. _update_script_names();
  2010. }
  2011. if (ED_IS_SHORTCUT("script_editor/window_move_up", p_event)) {
  2012. _menu_option(WINDOW_MOVE_UP);
  2013. }
  2014. if (ED_IS_SHORTCUT("script_editor/window_move_down", p_event)) {
  2015. _menu_option(WINDOW_MOVE_DOWN);
  2016. }
  2017. }
  2018. void ScriptEditor::_script_list_gui_input(const Ref<InputEvent> &ev) {
  2019. Ref<InputEventMouseButton> mb = ev;
  2020. if (mb.is_valid() && mb->is_pressed()) {
  2021. switch (mb->get_button_index()) {
  2022. case BUTTON_MIDDLE: {
  2023. // Right-click selects automatically; middle-click does not.
  2024. int idx = script_list->get_item_at_position(mb->get_position(), true);
  2025. if (idx >= 0) {
  2026. script_list->select(idx);
  2027. _script_selected(idx);
  2028. _menu_option(FILE_CLOSE);
  2029. }
  2030. } break;
  2031. case BUTTON_RIGHT: {
  2032. _make_script_list_context_menu();
  2033. } break;
  2034. }
  2035. }
  2036. }
  2037. void ScriptEditor::_make_script_list_context_menu() {
  2038. context_menu->clear();
  2039. int selected = tab_container->get_current_tab();
  2040. if (selected < 0 || selected >= tab_container->get_child_count())
  2041. return;
  2042. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(selected));
  2043. if (se) {
  2044. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save"), FILE_SAVE);
  2045. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save_as"), FILE_SAVE_AS);
  2046. }
  2047. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_file"), FILE_CLOSE);
  2048. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_all"), CLOSE_ALL);
  2049. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_other_tabs"), CLOSE_OTHER_TABS);
  2050. context_menu->add_separator();
  2051. if (se) {
  2052. Ref<Script> scr = se->get_edited_resource();
  2053. if (scr != NULL) {
  2054. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/reload_script_soft"), FILE_TOOL_RELOAD_SOFT);
  2055. if (!scr.is_null() && scr->is_tool()) {
  2056. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/run_file"), FILE_RUN);
  2057. context_menu->add_separator();
  2058. }
  2059. }
  2060. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/copy_path"), FILE_COPY_PATH);
  2061. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/show_in_file_system"), SHOW_IN_FILE_SYSTEM);
  2062. context_menu->add_separator();
  2063. }
  2064. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_up"), WINDOW_MOVE_UP);
  2065. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_down"), WINDOW_MOVE_DOWN);
  2066. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_sort"), WINDOW_SORT);
  2067. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_scripts_panel"), TOGGLE_SCRIPTS_PANEL);
  2068. context_menu->set_position(get_global_transform().xform(get_local_mouse_position()));
  2069. context_menu->set_size(Vector2(1, 1));
  2070. context_menu->popup();
  2071. }
  2072. void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
  2073. if (!bool(EDITOR_DEF("text_editor/files/restore_scripts_on_load", true))) {
  2074. return;
  2075. }
  2076. if (!p_layout->has_section_key("ScriptEditor", "open_scripts") && !p_layout->has_section_key("ScriptEditor", "open_help"))
  2077. return;
  2078. Array scripts = p_layout->get_value("ScriptEditor", "open_scripts");
  2079. Array helps;
  2080. if (p_layout->has_section_key("ScriptEditor", "open_help"))
  2081. helps = p_layout->get_value("ScriptEditor", "open_help");
  2082. restoring_layout = true;
  2083. List<String> extensions;
  2084. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  2085. for (int i = 0; i < scripts.size(); i++) {
  2086. String path = scripts[i];
  2087. Dictionary script_info = scripts[i];
  2088. if (!script_info.empty()) {
  2089. path = script_info["path"];
  2090. }
  2091. if (!FileAccess::exists(path))
  2092. continue;
  2093. if (extensions.find(path.get_extension())) {
  2094. Ref<Script> scr = ResourceLoader::load(path);
  2095. if (!scr.is_valid()) {
  2096. continue;
  2097. }
  2098. if (!edit(scr)) {
  2099. continue;
  2100. }
  2101. } else {
  2102. Error error;
  2103. Ref<TextFile> text_file = _load_text_file(path, &error);
  2104. if (error != OK || !text_file.is_valid()) {
  2105. continue;
  2106. }
  2107. if (!edit(text_file)) {
  2108. continue;
  2109. }
  2110. }
  2111. if (!script_info.empty()) {
  2112. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(tab_container->get_tab_count() - 1));
  2113. if (se) {
  2114. se->set_edit_state(script_info["state"]);
  2115. }
  2116. }
  2117. }
  2118. for (int i = 0; i < helps.size(); i++) {
  2119. String path = helps[i];
  2120. if (path == "") { // invalid, skip
  2121. continue;
  2122. }
  2123. _help_class_open(path);
  2124. }
  2125. for (int i = 0; i < tab_container->get_child_count(); i++) {
  2126. tab_container->get_child(i)->set_meta("__editor_pass", Variant());
  2127. }
  2128. if (p_layout->has_section_key("ScriptEditor", "split_offset")) {
  2129. script_split->set_split_offset(p_layout->get_value("ScriptEditor", "split_offset"));
  2130. }
  2131. restoring_layout = false;
  2132. _update_script_names();
  2133. }
  2134. void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
  2135. Array scripts;
  2136. Array helps;
  2137. for (int i = 0; i < tab_container->get_child_count(); i++) {
  2138. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  2139. if (se) {
  2140. String path = se->get_edited_resource()->get_path();
  2141. if (!path.is_resource_file())
  2142. continue;
  2143. Dictionary script_info;
  2144. script_info["path"] = path;
  2145. script_info["state"] = se->get_edit_state();
  2146. scripts.push_back(script_info);
  2147. }
  2148. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_child(i));
  2149. if (eh) {
  2150. helps.push_back(eh->get_class());
  2151. }
  2152. }
  2153. p_layout->set_value("ScriptEditor", "open_scripts", scripts);
  2154. p_layout->set_value("ScriptEditor", "open_help", helps);
  2155. p_layout->set_value("ScriptEditor", "split_offset", script_split->get_split_offset());
  2156. }
  2157. void ScriptEditor::_help_class_open(const String &p_class) {
  2158. if (p_class == "")
  2159. return;
  2160. for (int i = 0; i < tab_container->get_child_count(); i++) {
  2161. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_child(i));
  2162. if (eh && eh->get_class() == p_class) {
  2163. _go_to_tab(i);
  2164. _update_script_names();
  2165. return;
  2166. }
  2167. }
  2168. EditorHelp *eh = memnew(EditorHelp);
  2169. eh->set_name(p_class);
  2170. tab_container->add_child(eh);
  2171. _go_to_tab(tab_container->get_tab_count() - 1);
  2172. eh->go_to_class(p_class, 0);
  2173. eh->connect("go_to_help", this, "_help_class_goto");
  2174. _add_recent_script(p_class);
  2175. _sort_list_on_update = true;
  2176. _update_script_names();
  2177. _save_layout();
  2178. }
  2179. void ScriptEditor::_help_class_goto(const String &p_desc) {
  2180. String cname = p_desc.get_slice(":", 1);
  2181. for (int i = 0; i < tab_container->get_child_count(); i++) {
  2182. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_child(i));
  2183. if (eh && eh->get_class() == cname) {
  2184. _go_to_tab(i);
  2185. eh->go_to_help(p_desc);
  2186. _update_script_names();
  2187. return;
  2188. }
  2189. }
  2190. EditorHelp *eh = memnew(EditorHelp);
  2191. eh->set_name(cname);
  2192. tab_container->add_child(eh);
  2193. _go_to_tab(tab_container->get_tab_count() - 1);
  2194. eh->go_to_help(p_desc);
  2195. eh->connect("go_to_help", this, "_help_class_goto");
  2196. _add_recent_script(eh->get_class());
  2197. _sort_list_on_update = true;
  2198. _update_script_names();
  2199. _save_layout();
  2200. }
  2201. void ScriptEditor::_update_selected_editor_menu() {
  2202. for (int i = 0; i < tab_container->get_child_count(); i++) {
  2203. bool current = tab_container->get_current_tab() == i;
  2204. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  2205. if (se && se->get_edit_menu()) {
  2206. if (current)
  2207. se->get_edit_menu()->show();
  2208. else
  2209. se->get_edit_menu()->hide();
  2210. }
  2211. }
  2212. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_current_tab_control());
  2213. script_search_menu->get_popup()->clear();
  2214. if (eh) {
  2215. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find..."), KEY_MASK_CMD | KEY_F), HELP_SEARCH_FIND);
  2216. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), HELP_SEARCH_FIND_NEXT);
  2217. script_search_menu->get_popup()->add_separator();
  2218. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_in_files", TTR("Find in Files"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_F), SEARCH_IN_FILES);
  2219. script_search_menu->show();
  2220. } else {
  2221. if (tab_container->get_child_count() == 0) {
  2222. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_in_files", TTR("Find in Files"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_F), SEARCH_IN_FILES);
  2223. script_search_menu->show();
  2224. } else {
  2225. script_search_menu->hide();
  2226. }
  2227. }
  2228. }
  2229. void ScriptEditor::_update_history_pos(int p_new_pos) {
  2230. Node *n = tab_container->get_current_tab_control();
  2231. if (Object::cast_to<ScriptEditorBase>(n)) {
  2232. history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_edit_state();
  2233. }
  2234. if (Object::cast_to<EditorHelp>(n)) {
  2235. history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();
  2236. }
  2237. history_pos = p_new_pos;
  2238. tab_container->set_current_tab(history[history_pos].control->get_index());
  2239. n = history[history_pos].control;
  2240. if (Object::cast_to<ScriptEditorBase>(n)) {
  2241. Object::cast_to<ScriptEditorBase>(n)->set_edit_state(history[history_pos].state);
  2242. Object::cast_to<ScriptEditorBase>(n)->ensure_focus();
  2243. Ref<Script> script = Object::cast_to<ScriptEditorBase>(n)->get_edited_resource();
  2244. if (script != NULL) {
  2245. notify_script_changed(script);
  2246. }
  2247. }
  2248. if (Object::cast_to<EditorHelp>(n)) {
  2249. Object::cast_to<EditorHelp>(n)->set_scroll(history[history_pos].state);
  2250. Object::cast_to<EditorHelp>(n)->set_focused();
  2251. }
  2252. n->set_meta("__editor_pass", ++edit_pass);
  2253. _update_script_names();
  2254. _update_history_arrows();
  2255. _update_selected_editor_menu();
  2256. }
  2257. void ScriptEditor::_history_forward() {
  2258. if (history_pos < history.size() - 1) {
  2259. _update_history_pos(history_pos + 1);
  2260. }
  2261. }
  2262. void ScriptEditor::_history_back() {
  2263. if (history_pos > 0) {
  2264. _update_history_pos(history_pos - 1);
  2265. }
  2266. }
  2267. Vector<Ref<Script> > ScriptEditor::get_open_scripts() const {
  2268. Vector<Ref<Script> > out_scripts = Vector<Ref<Script> >();
  2269. for (int i = 0; i < tab_container->get_child_count(); i++) {
  2270. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  2271. if (!se)
  2272. continue;
  2273. Ref<Script> script = se->get_edited_resource();
  2274. if (script != NULL) {
  2275. out_scripts.push_back(script);
  2276. }
  2277. }
  2278. return out_scripts;
  2279. }
  2280. void ScriptEditor::set_scene_root_script(Ref<Script> p_script) {
  2281. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/files/open_dominant_script_on_scene_change");
  2282. if (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor")))
  2283. return;
  2284. if (open_dominant && p_script.is_valid()) {
  2285. edit(p_script);
  2286. }
  2287. }
  2288. bool ScriptEditor::script_goto_method(Ref<Script> p_script, const String &p_method) {
  2289. int line = p_script->get_member_line(p_method);
  2290. if (line == -1)
  2291. return false;
  2292. return edit(p_script, line, 0);
  2293. }
  2294. void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) {
  2295. auto_reload_running_scripts = p_enabled;
  2296. }
  2297. void ScriptEditor::_help_search(String p_text) {
  2298. help_search_dialog->popup_dialog(p_text);
  2299. }
  2300. void ScriptEditor::_open_script_request(const String &p_path) {
  2301. Ref<Script> script = ResourceLoader::load(p_path);
  2302. if (script.is_valid()) {
  2303. script_editor->edit(script, false);
  2304. return;
  2305. }
  2306. Error err;
  2307. Ref<TextFile> text_file = script_editor->_load_text_file(p_path, &err);
  2308. if (text_file.is_valid()) {
  2309. script_editor->edit(text_file, false);
  2310. return;
  2311. }
  2312. }
  2313. int ScriptEditor::syntax_highlighters_func_count = 0;
  2314. CreateSyntaxHighlighterFunc ScriptEditor::syntax_highlighters_funcs[ScriptEditor::SYNTAX_HIGHLIGHTER_FUNC_MAX];
  2315. void ScriptEditor::register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func) {
  2316. ERR_FAIL_COND(syntax_highlighters_func_count == SYNTAX_HIGHLIGHTER_FUNC_MAX);
  2317. syntax_highlighters_funcs[syntax_highlighters_func_count++] = p_func;
  2318. }
  2319. int ScriptEditor::script_editor_func_count = 0;
  2320. CreateScriptEditorFunc ScriptEditor::script_editor_funcs[ScriptEditor::SCRIPT_EDITOR_FUNC_MAX];
  2321. void ScriptEditor::register_create_script_editor_function(CreateScriptEditorFunc p_func) {
  2322. ERR_FAIL_COND(script_editor_func_count == SCRIPT_EDITOR_FUNC_MAX);
  2323. script_editor_funcs[script_editor_func_count++] = p_func;
  2324. }
  2325. void ScriptEditor::_script_changed() {
  2326. NodeDock::singleton->update_lists();
  2327. }
  2328. void ScriptEditor::_on_find_in_files_requested(String text) {
  2329. find_in_files_dialog->set_search_text(text);
  2330. find_in_files_dialog->popup_centered_minsize();
  2331. }
  2332. void ScriptEditor::_on_find_in_files_result_selected(String fpath, int line_number, int begin, int end) {
  2333. RES res = ResourceLoader::load(fpath);
  2334. if (fpath.get_extension() == "shader") {
  2335. ShaderEditorPlugin *shader_editor = Object::cast_to<ShaderEditorPlugin>(EditorNode::get_singleton()->get_editor_data().get_editor("Shader"));
  2336. shader_editor->edit(res.ptr());
  2337. shader_editor->make_visible(true);
  2338. shader_editor->get_shader_editor()->goto_line_selection(line_number - 1, begin, end);
  2339. } else {
  2340. edit(res);
  2341. ScriptTextEditor *ste = Object::cast_to<ScriptTextEditor>(_get_current_editor());
  2342. if (ste) {
  2343. ste->goto_line_selection(line_number - 1, begin, end);
  2344. }
  2345. }
  2346. }
  2347. void ScriptEditor::_start_find_in_files(bool with_replace) {
  2348. FindInFiles *f = find_in_files->get_finder();
  2349. f->set_search_text(find_in_files_dialog->get_search_text());
  2350. f->set_match_case(find_in_files_dialog->is_match_case());
  2351. f->set_whole_words(find_in_files_dialog->is_whole_words());
  2352. f->set_folder(find_in_files_dialog->get_folder());
  2353. f->set_filter(find_in_files_dialog->get_filter());
  2354. find_in_files->set_with_replace(with_replace);
  2355. find_in_files->start_search();
  2356. editor->make_bottom_panel_item_visible(find_in_files);
  2357. }
  2358. void ScriptEditor::_on_find_in_files_modified_files(PoolStringArray paths) {
  2359. _test_script_times_on_disk();
  2360. _update_modified_scripts_for_external_editor();
  2361. }
  2362. void ScriptEditor::_filter_scripts_text_changed(const String &p_newtext) {
  2363. _update_script_names();
  2364. }
  2365. void ScriptEditor::_filter_methods_text_changed(const String &p_newtext) {
  2366. _update_members_overview();
  2367. }
  2368. void ScriptEditor::_bind_methods() {
  2369. ClassDB::bind_method("_file_dialog_action", &ScriptEditor::_file_dialog_action);
  2370. ClassDB::bind_method("_tab_changed", &ScriptEditor::_tab_changed);
  2371. ClassDB::bind_method("_menu_option", &ScriptEditor::_menu_option);
  2372. ClassDB::bind_method("_close_current_tab", &ScriptEditor::_close_current_tab);
  2373. ClassDB::bind_method("_close_discard_current_tab", &ScriptEditor::_close_discard_current_tab);
  2374. ClassDB::bind_method("_close_docs_tab", &ScriptEditor::_close_docs_tab);
  2375. ClassDB::bind_method("_close_all_tabs", &ScriptEditor::_close_all_tabs);
  2376. ClassDB::bind_method("_close_other_tabs", &ScriptEditor::_close_other_tabs);
  2377. ClassDB::bind_method("_open_recent_script", &ScriptEditor::_open_recent_script);
  2378. ClassDB::bind_method("_theme_option", &ScriptEditor::_theme_option);
  2379. ClassDB::bind_method("_editor_play", &ScriptEditor::_editor_play);
  2380. ClassDB::bind_method("_editor_pause", &ScriptEditor::_editor_pause);
  2381. ClassDB::bind_method("_editor_stop", &ScriptEditor::_editor_stop);
  2382. ClassDB::bind_method("_add_callback", &ScriptEditor::_add_callback);
  2383. ClassDB::bind_method("_reload_scripts", &ScriptEditor::_reload_scripts);
  2384. ClassDB::bind_method("_resave_scripts", &ScriptEditor::_resave_scripts);
  2385. ClassDB::bind_method("_res_saved_callback", &ScriptEditor::_res_saved_callback);
  2386. ClassDB::bind_method("_goto_script_line", &ScriptEditor::_goto_script_line);
  2387. ClassDB::bind_method("_goto_script_line2", &ScriptEditor::_goto_script_line2);
  2388. ClassDB::bind_method("_set_execution", &ScriptEditor::_set_execution);
  2389. ClassDB::bind_method("_clear_execution", &ScriptEditor::_clear_execution);
  2390. ClassDB::bind_method("_help_search", &ScriptEditor::_help_search);
  2391. ClassDB::bind_method("_save_history", &ScriptEditor::_save_history);
  2392. ClassDB::bind_method("_copy_script_path", &ScriptEditor::_copy_script_path);
  2393. ClassDB::bind_method("_breaked", &ScriptEditor::_breaked);
  2394. ClassDB::bind_method("_show_debugger", &ScriptEditor::_show_debugger);
  2395. ClassDB::bind_method("_get_debug_tooltip", &ScriptEditor::_get_debug_tooltip);
  2396. ClassDB::bind_method("_autosave_scripts", &ScriptEditor::_autosave_scripts);
  2397. ClassDB::bind_method("_editor_settings_changed", &ScriptEditor::_editor_settings_changed);
  2398. ClassDB::bind_method("_update_script_names", &ScriptEditor::_update_script_names);
  2399. ClassDB::bind_method("_update_script_connections", &ScriptEditor::_update_script_connections);
  2400. ClassDB::bind_method("_tree_changed", &ScriptEditor::_tree_changed);
  2401. ClassDB::bind_method("_members_overview_selected", &ScriptEditor::_members_overview_selected);
  2402. ClassDB::bind_method("_help_overview_selected", &ScriptEditor::_help_overview_selected);
  2403. ClassDB::bind_method("_script_selected", &ScriptEditor::_script_selected);
  2404. ClassDB::bind_method("_script_created", &ScriptEditor::_script_created);
  2405. ClassDB::bind_method("_script_split_dragged", &ScriptEditor::_script_split_dragged);
  2406. ClassDB::bind_method("_help_class_open", &ScriptEditor::_help_class_open);
  2407. ClassDB::bind_method("_help_class_goto", &ScriptEditor::_help_class_goto);
  2408. ClassDB::bind_method("_request_help", &ScriptEditor::_help_class_open);
  2409. ClassDB::bind_method("_history_forward", &ScriptEditor::_history_forward);
  2410. ClassDB::bind_method("_history_back", &ScriptEditor::_history_back);
  2411. ClassDB::bind_method("_live_auto_reload_running_scripts", &ScriptEditor::_live_auto_reload_running_scripts);
  2412. ClassDB::bind_method("_unhandled_input", &ScriptEditor::_unhandled_input);
  2413. ClassDB::bind_method("_script_list_gui_input", &ScriptEditor::_script_list_gui_input);
  2414. ClassDB::bind_method("_toggle_members_overview_alpha_sort", &ScriptEditor::_toggle_members_overview_alpha_sort);
  2415. ClassDB::bind_method("_update_members_overview", &ScriptEditor::_update_members_overview);
  2416. ClassDB::bind_method("_script_changed", &ScriptEditor::_script_changed);
  2417. ClassDB::bind_method("_filter_scripts_text_changed", &ScriptEditor::_filter_scripts_text_changed);
  2418. ClassDB::bind_method("_filter_methods_text_changed", &ScriptEditor::_filter_methods_text_changed);
  2419. ClassDB::bind_method("_update_recent_scripts", &ScriptEditor::_update_recent_scripts);
  2420. ClassDB::bind_method("_on_find_in_files_requested", &ScriptEditor::_on_find_in_files_requested);
  2421. ClassDB::bind_method("_start_find_in_files", &ScriptEditor::_start_find_in_files);
  2422. ClassDB::bind_method("_on_find_in_files_result_selected", &ScriptEditor::_on_find_in_files_result_selected);
  2423. ClassDB::bind_method("_on_find_in_files_modified_files", &ScriptEditor::_on_find_in_files_modified_files);
  2424. ClassDB::bind_method(D_METHOD("get_drag_data_fw", "point", "from"), &ScriptEditor::get_drag_data_fw);
  2425. ClassDB::bind_method(D_METHOD("can_drop_data_fw", "point", "data", "from"), &ScriptEditor::can_drop_data_fw);
  2426. ClassDB::bind_method(D_METHOD("drop_data_fw", "point", "data", "from"), &ScriptEditor::drop_data_fw);
  2427. ClassDB::bind_method(D_METHOD("goto_line", "line_number"), &ScriptEditor::_goto_script_line2);
  2428. ClassDB::bind_method(D_METHOD("get_current_script"), &ScriptEditor::_get_current_script);
  2429. ClassDB::bind_method(D_METHOD("get_open_scripts"), &ScriptEditor::_get_open_scripts);
  2430. ClassDB::bind_method(D_METHOD("open_script_create_dialog", "base_name", "base_path"), &ScriptEditor::open_script_create_dialog);
  2431. ADD_SIGNAL(MethodInfo("editor_script_changed", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script")));
  2432. ADD_SIGNAL(MethodInfo("script_close", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script")));
  2433. }
  2434. ScriptEditor::ScriptEditor(EditorNode *p_editor) {
  2435. current_theme = "";
  2436. completion_cache = memnew(EditorScriptCodeCompletionCache);
  2437. restoring_layout = false;
  2438. waiting_update_names = false;
  2439. pending_auto_reload = false;
  2440. auto_reload_running_scripts = true;
  2441. members_overview_enabled = EditorSettings::get_singleton()->get("text_editor/open_scripts/show_members_overview");
  2442. help_overview_enabled = EditorSettings::get_singleton()->get("text_editor/help/show_help_index");
  2443. editor = p_editor;
  2444. VBoxContainer *main_container = memnew(VBoxContainer);
  2445. add_child(main_container);
  2446. menu_hb = memnew(HBoxContainer);
  2447. main_container->add_child(menu_hb);
  2448. script_split = memnew(HSplitContainer);
  2449. main_container->add_child(script_split);
  2450. script_split->set_v_size_flags(SIZE_EXPAND_FILL);
  2451. list_split = memnew(VSplitContainer);
  2452. script_split->add_child(list_split);
  2453. list_split->set_v_size_flags(SIZE_EXPAND_FILL);
  2454. scripts_vbox = memnew(VBoxContainer);
  2455. scripts_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  2456. list_split->add_child(scripts_vbox);
  2457. filter_scripts = memnew(LineEdit);
  2458. filter_scripts->set_placeholder(TTR("Filter scripts"));
  2459. filter_scripts->set_clear_button_enabled(true);
  2460. filter_scripts->connect("text_changed", this, "_filter_scripts_text_changed");
  2461. scripts_vbox->add_child(filter_scripts);
  2462. script_list = memnew(ItemList);
  2463. scripts_vbox->add_child(script_list);
  2464. script_list->set_custom_minimum_size(Size2(150, 90) * EDSCALE); //need to give a bit of limit to avoid it from disappearing
  2465. script_list->set_v_size_flags(SIZE_EXPAND_FILL);
  2466. script_split->set_split_offset(140);
  2467. _sort_list_on_update = true;
  2468. script_list->connect("gui_input", this, "_script_list_gui_input", varray(), CONNECT_DEFERRED);
  2469. script_list->set_allow_rmb_select(true);
  2470. script_list->set_drag_forwarding(this);
  2471. context_menu = memnew(PopupMenu);
  2472. add_child(context_menu);
  2473. context_menu->connect("id_pressed", this, "_menu_option");
  2474. context_menu->set_hide_on_window_lose_focus(true);
  2475. overview_vbox = memnew(VBoxContainer);
  2476. overview_vbox->set_custom_minimum_size(Size2(0, 90));
  2477. overview_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  2478. list_split->add_child(overview_vbox);
  2479. buttons_hbox = memnew(HBoxContainer);
  2480. overview_vbox->add_child(buttons_hbox);
  2481. filename = memnew(Label);
  2482. filename->set_clip_text(true);
  2483. filename->set_h_size_flags(SIZE_EXPAND_FILL);
  2484. filename->add_style_override("normal", EditorNode::get_singleton()->get_gui_base()->get_stylebox("normal", "LineEdit"));
  2485. buttons_hbox->add_child(filename);
  2486. members_overview_alphabeta_sort_button = memnew(ToolButton);
  2487. members_overview_alphabeta_sort_button->set_tooltip(TTR("Toggle alphabetical sorting of the method list."));
  2488. members_overview_alphabeta_sort_button->set_toggle_mode(true);
  2489. members_overview_alphabeta_sort_button->set_pressed(EditorSettings::get_singleton()->get("text_editor/tools/sort_members_outline_alphabetically"));
  2490. members_overview_alphabeta_sort_button->connect("toggled", this, "_toggle_members_overview_alpha_sort");
  2491. buttons_hbox->add_child(members_overview_alphabeta_sort_button);
  2492. filter_methods = memnew(LineEdit);
  2493. filter_methods->set_placeholder(TTR("Filter methods"));
  2494. filter_methods->set_clear_button_enabled(true);
  2495. filter_methods->connect("text_changed", this, "_filter_methods_text_changed");
  2496. overview_vbox->add_child(filter_methods);
  2497. members_overview = memnew(ItemList);
  2498. overview_vbox->add_child(members_overview);
  2499. members_overview->set_allow_reselect(true);
  2500. members_overview->set_custom_minimum_size(Size2(0, 90) * EDSCALE); //need to give a bit of limit to avoid it from disappearing
  2501. members_overview->set_v_size_flags(SIZE_EXPAND_FILL);
  2502. members_overview->set_allow_rmb_select(true);
  2503. help_overview = memnew(ItemList);
  2504. overview_vbox->add_child(help_overview);
  2505. help_overview->set_allow_reselect(true);
  2506. help_overview->set_custom_minimum_size(Size2(0, 90) * EDSCALE); //need to give a bit of limit to avoid it from disappearing
  2507. help_overview->set_v_size_flags(SIZE_EXPAND_FILL);
  2508. tab_container = memnew(TabContainer);
  2509. tab_container->set_tabs_visible(false);
  2510. tab_container->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  2511. script_split->add_child(tab_container);
  2512. tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
  2513. ED_SHORTCUT("script_editor/window_sort", TTR("Sort"));
  2514. ED_SHORTCUT("script_editor/window_move_up", TTR("Move Up"), KEY_MASK_SHIFT | KEY_MASK_ALT | KEY_UP);
  2515. ED_SHORTCUT("script_editor/window_move_down", TTR("Move Down"), KEY_MASK_SHIFT | KEY_MASK_ALT | KEY_DOWN);
  2516. ED_SHORTCUT("script_editor/next_script", TTR("Next script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_PERIOD); // these should be KEY_GREATER and KEY_LESS but those don't work
  2517. ED_SHORTCUT("script_editor/prev_script", TTR("Previous script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_COLON);
  2518. set_process_unhandled_input(true);
  2519. file_menu = memnew(MenuButton);
  2520. menu_hb->add_child(file_menu);
  2521. file_menu->set_text(TTR("File"));
  2522. file_menu->set_switch_on_hover(true);
  2523. file_menu->get_popup()->set_hide_on_window_lose_focus(true);
  2524. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTR("New Script...")), FILE_NEW);
  2525. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new_textfile", TTR("New TextFile...")), FILE_NEW_TEXTFILE);
  2526. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open", TTR("Open...")), FILE_OPEN);
  2527. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reopen_closed_script", TTR("Reopen Closed Script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_T), FILE_REOPEN_CLOSED);
  2528. file_menu->get_popup()->add_submenu_item(TTR("Open Recent"), "RecentScripts", FILE_OPEN_RECENT);
  2529. recent_scripts = memnew(PopupMenu);
  2530. recent_scripts->set_name("RecentScripts");
  2531. file_menu->get_popup()->add_child(recent_scripts);
  2532. recent_scripts->connect("id_pressed", this, "_open_recent_script");
  2533. _update_recent_scripts();
  2534. file_menu->get_popup()->add_separator();
  2535. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save", TTR("Save"), KEY_MASK_ALT | KEY_MASK_CMD | KEY_S), FILE_SAVE);
  2536. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as", TTR("Save As...")), FILE_SAVE_AS);
  2537. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_all", TTR("Save All"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_MASK_ALT | KEY_S), FILE_SAVE_ALL);
  2538. file_menu->get_popup()->add_separator();
  2539. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_script_soft", TTR("Soft Reload Script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_R), FILE_TOOL_RELOAD_SOFT);
  2540. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy_path", TTR("Copy Script Path")), FILE_COPY_PATH);
  2541. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/show_in_file_system", TTR("Show in FileSystem")), SHOW_IN_FILE_SYSTEM);
  2542. file_menu->get_popup()->add_separator();
  2543. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_previous", TTR("History Previous"), KEY_MASK_ALT | KEY_LEFT), WINDOW_PREV);
  2544. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_next", TTR("History Next"), KEY_MASK_ALT | KEY_RIGHT), WINDOW_NEXT);
  2545. file_menu->get_popup()->add_separator();
  2546. file_menu->get_popup()->add_submenu_item(TTR("Theme"), "Theme", FILE_THEME);
  2547. theme_submenu = memnew(PopupMenu);
  2548. theme_submenu->set_name("Theme");
  2549. file_menu->get_popup()->add_child(theme_submenu);
  2550. theme_submenu->connect("id_pressed", this, "_theme_option");
  2551. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/import_theme", TTR("Import Theme...")), THEME_IMPORT);
  2552. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/reload_theme", TTR("Reload Theme")), THEME_RELOAD);
  2553. theme_submenu->add_separator();
  2554. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/save_theme", TTR("Save Theme")), THEME_SAVE);
  2555. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as", TTR("Save Theme As...")), THEME_SAVE_AS);
  2556. file_menu->get_popup()->add_separator();
  2557. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_docs", TTR("Close Docs")), CLOSE_DOCS);
  2558. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file", TTR("Close"), KEY_MASK_CMD | KEY_W), FILE_CLOSE);
  2559. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_all", TTR("Close All")), CLOSE_ALL);
  2560. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_other_tabs", TTR("Close Other Tabs")), CLOSE_OTHER_TABS);
  2561. file_menu->get_popup()->add_separator();
  2562. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/run_file", TTR("Run"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_X), FILE_RUN);
  2563. file_menu->get_popup()->add_separator();
  2564. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_scripts_panel", TTR("Toggle Scripts Panel"), KEY_MASK_CMD | KEY_BACKSLASH), TOGGLE_SCRIPTS_PANEL);
  2565. file_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2566. script_search_menu = memnew(MenuButton);
  2567. menu_hb->add_child(script_search_menu);
  2568. script_search_menu->set_text(TTR("Search"));
  2569. script_search_menu->set_switch_on_hover(true);
  2570. script_search_menu->get_popup()->set_hide_on_window_lose_focus(true);
  2571. script_search_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2572. debug_menu = memnew(MenuButton);
  2573. menu_hb->add_child(debug_menu);
  2574. debug_menu->set_text(TTR("Debug"));
  2575. debug_menu->set_switch_on_hover(true);
  2576. debug_menu->get_popup()->set_hide_on_window_lose_focus(true);
  2577. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_over", TTR("Step Over"), KEY_F10), DEBUG_NEXT);
  2578. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_into", TTR("Step Into"), KEY_F11), DEBUG_STEP);
  2579. debug_menu->get_popup()->add_separator();
  2580. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/break", TTR("Break")), DEBUG_BREAK);
  2581. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/continue", TTR("Continue"), KEY_F12), DEBUG_CONTINUE);
  2582. debug_menu->get_popup()->add_separator();
  2583. //debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
  2584. debug_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("debugger/keep_debugger_open", TTR("Keep Debugger Open")), DEBUG_SHOW_KEEP_OPEN);
  2585. debug_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("debugger/debug_with_external_editor", TTR("Debug with External Editor")), DEBUG_WITH_EXTERNAL_EDITOR);
  2586. debug_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2587. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  2588. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  2589. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true);
  2590. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  2591. menu_hb->add_spacer();
  2592. script_icon = memnew(TextureRect);
  2593. menu_hb->add_child(script_icon);
  2594. script_name_label = memnew(Label);
  2595. menu_hb->add_child(script_name_label);
  2596. script_icon->hide();
  2597. script_name_label->hide();
  2598. menu_hb->add_spacer();
  2599. site_search = memnew(ToolButton);
  2600. site_search->set_text(TTR("Online Docs"));
  2601. site_search->connect("pressed", this, "_menu_option", varray(SEARCH_WEBSITE));
  2602. menu_hb->add_child(site_search);
  2603. site_search->set_tooltip(TTR("Open Godot online documentation."));
  2604. request_docs = memnew(ToolButton);
  2605. request_docs->set_text(TTR("Request Docs"));
  2606. request_docs->connect("pressed", this, "_menu_option", varray(REQUEST_DOCS));
  2607. menu_hb->add_child(request_docs);
  2608. request_docs->set_tooltip(TTR("Help improve the Godot documentation by giving feedback."));
  2609. help_search = memnew(ToolButton);
  2610. help_search->set_text(TTR("Search Help"));
  2611. help_search->connect("pressed", this, "_menu_option", varray(SEARCH_HELP));
  2612. menu_hb->add_child(help_search);
  2613. help_search->set_tooltip(TTR("Search the reference documentation."));
  2614. menu_hb->add_child(memnew(VSeparator));
  2615. script_back = memnew(ToolButton);
  2616. script_back->connect("pressed", this, "_history_back");
  2617. menu_hb->add_child(script_back);
  2618. script_back->set_disabled(true);
  2619. script_back->set_tooltip(TTR("Go to previous edited document."));
  2620. script_forward = memnew(ToolButton);
  2621. script_forward->connect("pressed", this, "_history_forward");
  2622. menu_hb->add_child(script_forward);
  2623. script_forward->set_disabled(true);
  2624. script_forward->set_tooltip(TTR("Go to next edited document."));
  2625. tab_container->connect("tab_changed", this, "_tab_changed");
  2626. erase_tab_confirm = memnew(ConfirmationDialog);
  2627. erase_tab_confirm->get_ok()->set_text(TTR("Save"));
  2628. erase_tab_confirm->add_button(TTR("Discard"), OS::get_singleton()->get_swap_ok_cancel(), "discard");
  2629. erase_tab_confirm->connect("confirmed", this, "_close_current_tab");
  2630. erase_tab_confirm->connect("custom_action", this, "_close_discard_current_tab");
  2631. add_child(erase_tab_confirm);
  2632. script_create_dialog = memnew(ScriptCreateDialog);
  2633. script_create_dialog->set_title(TTR("Create Script"));
  2634. add_child(script_create_dialog);
  2635. script_create_dialog->connect("script_created", this, "_script_created");
  2636. file_dialog_option = -1;
  2637. file_dialog = memnew(EditorFileDialog);
  2638. add_child(file_dialog);
  2639. file_dialog->connect("file_selected", this, "_file_dialog_action");
  2640. error_dialog = memnew(AcceptDialog);
  2641. add_child(error_dialog);
  2642. debugger = memnew(ScriptEditorDebugger(editor));
  2643. debugger->connect("goto_script_line", this, "_goto_script_line");
  2644. debugger->connect("set_execution", this, "_set_execution");
  2645. debugger->connect("clear_execution", this, "_clear_execution");
  2646. debugger->connect("show_debugger", this, "_show_debugger");
  2647. disk_changed = memnew(ConfirmationDialog);
  2648. {
  2649. VBoxContainer *vbc = memnew(VBoxContainer);
  2650. disk_changed->add_child(vbc);
  2651. Label *dl = memnew(Label);
  2652. dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?:"));
  2653. vbc->add_child(dl);
  2654. disk_changed_list = memnew(Tree);
  2655. vbc->add_child(disk_changed_list);
  2656. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  2657. disk_changed->connect("confirmed", this, "_reload_scripts");
  2658. disk_changed->get_ok()->set_text(TTR("Reload"));
  2659. disk_changed->add_button(TTR("Resave"), !OS::get_singleton()->get_swap_ok_cancel(), "resave");
  2660. disk_changed->connect("custom_action", this, "_resave_scripts");
  2661. }
  2662. add_child(disk_changed);
  2663. script_editor = this;
  2664. Button *db = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Debugger"), debugger);
  2665. debugger->set_tool_button(db);
  2666. debugger->connect("breaked", this, "_breaked");
  2667. autosave_timer = memnew(Timer);
  2668. autosave_timer->set_one_shot(false);
  2669. add_child(autosave_timer);
  2670. grab_focus_block = false;
  2671. help_search_dialog = memnew(EditorHelpSearch);
  2672. add_child(help_search_dialog);
  2673. help_search_dialog->connect("go_to_help", this, "_help_class_goto");
  2674. find_in_files_dialog = memnew(FindInFilesDialog);
  2675. find_in_files_dialog->connect(FindInFilesDialog::SIGNAL_FIND_REQUESTED, this, "_start_find_in_files", varray(false));
  2676. find_in_files_dialog->connect(FindInFilesDialog::SIGNAL_REPLACE_REQUESTED, this, "_start_find_in_files", varray(true));
  2677. add_child(find_in_files_dialog);
  2678. find_in_files = memnew(FindInFilesPanel);
  2679. find_in_files_button = editor->add_bottom_panel_item(TTR("Search Results"), find_in_files);
  2680. find_in_files->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  2681. find_in_files->connect(FindInFilesPanel::SIGNAL_RESULT_SELECTED, this, "_on_find_in_files_result_selected");
  2682. find_in_files->connect(FindInFilesPanel::SIGNAL_FILES_MODIFIED, this, "_on_find_in_files_modified_files");
  2683. find_in_files->hide();
  2684. find_in_files_button->hide();
  2685. history_pos = -1;
  2686. //debugger_gui->hide();
  2687. edit_pass = 0;
  2688. trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/files/trim_trailing_whitespace_on_save");
  2689. convert_indent_on_save = EditorSettings::get_singleton()->get("text_editor/indent/convert_indent_on_save");
  2690. use_space_indentation = EditorSettings::get_singleton()->get("text_editor/indent/type");
  2691. ScriptServer::edit_request_func = _open_script_request;
  2692. add_style_override("panel", editor->get_gui_base()->get_stylebox("ScriptEditorPanel", "EditorStyles"));
  2693. tab_container->add_style_override("panel", editor->get_gui_base()->get_stylebox("ScriptEditor", "EditorStyles"));
  2694. }
  2695. ScriptEditor::~ScriptEditor() {
  2696. memdelete(completion_cache);
  2697. }
  2698. void ScriptEditorPlugin::edit(Object *p_object) {
  2699. if (Object::cast_to<Script>(p_object)) {
  2700. Script *p_script = Object::cast_to<Script>(p_object);
  2701. String scene_path = p_script->get_path().get_slice("::", 0);
  2702. if (_is_built_in_script(p_script) && !EditorNode::get_singleton()->is_scene_open(scene_path)) {
  2703. EditorNode::get_singleton()->load_scene(scene_path);
  2704. script_editor->call_deferred("edit", p_script);
  2705. } else {
  2706. script_editor->edit(p_script);
  2707. }
  2708. } else if (Object::cast_to<TextFile>(p_object)) {
  2709. script_editor->edit(Object::cast_to<TextFile>(p_object));
  2710. }
  2711. }
  2712. bool ScriptEditorPlugin::handles(Object *p_object) const {
  2713. if (Object::cast_to<TextFile>(p_object)) {
  2714. return true;
  2715. }
  2716. if (Object::cast_to<Script>(p_object)) {
  2717. return true;
  2718. }
  2719. return p_object->is_class("Script");
  2720. }
  2721. void ScriptEditorPlugin::make_visible(bool p_visible) {
  2722. if (p_visible) {
  2723. script_editor->show();
  2724. script_editor->set_process(true);
  2725. script_editor->ensure_select_current();
  2726. } else {
  2727. script_editor->hide();
  2728. script_editor->set_process(false);
  2729. }
  2730. }
  2731. void ScriptEditorPlugin::selected_notify() {
  2732. script_editor->ensure_select_current();
  2733. }
  2734. void ScriptEditorPlugin::save_external_data() {
  2735. script_editor->save_all_scripts();
  2736. }
  2737. void ScriptEditorPlugin::apply_changes() {
  2738. script_editor->apply_scripts();
  2739. }
  2740. void ScriptEditorPlugin::restore_global_state() {
  2741. }
  2742. void ScriptEditorPlugin::save_global_state() {
  2743. }
  2744. void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  2745. script_editor->set_window_layout(p_layout);
  2746. }
  2747. void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
  2748. script_editor->get_window_layout(p_layout);
  2749. }
  2750. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  2751. script_editor->get_breakpoints(p_breakpoints);
  2752. }
  2753. void ScriptEditorPlugin::edited_scene_changed() {
  2754. script_editor->edited_scene_changed();
  2755. }
  2756. ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
  2757. editor = p_node;
  2758. script_editor = memnew(ScriptEditor(p_node));
  2759. editor->get_viewport()->add_child(script_editor);
  2760. script_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2761. script_editor->hide();
  2762. EDITOR_DEF("text_editor/files/auto_reload_scripts_on_external_change", true);
  2763. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/files/auto_reload_and_parse_scripts_on_save", true));
  2764. EDITOR_DEF("text_editor/files/open_dominant_script_on_scene_change", true);
  2765. EDITOR_DEF("text_editor/external/use_external_editor", false);
  2766. EDITOR_DEF("text_editor/external/exec_path", "");
  2767. EDITOR_DEF("text_editor/open_scripts/script_temperature_enabled", true);
  2768. EDITOR_DEF("text_editor/open_scripts/highlight_current_script", true);
  2769. EDITOR_DEF("text_editor/open_scripts/script_temperature_history_size", 15);
  2770. EDITOR_DEF("text_editor/open_scripts/current_script_background_color", Color(1, 1, 1, 0.3));
  2771. EDITOR_DEF("text_editor/open_scripts/group_help_pages", true);
  2772. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/open_scripts/sort_scripts_by", PROPERTY_HINT_ENUM, "Name,Path,None"));
  2773. EDITOR_DEF("text_editor/open_scripts/sort_scripts_by", 0);
  2774. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/open_scripts/list_script_names_as", PROPERTY_HINT_ENUM, "Name,Parent Directory And Name,Full Path"));
  2775. EDITOR_DEF("text_editor/open_scripts/list_script_names_as", 0);
  2776. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "text_editor/external/exec_path", PROPERTY_HINT_GLOBAL_FILE));
  2777. EDITOR_DEF("text_editor/external/exec_flags", "{file}");
  2778. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "text_editor/external/exec_flags", PROPERTY_HINT_PLACEHOLDER_TEXT, "Call flags with placeholders: {project}, {file}, {col}, {line}."));
  2779. ED_SHORTCUT("script_editor/reopen_closed_script", TTR("Reopen Closed Script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_T);
  2780. ED_SHORTCUT("script_editor/open_recent", TTR("Open Recent"));
  2781. ED_SHORTCUT("script_editor/clear_recent", TTR("Clear Recent Files"));
  2782. }
  2783. ScriptEditorPlugin::~ScriptEditorPlugin() {
  2784. }