script_editor_plugin.cpp 90 KB

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