script_editor_plugin.cpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991
  1. /*************************************************************************/
  2. /* script_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "script_editor_plugin.h"
  30. #include "tools/editor/editor_settings.h"
  31. #include "io/resource_loader.h"
  32. #include "io/resource_saver.h"
  33. #include "os/keyboard.h"
  34. #include "os/os.h"
  35. #include "tools/editor/editor_node.h"
  36. #include "tools/editor/script_editor_debugger.h"
  37. #include "globals.h"
  38. #include "os/file_access.h"
  39. #include "scene/main/viewport.h"
  40. #include "os/keyboard.h"
  41. #include "os/input.h"
  42. /*** SCRIPT EDITOR ****/
  43. class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache {
  44. struct Cache {
  45. uint64_t time_loaded;
  46. RES cache;
  47. };
  48. Map<String,Cache> cached;
  49. public:
  50. uint64_t max_time_cache;
  51. int max_cache_size;
  52. void cleanup() {
  53. List< Map<String,Cache>::Element * > to_clean;
  54. Map<String,Cache>::Element *I=cached.front();
  55. while(I) {
  56. if ((OS::get_singleton()->get_ticks_msec()-I->get().time_loaded)>max_time_cache) {
  57. to_clean.push_back(I);
  58. }
  59. I=I->next();
  60. }
  61. while(to_clean.front()) {
  62. cached.erase(to_clean.front()->get());
  63. to_clean.pop_front();
  64. }
  65. }
  66. RES get_cached_resource(const String& p_path) {
  67. Map<String,Cache>::Element *E=cached.find(p_path);
  68. if (!E) {
  69. Cache c;
  70. c.cache=ResourceLoader::load(p_path);
  71. E=cached.insert(p_path,c);
  72. }
  73. E->get().time_loaded=OS::get_singleton()->get_ticks_msec();
  74. if (cached.size()>max_cache_size) {
  75. uint64_t older;
  76. Map<String,Cache>::Element *O=cached.front();
  77. older=O->get().time_loaded;
  78. Map<String,Cache>::Element *I=O;
  79. while(I) {
  80. if (I->get().time_loaded<older) {
  81. older = I->get().time_loaded;
  82. O=I;
  83. }
  84. I=I->next();
  85. }
  86. if (O!=E) {//should never heppane..
  87. cached.erase(O);
  88. }
  89. }
  90. return E->get().cache;
  91. }
  92. EditorScriptCodeCompletionCache() {
  93. max_cache_size=128;
  94. max_time_cache=5*60*1000; //minutes, five
  95. }
  96. };
  97. #define SORT_SCRIPT_LIST
  98. void ScriptEditorQuickOpen::popup(const Vector<String>& p_functions, bool p_dontclear) {
  99. popup_centered_ratio(0.6);
  100. if (p_dontclear)
  101. search_box->select_all();
  102. else
  103. search_box->clear();
  104. search_box->grab_focus();
  105. functions=p_functions;
  106. _update_search();
  107. }
  108. void ScriptEditorQuickOpen::_text_changed(const String& p_newtext) {
  109. _update_search();
  110. }
  111. void ScriptEditorQuickOpen::_sbox_input(const InputEvent& p_ie) {
  112. if (p_ie.type==InputEvent::KEY && (
  113. p_ie.key.scancode == KEY_UP ||
  114. p_ie.key.scancode == KEY_DOWN ||
  115. p_ie.key.scancode == KEY_PAGEUP ||
  116. p_ie.key.scancode == KEY_PAGEDOWN ) ) {
  117. search_options->call("_input_event",p_ie);
  118. search_box->accept_event();
  119. }
  120. }
  121. void ScriptEditorQuickOpen::_update_search() {
  122. search_options->clear();
  123. TreeItem *root = search_options->create_item();
  124. for(int i=0;i<functions.size();i++) {
  125. String file = functions[i];
  126. if ((search_box->get_text()=="" || file.findn(search_box->get_text())!=-1)) {
  127. TreeItem *ti = search_options->create_item(root);
  128. ti->set_text(0,file);
  129. if (root->get_children()==ti)
  130. ti->select(0);
  131. }
  132. }
  133. get_ok()->set_disabled(root->get_children()==NULL);
  134. }
  135. void ScriptEditorQuickOpen::_confirmed() {
  136. TreeItem *ti = search_options->get_selected();
  137. if (!ti)
  138. return;
  139. int line = ti->get_text(0).get_slice(":",1).to_int();
  140. emit_signal("goto_line",line-1);
  141. hide();
  142. }
  143. void ScriptEditorQuickOpen::_notification(int p_what) {
  144. if (p_what==NOTIFICATION_ENTER_TREE) {
  145. connect("confirmed",this,"_confirmed");
  146. }
  147. }
  148. void ScriptEditorQuickOpen::_bind_methods() {
  149. ObjectTypeDB::bind_method(_MD("_text_changed"),&ScriptEditorQuickOpen::_text_changed);
  150. ObjectTypeDB::bind_method(_MD("_confirmed"),&ScriptEditorQuickOpen::_confirmed);
  151. ObjectTypeDB::bind_method(_MD("_sbox_input"),&ScriptEditorQuickOpen::_sbox_input);
  152. ADD_SIGNAL(MethodInfo("goto_line",PropertyInfo(Variant::INT,"line")));
  153. }
  154. ScriptEditorQuickOpen::ScriptEditorQuickOpen() {
  155. VBoxContainer *vbc = memnew( VBoxContainer );
  156. add_child(vbc);
  157. set_child_rect(vbc);
  158. search_box = memnew( LineEdit );
  159. vbc->add_margin_child(TTR("Search:"),search_box);
  160. search_box->connect("text_changed",this,"_text_changed");
  161. search_box->connect("input_event",this,"_sbox_input");
  162. search_options = memnew( Tree );
  163. vbc->add_margin_child(TTR("Matches:"),search_options,true);
  164. get_ok()->set_text(TTR("Open"));
  165. get_ok()->set_disabled(true);
  166. register_text_enter(search_box);
  167. set_hide_on_ok(false);
  168. search_options->connect("item_activated",this,"_confirmed");
  169. search_options->set_hide_root(true);
  170. }
  171. /////////////////////////////////
  172. ScriptEditor *ScriptEditor::script_editor=NULL;
  173. Vector<String> ScriptTextEditor::get_functions() {
  174. String errortxt;
  175. int line=-1,col;
  176. TextEdit *te=get_text_edit();
  177. String text = te->get_text();
  178. List<String> fnc;
  179. if (script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  180. //if valid rewrite functions to latest
  181. functions.clear();
  182. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  183. functions.push_back(E->get());
  184. }
  185. }
  186. return functions;
  187. }
  188. void ScriptTextEditor::apply_code() {
  189. if (script.is_null())
  190. return;
  191. // print_line("applying code");
  192. script->set_source_code(get_text_edit()->get_text());
  193. script->update_exports();
  194. }
  195. Ref<Script> ScriptTextEditor::get_edited_script() const {
  196. return script;
  197. }
  198. void ScriptTextEditor::_load_theme_settings() {
  199. get_text_edit()->clear_colors();
  200. /* keyword color */
  201. get_text_edit()->set_custom_bg_color(EDITOR_DEF("text_editor/background_color",Color(0,0,0,0)));
  202. get_text_edit()->add_color_override("font_color",EDITOR_DEF("text_editor/text_color",Color(0,0,0)));
  203. get_text_edit()->add_color_override("line_number_color",EDITOR_DEF("text_editor/line_number_color",Color(0,0,0)));
  204. get_text_edit()->add_color_override("caret_color",EDITOR_DEF("text_editor/caret_color",Color(0,0,0)));
  205. get_text_edit()->add_color_override("font_selected_color",EDITOR_DEF("text_editor/text_selected_color",Color(1,1,1)));
  206. get_text_edit()->add_color_override("selection_color",EDITOR_DEF("text_editor/selection_color",Color(0.2,0.2,1)));
  207. get_text_edit()->add_color_override("brace_mismatch_color",EDITOR_DEF("text_editor/brace_mismatch_color",Color(1,0.2,0.2)));
  208. get_text_edit()->add_color_override("current_line_color",EDITOR_DEF("text_editor/current_line_color",Color(0.3,0.5,0.8,0.15)));
  209. get_text_edit()->add_color_override("word_highlighted_color",EDITOR_DEF("text_editor/word_highlighted_color",Color(0.8,0.9,0.9,0.15)));
  210. get_text_edit()->add_color_override("number_color",EDITOR_DEF("text_editor/number_color",Color(0.9,0.6,0.0,2)));
  211. get_text_edit()->add_color_override("function_color",EDITOR_DEF("text_editor/function_color",Color(0.4,0.6,0.8)));
  212. get_text_edit()->add_color_override("member_variable_color",EDITOR_DEF("text_editor/member_variable_color",Color(0.9,0.3,0.3)));
  213. get_text_edit()->add_color_override("mark_color", EDITOR_DEF("text_editor/mark_color", Color(1.0,0.4,0.4,0.4)));
  214. get_text_edit()->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/breakpoint_color", Color(0.8,0.8,0.4,0.2)));
  215. get_text_edit()->add_color_override("search_result_color",EDITOR_DEF("text_editor/search_result_color",Color(0.05,0.25,0.05,1)));
  216. get_text_edit()->add_color_override("search_result_border_color",EDITOR_DEF("text_editor/search_result_border_color",Color(0.1,0.45,0.1,1)));
  217. get_text_edit()->add_constant_override("line_spacing", EDITOR_DEF("text_editor/line_spacing",4));
  218. Color keyword_color= EDITOR_DEF("text_editor/keyword_color",Color(0.5,0.0,0.2));
  219. List<String> keywords;
  220. script->get_language()->get_reserved_words(&keywords);
  221. for(List<String>::Element *E=keywords.front();E;E=E->next()) {
  222. get_text_edit()->add_keyword_color(E->get(),keyword_color);
  223. }
  224. //colorize core types
  225. Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0));
  226. get_text_edit()->add_keyword_color("Vector2",basetype_color);
  227. get_text_edit()->add_keyword_color("Vector3",basetype_color);
  228. get_text_edit()->add_keyword_color("Plane",basetype_color);
  229. get_text_edit()->add_keyword_color("Quat",basetype_color);
  230. get_text_edit()->add_keyword_color("AABB",basetype_color);
  231. get_text_edit()->add_keyword_color("Matrix3",basetype_color);
  232. get_text_edit()->add_keyword_color("Transform",basetype_color);
  233. get_text_edit()->add_keyword_color("Color",basetype_color);
  234. get_text_edit()->add_keyword_color("Image",basetype_color);
  235. get_text_edit()->add_keyword_color("InputEvent",basetype_color);
  236. get_text_edit()->add_keyword_color("Rect2",basetype_color);
  237. get_text_edit()->add_keyword_color("NodePath",basetype_color);
  238. //colorize engine types
  239. Color type_color= EDITOR_DEF("text_editor/engine_type_color",Color(0.0,0.2,0.4));
  240. List<StringName> types;
  241. ObjectTypeDB::get_type_list(&types);
  242. for(List<StringName>::Element *E=types.front();E;E=E->next()) {
  243. String n = E->get();
  244. if (n.begins_with("_"))
  245. n = n.substr(1, n.length());
  246. get_text_edit()->add_keyword_color(n,type_color);
  247. }
  248. //colorize comments
  249. Color comment_color = EDITOR_DEF("text_editor/comment_color",Color::hex(0x797e7eff));
  250. List<String> comments;
  251. script->get_language()->get_comment_delimiters(&comments);
  252. for(List<String>::Element *E=comments.front();E;E=E->next()) {
  253. String comment = E->get();
  254. String beg = comment.get_slice(" ",0);
  255. String end = comment.get_slice_count(" ")>1?comment.get_slice(" ",1):String();
  256. get_text_edit()->add_color_region(beg,end,comment_color,end=="");
  257. }
  258. //colorize strings
  259. Color string_color = EDITOR_DEF("text_editor/string_color",Color::hex(0x6b6f00ff));
  260. List<String> strings;
  261. script->get_language()->get_string_delimiters(&strings);
  262. for (List<String>::Element *E=strings.front();E;E=E->next()) {
  263. String string = E->get();
  264. String beg = string.get_slice(" ",0);
  265. String end = string.get_slice_count(" ")>1?string.get_slice(" ",1):String();
  266. get_text_edit()->add_color_region(beg,end,string_color,end=="");
  267. }
  268. //colorize symbols
  269. Color symbol_color= EDITOR_DEF("text_editor/symbol_color",Color::hex(0x005291ff));
  270. get_text_edit()->set_symbol_color(symbol_color);
  271. }
  272. void ScriptTextEditor::reload_text() {
  273. ERR_FAIL_COND(script.is_null()) ;
  274. TextEdit *te = get_text_edit();
  275. int column = te->cursor_get_column();
  276. int row = te->cursor_get_line();
  277. int h = te->get_h_scroll();
  278. int v = te->get_v_scroll();
  279. te->set_text(script->get_source_code());
  280. te->clear_undo_history();
  281. te->cursor_set_line(row);
  282. te->cursor_set_column(column);
  283. te->set_h_scroll(h);
  284. te->set_v_scroll(v);
  285. te->tag_saved_version();
  286. _line_col_changed();
  287. }
  288. void ScriptTextEditor::_notification(int p_what) {
  289. if (p_what==NOTIFICATION_READY) {
  290. //emit_signal("name_changed");
  291. }
  292. }
  293. bool ScriptTextEditor::is_unsaved() {
  294. return get_text_edit()->get_version()!=get_text_edit()->get_saved_version();
  295. }
  296. String ScriptTextEditor::get_name() {
  297. String name;
  298. if (script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  299. name=script->get_path().get_file();
  300. if (get_text_edit()->get_version()!=get_text_edit()->get_saved_version()) {
  301. name+="(*)";
  302. }
  303. } else if (script->get_name()!="")
  304. name=script->get_name();
  305. else
  306. name=script->get_type()+"("+itos(script->get_instance_ID())+")";
  307. return name;
  308. }
  309. Ref<Texture> ScriptTextEditor::get_icon() {
  310. if (get_parent_control() && get_parent_control()->has_icon(script->get_type(),"EditorIcons")) {
  311. return get_parent_control()->get_icon(script->get_type(),"EditorIcons");
  312. }
  313. return Ref<Texture>();
  314. }
  315. void ScriptTextEditor::set_edited_script(const Ref<Script>& p_script) {
  316. ERR_FAIL_COND(!script.is_null());
  317. script=p_script;
  318. _load_theme_settings();
  319. get_text_edit()->set_text(script->get_source_code());
  320. get_text_edit()->clear_undo_history();
  321. get_text_edit()->tag_saved_version();
  322. emit_signal("name_changed");
  323. _line_col_changed();
  324. }
  325. void ScriptTextEditor::_validate_script() {
  326. String errortxt;
  327. int line=-1,col;
  328. TextEdit *te=get_text_edit();
  329. String text = te->get_text();
  330. List<String> fnc;
  331. if (!script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  332. String error_text="error("+itos(line)+","+itos(col)+"): "+errortxt;
  333. set_error(error_text);
  334. } else {
  335. set_error("");
  336. line=-1;
  337. if (!script->is_tool()) {
  338. script->set_source_code(text);
  339. script->update_exports();
  340. //script->reload(); //will update all the variables in property editors
  341. }
  342. functions.clear();
  343. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  344. functions.push_back(E->get());
  345. }
  346. }
  347. line--;
  348. for(int i=0;i<te->get_line_count();i++) {
  349. te->set_line_as_marked(i,line==i);
  350. }
  351. emit_signal("name_changed");
  352. }
  353. static Node* _find_node_for_script(Node* p_base, Node*p_current, const Ref<Script>& p_script) {
  354. if (p_current->get_owner()!=p_base && p_base!=p_current)
  355. return NULL;
  356. Ref<Script> c = p_current->get_script();
  357. if (c==p_script)
  358. return p_current;
  359. for(int i=0;i<p_current->get_child_count();i++) {
  360. Node *found = _find_node_for_script(p_base,p_current->get_child(i),p_script);
  361. if (found)
  362. return found;
  363. }
  364. return NULL;
  365. }
  366. static void _find_changed_scripts_for_external_editor(Node* p_base, Node*p_current, Set<Ref<Script> > &r_scripts) {
  367. if (p_current->get_owner()!=p_base && p_base!=p_current)
  368. return;
  369. Ref<Script> c = p_current->get_script();
  370. if (c.is_valid())
  371. r_scripts.insert(c);
  372. for(int i=0;i<p_current->get_child_count();i++) {
  373. _find_changed_scripts_for_external_editor(p_base,p_current->get_child(i),r_scripts);
  374. }
  375. }
  376. void ScriptEditor::_update_modified_scripts_for_external_editor() {
  377. if (!bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
  378. return;
  379. Set<Ref<Script> > scripts;
  380. Node *base = get_tree()->get_edited_scene_root();
  381. if (base) {
  382. _find_changed_scripts_for_external_editor(base,base,scripts);
  383. }
  384. for (Set<Ref<Script> >::Element *E=scripts.front();E;E=E->next()) {
  385. Ref<Script> script = E->get();
  386. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  387. continue; //internal script, who cares, though weird
  388. }
  389. uint64_t last_date = script->get_last_modified_time();
  390. uint64_t date = FileAccess::get_modified_time(script->get_path());
  391. if (last_date!=date) {
  392. Ref<Script> rel_script = ResourceLoader::load(script->get_path(),script->get_type(),true);
  393. ERR_CONTINUE(!rel_script.is_valid());
  394. script->set_source_code( rel_script->get_source_code() );
  395. script->set_last_modified_time( rel_script->get_last_modified_time() );
  396. script->update_exports();
  397. }
  398. }
  399. }
  400. void ScriptTextEditor::_code_complete_script(const String& p_code, List<String>* r_options) {
  401. Node *base = get_tree()->get_edited_scene_root();
  402. if (base) {
  403. base = _find_node_for_script(base,base,script);
  404. }
  405. String hint;
  406. Error err = script->get_language()->complete_code(p_code,script->get_path().get_base_dir(),base,r_options,hint);
  407. if (hint!="") {
  408. get_text_edit()->set_code_hint(hint);
  409. print_line("hint: "+hint.replace(String::chr(0xFFFF),"|"));
  410. }
  411. }
  412. void ScriptTextEditor::_bind_methods() {
  413. ADD_SIGNAL(MethodInfo("name_changed"));
  414. }
  415. ScriptTextEditor::ScriptTextEditor() {
  416. }
  417. /*** SCRIPT EDITOR ******/
  418. String ScriptEditor::_get_debug_tooltip(const String&p_text,Node *_ste) {
  419. ScriptTextEditor *ste=_ste->cast_to<ScriptTextEditor>();
  420. String val = debugger->get_var_value(p_text);
  421. if (val!=String()) {
  422. return p_text+": "+val;
  423. } else {
  424. return String();
  425. }
  426. }
  427. void ScriptEditor::_breaked(bool p_breaked,bool p_can_debug) {
  428. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug));
  429. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug) );
  430. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked );
  431. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked );
  432. }
  433. void ScriptEditor::_show_debugger(bool p_show) {
  434. // debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), p_show);
  435. }
  436. void ScriptEditor::_script_created(Ref<Script> p_script) {
  437. editor->push_item(p_script.operator->());
  438. }
  439. void ScriptEditor::_trim_trailing_whitespace(TextEdit *tx) {
  440. bool trimed_whitespace = false;
  441. for (int i = 0; i < tx->get_line_count(); i++) {
  442. String line = tx->get_line(i);
  443. if (line.ends_with(" ") || line.ends_with("\t")) {
  444. if (!trimed_whitespace) {
  445. tx->begin_complex_operation();
  446. trimed_whitespace = true;
  447. }
  448. int end = 0;
  449. for (int j = line.length() - 1; j > -1; j--) {
  450. if (line[j] != ' ' && line[j] != '\t') {
  451. end = j+1;
  452. break;
  453. }
  454. }
  455. tx->set_line(i, line.substr(0, end));
  456. }
  457. }
  458. if (trimed_whitespace) {
  459. tx->end_complex_operation();
  460. tx->update();
  461. }
  462. }
  463. void ScriptEditor::_goto_script_line2(int p_line) {
  464. int selected = tab_container->get_current_tab();
  465. if (selected<0 || selected>=tab_container->get_child_count())
  466. return;
  467. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  468. if (!current)
  469. return;
  470. current->get_text_edit()->cursor_set_line(p_line);
  471. }
  472. void ScriptEditor::_goto_script_line(REF p_script,int p_line) {
  473. editor->push_item(p_script.ptr());
  474. _goto_script_line2(p_line);
  475. }
  476. void ScriptEditor::_update_history_arrows() {
  477. script_back->set_disabled( history_pos<=0 );
  478. script_forward->set_disabled( history_pos>=history.size()-1 );
  479. }
  480. void ScriptEditor::_go_to_tab(int p_idx) {
  481. Node *cn = tab_container->get_child(p_idx);
  482. if (!cn)
  483. return;
  484. Control *c = cn->cast_to<Control>();
  485. if (!c)
  486. return;
  487. if (history_pos>=0 && history_pos<history.size() && history[history_pos].control==tab_container->get_current_tab_control()) {
  488. Node *n = tab_container->get_current_tab_control();
  489. if (n->cast_to<ScriptTextEditor>()) {
  490. history[history_pos].scroll_pos=n->cast_to<ScriptTextEditor>()->get_text_edit()->get_v_scroll();
  491. history[history_pos].cursor_column=n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_column();
  492. history[history_pos].cursor_row=n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_line();
  493. }
  494. if (n->cast_to<EditorHelp>()) {
  495. history[history_pos].scroll_pos=n->cast_to<EditorHelp>()->get_scroll();
  496. }
  497. }
  498. history.resize(history_pos+1);
  499. ScriptHistory sh;
  500. sh.control=c;
  501. sh.scroll_pos=0;
  502. history.push_back(sh);
  503. history_pos++;
  504. tab_container->set_current_tab(p_idx);
  505. c = tab_container->get_current_tab_control();
  506. if (c->cast_to<ScriptTextEditor>()) {
  507. script_name_label->set_text(c->cast_to<ScriptTextEditor>()->get_name());
  508. script_icon->set_texture(c->cast_to<ScriptTextEditor>()->get_icon());
  509. if (is_visible())
  510. c->cast_to<ScriptTextEditor>()->get_text_edit()->grab_focus();
  511. }
  512. if (c->cast_to<EditorHelp>()) {
  513. script_name_label->set_text(c->cast_to<EditorHelp>()->get_class_name());
  514. script_icon->set_texture(get_icon("Help","EditorIcons"));
  515. if (is_visible())
  516. c->cast_to<EditorHelp>()->set_focused();
  517. }
  518. c->set_meta("__editor_pass",++edit_pass);
  519. _update_history_arrows();
  520. _update_script_colors();
  521. }
  522. void ScriptEditor::_close_current_tab() {
  523. int selected = tab_container->get_current_tab();
  524. if (selected<0 || selected>=tab_container->get_child_count())
  525. return;
  526. Node *tselected = tab_container->get_child(selected);
  527. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  528. if (current) {
  529. apply_scripts();
  530. }
  531. //remove from history
  532. history.resize(history_pos+1);
  533. for(int i=0;i<history.size();i++) {
  534. if (history[i].control==tselected) {
  535. history.remove(i);
  536. i--;
  537. history_pos--;
  538. }
  539. }
  540. if (history_pos>=history.size()) {
  541. history_pos=history.size()-1;
  542. }
  543. int idx = tab_container->get_current_tab();
  544. memdelete(tselected);
  545. if (idx>=tab_container->get_child_count())
  546. idx=tab_container->get_child_count()-1;
  547. if (idx>=0) {
  548. if (history_pos>=0) {
  549. idx = history[history_pos].control->get_index();
  550. }
  551. tab_container->set_current_tab(idx);
  552. //script_list->select(idx);
  553. }
  554. _update_history_arrows();
  555. _update_script_names();
  556. EditorNode::get_singleton()->save_layout();
  557. }
  558. void ScriptEditor::_resave_scripts(const String& p_str) {
  559. apply_scripts();
  560. for(int i=0;i<tab_container->get_child_count();i++) {
  561. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  562. if (!ste)
  563. continue;
  564. Ref<Script> script = ste->get_edited_script();
  565. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  566. continue; //internal script, who cares
  567. if (trim_trailing_whitespace_on_save) {
  568. _trim_trailing_whitespace(ste->get_text_edit());
  569. }
  570. editor->save_resource(script);
  571. ste->get_text_edit()->tag_saved_version();
  572. }
  573. disk_changed->hide();
  574. }
  575. void ScriptEditor::_reload_scripts(){
  576. for(int i=0;i<tab_container->get_child_count();i++) {
  577. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  578. if (!ste) {
  579. continue;
  580. }
  581. Ref<Script> script = ste->get_edited_script();
  582. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  583. continue; //internal script, who cares
  584. }
  585. Ref<Script> rel_script = ResourceLoader::load(script->get_path(),script->get_type(),true);
  586. ERR_CONTINUE(!rel_script.is_valid());
  587. script->set_source_code( rel_script->get_source_code() );
  588. script->set_last_modified_time( rel_script->get_last_modified_time() );
  589. script->reload();
  590. ste->reload_text();
  591. }
  592. disk_changed->hide();
  593. _update_script_names();
  594. }
  595. void ScriptEditor::_res_saved_callback(const Ref<Resource>& p_res) {
  596. for(int i=0;i<tab_container->get_child_count();i++) {
  597. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  598. if (!ste) {
  599. continue;
  600. }
  601. Ref<Script> script = ste->get_edited_script();
  602. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  603. continue; //internal script, who cares
  604. }
  605. if (script==p_res) {
  606. ste->get_text_edit()->tag_saved_version();
  607. }
  608. }
  609. _update_script_names();
  610. if (!pending_auto_reload && auto_reload_running_scripts) {
  611. call_deferred("_live_auto_reload_running_scripts");
  612. pending_auto_reload=true;
  613. }
  614. }
  615. void ScriptEditor::_live_auto_reload_running_scripts() {
  616. pending_auto_reload=false;
  617. debugger->reload_scripts();
  618. }
  619. bool ScriptEditor::_test_script_times_on_disk() {
  620. disk_changed_list->clear();
  621. TreeItem *r = disk_changed_list->create_item();
  622. disk_changed_list->set_hide_root(true);
  623. bool need_ask=false;
  624. bool need_reload=false;
  625. bool use_autoreload=bool(EDITOR_DEF("text_editor/auto_reload_scripts_on_external_change",false));
  626. for(int i=0;i<tab_container->get_child_count();i++) {
  627. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  628. if (ste) {
  629. Ref<Script> script = ste->get_edited_script();
  630. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  631. continue; //internal script, who cares
  632. uint64_t last_date = script->get_last_modified_time();
  633. uint64_t date = FileAccess::get_modified_time(script->get_path());
  634. //printf("last date: %lli vs date: %lli\n",last_date,date);
  635. if (last_date!=date) {
  636. TreeItem *ti = disk_changed_list->create_item(r);
  637. ti->set_text(0,script->get_path().get_file());
  638. if (!use_autoreload || ste->is_unsaved()) {
  639. need_ask=true;
  640. }
  641. need_reload=true;
  642. //r->set_metadata(0,);
  643. }
  644. }
  645. }
  646. if (need_reload) {
  647. if (!need_ask) {
  648. script_editor->_reload_scripts();
  649. need_reload=false;
  650. } else {
  651. disk_changed->call_deferred("popup_centered_ratio",0.5);
  652. }
  653. }
  654. return need_reload;
  655. }
  656. void ScriptEditor::swap_lines(TextEdit *tx, int line1, int line2)
  657. {
  658. String tmp = tx->get_line(line1);
  659. String tmp2 = tx->get_line(line2);
  660. tx->set_line(line2, tmp);
  661. tx->set_line(line1, tmp2);
  662. tx->cursor_set_line(line2);
  663. }
  664. void ScriptEditor::_file_dialog_action(String p_file) {
  665. switch (file_dialog_option) {
  666. case FILE_SAVE_THEME_AS: {
  667. if(!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) {
  668. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  669. }
  670. } break;
  671. case FILE_IMPORT_THEME: {
  672. if(!EditorSettings::get_singleton()->import_text_editor_theme(p_file)) {
  673. editor->show_warning(TTR("Error importing theme"), TTR("Error importing"));
  674. }
  675. } break;
  676. }
  677. file_dialog_option = -1;
  678. }
  679. void ScriptEditor::_menu_option(int p_option) {
  680. switch(p_option) {
  681. case FILE_NEW: {
  682. script_create_dialog->config("Node", ".gd");
  683. script_create_dialog->popup_centered(Size2(300, 300)*EDSCALE);
  684. } break;
  685. case FILE_OPEN: {
  686. editor->open_resource("Script");
  687. return;
  688. } break;
  689. case FILE_SAVE_ALL: {
  690. if (!_test_script_times_on_disk())
  691. return;
  692. save_all_scripts();
  693. #if 0
  694. for(int i=0;i<tab_container->get_child_count();i++) {
  695. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  696. if (!ste)
  697. continue;
  698. Ref<Script> script = ste->get_edited_script();
  699. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  700. continue; //internal script, who cares
  701. editor->save_resource( script );
  702. }
  703. #endif
  704. } break;
  705. case FILE_IMPORT_THEME: {
  706. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  707. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  708. file_dialog_option = FILE_IMPORT_THEME;
  709. file_dialog->clear_filters();
  710. file_dialog->add_filter("*.tet");
  711. file_dialog->popup_centered_ratio();
  712. file_dialog->set_title(TTR("Import Theme"));
  713. } break;
  714. case FILE_RELOAD_THEME: {
  715. EditorSettings::get_singleton()->load_text_editor_theme();
  716. } break;
  717. case FILE_SAVE_THEME: {
  718. if(!EditorSettings::get_singleton()->save_text_editor_theme()) {
  719. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  720. }
  721. } break;
  722. case FILE_SAVE_THEME_AS: {
  723. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  724. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  725. file_dialog_option = FILE_SAVE_THEME_AS;
  726. file_dialog->clear_filters();
  727. file_dialog->add_filter("*.tet");
  728. file_dialog->set_current_path(EditorSettings::get_singleton()->get_settings_path() + "/text_editor_themes/" + EditorSettings::get_singleton()->get("text_editor/color_theme"));
  729. file_dialog->popup_centered_ratio();
  730. file_dialog->set_title(TTR("Save Theme As.."));
  731. } break;
  732. case SEARCH_HELP: {
  733. help_search_dialog->popup();
  734. } break;
  735. case SEARCH_CLASSES: {
  736. String current;
  737. if (tab_container->get_tab_count()>0) {
  738. EditorHelp *eh = tab_container->get_child( tab_container->get_current_tab() )->cast_to<EditorHelp>();
  739. if (eh) {
  740. current=eh->get_class_name();
  741. }
  742. }
  743. help_index->popup();
  744. if (current!="") {
  745. help_index->call_deferred("select_class",current);
  746. }
  747. } break;
  748. case SEARCH_WEBSITE: {
  749. OS::get_singleton()->shell_open("http://docs.godotengine.org/");
  750. } break;
  751. case WINDOW_NEXT: {
  752. _history_forward();
  753. } break;
  754. case WINDOW_PREV: {
  755. _history_back();
  756. } break;
  757. case DEBUG_SHOW: {
  758. if (debugger) {
  759. bool visible = debug_menu->get_popup()->is_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW) );
  760. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), !visible);
  761. if (visible)
  762. debugger->hide();
  763. else
  764. debugger->show();
  765. }
  766. } break;
  767. case DEBUG_SHOW_KEEP_OPEN: {
  768. bool visible = debug_menu->get_popup()->is_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN) );
  769. if (debugger)
  770. debugger->set_hide_on_stop(visible);
  771. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN), !visible);
  772. } break;
  773. }
  774. int selected = tab_container->get_current_tab();
  775. if (selected<0 || selected>=tab_container->get_child_count())
  776. return;
  777. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  778. if (current) {
  779. switch(p_option) {
  780. case FILE_NEW: {
  781. script_create_dialog->config("Node", ".gd");
  782. script_create_dialog->popup_centered(Size2(300, 300)*EDSCALE);
  783. } break;
  784. case FILE_SAVE: {
  785. if (_test_script_times_on_disk())
  786. return;
  787. if (trim_trailing_whitespace_on_save) {
  788. _trim_trailing_whitespace(current->get_text_edit());
  789. }
  790. editor->save_resource( current->get_edited_script() );
  791. } break;
  792. case FILE_SAVE_AS: {
  793. if (trim_trailing_whitespace_on_save) {
  794. _trim_trailing_whitespace(current->get_text_edit());
  795. }
  796. editor->save_resource_as( current->get_edited_script() );
  797. } break;
  798. case EDIT_UNDO: {
  799. current->get_text_edit()->undo();
  800. current->get_text_edit()->call_deferred("grab_focus");
  801. } break;
  802. case EDIT_REDO: {
  803. current->get_text_edit()->redo();
  804. current->get_text_edit()->call_deferred("grab_focus");
  805. } break;
  806. case EDIT_CUT: {
  807. current->get_text_edit()->cut();
  808. current->get_text_edit()->call_deferred("grab_focus");
  809. } break;
  810. case EDIT_COPY: {
  811. current->get_text_edit()->copy();
  812. current->get_text_edit()->call_deferred("grab_focus");
  813. } break;
  814. case EDIT_PASTE: {
  815. current->get_text_edit()->paste();
  816. current->get_text_edit()->call_deferred("grab_focus");
  817. } break;
  818. case EDIT_SELECT_ALL: {
  819. current->get_text_edit()->select_all();
  820. current->get_text_edit()->call_deferred("grab_focus");
  821. } break;
  822. case EDIT_MOVE_LINE_UP: {
  823. TextEdit *tx = current->get_text_edit();
  824. Ref<Script> scr = current->get_edited_script();
  825. if (scr.is_null())
  826. return;
  827. tx->begin_complex_operation();
  828. if (tx->is_selection_active())
  829. {
  830. int from_line = tx->get_selection_from_line();
  831. int from_col = tx->get_selection_from_column();
  832. int to_line = tx->get_selection_to_line();
  833. int to_column = tx->get_selection_to_column();
  834. for (int i = from_line; i <= to_line; i++)
  835. {
  836. int line_id = i;
  837. int next_id = i - 1;
  838. if (line_id == 0 || next_id < 0)
  839. return;
  840. swap_lines(tx, line_id, next_id);
  841. }
  842. int from_line_up = from_line > 0 ? from_line-1 : from_line;
  843. int to_line_up = to_line > 0 ? to_line-1 : to_line;
  844. tx->select(from_line_up, from_col, to_line_up, to_column);
  845. }
  846. else
  847. {
  848. int line_id = tx->cursor_get_line();
  849. int next_id = line_id - 1;
  850. if (line_id == 0 || next_id < 0)
  851. return;
  852. swap_lines(tx, line_id, next_id);
  853. }
  854. tx->end_complex_operation();
  855. tx->update();
  856. } break;
  857. case EDIT_MOVE_LINE_DOWN: {
  858. TextEdit *tx = current->get_text_edit();
  859. Ref<Script> scr = current->get_edited_script();
  860. if (scr.is_null())
  861. return;
  862. tx->begin_complex_operation();
  863. if (tx->is_selection_active())
  864. {
  865. int from_line = tx->get_selection_from_line();
  866. int from_col = tx->get_selection_from_column();
  867. int to_line = tx->get_selection_to_line();
  868. int to_column = tx->get_selection_to_column();
  869. for (int i = to_line; i >= from_line; i--)
  870. {
  871. int line_id = i;
  872. int next_id = i + 1;
  873. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  874. return;
  875. swap_lines(tx, line_id, next_id);
  876. }
  877. int from_line_down = from_line < tx->get_line_count() ? from_line+1 : from_line;
  878. int to_line_down = to_line < tx->get_line_count() ? to_line+1 : to_line;
  879. tx->select(from_line_down, from_col, to_line_down, to_column);
  880. }
  881. else
  882. {
  883. int line_id = tx->cursor_get_line();
  884. int next_id = line_id + 1;
  885. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  886. return;
  887. swap_lines(tx, line_id, next_id);
  888. }
  889. tx->end_complex_operation();
  890. tx->update();
  891. } break;
  892. case EDIT_INDENT_LEFT: {
  893. TextEdit *tx = current->get_text_edit();
  894. Ref<Script> scr = current->get_edited_script();
  895. if (scr.is_null())
  896. return;
  897. tx->begin_complex_operation();
  898. if (tx->is_selection_active())
  899. {
  900. tx->indent_selection_left();
  901. }
  902. else
  903. {
  904. int begin = tx->cursor_get_line();
  905. String line_text = tx->get_line(begin);
  906. // begins with tab
  907. if (line_text.begins_with("\t"))
  908. {
  909. line_text = line_text.substr(1, line_text.length());
  910. tx->set_line(begin, line_text);
  911. }
  912. // begins with 4 spaces
  913. else if (line_text.begins_with(" "))
  914. {
  915. line_text = line_text.substr(4, line_text.length());
  916. tx->set_line(begin, line_text);
  917. }
  918. }
  919. tx->end_complex_operation();
  920. tx->update();
  921. //tx->deselect();
  922. } break;
  923. case EDIT_INDENT_RIGHT: {
  924. TextEdit *tx = current->get_text_edit();
  925. Ref<Script> scr = current->get_edited_script();
  926. if (scr.is_null())
  927. return;
  928. tx->begin_complex_operation();
  929. if (tx->is_selection_active())
  930. {
  931. tx->indent_selection_right();
  932. }
  933. else
  934. {
  935. int begin = tx->cursor_get_line();
  936. String line_text = tx->get_line(begin);
  937. line_text = '\t' + line_text;
  938. tx->set_line(begin, line_text);
  939. }
  940. tx->end_complex_operation();
  941. tx->update();
  942. //tx->deselect();
  943. } break;
  944. case EDIT_CLONE_DOWN: {
  945. TextEdit *tx = current->get_text_edit();
  946. Ref<Script> scr = current->get_edited_script();
  947. if (scr.is_null())
  948. return;
  949. int from_line = tx->cursor_get_line();
  950. int to_line = tx->cursor_get_line();
  951. int column = tx->cursor_get_column();
  952. if (tx->is_selection_active()) {
  953. from_line = tx->get_selection_from_line();
  954. to_line = tx->get_selection_to_line();
  955. column = tx->cursor_get_column();
  956. }
  957. int next_line = to_line + 1;
  958. tx->begin_complex_operation();
  959. for (int i = from_line; i <= to_line; i++) {
  960. if (i >= tx->get_line_count() - 1) {
  961. tx->set_line(i, tx->get_line(i) + "\n");
  962. }
  963. String line_clone = tx->get_line(i);
  964. tx->insert_at(line_clone, next_line);
  965. next_line++;
  966. }
  967. tx->cursor_set_column(column);
  968. if (tx->is_selection_active()) {
  969. tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
  970. }
  971. tx->end_complex_operation();
  972. tx->update();
  973. } break;
  974. case EDIT_TOGGLE_COMMENT: {
  975. TextEdit *tx = current->get_text_edit();
  976. Ref<Script> scr = current->get_edited_script();
  977. if (scr.is_null())
  978. return;
  979. tx->begin_complex_operation();
  980. if (tx->is_selection_active())
  981. {
  982. int begin = tx->get_selection_from_line();
  983. int end = tx->get_selection_to_line();
  984. // End of selection ends on the first column of the last line, ignore it.
  985. if(tx->get_selection_to_column() == 0)
  986. end -= 1;
  987. for (int i = begin; i <= end; i++)
  988. {
  989. String line_text = tx->get_line(i);
  990. if (line_text.begins_with("#"))
  991. line_text = line_text.substr(1, line_text.length());
  992. else
  993. line_text = "#" + line_text;
  994. tx->set_line(i, line_text);
  995. }
  996. }
  997. else
  998. {
  999. int begin = tx->cursor_get_line();
  1000. String line_text = tx->get_line(begin);
  1001. if (line_text.begins_with("#"))
  1002. line_text = line_text.substr(1, line_text.length());
  1003. else
  1004. line_text = "#" + line_text;
  1005. tx->set_line(begin, line_text);
  1006. }
  1007. tx->end_complex_operation();
  1008. tx->update();
  1009. //tx->deselect();
  1010. } break;
  1011. case EDIT_COMPLETE: {
  1012. current->get_text_edit()->query_code_comple();
  1013. } break;
  1014. case EDIT_AUTO_INDENT: {
  1015. TextEdit *te = current->get_text_edit();
  1016. String text = te->get_text();
  1017. Ref<Script> scr = current->get_edited_script();
  1018. if (scr.is_null())
  1019. return;
  1020. int begin,end;
  1021. if (te->is_selection_active()) {
  1022. begin=te->get_selection_from_line();
  1023. end=te->get_selection_to_line();
  1024. } else {
  1025. begin=0;
  1026. end=te->get_line_count()-1;
  1027. }
  1028. scr->get_language()->auto_indent_code(text,begin,end);
  1029. te->set_text(text);
  1030. } break;
  1031. case FILE_TOOL_RELOAD:
  1032. case FILE_TOOL_RELOAD_SOFT: {
  1033. TextEdit *te = current->get_text_edit();
  1034. Ref<Script> scr = current->get_edited_script();
  1035. if (scr.is_null())
  1036. return;
  1037. scr->set_source_code(te->get_text());
  1038. scr->get_language()->reload_tool_script(scr,p_option==FILE_TOOL_RELOAD_SOFT);
  1039. } break;
  1040. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  1041. _trim_trailing_whitespace(current->get_text_edit());
  1042. } break;
  1043. case SEARCH_FIND: {
  1044. current->get_find_replace_bar()->popup_search();
  1045. } break;
  1046. case SEARCH_FIND_NEXT: {
  1047. current->get_find_replace_bar()->search_next();
  1048. } break;
  1049. case SEARCH_FIND_PREV: {
  1050. current->get_find_replace_bar()->search_prev();
  1051. } break;
  1052. case SEARCH_REPLACE: {
  1053. current->get_find_replace_bar()->popup_replace();
  1054. } break;
  1055. case SEARCH_LOCATE_FUNCTION: {
  1056. if (!current)
  1057. return;
  1058. quick_open->popup(current->get_functions());
  1059. } break;
  1060. case SEARCH_GOTO_LINE: {
  1061. goto_line_dialog->popup_find_line(current->get_text_edit());
  1062. } break;
  1063. case DEBUG_TOGGLE_BREAKPOINT: {
  1064. int line=current->get_text_edit()->cursor_get_line();
  1065. bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
  1066. current->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  1067. get_debugger()->set_breakpoint(current->get_edited_script()->get_path(),line+1,dobreak);
  1068. } break;
  1069. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  1070. List<int> bpoints;
  1071. current->get_text_edit()->get_breakpoints(&bpoints);
  1072. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  1073. int line = E->get();
  1074. bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
  1075. current->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  1076. get_debugger()->set_breakpoint(current->get_edited_script()->get_path(),line+1,dobreak);
  1077. }
  1078. }
  1079. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  1080. List<int> bpoints;
  1081. current->get_text_edit()->get_breakpoints(&bpoints);
  1082. if (bpoints.size() <= 0) {
  1083. return;
  1084. }
  1085. int line=current->get_text_edit()->cursor_get_line();
  1086. // wrap around
  1087. if (line >= bpoints[bpoints.size() - 1]) {
  1088. current->get_text_edit()->cursor_set_line(bpoints[0]);
  1089. } else {
  1090. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  1091. int bline = E->get();
  1092. if (bline > line) {
  1093. current->get_text_edit()->cursor_set_line(bline);
  1094. return;
  1095. }
  1096. }
  1097. }
  1098. } break;
  1099. case DEBUG_GOTO_PREV_BREAKPOINT: {
  1100. List<int> bpoints;
  1101. current->get_text_edit()->get_breakpoints(&bpoints);
  1102. if (bpoints.size() <= 0) {
  1103. return;
  1104. }
  1105. int line=current->get_text_edit()->cursor_get_line();
  1106. // wrap around
  1107. if (line <= bpoints[0]) {
  1108. current->get_text_edit()->cursor_set_line(bpoints[bpoints.size() - 1]);
  1109. } else {
  1110. for(List<int>::Element *E=bpoints.back();E;E=E->prev()) {
  1111. int bline = E->get();
  1112. if (bline < line) {
  1113. current->get_text_edit()->cursor_set_line(bline);
  1114. return;
  1115. }
  1116. }
  1117. }
  1118. } break;
  1119. case DEBUG_NEXT: {
  1120. if (debugger)
  1121. debugger->debug_next();
  1122. } break;
  1123. case DEBUG_STEP: {
  1124. if (debugger)
  1125. debugger->debug_step();
  1126. } break;
  1127. case DEBUG_BREAK: {
  1128. if (debugger)
  1129. debugger->debug_break();
  1130. } break;
  1131. case DEBUG_CONTINUE: {
  1132. if (debugger)
  1133. debugger->debug_continue();
  1134. } break;
  1135. case HELP_CONTEXTUAL: {
  1136. String text = current->get_text_edit()->get_selection_text();
  1137. if (text == "")
  1138. text = current->get_text_edit()->get_word_under_cursor();
  1139. if (text != "")
  1140. help_search_dialog->popup(text);
  1141. } break;
  1142. case FILE_CLOSE: {
  1143. if (current->get_text_edit()->get_version()!=current->get_text_edit()->get_saved_version()) {
  1144. erase_tab_confirm->set_text("Close and save changes?\n\""+current->get_name()+"\"");
  1145. erase_tab_confirm->popup_centered_minsize();
  1146. } else {
  1147. _close_current_tab();
  1148. }
  1149. } break;
  1150. case WINDOW_MOVE_LEFT: {
  1151. if (tab_container->get_current_tab()>0) {
  1152. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()-1);
  1153. script_list->call_deferred("select",tab_container->get_current_tab()-1);
  1154. tab_container->move_child(current,tab_container->get_current_tab()-1);
  1155. _update_script_names();
  1156. }
  1157. } break;
  1158. case WINDOW_MOVE_RIGHT: {
  1159. if (tab_container->get_current_tab()<tab_container->get_child_count()-1) {
  1160. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()+1);
  1161. script_list->call_deferred("select",tab_container->get_current_tab()+1);
  1162. tab_container->move_child(current,tab_container->get_current_tab()+1);
  1163. _update_script_names();
  1164. }
  1165. } break;
  1166. default: {
  1167. if (p_option>=WINDOW_SELECT_BASE) {
  1168. tab_container->set_current_tab(p_option-WINDOW_SELECT_BASE);
  1169. script_list->select(p_option-WINDOW_SELECT_BASE);
  1170. }
  1171. }
  1172. }
  1173. }
  1174. EditorHelp *help = tab_container->get_child(selected)->cast_to<EditorHelp>();
  1175. if (help) {
  1176. switch(p_option) {
  1177. case SEARCH_FIND: {
  1178. help->popup_search();
  1179. } break;
  1180. case SEARCH_FIND_NEXT: {
  1181. help->search_again();
  1182. } break;
  1183. case FILE_CLOSE: {
  1184. _close_current_tab();
  1185. } break;
  1186. }
  1187. }
  1188. }
  1189. void ScriptEditor::_tab_changed(int p_which) {
  1190. ensure_select_current();
  1191. }
  1192. void ScriptEditor::_notification(int p_what) {
  1193. if (p_what==NOTIFICATION_ENTER_TREE) {
  1194. editor->connect("play_pressed",this,"_editor_play");
  1195. editor->connect("pause_pressed",this,"_editor_pause");
  1196. editor->connect("stop_pressed",this,"_editor_stop");
  1197. editor->connect("script_add_function_request",this,"_add_callback");
  1198. editor->connect("resource_saved",this,"_res_saved_callback");
  1199. script_list->connect("item_selected",this,"_script_selected");
  1200. script_split->connect("dragged",this,"_script_split_dragged");
  1201. autosave_timer->connect("timeout",this,"_autosave_scripts");
  1202. {
  1203. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  1204. if (autosave_time>0) {
  1205. autosave_timer->set_wait_time(autosave_time);
  1206. autosave_timer->start();
  1207. } else {
  1208. autosave_timer->stop();
  1209. }
  1210. }
  1211. EditorSettings::get_singleton()->connect("settings_changed",this,"_editor_settings_changed");
  1212. help_search->set_icon(get_icon("Help","EditorIcons"));
  1213. site_search->set_icon(get_icon("Godot","EditorIcons"));
  1214. class_search->set_icon(get_icon("ClassList","EditorIcons"));
  1215. script_forward->set_icon(get_icon("Forward","EditorIcons"));
  1216. script_back->set_icon(get_icon("Back","EditorIcons"));
  1217. }
  1218. if (p_what==NOTIFICATION_READY) {
  1219. get_tree()->connect("tree_changed",this,"_tree_changed");
  1220. editor->connect("request_help",this,"_request_help");
  1221. }
  1222. if (p_what==NOTIFICATION_EXIT_TREE) {
  1223. editor->disconnect("play_pressed",this,"_editor_play");
  1224. editor->disconnect("pause_pressed",this,"_editor_pause");
  1225. editor->disconnect("stop_pressed",this,"_editor_stop");
  1226. }
  1227. if (p_what==MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  1228. _test_script_times_on_disk();
  1229. _update_modified_scripts_for_external_editor();
  1230. }
  1231. if (p_what==NOTIFICATION_PROCESS) {
  1232. }
  1233. }
  1234. void ScriptEditor::edited_scene_changed() {
  1235. _update_modified_scripts_for_external_editor();
  1236. }
  1237. static const Node * _find_node_with_script(const Node* p_node, const RefPtr & p_script) {
  1238. if (p_node->get_script()==p_script)
  1239. return p_node;
  1240. for(int i=0;i<p_node->get_child_count();i++) {
  1241. const Node *result = _find_node_with_script(p_node->get_child(i),p_script);
  1242. if (result)
  1243. return result;
  1244. }
  1245. return NULL;
  1246. }
  1247. Dictionary ScriptEditor::get_state() const {
  1248. // apply_scripts();
  1249. Dictionary state;
  1250. #if 0
  1251. Array paths;
  1252. int open=-1;
  1253. for(int i=0;i<tab_container->get_child_count();i++) {
  1254. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1255. if (!ste)
  1256. continue;
  1257. Ref<Script> script = ste->get_edited_script();
  1258. if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  1259. paths.push_back(script->get_path());
  1260. } else {
  1261. const Node *owner = _find_node_with_script(get_tree()->get_root(),script.get_ref_ptr());
  1262. if (owner)
  1263. paths.push_back(owner->get_path());
  1264. }
  1265. if (i==tab_container->get_current_tab())
  1266. open=i;
  1267. }
  1268. if (paths.size())
  1269. state["sources"]=paths;
  1270. if (open!=-1)
  1271. state["current"]=open;
  1272. #endif
  1273. return state;
  1274. }
  1275. void ScriptEditor::set_state(const Dictionary& p_state) {
  1276. #if 0
  1277. print_line("attempt set state: "+String(Variant(p_state)));
  1278. if (!p_state.has("sources"))
  1279. return; //bleh
  1280. Array sources = p_state["sources"];
  1281. for(int i=0;i<sources.size();i++) {
  1282. Variant source=sources[i];
  1283. Ref<Script> script;
  1284. if (source.get_type()==Variant::NODE_PATH) {
  1285. Node *owner=get_tree()->get_root()->get_node(source);
  1286. if (!owner)
  1287. continue;
  1288. script = owner->get_script();
  1289. } else if (source.get_type()==Variant::STRING) {
  1290. script = ResourceLoader::load(source,"Script");
  1291. }
  1292. if (script.is_null()) //ah well..
  1293. continue;
  1294. editor->call("_resource_selected",script);
  1295. }
  1296. if (p_state.has("current")) {
  1297. tab_container->set_current_tab(p_state["current"]);
  1298. }
  1299. #endif
  1300. }
  1301. void ScriptEditor::clear() {
  1302. #if 0
  1303. List<ScriptTextEditor*> stes;
  1304. for(int i=0;i<tab_container->get_child_count();i++) {
  1305. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1306. if (!ste)
  1307. continue;
  1308. stes.push_back(ste);
  1309. }
  1310. while(stes.size()) {
  1311. memdelete(stes.front()->get());
  1312. stes.pop_front();
  1313. }
  1314. int idx = tab_container->get_current_tab();
  1315. if (idx>=tab_container->get_child_count())
  1316. idx=tab_container->get_child_count()-1;
  1317. if (idx>=0) {
  1318. tab_container->set_current_tab(idx);
  1319. script_list->select( script_list->find_metadata(idx) );
  1320. }
  1321. #endif
  1322. }
  1323. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  1324. for(int i=0;i<tab_container->get_child_count();i++) {
  1325. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1326. if (!ste)
  1327. continue;
  1328. List<int> bpoints;
  1329. ste->get_text_edit()->get_breakpoints(&bpoints);
  1330. Ref<Script> script = ste->get_edited_script();
  1331. String base = script->get_path();
  1332. ERR_CONTINUE( base.begins_with("local://") || base=="" );
  1333. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  1334. p_breakpoints->push_back(base+":"+itos(E->get()+1));
  1335. }
  1336. }
  1337. }
  1338. void ScriptEditor::ensure_focus_current() {
  1339. if (!is_inside_tree())
  1340. return;
  1341. int cidx = tab_container->get_current_tab();
  1342. if (cidx<0 || cidx>=tab_container->get_tab_count());
  1343. Control *c = tab_container->get_child(cidx)->cast_to<Control>();
  1344. if (!c)
  1345. return;
  1346. ScriptTextEditor *ste = c->cast_to<ScriptTextEditor>();
  1347. if (!ste)
  1348. return;
  1349. ste->get_text_edit()->grab_focus();
  1350. }
  1351. void ScriptEditor::_script_selected(int p_idx) {
  1352. grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing
  1353. _go_to_tab(script_list->get_item_metadata(p_idx));
  1354. grab_focus_block=false;
  1355. }
  1356. void ScriptEditor::ensure_select_current() {
  1357. if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
  1358. Node *current = tab_container->get_child(tab_container->get_current_tab());
  1359. ScriptTextEditor *ste = current->cast_to<ScriptTextEditor>();
  1360. if (ste) {
  1361. Ref<Script> script = ste->get_edited_script();
  1362. if (!grab_focus_block && is_visible())
  1363. ste->get_text_edit()->grab_focus();
  1364. edit_menu->show();
  1365. search_menu->show();
  1366. script_search_menu->hide();
  1367. }
  1368. EditorHelp *eh = current->cast_to<EditorHelp>();
  1369. if (eh) {
  1370. edit_menu->hide();
  1371. search_menu->hide();
  1372. script_search_menu->show();
  1373. }
  1374. }
  1375. }
  1376. void ScriptEditor::_find_scripts(Node* p_base, Node* p_current, Set<Ref<Script> > &used) {
  1377. if (p_current!=p_base && p_current->get_owner()!=p_base)
  1378. return;
  1379. if (p_current->get_script_instance()) {
  1380. Ref<Script> scr = p_current->get_script();
  1381. if (scr.is_valid())
  1382. used.insert(scr);
  1383. }
  1384. for(int i=0;i<p_current->get_child_count();i++) {
  1385. _find_scripts(p_base,p_current->get_child(i),used);
  1386. }
  1387. }
  1388. struct _ScriptEditorItemData {
  1389. String name;
  1390. Ref<Texture> icon;
  1391. int index;
  1392. String tooltip;
  1393. bool used;
  1394. int category;
  1395. bool operator<(const _ScriptEditorItemData& id) const {
  1396. return category==id.category?name.nocasecmp_to(id.name)<0:category<id.category;
  1397. }
  1398. };
  1399. void ScriptEditor::_update_script_colors() {
  1400. bool enabled = EditorSettings::get_singleton()->get("text_editor/script_temperature_enabled");
  1401. if (!enabled)
  1402. return;
  1403. int hist_size = EditorSettings::get_singleton()->get("text_editor/script_temperature_history_size");
  1404. Color hot_color=EditorSettings::get_singleton()->get("text_editor/script_temperature_hot_color");
  1405. Color cold_color=EditorSettings::get_singleton()->get("text_editor/script_temperature_cold_color");
  1406. for(int i=0;i<script_list->get_item_count();i++) {
  1407. int c = script_list->get_item_metadata(i);
  1408. Node *n = tab_container->get_child(c);
  1409. if (!n)
  1410. continue;
  1411. script_list->set_item_custom_bg_color(i,Color(0,0,0,0));
  1412. if (!n->has_meta("__editor_pass")) {
  1413. continue;
  1414. }
  1415. int pass=n->get_meta("__editor_pass");
  1416. int h = edit_pass - pass;
  1417. if (h>hist_size) {
  1418. continue;
  1419. }
  1420. int non_zero_hist_size = ( hist_size == 0 ) ? 1 : hist_size;
  1421. float v = Math::ease((edit_pass-pass)/float(non_zero_hist_size),0.4);
  1422. script_list->set_item_custom_bg_color(i,hot_color.linear_interpolate(cold_color,v));
  1423. }
  1424. }
  1425. void ScriptEditor::_update_script_names() {
  1426. waiting_update_names=false;
  1427. Set<Ref<Script> > used;
  1428. Node* edited = EditorNode::get_singleton()->get_edited_scene();
  1429. if (edited) {
  1430. _find_scripts(edited,edited,used);
  1431. }
  1432. script_list->clear();
  1433. bool split_script_help = EditorSettings::get_singleton()->get("text_editor/group_help_pages");
  1434. Vector<_ScriptEditorItemData> sedata;
  1435. for(int i=0;i<tab_container->get_child_count();i++) {
  1436. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1437. if (ste) {
  1438. String name = ste->get_name();
  1439. Ref<Texture> icon = ste->get_icon();
  1440. String tooltip = ste->get_edited_script()->get_path();
  1441. _ScriptEditorItemData sd;
  1442. sd.icon=icon;
  1443. sd.name=name;
  1444. sd.tooltip=tooltip;
  1445. sd.index=i;
  1446. sd.used=used.has(ste->get_edited_script());
  1447. sd.category=0;
  1448. sedata.push_back(sd);
  1449. }
  1450. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1451. if (eh) {
  1452. String name = eh->get_class_name();
  1453. Ref<Texture> icon = get_icon("Help","EditorIcons");
  1454. String tooltip = name+" Class Reference";
  1455. _ScriptEditorItemData sd;
  1456. sd.icon=icon;
  1457. sd.name=name;
  1458. sd.tooltip=tooltip;
  1459. sd.index=i;
  1460. sd.used=false;
  1461. sd.category=split_script_help?1:0;
  1462. sedata.push_back(sd);
  1463. }
  1464. }
  1465. sedata.sort();
  1466. for(int i=0;i<sedata.size();i++) {
  1467. script_list->add_item(sedata[i].name,sedata[i].icon);
  1468. int index = script_list->get_item_count()-1;
  1469. script_list->set_item_tooltip(index,sedata[i].tooltip);
  1470. script_list->set_item_metadata(index,sedata[i].index);
  1471. if (sedata[i].used) {
  1472. script_list->set_item_custom_bg_color(index,Color(88/255.0,88/255.0,60/255.0));
  1473. }
  1474. if (tab_container->get_current_tab()==sedata[i].index) {
  1475. script_list->select(index);
  1476. script_name_label->set_text(sedata[i].name);
  1477. script_icon->set_texture(sedata[i].icon);
  1478. }
  1479. }
  1480. _update_script_colors();
  1481. }
  1482. void ScriptEditor::edit(const Ref<Script>& p_script) {
  1483. if (p_script.is_null())
  1484. return;
  1485. // see if already has it
  1486. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
  1487. if (p_script->get_path().is_resource_file() && bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor"))) {
  1488. String path = EditorSettings::get_singleton()->get("external_editor/exec_path");
  1489. String flags = EditorSettings::get_singleton()->get("external_editor/exec_flags");
  1490. List<String> args;
  1491. flags=flags.strip_edges();
  1492. if (flags!=String()) {
  1493. Vector<String> flagss = flags.split(" ",false);
  1494. for(int i=0;i<flagss.size();i++)
  1495. args.push_back(flagss[i]);
  1496. }
  1497. args.push_back(Globals::get_singleton()->globalize_path(p_script->get_path()));
  1498. Error err = OS::get_singleton()->execute(path,args,false);
  1499. if (err==OK)
  1500. return;
  1501. WARN_PRINT("Couldn't open external text editor, using internal");
  1502. }
  1503. for(int i=0;i<tab_container->get_child_count();i++) {
  1504. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1505. if (!ste)
  1506. continue;
  1507. if (ste->get_edited_script()==p_script) {
  1508. if (open_dominant || !EditorNode::get_singleton()->is_changing_scene()) {
  1509. if (tab_container->get_current_tab()!=i) {
  1510. _go_to_tab(i);
  1511. script_list->select( script_list->find_metadata(i) );
  1512. }
  1513. if (is_visible())
  1514. ste->get_text_edit()->grab_focus();
  1515. }
  1516. return;
  1517. }
  1518. }
  1519. // doesn't have it, make a new one
  1520. ScriptTextEditor *ste = memnew( ScriptTextEditor );
  1521. ste->set_edited_script(p_script);
  1522. ste->get_text_edit()->set_tooltip_request_func(this,"_get_debug_tooltip",ste);
  1523. ste->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  1524. ste->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  1525. ste->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  1526. ste->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  1527. ste->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  1528. ste->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  1529. ste->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  1530. ste->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  1531. ste->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  1532. ste->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  1533. ste->get_text_edit()->set_callhint_settings(
  1534. EditorSettings::get_singleton()->get("text_editor/put_callhint_tooltip_below_current_line"),
  1535. EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset"));
  1536. tab_container->add_child(ste);
  1537. _go_to_tab(tab_container->get_tab_count()-1);
  1538. _update_script_names();
  1539. ste->connect("name_changed",this,"_update_script_names");
  1540. if (!restoring_layout) {
  1541. EditorNode::get_singleton()->save_layout();
  1542. }
  1543. }
  1544. void ScriptEditor::save_all_scripts() {
  1545. for(int i=0;i<tab_container->get_child_count();i++) {
  1546. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1547. if (!ste)
  1548. continue;
  1549. if (trim_trailing_whitespace_on_save) {
  1550. _trim_trailing_whitespace(ste->get_text_edit());
  1551. }
  1552. if (ste->get_text_edit()->get_version()==ste->get_text_edit()->get_saved_version())
  1553. continue;
  1554. Ref<Script> script = ste->get_edited_script();
  1555. if (script.is_valid())
  1556. ste->apply_code();
  1557. if (script->get_path()!="" && script->get_path().find("local://")==-1 &&script->get_path().find("::")==-1) {
  1558. //external script, save it
  1559. editor->save_resource(script);
  1560. //ResourceSaver::save(script->get_path(),script);
  1561. }
  1562. }
  1563. }
  1564. void ScriptEditor::apply_scripts() const {
  1565. for(int i=0;i<tab_container->get_child_count();i++) {
  1566. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1567. if (!ste)
  1568. continue;
  1569. ste->apply_code();
  1570. }
  1571. }
  1572. void ScriptEditor::_editor_play() {
  1573. debugger->start();
  1574. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1575. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1576. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false );
  1577. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1578. //debugger_gui->start_listening(Globals::get_singleton()->get("debug/debug_port"));
  1579. }
  1580. void ScriptEditor::_editor_pause() {
  1581. }
  1582. void ScriptEditor::_editor_stop() {
  1583. debugger->stop();
  1584. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1585. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1586. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  1587. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1588. }
  1589. void ScriptEditor::_add_callback(Object *p_obj, const String& p_function, const StringArray& p_args) {
  1590. print_line("add callback! hohoho");
  1591. ERR_FAIL_COND(!p_obj);
  1592. Ref<Script> script = p_obj->get_script();
  1593. ERR_FAIL_COND( !script.is_valid() );
  1594. editor->push_item(script.ptr());
  1595. for(int i=0;i<tab_container->get_child_count();i++) {
  1596. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1597. if (!ste)
  1598. continue;
  1599. if (ste->get_edited_script()!=script)
  1600. continue;
  1601. String code = ste->get_text_edit()->get_text();
  1602. int pos = script->get_language()->find_function(p_function,code);
  1603. if (pos==-1) {
  1604. //does not exist
  1605. ste->get_text_edit()->deselect();
  1606. pos=ste->get_text_edit()->get_line_count()+2;
  1607. String func = script->get_language()->make_function("",p_function,p_args);
  1608. //code=code+func;
  1609. ste->get_text_edit()->cursor_set_line(pos+1);
  1610. ste->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  1611. ste->get_text_edit()->insert_text_at_cursor("\n\n"+func);
  1612. }
  1613. _go_to_tab(i);
  1614. ste->get_text_edit()->cursor_set_line(pos);
  1615. ste->get_text_edit()->cursor_set_column(1);
  1616. script_list->select( script_list->find_metadata(i) );
  1617. break;
  1618. }
  1619. }
  1620. void ScriptEditor::_editor_settings_changed() {
  1621. print_line("settings changed");
  1622. trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/trim_trailing_whitespace_on_save");
  1623. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  1624. if (autosave_time>0) {
  1625. autosave_timer->set_wait_time(autosave_time);
  1626. autosave_timer->start();
  1627. } else {
  1628. autosave_timer->stop();
  1629. }
  1630. if (current_theme == "") {
  1631. current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
  1632. } else if (current_theme != EditorSettings::get_singleton()->get("text_editor/color_theme")) {
  1633. current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
  1634. EditorSettings::get_singleton()->load_text_editor_theme();
  1635. }
  1636. for(int i=0;i<tab_container->get_child_count();i++) {
  1637. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1638. if (!ste)
  1639. continue;
  1640. ste->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  1641. ste->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  1642. ste->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  1643. ste->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  1644. ste->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  1645. ste->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  1646. ste->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  1647. ste->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  1648. ste->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  1649. ste->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  1650. }
  1651. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save",true));
  1652. }
  1653. void ScriptEditor::_autosave_scripts() {
  1654. print_line("autosaving");
  1655. save_all_scripts();
  1656. }
  1657. void ScriptEditor::_tree_changed() {
  1658. if (waiting_update_names)
  1659. return;
  1660. waiting_update_names=true;
  1661. call_deferred("_update_script_names");
  1662. }
  1663. void ScriptEditor::_script_split_dragged(float) {
  1664. EditorNode::get_singleton()->save_layout();
  1665. }
  1666. void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
  1667. if (!bool(EDITOR_DEF("text_editor/restore_scripts_on_load",true))) {
  1668. return;
  1669. }
  1670. if (!p_layout->has_section_key("ScriptEditor","open_scripts") && !p_layout->has_section_key("ScriptEditor","open_help"))
  1671. return;
  1672. Array scripts = p_layout->get_value("ScriptEditor","open_scripts");
  1673. Array helps;
  1674. if (p_layout->has_section_key("ScriptEditor","open_help"))
  1675. helps=p_layout->get_value("ScriptEditor","open_help");
  1676. restoring_layout=true;
  1677. for(int i=0;i<scripts.size();i++) {
  1678. String path = scripts[i];
  1679. Ref<Script> scr = ResourceLoader::load(path);
  1680. if (scr.is_valid()) {
  1681. edit(scr);
  1682. }
  1683. }
  1684. for(int i=0;i<helps.size();i++) {
  1685. String path = helps[i];
  1686. _help_class_open(path);
  1687. }
  1688. for(int i=0;i<tab_container->get_child_count();i++) {
  1689. tab_container->get_child(i)->set_meta("__editor_pass",Variant());
  1690. }
  1691. if (p_layout->has_section_key("ScriptEditor","split_offset")) {
  1692. script_split->set_split_offset(p_layout->get_value("ScriptEditor","split_offset"));
  1693. }
  1694. restoring_layout=false;
  1695. }
  1696. void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
  1697. Array scripts;
  1698. Array helps;
  1699. for(int i=0;i<tab_container->get_child_count();i++) {
  1700. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1701. if (ste) {
  1702. String path = ste->get_edited_script()->get_path();
  1703. if (!path.is_resource_file())
  1704. continue;
  1705. scripts.push_back(path);
  1706. }
  1707. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1708. if (eh) {
  1709. helps.push_back(eh->get_class_name());
  1710. }
  1711. }
  1712. p_layout->set_value("ScriptEditor","open_scripts",scripts);
  1713. p_layout->set_value("ScriptEditor","open_help",helps);
  1714. p_layout->set_value("ScriptEditor","split_offset",script_split->get_split_offset());
  1715. }
  1716. void ScriptEditor::_help_class_open(const String& p_class) {
  1717. for(int i=0;i<tab_container->get_child_count();i++) {
  1718. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1719. if (eh && eh->get_class_name()==p_class) {
  1720. _go_to_tab(i);
  1721. _update_script_names();
  1722. return;
  1723. }
  1724. }
  1725. EditorHelp * eh = memnew( EditorHelp );
  1726. eh->set_name(p_class);
  1727. tab_container->add_child(eh);
  1728. _go_to_tab(tab_container->get_tab_count()-1);
  1729. eh->go_to_class(p_class,0);
  1730. eh->connect("go_to_help",this,"_help_class_goto");
  1731. _update_script_names();
  1732. }
  1733. void ScriptEditor::_help_class_goto(const String& p_desc) {
  1734. String cname=p_desc.get_slice(":",1);
  1735. for(int i=0;i<tab_container->get_child_count();i++) {
  1736. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1737. if (eh && eh->get_class_name()==cname) {
  1738. _go_to_tab(i);
  1739. eh->go_to_help(p_desc);
  1740. _update_script_names();
  1741. return;
  1742. }
  1743. }
  1744. EditorHelp * eh = memnew( EditorHelp );
  1745. eh->set_name(cname);
  1746. tab_container->add_child(eh);
  1747. _go_to_tab(tab_container->get_tab_count()-1);
  1748. eh->go_to_help(p_desc);
  1749. eh->connect("go_to_help",this,"_help_class_goto");
  1750. _update_script_names();
  1751. }
  1752. void ScriptEditor::_update_history_pos(int p_new_pos) {
  1753. Node *n = tab_container->get_current_tab_control();
  1754. if (n->cast_to<ScriptTextEditor>()) {
  1755. history[history_pos].scroll_pos=n->cast_to<ScriptTextEditor>()->get_text_edit()->get_v_scroll();
  1756. history[history_pos].cursor_column=n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_column();
  1757. history[history_pos].cursor_row=n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_line();
  1758. }
  1759. if (n->cast_to<EditorHelp>()) {
  1760. history[history_pos].scroll_pos=n->cast_to<EditorHelp>()->get_scroll();
  1761. }
  1762. history_pos=p_new_pos;
  1763. tab_container->set_current_tab(history[history_pos].control->get_index());
  1764. n = history[history_pos].control;
  1765. if (n->cast_to<ScriptTextEditor>()) {
  1766. n->cast_to<ScriptTextEditor>()->get_text_edit()->set_v_scroll(history[history_pos].scroll_pos);
  1767. n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_set_column( history[history_pos].cursor_column );
  1768. n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_set_line( history[history_pos].cursor_row );
  1769. n->cast_to<ScriptTextEditor>()->get_text_edit()->grab_focus();
  1770. }
  1771. if (n->cast_to<EditorHelp>()) {
  1772. n->cast_to<EditorHelp>()->set_scroll(history[history_pos].scroll_pos);
  1773. n->cast_to<EditorHelp>()->set_focused();
  1774. }
  1775. n->set_meta("__editor_pass",++edit_pass);
  1776. _update_script_names();
  1777. _update_history_arrows();
  1778. }
  1779. void ScriptEditor::_history_forward() {
  1780. if (history_pos<history.size()-1) {
  1781. _update_history_pos(history_pos+1);
  1782. }
  1783. }
  1784. void ScriptEditor::_history_back(){
  1785. if (history_pos>0) {
  1786. _update_history_pos(history_pos-1);
  1787. }
  1788. }
  1789. void ScriptEditor::set_scene_root_script( Ref<Script> p_script ) {
  1790. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
  1791. if (bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
  1792. return;
  1793. if (open_dominant && p_script.is_valid()) {
  1794. edit(p_script);
  1795. }
  1796. }
  1797. bool ScriptEditor::script_go_to_method(Ref<Script> p_script, const String& p_method) {
  1798. Vector<String> functions;
  1799. bool found=false;
  1800. for (int i=0;i<tab_container->get_child_count();i++) {
  1801. ScriptTextEditor *current = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1802. if (current && current->get_edited_script()==p_script) {
  1803. functions=current->get_functions();
  1804. found=true;
  1805. break;
  1806. }
  1807. }
  1808. if (!found) {
  1809. String errortxt;
  1810. int line=-1,col;
  1811. String text=p_script->get_source_code();
  1812. List<String> fnc;
  1813. if (p_script->get_language()->validate(text,line,col,errortxt,p_script->get_path(),&fnc)) {
  1814. for (List<String>::Element *E=fnc.front();E;E=E->next())
  1815. functions.push_back(E->get());
  1816. }
  1817. }
  1818. String method_search = p_method + ":";
  1819. for (int i=0;i<functions.size();i++) {
  1820. String function=functions[i];
  1821. if (function.begins_with(method_search)) {
  1822. edit(p_script);
  1823. int line=function.get_slice(":",1).to_int();
  1824. _goto_script_line2(line-1);
  1825. return true;
  1826. }
  1827. }
  1828. return false;
  1829. }
  1830. void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) {
  1831. auto_reload_running_scripts=p_enabled;
  1832. }
  1833. void ScriptEditor::_bind_methods() {
  1834. ObjectTypeDB::bind_method("_file_dialog_action",&ScriptEditor::_file_dialog_action);
  1835. ObjectTypeDB::bind_method("_tab_changed",&ScriptEditor::_tab_changed);
  1836. ObjectTypeDB::bind_method("_menu_option",&ScriptEditor::_menu_option);
  1837. ObjectTypeDB::bind_method("_close_current_tab",&ScriptEditor::_close_current_tab);
  1838. ObjectTypeDB::bind_method("_editor_play",&ScriptEditor::_editor_play);
  1839. ObjectTypeDB::bind_method("_editor_pause",&ScriptEditor::_editor_pause);
  1840. ObjectTypeDB::bind_method("_editor_stop",&ScriptEditor::_editor_stop);
  1841. ObjectTypeDB::bind_method("_add_callback",&ScriptEditor::_add_callback);
  1842. ObjectTypeDB::bind_method("_reload_scripts",&ScriptEditor::_reload_scripts);
  1843. ObjectTypeDB::bind_method("_resave_scripts",&ScriptEditor::_resave_scripts);
  1844. ObjectTypeDB::bind_method("_res_saved_callback",&ScriptEditor::_res_saved_callback);
  1845. ObjectTypeDB::bind_method("_goto_script_line",&ScriptEditor::_goto_script_line);
  1846. ObjectTypeDB::bind_method("_goto_script_line2",&ScriptEditor::_goto_script_line2);
  1847. ObjectTypeDB::bind_method("_breaked",&ScriptEditor::_breaked);
  1848. ObjectTypeDB::bind_method("_show_debugger",&ScriptEditor::_show_debugger);
  1849. ObjectTypeDB::bind_method("_get_debug_tooltip",&ScriptEditor::_get_debug_tooltip);
  1850. ObjectTypeDB::bind_method("_autosave_scripts",&ScriptEditor::_autosave_scripts);
  1851. ObjectTypeDB::bind_method("_editor_settings_changed",&ScriptEditor::_editor_settings_changed);
  1852. ObjectTypeDB::bind_method("_update_script_names",&ScriptEditor::_update_script_names);
  1853. ObjectTypeDB::bind_method("_tree_changed",&ScriptEditor::_tree_changed);
  1854. ObjectTypeDB::bind_method("_script_selected",&ScriptEditor::_script_selected);
  1855. ObjectTypeDB::bind_method("_script_created",&ScriptEditor::_script_created);
  1856. ObjectTypeDB::bind_method("_script_split_dragged",&ScriptEditor::_script_split_dragged);
  1857. ObjectTypeDB::bind_method("_help_class_open",&ScriptEditor::_help_class_open);
  1858. ObjectTypeDB::bind_method("_help_class_goto",&ScriptEditor::_help_class_goto);
  1859. ObjectTypeDB::bind_method("_request_help",&ScriptEditor::_help_class_open);
  1860. ObjectTypeDB::bind_method("_history_forward",&ScriptEditor::_history_forward);
  1861. ObjectTypeDB::bind_method("_history_back",&ScriptEditor::_history_back);
  1862. ObjectTypeDB::bind_method("_live_auto_reload_running_scripts",&ScriptEditor::_live_auto_reload_running_scripts);
  1863. }
  1864. ScriptEditor::ScriptEditor(EditorNode *p_editor) {
  1865. current_theme = "";
  1866. completion_cache = memnew( EditorScriptCodeCompletionCache );
  1867. restoring_layout=false;
  1868. waiting_update_names=false;
  1869. auto_reload_running_scripts=false;
  1870. editor=p_editor;
  1871. menu_hb = memnew( HBoxContainer );
  1872. add_child(menu_hb);
  1873. script_split = memnew( HSplitContainer );
  1874. add_child(script_split);
  1875. script_split->set_v_size_flags(SIZE_EXPAND_FILL);
  1876. script_list = memnew( ItemList );
  1877. script_split->add_child(script_list);
  1878. script_list->set_custom_minimum_size(Size2(0,0));
  1879. script_split->set_split_offset(140);
  1880. tab_container = memnew( TabContainer );
  1881. tab_container->set_tabs_visible(false);
  1882. script_split->add_child(tab_container);
  1883. tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
  1884. file_menu = memnew( MenuButton );
  1885. menu_hb->add_child(file_menu);
  1886. file_menu->set_text(TTR("File"));
  1887. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTR("New")), FILE_NEW);
  1888. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open", TTR("Open")), FILE_OPEN);
  1889. file_menu->get_popup()->add_separator();
  1890. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save", TTR("Save"), KEY_MASK_ALT|KEY_MASK_CMD|KEY_S), FILE_SAVE);
  1891. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as", TTR("Save As..")), FILE_SAVE_AS);
  1892. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_all", TTR("Save All"), KEY_MASK_CMD|KEY_MASK_SHIFT|KEY_S), FILE_SAVE_ALL);
  1893. file_menu->get_popup()->add_separator();
  1894. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_previous", TTR("History Prev"), KEY_MASK_CTRL|KEY_MASK_ALT|KEY_LEFT), WINDOW_PREV);
  1895. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_next", TTR("History Next"), KEY_MASK_CTRL|KEY_MASK_ALT|KEY_RIGHT), WINDOW_NEXT);
  1896. file_menu->get_popup()->add_separator();
  1897. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/import_theme", TTR("Import Theme")), FILE_IMPORT_THEME);
  1898. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_theme", TTR("Reload Theme")), FILE_RELOAD_THEME);
  1899. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme", TTR("Save Theme")), FILE_SAVE_THEME);
  1900. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as", TTR("Save Theme As")), FILE_SAVE_THEME_AS);
  1901. file_menu->get_popup()->add_separator();
  1902. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file", TTR("Close"), KEY_MASK_CMD|KEY_W), FILE_CLOSE);
  1903. file_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1904. edit_menu = memnew( MenuButton );
  1905. menu_hb->add_child(edit_menu);
  1906. edit_menu->set_text(TTR("Edit"));
  1907. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/undo", TTR("Undo"), KEY_MASK_CMD|KEY_Z), EDIT_UNDO);
  1908. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/redo", TTR("Redo"), KEY_MASK_CMD|KEY_Y), EDIT_REDO);
  1909. edit_menu->get_popup()->add_separator();
  1910. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/cut", TTR("Cut"), KEY_MASK_CMD|KEY_X), EDIT_CUT);
  1911. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy", TTR("Copy"), KEY_MASK_CMD|KEY_C), EDIT_COPY);
  1912. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/paste", TTR("Paste"), KEY_MASK_CMD|KEY_V), EDIT_PASTE);
  1913. edit_menu->get_popup()->add_separator();
  1914. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/select_all", TTR("Select All"), KEY_MASK_CMD|KEY_A), EDIT_SELECT_ALL);
  1915. edit_menu->get_popup()->add_separator();
  1916. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/move_up", TTR("Move Up"), KEY_MASK_ALT|KEY_UP), EDIT_MOVE_LINE_UP);
  1917. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/move_down", TTR("Move Down"), KEY_MASK_ALT|KEY_DOWN), EDIT_MOVE_LINE_DOWN);
  1918. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/indent_left", TTR("Indent Left"), KEY_MASK_ALT|KEY_LEFT), EDIT_INDENT_LEFT);
  1919. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/indent_right", TTR("Indent Right"), KEY_MASK_ALT|KEY_RIGHT), EDIT_INDENT_RIGHT);
  1920. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD|KEY_K), EDIT_TOGGLE_COMMENT);
  1921. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD|KEY_B), EDIT_CLONE_DOWN);
  1922. edit_menu->get_popup()->add_separator();
  1923. #ifdef OSX_ENABLED
  1924. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL|KEY_SPACE), EDIT_COMPLETE);
  1925. #else
  1926. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD|KEY_SPACE), EDIT_COMPLETE);
  1927. #endif
  1928. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CTRL|KEY_MASK_ALT|KEY_T), EDIT_TRIM_TRAILING_WHITESAPCE);
  1929. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD|KEY_I), EDIT_AUTO_INDENT);
  1930. edit_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1931. edit_menu->get_popup()->add_separator();
  1932. edit_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);
  1933. search_menu = memnew( MenuButton );
  1934. menu_hb->add_child(search_menu);
  1935. search_menu->set_text(TTR("Search"));
  1936. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD|KEY_F), SEARCH_FIND);
  1937. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
  1938. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT|KEY_F3), SEARCH_FIND_PREV);
  1939. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace", TTR("Replace.."), KEY_MASK_CMD|KEY_R), SEARCH_REPLACE);
  1940. search_menu->get_popup()->add_separator();
  1941. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_function", TTR("Goto Function.."), KEY_MASK_SHIFT|KEY_MASK_CMD|KEY_F), SEARCH_LOCATE_FUNCTION);
  1942. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD|KEY_L), SEARCH_GOTO_LINE);
  1943. search_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1944. script_search_menu = memnew( MenuButton );
  1945. menu_hb->add_child(script_search_menu);
  1946. script_search_menu->set_text(TTR("Search"));
  1947. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD|KEY_F), SEARCH_FIND);
  1948. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
  1949. script_search_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1950. script_search_menu->hide();
  1951. debug_menu = memnew( MenuButton );
  1952. menu_hb->add_child(debug_menu);
  1953. debug_menu->set_text(TTR("Debug"));
  1954. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9), DEBUG_TOGGLE_BREAKPOINT);
  1955. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KEY_MASK_CTRL|KEY_MASK_SHIFT|KEY_F9), DEBUG_REMOVE_ALL_BREAKPOINTS);
  1956. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_next_breakpoint", TTR("Goto Next Breakpoint"), KEY_MASK_CTRL|KEY_PERIOD), DEBUG_GOTO_NEXT_BREAKPOINT);
  1957. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_previous_breakpoint", TTR("Goto Previous Breakpoint"), KEY_MASK_CTRL|KEY_COMMA), DEBUG_GOTO_PREV_BREAKPOINT);
  1958. debug_menu->get_popup()->add_separator();
  1959. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_over", TTR("Step Over"), KEY_F10), DEBUG_NEXT);
  1960. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_into", TTR("Step Into"), KEY_F11), DEBUG_STEP);
  1961. debug_menu->get_popup()->add_separator();
  1962. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/break", TTR("Break")), DEBUG_BREAK);
  1963. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/continue", TTR("Continue")), DEBUG_CONTINUE);
  1964. debug_menu->get_popup()->add_separator();
  1965. //debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
  1966. debug_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("debugger/keep_debugger_open", TTR("Keep Debugger Open")), DEBUG_SHOW_KEEP_OPEN);
  1967. debug_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1968. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1969. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1970. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  1971. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1972. #if 0
  1973. window_menu = memnew( MenuButton );
  1974. menu_hb->add_child(window_menu);
  1975. window_menu->set_text(TTR("Window"));
  1976. window_menu->get_popup()->add_item(TTR("Close"),WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
  1977. window_menu->get_popup()->add_separator();
  1978. window_menu->get_popup()->add_item(TTR("Move Left"),WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
  1979. window_menu->get_popup()->add_item(TTR("Move Right"),WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
  1980. window_menu->get_popup()->add_separator();
  1981. window_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1982. #endif
  1983. help_menu = memnew( MenuButton );
  1984. menu_hb->add_child(help_menu);
  1985. help_menu->set_text(TTR("Help"));
  1986. help_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/Contextual", TTR("Contextual Help"), KEY_MASK_SHIFT|KEY_F1), HELP_CONTEXTUAL);
  1987. help_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1988. menu_hb->add_spacer();
  1989. script_icon = memnew( TextureFrame );
  1990. menu_hb->add_child(script_icon);
  1991. script_name_label = memnew( Label );
  1992. menu_hb->add_child(script_name_label);
  1993. script_icon->hide();
  1994. script_name_label->hide();
  1995. menu_hb->add_spacer();
  1996. site_search = memnew( ToolButton );
  1997. site_search->set_text(TTR("Tutorials"));
  1998. site_search->connect("pressed",this,"_menu_option",varray(SEARCH_WEBSITE));
  1999. menu_hb->add_child(site_search);
  2000. site_search->set_tooltip(TTR("Open https://godotengine.org at tutorials section."));
  2001. class_search = memnew( ToolButton );
  2002. class_search->set_text(TTR("Classes"));
  2003. class_search->connect("pressed",this,"_menu_option",varray(SEARCH_CLASSES));
  2004. menu_hb->add_child(class_search);
  2005. class_search->set_tooltip(TTR("Search the class hierarchy."));
  2006. help_search = memnew( ToolButton );
  2007. help_search->set_text(TTR("Search Help"));
  2008. help_search->connect("pressed",this,"_menu_option",varray(SEARCH_HELP));
  2009. menu_hb->add_child(help_search);
  2010. help_search->set_tooltip(TTR("Search the reference documentation."));
  2011. menu_hb->add_child( memnew( VSeparator) );
  2012. script_back = memnew( ToolButton );
  2013. script_back->connect("pressed",this,"_history_back");
  2014. menu_hb->add_child(script_back);
  2015. script_back->set_disabled(true);
  2016. script_back->set_tooltip(TTR("Go to previous edited document."));
  2017. script_forward = memnew( ToolButton );
  2018. script_forward->connect("pressed",this,"_history_forward");
  2019. menu_hb->add_child(script_forward);
  2020. script_forward->set_disabled(true);
  2021. script_forward->set_tooltip(TTR("Go to next edited document."));
  2022. tab_container->connect("tab_changed", this,"_tab_changed");
  2023. erase_tab_confirm = memnew( ConfirmationDialog );
  2024. add_child(erase_tab_confirm);
  2025. erase_tab_confirm->connect("confirmed", this,"_close_current_tab");
  2026. script_create_dialog = memnew(ScriptCreateDialog);
  2027. script_create_dialog->set_title(TTR("Create Script"));
  2028. add_child(script_create_dialog);
  2029. script_create_dialog->connect("script_created", this, "_script_created");
  2030. file_dialog_option = -1;
  2031. file_dialog = memnew( EditorFileDialog );
  2032. add_child(file_dialog);
  2033. file_dialog->connect("file_selected", this,"_file_dialog_action");
  2034. goto_line_dialog = memnew(GotoLineDialog);
  2035. add_child(goto_line_dialog);
  2036. debugger = memnew( ScriptEditorDebugger(editor) );
  2037. debugger->connect("goto_script_line",this,"_goto_script_line");
  2038. debugger->connect("show_debugger",this,"_show_debugger");
  2039. disk_changed = memnew( ConfirmationDialog );
  2040. {
  2041. VBoxContainer *vbc = memnew( VBoxContainer );
  2042. disk_changed->add_child(vbc);
  2043. disk_changed->set_child_rect(vbc);
  2044. Label *dl = memnew( Label );
  2045. dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?:"));
  2046. vbc->add_child(dl);
  2047. disk_changed_list = memnew( Tree );
  2048. vbc->add_child(disk_changed_list);
  2049. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  2050. disk_changed->connect("confirmed",this,"_reload_scripts");
  2051. disk_changed->get_ok()->set_text(TTR("Reload"));
  2052. disk_changed->add_button(TTR("Resave"),!OS::get_singleton()->get_swap_ok_cancel(),"resave");
  2053. disk_changed->connect("custom_action",this,"_resave_scripts");
  2054. }
  2055. add_child(disk_changed);
  2056. script_editor=this;
  2057. quick_open = memnew( ScriptEditorQuickOpen );
  2058. add_child(quick_open);
  2059. quick_open->connect("goto_line",this,"_goto_script_line2");
  2060. Button *db = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Debugger"),debugger);
  2061. debugger->set_tool_button(db);
  2062. debugger->connect("breaked",this,"_breaked");
  2063. autosave_timer = memnew( Timer );
  2064. autosave_timer->set_one_shot(false);
  2065. add_child(autosave_timer);
  2066. grab_focus_block=false;
  2067. help_search_dialog = memnew( EditorHelpSearch );
  2068. add_child(help_search_dialog);
  2069. help_search_dialog->connect("go_to_help",this,"_help_class_goto");
  2070. help_index = memnew( EditorHelpIndex );
  2071. add_child(help_index);
  2072. help_index->connect("open_class",this,"_help_class_open");
  2073. history_pos=-1;
  2074. // debugger_gui->hide();
  2075. edit_pass=0;
  2076. trim_trailing_whitespace_on_save = false;
  2077. }
  2078. ScriptEditor::~ScriptEditor() {
  2079. memdelete(completion_cache);
  2080. }
  2081. void ScriptEditorPlugin::edit(Object *p_object) {
  2082. if (!p_object->cast_to<Script>())
  2083. return;
  2084. script_editor->edit(p_object->cast_to<Script>());
  2085. }
  2086. bool ScriptEditorPlugin::handles(Object *p_object) const {
  2087. return p_object->is_type("Script");
  2088. }
  2089. void ScriptEditorPlugin::make_visible(bool p_visible) {
  2090. if (p_visible) {
  2091. script_editor->show();
  2092. script_editor->set_process(true);
  2093. script_editor->ensure_select_current();
  2094. } else {
  2095. script_editor->hide();
  2096. script_editor->set_process(false);
  2097. }
  2098. }
  2099. void ScriptEditorPlugin::selected_notify() {
  2100. script_editor->ensure_select_current();
  2101. }
  2102. Dictionary ScriptEditorPlugin::get_state() const {
  2103. return script_editor->get_state();
  2104. }
  2105. void ScriptEditorPlugin::set_state(const Dictionary& p_state) {
  2106. script_editor->set_state(p_state);
  2107. }
  2108. void ScriptEditorPlugin::clear() {
  2109. script_editor->clear();
  2110. }
  2111. void ScriptEditorPlugin::save_external_data() {
  2112. script_editor->save_all_scripts();
  2113. }
  2114. void ScriptEditorPlugin::apply_changes() {
  2115. script_editor->apply_scripts();
  2116. }
  2117. void ScriptEditorPlugin::restore_global_state() {
  2118. }
  2119. void ScriptEditorPlugin::save_global_state() {
  2120. }
  2121. void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  2122. script_editor->set_window_layout(p_layout);
  2123. }
  2124. void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout){
  2125. script_editor->get_window_layout(p_layout);
  2126. }
  2127. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  2128. return script_editor->get_breakpoints(p_breakpoints);
  2129. }
  2130. void ScriptEditorPlugin::edited_scene_changed() {
  2131. script_editor->edited_scene_changed();
  2132. }
  2133. ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
  2134. editor=p_node;
  2135. script_editor = memnew( ScriptEditor(p_node) );
  2136. editor->get_viewport()->add_child(script_editor);
  2137. script_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2138. script_editor->hide();
  2139. EDITOR_DEF("text_editor/auto_reload_scripts_on_external_change",true);
  2140. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save",true));
  2141. EDITOR_DEF("text_editor/open_dominant_script_on_scene_change",true);
  2142. EDITOR_DEF("external_editor/use_external_editor",false);
  2143. EDITOR_DEF("external_editor/exec_path","");
  2144. EDITOR_DEF("text_editor/script_temperature_enabled",true);
  2145. EDITOR_DEF("text_editor/script_temperature_history_size",15);
  2146. EDITOR_DEF("text_editor/script_temperature_hot_color",Color(1,0,0,0.3));
  2147. EDITOR_DEF("text_editor/script_temperature_cold_color",Color(0,0,1,0.3));
  2148. EDITOR_DEF("text_editor/group_help_pages",true);
  2149. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"external_editor/exec_path",PROPERTY_HINT_GLOBAL_FILE));
  2150. EDITOR_DEF("external_editor/exec_flags","");
  2151. }
  2152. ScriptEditorPlugin::~ScriptEditorPlugin()
  2153. {
  2154. }