script_editor_plugin.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  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-2014 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. /*** SCRIPT EDITOR ****/
  42. void ScriptEditorQuickOpen::popup(const Vector<String>& p_functions, bool p_dontclear) {
  43. popup_centered_ratio(0.6);
  44. if (p_dontclear)
  45. search_box->select_all();
  46. else
  47. search_box->clear();
  48. search_box->grab_focus();
  49. functions=p_functions;
  50. _update_search();
  51. }
  52. void ScriptEditorQuickOpen::_text_changed(const String& p_newtext) {
  53. _update_search();
  54. }
  55. void ScriptEditorQuickOpen::_sbox_input(const InputEvent& p_ie) {
  56. if (p_ie.type==InputEvent::KEY && (
  57. p_ie.key.scancode == KEY_UP ||
  58. p_ie.key.scancode == KEY_DOWN ||
  59. p_ie.key.scancode == KEY_PAGEUP ||
  60. p_ie.key.scancode == KEY_PAGEDOWN ) ) {
  61. search_options->call("_input_event",p_ie);
  62. search_box->accept_event();
  63. }
  64. }
  65. void ScriptEditorQuickOpen::_update_search() {
  66. search_options->clear();
  67. TreeItem *root = search_options->create_item();
  68. for(int i=0;i<functions.size();i++) {
  69. String file = functions[i];
  70. if ((search_box->get_text()=="" || file.findn(search_box->get_text())!=-1)) {
  71. TreeItem *ti = search_options->create_item(root);
  72. ti->set_text(0,file);
  73. if (root->get_children()==ti)
  74. ti->select(0);
  75. }
  76. }
  77. get_ok()->set_disabled(root->get_children()==NULL);
  78. }
  79. void ScriptEditorQuickOpen::_confirmed() {
  80. TreeItem *ti = search_options->get_selected();
  81. if (!ti)
  82. return;
  83. int line = ti->get_text(0).get_slice(":",1).to_int();
  84. emit_signal("goto_line",line-1);
  85. hide();
  86. }
  87. void ScriptEditorQuickOpen::_notification(int p_what) {
  88. if (p_what==NOTIFICATION_ENTER_TREE) {
  89. connect("confirmed",this,"_confirmed");
  90. }
  91. }
  92. void ScriptEditorQuickOpen::_bind_methods() {
  93. ObjectTypeDB::bind_method(_MD("_text_changed"),&ScriptEditorQuickOpen::_text_changed);
  94. ObjectTypeDB::bind_method(_MD("_confirmed"),&ScriptEditorQuickOpen::_confirmed);
  95. ObjectTypeDB::bind_method(_MD("_sbox_input"),&ScriptEditorQuickOpen::_sbox_input);
  96. ADD_SIGNAL(MethodInfo("goto_line",PropertyInfo(Variant::INT,"line")));
  97. }
  98. ScriptEditorQuickOpen::ScriptEditorQuickOpen() {
  99. VBoxContainer *vbc = memnew( VBoxContainer );
  100. add_child(vbc);
  101. set_child_rect(vbc);
  102. search_box = memnew( LineEdit );
  103. vbc->add_margin_child("Search:",search_box);
  104. search_box->connect("text_changed",this,"_text_changed");
  105. search_box->connect("input_event",this,"_sbox_input");
  106. search_options = memnew( Tree );
  107. vbc->add_margin_child("Matches:",search_options,true);
  108. get_ok()->set_text("Open");
  109. get_ok()->set_disabled(true);
  110. register_text_enter(search_box);
  111. set_hide_on_ok(false);
  112. search_options->connect("item_activated",this,"_confirmed");
  113. search_options->set_hide_root(true);
  114. }
  115. /////////////////////////////////
  116. ScriptEditor *ScriptEditor::script_editor=NULL;
  117. Vector<String> ScriptTextEditor::get_functions() {
  118. String errortxt;
  119. int line=-1,col;
  120. TextEdit *te=get_text_edit();
  121. String text = te->get_text();
  122. List<String> fnc;
  123. if (script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  124. //if valid rewrite functions to latest
  125. functions.clear();
  126. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  127. functions.push_back(E->get());
  128. }
  129. }
  130. return functions;
  131. }
  132. void ScriptTextEditor::apply_code() {
  133. if (script.is_null())
  134. return;
  135. print_line("applying code");
  136. script->set_source_code(get_text_edit()->get_text());
  137. script->update_exports();
  138. }
  139. Ref<Script> ScriptTextEditor::get_edited_script() const {
  140. return script;
  141. }
  142. void ScriptTextEditor::_load_theme_settings() {
  143. get_text_edit()->clear_colors();
  144. /* keyword color */
  145. get_text_edit()->set_custom_bg_color(EDITOR_DEF("text_editor/background_color",Color(0,0,0,0)));
  146. get_text_edit()->add_color_override("font_color",EDITOR_DEF("text_editor/text_color",Color(0,0,0)));
  147. get_text_edit()->add_color_override("font_selected_color",EDITOR_DEF("text_editor/text_selected_color",Color(1,1,1)));
  148. get_text_edit()->add_color_override("selection_color",EDITOR_DEF("text_editor/selection_color",Color(0.2,0.2,1)));
  149. get_text_edit()->add_color_override("brace_mismatch_color",EDITOR_DEF("text_editor/brace_mismatch_color",Color(1,0.2,0.2)));
  150. Color keyword_color= EDITOR_DEF("text_editor/keyword_color",Color(0.5,0.0,0.2));
  151. get_text_edit()->set_syntax_coloring(true);
  152. List<String> keywords;
  153. script->get_language()->get_reserved_words(&keywords);
  154. for(List<String>::Element *E=keywords.front();E;E=E->next()) {
  155. get_text_edit()->add_keyword_color(E->get(),keyword_color);
  156. }
  157. //colorize core types
  158. Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0));
  159. get_text_edit()->add_keyword_color("Vector2",basetype_color);
  160. get_text_edit()->add_keyword_color("Vector3",basetype_color);
  161. get_text_edit()->add_keyword_color("Plane",basetype_color);
  162. get_text_edit()->add_keyword_color("Quat",basetype_color);
  163. get_text_edit()->add_keyword_color("AABB",basetype_color);
  164. get_text_edit()->add_keyword_color("Matrix3",basetype_color);
  165. get_text_edit()->add_keyword_color("Transform",basetype_color);
  166. get_text_edit()->add_keyword_color("Color",basetype_color);
  167. get_text_edit()->add_keyword_color("Image",basetype_color);
  168. get_text_edit()->add_keyword_color("InputEvent",basetype_color);
  169. //colorize engine types
  170. Color type_color= EDITOR_DEF("text_editor/engine_type_color",Color(0.0,0.2,0.4));
  171. List<String> types;
  172. ObjectTypeDB::get_type_list(&types);
  173. for(List<String>::Element *E=types.front();E;E=E->next()) {
  174. get_text_edit()->add_keyword_color(E->get(),type_color);
  175. }
  176. //colorize comments
  177. Color comment_color = EDITOR_DEF("text_editor/comment_color",Color::hex(0x797e7eff));
  178. List<String> comments;
  179. script->get_language()->get_comment_delimiters(&comments);
  180. for(List<String>::Element *E=comments.front();E;E=E->next()) {
  181. String comment = E->get();
  182. String beg = comment.get_slice(" ",0);
  183. String end = comment.get_slice_count(" ")>1?comment.get_slice(" ",1):String();
  184. get_text_edit()->add_color_region(beg,end,comment_color,end=="");
  185. }
  186. //colorize strings
  187. Color string_color = EDITOR_DEF("text_editor/string_color",Color::hex(0x6b6f00ff));
  188. List<String> strings;
  189. script->get_language()->get_string_delimiters(&strings);
  190. for (List<String>::Element *E=strings.front();E;E=E->next()) {
  191. String string = E->get();
  192. String beg = string.get_slice(" ",0);
  193. String end = string.get_slice_count(" ")>1?string.get_slice(" ",1):String();
  194. get_text_edit()->add_color_region(beg,end,string_color,end=="");
  195. }
  196. //colorize symbols
  197. Color symbol_color= EDITOR_DEF("text_editor/symbol_color",Color::hex(0x005291ff));
  198. get_text_edit()->set_symbol_color(symbol_color);
  199. }
  200. void ScriptTextEditor::reload_text() {
  201. ERR_FAIL_COND(script.is_null()) ;
  202. get_text_edit()->set_text(script->get_source_code());
  203. get_text_edit()->clear_undo_history();
  204. _line_col_changed();
  205. }
  206. void ScriptTextEditor::_notification(int p_what) {
  207. if (p_what==NOTIFICATION_READY) {
  208. _update_name();
  209. }
  210. }
  211. void ScriptTextEditor::_update_name() {
  212. String name;
  213. if (script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  214. name=script->get_path().get_file();
  215. if (get_text_edit()->get_version()!=get_text_edit()->get_saved_version()) {
  216. name+="(*)";
  217. }
  218. } else if (script->get_name()!="")
  219. name=script->get_name();
  220. else
  221. name=script->get_type()+"("+itos(script->get_instance_ID())+")";
  222. if (name!=String(get_name())) {
  223. set_name(name);
  224. }
  225. if (!has_meta("_tab_icon")) {
  226. if (get_parent_control() && get_parent_control()->has_icon(script->get_type(),"EditorIcons")) {
  227. set_meta("_tab_icon",get_parent_control()->get_icon(script->get_type(),"EditorIcons"));
  228. }
  229. }
  230. }
  231. void ScriptTextEditor::set_edited_script(const Ref<Script>& p_script) {
  232. ERR_FAIL_COND(!script.is_null());
  233. script=p_script;
  234. _load_theme_settings();
  235. get_text_edit()->set_text(script->get_source_code());
  236. get_text_edit()->clear_undo_history();
  237. get_text_edit()->tag_saved_version();
  238. _update_name();
  239. _line_col_changed();
  240. }
  241. void ScriptTextEditor::_validate_script() {
  242. String errortxt;
  243. int line=-1,col;
  244. TextEdit *te=get_text_edit();
  245. String text = te->get_text();
  246. List<String> fnc;
  247. if (!script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  248. String error_text="error("+itos(line)+","+itos(col)+"): "+errortxt;
  249. set_error(error_text);
  250. } else {
  251. set_error("");
  252. line=-1;
  253. if (!script->is_tool()) {
  254. script->set_source_code(text);
  255. script->update_exports();
  256. //script->reload(); //will update all the variables in property editors
  257. }
  258. functions.clear();
  259. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  260. functions.push_back(E->get());
  261. }
  262. }
  263. line--;
  264. for(int i=0;i<te->get_line_count();i++) {
  265. te->set_line_as_marked(i,line==i);
  266. }
  267. _update_name();
  268. }
  269. static Node* _find_node_for_script(Node* p_base, Node*p_current, const Ref<Script>& p_script) {
  270. if (p_current->get_owner()!=p_base && p_base!=p_current)
  271. return NULL;
  272. Ref<Script> c = p_current->get_script();
  273. if (c==p_script)
  274. return p_current;
  275. for(int i=0;i<p_current->get_child_count();i++) {
  276. Node *found = _find_node_for_script(p_base,p_current->get_child(i),p_script);
  277. if (found)
  278. return found;
  279. }
  280. return NULL;
  281. }
  282. void ScriptTextEditor::_code_complete_script(const String& p_code, List<String>* r_options) {
  283. Node *base = get_tree()->get_edited_scene_root();
  284. if (base) {
  285. base = _find_node_for_script(base,base,script);
  286. }
  287. String hint;
  288. Error err = script->get_language()->complete_code(p_code,script->get_path().get_base_dir(),base,r_options,hint);
  289. if (hint!="") {
  290. get_text_edit()->set_code_hint(hint);
  291. print_line("hint: "+hint.replace(String::chr(0xFFFF),"|"));
  292. }
  293. }
  294. ScriptTextEditor::ScriptTextEditor() {
  295. get_text_edit()->set_draw_tabs(true);
  296. }
  297. /*** SCRIPT EDITOR ******/
  298. String ScriptEditor::_get_debug_tooltip(const String&p_text,Node *_ste) {
  299. ScriptTextEditor *ste=_ste->cast_to<ScriptTextEditor>();
  300. String val = debugger->get_var_value(p_text);
  301. if (val!=String()) {
  302. return p_text+": "+val;
  303. } else {
  304. return String();
  305. }
  306. }
  307. void ScriptEditor::_breaked(bool p_breaked,bool p_can_debug) {
  308. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug));
  309. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug) );
  310. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked );
  311. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked );
  312. }
  313. void ScriptEditor::_show_debugger(bool p_show) {
  314. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), p_show);
  315. }
  316. void ScriptEditor::_goto_script_line2(int p_line) {
  317. int selected = tab_container->get_current_tab();
  318. if (selected<0 || selected>=tab_container->get_child_count())
  319. return;
  320. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  321. if (!current)
  322. return;
  323. current->get_text_edit()->cursor_set_line(p_line);
  324. }
  325. void ScriptEditor::_goto_script_line(REF p_script,int p_line) {
  326. editor->push_item(p_script.ptr());
  327. _goto_script_line2(p_line);
  328. }
  329. void ScriptEditor::_close_current_tab() {
  330. int selected = tab_container->get_current_tab();
  331. if (selected<0 || selected>=tab_container->get_child_count())
  332. return;
  333. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  334. if (!current)
  335. return;
  336. apply_scripts();
  337. int idx = tab_container->get_current_tab();
  338. memdelete(current);
  339. if (idx>=tab_container->get_child_count())
  340. idx=tab_container->get_child_count()-1;
  341. if (idx>=0)
  342. tab_container->set_current_tab(idx);
  343. _update_window_menu();
  344. _save_files_state();
  345. }
  346. void ScriptEditor::_resave_scripts(const String& p_str) {
  347. apply_scripts();
  348. for(int i=0;i<tab_container->get_child_count();i++) {
  349. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  350. if (!ste)
  351. continue;
  352. Ref<Script> script = ste->get_edited_script();
  353. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  354. continue; //internal script, who cares
  355. editor->save_resource(script);
  356. ste->get_text_edit()->tag_saved_version();
  357. }
  358. disk_changed->hide();
  359. }
  360. void ScriptEditor::_reload_scripts(){
  361. for(int i=0;i<tab_container->get_child_count();i++) {
  362. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  363. if (!ste) {
  364. continue;
  365. }
  366. Ref<Script> script = ste->get_edited_script();
  367. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  368. continue; //internal script, who cares
  369. }
  370. Ref<Script> rel_script = ResourceLoader::load(script->get_path(),script->get_type(),true);
  371. ERR_CONTINUE(!rel_script.is_valid());
  372. script->set_source_code( rel_script->get_source_code() );
  373. script->set_last_modified_time( rel_script->get_last_modified_time() );
  374. script->reload();
  375. ste->reload_text();
  376. }
  377. disk_changed->hide();
  378. }
  379. void ScriptEditor::_res_saved_callback(const Ref<Resource>& p_res) {
  380. for(int i=0;i<tab_container->get_child_count();i++) {
  381. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  382. if (!ste) {
  383. continue;
  384. }
  385. Ref<Script> script = ste->get_edited_script();
  386. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  387. continue; //internal script, who cares
  388. }
  389. if (script==p_res) {
  390. ste->get_text_edit()->tag_saved_version();
  391. }
  392. ste->_update_name();
  393. }
  394. }
  395. bool ScriptEditor::_test_script_times_on_disk() {
  396. disk_changed_list->clear();
  397. TreeItem *r = disk_changed_list->create_item();
  398. disk_changed_list->set_hide_root(true);
  399. bool all_ok=true;
  400. for(int i=0;i<tab_container->get_child_count();i++) {
  401. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  402. if (!ste)
  403. continue;
  404. Ref<Script> script = ste->get_edited_script();
  405. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  406. continue; //internal script, who cares
  407. uint64_t last_date = script->get_last_modified_time();
  408. uint64_t date = FileAccess::get_modified_time(script->get_path());
  409. //printf("last date: %lli vs date: %lli\n",last_date,date);
  410. if (last_date!=date) {
  411. TreeItem *ti = disk_changed_list->create_item(r);
  412. ti->set_text(0,script->get_path().get_file());
  413. all_ok=false;
  414. //r->set_metadata(0,);
  415. }
  416. }
  417. if (!all_ok)
  418. disk_changed->call_deferred("popup_centered_ratio",0.5);
  419. return all_ok;
  420. }
  421. void ScriptEditor::swap_lines(TextEdit *tx, int line1, int line2)
  422. {
  423. String tmp = tx->get_line(line1);
  424. String tmp2 = tx->get_line(line2);
  425. tx->set_line(line2, tmp);
  426. tx->set_line(line1, tmp2);
  427. tx->cursor_set_line(line2);
  428. }
  429. void ScriptEditor::_menu_option(int p_option) {
  430. if (p_option==FILE_OPEN) {
  431. editor->open_resource("Script");
  432. return;
  433. }
  434. int selected = tab_container->get_current_tab();
  435. if (selected<0 || selected>=tab_container->get_child_count())
  436. return;
  437. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  438. if (!current)
  439. return;
  440. switch(p_option) {
  441. case FILE_SAVE: {
  442. if (!_test_script_times_on_disk())
  443. return;
  444. editor->save_resource( current->get_edited_script() );
  445. } break;
  446. case FILE_SAVE_AS: {
  447. editor->save_resource_as( current->get_edited_script() );
  448. } break;
  449. case FILE_SAVE_ALL: {
  450. if (!_test_script_times_on_disk())
  451. return;
  452. for(int i=0;i<tab_container->get_child_count();i++) {
  453. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  454. if (!ste)
  455. continue;
  456. Ref<Script> script = ste->get_edited_script();
  457. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  458. continue; //internal script, who cares
  459. editor->save_resource( script );
  460. }
  461. } break;
  462. case EDIT_UNDO: {
  463. current->get_text_edit()->undo();
  464. } break;
  465. case EDIT_REDO: {
  466. current->get_text_edit()->redo();
  467. } break;
  468. case EDIT_CUT: {
  469. current->get_text_edit()->cut();
  470. } break;
  471. case EDIT_COPY: {
  472. current->get_text_edit()->copy();
  473. } break;
  474. case EDIT_PASTE: {
  475. current->get_text_edit()->paste();
  476. } break;
  477. case EDIT_SELECT_ALL: {
  478. current->get_text_edit()->select_all();
  479. } break;
  480. case EDIT_MOVE_LINE_UP: {
  481. TextEdit *tx = current->get_text_edit();
  482. Ref<Script> scr = current->get_edited_script();
  483. if (scr.is_null())
  484. return;
  485. if (tx->is_selection_active())
  486. {
  487. int from_line = tx->get_selection_from_line();
  488. int from_col = tx->get_selection_from_column();
  489. int to_line = tx->get_selection_to_line();
  490. int to_column = tx->get_selection_to_column();
  491. for (int i = from_line; i <= to_line; i++)
  492. {
  493. int line_id = i;
  494. int next_id = i - 1;
  495. if (line_id == 0 || next_id < 0)
  496. return;
  497. swap_lines(tx, line_id, next_id);
  498. }
  499. int from_line_up = from_line > 0 ? from_line-1 : from_line;
  500. int to_line_up = to_line > 0 ? to_line-1 : to_line;
  501. tx->select(from_line_up, from_col, to_line_up, to_column);
  502. }
  503. else
  504. {
  505. int line_id = tx->cursor_get_line();
  506. int next_id = line_id - 1;
  507. if (line_id == 0 || next_id < 0)
  508. return;
  509. swap_lines(tx, line_id, next_id);
  510. }
  511. tx->update();
  512. } break;
  513. case EDIT_MOVE_LINE_DOWN: {
  514. TextEdit *tx = current->get_text_edit();
  515. Ref<Script> scr = current->get_edited_script();
  516. if (scr.is_null())
  517. return;
  518. if (tx->is_selection_active())
  519. {
  520. int from_line = tx->get_selection_from_line();
  521. int from_col = tx->get_selection_from_column();
  522. int to_line = tx->get_selection_to_line();
  523. int to_column = tx->get_selection_to_column();
  524. for (int i = to_line; i >= from_line; i--)
  525. {
  526. int line_id = i;
  527. int next_id = i + 1;
  528. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  529. return;
  530. swap_lines(tx, line_id, next_id);
  531. }
  532. int from_line_down = from_line < tx->get_line_count() ? from_line+1 : from_line;
  533. int to_line_down = to_line < tx->get_line_count() ? to_line+1 : to_line;
  534. tx->select(from_line_down, from_col, to_line_down, to_column);
  535. }
  536. else
  537. {
  538. int line_id = tx->cursor_get_line();
  539. int next_id = line_id + 1;
  540. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  541. return;
  542. swap_lines(tx, line_id, next_id);
  543. }
  544. tx->update();
  545. } break;
  546. case EDIT_INDENT_LEFT: {
  547. TextEdit *tx = current->get_text_edit();
  548. Ref<Script> scr = current->get_edited_script();
  549. if (scr.is_null())
  550. return;
  551. int begin, end;
  552. begin = tx->get_selection_from_line();
  553. if (tx->is_selection_active())
  554. {
  555. end = tx->get_selection_to_line();
  556. for (int i = begin; i <= end; i++)
  557. {
  558. String line_text = tx->get_line(i);
  559. // begins with tab
  560. if (line_text.begins_with("\t"))
  561. {
  562. line_text = line_text.substr(1, line_text.length());
  563. tx->set_line(i, line_text);
  564. }
  565. // begins with 4 spaces
  566. else if (line_text.begins_with(" "))
  567. {
  568. line_text = line_text.substr(4, line_text.length());
  569. tx->set_line(i, line_text);
  570. }
  571. }
  572. }
  573. else
  574. {
  575. begin = tx->cursor_get_line();
  576. String line_text = tx->get_line(begin);
  577. // begins with tab
  578. if (line_text.begins_with("\t"))
  579. {
  580. line_text = line_text.substr(1, line_text.length());
  581. tx->set_line(begin, line_text);
  582. }
  583. // begins with 4 spaces
  584. else if (line_text.begins_with(" "))
  585. {
  586. line_text = line_text.substr(4, line_text.length());
  587. tx->set_line(begin, line_text);
  588. }
  589. }
  590. tx->update();
  591. //tx->deselect();
  592. } break;
  593. case EDIT_INDENT_RIGHT: {
  594. TextEdit *tx = current->get_text_edit();
  595. Ref<Script> scr = current->get_edited_script();
  596. if (scr.is_null())
  597. return;
  598. int begin, end;
  599. begin = tx->get_selection_from_line();
  600. if (tx->is_selection_active())
  601. {
  602. end = tx->get_selection_to_line();
  603. for (int i = begin; i <= end; i++)
  604. {
  605. String line_text = tx->get_line(i);
  606. line_text = '\t' + line_text;
  607. tx->set_line(i, line_text);
  608. }
  609. }
  610. else
  611. {
  612. begin = tx->cursor_get_line();
  613. String line_text = tx->get_line(begin);
  614. line_text = '\t' + line_text;
  615. tx->set_line(begin, line_text);
  616. }
  617. tx->update();
  618. //tx->deselect();
  619. } break;
  620. case EDIT_CLONE_DOWN: {
  621. TextEdit *tx = current->get_text_edit();
  622. Ref<Script> scr = current->get_edited_script();
  623. if (scr.is_null())
  624. return;
  625. int line = tx->cursor_get_line();
  626. int next_line = line + 1;
  627. if (line == tx->get_line_count() || next_line > tx->get_line_count())
  628. return;
  629. String line_clone = tx->get_line(line);
  630. tx->insert_at(line_clone, next_line);
  631. tx->update();
  632. } break;
  633. case EDIT_TOGGLE_COMMENT: {
  634. TextEdit *tx = current->get_text_edit();
  635. Ref<Script> scr = current->get_edited_script();
  636. if (scr.is_null())
  637. return;
  638. int begin, end;
  639. begin = tx->get_selection_from_line();
  640. if (tx->is_selection_active())
  641. {
  642. end = tx->get_selection_to_line();
  643. for (int i = begin; i <= end; i++)
  644. {
  645. String line_text = tx->get_line(i);
  646. if (line_text.begins_with("#"))
  647. line_text = line_text.strip_edges().substr(1, line_text.length());
  648. else
  649. line_text = "#" + line_text;
  650. tx->set_line(i, line_text);
  651. }
  652. }
  653. else
  654. {
  655. begin = tx->cursor_get_line();
  656. String line_text = tx->get_line(begin);
  657. if (line_text.begins_with("#"))
  658. line_text = line_text.strip_edges().substr(1, line_text.length());
  659. else
  660. line_text = "#" + line_text;
  661. tx->set_line(begin, line_text);
  662. }
  663. tx->update();
  664. //tx->deselect();
  665. } break;
  666. case EDIT_COMPLETE: {
  667. current->get_text_edit()->query_code_comple();
  668. } break;
  669. case EDIT_AUTO_INDENT: {
  670. TextEdit *te = current->get_text_edit();
  671. String text = te->get_text();
  672. Ref<Script> scr = current->get_edited_script();
  673. if (scr.is_null())
  674. return;
  675. int begin,end;
  676. if (te->is_selection_active()) {
  677. begin=te->get_selection_from_line();
  678. end=te->get_selection_to_line();
  679. } else {
  680. begin=0;
  681. end=te->get_line_count()-1;
  682. }
  683. scr->get_language()->auto_indent_code(text,begin,end);
  684. te->set_text(text);
  685. } break;
  686. case SEARCH_FIND: {
  687. find_replace_dialog->set_text_edit(current->get_text_edit());
  688. find_replace_dialog->popup_search();
  689. } break;
  690. case SEARCH_FIND_NEXT: {
  691. find_replace_dialog->set_text_edit(current->get_text_edit());
  692. find_replace_dialog->search_next();
  693. } break;
  694. case SEARCH_REPLACE: {
  695. find_replace_dialog->set_text_edit(current->get_text_edit());
  696. find_replace_dialog->popup_replace();
  697. } break;
  698. case SEARCH_LOCATE_FUNCTION: {
  699. if (!current)
  700. return;
  701. quick_open->popup(current->get_functions());
  702. } break;
  703. case SEARCH_GOTO_LINE: {
  704. goto_line_dialog->popup_find_line(current->get_text_edit());
  705. } break;
  706. case DEBUG_TOGGLE_BREAKPOINT: {
  707. int line=current->get_text_edit()->cursor_get_line();
  708. bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
  709. current->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  710. } break;
  711. case DEBUG_NEXT: {
  712. if (debugger)
  713. debugger->debug_next();
  714. } break;
  715. case DEBUG_STEP: {
  716. if (debugger)
  717. debugger->debug_step();
  718. } break;
  719. case DEBUG_BREAK: {
  720. if (debugger)
  721. debugger->debug_break();
  722. } break;
  723. case DEBUG_CONTINUE: {
  724. if (debugger)
  725. debugger->debug_continue();
  726. } break;
  727. case DEBUG_SHOW: {
  728. if (debugger) {
  729. bool visible = debug_menu->get_popup()->is_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW) );
  730. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), !visible);
  731. if (visible)
  732. debugger->hide();
  733. else
  734. debugger->show();
  735. }
  736. } break;
  737. case HELP_CONTEXTUAL: {
  738. String text = current->get_text_edit()->get_selection_text();
  739. if (text == "")
  740. text = current->get_text_edit()->get_word_under_cursor();
  741. if (text != "")
  742. editor->emit_signal("request_help", text);
  743. } break;
  744. case WINDOW_CLOSE: {
  745. erase_tab_confirm->set_text("Close Tab?:\n\""+current->get_name()+"\"");
  746. erase_tab_confirm->popup_centered(Point2(250,80));
  747. } break;
  748. case WINDOW_MOVE_LEFT: {
  749. if (tab_container->get_current_tab()>0) {
  750. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()-1);
  751. tab_container->move_child(current,tab_container->get_current_tab()-1);
  752. _update_window_menu();
  753. }
  754. } break;
  755. case WINDOW_MOVE_RIGHT: {
  756. if (tab_container->get_current_tab()<tab_container->get_child_count()-1) {
  757. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()+1);
  758. tab_container->move_child(current,tab_container->get_current_tab()+1);
  759. _update_window_menu();
  760. }
  761. } break;
  762. default: {
  763. if (p_option>=WINDOW_SELECT_BASE) {
  764. tab_container->set_current_tab(p_option-WINDOW_SELECT_BASE);
  765. }
  766. }
  767. }
  768. }
  769. void ScriptEditor::_tab_changed(int p_which) {
  770. ensure_select_current();
  771. }
  772. void ScriptEditor::_notification(int p_what) {
  773. if (p_what==NOTIFICATION_ENTER_TREE) {
  774. editor->connect("play_pressed",this,"_editor_play");
  775. editor->connect("pause_pressed",this,"_editor_pause");
  776. editor->connect("stop_pressed",this,"_editor_stop");
  777. editor->connect("script_add_function_request",this,"_add_callback");
  778. editor->connect("resource_saved",this,"_res_saved_callback");
  779. }
  780. if (p_what==NOTIFICATION_READY) {
  781. _update_window_menu();
  782. }
  783. if (p_what==NOTIFICATION_EXIT_TREE) {
  784. editor->disconnect("play_pressed",this,"_editor_play");
  785. editor->disconnect("pause_pressed",this,"_editor_pause");
  786. editor->disconnect("stop_pressed",this,"_editor_stop");
  787. }
  788. if (p_what==MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  789. _test_script_times_on_disk();
  790. }
  791. if (p_what==NOTIFICATION_PROCESS) {
  792. }
  793. }
  794. static const Node * _find_node_with_script(const Node* p_node, const RefPtr & p_script) {
  795. if (p_node->get_script()==p_script)
  796. return p_node;
  797. for(int i=0;i<p_node->get_child_count();i++) {
  798. const Node *result = _find_node_with_script(p_node->get_child(i),p_script);
  799. if (result)
  800. return result;
  801. }
  802. return NULL;
  803. }
  804. Dictionary ScriptEditor::get_state() const {
  805. apply_scripts();
  806. Dictionary state;
  807. Array paths;
  808. int open=-1;
  809. for(int i=0;i<tab_container->get_child_count();i++) {
  810. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  811. if (!ste)
  812. continue;
  813. Ref<Script> script = ste->get_edited_script();
  814. if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  815. paths.push_back(script->get_path());
  816. } else {
  817. const Node *owner = _find_node_with_script(get_tree()->get_root(),script.get_ref_ptr());
  818. if (owner)
  819. paths.push_back(owner->get_path());
  820. }
  821. if (i==tab_container->get_current_tab())
  822. open=i;
  823. }
  824. if (paths.size())
  825. state["sources"]=paths;
  826. if (open!=-1)
  827. state["current"]=open;
  828. return state;
  829. }
  830. void ScriptEditor::set_state(const Dictionary& p_state) {
  831. print_line("attempt set state: "+String(Variant(p_state)));
  832. if (!p_state.has("sources"))
  833. return; //bleh
  834. Array sources = p_state["sources"];
  835. for(int i=0;i<sources.size();i++) {
  836. Variant source=sources[i];
  837. Ref<Script> script;
  838. if (source.get_type()==Variant::NODE_PATH) {
  839. Node *owner=get_tree()->get_root()->get_node(source);
  840. if (!owner)
  841. continue;
  842. script = owner->get_script();
  843. } else if (source.get_type()==Variant::STRING) {
  844. script = ResourceLoader::load(source,"Script");
  845. }
  846. if (script.is_null()) //ah well..
  847. continue;
  848. editor->call("_resource_selected",script);
  849. }
  850. if (p_state.has("current")) {
  851. tab_container->set_current_tab(p_state["current"]);
  852. }
  853. }
  854. void ScriptEditor::clear() {
  855. List<ScriptTextEditor*> stes;
  856. for(int i=0;i<tab_container->get_child_count();i++) {
  857. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  858. if (!ste)
  859. continue;
  860. stes.push_back(ste);
  861. }
  862. while(stes.size()) {
  863. memdelete(stes.front()->get());
  864. stes.pop_front();
  865. }
  866. int idx = tab_container->get_current_tab();
  867. if (idx>=tab_container->get_child_count())
  868. idx=tab_container->get_child_count()-1;
  869. if (idx>=0)
  870. tab_container->set_current_tab(idx);
  871. _update_window_menu();
  872. }
  873. void ScriptEditor::_save_files_state() {
  874. return; //no thank you
  875. String rpath="_open_scripts_"+Globals::get_singleton()->get_resource_path();
  876. rpath=rpath.replace("\\","_-_");
  877. rpath=rpath.replace("/","_-_");
  878. rpath=rpath.replace(":","_");
  879. Vector<String> scripts;
  880. for(int i=0;i<tab_container->get_child_count();i++) {
  881. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  882. if (!ste)
  883. continue;
  884. Ref<Script> script = ste->get_edited_script();
  885. if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  886. scripts.push_back(script->get_path());
  887. }
  888. }
  889. EditorSettings::get_singleton()->set(rpath,scripts);
  890. EditorSettings::get_singleton()->save();
  891. }
  892. void ScriptEditor::_load_files_state() {
  893. return;
  894. String rpath="_open_scripts_"+Globals::get_singleton()->get_resource_path();
  895. rpath=rpath.replace("\\","_-_");
  896. rpath=rpath.replace("/","_-_");
  897. rpath=rpath.replace(":","_");
  898. if (EditorSettings::get_singleton()->has(rpath)) {
  899. Vector<String> open_files=EditorSettings::get_singleton()->get("rpath");
  900. for(int i=0;i<open_files.size();i++) {
  901. Ref<Script> scr = ResourceLoader::load(open_files[i]);
  902. if (!scr.is_valid())
  903. continue;
  904. editor->edit_resource(scr);
  905. }
  906. }
  907. }
  908. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  909. for(int i=0;i<tab_container->get_child_count();i++) {
  910. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  911. if (!ste)
  912. continue;
  913. List<int> bpoints;
  914. ste->get_text_edit()->get_breakpoints(&bpoints);
  915. Ref<Script> script = ste->get_edited_script();
  916. String base = script->get_path();
  917. ERR_CONTINUE( base.begins_with("local://") || base=="" );
  918. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  919. p_breakpoints->push_back(base+":"+itos(E->get()+1));
  920. }
  921. }
  922. }
  923. void ScriptEditor::_bind_methods() {
  924. ObjectTypeDB::bind_method("_tab_changed",&ScriptEditor::_tab_changed);
  925. ObjectTypeDB::bind_method("_menu_option",&ScriptEditor::_menu_option);
  926. ObjectTypeDB::bind_method("_close_current_tab",&ScriptEditor::_close_current_tab);
  927. ObjectTypeDB::bind_method("_editor_play",&ScriptEditor::_editor_play);
  928. ObjectTypeDB::bind_method("_editor_pause",&ScriptEditor::_editor_pause);
  929. ObjectTypeDB::bind_method("_editor_stop",&ScriptEditor::_editor_stop);
  930. ObjectTypeDB::bind_method("_add_callback",&ScriptEditor::_add_callback);
  931. ObjectTypeDB::bind_method("_reload_scripts",&ScriptEditor::_reload_scripts);
  932. ObjectTypeDB::bind_method("_resave_scripts",&ScriptEditor::_resave_scripts);
  933. ObjectTypeDB::bind_method("_res_saved_callback",&ScriptEditor::_res_saved_callback);
  934. ObjectTypeDB::bind_method("_goto_script_line",&ScriptEditor::_goto_script_line);
  935. ObjectTypeDB::bind_method("_goto_script_line2",&ScriptEditor::_goto_script_line2);
  936. ObjectTypeDB::bind_method("_breaked",&ScriptEditor::_breaked);
  937. ObjectTypeDB::bind_method("_show_debugger",&ScriptEditor::_show_debugger);
  938. ObjectTypeDB::bind_method("_get_debug_tooltip",&ScriptEditor::_get_debug_tooltip);
  939. }
  940. void ScriptEditor::ensure_focus_current() {
  941. int cidx = tab_container->get_current_tab();
  942. if (cidx<0 || cidx>=tab_container->get_tab_count());
  943. Control *c = tab_container->get_child(cidx)->cast_to<Control>();
  944. if (!c)
  945. return;
  946. ScriptTextEditor *ste = c->cast_to<ScriptTextEditor>();
  947. if (!ste)
  948. return;
  949. ste->get_text_edit()->grab_focus();
  950. }
  951. void ScriptEditor::ensure_select_current() {
  952. if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
  953. ScriptTextEditor *ste = tab_container->get_child(tab_container->get_current_tab())->cast_to<ScriptTextEditor>();
  954. if (!ste)
  955. return;
  956. Ref<Script> script = ste->get_edited_script();
  957. ste->get_text_edit()->grab_focus();
  958. }
  959. }
  960. void ScriptEditor::edit(const Ref<Script>& p_script) {
  961. if (p_script.is_null())
  962. return;
  963. // see if already has it
  964. if (p_script->get_path().is_resource_file() && bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor"))) {
  965. String path = EditorSettings::get_singleton()->get("external_editor/exec_path");
  966. String flags = EditorSettings::get_singleton()->get("external_editor/exec_flags");
  967. List<String> args;
  968. flags=flags.strip_edges();
  969. if (flags!=String()) {
  970. Vector<String> flagss = flags.split(" ",false);
  971. for(int i=0;i<flagss.size();i++)
  972. args.push_back(flagss[i]);
  973. }
  974. args.push_back(Globals::get_singleton()->globalize_path(p_script->get_path()));
  975. Error err = OS::get_singleton()->execute(path,args,false);
  976. if (err==OK)
  977. return;
  978. WARN_PRINT("Couldn't open external text editor, using internal");
  979. }
  980. for(int i=0;i<tab_container->get_child_count();i++) {
  981. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  982. if (!ste)
  983. continue;
  984. if (ste->get_edited_script()==p_script) {
  985. if (tab_container->get_current_tab()!=i)
  986. tab_container->set_current_tab(i);
  987. ste->get_text_edit()->grab_focus();
  988. return;
  989. }
  990. }
  991. // doesn't have it, make a new one
  992. ScriptTextEditor *ste = memnew( ScriptTextEditor );
  993. ste->set_edited_script(p_script);
  994. ste->get_text_edit()->set_tooltip_request_func(this,"_get_debug_tooltip",ste);
  995. tab_container->add_child(ste);
  996. tab_container->set_current_tab(tab_container->get_tab_count()-1);
  997. _update_window_menu();
  998. _save_files_state();
  999. }
  1000. void ScriptEditor::save_external_data() {
  1001. apply_scripts();
  1002. for(int i=0;i<tab_container->get_child_count();i++) {
  1003. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1004. if (!ste)
  1005. continue;
  1006. Ref<Script> script = ste->get_edited_script();
  1007. if (script->get_path()!="" && script->get_path().find("local://")==-1 &&script->get_path().find("::")==-1) {
  1008. //external script, save it
  1009. editor->save_resource(script);
  1010. //ResourceSaver::save(script->get_path(),script);
  1011. }
  1012. }
  1013. }
  1014. void ScriptEditor::apply_scripts() const {
  1015. for(int i=0;i<tab_container->get_child_count();i++) {
  1016. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1017. if (!ste)
  1018. continue;
  1019. ste->apply_code();
  1020. }
  1021. }
  1022. void ScriptEditor::_editor_play() {
  1023. debugger->start();
  1024. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1025. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1026. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false );
  1027. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1028. //debugger_gui->start_listening(Globals::get_singleton()->get("debug/debug_port"));
  1029. }
  1030. void ScriptEditor::_editor_pause() {
  1031. }
  1032. void ScriptEditor::_editor_stop() {
  1033. debugger->stop();
  1034. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1035. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1036. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  1037. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1038. }
  1039. void ScriptEditor::_update_window_menu() {
  1040. int idx=0;
  1041. for(int i=0;i<tab_container->get_child_count();i++) {
  1042. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1043. if (!ste)
  1044. continue;
  1045. idx++;
  1046. }
  1047. if (idx==0) {
  1048. window_menu->set_disabled(true);
  1049. edit_menu->set_disabled(true);
  1050. search_menu->set_disabled(true);
  1051. return;
  1052. } else {
  1053. window_menu->set_disabled(false);
  1054. edit_menu->set_disabled(false);
  1055. search_menu->set_disabled(false);
  1056. }
  1057. window_menu->get_popup()->clear();
  1058. window_menu->get_popup()->add_item("Close",WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
  1059. window_menu->get_popup()->add_separator();
  1060. window_menu->get_popup()->add_item("Move Left",WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
  1061. window_menu->get_popup()->add_item("Move Right",WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
  1062. window_menu->get_popup()->add_separator();
  1063. idx=0;
  1064. for(int i=0;i<tab_container->get_child_count();i++) {
  1065. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1066. if (!ste)
  1067. continue;
  1068. String n = ste->get_name();
  1069. uint32_t accel=0;
  1070. if (idx<9) {
  1071. accel=KEY_MASK_ALT|(KEY_1+idx);
  1072. }
  1073. window_menu->get_popup()->add_item(n,WINDOW_SELECT_BASE+idx,accel);
  1074. idx++;
  1075. }
  1076. }
  1077. void ScriptEditor::_add_callback(Object *p_obj, const String& p_function, const StringArray& p_args) {
  1078. print_line("add callback! hohoho");
  1079. ERR_FAIL_COND(!p_obj);
  1080. Ref<Script> script = p_obj->get_script();
  1081. ERR_FAIL_COND( !script.is_valid() );
  1082. editor->push_item(script.ptr());
  1083. for(int i=0;i<tab_container->get_child_count();i++) {
  1084. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1085. if (!ste)
  1086. continue;
  1087. if (ste->get_edited_script()!=script)
  1088. continue;
  1089. String code = ste->get_text_edit()->get_text();
  1090. int pos = script->get_language()->find_function(p_function,code);
  1091. if (pos==-1) {
  1092. //does not exist
  1093. ste->get_text_edit()->deselect();
  1094. pos=ste->get_text_edit()->get_line_count()+2;
  1095. String func = script->get_language()->make_function("",p_function,p_args);
  1096. //code=code+func;
  1097. ste->get_text_edit()->cursor_set_line(pos+1);
  1098. ste->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  1099. ste->get_text_edit()->insert_text_at_cursor("\n\n"+func);
  1100. }
  1101. tab_container->set_current_tab(i);
  1102. ste->get_text_edit()->cursor_set_line(pos);
  1103. ste->get_text_edit()->cursor_set_column(1);
  1104. break;
  1105. }
  1106. }
  1107. ScriptEditor::ScriptEditor(EditorNode *p_editor) {
  1108. editor=p_editor;
  1109. menu_hb = memnew( HBoxContainer );
  1110. add_child(menu_hb);
  1111. v_split = memnew( VSplitContainer );
  1112. add_child(v_split);
  1113. v_split->set_v_size_flags(SIZE_EXPAND_FILL);
  1114. tab_container = memnew( TabContainer );
  1115. v_split->add_child(tab_container);
  1116. tab_container->set_v_size_flags(SIZE_EXPAND_FILL);
  1117. file_menu = memnew( MenuButton );
  1118. menu_hb->add_child(file_menu);
  1119. file_menu->set_text("File");
  1120. file_menu->get_popup()->add_item("Open",FILE_OPEN);
  1121. file_menu->get_popup()->add_item("Save",FILE_SAVE,KEY_MASK_ALT|KEY_S);
  1122. file_menu->get_popup()->add_item("Save As..",FILE_SAVE_AS);
  1123. file_menu->get_popup()->add_item("Save All",FILE_SAVE_ALL,KEY_MASK_CMD|KEY_MASK_SHIFT|KEY_S);
  1124. file_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1125. edit_menu = memnew( MenuButton );
  1126. menu_hb->add_child(edit_menu);
  1127. edit_menu->set_text("Edit");
  1128. edit_menu->get_popup()->add_item("Undo",EDIT_UNDO,KEY_MASK_CMD|KEY_Z);
  1129. edit_menu->get_popup()->add_item("Redo",EDIT_REDO,KEY_MASK_CMD|KEY_Y);
  1130. edit_menu->get_popup()->add_separator();
  1131. edit_menu->get_popup()->add_item("Cut",EDIT_CUT,KEY_MASK_CMD|KEY_X);
  1132. edit_menu->get_popup()->add_item("Copy",EDIT_COPY,KEY_MASK_CMD|KEY_C);
  1133. edit_menu->get_popup()->add_item("Paste",EDIT_PASTE,KEY_MASK_CMD|KEY_V);
  1134. edit_menu->get_popup()->add_separator();
  1135. edit_menu->get_popup()->add_item("Select All",EDIT_SELECT_ALL,KEY_MASK_CMD|KEY_A);
  1136. edit_menu->get_popup()->add_separator();
  1137. edit_menu->get_popup()->add_item("Move Up",EDIT_MOVE_LINE_UP,KEY_MASK_ALT|KEY_UP);
  1138. edit_menu->get_popup()->add_item("Move Down",EDIT_MOVE_LINE_DOWN,KEY_MASK_ALT|KEY_DOWN);
  1139. edit_menu->get_popup()->add_item("Indent Left",EDIT_INDENT_LEFT,KEY_MASK_ALT|KEY_LEFT);
  1140. edit_menu->get_popup()->add_item("Indent Right",EDIT_INDENT_RIGHT,KEY_MASK_ALT|KEY_RIGHT);
  1141. edit_menu->get_popup()->add_item("Toggle Comment",EDIT_TOGGLE_COMMENT,KEY_MASK_CMD|KEY_K);
  1142. edit_menu->get_popup()->add_item("Clone Down",EDIT_CLONE_DOWN,KEY_MASK_CMD|KEY_B);
  1143. edit_menu->get_popup()->add_separator();
  1144. edit_menu->get_popup()->add_item("Complete Symbol",EDIT_COMPLETE,KEY_MASK_CMD|KEY_SPACE);
  1145. edit_menu->get_popup()->add_item("Auto Indent",EDIT_AUTO_INDENT,KEY_MASK_CMD|KEY_I);
  1146. edit_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1147. search_menu = memnew( MenuButton );
  1148. menu_hb->add_child(search_menu);
  1149. search_menu->set_text("Search");
  1150. search_menu->get_popup()->add_item("Find..",SEARCH_FIND,KEY_MASK_CMD|KEY_F);
  1151. search_menu->get_popup()->add_item("Find Next",SEARCH_FIND_NEXT,KEY_MASK_CMD|KEY_G);
  1152. search_menu->get_popup()->add_item("Replace..",SEARCH_REPLACE,KEY_MASK_CMD|KEY_R);
  1153. search_menu->get_popup()->add_separator();
  1154. search_menu->get_popup()->add_item("Goto Function..",SEARCH_LOCATE_FUNCTION,KEY_MASK_SHIFT|KEY_MASK_CMD|KEY_F);
  1155. search_menu->get_popup()->add_item("Goto Line..",SEARCH_GOTO_LINE,KEY_MASK_CMD|KEY_L);
  1156. search_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1157. debug_menu = memnew( MenuButton );
  1158. menu_hb->add_child(debug_menu);
  1159. debug_menu->set_text("Debug");
  1160. debug_menu->get_popup()->add_item("Toggle Breakpoint",DEBUG_TOGGLE_BREAKPOINT,KEY_F9);
  1161. debug_menu->get_popup()->add_separator();
  1162. debug_menu->get_popup()->add_item("Step Over",DEBUG_NEXT,KEY_F10);
  1163. debug_menu->get_popup()->add_item("Step Into",DEBUG_STEP,KEY_F11);
  1164. debug_menu->get_popup()->add_separator();
  1165. debug_menu->get_popup()->add_item("Break",DEBUG_BREAK);
  1166. debug_menu->get_popup()->add_item("Continue",DEBUG_CONTINUE);
  1167. debug_menu->get_popup()->add_separator();
  1168. debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
  1169. debug_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1170. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1171. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1172. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  1173. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1174. window_menu = memnew( MenuButton );
  1175. menu_hb->add_child(window_menu);
  1176. window_menu->set_text("Window");
  1177. window_menu->get_popup()->add_item("Close",WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
  1178. window_menu->get_popup()->add_separator();
  1179. window_menu->get_popup()->add_item("Move Left",WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
  1180. window_menu->get_popup()->add_item("Move Right",WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
  1181. window_menu->get_popup()->add_separator();
  1182. window_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1183. help_menu = memnew( MenuButton );
  1184. menu_hb->add_child(help_menu);
  1185. help_menu->set_text("Help");
  1186. help_menu->get_popup()->add_item("Contextual", HELP_CONTEXTUAL, KEY_MASK_SHIFT|KEY_F1);
  1187. help_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1188. tab_container->connect("tab_changed", this,"_tab_changed");
  1189. find_replace_dialog = memnew(FindReplaceDialog);
  1190. add_child(find_replace_dialog);
  1191. erase_tab_confirm = memnew( ConfirmationDialog );
  1192. add_child(erase_tab_confirm);
  1193. erase_tab_confirm->connect("confirmed", this,"_close_current_tab");
  1194. goto_line_dialog = memnew(GotoLineDialog);
  1195. add_child(goto_line_dialog);
  1196. debugger = memnew( ScriptEditorDebugger(editor) );
  1197. debugger->connect("goto_script_line",this,"_goto_script_line");
  1198. debugger->connect("show_debugger",this,"_show_debugger");
  1199. disk_changed = memnew( ConfirmationDialog );
  1200. {
  1201. VBoxContainer *vbc = memnew( VBoxContainer );
  1202. disk_changed->add_child(vbc);
  1203. disk_changed->set_child_rect(vbc);
  1204. Label *dl = memnew( Label );
  1205. dl->set_text("The following files are newer on disk.\nWhat action should be taken?:");
  1206. vbc->add_child(dl);
  1207. disk_changed_list = memnew( Tree );
  1208. vbc->add_child(disk_changed_list);
  1209. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  1210. disk_changed->connect("confirmed",this,"_reload_scripts");
  1211. disk_changed->get_ok()->set_text("Reload");
  1212. disk_changed->add_button("Resave",!OS::get_singleton()->get_swap_ok_cancel(),"resave");
  1213. disk_changed->connect("custom_action",this,"_resave_scripts");
  1214. }
  1215. add_child(disk_changed);
  1216. script_editor=this;
  1217. quick_open = memnew( ScriptEditorQuickOpen );
  1218. add_child(quick_open);
  1219. quick_open->connect("goto_line",this,"_goto_script_line2");
  1220. v_split->add_child(debugger);
  1221. debugger->connect("breaked",this,"_breaked");
  1222. // debugger_gui->hide();
  1223. }
  1224. void ScriptEditorPlugin::edit(Object *p_object) {
  1225. if (!p_object->cast_to<Script>())
  1226. return;
  1227. script_editor->edit(p_object->cast_to<Script>());
  1228. }
  1229. bool ScriptEditorPlugin::handles(Object *p_object) const {
  1230. return p_object->is_type("Script");
  1231. }
  1232. void ScriptEditorPlugin::make_visible(bool p_visible) {
  1233. if (p_visible) {
  1234. script_editor->show();
  1235. script_editor->set_process(true);
  1236. script_editor->ensure_select_current();
  1237. } else {
  1238. script_editor->hide();
  1239. script_editor->set_process(false);
  1240. }
  1241. }
  1242. void ScriptEditorPlugin::selected_notify() {
  1243. script_editor->ensure_select_current();
  1244. }
  1245. Dictionary ScriptEditorPlugin::get_state() const {
  1246. return script_editor->get_state();
  1247. }
  1248. void ScriptEditorPlugin::set_state(const Dictionary& p_state) {
  1249. script_editor->set_state(p_state);
  1250. }
  1251. void ScriptEditorPlugin::clear() {
  1252. script_editor->clear();
  1253. }
  1254. void ScriptEditorPlugin::save_external_data() {
  1255. script_editor->save_external_data();
  1256. }
  1257. void ScriptEditorPlugin::apply_changes() {
  1258. script_editor->apply_scripts();
  1259. }
  1260. void ScriptEditorPlugin::restore_global_state() {
  1261. if (bool(EDITOR_DEF("text_editor/restore_scripts_on_load",true))) {
  1262. script_editor->_load_files_state();
  1263. }
  1264. }
  1265. void ScriptEditorPlugin::save_global_state() {
  1266. if (bool(EDITOR_DEF("text_editor/restore_scripts_on_load",true))) {
  1267. script_editor->_save_files_state();
  1268. }
  1269. }
  1270. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  1271. return script_editor->get_breakpoints(p_breakpoints);
  1272. }
  1273. ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
  1274. editor=p_node;
  1275. script_editor = memnew( ScriptEditor(p_node) );
  1276. editor->get_viewport()->add_child(script_editor);
  1277. script_editor->set_area_as_parent_rect();
  1278. script_editor->hide();
  1279. EDITOR_DEF("external_editor/use_external_editor",false);
  1280. EDITOR_DEF("external_editor/exec_path","");
  1281. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"external_editor/exec_path",PROPERTY_HINT_GLOBAL_FILE));
  1282. EDITOR_DEF("external_editor/exec_flags","");
  1283. }
  1284. ScriptEditorPlugin::~ScriptEditorPlugin()
  1285. {
  1286. }