script_editor_plugin.cpp 76 KB

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