script_editor_plugin.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413
  1. /*************************************************************************/
  2. /* script_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "script_editor_plugin.h"
  30. #include "tools/editor/editor_settings.h"
  31. #include "io/resource_loader.h"
  32. #include "io/resource_saver.h"
  33. #include "os/keyboard.h"
  34. #include "os/os.h"
  35. #include "tools/editor/editor_node.h"
  36. #include "tools/editor/script_editor_debugger.h"
  37. #include "globals.h"
  38. #include "os/file_access.h"
  39. #include "scene/main/viewport.h"
  40. #include "os/keyboard.h"
  41. #include "os/input.h"
  42. /*** SCRIPT EDITOR ****/
  43. static bool _can_open_in_editor(Script* p_script) {
  44. String path = p_script->get_path();
  45. if (path.find("::")!=-1) {
  46. //refuse handling this if it can't be edited
  47. bool valid=false;
  48. for(int i=0;i<EditorNode::get_singleton()->get_editor_data().get_edited_scene_count();i++) {
  49. if (path.begins_with(EditorNode::get_singleton()->get_editor_data().get_scene_path(i))) {
  50. valid=true;
  51. break;
  52. }
  53. }
  54. return valid;
  55. }
  56. return true;
  57. }
  58. class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache {
  59. struct Cache {
  60. uint64_t time_loaded;
  61. RES cache;
  62. };
  63. Map<String,Cache> cached;
  64. public:
  65. uint64_t max_time_cache;
  66. int max_cache_size;
  67. void cleanup() {
  68. List< Map<String,Cache>::Element * > to_clean;
  69. Map<String,Cache>::Element *I=cached.front();
  70. while(I) {
  71. if ((OS::get_singleton()->get_ticks_msec()-I->get().time_loaded)>max_time_cache) {
  72. to_clean.push_back(I);
  73. }
  74. I=I->next();
  75. }
  76. while(to_clean.front()) {
  77. cached.erase(to_clean.front()->get());
  78. to_clean.pop_front();
  79. }
  80. }
  81. RES get_cached_resource(const String& p_path) {
  82. Map<String,Cache>::Element *E=cached.find(p_path);
  83. if (!E) {
  84. Cache c;
  85. c.cache=ResourceLoader::load(p_path);
  86. E=cached.insert(p_path,c);
  87. }
  88. E->get().time_loaded=OS::get_singleton()->get_ticks_msec();
  89. if (cached.size()>max_cache_size) {
  90. uint64_t older;
  91. Map<String,Cache>::Element *O=cached.front();
  92. older=O->get().time_loaded;
  93. Map<String,Cache>::Element *I=O;
  94. while(I) {
  95. if (I->get().time_loaded<older) {
  96. older = I->get().time_loaded;
  97. O=I;
  98. }
  99. I=I->next();
  100. }
  101. if (O!=E) {//should never heppane..
  102. cached.erase(O);
  103. }
  104. }
  105. return E->get().cache;
  106. }
  107. EditorScriptCodeCompletionCache() {
  108. max_cache_size=128;
  109. max_time_cache=5*60*1000; //minutes, five
  110. }
  111. };
  112. #define SORT_SCRIPT_LIST
  113. void ScriptEditorQuickOpen::popup(const Vector<String>& p_functions, bool p_dontclear) {
  114. popup_centered_ratio(0.6);
  115. if (p_dontclear)
  116. search_box->select_all();
  117. else
  118. search_box->clear();
  119. search_box->grab_focus();
  120. functions=p_functions;
  121. _update_search();
  122. }
  123. void ScriptEditorQuickOpen::_text_changed(const String& p_newtext) {
  124. _update_search();
  125. }
  126. void ScriptEditorQuickOpen::_sbox_input(const InputEvent& p_ie) {
  127. if (p_ie.type==InputEvent::KEY && (
  128. p_ie.key.scancode == KEY_UP ||
  129. p_ie.key.scancode == KEY_DOWN ||
  130. p_ie.key.scancode == KEY_PAGEUP ||
  131. p_ie.key.scancode == KEY_PAGEDOWN ) ) {
  132. search_options->call("_input_event",p_ie);
  133. search_box->accept_event();
  134. }
  135. }
  136. void ScriptEditorQuickOpen::_update_search() {
  137. search_options->clear();
  138. TreeItem *root = search_options->create_item();
  139. for(int i=0;i<functions.size();i++) {
  140. String file = functions[i];
  141. if ((search_box->get_text()=="" || file.findn(search_box->get_text())!=-1)) {
  142. TreeItem *ti = search_options->create_item(root);
  143. ti->set_text(0,file);
  144. if (root->get_children()==ti)
  145. ti->select(0);
  146. }
  147. }
  148. get_ok()->set_disabled(root->get_children()==NULL);
  149. }
  150. void ScriptEditorQuickOpen::_confirmed() {
  151. TreeItem *ti = search_options->get_selected();
  152. if (!ti)
  153. return;
  154. int line = ti->get_text(0).get_slice(":",1).to_int();
  155. emit_signal("goto_line",line-1);
  156. hide();
  157. }
  158. void ScriptEditorQuickOpen::_notification(int p_what) {
  159. if (p_what==NOTIFICATION_ENTER_TREE) {
  160. connect("confirmed",this,"_confirmed");
  161. }
  162. }
  163. void ScriptEditorQuickOpen::_bind_methods() {
  164. ObjectTypeDB::bind_method(_MD("_text_changed"),&ScriptEditorQuickOpen::_text_changed);
  165. ObjectTypeDB::bind_method(_MD("_confirmed"),&ScriptEditorQuickOpen::_confirmed);
  166. ObjectTypeDB::bind_method(_MD("_sbox_input"),&ScriptEditorQuickOpen::_sbox_input);
  167. ADD_SIGNAL(MethodInfo("goto_line",PropertyInfo(Variant::INT,"line")));
  168. }
  169. ScriptEditorQuickOpen::ScriptEditorQuickOpen() {
  170. VBoxContainer *vbc = memnew( VBoxContainer );
  171. add_child(vbc);
  172. set_child_rect(vbc);
  173. search_box = memnew( LineEdit );
  174. vbc->add_margin_child(TTR("Search:"),search_box);
  175. search_box->connect("text_changed",this,"_text_changed");
  176. search_box->connect("input_event",this,"_sbox_input");
  177. search_options = memnew( Tree );
  178. vbc->add_margin_child(TTR("Matches:"),search_options,true);
  179. get_ok()->set_text(TTR("Open"));
  180. get_ok()->set_disabled(true);
  181. register_text_enter(search_box);
  182. set_hide_on_ok(false);
  183. search_options->connect("item_activated",this,"_confirmed");
  184. search_options->set_hide_root(true);
  185. }
  186. /////////////////////////////////
  187. ScriptEditor *ScriptEditor::script_editor=NULL;
  188. /*** SCRIPT EDITOR ******/
  189. String ScriptEditor::_get_debug_tooltip(const String&p_text,Node *_se) {
  190. // ScriptEditorBase *se=_se->cast_to<ScriptEditorBase>();
  191. String val = debugger->get_var_value(p_text);
  192. if (val!=String()) {
  193. return p_text+": "+val;
  194. } else {
  195. return String();
  196. }
  197. }
  198. void ScriptEditor::_breaked(bool p_breaked,bool p_can_debug) {
  199. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug));
  200. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug) );
  201. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked );
  202. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked );
  203. for(int i=0;i<tab_container->get_child_count();i++) {
  204. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  205. if (!se) {
  206. continue;
  207. }
  208. se->set_debugger_active(p_breaked);
  209. }
  210. }
  211. void ScriptEditor::_show_debugger(bool p_show) {
  212. // debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), p_show);
  213. }
  214. void ScriptEditor::_script_created(Ref<Script> p_script) {
  215. editor->push_item(p_script.operator->());
  216. }
  217. void ScriptEditor::_goto_script_line2(int p_line) {
  218. int selected = tab_container->get_current_tab();
  219. if (selected<0 || selected>=tab_container->get_child_count())
  220. return;
  221. ScriptEditorBase *current = tab_container->get_child(selected)->cast_to<ScriptEditorBase>();
  222. if (!current)
  223. return;
  224. current->goto_line(p_line);
  225. }
  226. void ScriptEditor::_goto_script_line(REF p_script,int p_line) {
  227. editor->push_item(p_script.ptr());
  228. int selected = tab_container->get_current_tab();
  229. if (selected<0 || selected>=tab_container->get_child_count())
  230. return;
  231. ScriptEditorBase *current = tab_container->get_child(selected)->cast_to<ScriptEditorBase>();
  232. if (!current)
  233. return;
  234. current->goto_line(p_line,true);
  235. }
  236. void ScriptEditor::_update_history_arrows() {
  237. script_back->set_disabled( history_pos<=0 );
  238. script_forward->set_disabled( history_pos>=history.size()-1 );
  239. }
  240. void ScriptEditor::_go_to_tab(int p_idx) {
  241. Node *cn = tab_container->get_child(p_idx);
  242. if (!cn)
  243. return;
  244. Control *c = cn->cast_to<Control>();
  245. if (!c)
  246. return;
  247. if (history_pos>=0 && history_pos<history.size() && history[history_pos].control==tab_container->get_current_tab_control()) {
  248. Node *n = tab_container->get_current_tab_control();
  249. if (n->cast_to<ScriptEditorBase>()) {
  250. history[history_pos].state=n->cast_to<ScriptEditorBase>()->get_edit_state();
  251. }
  252. if (n->cast_to<EditorHelp>()) {
  253. history[history_pos].state=n->cast_to<EditorHelp>()->get_scroll();
  254. }
  255. }
  256. history.resize(history_pos+1);
  257. ScriptHistory sh;
  258. sh.control=c;
  259. sh.state=Variant();
  260. history.push_back(sh);
  261. history_pos++;
  262. tab_container->set_current_tab(p_idx);
  263. c = tab_container->get_current_tab_control();
  264. if (c->cast_to<ScriptEditorBase>()) {
  265. script_name_label->set_text(c->cast_to<ScriptEditorBase>()->get_name());
  266. script_icon->set_texture(c->cast_to<ScriptEditorBase>()->get_icon());
  267. if (is_visible())
  268. c->cast_to<ScriptEditorBase>()->ensure_focus();
  269. }
  270. if (c->cast_to<EditorHelp>()) {
  271. script_name_label->set_text(c->cast_to<EditorHelp>()->get_class_name());
  272. script_icon->set_texture(get_icon("Help","EditorIcons"));
  273. if (is_visible())
  274. c->cast_to<EditorHelp>()->set_focused();
  275. }
  276. c->set_meta("__editor_pass",++edit_pass);
  277. _update_history_arrows();
  278. _update_script_colors();
  279. _update_selected_editor_menu();
  280. }
  281. void ScriptEditor::_close_tab(int p_idx) {
  282. int selected = p_idx;
  283. if (selected<0 || selected>=tab_container->get_child_count())
  284. return;
  285. Node *tselected = tab_container->get_child(selected);
  286. ScriptEditorBase *current = tab_container->get_child(selected)->cast_to<ScriptEditorBase>();
  287. if (current) {
  288. apply_scripts();
  289. if (current->get_edit_menu()) {
  290. memdelete(current->get_edit_menu());
  291. }
  292. }
  293. //remove from history
  294. history.resize(history_pos+1);
  295. for(int i=0;i<history.size();i++) {
  296. if (history[i].control==tselected) {
  297. history.remove(i);
  298. i--;
  299. history_pos--;
  300. }
  301. }
  302. if (history_pos>=history.size()) {
  303. history_pos=history.size()-1;
  304. }
  305. int idx = tab_container->get_current_tab();
  306. memdelete(tselected);
  307. if (idx>=tab_container->get_child_count())
  308. idx=tab_container->get_child_count()-1;
  309. if (idx>=0) {
  310. if (history_pos>=0) {
  311. idx = history[history_pos].control->get_index();
  312. }
  313. tab_container->set_current_tab(idx);
  314. //script_list->select(idx);
  315. }
  316. _update_history_arrows();
  317. _update_script_names();
  318. _save_layout();
  319. }
  320. void ScriptEditor::_close_current_tab() {
  321. _close_tab(tab_container->get_current_tab());
  322. }
  323. void ScriptEditor::_close_docs_tab() {
  324. int child_count = tab_container->get_child_count();
  325. for (int i = child_count-1; i>=0; i--) {
  326. EditorHelp *se = tab_container->get_child(i)->cast_to<EditorHelp>();
  327. if (se) {
  328. _close_tab(i);
  329. }
  330. }
  331. }
  332. void ScriptEditor::_close_all_tabs() {
  333. int child_count = tab_container->get_child_count();
  334. for (int i = child_count-1; i>=0; i--) {
  335. tab_container->set_current_tab(i);
  336. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  337. if (se) {
  338. // Maybe there are unsaved changes
  339. if (se->is_unsaved()) {
  340. _ask_close_current_unsaved_tab(se);
  341. continue;
  342. }
  343. }
  344. _close_current_tab();
  345. }
  346. }
  347. void ScriptEditor::_ask_close_current_unsaved_tab(ScriptEditorBase *current) {
  348. erase_tab_confirm->set_text("Close and save changes?\n\""+current->get_name()+"\"");
  349. erase_tab_confirm->popup_centered_minsize();
  350. }
  351. void ScriptEditor::_resave_scripts(const String& p_str) {
  352. apply_scripts();
  353. for(int i=0;i<tab_container->get_child_count();i++) {
  354. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  355. if (!se)
  356. continue;
  357. Ref<Script> script = se->get_edited_script();
  358. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  359. continue; //internal script, who cares
  360. if (trim_trailing_whitespace_on_save) {
  361. se->trim_trailing_whitespace();
  362. }
  363. editor->save_resource(script);
  364. se->tag_saved_version();
  365. }
  366. disk_changed->hide();
  367. }
  368. void ScriptEditor::_reload_scripts(){
  369. for(int i=0;i<tab_container->get_child_count();i++) {
  370. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  371. if (!se) {
  372. continue;
  373. }
  374. Ref<Script> script = se->get_edited_script();
  375. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  376. continue; //internal script, who cares
  377. }
  378. uint64_t last_date = script->get_last_modified_time();
  379. uint64_t date = FileAccess::get_modified_time(script->get_path());
  380. //printf("last date: %lli vs date: %lli\n",last_date,date);
  381. if (last_date==date) {
  382. continue;
  383. }
  384. Ref<Script> rel_script = ResourceLoader::load(script->get_path(),script->get_type(),true);
  385. ERR_CONTINUE(!rel_script.is_valid());
  386. script->set_source_code( rel_script->get_source_code() );
  387. script->set_last_modified_time( rel_script->get_last_modified_time() );
  388. script->reload();
  389. se->reload_text();
  390. }
  391. disk_changed->hide();
  392. _update_script_names();
  393. }
  394. void ScriptEditor::_res_saved_callback(const Ref<Resource>& p_res) {
  395. for(int i=0;i<tab_container->get_child_count();i++) {
  396. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  397. if (!se) {
  398. continue;
  399. }
  400. Ref<Script> script = se->get_edited_script();
  401. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  402. continue; //internal script, who cares
  403. }
  404. if (script==p_res) {
  405. se->tag_saved_version();
  406. }
  407. }
  408. _update_script_names();
  409. if (!pending_auto_reload && auto_reload_running_scripts) {
  410. call_deferred("_live_auto_reload_running_scripts");
  411. pending_auto_reload=true;
  412. }
  413. }
  414. void ScriptEditor::_live_auto_reload_running_scripts() {
  415. pending_auto_reload=false;
  416. debugger->reload_scripts();
  417. }
  418. bool ScriptEditor::_test_script_times_on_disk(Ref<Script> p_for_script) {
  419. disk_changed_list->clear();
  420. TreeItem *r = disk_changed_list->create_item();
  421. disk_changed_list->set_hide_root(true);
  422. bool need_ask=false;
  423. bool need_reload=false;
  424. bool use_autoreload=bool(EDITOR_DEF("text_editor/auto_reload_scripts_on_external_change",false));
  425. for(int i=0;i<tab_container->get_child_count();i++) {
  426. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  427. if (se) {
  428. Ref<Script> script = se->get_edited_script();
  429. if (p_for_script.is_valid() && p_for_script!=script)
  430. continue;
  431. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  432. continue; //internal script, who cares
  433. uint64_t last_date = script->get_last_modified_time();
  434. uint64_t date = FileAccess::get_modified_time(script->get_path());
  435. //printf("last date: %lli vs date: %lli\n",last_date,date);
  436. if (last_date!=date) {
  437. TreeItem *ti = disk_changed_list->create_item(r);
  438. ti->set_text(0,script->get_path().get_file());
  439. if (!use_autoreload || se->is_unsaved()) {
  440. need_ask=true;
  441. }
  442. need_reload=true;
  443. //r->set_metadata(0,);
  444. }
  445. }
  446. }
  447. if (need_reload) {
  448. if (!need_ask) {
  449. script_editor->_reload_scripts();
  450. need_reload=false;
  451. } else {
  452. disk_changed->call_deferred("popup_centered_ratio",0.5);
  453. }
  454. }
  455. return need_reload;
  456. }
  457. void ScriptEditor::_file_dialog_action(String p_file) {
  458. switch (file_dialog_option) {
  459. case FILE_SAVE_THEME_AS: {
  460. if(!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) {
  461. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  462. }
  463. } break;
  464. case FILE_IMPORT_THEME: {
  465. if(!EditorSettings::get_singleton()->import_text_editor_theme(p_file)) {
  466. editor->show_warning(TTR("Error importing theme"), TTR("Error importing"));
  467. }
  468. } break;
  469. }
  470. file_dialog_option = -1;
  471. }
  472. void ScriptEditor::_menu_option(int p_option) {
  473. switch(p_option) {
  474. case FILE_NEW: {
  475. script_create_dialog->config("Node", ".gd");
  476. script_create_dialog->popup_centered(Size2(300, 300)*EDSCALE);
  477. } break;
  478. case FILE_OPEN: {
  479. editor->open_resource("Script");
  480. return;
  481. } break;
  482. case FILE_SAVE_ALL: {
  483. if (!_test_script_times_on_disk())
  484. return;
  485. save_all_scripts();
  486. #if 0
  487. for(int i=0;i<tab_container->get_child_count();i++) {
  488. ScriptTextEditor *se = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  489. if (!se)
  490. continue;
  491. Ref<Script> script = se->get_edited_script();
  492. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  493. continue; //internal script, who cares
  494. editor->save_resource( script );
  495. }
  496. #endif
  497. } break;
  498. case FILE_IMPORT_THEME: {
  499. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  500. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  501. file_dialog_option = FILE_IMPORT_THEME;
  502. file_dialog->clear_filters();
  503. file_dialog->add_filter("*.tet");
  504. file_dialog->popup_centered_ratio();
  505. file_dialog->set_title(TTR("Import Theme"));
  506. } break;
  507. case FILE_RELOAD_THEME: {
  508. EditorSettings::get_singleton()->load_text_editor_theme();
  509. } break;
  510. case FILE_SAVE_THEME: {
  511. if(!EditorSettings::get_singleton()->save_text_editor_theme()) {
  512. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  513. }
  514. } break;
  515. case FILE_SAVE_THEME_AS: {
  516. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  517. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  518. file_dialog_option = FILE_SAVE_THEME_AS;
  519. file_dialog->clear_filters();
  520. file_dialog->add_filter("*.tet");
  521. file_dialog->set_current_path(EditorSettings::get_singleton()->get_settings_path() + "/text_editor_themes/" + EditorSettings::get_singleton()->get("text_editor/color_theme"));
  522. file_dialog->popup_centered_ratio();
  523. file_dialog->set_title(TTR("Save Theme As.."));
  524. } break;
  525. case SEARCH_HELP: {
  526. help_search_dialog->popup();
  527. } break;
  528. case SEARCH_CLASSES: {
  529. String current;
  530. if (tab_container->get_tab_count()>0) {
  531. EditorHelp *eh = tab_container->get_child( tab_container->get_current_tab() )->cast_to<EditorHelp>();
  532. if (eh) {
  533. current=eh->get_class_name();
  534. }
  535. }
  536. help_index->popup();
  537. if (current!="") {
  538. help_index->call_deferred("select_class",current);
  539. }
  540. } break;
  541. case SEARCH_WEBSITE: {
  542. OS::get_singleton()->shell_open("http://docs.godotengine.org/");
  543. } break;
  544. case WINDOW_NEXT: {
  545. _history_forward();
  546. } break;
  547. case WINDOW_PREV: {
  548. _history_back();
  549. } break;
  550. case DEBUG_SHOW: {
  551. if (debugger) {
  552. bool visible = debug_menu->get_popup()->is_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW) );
  553. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), !visible);
  554. if (visible)
  555. debugger->hide();
  556. else
  557. debugger->show();
  558. }
  559. } break;
  560. case DEBUG_SHOW_KEEP_OPEN: {
  561. bool visible = debug_menu->get_popup()->is_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN) );
  562. if (debugger)
  563. debugger->set_hide_on_stop(visible);
  564. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN), !visible);
  565. } break;
  566. }
  567. int selected = tab_container->get_current_tab();
  568. if (selected<0 || selected>=tab_container->get_child_count())
  569. return;
  570. ScriptEditorBase *current = tab_container->get_child(selected)->cast_to<ScriptEditorBase>();
  571. if (current) {
  572. switch(p_option) {
  573. case FILE_NEW: {
  574. script_create_dialog->config("Node", ".gd");
  575. script_create_dialog->popup_centered(Size2(300, 300)*EDSCALE);
  576. } break;
  577. case FILE_SAVE: {
  578. if (_test_script_times_on_disk())
  579. return;
  580. if (trim_trailing_whitespace_on_save)
  581. current->trim_trailing_whitespace();
  582. editor->save_resource( current->get_edited_script() );
  583. } break;
  584. case FILE_SAVE_AS: {
  585. current->trim_trailing_whitespace();
  586. editor->push_item(current->get_edited_script()->cast_to<Object>());
  587. editor->save_resource_as( current->get_edited_script() );
  588. } break;
  589. case FILE_TOOL_RELOAD:
  590. case FILE_TOOL_RELOAD_SOFT: {
  591. current->reload(p_option==FILE_TOOL_RELOAD_SOFT);
  592. } break;
  593. case FILE_CLOSE: {
  594. if (current->is_unsaved()) {
  595. _ask_close_current_unsaved_tab(current);
  596. } else {
  597. _close_current_tab();
  598. }
  599. } break;
  600. case CLOSE_DOCS: {
  601. _close_docs_tab();
  602. } break;
  603. case CLOSE_ALL: {
  604. _close_all_tabs();
  605. } break;
  606. case DEBUG_NEXT: {
  607. if (debugger)
  608. debugger->debug_next();
  609. } break;
  610. case DEBUG_STEP: {
  611. if (debugger)
  612. debugger->debug_step();
  613. } break;
  614. case DEBUG_BREAK: {
  615. if (debugger)
  616. debugger->debug_break();
  617. } break;
  618. case DEBUG_CONTINUE: {
  619. if (debugger)
  620. debugger->debug_continue();
  621. } break;
  622. case WINDOW_MOVE_LEFT: {
  623. if (tab_container->get_current_tab()>0) {
  624. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()-1);
  625. script_list->call_deferred("select",tab_container->get_current_tab()-1);
  626. tab_container->move_child(current,tab_container->get_current_tab()-1);
  627. _update_script_names();
  628. }
  629. } break;
  630. case WINDOW_MOVE_RIGHT: {
  631. if (tab_container->get_current_tab()<tab_container->get_child_count()-1) {
  632. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()+1);
  633. script_list->call_deferred("select",tab_container->get_current_tab()+1);
  634. tab_container->move_child(current,tab_container->get_current_tab()+1);
  635. _update_script_names();
  636. }
  637. } break;
  638. default: {
  639. if (p_option>=WINDOW_SELECT_BASE) {
  640. tab_container->set_current_tab(p_option-WINDOW_SELECT_BASE);
  641. script_list->select(p_option-WINDOW_SELECT_BASE);
  642. }
  643. }
  644. }
  645. }
  646. EditorHelp *help = tab_container->get_child(selected)->cast_to<EditorHelp>();
  647. if (help) {
  648. switch(p_option) {
  649. case HELP_SEARCH_FIND: {
  650. help->popup_search();
  651. } break;
  652. case HELP_SEARCH_FIND_NEXT: {
  653. help->search_again();
  654. } break;
  655. case FILE_CLOSE: {
  656. _close_current_tab();
  657. } break;
  658. case CLOSE_DOCS: {
  659. _close_docs_tab();
  660. } break;
  661. case CLOSE_ALL: {
  662. _close_all_tabs();
  663. } break;
  664. }
  665. }
  666. }
  667. void ScriptEditor::_tab_changed(int p_which) {
  668. ensure_select_current();
  669. }
  670. void ScriptEditor::_notification(int p_what) {
  671. if (p_what==NOTIFICATION_ENTER_TREE) {
  672. editor->connect("play_pressed",this,"_editor_play");
  673. editor->connect("pause_pressed",this,"_editor_pause");
  674. editor->connect("stop_pressed",this,"_editor_stop");
  675. editor->connect("script_add_function_request",this,"_add_callback");
  676. editor->connect("resource_saved",this,"_res_saved_callback");
  677. script_list->connect("item_selected",this,"_script_selected");
  678. script_split->connect("dragged",this,"_script_split_dragged");
  679. autosave_timer->connect("timeout",this,"_autosave_scripts");
  680. {
  681. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  682. if (autosave_time>0) {
  683. autosave_timer->set_wait_time(autosave_time);
  684. autosave_timer->start();
  685. } else {
  686. autosave_timer->stop();
  687. }
  688. }
  689. EditorSettings::get_singleton()->connect("settings_changed",this,"_editor_settings_changed");
  690. help_search->set_icon(get_icon("Help","EditorIcons"));
  691. site_search->set_icon(get_icon("Godot","EditorIcons"));
  692. class_search->set_icon(get_icon("ClassList","EditorIcons"));
  693. script_forward->set_icon(get_icon("Forward","EditorIcons"));
  694. script_back->set_icon(get_icon("Back","EditorIcons"));
  695. }
  696. if (p_what==NOTIFICATION_READY) {
  697. get_tree()->connect("tree_changed",this,"_tree_changed");
  698. editor->connect("request_help",this,"_request_help");
  699. }
  700. if (p_what==NOTIFICATION_EXIT_TREE) {
  701. editor->disconnect("play_pressed",this,"_editor_play");
  702. editor->disconnect("pause_pressed",this,"_editor_pause");
  703. editor->disconnect("stop_pressed",this,"_editor_stop");
  704. }
  705. if (p_what==MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  706. _test_script_times_on_disk();
  707. _update_modified_scripts_for_external_editor();
  708. }
  709. if (p_what==NOTIFICATION_PROCESS) {
  710. }
  711. }
  712. bool ScriptEditor::can_take_away_focus() const {
  713. int selected = tab_container->get_current_tab();
  714. if (selected<0 || selected>=tab_container->get_child_count())
  715. return true;
  716. ScriptEditorBase *current = tab_container->get_child(selected)->cast_to<ScriptEditorBase>();
  717. if (!current)
  718. return true;
  719. return current->can_lose_focus_on_node_selection();
  720. }
  721. void ScriptEditor::close_builtin_scripts_from_scene(const String& p_scene) {
  722. for(int i=0;i<tab_container->get_child_count();i++) {
  723. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  724. if (se) {
  725. Ref<Script> script = se->get_edited_script();
  726. if (!script.is_valid())
  727. continue;
  728. if (script->get_path().find("::")!=-1 && script->get_path().begins_with(p_scene)) { //is an internal script and belongs to scene being closed
  729. _close_tab(i);
  730. i--;
  731. }
  732. }
  733. }
  734. }
  735. void ScriptEditor::edited_scene_changed() {
  736. _update_modified_scripts_for_external_editor();
  737. }
  738. static const Node * _find_node_with_script(const Node* p_node, const RefPtr & p_script) {
  739. if (p_node->get_script()==p_script)
  740. return p_node;
  741. for(int i=0;i<p_node->get_child_count();i++) {
  742. const Node *result = _find_node_with_script(p_node->get_child(i),p_script);
  743. if (result)
  744. return result;
  745. }
  746. return NULL;
  747. }
  748. Dictionary ScriptEditor::get_state() const {
  749. // apply_scripts();
  750. Dictionary state;
  751. #if 0
  752. Array paths;
  753. int open=-1;
  754. for(int i=0;i<tab_container->get_child_count();i++) {
  755. ScriptTextEditor *se = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  756. if (!se)
  757. continue;
  758. Ref<Script> script = se->get_edited_script();
  759. if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  760. paths.push_back(script->get_path());
  761. } else {
  762. const Node *owner = _find_node_with_script(get_tree()->get_root(),script.get_ref_ptr());
  763. if (owner)
  764. paths.push_back(owner->get_path());
  765. }
  766. if (i==tab_container->get_current_tab())
  767. open=i;
  768. }
  769. if (paths.size())
  770. state["sources"]=paths;
  771. if (open!=-1)
  772. state["current"]=open;
  773. #endif
  774. return state;
  775. }
  776. void ScriptEditor::set_state(const Dictionary& p_state) {
  777. #if 0
  778. print_line("attempt set state: "+String(Variant(p_state)));
  779. if (!p_state.has("sources"))
  780. return; //bleh
  781. Array sources = p_state["sources"];
  782. for(int i=0;i<sources.size();i++) {
  783. Variant source=sources[i];
  784. Ref<Script> script;
  785. if (source.get_type()==Variant::NODE_PATH) {
  786. Node *owner=get_tree()->get_root()->get_node(source);
  787. if (!owner)
  788. continue;
  789. script = owner->get_script();
  790. } else if (source.get_type()==Variant::STRING) {
  791. script = ResourceLoader::load(source,"Script");
  792. }
  793. if (script.is_null()) //ah well..
  794. continue;
  795. editor->call("_resource_selected",script);
  796. }
  797. if (p_state.has("current")) {
  798. tab_container->set_current_tab(p_state["current"]);
  799. }
  800. #endif
  801. }
  802. void ScriptEditor::clear() {
  803. #if 0
  804. List<ScriptTextEditor*> stes;
  805. for(int i=0;i<tab_container->get_child_count();i++) {
  806. ScriptTextEditor *se = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  807. if (!se)
  808. continue;
  809. stes.push_back(se);
  810. }
  811. while(stes.size()) {
  812. memdelete(stes.front()->get());
  813. stes.pop_front();
  814. }
  815. int idx = tab_container->get_current_tab();
  816. if (idx>=tab_container->get_child_count())
  817. idx=tab_container->get_child_count()-1;
  818. if (idx>=0) {
  819. tab_container->set_current_tab(idx);
  820. script_list->select( script_list->find_metadata(idx) );
  821. }
  822. #endif
  823. }
  824. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  825. for(int i=0;i<tab_container->get_child_count();i++) {
  826. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  827. if (!se)
  828. continue;
  829. List<int> bpoints;
  830. se->get_breakpoints(&bpoints);
  831. Ref<Script> script = se->get_edited_script();
  832. String base = script->get_path();
  833. ERR_CONTINUE( base.begins_with("local://") || base=="" );
  834. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  835. p_breakpoints->push_back(base+":"+itos(E->get()+1));
  836. }
  837. }
  838. }
  839. void ScriptEditor::ensure_focus_current() {
  840. if (!is_inside_tree())
  841. return;
  842. int cidx = tab_container->get_current_tab();
  843. if (cidx<0 || cidx>=tab_container->get_tab_count());
  844. Control *c = tab_container->get_child(cidx)->cast_to<Control>();
  845. if (!c)
  846. return;
  847. ScriptEditorBase *se = c->cast_to<ScriptEditorBase>();
  848. if (!se)
  849. return;
  850. se->ensure_focus();
  851. }
  852. void ScriptEditor::_script_selected(int p_idx) {
  853. grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing
  854. _go_to_tab(script_list->get_item_metadata(p_idx));
  855. grab_focus_block=false;
  856. }
  857. void ScriptEditor::ensure_select_current() {
  858. if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
  859. Node *current = tab_container->get_child(tab_container->get_current_tab());
  860. ScriptEditorBase *se = current->cast_to<ScriptEditorBase>();
  861. if (se) {
  862. Ref<Script> script = se->get_edited_script();
  863. if (!grab_focus_block && is_visible())
  864. se->ensure_focus();
  865. //edit_menu->show();
  866. //search_menu->show();
  867. }
  868. EditorHelp *eh = current->cast_to<EditorHelp>();
  869. if (eh) {
  870. //edit_menu->hide();
  871. //search_menu->hide();
  872. //script_search_menu->show();
  873. }
  874. }
  875. _update_selected_editor_menu();
  876. }
  877. void ScriptEditor::_find_scripts(Node* p_base, Node* p_current, Set<Ref<Script> > &used) {
  878. if (p_current!=p_base && p_current->get_owner()!=p_base)
  879. return;
  880. if (p_current->get_script_instance()) {
  881. Ref<Script> scr = p_current->get_script();
  882. if (scr.is_valid())
  883. used.insert(scr);
  884. }
  885. for(int i=0;i<p_current->get_child_count();i++) {
  886. _find_scripts(p_base,p_current->get_child(i),used);
  887. }
  888. }
  889. struct _ScriptEditorItemData {
  890. String name;
  891. Ref<Texture> icon;
  892. int index;
  893. String tooltip;
  894. bool used;
  895. int category;
  896. bool operator<(const _ScriptEditorItemData& id) const {
  897. return category==id.category?name.nocasecmp_to(id.name)<0:category<id.category;
  898. }
  899. };
  900. void ScriptEditor::_update_script_colors() {
  901. bool enabled = EditorSettings::get_singleton()->get("text_editor/script_temperature_enabled");
  902. bool highlight_current = EditorSettings::get_singleton()->get("text_editor/highlight_current_script");
  903. if (!enabled)
  904. return;
  905. int hist_size = EditorSettings::get_singleton()->get("text_editor/script_temperature_history_size");
  906. Color hot_color=EditorSettings::get_singleton()->get("text_editor/script_temperature_hot_color");
  907. Color cold_color=EditorSettings::get_singleton()->get("text_editor/script_temperature_cold_color");
  908. for(int i=0;i<script_list->get_item_count();i++) {
  909. int c = script_list->get_item_metadata(i);
  910. Node *n = tab_container->get_child(c);
  911. if (!n)
  912. continue;
  913. script_list->set_item_custom_bg_color(i,Color(0,0,0,0));
  914. if (!n->has_meta("__editor_pass")) {
  915. continue;
  916. }
  917. int pass=n->get_meta("__editor_pass");
  918. int h = edit_pass - pass;
  919. if (h>hist_size) {
  920. continue;
  921. }
  922. int non_zero_hist_size = ( hist_size == 0 ) ? 1 : hist_size;
  923. float v = Math::ease((edit_pass-pass)/float(non_zero_hist_size),0.4);
  924. bool current = tab_container->get_current_tab() == c;
  925. if (current && highlight_current) {
  926. script_list->set_item_custom_bg_color(i, EditorSettings::get_singleton()->get("text_editor/current_script_background_color"));
  927. } else {
  928. script_list->set_item_custom_bg_color(i,hot_color.linear_interpolate(cold_color,v));
  929. }
  930. }
  931. }
  932. void ScriptEditor::_update_script_names() {
  933. if (restoring_layout)
  934. return;
  935. waiting_update_names=false;
  936. Set<Ref<Script> > used;
  937. Node* edited = EditorNode::get_singleton()->get_edited_scene();
  938. if (edited) {
  939. _find_scripts(edited,edited,used);
  940. }
  941. script_list->clear();
  942. bool split_script_help = EditorSettings::get_singleton()->get("text_editor/group_help_pages");
  943. Vector<_ScriptEditorItemData> sedata;
  944. for(int i=0;i<tab_container->get_child_count();i++) {
  945. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  946. if (se) {
  947. String name = se->get_name();
  948. Ref<Texture> icon = se->get_icon();
  949. String tooltip = se->get_edited_script()->get_path();
  950. _ScriptEditorItemData sd;
  951. sd.icon=icon;
  952. sd.name=name;
  953. sd.tooltip=tooltip;
  954. sd.index=i;
  955. sd.used=used.has(se->get_edited_script());
  956. sd.category=0;
  957. sedata.push_back(sd);
  958. }
  959. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  960. if (eh) {
  961. String name = eh->get_class_name();
  962. Ref<Texture> icon = get_icon("Help","EditorIcons");
  963. String tooltip = name+" Class Reference";
  964. _ScriptEditorItemData sd;
  965. sd.icon=icon;
  966. sd.name=name;
  967. sd.tooltip=tooltip;
  968. sd.index=i;
  969. sd.used=false;
  970. sd.category=split_script_help?1:0;
  971. sedata.push_back(sd);
  972. }
  973. }
  974. sedata.sort();
  975. for(int i=0;i<sedata.size();i++) {
  976. script_list->add_item(sedata[i].name,sedata[i].icon);
  977. int index = script_list->get_item_count()-1;
  978. script_list->set_item_tooltip(index,sedata[i].tooltip);
  979. script_list->set_item_metadata(index,sedata[i].index);
  980. if (sedata[i].used) {
  981. script_list->set_item_custom_bg_color(index,Color(88/255.0,88/255.0,60/255.0));
  982. }
  983. if (tab_container->get_current_tab()==sedata[i].index) {
  984. script_list->select(index);
  985. script_name_label->set_text(sedata[i].name);
  986. script_icon->set_texture(sedata[i].icon);
  987. }
  988. }
  989. _update_script_colors();
  990. }
  991. void ScriptEditor::edit(const Ref<Script>& p_script, bool p_grab_focus) {
  992. if (p_script.is_null())
  993. return;
  994. // refuse to open built-in if scene is not loaded
  995. // see if already has it
  996. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
  997. if (p_script->get_path().is_resource_file() && bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor"))) {
  998. String path = EditorSettings::get_singleton()->get("external_editor/exec_path");
  999. String flags = EditorSettings::get_singleton()->get("external_editor/exec_flags");
  1000. List<String> args;
  1001. flags=flags.strip_edges();
  1002. if (flags!=String()) {
  1003. Vector<String> flagss = flags.split(" ",false);
  1004. for(int i=0;i<flagss.size();i++)
  1005. args.push_back(flagss[i]);
  1006. }
  1007. args.push_back(Globals::get_singleton()->globalize_path(p_script->get_path()));
  1008. Error err = OS::get_singleton()->execute(path,args,false);
  1009. if (err==OK)
  1010. return;
  1011. WARN_PRINT("Couldn't open external text editor, using internal");
  1012. }
  1013. for(int i=0;i<tab_container->get_child_count();i++) {
  1014. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1015. if (!se)
  1016. continue;
  1017. if (se->get_edited_script()==p_script) {
  1018. if (open_dominant || !EditorNode::get_singleton()->is_changing_scene()) {
  1019. if (tab_container->get_current_tab()!=i) {
  1020. _go_to_tab(i);
  1021. script_list->select( script_list->find_metadata(i) );
  1022. }
  1023. if (is_visible())
  1024. se->ensure_focus();
  1025. }
  1026. return;
  1027. }
  1028. }
  1029. // doesn't have it, make a new one
  1030. ScriptEditorBase *se;
  1031. for(int i=script_editor_func_count-1;i>=0;i--) {
  1032. se = script_editor_funcs[i](p_script);
  1033. if (se)
  1034. break;
  1035. }
  1036. ERR_FAIL_COND(!se);
  1037. tab_container->add_child(se);
  1038. se->set_edited_script(p_script);
  1039. se->set_tooltip_request_func("_get_debug_tooltip",this);
  1040. if (se->get_edit_menu()) {
  1041. se->get_edit_menu()->hide();
  1042. menu_hb->add_child(se->get_edit_menu());
  1043. menu_hb->move_child(se->get_edit_menu(),1);
  1044. }
  1045. if (p_grab_focus) {
  1046. _go_to_tab(tab_container->get_tab_count()-1);
  1047. }
  1048. _update_script_names();
  1049. _save_layout();
  1050. se->connect("name_changed",this,"_update_script_names");
  1051. se->connect("request_help_search",this,"_help_search");
  1052. //test for modification, maybe the script was not edited but was loaded
  1053. _test_script_times_on_disk(p_script);
  1054. _update_modified_scripts_for_external_editor(p_script);
  1055. }
  1056. void ScriptEditor::save_all_scripts() {
  1057. for(int i=0;i<tab_container->get_child_count();i++) {
  1058. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1059. if (!se)
  1060. continue;
  1061. if (!se->is_unsaved())
  1062. continue;
  1063. if (trim_trailing_whitespace_on_save) {
  1064. se->trim_trailing_whitespace();
  1065. }
  1066. Ref<Script> script = se->get_edited_script();
  1067. if (script.is_valid())
  1068. se->apply_code();
  1069. if (script->get_path()!="" && script->get_path().find("local://")==-1 &&script->get_path().find("::")==-1) {
  1070. //external script, save it
  1071. editor->save_resource(script);
  1072. //ResourceSaver::save(script->get_path(),script);
  1073. }
  1074. }
  1075. _update_script_names();
  1076. }
  1077. void ScriptEditor::apply_scripts() const {
  1078. for(int i=0;i<tab_container->get_child_count();i++) {
  1079. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1080. if (!se)
  1081. continue;
  1082. se->apply_code();
  1083. }
  1084. }
  1085. void ScriptEditor::_editor_play() {
  1086. debugger->start();
  1087. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1088. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1089. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false );
  1090. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1091. //debugger_gui->start_listening(Globals::get_singleton()->get("debug/debug_port"));
  1092. }
  1093. void ScriptEditor::_editor_pause() {
  1094. }
  1095. void ScriptEditor::_editor_stop() {
  1096. debugger->stop();
  1097. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1098. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1099. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  1100. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1101. for(int i=0;i<tab_container->get_child_count();i++) {
  1102. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1103. if (!se) {
  1104. continue;
  1105. }
  1106. se->set_debugger_active(false);
  1107. }
  1108. }
  1109. void ScriptEditor::_add_callback(Object *p_obj, const String& p_function, const StringArray& p_args) {
  1110. //print_line("add callback! hohoho"); kinda sad to remove this
  1111. ERR_FAIL_COND(!p_obj);
  1112. Ref<Script> script = p_obj->get_script();
  1113. ERR_FAIL_COND( !script.is_valid() );
  1114. editor->push_item(script.ptr());
  1115. for(int i=0;i<tab_container->get_child_count();i++) {
  1116. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1117. if (!se)
  1118. continue;
  1119. if (se->get_edited_script()!=script)
  1120. continue;
  1121. se->add_callback(p_function,p_args);
  1122. _go_to_tab(i);
  1123. script_list->select( script_list->find_metadata(i) );
  1124. break;
  1125. }
  1126. }
  1127. void ScriptEditor::_save_layout() {
  1128. if (restoring_layout) {
  1129. return;
  1130. }
  1131. editor->save_layout();
  1132. }
  1133. void ScriptEditor::_editor_settings_changed() {
  1134. trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/trim_trailing_whitespace_on_save");
  1135. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  1136. if (autosave_time>0) {
  1137. autosave_timer->set_wait_time(autosave_time);
  1138. autosave_timer->start();
  1139. } else {
  1140. autosave_timer->stop();
  1141. }
  1142. if (current_theme == "") {
  1143. current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
  1144. } else if (current_theme != EditorSettings::get_singleton()->get("text_editor/color_theme")) {
  1145. current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
  1146. EditorSettings::get_singleton()->load_text_editor_theme();
  1147. }
  1148. for(int i=0;i<tab_container->get_child_count();i++) {
  1149. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1150. if (!se)
  1151. continue;
  1152. se->update_settings();
  1153. }
  1154. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save",true));
  1155. }
  1156. void ScriptEditor::_autosave_scripts() {
  1157. save_all_scripts();
  1158. }
  1159. void ScriptEditor::_tree_changed() {
  1160. if (waiting_update_names)
  1161. return;
  1162. waiting_update_names=true;
  1163. call_deferred("_update_script_names");
  1164. }
  1165. void ScriptEditor::_script_split_dragged(float) {
  1166. _save_layout();
  1167. }
  1168. void ScriptEditor::_unhandled_input(const InputEvent& p_event) {
  1169. if (p_event.key.pressed || !is_visible()) return;
  1170. if (ED_IS_SHORTCUT("script_editor/next_script", p_event)) {
  1171. int next_tab = script_list->get_current() + 1;
  1172. next_tab %= script_list->get_item_count();
  1173. _go_to_tab(script_list->get_item_metadata(next_tab));
  1174. _update_script_names();
  1175. }
  1176. if (ED_IS_SHORTCUT("script_editor/prev_script", p_event)) {
  1177. int next_tab = script_list->get_current() - 1;
  1178. next_tab = next_tab >= 0 ? next_tab : script_list->get_item_count() - 1;
  1179. _go_to_tab(script_list->get_item_metadata(next_tab));
  1180. _update_script_names();
  1181. }
  1182. }
  1183. void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
  1184. if (!bool(EDITOR_DEF("text_editor/restore_scripts_on_load",true))) {
  1185. return;
  1186. }
  1187. if (!p_layout->has_section_key("ScriptEditor","open_scripts") && !p_layout->has_section_key("ScriptEditor","open_help"))
  1188. return;
  1189. Array scripts = p_layout->get_value("ScriptEditor","open_scripts");
  1190. Array helps;
  1191. if (p_layout->has_section_key("ScriptEditor","open_help"))
  1192. helps=p_layout->get_value("ScriptEditor","open_help");
  1193. restoring_layout=true;
  1194. for(int i=0;i<scripts.size();i++) {
  1195. String path = scripts[i];
  1196. if (!FileAccess::exists(path))
  1197. continue;
  1198. Ref<Script> scr = ResourceLoader::load(path);
  1199. if (scr.is_valid()) {
  1200. edit(scr);
  1201. }
  1202. }
  1203. for(int i=0;i<helps.size();i++) {
  1204. String path = helps[i];
  1205. _help_class_open(path);
  1206. }
  1207. for(int i=0;i<tab_container->get_child_count();i++) {
  1208. tab_container->get_child(i)->set_meta("__editor_pass",Variant());
  1209. }
  1210. if (p_layout->has_section_key("ScriptEditor","split_offset")) {
  1211. script_split->set_split_offset(p_layout->get_value("ScriptEditor","split_offset"));
  1212. }
  1213. restoring_layout=false;
  1214. _update_script_names();
  1215. }
  1216. void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
  1217. Array scripts;
  1218. Array helps;
  1219. for(int i=0;i<tab_container->get_child_count();i++) {
  1220. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1221. if (se) {
  1222. String path = se->get_edited_script()->get_path();
  1223. if (!path.is_resource_file())
  1224. continue;
  1225. scripts.push_back(path);
  1226. }
  1227. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1228. if (eh) {
  1229. helps.push_back(eh->get_class_name());
  1230. }
  1231. }
  1232. p_layout->set_value("ScriptEditor","open_scripts",scripts);
  1233. p_layout->set_value("ScriptEditor","open_help",helps);
  1234. p_layout->set_value("ScriptEditor","split_offset",script_split->get_split_offset());
  1235. }
  1236. void ScriptEditor::_help_class_open(const String& p_class) {
  1237. for(int i=0;i<tab_container->get_child_count();i++) {
  1238. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1239. if (eh && eh->get_class_name()==p_class) {
  1240. _go_to_tab(i);
  1241. _update_script_names();
  1242. return;
  1243. }
  1244. }
  1245. EditorHelp * eh = memnew( EditorHelp );
  1246. eh->set_name(p_class);
  1247. tab_container->add_child(eh);
  1248. _go_to_tab(tab_container->get_tab_count()-1);
  1249. eh->go_to_class(p_class,0);
  1250. eh->connect("go_to_help",this,"_help_class_goto");
  1251. _update_script_names();
  1252. _save_layout();
  1253. }
  1254. void ScriptEditor::_help_class_goto(const String& p_desc) {
  1255. String cname=p_desc.get_slice(":",1);
  1256. for(int i=0;i<tab_container->get_child_count();i++) {
  1257. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1258. if (eh && eh->get_class_name()==cname) {
  1259. _go_to_tab(i);
  1260. eh->go_to_help(p_desc);
  1261. _update_script_names();
  1262. return;
  1263. }
  1264. }
  1265. EditorHelp * eh = memnew( EditorHelp );
  1266. eh->set_name(cname);
  1267. tab_container->add_child(eh);
  1268. _go_to_tab(tab_container->get_tab_count()-1);
  1269. eh->go_to_help(p_desc);
  1270. eh->connect("go_to_help",this,"_help_class_goto");
  1271. _update_script_names();
  1272. _save_layout();
  1273. }
  1274. void ScriptEditor::_update_selected_editor_menu() {
  1275. for(int i=0;i<tab_container->get_child_count();i++) {
  1276. bool current = tab_container->get_current_tab() == i;
  1277. ScriptEditorBase *se = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1278. if (se && se->get_edit_menu()) {
  1279. if (current)
  1280. se->get_edit_menu()->show();
  1281. else
  1282. se->get_edit_menu()->hide();
  1283. }
  1284. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1285. if (eh) {
  1286. if (current)
  1287. script_search_menu->show();
  1288. else
  1289. script_search_menu->hide();
  1290. }
  1291. }
  1292. }
  1293. void ScriptEditor::_update_history_pos(int p_new_pos) {
  1294. Node *n = tab_container->get_current_tab_control();
  1295. if (n->cast_to<ScriptEditorBase>()) {
  1296. history[history_pos].state=n->cast_to<ScriptEditorBase>()->get_edit_state();
  1297. }
  1298. if (n->cast_to<EditorHelp>()) {
  1299. history[history_pos].state=n->cast_to<EditorHelp>()->get_scroll();
  1300. }
  1301. history_pos=p_new_pos;
  1302. tab_container->set_current_tab(history[history_pos].control->get_index());
  1303. n = history[history_pos].control;
  1304. if (n->cast_to<ScriptEditorBase>()) {
  1305. n->cast_to<ScriptEditorBase>()->set_edit_state(history[history_pos].state);
  1306. n->cast_to<ScriptEditorBase>()->ensure_focus();
  1307. }
  1308. if (n->cast_to<EditorHelp>()) {
  1309. n->cast_to<EditorHelp>()->set_scroll(history[history_pos].state);
  1310. n->cast_to<EditorHelp>()->set_focused();
  1311. }
  1312. n->set_meta("__editor_pass",++edit_pass);
  1313. _update_script_names();
  1314. _update_history_arrows();
  1315. _update_selected_editor_menu();
  1316. }
  1317. void ScriptEditor::_history_forward() {
  1318. if (history_pos<history.size()-1) {
  1319. _update_history_pos(history_pos+1);
  1320. }
  1321. }
  1322. void ScriptEditor::_history_back(){
  1323. if (history_pos>0) {
  1324. _update_history_pos(history_pos-1);
  1325. }
  1326. }
  1327. void ScriptEditor::set_scene_root_script( Ref<Script> p_script ) {
  1328. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
  1329. if (bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
  1330. return;
  1331. if (open_dominant && p_script.is_valid() && _can_open_in_editor(p_script.ptr())) {
  1332. edit(p_script);
  1333. }
  1334. }
  1335. bool ScriptEditor::script_go_to_method(Ref<Script> p_script, const String& p_method) {
  1336. for (int i=0;i<tab_container->get_child_count();i++) {
  1337. ScriptEditorBase *current = tab_container->get_child(i)->cast_to<ScriptEditorBase>();
  1338. if (current && current->get_edited_script()==p_script) {
  1339. if (current->goto_method(p_method)) {
  1340. edit(p_script);
  1341. return true;
  1342. }
  1343. break;
  1344. }
  1345. }
  1346. return false;
  1347. }
  1348. void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) {
  1349. auto_reload_running_scripts=p_enabled;
  1350. }
  1351. void ScriptEditor::_help_search(String p_text) {
  1352. help_search_dialog->popup(p_text);
  1353. }
  1354. void ScriptEditor::_open_script_request(const String& p_path) {
  1355. Ref<Script> script = ResourceLoader::load(p_path);
  1356. if (script.is_valid()) {
  1357. script_editor->edit(script,false);
  1358. }
  1359. }
  1360. int ScriptEditor::script_editor_func_count=0;
  1361. CreateScriptEditorFunc ScriptEditor::script_editor_funcs[ScriptEditor::SCRIPT_EDITOR_FUNC_MAX];
  1362. void ScriptEditor::register_create_script_editor_function(CreateScriptEditorFunc p_func) {
  1363. ERR_FAIL_COND(script_editor_func_count==SCRIPT_EDITOR_FUNC_MAX);
  1364. script_editor_funcs[script_editor_func_count++]=p_func;
  1365. }
  1366. void ScriptEditor::_bind_methods() {
  1367. ObjectTypeDB::bind_method("_file_dialog_action",&ScriptEditor::_file_dialog_action);
  1368. ObjectTypeDB::bind_method("_tab_changed",&ScriptEditor::_tab_changed);
  1369. ObjectTypeDB::bind_method("_menu_option",&ScriptEditor::_menu_option);
  1370. ObjectTypeDB::bind_method("_close_current_tab",&ScriptEditor::_close_current_tab);
  1371. ObjectTypeDB::bind_method("_close_docs_tab", &ScriptEditor::_close_docs_tab);
  1372. ObjectTypeDB::bind_method("_close_all_tabs", &ScriptEditor::_close_all_tabs);
  1373. ObjectTypeDB::bind_method("_editor_play",&ScriptEditor::_editor_play);
  1374. ObjectTypeDB::bind_method("_editor_pause",&ScriptEditor::_editor_pause);
  1375. ObjectTypeDB::bind_method("_editor_stop",&ScriptEditor::_editor_stop);
  1376. ObjectTypeDB::bind_method("_add_callback",&ScriptEditor::_add_callback);
  1377. ObjectTypeDB::bind_method("_reload_scripts",&ScriptEditor::_reload_scripts);
  1378. ObjectTypeDB::bind_method("_resave_scripts",&ScriptEditor::_resave_scripts);
  1379. ObjectTypeDB::bind_method("_res_saved_callback",&ScriptEditor::_res_saved_callback);
  1380. ObjectTypeDB::bind_method("_goto_script_line",&ScriptEditor::_goto_script_line);
  1381. ObjectTypeDB::bind_method("_goto_script_line2",&ScriptEditor::_goto_script_line2);
  1382. ObjectTypeDB::bind_method("_help_search",&ScriptEditor::_help_search);
  1383. ObjectTypeDB::bind_method("_breaked",&ScriptEditor::_breaked);
  1384. ObjectTypeDB::bind_method("_show_debugger",&ScriptEditor::_show_debugger);
  1385. ObjectTypeDB::bind_method("_get_debug_tooltip",&ScriptEditor::_get_debug_tooltip);
  1386. ObjectTypeDB::bind_method("_autosave_scripts",&ScriptEditor::_autosave_scripts);
  1387. ObjectTypeDB::bind_method("_editor_settings_changed",&ScriptEditor::_editor_settings_changed);
  1388. ObjectTypeDB::bind_method("_update_script_names",&ScriptEditor::_update_script_names);
  1389. ObjectTypeDB::bind_method("_tree_changed",&ScriptEditor::_tree_changed);
  1390. ObjectTypeDB::bind_method("_script_selected",&ScriptEditor::_script_selected);
  1391. ObjectTypeDB::bind_method("_script_created",&ScriptEditor::_script_created);
  1392. ObjectTypeDB::bind_method("_script_split_dragged",&ScriptEditor::_script_split_dragged);
  1393. ObjectTypeDB::bind_method("_help_class_open",&ScriptEditor::_help_class_open);
  1394. ObjectTypeDB::bind_method("_help_class_goto",&ScriptEditor::_help_class_goto);
  1395. ObjectTypeDB::bind_method("_request_help",&ScriptEditor::_help_class_open);
  1396. ObjectTypeDB::bind_method("_history_forward",&ScriptEditor::_history_forward);
  1397. ObjectTypeDB::bind_method("_history_back",&ScriptEditor::_history_back);
  1398. ObjectTypeDB::bind_method("_live_auto_reload_running_scripts",&ScriptEditor::_live_auto_reload_running_scripts);
  1399. ObjectTypeDB::bind_method("_unhandled_input",&ScriptEditor::_unhandled_input);
  1400. }
  1401. ScriptEditor::ScriptEditor(EditorNode *p_editor) {
  1402. current_theme = "";
  1403. completion_cache = memnew( EditorScriptCodeCompletionCache );
  1404. restoring_layout=false;
  1405. waiting_update_names=false;
  1406. pending_auto_reload=false;
  1407. auto_reload_running_scripts=false;
  1408. editor=p_editor;
  1409. menu_hb = memnew( HBoxContainer );
  1410. add_child(menu_hb);
  1411. script_split = memnew( HSplitContainer );
  1412. add_child(script_split);
  1413. script_split->set_v_size_flags(SIZE_EXPAND_FILL);
  1414. script_list = memnew( ItemList );
  1415. script_split->add_child(script_list);
  1416. script_list->set_custom_minimum_size(Size2(0,0));
  1417. script_split->set_split_offset(140);
  1418. tab_container = memnew( TabContainer );
  1419. tab_container->set_tabs_visible(false);
  1420. script_split->add_child(tab_container);
  1421. tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
  1422. ED_SHORTCUT("script_editor/next_script", TTR("Next script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_GREATER);
  1423. ED_SHORTCUT("script_editor/prev_script", TTR("Previous script"), KEY_MASK_CMD | KEY_LESS);
  1424. set_process_unhandled_input(true);
  1425. file_menu = memnew( MenuButton );
  1426. menu_hb->add_child(file_menu);
  1427. file_menu->set_text(TTR("File"));
  1428. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTR("New")), FILE_NEW);
  1429. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open", TTR("Open")), FILE_OPEN);
  1430. file_menu->get_popup()->add_separator();
  1431. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save", TTR("Save"), KEY_MASK_ALT|KEY_MASK_CMD|KEY_S), FILE_SAVE);
  1432. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as", TTR("Save As..")), FILE_SAVE_AS);
  1433. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_all", TTR("Save All"), KEY_MASK_CMD|KEY_MASK_SHIFT|KEY_MASK_ALT|KEY_S), FILE_SAVE_ALL);
  1434. file_menu->get_popup()->add_separator();
  1435. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_script_soft", TTR("Soft Reload Script"), KEY_MASK_CMD|KEY_MASK_SHIFT|KEY_R), FILE_TOOL_RELOAD_SOFT);
  1436. file_menu->get_popup()->add_separator();
  1437. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_previous", TTR("History Prev"), KEY_MASK_CTRL|KEY_MASK_ALT|KEY_LEFT), WINDOW_PREV);
  1438. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_next", TTR("History Next"), KEY_MASK_CTRL|KEY_MASK_ALT|KEY_RIGHT), WINDOW_NEXT);
  1439. file_menu->get_popup()->add_separator();
  1440. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/import_theme", TTR("Import Theme")), FILE_IMPORT_THEME);
  1441. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_theme", TTR("Reload Theme")), FILE_RELOAD_THEME);
  1442. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme", TTR("Save Theme")), FILE_SAVE_THEME);
  1443. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as", TTR("Save Theme As")), FILE_SAVE_THEME_AS);
  1444. file_menu->get_popup()->add_separator();
  1445. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_docs", TTR("Close Docs")), CLOSE_DOCS);
  1446. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file", TTR("Close"), KEY_MASK_CMD | KEY_W), FILE_CLOSE);
  1447. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_all", TTR("Close All")), CLOSE_ALL);
  1448. file_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1449. script_search_menu = memnew( MenuButton );
  1450. menu_hb->add_child(script_search_menu);
  1451. script_search_menu->set_text(TTR("Search"));
  1452. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD|KEY_F), HELP_SEARCH_FIND);
  1453. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), HELP_SEARCH_FIND_NEXT);
  1454. script_search_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1455. script_search_menu->hide();
  1456. debug_menu = memnew( MenuButton );
  1457. menu_hb->add_child(debug_menu);
  1458. debug_menu->set_text(TTR("Debug"));
  1459. debug_menu->get_popup()->add_separator();
  1460. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_over", TTR("Step Over"), KEY_F10), DEBUG_NEXT);
  1461. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_into", TTR("Step Into"), KEY_F11), DEBUG_STEP);
  1462. debug_menu->get_popup()->add_separator();
  1463. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/break", TTR("Break")), DEBUG_BREAK);
  1464. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/continue", TTR("Continue")), DEBUG_CONTINUE);
  1465. debug_menu->get_popup()->add_separator();
  1466. //debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
  1467. debug_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("debugger/keep_debugger_open", TTR("Keep Debugger Open")), DEBUG_SHOW_KEEP_OPEN);
  1468. debug_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1469. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1470. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1471. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  1472. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1473. #if 0
  1474. window_menu = memnew( MenuButton );
  1475. menu_hb->add_child(window_menu);
  1476. window_menu->set_text(TTR("Window"));
  1477. window_menu->get_popup()->add_item(TTR("Close"),WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
  1478. window_menu->get_popup()->add_separator();
  1479. window_menu->get_popup()->add_item(TTR("Move Left"),WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
  1480. window_menu->get_popup()->add_item(TTR("Move Right"),WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
  1481. window_menu->get_popup()->add_separator();
  1482. window_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1483. #endif
  1484. menu_hb->add_spacer();
  1485. script_icon = memnew( TextureFrame );
  1486. menu_hb->add_child(script_icon);
  1487. script_name_label = memnew( Label );
  1488. menu_hb->add_child(script_name_label);
  1489. script_icon->hide();
  1490. script_name_label->hide();
  1491. menu_hb->add_spacer();
  1492. site_search = memnew( ToolButton );
  1493. site_search->set_text(TTR("Tutorials"));
  1494. site_search->connect("pressed",this,"_menu_option",varray(SEARCH_WEBSITE));
  1495. menu_hb->add_child(site_search);
  1496. site_search->set_tooltip(TTR("Open https://godotengine.org at tutorials section."));
  1497. class_search = memnew( ToolButton );
  1498. class_search->set_text(TTR("Classes"));
  1499. class_search->connect("pressed",this,"_menu_option",varray(SEARCH_CLASSES));
  1500. menu_hb->add_child(class_search);
  1501. class_search->set_tooltip(TTR("Search the class hierarchy."));
  1502. help_search = memnew( ToolButton );
  1503. help_search->set_text(TTR("Search Help"));
  1504. help_search->connect("pressed",this,"_menu_option",varray(SEARCH_HELP));
  1505. menu_hb->add_child(help_search);
  1506. help_search->set_tooltip(TTR("Search the reference documentation."));
  1507. menu_hb->add_child( memnew( VSeparator) );
  1508. script_back = memnew( ToolButton );
  1509. script_back->connect("pressed",this,"_history_back");
  1510. menu_hb->add_child(script_back);
  1511. script_back->set_disabled(true);
  1512. script_back->set_tooltip(TTR("Go to previous edited document."));
  1513. script_forward = memnew( ToolButton );
  1514. script_forward->connect("pressed",this,"_history_forward");
  1515. menu_hb->add_child(script_forward);
  1516. script_forward->set_disabled(true);
  1517. script_forward->set_tooltip(TTR("Go to next edited document."));
  1518. tab_container->connect("tab_changed", this,"_tab_changed");
  1519. erase_tab_confirm = memnew( ConfirmationDialog );
  1520. add_child(erase_tab_confirm);
  1521. erase_tab_confirm->connect("confirmed", this,"_close_current_tab");
  1522. script_create_dialog = memnew(ScriptCreateDialog);
  1523. script_create_dialog->set_title(TTR("Create Script"));
  1524. add_child(script_create_dialog);
  1525. script_create_dialog->connect("script_created", this, "_script_created");
  1526. file_dialog_option = -1;
  1527. file_dialog = memnew( EditorFileDialog );
  1528. add_child(file_dialog);
  1529. file_dialog->connect("file_selected", this,"_file_dialog_action");
  1530. debugger = memnew( ScriptEditorDebugger(editor) );
  1531. debugger->connect("goto_script_line",this,"_goto_script_line");
  1532. debugger->connect("show_debugger",this,"_show_debugger");
  1533. disk_changed = memnew( ConfirmationDialog );
  1534. {
  1535. VBoxContainer *vbc = memnew( VBoxContainer );
  1536. disk_changed->add_child(vbc);
  1537. disk_changed->set_child_rect(vbc);
  1538. Label *dl = memnew( Label );
  1539. dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?:"));
  1540. vbc->add_child(dl);
  1541. disk_changed_list = memnew( Tree );
  1542. vbc->add_child(disk_changed_list);
  1543. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  1544. disk_changed->connect("confirmed",this,"_reload_scripts");
  1545. disk_changed->get_ok()->set_text(TTR("Reload"));
  1546. disk_changed->add_button(TTR("Resave"),!OS::get_singleton()->get_swap_ok_cancel(),"resave");
  1547. disk_changed->connect("custom_action",this,"_resave_scripts");
  1548. }
  1549. add_child(disk_changed);
  1550. script_editor=this;
  1551. Button *db = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Debugger"),debugger);
  1552. debugger->set_tool_button(db);
  1553. debugger->connect("breaked",this,"_breaked");
  1554. autosave_timer = memnew( Timer );
  1555. autosave_timer->set_one_shot(false);
  1556. add_child(autosave_timer);
  1557. grab_focus_block=false;
  1558. help_search_dialog = memnew( EditorHelpSearch );
  1559. add_child(help_search_dialog);
  1560. help_search_dialog->connect("go_to_help",this,"_help_class_goto");
  1561. help_index = memnew( EditorHelpIndex );
  1562. add_child(help_index);
  1563. help_index->connect("open_class",this,"_help_class_open");
  1564. history_pos=-1;
  1565. // debugger_gui->hide();
  1566. edit_pass=0;
  1567. trim_trailing_whitespace_on_save = false;
  1568. ScriptServer::edit_request_func=_open_script_request;
  1569. }
  1570. ScriptEditor::~ScriptEditor() {
  1571. memdelete(completion_cache);
  1572. }
  1573. void ScriptEditorPlugin::edit(Object *p_object) {
  1574. if (!p_object->cast_to<Script>())
  1575. return;
  1576. script_editor->edit(p_object->cast_to<Script>());
  1577. }
  1578. bool ScriptEditorPlugin::handles(Object *p_object) const {
  1579. if (p_object->cast_to<Script>()) {
  1580. bool valid = _can_open_in_editor(p_object->cast_to<Script>());
  1581. if (!valid) { //user tried to open it by clicking
  1582. EditorNode::get_singleton()->show_warning(TTR("Built-in scripts can only be edited when the scene they belong to is loaded"));
  1583. }
  1584. return valid;
  1585. }
  1586. return p_object->is_type("Script");
  1587. }
  1588. void ScriptEditorPlugin::make_visible(bool p_visible) {
  1589. if (p_visible) {
  1590. script_editor->show();
  1591. script_editor->set_process(true);
  1592. script_editor->ensure_select_current();
  1593. } else {
  1594. script_editor->hide();
  1595. script_editor->set_process(false);
  1596. }
  1597. }
  1598. void ScriptEditorPlugin::selected_notify() {
  1599. script_editor->ensure_select_current();
  1600. }
  1601. Dictionary ScriptEditorPlugin::get_state() const {
  1602. return script_editor->get_state();
  1603. }
  1604. void ScriptEditorPlugin::set_state(const Dictionary& p_state) {
  1605. script_editor->set_state(p_state);
  1606. }
  1607. void ScriptEditorPlugin::clear() {
  1608. script_editor->clear();
  1609. }
  1610. void ScriptEditorPlugin::save_external_data() {
  1611. script_editor->save_all_scripts();
  1612. }
  1613. void ScriptEditorPlugin::apply_changes() {
  1614. script_editor->apply_scripts();
  1615. }
  1616. void ScriptEditorPlugin::restore_global_state() {
  1617. }
  1618. void ScriptEditorPlugin::save_global_state() {
  1619. }
  1620. void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  1621. script_editor->set_window_layout(p_layout);
  1622. }
  1623. void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout){
  1624. script_editor->get_window_layout(p_layout);
  1625. }
  1626. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  1627. return script_editor->get_breakpoints(p_breakpoints);
  1628. }
  1629. void ScriptEditorPlugin::edited_scene_changed() {
  1630. script_editor->edited_scene_changed();
  1631. }
  1632. ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
  1633. editor=p_node;
  1634. script_editor = memnew( ScriptEditor(p_node) );
  1635. editor->get_viewport()->add_child(script_editor);
  1636. script_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1637. script_editor->hide();
  1638. EDITOR_DEF("text_editor/auto_reload_scripts_on_external_change",true);
  1639. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save",true));
  1640. EDITOR_DEF("text_editor/open_dominant_script_on_scene_change",true);
  1641. EDITOR_DEF("external_editor/use_external_editor",false);
  1642. EDITOR_DEF("external_editor/exec_path","");
  1643. EDITOR_DEF("text_editor/script_temperature_enabled",true);
  1644. EDITOR_DEF("text_editor/highlight_current_script", true);
  1645. EDITOR_DEF("text_editor/script_temperature_history_size",15);
  1646. EDITOR_DEF("text_editor/script_temperature_hot_color",Color(1,0,0,0.3));
  1647. EDITOR_DEF("text_editor/script_temperature_cold_color",Color(0,0,1,0.3));
  1648. EDITOR_DEF("text_editor/current_script_background_color",Color(0.81,0.81,0.14,0.63));
  1649. EDITOR_DEF("text_editor/group_help_pages",true);
  1650. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"external_editor/exec_path",PROPERTY_HINT_GLOBAL_FILE));
  1651. EDITOR_DEF("external_editor/exec_flags","");
  1652. }
  1653. ScriptEditorPlugin::~ScriptEditorPlugin()
  1654. {
  1655. }