script_editor_plugin.cpp 79 KB

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