script_editor_plugin.cpp 102 KB

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