script_editor_plugin.cpp 51 KB

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