script_editor_plugin.cpp 103 KB

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