script_editor_plugin.cpp 102 KB

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