script_editor_plugin.cpp 92 KB

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