script_editor_plugin.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  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_SCENE) {
  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. Color keyword_color= EDITOR_DEF("text_editor/keyword_color",Color(0.5,0.0,0.2));
  150. get_text_edit()->set_syntax_coloring(true);
  151. List<String> keywords;
  152. script->get_language()->get_reserved_words(&keywords);
  153. for(List<String>::Element *E=keywords.front();E;E=E->next()) {
  154. get_text_edit()->add_keyword_color(E->get(),keyword_color);
  155. }
  156. //colorize core types
  157. Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0));
  158. get_text_edit()->add_keyword_color("Vector2",basetype_color);
  159. get_text_edit()->add_keyword_color("Vector3",basetype_color);
  160. get_text_edit()->add_keyword_color("Plane",basetype_color);
  161. get_text_edit()->add_keyword_color("Quat",basetype_color);
  162. get_text_edit()->add_keyword_color("AABB",basetype_color);
  163. get_text_edit()->add_keyword_color("Matrix3",basetype_color);
  164. get_text_edit()->add_keyword_color("Transform",basetype_color);
  165. get_text_edit()->add_keyword_color("Color",basetype_color);
  166. get_text_edit()->add_keyword_color("Image",basetype_color);
  167. get_text_edit()->add_keyword_color("InputEvent",basetype_color);
  168. //colorize engine types
  169. Color type_color= EDITOR_DEF("text_editor/engine_type_color",Color(0.0,0.2,0.4));
  170. List<String> types;
  171. ObjectTypeDB::get_type_list(&types);
  172. for(List<String>::Element *E=types.front();E;E=E->next()) {
  173. get_text_edit()->add_keyword_color(E->get(),type_color);
  174. }
  175. //colorize comments
  176. Color comment_color = EDITOR_DEF("text_editor/comment_color",Color::hex(0x797e7eff));
  177. List<String> comments;
  178. script->get_language()->get_comment_delimiters(&comments);
  179. for(List<String>::Element *E=comments.front();E;E=E->next()) {
  180. String comment = E->get();
  181. String beg = comment.get_slice(" ",0);
  182. String end = comment.get_slice_count(" ")>1?comment.get_slice(" ",1):String();
  183. get_text_edit()->add_color_region(beg,end,comment_color,end=="");
  184. }
  185. //colorize strings
  186. Color string_color = EDITOR_DEF("text_editor/string_color",Color::hex(0x6b6f00ff));
  187. List<String> strings;
  188. script->get_language()->get_string_delimiters(&strings);
  189. for (List<String>::Element *E=strings.front();E;E=E->next()) {
  190. String string = E->get();
  191. String beg = string.get_slice(" ",0);
  192. String end = string.get_slice_count(" ")>1?string.get_slice(" ",1):String();
  193. get_text_edit()->add_color_region(beg,end,string_color,end=="");
  194. }
  195. //colorize symbols
  196. Color symbol_color= EDITOR_DEF("text_editor/symbol_color",Color::hex(0x005291ff));
  197. get_text_edit()->set_symbol_color(symbol_color);
  198. }
  199. void ScriptTextEditor::reload_text() {
  200. ERR_FAIL_COND(script.is_null()) ;
  201. get_text_edit()->set_text(script->get_source_code());
  202. get_text_edit()->clear_undo_history();
  203. _line_col_changed();
  204. }
  205. void ScriptTextEditor::_notification(int p_what) {
  206. if (p_what==NOTIFICATION_READY) {
  207. _update_name();
  208. }
  209. }
  210. void ScriptTextEditor::_update_name() {
  211. String name;
  212. if (script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  213. name=script->get_path().get_file();
  214. if (get_text_edit()->get_version()!=get_text_edit()->get_saved_version()) {
  215. name+="(*)";
  216. }
  217. } else if (script->get_name()!="")
  218. name=script->get_name();
  219. else
  220. name=script->get_type()+"("+itos(script->get_instance_ID())+")";
  221. if (name!=String(get_name())) {
  222. set_name(name);
  223. }
  224. if (!has_meta("_tab_icon")) {
  225. if (get_parent_control() && get_parent_control()->has_icon(script->get_type(),"EditorIcons")) {
  226. set_meta("_tab_icon",get_parent_control()->get_icon(script->get_type(),"EditorIcons"));
  227. }
  228. }
  229. }
  230. void ScriptTextEditor::set_edited_script(const Ref<Script>& p_script) {
  231. ERR_FAIL_COND(!script.is_null());
  232. script=p_script;
  233. _load_theme_settings();
  234. get_text_edit()->set_text(script->get_source_code());
  235. get_text_edit()->clear_undo_history();
  236. get_text_edit()->tag_saved_version();
  237. _update_name();
  238. _line_col_changed();
  239. }
  240. void ScriptTextEditor::_validate_script() {
  241. String errortxt;
  242. int line=-1,col;
  243. TextEdit *te=get_text_edit();
  244. String text = te->get_text();
  245. List<String> fnc;
  246. if (!script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  247. String error_text="error("+itos(line)+","+itos(col)+"): "+errortxt;
  248. set_error(error_text);
  249. } else {
  250. set_error("");
  251. line=-1;
  252. if (!script->is_tool()) {
  253. script->set_source_code(text);
  254. script->update_exports();
  255. //script->reload(); //will update all the variables in property editors
  256. }
  257. functions.clear();
  258. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  259. functions.push_back(E->get());
  260. }
  261. }
  262. line--;
  263. for(int i=0;i<te->get_line_count();i++) {
  264. te->set_line_as_marked(i,line==i);
  265. }
  266. _update_name();
  267. }
  268. void ScriptTextEditor::_code_complete_script(const String& p_code, const String& p_keyword,int p_line, List<String>* r_options) {
  269. Error err = script->get_language()->complete_keyword(p_code,p_line,script->get_path().get_base_dir(),p_keyword,r_options);
  270. }
  271. ScriptTextEditor::ScriptTextEditor() {
  272. get_text_edit()->set_draw_tabs(true);
  273. }
  274. /*** SCRIPT EDITOR ******/
  275. String ScriptEditor::_get_debug_tooltip(const String&p_text,Node *_ste) {
  276. ScriptTextEditor *ste=_ste->cast_to<ScriptTextEditor>();
  277. String val = debugger->get_var_value(p_text);
  278. if (val!=String()) {
  279. return p_text+": "+val;
  280. } else {
  281. return String();
  282. }
  283. }
  284. void ScriptEditor::_breaked(bool p_breaked,bool p_can_debug) {
  285. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug));
  286. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug) );
  287. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked );
  288. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked );
  289. }
  290. void ScriptEditor::_show_debugger(bool p_show) {
  291. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), p_show);
  292. }
  293. void ScriptEditor::_goto_script_line2(int p_line) {
  294. int selected = tab_container->get_current_tab();
  295. if (selected<0 || selected>=tab_container->get_child_count())
  296. return;
  297. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  298. if (!current)
  299. return;
  300. current->get_text_edit()->cursor_set_line(p_line);
  301. }
  302. void ScriptEditor::_goto_script_line(REF p_script,int p_line) {
  303. editor->push_item(p_script.ptr());
  304. _goto_script_line2(p_line);
  305. }
  306. void ScriptEditor::_close_current_tab() {
  307. int selected = tab_container->get_current_tab();
  308. if (selected<0 || selected>=tab_container->get_child_count())
  309. return;
  310. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  311. if (!current)
  312. return;
  313. apply_scripts();
  314. int idx = tab_container->get_current_tab();
  315. memdelete(current);
  316. if (idx>=tab_container->get_child_count())
  317. idx=tab_container->get_child_count()-1;
  318. if (idx>=0)
  319. tab_container->set_current_tab(idx);
  320. _update_window_menu();
  321. _save_files_state();
  322. }
  323. void ScriptEditor::_resave_scripts(const String& p_str) {
  324. apply_scripts();
  325. for(int i=0;i<tab_container->get_child_count();i++) {
  326. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  327. if (!ste)
  328. continue;
  329. Ref<Script> script = ste->get_edited_script();
  330. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  331. continue; //internal script, who cares
  332. editor->save_resource(script);
  333. ste->get_text_edit()->tag_saved_version();
  334. }
  335. disk_changed->hide();
  336. }
  337. void ScriptEditor::_reload_scripts(){
  338. for(int i=0;i<tab_container->get_child_count();i++) {
  339. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  340. if (!ste) {
  341. continue;
  342. }
  343. Ref<Script> script = ste->get_edited_script();
  344. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  345. continue; //internal script, who cares
  346. }
  347. Ref<Script> rel_script = ResourceLoader::load(script->get_path(),script->get_type(),true);
  348. ERR_CONTINUE(!rel_script.is_valid());
  349. script->set_source_code( rel_script->get_source_code() );
  350. script->set_last_modified_time( rel_script->get_last_modified_time() );
  351. script->reload();
  352. ste->reload_text();
  353. }
  354. disk_changed->hide();
  355. }
  356. void ScriptEditor::_res_saved_callback(const Ref<Resource>& p_res) {
  357. for(int i=0;i<tab_container->get_child_count();i++) {
  358. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  359. if (!ste) {
  360. continue;
  361. }
  362. Ref<Script> script = ste->get_edited_script();
  363. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  364. continue; //internal script, who cares
  365. }
  366. if (script==p_res) {
  367. ste->get_text_edit()->tag_saved_version();
  368. }
  369. ste->_update_name();
  370. }
  371. }
  372. bool ScriptEditor::_test_script_times_on_disk() {
  373. disk_changed_list->clear();
  374. TreeItem *r = disk_changed_list->create_item();
  375. disk_changed_list->set_hide_root(true);
  376. bool all_ok=true;
  377. for(int i=0;i<tab_container->get_child_count();i++) {
  378. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  379. if (!ste)
  380. continue;
  381. Ref<Script> script = ste->get_edited_script();
  382. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  383. continue; //internal script, who cares
  384. uint64_t last_date = script->get_last_modified_time();
  385. uint64_t date = FileAccess::get_modified_time(script->get_path());
  386. //printf("last date: %lli vs date: %lli\n",last_date,date);
  387. if (last_date!=date) {
  388. TreeItem *ti = disk_changed_list->create_item(r);
  389. ti->set_text(0,script->get_path().get_file());
  390. all_ok=false;
  391. //r->set_metadata(0,);
  392. }
  393. }
  394. if (!all_ok)
  395. disk_changed->call_deferred("popup_centered_ratio",0.5);
  396. return all_ok;
  397. }
  398. void ScriptEditor::_menu_option(int p_option) {
  399. if (p_option==FILE_OPEN) {
  400. editor->open_resource("Script");
  401. return;
  402. }
  403. int selected = tab_container->get_current_tab();
  404. if (selected<0 || selected>=tab_container->get_child_count())
  405. return;
  406. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  407. if (!current)
  408. return;
  409. switch(p_option) {
  410. case FILE_SAVE: {
  411. if (!_test_script_times_on_disk())
  412. return;
  413. editor->save_resource( current->get_edited_script() );
  414. } break;
  415. case FILE_SAVE_AS: {
  416. editor->save_resource_as( current->get_edited_script() );
  417. } break;
  418. case FILE_SAVE_ALL: {
  419. if (!_test_script_times_on_disk())
  420. return;
  421. for(int i=0;i<tab_container->get_child_count();i++) {
  422. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  423. if (!ste)
  424. continue;
  425. Ref<Script> script = ste->get_edited_script();
  426. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  427. continue; //internal script, who cares
  428. editor->save_resource( script );
  429. }
  430. } break;
  431. case EDIT_UNDO: {
  432. current->get_text_edit()->undo();
  433. } break;
  434. case EDIT_REDO: {
  435. current->get_text_edit()->redo();
  436. } break;
  437. case EDIT_CUT: {
  438. current->get_text_edit()->cut();
  439. } break;
  440. case EDIT_COPY: {
  441. current->get_text_edit()->copy();
  442. } break;
  443. case EDIT_PASTE: {
  444. current->get_text_edit()->paste();
  445. } break;
  446. case EDIT_SELECT_ALL: {
  447. current->get_text_edit()->select_all();
  448. } break;
  449. case EDIT_COMPLETE: {
  450. current->get_text_edit()->query_code_comple();
  451. } break;
  452. case EDIT_AUTO_INDENT: {
  453. TextEdit *te = current->get_text_edit();
  454. String text = te->get_text();
  455. Ref<Script> scr = current->get_edited_script();
  456. if (scr.is_null())
  457. return;
  458. int begin,end;
  459. if (te->is_selection_active()) {
  460. begin=te->get_selection_from_line();
  461. end=te->get_selection_to_line();
  462. } else {
  463. begin=0;
  464. end=te->get_line_count()-1;
  465. }
  466. scr->get_language()->auto_indent_code(text,begin,end);
  467. te->set_text(text);
  468. } break;
  469. case SEARCH_FIND: {
  470. find_replace_dialog->set_text_edit(current->get_text_edit());
  471. find_replace_dialog->popup_search();
  472. } break;
  473. case SEARCH_FIND_NEXT: {
  474. find_replace_dialog->set_text_edit(current->get_text_edit());
  475. find_replace_dialog->search_next();
  476. } break;
  477. case SEARCH_REPLACE: {
  478. find_replace_dialog->set_text_edit(current->get_text_edit());
  479. find_replace_dialog->popup_replace();
  480. } break;
  481. case SEARCH_LOCATE_FUNCTION: {
  482. if (!current)
  483. return;
  484. quick_open->popup(current->get_functions());
  485. } break;
  486. case SEARCH_GOTO_LINE: {
  487. goto_line_dialog->popup_find_line(current->get_text_edit());
  488. } break;
  489. case DEBUG_TOGGLE_BREAKPOINT: {
  490. int line=current->get_text_edit()->cursor_get_line();
  491. bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
  492. current->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  493. } break;
  494. case DEBUG_NEXT: {
  495. if (debugger)
  496. debugger->debug_next();
  497. } break;
  498. case DEBUG_STEP: {
  499. if (debugger)
  500. debugger->debug_step();
  501. } break;
  502. case DEBUG_BREAK: {
  503. if (debugger)
  504. debugger->debug_break();
  505. } break;
  506. case DEBUG_CONTINUE: {
  507. if (debugger)
  508. debugger->debug_continue();
  509. } break;
  510. case DEBUG_SHOW: {
  511. if (debugger) {
  512. bool visible = debug_menu->get_popup()->is_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW) );
  513. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), !visible);
  514. if (visible)
  515. debugger->hide();
  516. else
  517. debugger->show();
  518. }
  519. } break;
  520. case HELP_CONTEXTUAL: {
  521. String text = current->get_text_edit()->get_selection_text();
  522. if (text == "")
  523. text = current->get_text_edit()->get_word_under_cursor();
  524. if (text != "")
  525. editor->emit_signal("request_help", text);
  526. } break;
  527. case WINDOW_CLOSE: {
  528. erase_tab_confirm->set_text("Close Tab?:\n\""+current->get_name()+"\"");
  529. erase_tab_confirm->popup_centered(Point2(250,80));
  530. } break;
  531. case WINDOW_MOVE_LEFT: {
  532. if (tab_container->get_current_tab()>0) {
  533. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()-1);
  534. tab_container->move_child(current,tab_container->get_current_tab()-1);
  535. _update_window_menu();
  536. }
  537. } break;
  538. case WINDOW_MOVE_RIGHT: {
  539. if (tab_container->get_current_tab()<tab_container->get_child_count()-1) {
  540. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()+1);
  541. tab_container->move_child(current,tab_container->get_current_tab()+1);
  542. _update_window_menu();
  543. }
  544. } break;
  545. default: {
  546. if (p_option>=WINDOW_SELECT_BASE) {
  547. tab_container->set_current_tab(p_option-WINDOW_SELECT_BASE);
  548. }
  549. }
  550. }
  551. }
  552. void ScriptEditor::_tab_changed(int p_which) {
  553. ensure_select_current();
  554. }
  555. void ScriptEditor::_notification(int p_what) {
  556. if (p_what==NOTIFICATION_ENTER_SCENE) {
  557. editor->connect("play_pressed",this,"_editor_play");
  558. editor->connect("pause_pressed",this,"_editor_pause");
  559. editor->connect("stop_pressed",this,"_editor_stop");
  560. editor->connect("script_add_function_request",this,"_add_callback");
  561. editor->connect("resource_saved",this,"_res_saved_callback");
  562. }
  563. if (p_what==NOTIFICATION_READY) {
  564. _update_window_menu();
  565. }
  566. if (p_what==NOTIFICATION_EXIT_SCENE) {
  567. editor->disconnect("play_pressed",this,"_editor_play");
  568. editor->disconnect("pause_pressed",this,"_editor_pause");
  569. editor->disconnect("stop_pressed",this,"_editor_stop");
  570. }
  571. if (p_what==MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  572. _test_script_times_on_disk();
  573. }
  574. if (p_what==NOTIFICATION_PROCESS) {
  575. }
  576. }
  577. static const Node * _find_node_with_script(const Node* p_node, const RefPtr & p_script) {
  578. if (p_node->get_script()==p_script)
  579. return p_node;
  580. for(int i=0;i<p_node->get_child_count();i++) {
  581. const Node *result = _find_node_with_script(p_node->get_child(i),p_script);
  582. if (result)
  583. return result;
  584. }
  585. return NULL;
  586. }
  587. Dictionary ScriptEditor::get_state() const {
  588. apply_scripts();
  589. Dictionary state;
  590. Array paths;
  591. int open=-1;
  592. for(int i=0;i<tab_container->get_child_count();i++) {
  593. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  594. if (!ste)
  595. continue;
  596. Ref<Script> script = ste->get_edited_script();
  597. if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  598. paths.push_back(script->get_path());
  599. } else {
  600. const Node *owner = _find_node_with_script(get_scene()->get_root(),script.get_ref_ptr());
  601. if (owner)
  602. paths.push_back(owner->get_path());
  603. }
  604. if (i==tab_container->get_current_tab())
  605. open=i;
  606. }
  607. if (paths.size())
  608. state["sources"]=paths;
  609. if (open!=-1)
  610. state["current"]=open;
  611. return state;
  612. }
  613. void ScriptEditor::set_state(const Dictionary& p_state) {
  614. print_line("attempt set state: "+String(Variant(p_state)));
  615. if (!p_state.has("sources"))
  616. return; //bleh
  617. Array sources = p_state["sources"];
  618. for(int i=0;i<sources.size();i++) {
  619. Variant source=sources[i];
  620. Ref<Script> script;
  621. if (source.get_type()==Variant::NODE_PATH) {
  622. Node *owner=get_scene()->get_root()->get_node(source);
  623. if (!owner)
  624. continue;
  625. script = owner->get_script();
  626. } else if (source.get_type()==Variant::STRING) {
  627. script = ResourceLoader::load(source,"Script");
  628. }
  629. if (script.is_null()) //ah well..
  630. continue;
  631. editor->call("_resource_selected",script);
  632. }
  633. if (p_state.has("current")) {
  634. tab_container->set_current_tab(p_state["current"]);
  635. }
  636. }
  637. void ScriptEditor::clear() {
  638. List<ScriptTextEditor*> stes;
  639. for(int i=0;i<tab_container->get_child_count();i++) {
  640. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  641. if (!ste)
  642. continue;
  643. stes.push_back(ste);
  644. }
  645. while(stes.size()) {
  646. memdelete(stes.front()->get());
  647. stes.pop_front();
  648. }
  649. int idx = tab_container->get_current_tab();
  650. if (idx>=tab_container->get_child_count())
  651. idx=tab_container->get_child_count()-1;
  652. if (idx>=0)
  653. tab_container->set_current_tab(idx);
  654. _update_window_menu();
  655. }
  656. void ScriptEditor::_save_files_state() {
  657. return; //no thank you
  658. String rpath="_open_scripts_"+Globals::get_singleton()->get_resource_path();
  659. rpath=rpath.replace("\\","_-_");
  660. rpath=rpath.replace("/","_-_");
  661. rpath=rpath.replace(":","_");
  662. Vector<String> scripts;
  663. for(int i=0;i<tab_container->get_child_count();i++) {
  664. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  665. if (!ste)
  666. continue;
  667. Ref<Script> script = ste->get_edited_script();
  668. if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  669. scripts.push_back(script->get_path());
  670. }
  671. }
  672. EditorSettings::get_singleton()->set(rpath,scripts);
  673. EditorSettings::get_singleton()->save();
  674. }
  675. void ScriptEditor::_load_files_state() {
  676. return;
  677. String rpath="_open_scripts_"+Globals::get_singleton()->get_resource_path();
  678. rpath=rpath.replace("\\","_-_");
  679. rpath=rpath.replace("/","_-_");
  680. rpath=rpath.replace(":","_");
  681. if (EditorSettings::get_singleton()->has(rpath)) {
  682. Vector<String> open_files=EditorSettings::get_singleton()->get("rpath");
  683. for(int i=0;i<open_files.size();i++) {
  684. Ref<Script> scr = ResourceLoader::load(open_files[i]);
  685. if (!scr.is_valid())
  686. continue;
  687. editor->edit_resource(scr);
  688. }
  689. }
  690. }
  691. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  692. for(int i=0;i<tab_container->get_child_count();i++) {
  693. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  694. if (!ste)
  695. continue;
  696. List<int> bpoints;
  697. ste->get_text_edit()->get_breakpoints(&bpoints);
  698. Ref<Script> script = ste->get_edited_script();
  699. String base = script->get_path();
  700. ERR_CONTINUE( base.begins_with("local://") || base=="" );
  701. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  702. p_breakpoints->push_back(base+":"+itos(E->get()+1));
  703. }
  704. }
  705. }
  706. void ScriptEditor::_bind_methods() {
  707. ObjectTypeDB::bind_method("_tab_changed",&ScriptEditor::_tab_changed);
  708. ObjectTypeDB::bind_method("_menu_option",&ScriptEditor::_menu_option);
  709. ObjectTypeDB::bind_method("_close_current_tab",&ScriptEditor::_close_current_tab);
  710. ObjectTypeDB::bind_method("_editor_play",&ScriptEditor::_editor_play);
  711. ObjectTypeDB::bind_method("_editor_pause",&ScriptEditor::_editor_pause);
  712. ObjectTypeDB::bind_method("_editor_stop",&ScriptEditor::_editor_stop);
  713. ObjectTypeDB::bind_method("_add_callback",&ScriptEditor::_add_callback);
  714. ObjectTypeDB::bind_method("_reload_scripts",&ScriptEditor::_reload_scripts);
  715. ObjectTypeDB::bind_method("_resave_scripts",&ScriptEditor::_resave_scripts);
  716. ObjectTypeDB::bind_method("_res_saved_callback",&ScriptEditor::_res_saved_callback);
  717. ObjectTypeDB::bind_method("_goto_script_line",&ScriptEditor::_goto_script_line);
  718. ObjectTypeDB::bind_method("_goto_script_line2",&ScriptEditor::_goto_script_line2);
  719. ObjectTypeDB::bind_method("_breaked",&ScriptEditor::_breaked);
  720. ObjectTypeDB::bind_method("_show_debugger",&ScriptEditor::_show_debugger);
  721. ObjectTypeDB::bind_method("_get_debug_tooltip",&ScriptEditor::_get_debug_tooltip);
  722. }
  723. void ScriptEditor::ensure_focus_current() {
  724. int cidx = tab_container->get_current_tab();
  725. if (cidx<0 || cidx>=tab_container->get_tab_count());
  726. Control *c = tab_container->get_child(cidx)->cast_to<Control>();
  727. if (!c)
  728. return;
  729. ScriptTextEditor *ste = c->cast_to<ScriptTextEditor>();
  730. if (!ste)
  731. return;
  732. ste->get_text_edit()->grab_focus();
  733. }
  734. void ScriptEditor::ensure_select_current() {
  735. if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
  736. ScriptTextEditor *ste = tab_container->get_child(tab_container->get_current_tab())->cast_to<ScriptTextEditor>();
  737. if (!ste)
  738. return;
  739. Ref<Script> script = ste->get_edited_script();
  740. ste->get_text_edit()->grab_focus();
  741. }
  742. }
  743. void ScriptEditor::edit(const Ref<Script>& p_script) {
  744. if (p_script.is_null())
  745. return;
  746. // see if already has it
  747. if (p_script->get_path().is_resource_file() && bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor"))) {
  748. String path = EditorSettings::get_singleton()->get("external_editor/exec_path");
  749. String flags = EditorSettings::get_singleton()->get("external_editor/exec_flags");
  750. List<String> args;
  751. flags=flags.strip_edges();
  752. if (flags!=String()) {
  753. Vector<String> flagss = flags.split(" ",false);
  754. for(int i=0;i<flagss.size();i++)
  755. args.push_back(flagss[i]);
  756. }
  757. args.push_back(Globals::get_singleton()->globalize_path(p_script->get_path()));
  758. Error err = OS::get_singleton()->execute(path,args,false);
  759. if (err==OK)
  760. return;
  761. WARN_PRINT("Couldn't open external text editor, using internal");
  762. }
  763. for(int i=0;i<tab_container->get_child_count();i++) {
  764. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  765. if (!ste)
  766. continue;
  767. if (ste->get_edited_script()==p_script) {
  768. if (tab_container->get_current_tab()!=i)
  769. tab_container->set_current_tab(i);
  770. ste->get_text_edit()->grab_focus();
  771. return;
  772. }
  773. }
  774. // doesn't have it, make a new one
  775. ScriptTextEditor *ste = memnew( ScriptTextEditor );
  776. ste->set_edited_script(p_script);
  777. ste->get_text_edit()->set_tooltip_request_func(this,"_get_debug_tooltip",ste);
  778. tab_container->add_child(ste);
  779. tab_container->set_current_tab(tab_container->get_tab_count()-1);
  780. _update_window_menu();
  781. _save_files_state();
  782. }
  783. void ScriptEditor::save_external_data() {
  784. apply_scripts();
  785. for(int i=0;i<tab_container->get_child_count();i++) {
  786. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  787. if (!ste)
  788. continue;
  789. Ref<Script> script = ste->get_edited_script();
  790. if (script->get_path()!="" && script->get_path().find("local://")==-1 &&script->get_path().find("::")==-1) {
  791. //external script, save it
  792. editor->save_resource(script);
  793. //ResourceSaver::save(script->get_path(),script);
  794. }
  795. }
  796. }
  797. void ScriptEditor::apply_scripts() const {
  798. for(int i=0;i<tab_container->get_child_count();i++) {
  799. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  800. if (!ste)
  801. continue;
  802. ste->apply_code();
  803. }
  804. }
  805. void ScriptEditor::_editor_play() {
  806. debugger->start();
  807. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  808. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  809. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false );
  810. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  811. //debugger_gui->start_listening(Globals::get_singleton()->get("debug/debug_port"));
  812. }
  813. void ScriptEditor::_editor_pause() {
  814. }
  815. void ScriptEditor::_editor_stop() {
  816. debugger->stop();
  817. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  818. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  819. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  820. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  821. }
  822. void ScriptEditor::_update_window_menu() {
  823. int idx=0;
  824. for(int i=0;i<tab_container->get_child_count();i++) {
  825. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  826. if (!ste)
  827. continue;
  828. idx++;
  829. }
  830. if (idx==0) {
  831. window_menu->set_disabled(true);
  832. edit_menu->set_disabled(true);
  833. search_menu->set_disabled(true);
  834. return;
  835. } else {
  836. window_menu->set_disabled(false);
  837. edit_menu->set_disabled(false);
  838. search_menu->set_disabled(false);
  839. }
  840. window_menu->get_popup()->clear();
  841. window_menu->get_popup()->add_item("Close",WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
  842. window_menu->get_popup()->add_separator();
  843. window_menu->get_popup()->add_item("Move Left",WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
  844. window_menu->get_popup()->add_item("Move Right",WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
  845. window_menu->get_popup()->add_separator();
  846. idx=0;
  847. for(int i=0;i<tab_container->get_child_count();i++) {
  848. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  849. if (!ste)
  850. continue;
  851. String n = ste->get_name();
  852. uint32_t accel=0;
  853. if (idx<9) {
  854. accel=KEY_MASK_ALT|(KEY_1+idx);
  855. }
  856. window_menu->get_popup()->add_item(n,WINDOW_SELECT_BASE+idx,accel);
  857. idx++;
  858. }
  859. }
  860. void ScriptEditor::_add_callback(Object *p_obj, const String& p_function, const StringArray& p_args) {
  861. print_line("add callback! hohoho");
  862. ERR_FAIL_COND(!p_obj);
  863. Ref<Script> script = p_obj->get_script();
  864. ERR_FAIL_COND( !script.is_valid() );
  865. editor->push_item(script.ptr());
  866. for(int i=0;i<tab_container->get_child_count();i++) {
  867. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  868. if (!ste)
  869. continue;
  870. if (ste->get_edited_script()!=script)
  871. continue;
  872. String code = ste->get_text_edit()->get_text();
  873. int pos = script->get_language()->find_function(code,p_function);
  874. if (pos==-1) {
  875. //does not exist
  876. pos=ste->get_text_edit()->get_line_count()+2;
  877. String func = script->get_language()->make_function("",p_function,p_args);
  878. //code=code+func;
  879. ste->get_text_edit()->cursor_set_line(pos+1);
  880. ste->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  881. ste->get_text_edit()->insert_text_at_cursor("\n\n"+func);
  882. }
  883. tab_container->set_current_tab(i);
  884. ste->get_text_edit()->cursor_set_line(pos);
  885. ste->get_text_edit()->cursor_set_column(1);
  886. break;
  887. }
  888. }
  889. ScriptEditor::ScriptEditor(EditorNode *p_editor) {
  890. editor=p_editor;
  891. menu_hb = memnew( HBoxContainer );
  892. add_child(menu_hb);
  893. v_split = memnew( VSplitContainer );
  894. add_child(v_split);
  895. v_split->set_v_size_flags(SIZE_EXPAND_FILL);
  896. tab_container = memnew( TabContainer );
  897. v_split->add_child(tab_container);
  898. tab_container->set_v_size_flags(SIZE_EXPAND_FILL);
  899. file_menu = memnew( MenuButton );
  900. menu_hb->add_child(file_menu);
  901. file_menu->set_text("File");
  902. file_menu->get_popup()->add_item("Open",FILE_OPEN);
  903. file_menu->get_popup()->add_item("Save",FILE_SAVE,KEY_MASK_ALT|KEY_S);
  904. file_menu->get_popup()->add_item("Save As..",FILE_SAVE_AS);
  905. file_menu->get_popup()->add_item("Save All",FILE_SAVE_ALL,KEY_MASK_CMD|KEY_MASK_SHIFT|KEY_S);
  906. file_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  907. edit_menu = memnew( MenuButton );
  908. menu_hb->add_child(edit_menu);
  909. edit_menu->set_text("Edit");
  910. edit_menu->get_popup()->add_item("Undo");
  911. edit_menu->get_popup()->add_item("Redo");
  912. edit_menu->get_popup()->add_separator();
  913. edit_menu->get_popup()->add_item("Cut",EDIT_CUT,KEY_MASK_CMD|KEY_X);
  914. edit_menu->get_popup()->add_item("Copy",EDIT_COPY,KEY_MASK_CMD|KEY_C);
  915. edit_menu->get_popup()->add_item("Paste",EDIT_PASTE,KEY_MASK_CMD|KEY_V);
  916. edit_menu->get_popup()->add_separator();
  917. edit_menu->get_popup()->add_item("Select All",EDIT_SELECT_ALL,KEY_MASK_CMD|KEY_A);
  918. edit_menu->get_popup()->add_separator();
  919. edit_menu->get_popup()->add_item("Complete Symbol",EDIT_COMPLETE,KEY_MASK_CMD|KEY_SPACE);
  920. edit_menu->get_popup()->add_item("Auto Indent",EDIT_AUTO_INDENT,KEY_MASK_CMD|KEY_I);
  921. edit_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  922. search_menu = memnew( MenuButton );
  923. menu_hb->add_child(search_menu);
  924. search_menu->set_text("Search");
  925. search_menu->get_popup()->add_item("Find..",SEARCH_FIND,KEY_MASK_CMD|KEY_F);
  926. search_menu->get_popup()->add_item("Find Next",SEARCH_FIND_NEXT,KEY_MASK_CMD|KEY_G);
  927. search_menu->get_popup()->add_item("Replace..",SEARCH_REPLACE,KEY_MASK_CMD|KEY_R);
  928. search_menu->get_popup()->add_separator();
  929. search_menu->get_popup()->add_item("Goto Function..",SEARCH_LOCATE_FUNCTION,KEY_MASK_SHIFT|KEY_MASK_CMD|KEY_F);
  930. search_menu->get_popup()->add_item("Goto Line..",SEARCH_GOTO_LINE,KEY_MASK_CMD|KEY_L);
  931. search_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  932. debug_menu = memnew( MenuButton );
  933. menu_hb->add_child(debug_menu);
  934. debug_menu->set_text("Debug");
  935. debug_menu->get_popup()->add_item("Toggle Breakpoint",DEBUG_TOGGLE_BREAKPOINT,KEY_F9);
  936. debug_menu->get_popup()->add_separator();
  937. debug_menu->get_popup()->add_item("Step Over",DEBUG_NEXT,KEY_F10);
  938. debug_menu->get_popup()->add_item("Step Into",DEBUG_STEP,KEY_F11);
  939. debug_menu->get_popup()->add_separator();
  940. debug_menu->get_popup()->add_item("Break",DEBUG_BREAK);
  941. debug_menu->get_popup()->add_item("Continue",DEBUG_CONTINUE);
  942. debug_menu->get_popup()->add_separator();
  943. debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
  944. debug_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  945. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  946. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  947. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  948. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  949. window_menu = memnew( MenuButton );
  950. menu_hb->add_child(window_menu);
  951. window_menu->set_text("Window");
  952. window_menu->get_popup()->add_item("Close",WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
  953. window_menu->get_popup()->add_separator();
  954. window_menu->get_popup()->add_item("Move Left",WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
  955. window_menu->get_popup()->add_item("Move Right",WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
  956. window_menu->get_popup()->add_separator();
  957. window_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  958. help_menu = memnew( MenuButton );
  959. menu_hb->add_child(help_menu);
  960. help_menu->set_text("Help");
  961. help_menu->get_popup()->add_item("Contextual", HELP_CONTEXTUAL, KEY_MASK_SHIFT|KEY_F1);
  962. help_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  963. tab_container->connect("tab_changed", this,"_tab_changed");
  964. find_replace_dialog = memnew(FindReplaceDialog);
  965. add_child(find_replace_dialog);
  966. erase_tab_confirm = memnew( ConfirmationDialog );
  967. add_child(erase_tab_confirm);
  968. erase_tab_confirm->connect("confirmed", this,"_close_current_tab");
  969. goto_line_dialog = memnew(GotoLineDialog);
  970. add_child(goto_line_dialog);
  971. debugger = memnew( ScriptEditorDebugger(editor) );
  972. debugger->connect("goto_script_line",this,"_goto_script_line");
  973. debugger->connect("show_debugger",this,"_show_debugger");
  974. disk_changed = memnew( ConfirmationDialog );
  975. {
  976. VBoxContainer *vbc = memnew( VBoxContainer );
  977. disk_changed->add_child(vbc);
  978. disk_changed->set_child_rect(vbc);
  979. Label *dl = memnew( Label );
  980. dl->set_text("The following files are newer on disk.\nWhat action should be taken?:");
  981. vbc->add_child(dl);
  982. disk_changed_list = memnew( Tree );
  983. vbc->add_child(disk_changed_list);
  984. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  985. disk_changed->connect("confirmed",this,"_reload_scripts");
  986. disk_changed->get_ok()->set_text("Reload");
  987. disk_changed->add_button("Resave",!OS::get_singleton()->get_swap_ok_cancel(),"resave");
  988. disk_changed->connect("custom_action",this,"_resave_scripts");
  989. }
  990. add_child(disk_changed);
  991. script_editor=this;
  992. quick_open = memnew( ScriptEditorQuickOpen );
  993. add_child(quick_open);
  994. quick_open->connect("goto_line",this,"_goto_script_line2");
  995. v_split->add_child(debugger);
  996. debugger->connect("breaked",this,"_breaked");
  997. // debugger_gui->hide();
  998. }
  999. void ScriptEditorPlugin::edit(Object *p_object) {
  1000. if (!p_object->cast_to<Script>())
  1001. return;
  1002. script_editor->edit(p_object->cast_to<Script>());
  1003. }
  1004. bool ScriptEditorPlugin::handles(Object *p_object) const {
  1005. return p_object->is_type("Script");
  1006. }
  1007. void ScriptEditorPlugin::make_visible(bool p_visible) {
  1008. if (p_visible) {
  1009. script_editor->show();
  1010. script_editor->set_process(true);
  1011. script_editor->ensure_select_current();
  1012. } else {
  1013. script_editor->hide();
  1014. script_editor->set_process(false);
  1015. }
  1016. }
  1017. void ScriptEditorPlugin::selected_notify() {
  1018. script_editor->ensure_select_current();
  1019. }
  1020. Dictionary ScriptEditorPlugin::get_state() const {
  1021. return script_editor->get_state();
  1022. }
  1023. void ScriptEditorPlugin::set_state(const Dictionary& p_state) {
  1024. script_editor->set_state(p_state);
  1025. }
  1026. void ScriptEditorPlugin::clear() {
  1027. script_editor->clear();
  1028. }
  1029. void ScriptEditorPlugin::save_external_data() {
  1030. script_editor->save_external_data();
  1031. }
  1032. void ScriptEditorPlugin::apply_changes() {
  1033. script_editor->apply_scripts();
  1034. }
  1035. void ScriptEditorPlugin::restore_global_state() {
  1036. if (bool(EDITOR_DEF("text_editor/restore_scripts_on_load",true))) {
  1037. script_editor->_load_files_state();
  1038. }
  1039. }
  1040. void ScriptEditorPlugin::save_global_state() {
  1041. if (bool(EDITOR_DEF("text_editor/restore_scripts_on_load",true))) {
  1042. script_editor->_save_files_state();
  1043. }
  1044. }
  1045. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  1046. return script_editor->get_breakpoints(p_breakpoints);
  1047. }
  1048. ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
  1049. editor=p_node;
  1050. script_editor = memnew( ScriptEditor(p_node) );
  1051. editor->get_viewport()->add_child(script_editor);
  1052. script_editor->set_area_as_parent_rect();
  1053. script_editor->hide();
  1054. EDITOR_DEF("external_editor/use_external_editor",false);
  1055. EDITOR_DEF("external_editor/exec_path","");
  1056. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"external_editor/exec_path",PROPERTY_HINT_GLOBAL_FILE));
  1057. EDITOR_DEF("external_editor/exec_flags","");
  1058. }
  1059. ScriptEditorPlugin::~ScriptEditorPlugin()
  1060. {
  1061. }