script_editor_plugin.cpp 52 KB

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