script_editor_plugin.cpp 95 KB

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