script_editor_plugin.cpp 68 KB

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