script_editor_plugin.cpp 47 KB

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