script_editor_plugin.cpp 102 KB

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