script_editor_plugin.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  1. /*************************************************************************/
  2. /* script_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "script_editor_plugin.h"
  30. #include "tools/editor/editor_settings.h"
  31. #include "io/resource_loader.h"
  32. #include "io/resource_saver.h"
  33. #include "os/keyboard.h"
  34. #include "os/os.h"
  35. #include "tools/editor/editor_node.h"
  36. #include "tools/editor/script_editor_debugger.h"
  37. #include "globals.h"
  38. #include "os/file_access.h"
  39. #include "scene/main/viewport.h"
  40. #include "os/keyboard.h"
  41. #include "os/input.h"
  42. /*** SCRIPT EDITOR ****/
  43. class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache {
  44. struct Cache {
  45. uint64_t time_loaded;
  46. RES cache;
  47. };
  48. Map<String,Cache> cached;
  49. public:
  50. uint64_t max_time_cache;
  51. int max_cache_size;
  52. void cleanup() {
  53. List< Map<String,Cache>::Element * > to_clean;
  54. Map<String,Cache>::Element *I=cached.front();
  55. while(I) {
  56. if ((OS::get_singleton()->get_ticks_msec()-I->get().time_loaded)>max_time_cache) {
  57. to_clean.push_back(I);
  58. }
  59. I=I->next();
  60. }
  61. while(to_clean.front()) {
  62. cached.erase(to_clean.front()->get());
  63. to_clean.pop_front();
  64. }
  65. }
  66. RES get_cached_resource(const String& p_path) {
  67. Map<String,Cache>::Element *E=cached.find(p_path);
  68. if (!E) {
  69. Cache c;
  70. c.cache=ResourceLoader::load(p_path);
  71. E=cached.insert(p_path,c);
  72. }
  73. E->get().time_loaded=OS::get_singleton()->get_ticks_msec();
  74. if (cached.size()>max_cache_size) {
  75. uint64_t older;
  76. Map<String,Cache>::Element *O=cached.front();
  77. older=O->get().time_loaded;
  78. Map<String,Cache>::Element *I=O;
  79. while(I) {
  80. if (I->get().time_loaded<older) {
  81. older = I->get().time_loaded;
  82. O=I;
  83. }
  84. I=I->next();
  85. }
  86. if (O!=E) {//should never heppane..
  87. cached.erase(O);
  88. }
  89. }
  90. return E->get().cache;
  91. }
  92. EditorScriptCodeCompletionCache() {
  93. max_cache_size=128;
  94. max_time_cache=5*60*1000; //minutes, five
  95. }
  96. };
  97. #define SORT_SCRIPT_LIST
  98. void ScriptEditorQuickOpen::popup(const Vector<String>& p_functions, bool p_dontclear) {
  99. popup_centered_ratio(0.6);
  100. if (p_dontclear)
  101. search_box->select_all();
  102. else
  103. search_box->clear();
  104. search_box->grab_focus();
  105. functions=p_functions;
  106. _update_search();
  107. }
  108. void ScriptEditorQuickOpen::_text_changed(const String& p_newtext) {
  109. _update_search();
  110. }
  111. void ScriptEditorQuickOpen::_sbox_input(const InputEvent& p_ie) {
  112. if (p_ie.type==InputEvent::KEY && (
  113. p_ie.key.scancode == KEY_UP ||
  114. p_ie.key.scancode == KEY_DOWN ||
  115. p_ie.key.scancode == KEY_PAGEUP ||
  116. p_ie.key.scancode == KEY_PAGEDOWN ) ) {
  117. search_options->call("_input_event",p_ie);
  118. search_box->accept_event();
  119. }
  120. }
  121. void ScriptEditorQuickOpen::_update_search() {
  122. search_options->clear();
  123. TreeItem *root = search_options->create_item();
  124. for(int i=0;i<functions.size();i++) {
  125. String file = functions[i];
  126. if ((search_box->get_text()=="" || file.findn(search_box->get_text())!=-1)) {
  127. TreeItem *ti = search_options->create_item(root);
  128. ti->set_text(0,file);
  129. if (root->get_children()==ti)
  130. ti->select(0);
  131. }
  132. }
  133. get_ok()->set_disabled(root->get_children()==NULL);
  134. }
  135. void ScriptEditorQuickOpen::_confirmed() {
  136. TreeItem *ti = search_options->get_selected();
  137. if (!ti)
  138. return;
  139. int line = ti->get_text(0).get_slice(":",1).to_int();
  140. emit_signal("goto_line",line-1);
  141. hide();
  142. }
  143. void ScriptEditorQuickOpen::_notification(int p_what) {
  144. if (p_what==NOTIFICATION_ENTER_TREE) {
  145. connect("confirmed",this,"_confirmed");
  146. }
  147. }
  148. void ScriptEditorQuickOpen::_bind_methods() {
  149. ObjectTypeDB::bind_method(_MD("_text_changed"),&ScriptEditorQuickOpen::_text_changed);
  150. ObjectTypeDB::bind_method(_MD("_confirmed"),&ScriptEditorQuickOpen::_confirmed);
  151. ObjectTypeDB::bind_method(_MD("_sbox_input"),&ScriptEditorQuickOpen::_sbox_input);
  152. ADD_SIGNAL(MethodInfo("goto_line",PropertyInfo(Variant::INT,"line")));
  153. }
  154. ScriptEditorQuickOpen::ScriptEditorQuickOpen() {
  155. VBoxContainer *vbc = memnew( VBoxContainer );
  156. add_child(vbc);
  157. set_child_rect(vbc);
  158. search_box = memnew( LineEdit );
  159. vbc->add_margin_child("Search:",search_box);
  160. search_box->connect("text_changed",this,"_text_changed");
  161. search_box->connect("input_event",this,"_sbox_input");
  162. search_options = memnew( Tree );
  163. vbc->add_margin_child("Matches:",search_options,true);
  164. get_ok()->set_text("Open");
  165. get_ok()->set_disabled(true);
  166. register_text_enter(search_box);
  167. set_hide_on_ok(false);
  168. search_options->connect("item_activated",this,"_confirmed");
  169. search_options->set_hide_root(true);
  170. }
  171. /////////////////////////////////
  172. ScriptEditor *ScriptEditor::script_editor=NULL;
  173. Vector<String> ScriptTextEditor::get_functions() {
  174. String errortxt;
  175. int line=-1,col;
  176. TextEdit *te=get_text_edit();
  177. String text = te->get_text();
  178. List<String> fnc;
  179. if (script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  180. //if valid rewrite functions to latest
  181. functions.clear();
  182. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  183. functions.push_back(E->get());
  184. }
  185. }
  186. return functions;
  187. }
  188. void ScriptTextEditor::apply_code() {
  189. if (script.is_null())
  190. return;
  191. // print_line("applying code");
  192. script->set_source_code(get_text_edit()->get_text());
  193. script->update_exports();
  194. }
  195. Ref<Script> ScriptTextEditor::get_edited_script() const {
  196. return script;
  197. }
  198. void ScriptTextEditor::_load_theme_settings() {
  199. get_text_edit()->clear_colors();
  200. /* keyword color */
  201. get_text_edit()->set_custom_bg_color(EDITOR_DEF("text_editor/background_color",Color(0,0,0,0)));
  202. get_text_edit()->add_color_override("font_color",EDITOR_DEF("text_editor/text_color",Color(0,0,0)));
  203. get_text_edit()->add_color_override("font_selected_color",EDITOR_DEF("text_editor/text_selected_color",Color(1,1,1)));
  204. get_text_edit()->add_color_override("selection_color",EDITOR_DEF("text_editor/selection_color",Color(0.2,0.2,1)));
  205. get_text_edit()->add_color_override("brace_mismatch_color",EDITOR_DEF("text_editor/brace_mismatch_color",Color(1,0.2,0.2)));
  206. get_text_edit()->add_color_override("current_line_color",EDITOR_DEF("text_editor/current_line_color",Color(0.3,0.5,0.8,0.15)));
  207. Color keyword_color= EDITOR_DEF("text_editor/keyword_color",Color(0.5,0.0,0.2));
  208. get_text_edit()->set_syntax_coloring(true);
  209. List<String> keywords;
  210. script->get_language()->get_reserved_words(&keywords);
  211. for(List<String>::Element *E=keywords.front();E;E=E->next()) {
  212. get_text_edit()->add_keyword_color(E->get(),keyword_color);
  213. }
  214. //colorize core types
  215. Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0));
  216. get_text_edit()->add_keyword_color("Vector2",basetype_color);
  217. get_text_edit()->add_keyword_color("Vector3",basetype_color);
  218. get_text_edit()->add_keyword_color("Plane",basetype_color);
  219. get_text_edit()->add_keyword_color("Quat",basetype_color);
  220. get_text_edit()->add_keyword_color("AABB",basetype_color);
  221. get_text_edit()->add_keyword_color("Matrix3",basetype_color);
  222. get_text_edit()->add_keyword_color("Transform",basetype_color);
  223. get_text_edit()->add_keyword_color("Color",basetype_color);
  224. get_text_edit()->add_keyword_color("Image",basetype_color);
  225. get_text_edit()->add_keyword_color("InputEvent",basetype_color);
  226. //colorize engine types
  227. Color type_color= EDITOR_DEF("text_editor/engine_type_color",Color(0.0,0.2,0.4));
  228. List<StringName> types;
  229. ObjectTypeDB::get_type_list(&types);
  230. for(List<StringName>::Element *E=types.front();E;E=E->next()) {
  231. get_text_edit()->add_keyword_color(E->get(),type_color);
  232. }
  233. //colorize comments
  234. Color comment_color = EDITOR_DEF("text_editor/comment_color",Color::hex(0x797e7eff));
  235. List<String> comments;
  236. script->get_language()->get_comment_delimiters(&comments);
  237. for(List<String>::Element *E=comments.front();E;E=E->next()) {
  238. String comment = E->get();
  239. String beg = comment.get_slice(" ",0);
  240. String end = comment.get_slice_count(" ")>1?comment.get_slice(" ",1):String();
  241. get_text_edit()->add_color_region(beg,end,comment_color,end=="");
  242. }
  243. //colorize strings
  244. Color string_color = EDITOR_DEF("text_editor/string_color",Color::hex(0x6b6f00ff));
  245. List<String> strings;
  246. script->get_language()->get_string_delimiters(&strings);
  247. for (List<String>::Element *E=strings.front();E;E=E->next()) {
  248. String string = E->get();
  249. String beg = string.get_slice(" ",0);
  250. String end = string.get_slice_count(" ")>1?string.get_slice(" ",1):String();
  251. get_text_edit()->add_color_region(beg,end,string_color,end=="");
  252. }
  253. //colorize symbols
  254. Color symbol_color= EDITOR_DEF("text_editor/symbol_color",Color::hex(0x005291ff));
  255. get_text_edit()->set_symbol_color(symbol_color);
  256. }
  257. void ScriptTextEditor::reload_text() {
  258. ERR_FAIL_COND(script.is_null()) ;
  259. TextEdit *te = get_text_edit();
  260. int column = te->cursor_get_column();
  261. int row = te->cursor_get_line();
  262. int h = te->get_h_scroll();
  263. int v = te->get_v_scroll();
  264. te->set_text(script->get_source_code());
  265. te->clear_undo_history();
  266. te->cursor_set_line(row);
  267. te->cursor_set_column(column);
  268. te->set_h_scroll(h);
  269. te->set_v_scroll(v);
  270. _line_col_changed();
  271. }
  272. void ScriptTextEditor::_notification(int p_what) {
  273. if (p_what==NOTIFICATION_READY) {
  274. //emit_signal("name_changed");
  275. }
  276. }
  277. String ScriptTextEditor::get_name() {
  278. String name;
  279. if (script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  280. name=script->get_path().get_file();
  281. if (get_text_edit()->get_version()!=get_text_edit()->get_saved_version()) {
  282. name+="(*)";
  283. }
  284. } else if (script->get_name()!="")
  285. name=script->get_name();
  286. else
  287. name=script->get_type()+"("+itos(script->get_instance_ID())+")";
  288. return name;
  289. }
  290. Ref<Texture> ScriptTextEditor::get_icon() {
  291. if (get_parent_control() && get_parent_control()->has_icon(script->get_type(),"EditorIcons")) {
  292. return get_parent_control()->get_icon(script->get_type(),"EditorIcons");
  293. }
  294. return Ref<Texture>();
  295. }
  296. void ScriptTextEditor::set_edited_script(const Ref<Script>& p_script) {
  297. ERR_FAIL_COND(!script.is_null());
  298. script=p_script;
  299. _load_theme_settings();
  300. get_text_edit()->set_text(script->get_source_code());
  301. get_text_edit()->clear_undo_history();
  302. get_text_edit()->tag_saved_version();
  303. emit_signal("name_changed");
  304. _line_col_changed();
  305. }
  306. void ScriptTextEditor::_validate_script() {
  307. String errortxt;
  308. int line=-1,col;
  309. TextEdit *te=get_text_edit();
  310. String text = te->get_text();
  311. List<String> fnc;
  312. if (!script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  313. String error_text="error("+itos(line)+","+itos(col)+"): "+errortxt;
  314. set_error(error_text);
  315. } else {
  316. set_error("");
  317. line=-1;
  318. if (!script->is_tool()) {
  319. script->set_source_code(text);
  320. script->update_exports();
  321. //script->reload(); //will update all the variables in property editors
  322. }
  323. functions.clear();
  324. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  325. functions.push_back(E->get());
  326. }
  327. }
  328. line--;
  329. for(int i=0;i<te->get_line_count();i++) {
  330. te->set_line_as_marked(i,line==i);
  331. }
  332. emit_signal("name_changed");
  333. }
  334. static Node* _find_node_for_script(Node* p_base, Node*p_current, const Ref<Script>& p_script) {
  335. if (p_current->get_owner()!=p_base && p_base!=p_current)
  336. return NULL;
  337. Ref<Script> c = p_current->get_script();
  338. if (c==p_script)
  339. return p_current;
  340. for(int i=0;i<p_current->get_child_count();i++) {
  341. Node *found = _find_node_for_script(p_base,p_current->get_child(i),p_script);
  342. if (found)
  343. return found;
  344. }
  345. return NULL;
  346. }
  347. void ScriptTextEditor::_code_complete_script(const String& p_code, List<String>* r_options) {
  348. Node *base = get_tree()->get_edited_scene_root();
  349. if (base) {
  350. base = _find_node_for_script(base,base,script);
  351. }
  352. String hint;
  353. Error err = script->get_language()->complete_code(p_code,script->get_path().get_base_dir(),base,r_options,hint);
  354. if (hint!="") {
  355. get_text_edit()->set_code_hint(hint);
  356. print_line("hint: "+hint.replace(String::chr(0xFFFF),"|"));
  357. }
  358. }
  359. void ScriptTextEditor::_bind_methods() {
  360. ADD_SIGNAL(MethodInfo("name_changed"));
  361. }
  362. ScriptTextEditor::ScriptTextEditor() {
  363. get_text_edit()->set_draw_tabs(true);
  364. }
  365. /*** SCRIPT EDITOR ******/
  366. String ScriptEditor::_get_debug_tooltip(const String&p_text,Node *_ste) {
  367. ScriptTextEditor *ste=_ste->cast_to<ScriptTextEditor>();
  368. String val = debugger->get_var_value(p_text);
  369. if (val!=String()) {
  370. return p_text+": "+val;
  371. } else {
  372. return String();
  373. }
  374. }
  375. void ScriptEditor::_breaked(bool p_breaked,bool p_can_debug) {
  376. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug));
  377. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug) );
  378. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked );
  379. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked );
  380. }
  381. void ScriptEditor::_show_debugger(bool p_show) {
  382. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), p_show);
  383. }
  384. void ScriptEditor::_goto_script_line2(int p_line) {
  385. int selected = tab_container->get_current_tab();
  386. if (selected<0 || selected>=tab_container->get_child_count())
  387. return;
  388. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  389. if (!current)
  390. return;
  391. current->get_text_edit()->cursor_set_line(p_line);
  392. }
  393. void ScriptEditor::_goto_script_line(REF p_script,int p_line) {
  394. editor->push_item(p_script.ptr());
  395. _goto_script_line2(p_line);
  396. }
  397. void ScriptEditor::_close_current_tab() {
  398. int selected = tab_container->get_current_tab();
  399. if (selected<0 || selected>=tab_container->get_child_count())
  400. return;
  401. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  402. if (!current)
  403. return;
  404. apply_scripts();
  405. int idx = tab_container->get_current_tab();
  406. memdelete(current);
  407. if (idx>=tab_container->get_child_count())
  408. idx=tab_container->get_child_count()-1;
  409. if (idx>=0) {
  410. tab_container->set_current_tab(idx);
  411. //script_list->select(idx);
  412. }
  413. _update_script_names();
  414. EditorNode::get_singleton()->save_layout();
  415. }
  416. void ScriptEditor::_resave_scripts(const String& p_str) {
  417. apply_scripts();
  418. for(int i=0;i<tab_container->get_child_count();i++) {
  419. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  420. if (!ste)
  421. continue;
  422. Ref<Script> script = ste->get_edited_script();
  423. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  424. continue; //internal script, who cares
  425. editor->save_resource(script);
  426. ste->get_text_edit()->tag_saved_version();
  427. }
  428. disk_changed->hide();
  429. }
  430. void ScriptEditor::_reload_scripts(){
  431. for(int i=0;i<tab_container->get_child_count();i++) {
  432. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  433. if (!ste) {
  434. continue;
  435. }
  436. Ref<Script> script = ste->get_edited_script();
  437. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  438. continue; //internal script, who cares
  439. }
  440. Ref<Script> rel_script = ResourceLoader::load(script->get_path(),script->get_type(),true);
  441. ERR_CONTINUE(!rel_script.is_valid());
  442. script->set_source_code( rel_script->get_source_code() );
  443. script->set_last_modified_time( rel_script->get_last_modified_time() );
  444. script->reload();
  445. ste->reload_text();
  446. }
  447. disk_changed->hide();
  448. }
  449. void ScriptEditor::_res_saved_callback(const Ref<Resource>& p_res) {
  450. for(int i=0;i<tab_container->get_child_count();i++) {
  451. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  452. if (!ste) {
  453. continue;
  454. }
  455. Ref<Script> script = ste->get_edited_script();
  456. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  457. continue; //internal script, who cares
  458. }
  459. if (script==p_res) {
  460. ste->get_text_edit()->tag_saved_version();
  461. }
  462. }
  463. _update_script_names();
  464. }
  465. bool ScriptEditor::_test_script_times_on_disk() {
  466. disk_changed_list->clear();
  467. TreeItem *r = disk_changed_list->create_item();
  468. disk_changed_list->set_hide_root(true);
  469. bool all_ok=true;
  470. for(int i=0;i<tab_container->get_child_count();i++) {
  471. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  472. if (!ste)
  473. continue;
  474. Ref<Script> script = ste->get_edited_script();
  475. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  476. continue; //internal script, who cares
  477. uint64_t last_date = script->get_last_modified_time();
  478. uint64_t date = FileAccess::get_modified_time(script->get_path());
  479. //printf("last date: %lli vs date: %lli\n",last_date,date);
  480. if (last_date!=date) {
  481. TreeItem *ti = disk_changed_list->create_item(r);
  482. ti->set_text(0,script->get_path().get_file());
  483. all_ok=false;
  484. //r->set_metadata(0,);
  485. }
  486. }
  487. if (!all_ok) {
  488. if (bool(EDITOR_DEF("text_editor/auto_reload_changed_scripts",false))) {
  489. script_editor->_reload_scripts();
  490. } else {
  491. disk_changed->call_deferred("popup_centered_ratio",0.5);
  492. }
  493. }
  494. return all_ok;
  495. }
  496. void ScriptEditor::swap_lines(TextEdit *tx, int line1, int line2)
  497. {
  498. String tmp = tx->get_line(line1);
  499. String tmp2 = tx->get_line(line2);
  500. tx->set_line(line2, tmp);
  501. tx->set_line(line1, tmp2);
  502. tx->cursor_set_line(line2);
  503. }
  504. void ScriptEditor::_menu_option(int p_option) {
  505. if (p_option==FILE_OPEN) {
  506. editor->open_resource("Script");
  507. return;
  508. }
  509. int selected = tab_container->get_current_tab();
  510. if (selected<0 || selected>=tab_container->get_child_count())
  511. return;
  512. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  513. if (!current)
  514. return;
  515. switch(p_option) {
  516. case FILE_SAVE: {
  517. if (!_test_script_times_on_disk())
  518. return;
  519. editor->save_resource( current->get_edited_script() );
  520. } break;
  521. case FILE_SAVE_AS: {
  522. editor->save_resource_as( current->get_edited_script() );
  523. } break;
  524. case FILE_SAVE_ALL: {
  525. if (!_test_script_times_on_disk())
  526. return;
  527. for(int i=0;i<tab_container->get_child_count();i++) {
  528. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  529. if (!ste)
  530. continue;
  531. Ref<Script> script = ste->get_edited_script();
  532. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  533. continue; //internal script, who cares
  534. editor->save_resource( script );
  535. }
  536. } break;
  537. case EDIT_UNDO: {
  538. current->get_text_edit()->undo();
  539. current->get_text_edit()->call_deferred("grab_focus");
  540. } break;
  541. case EDIT_REDO: {
  542. current->get_text_edit()->redo();
  543. current->get_text_edit()->call_deferred("grab_focus");
  544. } break;
  545. case EDIT_CUT: {
  546. current->get_text_edit()->cut();
  547. current->get_text_edit()->call_deferred("grab_focus");
  548. } break;
  549. case EDIT_COPY: {
  550. current->get_text_edit()->copy();
  551. current->get_text_edit()->call_deferred("grab_focus");
  552. } break;
  553. case EDIT_PASTE: {
  554. current->get_text_edit()->paste();
  555. current->get_text_edit()->call_deferred("grab_focus");
  556. } break;
  557. case EDIT_SELECT_ALL: {
  558. current->get_text_edit()->select_all();
  559. current->get_text_edit()->call_deferred("grab_focus");
  560. } break;
  561. case EDIT_MOVE_LINE_UP: {
  562. TextEdit *tx = current->get_text_edit();
  563. Ref<Script> scr = current->get_edited_script();
  564. if (scr.is_null())
  565. return;
  566. if (tx->is_selection_active())
  567. {
  568. int from_line = tx->get_selection_from_line();
  569. int from_col = tx->get_selection_from_column();
  570. int to_line = tx->get_selection_to_line();
  571. int to_column = tx->get_selection_to_column();
  572. for (int i = from_line; i <= to_line; i++)
  573. {
  574. int line_id = i;
  575. int next_id = i - 1;
  576. if (line_id == 0 || next_id < 0)
  577. return;
  578. swap_lines(tx, line_id, next_id);
  579. }
  580. int from_line_up = from_line > 0 ? from_line-1 : from_line;
  581. int to_line_up = to_line > 0 ? to_line-1 : to_line;
  582. tx->select(from_line_up, from_col, to_line_up, to_column);
  583. }
  584. else
  585. {
  586. int line_id = tx->cursor_get_line();
  587. int next_id = line_id - 1;
  588. if (line_id == 0 || next_id < 0)
  589. return;
  590. swap_lines(tx, line_id, next_id);
  591. }
  592. tx->update();
  593. } break;
  594. case EDIT_MOVE_LINE_DOWN: {
  595. TextEdit *tx = current->get_text_edit();
  596. Ref<Script> scr = current->get_edited_script();
  597. if (scr.is_null())
  598. return;
  599. if (tx->is_selection_active())
  600. {
  601. int from_line = tx->get_selection_from_line();
  602. int from_col = tx->get_selection_from_column();
  603. int to_line = tx->get_selection_to_line();
  604. int to_column = tx->get_selection_to_column();
  605. for (int i = to_line; i >= from_line; i--)
  606. {
  607. int line_id = i;
  608. int next_id = i + 1;
  609. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  610. return;
  611. swap_lines(tx, line_id, next_id);
  612. }
  613. int from_line_down = from_line < tx->get_line_count() ? from_line+1 : from_line;
  614. int to_line_down = to_line < tx->get_line_count() ? to_line+1 : to_line;
  615. tx->select(from_line_down, from_col, to_line_down, to_column);
  616. }
  617. else
  618. {
  619. int line_id = tx->cursor_get_line();
  620. int next_id = line_id + 1;
  621. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  622. return;
  623. swap_lines(tx, line_id, next_id);
  624. }
  625. tx->update();
  626. } break;
  627. case EDIT_INDENT_LEFT: {
  628. TextEdit *tx = current->get_text_edit();
  629. Ref<Script> scr = current->get_edited_script();
  630. if (scr.is_null())
  631. return;
  632. if (tx->is_selection_active())
  633. {
  634. int begin = tx->get_selection_from_line();
  635. int end = tx->get_selection_to_line();
  636. for (int i = begin; i <= end; i++)
  637. {
  638. String line_text = tx->get_line(i);
  639. // begins with tab
  640. if (line_text.begins_with("\t"))
  641. {
  642. line_text = line_text.substr(1, line_text.length());
  643. tx->set_line(i, line_text);
  644. }
  645. // begins with 4 spaces
  646. else if (line_text.begins_with(" "))
  647. {
  648. line_text = line_text.substr(4, line_text.length());
  649. tx->set_line(i, line_text);
  650. }
  651. }
  652. }
  653. else
  654. {
  655. int begin = tx->cursor_get_line();
  656. String line_text = tx->get_line(begin);
  657. // begins with tab
  658. if (line_text.begins_with("\t"))
  659. {
  660. line_text = line_text.substr(1, line_text.length());
  661. tx->set_line(begin, line_text);
  662. }
  663. // begins with 4 spaces
  664. else if (line_text.begins_with(" "))
  665. {
  666. line_text = line_text.substr(4, line_text.length());
  667. tx->set_line(begin, line_text);
  668. }
  669. }
  670. tx->update();
  671. //tx->deselect();
  672. } break;
  673. case EDIT_INDENT_RIGHT: {
  674. TextEdit *tx = current->get_text_edit();
  675. Ref<Script> scr = current->get_edited_script();
  676. if (scr.is_null())
  677. return;
  678. if (tx->is_selection_active())
  679. {
  680. int begin = tx->get_selection_from_line();
  681. int end = tx->get_selection_to_line();
  682. for (int i = begin; i <= end; i++)
  683. {
  684. String line_text = tx->get_line(i);
  685. line_text = '\t' + line_text;
  686. tx->set_line(i, line_text);
  687. }
  688. }
  689. else
  690. {
  691. int begin = tx->cursor_get_line();
  692. String line_text = tx->get_line(begin);
  693. line_text = '\t' + line_text;
  694. tx->set_line(begin, line_text);
  695. }
  696. tx->update();
  697. //tx->deselect();
  698. } break;
  699. case EDIT_CLONE_DOWN: {
  700. TextEdit *tx = current->get_text_edit();
  701. Ref<Script> scr = current->get_edited_script();
  702. if (scr.is_null())
  703. return;
  704. int line = tx->cursor_get_line();
  705. int next_line = line + 1;
  706. if (line == tx->get_line_count() || next_line > tx->get_line_count())
  707. return;
  708. String line_clone = tx->get_line(line);
  709. tx->insert_at(line_clone, next_line);
  710. tx->update();
  711. } break;
  712. case EDIT_TOGGLE_COMMENT: {
  713. TextEdit *tx = current->get_text_edit();
  714. Ref<Script> scr = current->get_edited_script();
  715. if (scr.is_null())
  716. return;
  717. if (tx->is_selection_active())
  718. {
  719. int begin = tx->get_selection_from_line();
  720. int end = tx->get_selection_to_line();
  721. for (int i = begin; i <= end; i++)
  722. {
  723. String line_text = tx->get_line(i);
  724. if (line_text.begins_with("#"))
  725. line_text = line_text.substr(1, line_text.length());
  726. else
  727. line_text = "#" + line_text;
  728. tx->set_line(i, line_text);
  729. }
  730. }
  731. else
  732. {
  733. int begin = tx->cursor_get_line();
  734. String line_text = tx->get_line(begin);
  735. if (line_text.begins_with("#"))
  736. line_text = line_text.substr(1, line_text.length());
  737. else
  738. line_text = "#" + line_text;
  739. tx->set_line(begin, line_text);
  740. }
  741. tx->update();
  742. //tx->deselect();
  743. } break;
  744. case EDIT_COMPLETE: {
  745. current->get_text_edit()->query_code_comple();
  746. } break;
  747. case EDIT_AUTO_INDENT: {
  748. TextEdit *te = current->get_text_edit();
  749. String text = te->get_text();
  750. Ref<Script> scr = current->get_edited_script();
  751. if (scr.is_null())
  752. return;
  753. int begin,end;
  754. if (te->is_selection_active()) {
  755. begin=te->get_selection_from_line();
  756. end=te->get_selection_to_line();
  757. } else {
  758. begin=0;
  759. end=te->get_line_count()-1;
  760. }
  761. scr->get_language()->auto_indent_code(text,begin,end);
  762. te->set_text(text);
  763. } break;
  764. case SEARCH_FIND: {
  765. find_replace_dialog->set_text_edit(current->get_text_edit());
  766. find_replace_dialog->popup_search();
  767. } break;
  768. case SEARCH_FIND_NEXT: {
  769. find_replace_dialog->set_text_edit(current->get_text_edit());
  770. find_replace_dialog->search_next();
  771. } break;
  772. case SEARCH_REPLACE: {
  773. find_replace_dialog->set_text_edit(current->get_text_edit());
  774. find_replace_dialog->popup_replace();
  775. } break;
  776. case SEARCH_LOCATE_FUNCTION: {
  777. if (!current)
  778. return;
  779. quick_open->popup(current->get_functions());
  780. } break;
  781. case SEARCH_GOTO_LINE: {
  782. goto_line_dialog->popup_find_line(current->get_text_edit());
  783. } break;
  784. case DEBUG_TOGGLE_BREAKPOINT: {
  785. int line=current->get_text_edit()->cursor_get_line();
  786. bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
  787. current->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  788. get_debugger()->set_breakpoint(current->get_edited_script()->get_path(),line+1,dobreak);
  789. } break;
  790. case DEBUG_NEXT: {
  791. if (debugger)
  792. debugger->debug_next();
  793. } break;
  794. case DEBUG_STEP: {
  795. if (debugger)
  796. debugger->debug_step();
  797. } break;
  798. case DEBUG_BREAK: {
  799. if (debugger)
  800. debugger->debug_break();
  801. } break;
  802. case DEBUG_CONTINUE: {
  803. if (debugger)
  804. debugger->debug_continue();
  805. } break;
  806. case DEBUG_SHOW: {
  807. if (debugger) {
  808. bool visible = debug_menu->get_popup()->is_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW) );
  809. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), !visible);
  810. if (visible)
  811. debugger->hide();
  812. else
  813. debugger->show();
  814. }
  815. } break;
  816. case HELP_CONTEXTUAL: {
  817. String text = current->get_text_edit()->get_selection_text();
  818. if (text == "")
  819. text = current->get_text_edit()->get_word_under_cursor();
  820. if (text != "")
  821. editor->emit_signal("request_help", text);
  822. } break;
  823. case FILE_CLOSE: {
  824. if (current->get_text_edit()->get_version()!=current->get_text_edit()->get_saved_version()) {
  825. erase_tab_confirm->set_text("Close and save changes?\n\""+current->get_name()+"\"");
  826. erase_tab_confirm->popup_centered_minsize();
  827. } else {
  828. _close_current_tab();
  829. }
  830. } break;
  831. case WINDOW_MOVE_LEFT: {
  832. if (tab_container->get_current_tab()>0) {
  833. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()-1);
  834. script_list->call_deferred("select",tab_container->get_current_tab()-1);
  835. tab_container->move_child(current,tab_container->get_current_tab()-1);
  836. _update_script_names();
  837. }
  838. } break;
  839. case WINDOW_MOVE_RIGHT: {
  840. if (tab_container->get_current_tab()<tab_container->get_child_count()-1) {
  841. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()+1);
  842. script_list->call_deferred("select",tab_container->get_current_tab()+1);
  843. tab_container->move_child(current,tab_container->get_current_tab()+1);
  844. _update_script_names();
  845. }
  846. } break;
  847. default: {
  848. if (p_option>=WINDOW_SELECT_BASE) {
  849. tab_container->set_current_tab(p_option-WINDOW_SELECT_BASE);
  850. script_list->select(p_option-WINDOW_SELECT_BASE);
  851. }
  852. }
  853. }
  854. }
  855. void ScriptEditor::_tab_changed(int p_which) {
  856. ensure_select_current();
  857. }
  858. void ScriptEditor::_notification(int p_what) {
  859. if (p_what==NOTIFICATION_ENTER_TREE) {
  860. editor->connect("play_pressed",this,"_editor_play");
  861. editor->connect("pause_pressed",this,"_editor_pause");
  862. editor->connect("stop_pressed",this,"_editor_stop");
  863. editor->connect("script_add_function_request",this,"_add_callback");
  864. editor->connect("resource_saved",this,"_res_saved_callback");
  865. script_list->connect("item_selected",this,"_script_selected");
  866. script_split->connect("dragged",this,"_script_split_dragged");
  867. autosave_timer->connect("timeout",this,"_autosave_scripts");
  868. {
  869. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  870. if (autosave_time>0) {
  871. autosave_timer->set_wait_time(autosave_time);
  872. autosave_timer->start();
  873. } else {
  874. autosave_timer->stop();
  875. }
  876. }
  877. EditorSettings::get_singleton()->connect("settings_changed",this,"_editor_settings_changed");
  878. }
  879. if (p_what==NOTIFICATION_READY) {
  880. get_tree()->connect("tree_changed",this,"_tree_changed");
  881. }
  882. if (p_what==NOTIFICATION_EXIT_TREE) {
  883. editor->disconnect("play_pressed",this,"_editor_play");
  884. editor->disconnect("pause_pressed",this,"_editor_pause");
  885. editor->disconnect("stop_pressed",this,"_editor_stop");
  886. }
  887. if (p_what==MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  888. _test_script_times_on_disk();
  889. }
  890. if (p_what==NOTIFICATION_PROCESS) {
  891. }
  892. }
  893. static const Node * _find_node_with_script(const Node* p_node, const RefPtr & p_script) {
  894. if (p_node->get_script()==p_script)
  895. return p_node;
  896. for(int i=0;i<p_node->get_child_count();i++) {
  897. const Node *result = _find_node_with_script(p_node->get_child(i),p_script);
  898. if (result)
  899. return result;
  900. }
  901. return NULL;
  902. }
  903. Dictionary ScriptEditor::get_state() const {
  904. // apply_scripts();
  905. Dictionary state;
  906. #if 0
  907. Array paths;
  908. int open=-1;
  909. for(int i=0;i<tab_container->get_child_count();i++) {
  910. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  911. if (!ste)
  912. continue;
  913. Ref<Script> script = ste->get_edited_script();
  914. if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  915. paths.push_back(script->get_path());
  916. } else {
  917. const Node *owner = _find_node_with_script(get_tree()->get_root(),script.get_ref_ptr());
  918. if (owner)
  919. paths.push_back(owner->get_path());
  920. }
  921. if (i==tab_container->get_current_tab())
  922. open=i;
  923. }
  924. if (paths.size())
  925. state["sources"]=paths;
  926. if (open!=-1)
  927. state["current"]=open;
  928. #endif
  929. return state;
  930. }
  931. void ScriptEditor::set_state(const Dictionary& p_state) {
  932. #if 0
  933. print_line("attempt set state: "+String(Variant(p_state)));
  934. if (!p_state.has("sources"))
  935. return; //bleh
  936. Array sources = p_state["sources"];
  937. for(int i=0;i<sources.size();i++) {
  938. Variant source=sources[i];
  939. Ref<Script> script;
  940. if (source.get_type()==Variant::NODE_PATH) {
  941. Node *owner=get_tree()->get_root()->get_node(source);
  942. if (!owner)
  943. continue;
  944. script = owner->get_script();
  945. } else if (source.get_type()==Variant::STRING) {
  946. script = ResourceLoader::load(source,"Script");
  947. }
  948. if (script.is_null()) //ah well..
  949. continue;
  950. editor->call("_resource_selected",script);
  951. }
  952. if (p_state.has("current")) {
  953. tab_container->set_current_tab(p_state["current"]);
  954. }
  955. #endif
  956. }
  957. void ScriptEditor::clear() {
  958. List<ScriptTextEditor*> stes;
  959. for(int i=0;i<tab_container->get_child_count();i++) {
  960. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  961. if (!ste)
  962. continue;
  963. stes.push_back(ste);
  964. }
  965. while(stes.size()) {
  966. memdelete(stes.front()->get());
  967. stes.pop_front();
  968. }
  969. int idx = tab_container->get_current_tab();
  970. if (idx>=tab_container->get_child_count())
  971. idx=tab_container->get_child_count()-1;
  972. if (idx>=0) {
  973. tab_container->set_current_tab(idx);
  974. script_list->select( script_list->find_metadata(idx) );
  975. }
  976. }
  977. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  978. for(int i=0;i<tab_container->get_child_count();i++) {
  979. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  980. if (!ste)
  981. continue;
  982. List<int> bpoints;
  983. ste->get_text_edit()->get_breakpoints(&bpoints);
  984. Ref<Script> script = ste->get_edited_script();
  985. String base = script->get_path();
  986. ERR_CONTINUE( base.begins_with("local://") || base=="" );
  987. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  988. p_breakpoints->push_back(base+":"+itos(E->get()+1));
  989. }
  990. }
  991. }
  992. void ScriptEditor::_bind_methods() {
  993. ObjectTypeDB::bind_method("_tab_changed",&ScriptEditor::_tab_changed);
  994. ObjectTypeDB::bind_method("_menu_option",&ScriptEditor::_menu_option);
  995. ObjectTypeDB::bind_method("_close_current_tab",&ScriptEditor::_close_current_tab);
  996. ObjectTypeDB::bind_method("_editor_play",&ScriptEditor::_editor_play);
  997. ObjectTypeDB::bind_method("_editor_pause",&ScriptEditor::_editor_pause);
  998. ObjectTypeDB::bind_method("_editor_stop",&ScriptEditor::_editor_stop);
  999. ObjectTypeDB::bind_method("_add_callback",&ScriptEditor::_add_callback);
  1000. ObjectTypeDB::bind_method("_reload_scripts",&ScriptEditor::_reload_scripts);
  1001. ObjectTypeDB::bind_method("_resave_scripts",&ScriptEditor::_resave_scripts);
  1002. ObjectTypeDB::bind_method("_res_saved_callback",&ScriptEditor::_res_saved_callback);
  1003. ObjectTypeDB::bind_method("_goto_script_line",&ScriptEditor::_goto_script_line);
  1004. ObjectTypeDB::bind_method("_goto_script_line2",&ScriptEditor::_goto_script_line2);
  1005. ObjectTypeDB::bind_method("_breaked",&ScriptEditor::_breaked);
  1006. ObjectTypeDB::bind_method("_show_debugger",&ScriptEditor::_show_debugger);
  1007. ObjectTypeDB::bind_method("_get_debug_tooltip",&ScriptEditor::_get_debug_tooltip);
  1008. ObjectTypeDB::bind_method("_autosave_scripts",&ScriptEditor::_autosave_scripts);
  1009. ObjectTypeDB::bind_method("_editor_settings_changed",&ScriptEditor::_editor_settings_changed);
  1010. ObjectTypeDB::bind_method("_update_script_names",&ScriptEditor::_update_script_names);
  1011. ObjectTypeDB::bind_method("_tree_changed",&ScriptEditor::_tree_changed);
  1012. ObjectTypeDB::bind_method("_script_selected",&ScriptEditor::_script_selected);
  1013. ObjectTypeDB::bind_method("_script_split_dragged",&ScriptEditor::_script_split_dragged);
  1014. }
  1015. void ScriptEditor::ensure_focus_current() {
  1016. if (!is_inside_tree())
  1017. return;
  1018. int cidx = tab_container->get_current_tab();
  1019. if (cidx<0 || cidx>=tab_container->get_tab_count());
  1020. Control *c = tab_container->get_child(cidx)->cast_to<Control>();
  1021. if (!c)
  1022. return;
  1023. ScriptTextEditor *ste = c->cast_to<ScriptTextEditor>();
  1024. if (!ste)
  1025. return;
  1026. ste->get_text_edit()->grab_focus();
  1027. }
  1028. void ScriptEditor::_script_selected(int p_idx) {
  1029. grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing
  1030. tab_container->set_current_tab(script_list->get_item_metadata(p_idx));
  1031. grab_focus_block=false;
  1032. }
  1033. void ScriptEditor::ensure_select_current() {
  1034. if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
  1035. ScriptTextEditor *ste = tab_container->get_child(tab_container->get_current_tab())->cast_to<ScriptTextEditor>();
  1036. if (!ste)
  1037. return;
  1038. Ref<Script> script = ste->get_edited_script();
  1039. if (!grab_focus_block && is_inside_tree())
  1040. ste->get_text_edit()->grab_focus();
  1041. }
  1042. }
  1043. void ScriptEditor::_find_scripts(Node* p_base, Node* p_current, Set<Ref<Script> > &used) {
  1044. if (p_current!=p_base && p_current->get_owner()!=p_base)
  1045. return;
  1046. if (p_current->get_script_instance()) {
  1047. Ref<Script> scr = p_current->get_script();
  1048. if (scr.is_valid())
  1049. used.insert(scr);
  1050. }
  1051. for(int i=0;i<p_current->get_child_count();i++) {
  1052. _find_scripts(p_base,p_current->get_child(i),used);
  1053. }
  1054. }
  1055. void ScriptEditor::_update_script_names() {
  1056. waiting_update_names=false;
  1057. Set<Ref<Script> > used;
  1058. Node* edited = EditorNode::get_singleton()->get_edited_scene();
  1059. if (edited) {
  1060. _find_scripts(edited,edited,used);
  1061. }
  1062. script_list->clear();
  1063. for(int i=0;i<tab_container->get_child_count();i++) {
  1064. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1065. if (!ste)
  1066. continue;
  1067. String script = ste->get_name();
  1068. Ref<Texture> icon = ste->get_icon();
  1069. String path = ste->get_edited_script()->get_path();
  1070. script_list->add_item(script,icon);
  1071. int index = script_list->get_item_count()-1;
  1072. script_list->set_item_tooltip(index,path);
  1073. script_list->set_item_metadata(index,i);
  1074. if (used.has(ste->get_edited_script())) {
  1075. script_list->set_item_custom_bg_color(index,Color(88/255.0,88/255.0,60/255.0));
  1076. }
  1077. if (tab_container->get_current_tab()==index) {
  1078. script_list->select(index);
  1079. }
  1080. }
  1081. script_list->sort_items_by_text();
  1082. }
  1083. void ScriptEditor::edit(const Ref<Script>& p_script) {
  1084. if (p_script.is_null())
  1085. return;
  1086. // see if already has it
  1087. if (p_script->get_path().is_resource_file() && bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor"))) {
  1088. String path = EditorSettings::get_singleton()->get("external_editor/exec_path");
  1089. String flags = EditorSettings::get_singleton()->get("external_editor/exec_flags");
  1090. List<String> args;
  1091. flags=flags.strip_edges();
  1092. if (flags!=String()) {
  1093. Vector<String> flagss = flags.split(" ",false);
  1094. for(int i=0;i<flagss.size();i++)
  1095. args.push_back(flagss[i]);
  1096. }
  1097. args.push_back(Globals::get_singleton()->globalize_path(p_script->get_path()));
  1098. Error err = OS::get_singleton()->execute(path,args,false);
  1099. if (err==OK)
  1100. return;
  1101. WARN_PRINT("Couldn't open external text editor, using internal");
  1102. }
  1103. for(int i=0;i<tab_container->get_child_count();i++) {
  1104. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1105. if (!ste)
  1106. continue;
  1107. if (ste->get_edited_script()==p_script) {
  1108. if (!EditorNode::get_singleton()->is_changing_scene()) {
  1109. if (tab_container->get_current_tab()!=i) {
  1110. tab_container->set_current_tab(i);
  1111. script_list->select( script_list->find_metadata(i) );
  1112. }
  1113. ste->get_text_edit()->grab_focus();
  1114. }
  1115. return;
  1116. }
  1117. }
  1118. // doesn't have it, make a new one
  1119. ScriptTextEditor *ste = memnew( ScriptTextEditor );
  1120. ste->set_edited_script(p_script);
  1121. ste->get_text_edit()->set_tooltip_request_func(this,"_get_debug_tooltip",ste);
  1122. tab_container->add_child(ste);
  1123. tab_container->set_current_tab(tab_container->get_tab_count()-1);
  1124. _update_script_names();
  1125. ste->connect("name_changed",this,"_update_script_names");
  1126. if (!restoring_layout) {
  1127. EditorNode::get_singleton()->save_layout();
  1128. }
  1129. }
  1130. void ScriptEditor::save_external_data() {
  1131. apply_scripts();
  1132. for(int i=0;i<tab_container->get_child_count();i++) {
  1133. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1134. if (!ste)
  1135. continue;
  1136. Ref<Script> script = ste->get_edited_script();
  1137. if (script->get_path()!="" && script->get_path().find("local://")==-1 &&script->get_path().find("::")==-1) {
  1138. //external script, save it
  1139. editor->save_resource(script);
  1140. //ResourceSaver::save(script->get_path(),script);
  1141. }
  1142. }
  1143. }
  1144. void ScriptEditor::apply_scripts() const {
  1145. for(int i=0;i<tab_container->get_child_count();i++) {
  1146. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1147. if (!ste)
  1148. continue;
  1149. ste->apply_code();
  1150. }
  1151. }
  1152. void ScriptEditor::_editor_play() {
  1153. debugger->start();
  1154. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1155. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1156. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false );
  1157. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1158. //debugger_gui->start_listening(Globals::get_singleton()->get("debug/debug_port"));
  1159. }
  1160. void ScriptEditor::_editor_pause() {
  1161. }
  1162. void ScriptEditor::_editor_stop() {
  1163. debugger->stop();
  1164. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1165. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1166. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  1167. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1168. }
  1169. void ScriptEditor::_add_callback(Object *p_obj, const String& p_function, const StringArray& p_args) {
  1170. print_line("add callback! hohoho");
  1171. ERR_FAIL_COND(!p_obj);
  1172. Ref<Script> script = p_obj->get_script();
  1173. ERR_FAIL_COND( !script.is_valid() );
  1174. editor->push_item(script.ptr());
  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. if (ste->get_edited_script()!=script)
  1180. continue;
  1181. String code = ste->get_text_edit()->get_text();
  1182. int pos = script->get_language()->find_function(p_function,code);
  1183. if (pos==-1) {
  1184. //does not exist
  1185. ste->get_text_edit()->deselect();
  1186. pos=ste->get_text_edit()->get_line_count()+2;
  1187. String func = script->get_language()->make_function("",p_function,p_args);
  1188. //code=code+func;
  1189. ste->get_text_edit()->cursor_set_line(pos+1);
  1190. ste->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  1191. ste->get_text_edit()->insert_text_at_cursor("\n\n"+func);
  1192. }
  1193. tab_container->set_current_tab(i);
  1194. ste->get_text_edit()->cursor_set_line(pos);
  1195. ste->get_text_edit()->cursor_set_column(1);
  1196. script_list->select( script_list->find_metadata(i) );
  1197. break;
  1198. }
  1199. }
  1200. void ScriptEditor::_editor_settings_changed() {
  1201. print_line("settings changed");
  1202. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  1203. if (autosave_time>0) {
  1204. autosave_timer->set_wait_time(autosave_time);
  1205. autosave_timer->start();
  1206. } else {
  1207. autosave_timer->stop();
  1208. }
  1209. }
  1210. void ScriptEditor::_autosave_scripts() {
  1211. print_line("autosaving");
  1212. save_external_data();
  1213. }
  1214. void ScriptEditor::_tree_changed() {
  1215. if (waiting_update_names)
  1216. return;
  1217. waiting_update_names=true;
  1218. call_deferred("_update_script_names");
  1219. }
  1220. void ScriptEditor::_script_split_dragged(float) {
  1221. EditorNode::get_singleton()->save_layout();
  1222. }
  1223. void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
  1224. if (!bool(EDITOR_DEF("text_editor/restore_scripts_on_load",true))) {
  1225. return;
  1226. }
  1227. if (!p_layout->has_section_key("ScriptEditor","open_scripts"))
  1228. return;
  1229. Array scripts = p_layout->get_value("ScriptEditor","open_scripts");
  1230. restoring_layout=true;
  1231. for(int i=0;i<scripts.size();i++) {
  1232. String path = scripts[i];
  1233. Ref<Script> scr = ResourceLoader::load(path);
  1234. if (scr.is_valid()) {
  1235. edit(scr);
  1236. }
  1237. }
  1238. if (p_layout->has_section_key("ScriptEditor","split_offset")) {
  1239. script_split->set_split_offset(p_layout->get_value("ScriptEditor","split_offset"));
  1240. }
  1241. restoring_layout=false;
  1242. }
  1243. void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
  1244. Array scripts;
  1245. for(int i=0;i<tab_container->get_child_count();i++) {
  1246. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1247. if (!ste)
  1248. continue;
  1249. String path = ste->get_edited_script()->get_path();
  1250. if (!path.is_resource_file())
  1251. continue;
  1252. scripts.push_back(path);
  1253. }
  1254. p_layout->set_value("ScriptEditor","open_scripts",scripts);
  1255. p_layout->set_value("ScriptEditor","split_offset",script_split->get_split_offset());
  1256. }
  1257. ScriptEditor::ScriptEditor(EditorNode *p_editor) {
  1258. completion_cache = memnew( EditorScriptCodeCompletionCache );
  1259. restoring_layout=false;
  1260. waiting_update_names=false;
  1261. editor=p_editor;
  1262. menu_hb = memnew( HBoxContainer );
  1263. add_child(menu_hb);
  1264. v_split = memnew( VSplitContainer );
  1265. add_child(v_split);
  1266. v_split->set_v_size_flags(SIZE_EXPAND_FILL);
  1267. script_split = memnew( HSplitContainer );
  1268. v_split->add_child(script_split);
  1269. script_split->set_v_size_flags(SIZE_EXPAND_FILL);
  1270. script_list = memnew( ItemList );
  1271. script_split->add_child(script_list);
  1272. script_list->set_custom_minimum_size(Size2(70,0));
  1273. script_split->set_split_offset(70);
  1274. tab_container = memnew( TabContainer );
  1275. tab_container->set_tabs_visible(false);
  1276. script_split->add_child(tab_container);
  1277. tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
  1278. file_menu = memnew( MenuButton );
  1279. menu_hb->add_child(file_menu);
  1280. file_menu->set_text("File");
  1281. file_menu->get_popup()->add_item("Open",FILE_OPEN);
  1282. file_menu->get_popup()->add_separator();
  1283. file_menu->get_popup()->add_item("Save",FILE_SAVE,KEY_MASK_ALT|KEY_MASK_CMD|KEY_S);
  1284. file_menu->get_popup()->add_item("Save As..",FILE_SAVE_AS);
  1285. file_menu->get_popup()->add_item("Save All",FILE_SAVE_ALL,KEY_MASK_CMD|KEY_MASK_SHIFT|KEY_S);
  1286. file_menu->get_popup()->add_separator();
  1287. file_menu->get_popup()->add_item("Close",FILE_CLOSE,KEY_MASK_CMD|KEY_W);
  1288. file_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1289. edit_menu = memnew( MenuButton );
  1290. menu_hb->add_child(edit_menu);
  1291. edit_menu->set_text("Edit");
  1292. edit_menu->get_popup()->add_item("Undo",EDIT_UNDO,KEY_MASK_CMD|KEY_Z);
  1293. edit_menu->get_popup()->add_item("Redo",EDIT_REDO,KEY_MASK_CMD|KEY_Y);
  1294. edit_menu->get_popup()->add_separator();
  1295. edit_menu->get_popup()->add_item("Cut",EDIT_CUT,KEY_MASK_CMD|KEY_X);
  1296. edit_menu->get_popup()->add_item("Copy",EDIT_COPY,KEY_MASK_CMD|KEY_C);
  1297. edit_menu->get_popup()->add_item("Paste",EDIT_PASTE,KEY_MASK_CMD|KEY_V);
  1298. edit_menu->get_popup()->add_separator();
  1299. edit_menu->get_popup()->add_item("Select All",EDIT_SELECT_ALL,KEY_MASK_CMD|KEY_A);
  1300. edit_menu->get_popup()->add_separator();
  1301. edit_menu->get_popup()->add_item("Move Up",EDIT_MOVE_LINE_UP,KEY_MASK_ALT|KEY_UP);
  1302. edit_menu->get_popup()->add_item("Move Down",EDIT_MOVE_LINE_DOWN,KEY_MASK_ALT|KEY_DOWN);
  1303. edit_menu->get_popup()->add_item("Indent Left",EDIT_INDENT_LEFT,KEY_MASK_ALT|KEY_LEFT);
  1304. edit_menu->get_popup()->add_item("Indent Right",EDIT_INDENT_RIGHT,KEY_MASK_ALT|KEY_RIGHT);
  1305. edit_menu->get_popup()->add_item("Toggle Comment",EDIT_TOGGLE_COMMENT,KEY_MASK_CMD|KEY_K);
  1306. edit_menu->get_popup()->add_item("Clone Down",EDIT_CLONE_DOWN,KEY_MASK_CMD|KEY_B);
  1307. edit_menu->get_popup()->add_separator();
  1308. #ifdef OSX_ENABLED
  1309. edit_menu->get_popup()->add_item("Complete Symbol",EDIT_COMPLETE,KEY_MASK_CTRL|KEY_SPACE);
  1310. #else
  1311. edit_menu->get_popup()->add_item("Complete Symbol",EDIT_COMPLETE,KEY_MASK_CMD|KEY_SPACE);
  1312. #endif
  1313. edit_menu->get_popup()->add_item("Auto Indent",EDIT_AUTO_INDENT,KEY_MASK_CMD|KEY_I);
  1314. edit_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1315. search_menu = memnew( MenuButton );
  1316. menu_hb->add_child(search_menu);
  1317. search_menu->set_text("Search");
  1318. search_menu->get_popup()->add_item("Find..",SEARCH_FIND,KEY_MASK_CMD|KEY_F);
  1319. search_menu->get_popup()->add_item("Find Next",SEARCH_FIND_NEXT,KEY_MASK_CMD|KEY_G);
  1320. search_menu->get_popup()->add_item("Replace..",SEARCH_REPLACE,KEY_MASK_CMD|KEY_R);
  1321. search_menu->get_popup()->add_separator();
  1322. search_menu->get_popup()->add_item("Goto Function..",SEARCH_LOCATE_FUNCTION,KEY_MASK_SHIFT|KEY_MASK_CMD|KEY_F);
  1323. search_menu->get_popup()->add_item("Goto Line..",SEARCH_GOTO_LINE,KEY_MASK_CMD|KEY_L);
  1324. search_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1325. debug_menu = memnew( MenuButton );
  1326. menu_hb->add_child(debug_menu);
  1327. debug_menu->set_text("Debug");
  1328. debug_menu->get_popup()->add_item("Toggle Breakpoint",DEBUG_TOGGLE_BREAKPOINT,KEY_F9);
  1329. debug_menu->get_popup()->add_separator();
  1330. debug_menu->get_popup()->add_item("Step Over",DEBUG_NEXT,KEY_F10);
  1331. debug_menu->get_popup()->add_item("Step Into",DEBUG_STEP,KEY_F11);
  1332. debug_menu->get_popup()->add_separator();
  1333. debug_menu->get_popup()->add_item("Break",DEBUG_BREAK);
  1334. debug_menu->get_popup()->add_item("Continue",DEBUG_CONTINUE);
  1335. debug_menu->get_popup()->add_separator();
  1336. debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
  1337. debug_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1338. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1339. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1340. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  1341. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1342. #if 0
  1343. window_menu = memnew( MenuButton );
  1344. menu_hb->add_child(window_menu);
  1345. window_menu->set_text("Window");
  1346. window_menu->get_popup()->add_item("Close",WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
  1347. window_menu->get_popup()->add_separator();
  1348. window_menu->get_popup()->add_item("Move Left",WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
  1349. window_menu->get_popup()->add_item("Move Right",WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
  1350. window_menu->get_popup()->add_separator();
  1351. window_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1352. #endif
  1353. help_menu = memnew( MenuButton );
  1354. menu_hb->add_child(help_menu);
  1355. help_menu->set_text("Help");
  1356. help_menu->get_popup()->add_item("Contextual", HELP_CONTEXTUAL, KEY_MASK_SHIFT|KEY_F1);
  1357. help_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1358. tab_container->connect("tab_changed", this,"_tab_changed");
  1359. find_replace_dialog = memnew(FindReplaceDialog);
  1360. add_child(find_replace_dialog);
  1361. erase_tab_confirm = memnew( ConfirmationDialog );
  1362. add_child(erase_tab_confirm);
  1363. erase_tab_confirm->connect("confirmed", this,"_close_current_tab");
  1364. goto_line_dialog = memnew(GotoLineDialog);
  1365. add_child(goto_line_dialog);
  1366. debugger = memnew( ScriptEditorDebugger(editor) );
  1367. debugger->connect("goto_script_line",this,"_goto_script_line");
  1368. debugger->connect("show_debugger",this,"_show_debugger");
  1369. disk_changed = memnew( ConfirmationDialog );
  1370. {
  1371. VBoxContainer *vbc = memnew( VBoxContainer );
  1372. disk_changed->add_child(vbc);
  1373. disk_changed->set_child_rect(vbc);
  1374. Label *dl = memnew( Label );
  1375. dl->set_text("The following files are newer on disk.\nWhat action should be taken?:");
  1376. vbc->add_child(dl);
  1377. disk_changed_list = memnew( Tree );
  1378. vbc->add_child(disk_changed_list);
  1379. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  1380. disk_changed->connect("confirmed",this,"_reload_scripts");
  1381. disk_changed->get_ok()->set_text("Reload");
  1382. disk_changed->add_button("Resave",!OS::get_singleton()->get_swap_ok_cancel(),"resave");
  1383. disk_changed->connect("custom_action",this,"_resave_scripts");
  1384. }
  1385. add_child(disk_changed);
  1386. script_editor=this;
  1387. quick_open = memnew( ScriptEditorQuickOpen );
  1388. add_child(quick_open);
  1389. quick_open->connect("goto_line",this,"_goto_script_line2");
  1390. v_split->add_child(debugger);
  1391. debugger->connect("breaked",this,"_breaked");
  1392. autosave_timer = memnew( Timer );
  1393. autosave_timer->set_one_shot(false);
  1394. add_child(autosave_timer);
  1395. grab_focus_block=false;
  1396. // debugger_gui->hide();
  1397. }
  1398. ScriptEditor::~ScriptEditor() {
  1399. memdelete(completion_cache);
  1400. }
  1401. void ScriptEditorPlugin::edit(Object *p_object) {
  1402. if (!p_object->cast_to<Script>())
  1403. return;
  1404. script_editor->edit(p_object->cast_to<Script>());
  1405. }
  1406. bool ScriptEditorPlugin::handles(Object *p_object) const {
  1407. return p_object->is_type("Script");
  1408. }
  1409. void ScriptEditorPlugin::make_visible(bool p_visible) {
  1410. if (p_visible) {
  1411. script_editor->show();
  1412. script_editor->set_process(true);
  1413. script_editor->ensure_select_current();
  1414. } else {
  1415. script_editor->hide();
  1416. script_editor->set_process(false);
  1417. }
  1418. }
  1419. void ScriptEditorPlugin::selected_notify() {
  1420. script_editor->ensure_select_current();
  1421. }
  1422. Dictionary ScriptEditorPlugin::get_state() const {
  1423. return script_editor->get_state();
  1424. }
  1425. void ScriptEditorPlugin::set_state(const Dictionary& p_state) {
  1426. script_editor->set_state(p_state);
  1427. }
  1428. void ScriptEditorPlugin::clear() {
  1429. script_editor->clear();
  1430. }
  1431. void ScriptEditorPlugin::save_external_data() {
  1432. script_editor->save_external_data();
  1433. }
  1434. void ScriptEditorPlugin::apply_changes() {
  1435. script_editor->apply_scripts();
  1436. }
  1437. void ScriptEditorPlugin::restore_global_state() {
  1438. }
  1439. void ScriptEditorPlugin::save_global_state() {
  1440. }
  1441. void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  1442. script_editor->set_window_layout(p_layout);
  1443. }
  1444. void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout){
  1445. script_editor->get_window_layout(p_layout);
  1446. }
  1447. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  1448. return script_editor->get_breakpoints(p_breakpoints);
  1449. }
  1450. ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
  1451. editor=p_node;
  1452. script_editor = memnew( ScriptEditor(p_node) );
  1453. editor->get_viewport()->add_child(script_editor);
  1454. script_editor->set_area_as_parent_rect();
  1455. script_editor->hide();
  1456. EDITOR_DEF("text_editor/auto_reload_changed_scripts",false);
  1457. EDITOR_DEF("external_editor/use_external_editor",false);
  1458. EDITOR_DEF("external_editor/exec_path","");
  1459. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"external_editor/exec_path",PROPERTY_HINT_GLOBAL_FILE));
  1460. EDITOR_DEF("external_editor/exec_flags","");
  1461. }
  1462. ScriptEditorPlugin::~ScriptEditorPlugin()
  1463. {
  1464. }