script_editor_plugin.cpp 102 KB

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