script_editor_plugin.cpp 103 KB

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