script_editor_plugin.cpp 68 KB

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