script_editor_plugin.cpp 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965
  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 "globals.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/os.h"
  41. #include "scene/main/viewport.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 && (p_ie.key.scancode == KEY_UP ||
  128. p_ie.key.scancode == KEY_DOWN ||
  129. p_ie.key.scancode == KEY_PAGEUP ||
  130. p_ie.key.scancode == KEY_PAGEDOWN)) {
  131. search_options->call("_input_event", p_ie);
  132. search_box->accept_event();
  133. }
  134. }
  135. void ScriptEditorQuickOpen::_update_search() {
  136. search_options->clear();
  137. TreeItem *root = search_options->create_item();
  138. for (int i = 0; i < functions.size(); i++) {
  139. String file = functions[i];
  140. if ((search_box->get_text() == "" || file.findn(search_box->get_text()) != -1)) {
  141. TreeItem *ti = search_options->create_item(root);
  142. ti->set_text(0, file);
  143. if (root->get_children() == ti)
  144. ti->select(0);
  145. }
  146. }
  147. get_ok()->set_disabled(root->get_children() == NULL);
  148. }
  149. void ScriptEditorQuickOpen::_confirmed() {
  150. TreeItem *ti = search_options->get_selected();
  151. if (!ti)
  152. return;
  153. int line = ti->get_text(0).get_slice(":", 1).to_int();
  154. emit_signal("goto_line", line - 1);
  155. hide();
  156. }
  157. void ScriptEditorQuickOpen::_notification(int p_what) {
  158. if (p_what == NOTIFICATION_ENTER_TREE) {
  159. connect("confirmed", this, "_confirmed");
  160. }
  161. }
  162. void ScriptEditorQuickOpen::_bind_methods() {
  163. ObjectTypeDB::bind_method(_MD("_text_changed"), &ScriptEditorQuickOpen::_text_changed);
  164. ObjectTypeDB::bind_method(_MD("_confirmed"), &ScriptEditorQuickOpen::_confirmed);
  165. ObjectTypeDB::bind_method(_MD("_sbox_input"), &ScriptEditorQuickOpen::_sbox_input);
  166. ADD_SIGNAL(MethodInfo("goto_line", PropertyInfo(Variant::INT, "line")));
  167. }
  168. ScriptEditorQuickOpen::ScriptEditorQuickOpen() {
  169. VBoxContainer *vbc = memnew(VBoxContainer);
  170. add_child(vbc);
  171. set_child_rect(vbc);
  172. search_box = memnew(LineEdit);
  173. vbc->add_margin_child(TTR("Search:"), search_box);
  174. search_box->connect("text_changed", this, "_text_changed");
  175. search_box->connect("input_event", this, "_sbox_input");
  176. search_options = memnew(Tree);
  177. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  178. get_ok()->set_text(TTR("Open"));
  179. get_ok()->set_disabled(true);
  180. register_text_enter(search_box);
  181. set_hide_on_ok(false);
  182. search_options->connect("item_activated", this, "_confirmed");
  183. search_options->set_hide_root(true);
  184. }
  185. /////////////////////////////////
  186. ScriptEditor *ScriptEditor::script_editor = NULL;
  187. Vector<String> ScriptTextEditor::get_functions() {
  188. String errortxt;
  189. int line = -1, col;
  190. TextEdit *te = get_text_edit();
  191. String text = te->get_text();
  192. List<String> fnc;
  193. if (script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
  194. //if valid rewrite functions to latest
  195. functions.clear();
  196. for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
  197. functions.push_back(E->get());
  198. }
  199. }
  200. return functions;
  201. }
  202. void ScriptTextEditor::apply_code() {
  203. if (script.is_null())
  204. return;
  205. // print_line("applying code");
  206. script->set_source_code(get_text_edit()->get_text());
  207. script->update_exports();
  208. }
  209. Ref<Script> ScriptTextEditor::get_edited_script() const {
  210. return script;
  211. }
  212. void ScriptTextEditor::_load_theme_settings() {
  213. get_text_edit()->clear_colors();
  214. /* keyword color */
  215. get_text_edit()->set_custom_bg_color(EDITOR_DEF("text_editor/background_color", Color(0, 0, 0, 0)));
  216. get_text_edit()->add_color_override("completion_background_color", EDITOR_DEF("text_editor/completion_background_color", Color(0, 0, 0, 0)));
  217. get_text_edit()->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/completion_selected_color", Color::html("434244")));
  218. get_text_edit()->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/completion_existing_color", Color::html("21dfdfdf")));
  219. get_text_edit()->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/completion_scroll_color", Color::html("ffffff")));
  220. get_text_edit()->add_color_override("completion_font_color", EDITOR_DEF("text_editor/completion_font_color", Color::html("aaaaaa")));
  221. get_text_edit()->add_color_override("font_color", EDITOR_DEF("text_editor/text_color", Color(0, 0, 0)));
  222. get_text_edit()->add_color_override("line_number_color", EDITOR_DEF("text_editor/line_number_color", Color(0, 0, 0)));
  223. get_text_edit()->add_color_override("caret_color", EDITOR_DEF("text_editor/caret_color", Color(0, 0, 0)));
  224. get_text_edit()->add_color_override("caret_background_color", EDITOR_DEF("text_editor/caret_background_color", Color(0, 0, 0)));
  225. get_text_edit()->add_color_override("font_selected_color", EDITOR_DEF("text_editor/text_selected_color", Color(1, 1, 1)));
  226. get_text_edit()->add_color_override("selection_color", EDITOR_DEF("text_editor/selection_color", Color(0.2, 0.2, 1)));
  227. get_text_edit()->add_color_override("brace_mismatch_color", EDITOR_DEF("text_editor/brace_mismatch_color", Color(1, 0.2, 0.2)));
  228. get_text_edit()->add_color_override("current_line_color", EDITOR_DEF("text_editor/current_line_color", Color(0.3, 0.5, 0.8, 0.15)));
  229. get_text_edit()->add_color_override("word_highlighted_color", EDITOR_DEF("text_editor/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15)));
  230. get_text_edit()->add_color_override("number_color", EDITOR_DEF("text_editor/number_color", Color(0.9, 0.6, 0.0, 2)));
  231. get_text_edit()->add_color_override("function_color", EDITOR_DEF("text_editor/function_color", Color(0.4, 0.6, 0.8)));
  232. get_text_edit()->add_color_override("member_variable_color", EDITOR_DEF("text_editor/member_variable_color", Color(0.9, 0.3, 0.3)));
  233. get_text_edit()->add_color_override("mark_color", EDITOR_DEF("text_editor/mark_color", Color(1.0, 0.4, 0.4, 0.4)));
  234. get_text_edit()->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2)));
  235. get_text_edit()->add_color_override("search_result_color", EDITOR_DEF("text_editor/search_result_color", Color(0.05, 0.25, 0.05, 1)));
  236. get_text_edit()->add_color_override("search_result_border_color", EDITOR_DEF("text_editor/search_result_border_color", Color(0.1, 0.45, 0.1, 1)));
  237. get_text_edit()->add_constant_override("line_spacing", EDITOR_DEF("text_editor/line_spacing", 4));
  238. Color keyword_color = EDITOR_DEF("text_editor/keyword_color", Color(0.5, 0.0, 0.2));
  239. List<String> keywords;
  240. script->get_language()->get_reserved_words(&keywords);
  241. for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
  242. get_text_edit()->add_keyword_color(E->get(), keyword_color);
  243. }
  244. //colorize core types
  245. Color basetype_color = EDITOR_DEF("text_editor/base_type_color", Color(0.3, 0.3, 0.0));
  246. get_text_edit()->add_keyword_color("Vector2", basetype_color);
  247. get_text_edit()->add_keyword_color("Vector3", basetype_color);
  248. get_text_edit()->add_keyword_color("Plane", basetype_color);
  249. get_text_edit()->add_keyword_color("Quat", basetype_color);
  250. get_text_edit()->add_keyword_color("AABB", basetype_color);
  251. get_text_edit()->add_keyword_color("Matrix3", basetype_color);
  252. get_text_edit()->add_keyword_color("Transform", basetype_color);
  253. get_text_edit()->add_keyword_color("Color", basetype_color);
  254. get_text_edit()->add_keyword_color("Image", basetype_color);
  255. get_text_edit()->add_keyword_color("InputEvent", basetype_color);
  256. get_text_edit()->add_keyword_color("Rect2", basetype_color);
  257. get_text_edit()->add_keyword_color("NodePath", basetype_color);
  258. //colorize engine types
  259. Color type_color = EDITOR_DEF("text_editor/engine_type_color", Color(0.0, 0.2, 0.4));
  260. List<StringName> types;
  261. ObjectTypeDB::get_type_list(&types);
  262. for (List<StringName>::Element *E = types.front(); E; E = E->next()) {
  263. String n = E->get();
  264. if (n.begins_with("_"))
  265. n = n.substr(1, n.length());
  266. get_text_edit()->add_keyword_color(n, type_color);
  267. }
  268. //colorize comments
  269. Color comment_color = EDITOR_DEF("text_editor/comment_color", Color::hex(0x797e7eff));
  270. List<String> comments;
  271. script->get_language()->get_comment_delimiters(&comments);
  272. for (List<String>::Element *E = comments.front(); E; E = E->next()) {
  273. String comment = E->get();
  274. String beg = comment.get_slice(" ", 0);
  275. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  276. get_text_edit()->add_color_region(beg, end, comment_color, end == "");
  277. }
  278. //colorize strings
  279. Color string_color = EDITOR_DEF("text_editor/string_color", Color::hex(0x6b6f00ff));
  280. List<String> strings;
  281. script->get_language()->get_string_delimiters(&strings);
  282. for (List<String>::Element *E = strings.front(); E; E = E->next()) {
  283. String string = E->get();
  284. String beg = string.get_slice(" ", 0);
  285. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  286. get_text_edit()->add_color_region(beg, end, string_color, end == "");
  287. }
  288. //colorize symbols
  289. Color symbol_color = EDITOR_DEF("text_editor/symbol_color", Color::hex(0x005291ff));
  290. get_text_edit()->set_symbol_color(symbol_color);
  291. }
  292. void ScriptTextEditor::reload_text() {
  293. ERR_FAIL_COND(script.is_null());
  294. TextEdit *te = get_text_edit();
  295. int column = te->cursor_get_column();
  296. int row = te->cursor_get_line();
  297. int h = te->get_h_scroll();
  298. int v = te->get_v_scroll();
  299. te->set_text(script->get_source_code());
  300. te->clear_undo_history();
  301. te->cursor_set_line(row);
  302. te->cursor_set_column(column);
  303. te->set_h_scroll(h);
  304. te->set_v_scroll(v);
  305. te->tag_saved_version();
  306. _line_col_changed();
  307. }
  308. void ScriptTextEditor::_notification(int p_what) {
  309. if (p_what == NOTIFICATION_READY) {
  310. //emit_signal("name_changed");
  311. }
  312. }
  313. bool ScriptTextEditor::is_unsaved() {
  314. return get_text_edit()->get_version() != get_text_edit()->get_saved_version();
  315. }
  316. String ScriptTextEditor::get_name() {
  317. String name;
  318. if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  319. name = script->get_path().get_file();
  320. if (get_text_edit()->get_version() != get_text_edit()->get_saved_version()) {
  321. name += "(*)";
  322. }
  323. } else if (script->get_name() != "")
  324. name = script->get_name();
  325. else
  326. name = script->get_type() + "(" + itos(script->get_instance_ID()) + ")";
  327. return name;
  328. }
  329. Ref<Texture> ScriptTextEditor::get_icon() {
  330. if (get_parent_control() && get_parent_control()->has_icon(script->get_type(), "EditorIcons")) {
  331. return get_parent_control()->get_icon(script->get_type(), "EditorIcons");
  332. }
  333. return Ref<Texture>();
  334. }
  335. void ScriptTextEditor::set_edited_script(const Ref<Script> &p_script) {
  336. ERR_FAIL_COND(!script.is_null());
  337. script = p_script;
  338. _load_theme_settings();
  339. get_text_edit()->set_text(script->get_source_code());
  340. get_text_edit()->clear_undo_history();
  341. get_text_edit()->tag_saved_version();
  342. emit_signal("name_changed");
  343. _line_col_changed();
  344. }
  345. void ScriptTextEditor::_validate_script() {
  346. String errortxt;
  347. int line = -1, col;
  348. TextEdit *te = get_text_edit();
  349. String text = te->get_text();
  350. List<String> fnc;
  351. if (!script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
  352. String error_text = "error(" + itos(line) + "," + itos(col) + "): " + errortxt;
  353. set_error(error_text);
  354. } else {
  355. set_error("");
  356. line = -1;
  357. if (!script->is_tool()) {
  358. script->set_source_code(text);
  359. script->update_exports();
  360. //script->reload(); //will update all the variables in property editors
  361. }
  362. functions.clear();
  363. for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
  364. functions.push_back(E->get());
  365. }
  366. }
  367. line--;
  368. for (int i = 0; i < te->get_line_count(); i++) {
  369. te->set_line_as_marked(i, line == i);
  370. }
  371. emit_signal("name_changed");
  372. }
  373. static Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  374. if (p_current->get_owner() != p_base && p_base != p_current)
  375. return NULL;
  376. Ref<Script> c = p_current->get_script();
  377. if (c == p_script)
  378. return p_current;
  379. for (int i = 0; i < p_current->get_child_count(); i++) {
  380. Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
  381. if (found)
  382. return found;
  383. }
  384. return NULL;
  385. }
  386. static void _find_changed_scripts_for_external_editor(Node *p_base, Node *p_current, Set<Ref<Script> > &r_scripts) {
  387. if (p_current->get_owner() != p_base && p_base != p_current)
  388. return;
  389. Ref<Script> c = p_current->get_script();
  390. if (c.is_valid())
  391. r_scripts.insert(c);
  392. for (int i = 0; i < p_current->get_child_count(); i++) {
  393. _find_changed_scripts_for_external_editor(p_base, p_current->get_child(i), r_scripts);
  394. }
  395. }
  396. void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
  397. if (!bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
  398. return;
  399. Set<Ref<Script> > scripts;
  400. Node *base = get_tree()->get_edited_scene_root();
  401. if (base) {
  402. _find_changed_scripts_for_external_editor(base, base, scripts);
  403. }
  404. for (Set<Ref<Script> >::Element *E = scripts.front(); E; E = E->next()) {
  405. Ref<Script> script = E->get();
  406. if (p_for_script.is_valid() && p_for_script != script)
  407. continue;
  408. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
  409. continue; //internal script, who cares, though weird
  410. }
  411. uint64_t last_date = script->get_last_modified_time();
  412. uint64_t date = FileAccess::get_modified_time(script->get_path());
  413. if (last_date != date) {
  414. Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_type(), true);
  415. ERR_CONTINUE(!rel_script.is_valid());
  416. script->set_source_code(rel_script->get_source_code());
  417. script->set_last_modified_time(rel_script->get_last_modified_time());
  418. script->update_exports();
  419. }
  420. }
  421. }
  422. void ScriptTextEditor::_code_complete_script(const String &p_code, List<String> *r_options) {
  423. Node *base = get_tree()->get_edited_scene_root();
  424. if (base) {
  425. base = _find_node_for_script(base, base, script);
  426. }
  427. String hint;
  428. Error err = script->get_language()->complete_code(p_code, script->get_path().get_base_dir(), base, r_options, hint);
  429. if (hint != "") {
  430. get_text_edit()->set_code_hint(hint);
  431. }
  432. }
  433. void ScriptTextEditor::_bind_methods() {
  434. ADD_SIGNAL(MethodInfo("name_changed"));
  435. }
  436. ScriptTextEditor::ScriptTextEditor() {
  437. }
  438. /*** SCRIPT EDITOR ******/
  439. String ScriptEditor::_get_debug_tooltip(const String &p_text, Node *_ste) {
  440. ScriptTextEditor *ste = _ste->cast_to<ScriptTextEditor>();
  441. String val = debugger->get_var_value(p_text);
  442. if (val != String()) {
  443. return p_text + ": " + val;
  444. } else {
  445. return String();
  446. }
  447. }
  448. void ScriptEditor::_breaked(bool p_breaked, bool p_can_debug) {
  449. if (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor"))) {
  450. return;
  451. }
  452. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug));
  453. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug));
  454. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked);
  455. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked);
  456. }
  457. void ScriptEditor::_show_debugger(bool p_show) {
  458. // debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), p_show);
  459. }
  460. void ScriptEditor::_script_created(Ref<Script> p_script) {
  461. editor->push_item(p_script.operator->());
  462. }
  463. void ScriptEditor::_trim_trailing_whitespace(TextEdit *tx) {
  464. bool trimed_whitespace = false;
  465. for (int i = 0; i < tx->get_line_count(); i++) {
  466. String line = tx->get_line(i);
  467. if (line.ends_with(" ") || line.ends_with("\t")) {
  468. if (!trimed_whitespace) {
  469. tx->begin_complex_operation();
  470. trimed_whitespace = true;
  471. }
  472. int end = 0;
  473. for (int j = line.length() - 1; j > -1; j--) {
  474. if (line[j] != ' ' && line[j] != '\t') {
  475. end = j + 1;
  476. break;
  477. }
  478. }
  479. tx->set_line(i, line.substr(0, end));
  480. }
  481. }
  482. if (trimed_whitespace) {
  483. tx->end_complex_operation();
  484. tx->update();
  485. }
  486. }
  487. void ScriptEditor::_goto_script_line2(int p_line) {
  488. int selected = tab_container->get_current_tab();
  489. if (selected < 0 || selected >= tab_container->get_child_count())
  490. return;
  491. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  492. if (!current)
  493. return;
  494. current->get_text_edit()->cursor_set_line(p_line);
  495. }
  496. void ScriptEditor::_goto_script_line(REF p_script, int p_line) {
  497. editor->push_item(p_script.ptr());
  498. _goto_script_line2(p_line);
  499. }
  500. void ScriptEditor::_update_history_arrows() {
  501. script_back->set_disabled(history_pos <= 0);
  502. script_forward->set_disabled(history_pos >= history.size() - 1);
  503. }
  504. void ScriptEditor::_go_to_tab(int p_idx) {
  505. Node *cn = tab_container->get_child(p_idx);
  506. if (!cn)
  507. return;
  508. Control *c = cn->cast_to<Control>();
  509. if (!c)
  510. return;
  511. if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) {
  512. Node *n = tab_container->get_current_tab_control();
  513. if (n->cast_to<ScriptTextEditor>()) {
  514. history[history_pos].scroll_pos = n->cast_to<ScriptTextEditor>()->get_text_edit()->get_v_scroll();
  515. history[history_pos].cursor_column = n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_column();
  516. history[history_pos].cursor_row = n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_line();
  517. }
  518. if (n->cast_to<EditorHelp>()) {
  519. history[history_pos].scroll_pos = n->cast_to<EditorHelp>()->get_scroll();
  520. }
  521. }
  522. history.resize(history_pos + 1);
  523. ScriptHistory sh;
  524. sh.control = c;
  525. sh.scroll_pos = 0;
  526. history.push_back(sh);
  527. history_pos++;
  528. tab_container->set_current_tab(p_idx);
  529. c = tab_container->get_current_tab_control();
  530. if (c->cast_to<ScriptTextEditor>()) {
  531. script_name_label->set_text(c->cast_to<ScriptTextEditor>()->get_name());
  532. script_icon->set_texture(c->cast_to<ScriptTextEditor>()->get_icon());
  533. if (is_visible())
  534. c->cast_to<ScriptTextEditor>()->get_text_edit()->grab_focus();
  535. }
  536. if (c->cast_to<EditorHelp>()) {
  537. script_name_label->set_text(c->cast_to<EditorHelp>()->get_class_name());
  538. script_icon->set_texture(get_icon("Help", "EditorIcons"));
  539. if (is_visible())
  540. c->cast_to<EditorHelp>()->set_focused();
  541. }
  542. c->set_meta("__editor_pass", ++edit_pass);
  543. _update_history_arrows();
  544. _update_script_colors();
  545. }
  546. void ScriptEditor::_close_tab(int p_idx) {
  547. int selected = p_idx;
  548. if (selected < 0 || selected >= tab_container->get_child_count())
  549. return;
  550. Node *tselected = tab_container->get_child(selected);
  551. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  552. if (current) {
  553. apply_scripts();
  554. }
  555. //remove from history
  556. history.resize(history_pos + 1);
  557. for (int i = 0; i < history.size(); i++) {
  558. if (history[i].control == tselected) {
  559. history.remove(i);
  560. i--;
  561. history_pos--;
  562. }
  563. }
  564. if (history_pos >= history.size()) {
  565. history_pos = history.size() - 1;
  566. }
  567. int idx = tab_container->get_current_tab();
  568. memdelete(tselected);
  569. if (idx >= tab_container->get_child_count())
  570. idx = tab_container->get_child_count() - 1;
  571. if (idx >= 0) {
  572. if (history_pos >= 0) {
  573. idx = history[history_pos].control->get_index();
  574. }
  575. tab_container->set_current_tab(idx);
  576. //script_list->select(idx);
  577. }
  578. _update_history_arrows();
  579. _update_script_names();
  580. _save_layout();
  581. }
  582. void ScriptEditor::_close_current_tab() {
  583. _close_tab(tab_container->get_current_tab());
  584. }
  585. void ScriptEditor::_close_docs_tab() {
  586. int child_count = tab_container->get_child_count();
  587. for (int i = child_count - 1; i >= 0; i--) {
  588. EditorHelp *ste = tab_container->get_child(i)->cast_to<EditorHelp>();
  589. if (ste) {
  590. _close_tab(i);
  591. }
  592. }
  593. }
  594. void ScriptEditor::_resave_scripts(const String &p_str) {
  595. apply_scripts();
  596. for (int i = 0; i < tab_container->get_child_count(); i++) {
  597. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  598. if (!ste)
  599. continue;
  600. Ref<Script> script = ste->get_edited_script();
  601. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1)
  602. continue; //internal script, who cares
  603. if (trim_trailing_whitespace_on_save) {
  604. _trim_trailing_whitespace(ste->get_text_edit());
  605. }
  606. editor->save_resource(script);
  607. ste->get_text_edit()->tag_saved_version();
  608. }
  609. disk_changed->hide();
  610. }
  611. void ScriptEditor::_reload_scripts() {
  612. for (int i = 0; i < tab_container->get_child_count(); i++) {
  613. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  614. if (!ste) {
  615. continue;
  616. }
  617. Ref<Script> script = ste->get_edited_script();
  618. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
  619. continue; //internal script, who cares
  620. }
  621. uint64_t last_date = script->get_last_modified_time();
  622. uint64_t date = FileAccess::get_modified_time(script->get_path());
  623. //printf("last date: %lli vs date: %lli\n",last_date,date);
  624. if (last_date == date) {
  625. continue;
  626. }
  627. Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_type(), true);
  628. ERR_CONTINUE(!rel_script.is_valid());
  629. script->set_source_code(rel_script->get_source_code());
  630. script->set_last_modified_time(rel_script->get_last_modified_time());
  631. script->reload();
  632. ste->reload_text();
  633. }
  634. disk_changed->hide();
  635. _update_script_names();
  636. }
  637. void ScriptEditor::_res_saved_callback(const Ref<Resource> &p_res) {
  638. for (int i = 0; i < tab_container->get_child_count(); i++) {
  639. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  640. if (!ste) {
  641. continue;
  642. }
  643. Ref<Script> script = ste->get_edited_script();
  644. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
  645. continue; //internal script, who cares
  646. }
  647. if (script == p_res) {
  648. ste->get_text_edit()->tag_saved_version();
  649. }
  650. }
  651. _update_script_names();
  652. if (!pending_auto_reload && auto_reload_running_scripts) {
  653. call_deferred("_live_auto_reload_running_scripts");
  654. pending_auto_reload = true;
  655. }
  656. }
  657. void ScriptEditor::_live_auto_reload_running_scripts() {
  658. pending_auto_reload = false;
  659. debugger->reload_scripts();
  660. }
  661. bool ScriptEditor::_test_script_times_on_disk(Ref<Script> p_for_script) {
  662. disk_changed_list->clear();
  663. TreeItem *r = disk_changed_list->create_item();
  664. disk_changed_list->set_hide_root(true);
  665. bool need_ask = false;
  666. bool need_reload = false;
  667. bool use_autoreload = bool(EDITOR_DEF("text_editor/auto_reload_scripts_on_external_change", false));
  668. for (int i = 0; i < tab_container->get_child_count(); i++) {
  669. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  670. if (ste) {
  671. Ref<Script> script = ste->get_edited_script();
  672. if (p_for_script.is_valid() && p_for_script != script)
  673. continue;
  674. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1)
  675. continue; //internal script, who cares
  676. uint64_t last_date = script->get_last_modified_time();
  677. uint64_t date = FileAccess::get_modified_time(script->get_path());
  678. //printf("last date: %lli vs date: %lli\n",last_date,date);
  679. if (last_date != date) {
  680. TreeItem *ti = disk_changed_list->create_item(r);
  681. ti->set_text(0, script->get_path().get_file());
  682. if (!use_autoreload || ste->is_unsaved()) {
  683. need_ask = true;
  684. }
  685. need_reload = true;
  686. //r->set_metadata(0,);
  687. }
  688. }
  689. }
  690. if (need_reload) {
  691. if (!need_ask) {
  692. script_editor->_reload_scripts();
  693. need_reload = false;
  694. } else {
  695. disk_changed->call_deferred("popup_centered_ratio", 0.5);
  696. }
  697. }
  698. return need_reload;
  699. }
  700. void ScriptEditor::swap_lines(TextEdit *tx, int line1, int line2) {
  701. String tmp = tx->get_line(line1);
  702. String tmp2 = tx->get_line(line2);
  703. tx->set_line(line2, tmp);
  704. tx->set_line(line1, tmp2);
  705. tx->cursor_set_line(line2);
  706. }
  707. void ScriptEditor::_breakpoint_toggled(const int p_row) {
  708. int selected = tab_container->get_current_tab();
  709. if (selected < 0 || selected >= tab_container->get_child_count()) {
  710. return;
  711. }
  712. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  713. if (current) {
  714. get_debugger()->set_breakpoint(current->get_edited_script()->get_path(), p_row + 1, current->get_text_edit()->is_line_set_as_breakpoint(p_row));
  715. }
  716. }
  717. void ScriptEditor::_file_dialog_action(String p_file) {
  718. switch (file_dialog_option) {
  719. case FILE_SAVE_THEME_AS: {
  720. if (!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) {
  721. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  722. }
  723. } break;
  724. case FILE_IMPORT_THEME: {
  725. if (!EditorSettings::get_singleton()->import_text_editor_theme(p_file)) {
  726. editor->show_warning(TTR("Error importing theme"), TTR("Error importing"));
  727. }
  728. } break;
  729. }
  730. file_dialog_option = -1;
  731. }
  732. void ScriptEditor::_menu_option(int p_option) {
  733. switch (p_option) {
  734. case FILE_NEW: {
  735. script_create_dialog->config("Node", ".gd");
  736. script_create_dialog->popup_centered(Size2(300, 300) * EDSCALE);
  737. } break;
  738. case FILE_OPEN: {
  739. editor->open_resource("Script");
  740. return;
  741. } break;
  742. case FILE_SAVE_ALL: {
  743. if (!_test_script_times_on_disk())
  744. return;
  745. save_all_scripts();
  746. #if 0
  747. for(int i=0;i<tab_container->get_child_count();i++) {
  748. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  749. if (!ste)
  750. continue;
  751. Ref<Script> script = ste->get_edited_script();
  752. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  753. continue; //internal script, who cares
  754. editor->save_resource( script );
  755. }
  756. #endif
  757. } break;
  758. case FILE_IMPORT_THEME: {
  759. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  760. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  761. file_dialog_option = FILE_IMPORT_THEME;
  762. file_dialog->clear_filters();
  763. file_dialog->add_filter("*.tet");
  764. file_dialog->popup_centered_ratio();
  765. file_dialog->set_title(TTR("Import Theme"));
  766. } break;
  767. case FILE_RELOAD_THEME: {
  768. EditorSettings::get_singleton()->load_text_editor_theme();
  769. } break;
  770. case FILE_SAVE_THEME: {
  771. if (!EditorSettings::get_singleton()->save_text_editor_theme()) {
  772. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  773. }
  774. } break;
  775. case FILE_SAVE_THEME_AS: {
  776. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  777. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  778. file_dialog_option = FILE_SAVE_THEME_AS;
  779. file_dialog->clear_filters();
  780. file_dialog->add_filter("*.tet");
  781. file_dialog->set_current_path(EditorSettings::get_singleton()->get_settings_path() + "/text_editor_themes/" + EditorSettings::get_singleton()->get("text_editor/color_theme"));
  782. file_dialog->popup_centered_ratio();
  783. file_dialog->set_title(TTR("Save Theme As.."));
  784. } break;
  785. case SEARCH_HELP: {
  786. help_search_dialog->popup();
  787. } break;
  788. case SEARCH_CLASSES: {
  789. String current;
  790. if (tab_container->get_tab_count() > 0) {
  791. EditorHelp *eh = tab_container->get_child(tab_container->get_current_tab())->cast_to<EditorHelp>();
  792. if (eh) {
  793. current = eh->get_class_name();
  794. }
  795. }
  796. help_index->popup();
  797. if (current != "") {
  798. help_index->call_deferred("select_class", current);
  799. }
  800. } break;
  801. case SEARCH_WEBSITE: {
  802. OS::get_singleton()->shell_open("http://docs.godotengine.org/");
  803. } break;
  804. case WINDOW_NEXT: {
  805. _history_forward();
  806. } break;
  807. case WINDOW_PREV: {
  808. _history_back();
  809. } break;
  810. case DEBUG_SHOW: {
  811. if (debugger) {
  812. bool visible = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW));
  813. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW), !visible);
  814. if (visible)
  815. debugger->hide();
  816. else
  817. debugger->show();
  818. }
  819. } break;
  820. case DEBUG_SHOW_KEEP_OPEN: {
  821. bool visible = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN));
  822. if (debugger)
  823. debugger->set_hide_on_stop(visible);
  824. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN), !visible);
  825. } break;
  826. }
  827. int selected = tab_container->get_current_tab();
  828. if (selected < 0 || selected >= tab_container->get_child_count())
  829. return;
  830. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  831. if (current) {
  832. switch (p_option) {
  833. case FILE_NEW: {
  834. script_create_dialog->config("Node", ".gd");
  835. script_create_dialog->popup_centered(Size2(300, 300) * EDSCALE);
  836. } break;
  837. case FILE_SAVE: {
  838. if (_test_script_times_on_disk())
  839. return;
  840. if (trim_trailing_whitespace_on_save) {
  841. _trim_trailing_whitespace(current->get_text_edit());
  842. }
  843. editor->save_resource(current->get_edited_script());
  844. } break;
  845. case FILE_SAVE_AS: {
  846. if (trim_trailing_whitespace_on_save) {
  847. _trim_trailing_whitespace(current->get_text_edit());
  848. }
  849. editor->push_item(current->get_edited_script()->cast_to<Object>());
  850. editor->save_resource_as(current->get_edited_script());
  851. } break;
  852. case EDIT_UNDO: {
  853. current->get_text_edit()->undo();
  854. current->get_text_edit()->call_deferred("grab_focus");
  855. } break;
  856. case EDIT_REDO: {
  857. current->get_text_edit()->redo();
  858. current->get_text_edit()->call_deferred("grab_focus");
  859. } break;
  860. case EDIT_CUT: {
  861. current->get_text_edit()->cut();
  862. current->get_text_edit()->call_deferred("grab_focus");
  863. } break;
  864. case EDIT_COPY: {
  865. current->get_text_edit()->copy();
  866. current->get_text_edit()->call_deferred("grab_focus");
  867. } break;
  868. case EDIT_PASTE: {
  869. current->get_text_edit()->paste();
  870. current->get_text_edit()->call_deferred("grab_focus");
  871. } break;
  872. case EDIT_SELECT_ALL: {
  873. current->get_text_edit()->select_all();
  874. current->get_text_edit()->call_deferred("grab_focus");
  875. } break;
  876. case EDIT_MOVE_LINE_UP: {
  877. TextEdit *tx = current->get_text_edit();
  878. Ref<Script> scr = current->get_edited_script();
  879. if (scr.is_null())
  880. return;
  881. tx->begin_complex_operation();
  882. if (tx->is_selection_active()) {
  883. int from_line = tx->get_selection_from_line();
  884. int from_col = tx->get_selection_from_column();
  885. int to_line = tx->get_selection_to_line();
  886. int to_column = tx->get_selection_to_column();
  887. for (int i = from_line; i <= to_line; i++) {
  888. int line_id = i;
  889. int next_id = i - 1;
  890. if (line_id == 0 || next_id < 0)
  891. return;
  892. swap_lines(tx, line_id, next_id);
  893. }
  894. int from_line_up = from_line > 0 ? from_line - 1 : from_line;
  895. int to_line_up = to_line > 0 ? to_line - 1 : to_line;
  896. tx->select(from_line_up, from_col, to_line_up, to_column);
  897. } else {
  898. int line_id = tx->cursor_get_line();
  899. int next_id = line_id - 1;
  900. if (line_id == 0 || next_id < 0)
  901. return;
  902. swap_lines(tx, line_id, next_id);
  903. }
  904. tx->end_complex_operation();
  905. tx->update();
  906. } break;
  907. case EDIT_MOVE_LINE_DOWN: {
  908. TextEdit *tx = current->get_text_edit();
  909. Ref<Script> scr = current->get_edited_script();
  910. if (scr.is_null())
  911. return;
  912. tx->begin_complex_operation();
  913. if (tx->is_selection_active()) {
  914. int from_line = tx->get_selection_from_line();
  915. int from_col = tx->get_selection_from_column();
  916. int to_line = tx->get_selection_to_line();
  917. int to_column = tx->get_selection_to_column();
  918. for (int i = to_line; i >= from_line; i--) {
  919. int line_id = i;
  920. int next_id = i + 1;
  921. if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
  922. return;
  923. swap_lines(tx, line_id, next_id);
  924. }
  925. int from_line_down = from_line < tx->get_line_count() ? from_line + 1 : from_line;
  926. int to_line_down = to_line < tx->get_line_count() ? to_line + 1 : to_line;
  927. tx->select(from_line_down, from_col, to_line_down, to_column);
  928. } else {
  929. int line_id = tx->cursor_get_line();
  930. int next_id = line_id + 1;
  931. if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
  932. return;
  933. swap_lines(tx, line_id, next_id);
  934. }
  935. tx->end_complex_operation();
  936. tx->update();
  937. } break;
  938. case EDIT_INDENT_LEFT: {
  939. TextEdit *tx = current->get_text_edit();
  940. Ref<Script> scr = current->get_edited_script();
  941. if (scr.is_null())
  942. return;
  943. tx->begin_complex_operation();
  944. if (tx->is_selection_active()) {
  945. tx->indent_selection_left();
  946. } else {
  947. int begin = tx->cursor_get_line();
  948. String line_text = tx->get_line(begin);
  949. // begins with tab
  950. if (line_text.begins_with("\t")) {
  951. line_text = line_text.substr(1, line_text.length());
  952. tx->set_line(begin, line_text);
  953. }
  954. // begins with 4 spaces
  955. else if (line_text.begins_with(" ")) {
  956. line_text = line_text.substr(4, line_text.length());
  957. tx->set_line(begin, line_text);
  958. }
  959. }
  960. tx->end_complex_operation();
  961. tx->update();
  962. //tx->deselect();
  963. } break;
  964. case EDIT_INDENT_RIGHT: {
  965. TextEdit *tx = current->get_text_edit();
  966. Ref<Script> scr = current->get_edited_script();
  967. if (scr.is_null())
  968. return;
  969. tx->begin_complex_operation();
  970. if (tx->is_selection_active()) {
  971. tx->indent_selection_right();
  972. } else {
  973. int begin = tx->cursor_get_line();
  974. String line_text = tx->get_line(begin);
  975. line_text = '\t' + line_text;
  976. tx->set_line(begin, line_text);
  977. }
  978. tx->end_complex_operation();
  979. tx->update();
  980. //tx->deselect();
  981. } break;
  982. case EDIT_CLONE_DOWN: {
  983. TextEdit *tx = current->get_text_edit();
  984. Ref<Script> scr = current->get_edited_script();
  985. if (scr.is_null())
  986. return;
  987. int from_line = tx->cursor_get_line();
  988. int to_line = tx->cursor_get_line();
  989. int column = tx->cursor_get_column();
  990. if (tx->is_selection_active()) {
  991. from_line = tx->get_selection_from_line();
  992. to_line = tx->get_selection_to_line();
  993. column = tx->cursor_get_column();
  994. }
  995. int next_line = to_line + 1;
  996. tx->begin_complex_operation();
  997. for (int i = from_line; i <= to_line; i++) {
  998. if (i >= tx->get_line_count() - 1) {
  999. tx->set_line(i, tx->get_line(i) + "\n");
  1000. }
  1001. String line_clone = tx->get_line(i);
  1002. tx->insert_at(line_clone, next_line);
  1003. next_line++;
  1004. }
  1005. tx->cursor_set_column(column);
  1006. if (tx->is_selection_active()) {
  1007. tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
  1008. }
  1009. tx->end_complex_operation();
  1010. tx->update();
  1011. } break;
  1012. case EDIT_TOGGLE_COMMENT: {
  1013. TextEdit *tx = current->get_text_edit();
  1014. Ref<Script> scr = current->get_edited_script();
  1015. if (scr.is_null())
  1016. return;
  1017. tx->begin_complex_operation();
  1018. if (tx->is_selection_active()) {
  1019. int begin = tx->get_selection_from_line();
  1020. int end = tx->get_selection_to_line();
  1021. // End of selection ends on the first column of the last line, ignore it.
  1022. if (tx->get_selection_to_column() == 0)
  1023. end -= 1;
  1024. for (int i = begin; i <= end; i++) {
  1025. String line_text = tx->get_line(i);
  1026. if (line_text.begins_with("#"))
  1027. line_text = line_text.substr(1, line_text.length());
  1028. else
  1029. line_text = "#" + line_text;
  1030. tx->set_line(i, line_text);
  1031. }
  1032. } else {
  1033. int begin = tx->cursor_get_line();
  1034. String line_text = tx->get_line(begin);
  1035. if (line_text.begins_with("#"))
  1036. line_text = line_text.substr(1, line_text.length());
  1037. else
  1038. line_text = "#" + line_text;
  1039. tx->set_line(begin, line_text);
  1040. }
  1041. tx->end_complex_operation();
  1042. tx->update();
  1043. //tx->deselect();
  1044. } break;
  1045. case EDIT_COMPLETE: {
  1046. current->get_text_edit()->query_code_comple();
  1047. } break;
  1048. case EDIT_AUTO_INDENT: {
  1049. TextEdit *te = current->get_text_edit();
  1050. String text = te->get_text();
  1051. Ref<Script> scr = current->get_edited_script();
  1052. if (scr.is_null())
  1053. return;
  1054. int begin, end;
  1055. if (te->is_selection_active()) {
  1056. begin = te->get_selection_from_line();
  1057. end = te->get_selection_to_line();
  1058. } else {
  1059. begin = 0;
  1060. end = te->get_line_count() - 1;
  1061. }
  1062. scr->get_language()->auto_indent_code(text, begin, end);
  1063. te->set_text(text);
  1064. } break;
  1065. case FILE_TOOL_RELOAD:
  1066. case FILE_TOOL_RELOAD_SOFT: {
  1067. TextEdit *te = current->get_text_edit();
  1068. Ref<Script> scr = current->get_edited_script();
  1069. if (scr.is_null())
  1070. return;
  1071. scr->set_source_code(te->get_text());
  1072. bool soft = p_option == FILE_TOOL_RELOAD_SOFT || scr->get_instance_base_type() == "EditorPlugin"; //always soft-reload editor plugins
  1073. scr->get_language()->reload_tool_script(scr, soft);
  1074. } break;
  1075. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  1076. _trim_trailing_whitespace(current->get_text_edit());
  1077. } break;
  1078. case SEARCH_FIND: {
  1079. current->get_find_replace_bar()->popup_search();
  1080. } break;
  1081. case SEARCH_FIND_NEXT: {
  1082. current->get_find_replace_bar()->search_next();
  1083. } break;
  1084. case SEARCH_FIND_PREV: {
  1085. current->get_find_replace_bar()->search_prev();
  1086. } break;
  1087. case SEARCH_REPLACE: {
  1088. current->get_find_replace_bar()->popup_replace();
  1089. } break;
  1090. case SEARCH_LOCATE_FUNCTION: {
  1091. if (!current)
  1092. return;
  1093. quick_open->popup(current->get_functions());
  1094. } break;
  1095. case SEARCH_GOTO_LINE: {
  1096. goto_line_dialog->popup_find_line(current->get_text_edit());
  1097. } break;
  1098. case DEBUG_TOGGLE_BREAKPOINT: {
  1099. int line = current->get_text_edit()->cursor_get_line();
  1100. bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
  1101. current->get_text_edit()->set_line_as_breakpoint(line, dobreak);
  1102. get_debugger()->set_breakpoint(current->get_edited_script()->get_path(), line + 1, dobreak);
  1103. } break;
  1104. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  1105. List<int> bpoints;
  1106. current->get_text_edit()->get_breakpoints(&bpoints);
  1107. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  1108. int line = E->get();
  1109. bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
  1110. current->get_text_edit()->set_line_as_breakpoint(line, dobreak);
  1111. get_debugger()->set_breakpoint(current->get_edited_script()->get_path(), line + 1, dobreak);
  1112. }
  1113. }
  1114. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  1115. List<int> bpoints;
  1116. current->get_text_edit()->get_breakpoints(&bpoints);
  1117. if (bpoints.size() <= 0) {
  1118. return;
  1119. }
  1120. int line = current->get_text_edit()->cursor_get_line();
  1121. // wrap around
  1122. if (line >= bpoints[bpoints.size() - 1]) {
  1123. current->get_text_edit()->cursor_set_line(bpoints[0]);
  1124. } else {
  1125. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  1126. int bline = E->get();
  1127. if (bline > line) {
  1128. current->get_text_edit()->cursor_set_line(bline);
  1129. return;
  1130. }
  1131. }
  1132. }
  1133. } break;
  1134. case DEBUG_GOTO_PREV_BREAKPOINT: {
  1135. List<int> bpoints;
  1136. current->get_text_edit()->get_breakpoints(&bpoints);
  1137. if (bpoints.size() <= 0) {
  1138. return;
  1139. }
  1140. int line = current->get_text_edit()->cursor_get_line();
  1141. // wrap around
  1142. if (line <= bpoints[0]) {
  1143. current->get_text_edit()->cursor_set_line(bpoints[bpoints.size() - 1]);
  1144. } else {
  1145. for (List<int>::Element *E = bpoints.back(); E; E = E->prev()) {
  1146. int bline = E->get();
  1147. if (bline < line) {
  1148. current->get_text_edit()->cursor_set_line(bline);
  1149. return;
  1150. }
  1151. }
  1152. }
  1153. } break;
  1154. case DEBUG_NEXT: {
  1155. if (debugger)
  1156. debugger->debug_next();
  1157. } break;
  1158. case DEBUG_STEP: {
  1159. if (debugger)
  1160. debugger->debug_step();
  1161. } break;
  1162. case DEBUG_BREAK: {
  1163. if (debugger)
  1164. debugger->debug_break();
  1165. } break;
  1166. case DEBUG_CONTINUE: {
  1167. if (debugger)
  1168. debugger->debug_continue();
  1169. } break;
  1170. case HELP_CONTEXTUAL: {
  1171. String text = current->get_text_edit()->get_selection_text();
  1172. if (text == "")
  1173. text = current->get_text_edit()->get_word_under_cursor();
  1174. if (text != "")
  1175. help_search_dialog->popup(text);
  1176. } break;
  1177. case FILE_CLOSE: {
  1178. if (current->get_text_edit()->get_version() != current->get_text_edit()->get_saved_version()) {
  1179. erase_tab_confirm->set_text("Close and save changes?\n\"" + current->get_name() + "\"");
  1180. erase_tab_confirm->popup_centered_minsize();
  1181. } else {
  1182. _close_current_tab();
  1183. }
  1184. } break;
  1185. case CLOSE_DOCS: {
  1186. _close_docs_tab();
  1187. } break;
  1188. case WINDOW_MOVE_LEFT: {
  1189. if (tab_container->get_current_tab() > 0) {
  1190. tab_container->call_deferred("set_current_tab", tab_container->get_current_tab() - 1);
  1191. script_list->call_deferred("select", tab_container->get_current_tab() - 1);
  1192. tab_container->move_child(current, tab_container->get_current_tab() - 1);
  1193. _update_script_names();
  1194. }
  1195. } break;
  1196. case WINDOW_MOVE_RIGHT: {
  1197. if (tab_container->get_current_tab() < tab_container->get_child_count() - 1) {
  1198. tab_container->call_deferred("set_current_tab", tab_container->get_current_tab() + 1);
  1199. script_list->call_deferred("select", tab_container->get_current_tab() + 1);
  1200. tab_container->move_child(current, tab_container->get_current_tab() + 1);
  1201. _update_script_names();
  1202. }
  1203. } break;
  1204. default: {
  1205. if (p_option >= WINDOW_SELECT_BASE) {
  1206. tab_container->set_current_tab(p_option - WINDOW_SELECT_BASE);
  1207. script_list->select(p_option - WINDOW_SELECT_BASE);
  1208. }
  1209. }
  1210. }
  1211. } else {
  1212. EditorHelp *help = tab_container->get_current_tab_control()->cast_to<EditorHelp>();
  1213. if (help) {
  1214. switch (p_option) {
  1215. case SEARCH_FIND: {
  1216. help->popup_search();
  1217. } break;
  1218. case SEARCH_FIND_NEXT: {
  1219. help->search_again();
  1220. } break;
  1221. case FILE_CLOSE: {
  1222. _close_current_tab();
  1223. } break;
  1224. case CLOSE_DOCS: {
  1225. _close_docs_tab();
  1226. } break;
  1227. }
  1228. }
  1229. }
  1230. }
  1231. void ScriptEditor::_tab_changed(int p_which) {
  1232. ensure_select_current();
  1233. }
  1234. void ScriptEditor::_notification(int p_what) {
  1235. if (p_what == NOTIFICATION_ENTER_TREE) {
  1236. editor->connect("play_pressed", this, "_editor_play");
  1237. editor->connect("pause_pressed", this, "_editor_pause");
  1238. editor->connect("stop_pressed", this, "_editor_stop");
  1239. editor->connect("script_add_function_request", this, "_add_callback");
  1240. editor->connect("resource_saved", this, "_res_saved_callback");
  1241. script_list->connect("item_selected", this, "_script_selected");
  1242. script_split->connect("dragged", this, "_script_split_dragged");
  1243. autosave_timer->connect("timeout", this, "_autosave_scripts");
  1244. {
  1245. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  1246. if (autosave_time > 0) {
  1247. autosave_timer->set_wait_time(autosave_time);
  1248. autosave_timer->start();
  1249. } else {
  1250. autosave_timer->stop();
  1251. }
  1252. }
  1253. EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
  1254. help_search->set_icon(get_icon("Help", "EditorIcons"));
  1255. site_search->set_icon(get_icon("Godot", "EditorIcons"));
  1256. class_search->set_icon(get_icon("ClassList", "EditorIcons"));
  1257. script_forward->set_icon(get_icon("Forward", "EditorIcons"));
  1258. script_back->set_icon(get_icon("Back", "EditorIcons"));
  1259. }
  1260. if (p_what == NOTIFICATION_READY) {
  1261. get_tree()->connect("tree_changed", this, "_tree_changed");
  1262. editor->connect("request_help", this, "_request_help");
  1263. editor->connect("request_help_search", this, "_help_search");
  1264. }
  1265. if (p_what == NOTIFICATION_EXIT_TREE) {
  1266. editor->disconnect("play_pressed", this, "_editor_play");
  1267. editor->disconnect("pause_pressed", this, "_editor_pause");
  1268. editor->disconnect("stop_pressed", this, "_editor_stop");
  1269. }
  1270. if (p_what == MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  1271. _test_script_times_on_disk();
  1272. _update_modified_scripts_for_external_editor();
  1273. }
  1274. if (p_what == NOTIFICATION_PROCESS) {
  1275. }
  1276. }
  1277. void ScriptEditor::close_builtin_scripts_from_scene(const String &p_scene) {
  1278. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1279. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1280. if (ste) {
  1281. Ref<Script> script = ste->get_edited_script();
  1282. if (!script.is_valid())
  1283. continue;
  1284. if (script->get_path().find("::") != -1 && script->get_path().begins_with(p_scene)) { //is an internal script and belongs to scene being closed
  1285. _close_tab(i);
  1286. i--;
  1287. }
  1288. }
  1289. }
  1290. }
  1291. void ScriptEditor::edited_scene_changed() {
  1292. _update_modified_scripts_for_external_editor();
  1293. }
  1294. static const Node *_find_node_with_script(const Node *p_node, const RefPtr &p_script) {
  1295. if (p_node->get_script() == p_script)
  1296. return p_node;
  1297. for (int i = 0; i < p_node->get_child_count(); i++) {
  1298. const Node *result = _find_node_with_script(p_node->get_child(i), p_script);
  1299. if (result)
  1300. return result;
  1301. }
  1302. return NULL;
  1303. }
  1304. Dictionary ScriptEditor::get_state() const {
  1305. // apply_scripts();
  1306. Dictionary state;
  1307. #if 0
  1308. Array paths;
  1309. int open=-1;
  1310. for(int i=0;i<tab_container->get_child_count();i++) {
  1311. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1312. if (!ste)
  1313. continue;
  1314. Ref<Script> script = ste->get_edited_script();
  1315. if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  1316. paths.push_back(script->get_path());
  1317. } else {
  1318. const Node *owner = _find_node_with_script(get_tree()->get_root(),script.get_ref_ptr());
  1319. if (owner)
  1320. paths.push_back(owner->get_path());
  1321. }
  1322. if (i==tab_container->get_current_tab())
  1323. open=i;
  1324. }
  1325. if (paths.size())
  1326. state["sources"]=paths;
  1327. if (open!=-1)
  1328. state["current"]=open;
  1329. #endif
  1330. return state;
  1331. }
  1332. void ScriptEditor::set_state(const Dictionary &p_state) {
  1333. #if 0
  1334. print_line("attempt set state: "+String(Variant(p_state)));
  1335. if (!p_state.has("sources"))
  1336. return; //bleh
  1337. Array sources = p_state["sources"];
  1338. for(int i=0;i<sources.size();i++) {
  1339. Variant source=sources[i];
  1340. Ref<Script> script;
  1341. if (source.get_type()==Variant::NODE_PATH) {
  1342. Node *owner=get_tree()->get_root()->get_node(source);
  1343. if (!owner)
  1344. continue;
  1345. script = owner->get_script();
  1346. } else if (source.get_type()==Variant::STRING) {
  1347. script = ResourceLoader::load(source,"Script");
  1348. }
  1349. if (script.is_null()) //ah well..
  1350. continue;
  1351. editor->call("_resource_selected",script);
  1352. }
  1353. if (p_state.has("current")) {
  1354. tab_container->set_current_tab(p_state["current"]);
  1355. }
  1356. #endif
  1357. }
  1358. void ScriptEditor::clear() {
  1359. #if 0
  1360. List<ScriptTextEditor*> stes;
  1361. for(int i=0;i<tab_container->get_child_count();i++) {
  1362. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1363. if (!ste)
  1364. continue;
  1365. stes.push_back(ste);
  1366. }
  1367. while(stes.size()) {
  1368. memdelete(stes.front()->get());
  1369. stes.pop_front();
  1370. }
  1371. int idx = tab_container->get_current_tab();
  1372. if (idx>=tab_container->get_child_count())
  1373. idx=tab_container->get_child_count()-1;
  1374. if (idx>=0) {
  1375. tab_container->set_current_tab(idx);
  1376. script_list->select( script_list->find_metadata(idx) );
  1377. }
  1378. #endif
  1379. }
  1380. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  1381. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1382. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1383. if (!ste)
  1384. continue;
  1385. List<int> bpoints;
  1386. ste->get_text_edit()->get_breakpoints(&bpoints);
  1387. Ref<Script> script = ste->get_edited_script();
  1388. String base = script->get_path();
  1389. ERR_CONTINUE(base.begins_with("local://") || base == "");
  1390. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  1391. p_breakpoints->push_back(base + ":" + itos(E->get() + 1));
  1392. }
  1393. }
  1394. }
  1395. void ScriptEditor::ensure_focus_current() {
  1396. if (!is_inside_tree())
  1397. return;
  1398. int cidx = tab_container->get_current_tab();
  1399. if (cidx < 0 || cidx >= tab_container->get_tab_count())
  1400. ;
  1401. Control *c = tab_container->get_child(cidx)->cast_to<Control>();
  1402. if (!c)
  1403. return;
  1404. ScriptTextEditor *ste = c->cast_to<ScriptTextEditor>();
  1405. if (!ste)
  1406. return;
  1407. ste->get_text_edit()->grab_focus();
  1408. }
  1409. void ScriptEditor::_script_selected(int p_idx) {
  1410. grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing
  1411. _go_to_tab(script_list->get_item_metadata(p_idx));
  1412. grab_focus_block = false;
  1413. }
  1414. void ScriptEditor::ensure_select_current() {
  1415. if (tab_container->get_child_count() && tab_container->get_current_tab() >= 0) {
  1416. Node *current = tab_container->get_child(tab_container->get_current_tab());
  1417. ScriptTextEditor *ste = current->cast_to<ScriptTextEditor>();
  1418. if (ste) {
  1419. Ref<Script> script = ste->get_edited_script();
  1420. if (!grab_focus_block && is_visible())
  1421. ste->get_text_edit()->grab_focus();
  1422. edit_menu->show();
  1423. search_menu->show();
  1424. script_search_menu->hide();
  1425. }
  1426. EditorHelp *eh = current->cast_to<EditorHelp>();
  1427. if (eh) {
  1428. edit_menu->hide();
  1429. search_menu->hide();
  1430. script_search_menu->show();
  1431. }
  1432. }
  1433. }
  1434. void ScriptEditor::_find_scripts(Node *p_base, Node *p_current, Set<Ref<Script> > &used) {
  1435. if (p_current != p_base && p_current->get_owner() != p_base)
  1436. return;
  1437. if (p_current->get_script_instance()) {
  1438. Ref<Script> scr = p_current->get_script();
  1439. if (scr.is_valid())
  1440. used.insert(scr);
  1441. }
  1442. for (int i = 0; i < p_current->get_child_count(); i++) {
  1443. _find_scripts(p_base, p_current->get_child(i), used);
  1444. }
  1445. }
  1446. struct _ScriptEditorItemData {
  1447. String name;
  1448. String sort_key;
  1449. Ref<Texture> icon;
  1450. int index;
  1451. String tooltip;
  1452. bool used;
  1453. int category;
  1454. bool operator<(const _ScriptEditorItemData &id) const {
  1455. return category == id.category ? sort_key < id.sort_key : category < id.category;
  1456. }
  1457. };
  1458. void ScriptEditor::_update_script_colors() {
  1459. bool script_temperature_enabled = EditorSettings::get_singleton()->get("text_editor/script_temperature_enabled");
  1460. bool highlight_current = EditorSettings::get_singleton()->get("text_editor/highlight_current_script");
  1461. int hist_size = EditorSettings::get_singleton()->get("text_editor/script_temperature_history_size");
  1462. Color hot_color = EditorSettings::get_singleton()->get("text_editor/script_temperature_hot_color");
  1463. Color cold_color = EditorSettings::get_singleton()->get("text_editor/script_temperature_cold_color");
  1464. for (int i = 0; i < script_list->get_item_count(); i++) {
  1465. int c = script_list->get_item_metadata(i);
  1466. Node *n = tab_container->get_child(c);
  1467. if (!n)
  1468. continue;
  1469. script_list->set_item_custom_bg_color(i, Color(0, 0, 0, 0));
  1470. bool current = tab_container->get_current_tab() == c;
  1471. if (current && highlight_current) {
  1472. script_list->set_item_custom_bg_color(i, EditorSettings::get_singleton()->get("text_editor/current_script_background_color"));
  1473. } else if (script_temperature_enabled) {
  1474. if (!n->has_meta("__editor_pass")) {
  1475. continue;
  1476. }
  1477. int pass = n->get_meta("__editor_pass");
  1478. int h = edit_pass - pass;
  1479. if (h > hist_size) {
  1480. continue;
  1481. }
  1482. int non_zero_hist_size = (hist_size == 0) ? 1 : hist_size;
  1483. float v = Math::ease((edit_pass - pass) / float(non_zero_hist_size), 0.4);
  1484. script_list->set_item_custom_bg_color(i, hot_color.linear_interpolate(cold_color, v));
  1485. }
  1486. }
  1487. }
  1488. void ScriptEditor::_update_script_names() {
  1489. if (restoring_layout)
  1490. return;
  1491. waiting_update_names = false;
  1492. Set<Ref<Script> > used;
  1493. Node *edited = EditorNode::get_singleton()->get_edited_scene();
  1494. if (edited) {
  1495. _find_scripts(edited, edited, used);
  1496. }
  1497. script_list->clear();
  1498. bool split_script_help = EditorSettings::get_singleton()->get("text_editor/group_help_pages");
  1499. ScriptSortBy sort_by = (ScriptSortBy)(int)EditorSettings::get_singleton()->get("text_editor/sort_scripts_by");
  1500. ScriptListName display_as = (ScriptListName)(int)EditorSettings::get_singleton()->get("text_editor/list_script_names_as");
  1501. Vector<_ScriptEditorItemData> sedata;
  1502. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1503. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1504. if (ste) {
  1505. String name = ste->get_name();
  1506. Ref<Texture> icon = ste->get_icon();
  1507. String path = ste->get_edited_script()->get_path();
  1508. _ScriptEditorItemData sd;
  1509. sd.icon = icon;
  1510. sd.name = name;
  1511. sd.tooltip = path;
  1512. sd.index = i;
  1513. sd.used = used.has(ste->get_edited_script());
  1514. sd.category = 0;
  1515. switch (sort_by) {
  1516. case SORT_BY_NAME: {
  1517. sd.sort_key = name.to_lower();
  1518. } break;
  1519. case SORT_BY_PATH: {
  1520. sd.sort_key = path;
  1521. } break;
  1522. }
  1523. switch (display_as) {
  1524. case DISPLAY_NAME: {
  1525. sd.name = name;
  1526. } break;
  1527. case DISPLAY_DIR_AND_NAME: {
  1528. if (!path.get_base_dir().get_file().empty()) {
  1529. sd.name = path.get_base_dir().get_file() + "/" + name;
  1530. } else {
  1531. sd.name = name;
  1532. }
  1533. } break;
  1534. case DISPLAY_FULL_PATH: {
  1535. sd.name = path;
  1536. } break;
  1537. }
  1538. sedata.push_back(sd);
  1539. }
  1540. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1541. if (eh) {
  1542. String name = eh->get_class_name();
  1543. Ref<Texture> icon = get_icon("Help", "EditorIcons");
  1544. String tooltip = name + " Class Reference";
  1545. _ScriptEditorItemData sd;
  1546. sd.icon = icon;
  1547. sd.name = name;
  1548. sd.sort_key = name;
  1549. sd.tooltip = tooltip;
  1550. sd.index = i;
  1551. sd.used = false;
  1552. sd.category = split_script_help ? 1 : 0;
  1553. sedata.push_back(sd);
  1554. }
  1555. }
  1556. sedata.sort();
  1557. for (int i = 0; i < sedata.size(); i++) {
  1558. script_list->add_item(sedata[i].name, sedata[i].icon);
  1559. int index = script_list->get_item_count() - 1;
  1560. script_list->set_item_tooltip(index, sedata[i].tooltip);
  1561. script_list->set_item_metadata(index, sedata[i].index);
  1562. if (sedata[i].used) {
  1563. script_list->set_item_custom_bg_color(index, Color(88 / 255.0, 88 / 255.0, 60 / 255.0));
  1564. }
  1565. if (tab_container->get_current_tab() == sedata[i].index) {
  1566. script_list->select(index);
  1567. script_name_label->set_text(sedata[i].name);
  1568. script_icon->set_texture(sedata[i].icon);
  1569. }
  1570. }
  1571. _update_script_colors();
  1572. }
  1573. void ScriptEditor::edit(const Ref<Script> &p_script) {
  1574. if (p_script.is_null())
  1575. return;
  1576. // refuse to open built-in if scene is not loaded
  1577. // see if already has it
  1578. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
  1579. if (p_script->get_path().is_resource_file() && bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor"))) {
  1580. String path = EditorSettings::get_singleton()->get("external_editor/exec_path");
  1581. String flags = EditorSettings::get_singleton()->get("external_editor/exec_flags");
  1582. List<String> args;
  1583. flags = flags.strip_edges();
  1584. if (flags != String()) {
  1585. Vector<String> flagss = flags.split(" ", false);
  1586. for (int i = 0; i < flagss.size(); i++)
  1587. args.push_back(flagss[i]);
  1588. }
  1589. args.push_back(Globals::get_singleton()->globalize_path(p_script->get_path()));
  1590. Error err = OS::get_singleton()->execute(path, args, false);
  1591. if (err == OK)
  1592. return;
  1593. WARN_PRINT("Couldn't open external text editor, using internal");
  1594. }
  1595. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1596. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1597. if (!ste)
  1598. continue;
  1599. if (ste->get_edited_script() == p_script) {
  1600. if (open_dominant || !EditorNode::get_singleton()->is_changing_scene()) {
  1601. if (tab_container->get_current_tab() != i) {
  1602. _go_to_tab(i);
  1603. script_list->select(script_list->find_metadata(i));
  1604. }
  1605. if (is_visible())
  1606. ste->get_text_edit()->grab_focus();
  1607. }
  1608. return;
  1609. }
  1610. }
  1611. // doesn't have it, make a new one
  1612. ScriptTextEditor *ste = memnew(ScriptTextEditor);
  1613. ste->update_editor_settings();
  1614. ste->set_edited_script(p_script);
  1615. ste->get_text_edit()->set_tooltip_request_func(this, "_get_debug_tooltip", ste);
  1616. ste->get_text_edit()->set_callhint_settings(
  1617. EditorSettings::get_singleton()->get("text_editor/put_callhint_tooltip_below_current_line"),
  1618. EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset"));
  1619. ste->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled");
  1620. tab_container->add_child(ste);
  1621. _go_to_tab(tab_container->get_tab_count() - 1);
  1622. _update_script_names();
  1623. _save_layout();
  1624. ste->connect("name_changed", this, "_update_script_names");
  1625. //test for modification, maybe the script was not edited but was loaded
  1626. _test_script_times_on_disk(p_script);
  1627. _update_modified_scripts_for_external_editor(p_script);
  1628. }
  1629. void ScriptEditor::save_all_scripts() {
  1630. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1631. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1632. if (!ste)
  1633. continue;
  1634. if (trim_trailing_whitespace_on_save) {
  1635. _trim_trailing_whitespace(ste->get_text_edit());
  1636. }
  1637. if (ste->get_text_edit()->get_version() == ste->get_text_edit()->get_saved_version())
  1638. continue;
  1639. Ref<Script> script = ste->get_edited_script();
  1640. if (script.is_valid())
  1641. ste->apply_code();
  1642. if (script->get_path() != "" && script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  1643. //external script, save it
  1644. editor->save_resource(script);
  1645. //ResourceSaver::save(script->get_path(),script);
  1646. }
  1647. }
  1648. }
  1649. void ScriptEditor::apply_scripts() const {
  1650. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1651. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1652. if (!ste)
  1653. continue;
  1654. ste->apply_code();
  1655. }
  1656. }
  1657. void ScriptEditor::_editor_play() {
  1658. debugger->start();
  1659. debug_menu->get_popup()->grab_focus();
  1660. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1661. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  1662. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false);
  1663. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  1664. }
  1665. void ScriptEditor::_editor_pause() {
  1666. }
  1667. void ScriptEditor::_editor_stop() {
  1668. debugger->stop();
  1669. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1670. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  1671. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true);
  1672. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  1673. }
  1674. void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const StringArray &p_args) {
  1675. //print_line("add callback! hohoho"); kinda sad to remove this
  1676. ERR_FAIL_COND(!p_obj);
  1677. Ref<Script> script = p_obj->get_script();
  1678. ERR_FAIL_COND(!script.is_valid());
  1679. editor->push_item(script.ptr());
  1680. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1681. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1682. if (!ste)
  1683. continue;
  1684. if (ste->get_edited_script() != script)
  1685. continue;
  1686. String code = ste->get_text_edit()->get_text();
  1687. int pos = script->get_language()->find_function(p_function, code);
  1688. if (pos == -1) {
  1689. //does not exist
  1690. ste->get_text_edit()->deselect();
  1691. pos = ste->get_text_edit()->get_line_count() + 2;
  1692. String func = script->get_language()->make_function("", p_function, p_args);
  1693. //code=code+func;
  1694. ste->get_text_edit()->cursor_set_line(pos + 1);
  1695. ste->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  1696. ste->get_text_edit()->insert_text_at_cursor("\n\n" + func);
  1697. }
  1698. _go_to_tab(i);
  1699. ste->get_text_edit()->cursor_set_line(pos);
  1700. ste->get_text_edit()->cursor_set_column(1);
  1701. script_list->select(script_list->find_metadata(i));
  1702. break;
  1703. }
  1704. }
  1705. void ScriptEditor::_save_layout() {
  1706. if (restoring_layout) {
  1707. return;
  1708. }
  1709. editor->save_layout();
  1710. }
  1711. void ScriptEditor::_editor_settings_changed() {
  1712. trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/trim_trailing_whitespace_on_save");
  1713. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  1714. if (autosave_time > 0) {
  1715. autosave_timer->set_wait_time(autosave_time);
  1716. autosave_timer->start();
  1717. } else {
  1718. autosave_timer->stop();
  1719. }
  1720. if (current_theme == "") {
  1721. current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
  1722. } else if (current_theme != EditorSettings::get_singleton()->get("text_editor/color_theme")) {
  1723. current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
  1724. EditorSettings::get_singleton()->load_text_editor_theme();
  1725. }
  1726. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1727. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1728. if (!ste)
  1729. continue;
  1730. ste->update_editor_settings();
  1731. }
  1732. _update_script_colors();
  1733. _update_script_names();
  1734. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save", true));
  1735. }
  1736. void ScriptEditor::_autosave_scripts() {
  1737. save_all_scripts();
  1738. }
  1739. void ScriptEditor::_tree_changed() {
  1740. if (waiting_update_names)
  1741. return;
  1742. waiting_update_names = true;
  1743. call_deferred("_update_script_names");
  1744. }
  1745. void ScriptEditor::_script_split_dragged(float) {
  1746. _save_layout();
  1747. }
  1748. void ScriptEditor::_unhandled_input(const InputEvent &p_event) {
  1749. if (p_event.key.pressed || !is_visible()) return;
  1750. if (ED_IS_SHORTCUT("script_editor/next_script", p_event)) {
  1751. int next_tab = script_list->get_current() + 1;
  1752. next_tab %= script_list->get_item_count();
  1753. _go_to_tab(script_list->get_item_metadata(next_tab));
  1754. _update_script_names();
  1755. }
  1756. if (ED_IS_SHORTCUT("script_editor/prev_script", p_event)) {
  1757. int next_tab = script_list->get_current() - 1;
  1758. next_tab = next_tab >= 0 ? next_tab : script_list->get_item_count() - 1;
  1759. _go_to_tab(script_list->get_item_metadata(next_tab));
  1760. _update_script_names();
  1761. }
  1762. }
  1763. void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
  1764. if (!bool(EDITOR_DEF("text_editor/restore_scripts_on_load", true))) {
  1765. return;
  1766. }
  1767. if (!p_layout->has_section_key("ScriptEditor", "open_scripts") && !p_layout->has_section_key("ScriptEditor", "open_help"))
  1768. return;
  1769. Array scripts = p_layout->get_value("ScriptEditor", "open_scripts");
  1770. Array helps;
  1771. if (p_layout->has_section_key("ScriptEditor", "open_help"))
  1772. helps = p_layout->get_value("ScriptEditor", "open_help");
  1773. restoring_layout = true;
  1774. for (int i = 0; i < scripts.size(); i++) {
  1775. String path = scripts[i];
  1776. if (!FileAccess::exists(path))
  1777. continue;
  1778. Ref<Script> scr = ResourceLoader::load(path);
  1779. if (scr.is_valid()) {
  1780. edit(scr);
  1781. }
  1782. }
  1783. for (int i = 0; i < helps.size(); i++) {
  1784. String path = helps[i];
  1785. _help_class_open(path);
  1786. }
  1787. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1788. tab_container->get_child(i)->set_meta("__editor_pass", Variant());
  1789. }
  1790. if (p_layout->has_section_key("ScriptEditor", "split_offset")) {
  1791. script_split->set_split_offset(p_layout->get_value("ScriptEditor", "split_offset"));
  1792. }
  1793. restoring_layout = false;
  1794. _update_script_names();
  1795. }
  1796. void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
  1797. Array scripts;
  1798. Array helps;
  1799. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1800. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1801. if (ste) {
  1802. String path = ste->get_edited_script()->get_path();
  1803. if (!path.is_resource_file())
  1804. continue;
  1805. scripts.push_back(path);
  1806. }
  1807. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1808. if (eh) {
  1809. helps.push_back(eh->get_class_name());
  1810. }
  1811. }
  1812. p_layout->set_value("ScriptEditor", "open_scripts", scripts);
  1813. p_layout->set_value("ScriptEditor", "open_help", helps);
  1814. p_layout->set_value("ScriptEditor", "split_offset", script_split->get_split_offset());
  1815. }
  1816. void ScriptEditor::_help_class_open(const String &p_class) {
  1817. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1818. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1819. if (eh && eh->get_class_name() == p_class) {
  1820. _go_to_tab(i);
  1821. _update_script_names();
  1822. return;
  1823. }
  1824. }
  1825. EditorHelp *eh = memnew(EditorHelp);
  1826. eh->set_name(p_class);
  1827. tab_container->add_child(eh);
  1828. _go_to_tab(tab_container->get_tab_count() - 1);
  1829. eh->go_to_class(p_class, 0);
  1830. eh->connect("go_to_help", this, "_help_class_goto");
  1831. _update_script_names();
  1832. _save_layout();
  1833. }
  1834. void ScriptEditor::_help_class_goto(const String &p_desc) {
  1835. String cname = p_desc.get_slice(":", 1);
  1836. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1837. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1838. if (eh && eh->get_class_name() == cname) {
  1839. _go_to_tab(i);
  1840. eh->go_to_help(p_desc);
  1841. _update_script_names();
  1842. return;
  1843. }
  1844. }
  1845. EditorHelp *eh = memnew(EditorHelp);
  1846. eh->set_name(cname);
  1847. tab_container->add_child(eh);
  1848. _go_to_tab(tab_container->get_tab_count() - 1);
  1849. eh->go_to_help(p_desc);
  1850. eh->connect("go_to_help", this, "_help_class_goto");
  1851. _update_script_names();
  1852. _save_layout();
  1853. }
  1854. void ScriptEditor::_update_history_pos(int p_new_pos) {
  1855. Node *n = tab_container->get_current_tab_control();
  1856. if (n->cast_to<ScriptTextEditor>()) {
  1857. history[history_pos].scroll_pos = n->cast_to<ScriptTextEditor>()->get_text_edit()->get_v_scroll();
  1858. history[history_pos].cursor_column = n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_column();
  1859. history[history_pos].cursor_row = n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_line();
  1860. }
  1861. if (n->cast_to<EditorHelp>()) {
  1862. history[history_pos].scroll_pos = n->cast_to<EditorHelp>()->get_scroll();
  1863. }
  1864. history_pos = p_new_pos;
  1865. tab_container->set_current_tab(history[history_pos].control->get_index());
  1866. n = history[history_pos].control;
  1867. if (n->cast_to<ScriptTextEditor>()) {
  1868. n->cast_to<ScriptTextEditor>()->get_text_edit()->set_v_scroll(history[history_pos].scroll_pos);
  1869. n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_set_column(history[history_pos].cursor_column);
  1870. n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_set_line(history[history_pos].cursor_row);
  1871. n->cast_to<ScriptTextEditor>()->get_text_edit()->grab_focus();
  1872. }
  1873. if (n->cast_to<EditorHelp>()) {
  1874. n->cast_to<EditorHelp>()->set_scroll(history[history_pos].scroll_pos);
  1875. n->cast_to<EditorHelp>()->set_focused();
  1876. }
  1877. n->set_meta("__editor_pass", ++edit_pass);
  1878. _update_script_names();
  1879. _update_history_arrows();
  1880. }
  1881. void ScriptEditor::_history_forward() {
  1882. if (history_pos < history.size() - 1) {
  1883. _update_history_pos(history_pos + 1);
  1884. }
  1885. }
  1886. void ScriptEditor::_history_back() {
  1887. if (history_pos > 0) {
  1888. _update_history_pos(history_pos - 1);
  1889. }
  1890. }
  1891. void ScriptEditor::set_scene_root_script(Ref<Script> p_script) {
  1892. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
  1893. if (bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
  1894. return;
  1895. if (open_dominant && p_script.is_valid() && _can_open_in_editor(p_script.ptr())) {
  1896. edit(p_script);
  1897. }
  1898. }
  1899. bool ScriptEditor::script_go_to_method(Ref<Script> p_script, const String &p_method) {
  1900. Vector<String> functions;
  1901. bool found = false;
  1902. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1903. ScriptTextEditor *current = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1904. if (current && current->get_edited_script() == p_script) {
  1905. functions = current->get_functions();
  1906. found = true;
  1907. break;
  1908. }
  1909. }
  1910. if (!found) {
  1911. String errortxt;
  1912. int line = -1, col;
  1913. String text = p_script->get_source_code();
  1914. List<String> fnc;
  1915. if (p_script->get_language()->validate(text, line, col, errortxt, p_script->get_path(), &fnc)) {
  1916. for (List<String>::Element *E = fnc.front(); E; E = E->next())
  1917. functions.push_back(E->get());
  1918. }
  1919. }
  1920. String method_search = p_method + ":";
  1921. for (int i = 0; i < functions.size(); i++) {
  1922. String function = functions[i];
  1923. if (function.begins_with(method_search)) {
  1924. edit(p_script);
  1925. int line = function.get_slice(":", 1).to_int();
  1926. _goto_script_line2(line - 1);
  1927. return true;
  1928. }
  1929. }
  1930. return false;
  1931. }
  1932. void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) {
  1933. auto_reload_running_scripts = p_enabled;
  1934. }
  1935. void ScriptEditor::_bind_methods() {
  1936. ObjectTypeDB::bind_method("_file_dialog_action", &ScriptEditor::_file_dialog_action);
  1937. ObjectTypeDB::bind_method("_tab_changed", &ScriptEditor::_tab_changed);
  1938. ObjectTypeDB::bind_method("_menu_option", &ScriptEditor::_menu_option);
  1939. ObjectTypeDB::bind_method("_close_current_tab", &ScriptEditor::_close_current_tab);
  1940. ObjectTypeDB::bind_method("_close_docs_tab", &ScriptEditor::_close_docs_tab);
  1941. ObjectTypeDB::bind_method("_editor_play", &ScriptEditor::_editor_play);
  1942. ObjectTypeDB::bind_method("_editor_pause", &ScriptEditor::_editor_pause);
  1943. ObjectTypeDB::bind_method("_editor_stop", &ScriptEditor::_editor_stop);
  1944. ObjectTypeDB::bind_method("_add_callback", &ScriptEditor::_add_callback);
  1945. ObjectTypeDB::bind_method("_reload_scripts", &ScriptEditor::_reload_scripts);
  1946. ObjectTypeDB::bind_method("_resave_scripts", &ScriptEditor::_resave_scripts);
  1947. ObjectTypeDB::bind_method("_res_saved_callback", &ScriptEditor::_res_saved_callback);
  1948. ObjectTypeDB::bind_method("_goto_script_line", &ScriptEditor::_goto_script_line);
  1949. ObjectTypeDB::bind_method("_goto_script_line2", &ScriptEditor::_goto_script_line2);
  1950. ObjectTypeDB::bind_method("_breakpoint_toggled", &ScriptEditor::_breakpoint_toggled);
  1951. ObjectTypeDB::bind_method("_breaked", &ScriptEditor::_breaked);
  1952. ObjectTypeDB::bind_method("_show_debugger", &ScriptEditor::_show_debugger);
  1953. ObjectTypeDB::bind_method("_get_debug_tooltip", &ScriptEditor::_get_debug_tooltip);
  1954. ObjectTypeDB::bind_method("_autosave_scripts", &ScriptEditor::_autosave_scripts);
  1955. ObjectTypeDB::bind_method("_editor_settings_changed", &ScriptEditor::_editor_settings_changed);
  1956. ObjectTypeDB::bind_method("_update_script_names", &ScriptEditor::_update_script_names);
  1957. ObjectTypeDB::bind_method("_tree_changed", &ScriptEditor::_tree_changed);
  1958. ObjectTypeDB::bind_method("_script_selected", &ScriptEditor::_script_selected);
  1959. ObjectTypeDB::bind_method("_script_created", &ScriptEditor::_script_created);
  1960. ObjectTypeDB::bind_method("_script_split_dragged", &ScriptEditor::_script_split_dragged);
  1961. ObjectTypeDB::bind_method("_help_class_open", &ScriptEditor::_help_class_open);
  1962. ObjectTypeDB::bind_method("_help_class_goto", &ScriptEditor::_help_class_goto);
  1963. ObjectTypeDB::bind_method("_request_help", &ScriptEditor::_help_class_open);
  1964. ObjectTypeDB::bind_method("_history_forward", &ScriptEditor::_history_forward);
  1965. ObjectTypeDB::bind_method("_history_back", &ScriptEditor::_history_back);
  1966. ObjectTypeDB::bind_method("_live_auto_reload_running_scripts", &ScriptEditor::_live_auto_reload_running_scripts);
  1967. ObjectTypeDB::bind_method("_unhandled_input", &ScriptEditor::_unhandled_input);
  1968. }
  1969. ScriptEditor::ScriptEditor(EditorNode *p_editor) {
  1970. current_theme = "";
  1971. completion_cache = memnew(EditorScriptCodeCompletionCache);
  1972. restoring_layout = false;
  1973. waiting_update_names = false;
  1974. pending_auto_reload = false;
  1975. auto_reload_running_scripts = false;
  1976. editor = p_editor;
  1977. menu_hb = memnew(HBoxContainer);
  1978. add_child(menu_hb);
  1979. script_split = memnew(HSplitContainer);
  1980. add_child(script_split);
  1981. script_split->set_v_size_flags(SIZE_EXPAND_FILL);
  1982. script_list = memnew(ItemList);
  1983. script_split->add_child(script_list);
  1984. script_list->set_custom_minimum_size(Size2(0, 0));
  1985. script_split->set_split_offset(140);
  1986. tab_container = memnew(TabContainer);
  1987. tab_container->set_tabs_visible(false);
  1988. script_split->add_child(tab_container);
  1989. tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
  1990. ED_SHORTCUT("script_editor/next_script", TTR("Next script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_GREATER);
  1991. ED_SHORTCUT("script_editor/prev_script", TTR("Previous script"), KEY_MASK_CMD | KEY_LESS);
  1992. set_process_unhandled_input(true);
  1993. file_menu = memnew(MenuButton);
  1994. menu_hb->add_child(file_menu);
  1995. file_menu->set_text(TTR("File"));
  1996. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTR("New")), FILE_NEW);
  1997. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open", TTR("Open")), FILE_OPEN);
  1998. file_menu->get_popup()->add_separator();
  1999. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save", TTR("Save"), KEY_MASK_ALT | KEY_MASK_CMD | KEY_S), FILE_SAVE);
  2000. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as", TTR("Save As..")), FILE_SAVE_AS);
  2001. 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);
  2002. file_menu->get_popup()->add_separator();
  2003. 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);
  2004. 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);
  2005. file_menu->get_popup()->add_separator();
  2006. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/import_theme", TTR("Import Theme")), FILE_IMPORT_THEME);
  2007. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_theme", TTR("Reload Theme")), FILE_RELOAD_THEME);
  2008. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme", TTR("Save Theme")), FILE_SAVE_THEME);
  2009. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as", TTR("Save Theme As")), FILE_SAVE_THEME_AS);
  2010. file_menu->get_popup()->add_separator();
  2011. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_docs", TTR("Close Docs")), CLOSE_DOCS);
  2012. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file", TTR("Close"), KEY_MASK_CMD | KEY_W), FILE_CLOSE);
  2013. file_menu->get_popup()->connect("item_pressed", this, "_menu_option");
  2014. edit_menu = memnew(MenuButton);
  2015. menu_hb->add_child(edit_menu);
  2016. edit_menu->set_text(TTR("Edit"));
  2017. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/undo", TTR("Undo"), KEY_MASK_CMD | KEY_Z), EDIT_UNDO);
  2018. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/redo", TTR("Redo"), KEY_MASK_CMD | KEY_Y), EDIT_REDO);
  2019. edit_menu->get_popup()->add_separator();
  2020. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/cut", TTR("Cut"), KEY_MASK_CMD | KEY_X), EDIT_CUT);
  2021. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy", TTR("Copy"), KEY_MASK_CMD | KEY_C), EDIT_COPY);
  2022. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/paste", TTR("Paste"), KEY_MASK_CMD | KEY_V), EDIT_PASTE);
  2023. edit_menu->get_popup()->add_separator();
  2024. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/select_all", TTR("Select All"), KEY_MASK_CMD | KEY_A), EDIT_SELECT_ALL);
  2025. edit_menu->get_popup()->add_separator();
  2026. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/move_up", TTR("Move Up"), KEY_MASK_ALT | KEY_UP), EDIT_MOVE_LINE_UP);
  2027. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/move_down", TTR("Move Down"), KEY_MASK_ALT | KEY_DOWN), EDIT_MOVE_LINE_DOWN);
  2028. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/indent_left", TTR("Indent Left"), KEY_MASK_ALT | KEY_LEFT), EDIT_INDENT_LEFT);
  2029. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/indent_right", TTR("Indent Right"), KEY_MASK_ALT | KEY_RIGHT), EDIT_INDENT_RIGHT);
  2030. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD | KEY_K), EDIT_TOGGLE_COMMENT);
  2031. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD | KEY_B), EDIT_CLONE_DOWN);
  2032. edit_menu->get_popup()->add_separator();
  2033. #ifdef OSX_ENABLED
  2034. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL | KEY_SPACE), EDIT_COMPLETE);
  2035. #else
  2036. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD | KEY_SPACE), EDIT_COMPLETE);
  2037. #endif
  2038. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CTRL | KEY_MASK_ALT | KEY_T), EDIT_TRIM_TRAILING_WHITESAPCE);
  2039. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD | KEY_I), EDIT_AUTO_INDENT);
  2040. edit_menu->get_popup()->connect("item_pressed", this, "_menu_option");
  2041. edit_menu->get_popup()->add_separator();
  2042. edit_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);
  2043. search_menu = memnew(MenuButton);
  2044. menu_hb->add_child(search_menu);
  2045. search_menu->set_text(TTR("Search"));
  2046. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F), SEARCH_FIND);
  2047. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
  2048. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT | KEY_F3), SEARCH_FIND_PREV);
  2049. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace", TTR("Replace.."), KEY_MASK_CMD | KEY_R), SEARCH_REPLACE);
  2050. search_menu->get_popup()->add_separator();
  2051. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_function", TTR("Goto Function.."), KEY_MASK_SHIFT | KEY_MASK_CMD | KEY_F), SEARCH_LOCATE_FUNCTION);
  2052. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD | KEY_L), SEARCH_GOTO_LINE);
  2053. search_menu->get_popup()->connect("item_pressed", this, "_menu_option");
  2054. script_search_menu = memnew(MenuButton);
  2055. menu_hb->add_child(script_search_menu);
  2056. script_search_menu->set_text(TTR("Search"));
  2057. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F), SEARCH_FIND);
  2058. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
  2059. script_search_menu->get_popup()->connect("item_pressed", this, "_menu_option");
  2060. script_search_menu->hide();
  2061. debug_menu = memnew(MenuButton);
  2062. menu_hb->add_child(debug_menu);
  2063. debug_menu->set_text(TTR("Debug"));
  2064. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9), DEBUG_TOGGLE_BREAKPOINT);
  2065. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_F9), DEBUG_REMOVE_ALL_BREAKPOINTS);
  2066. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_next_breakpoint", TTR("Goto Next Breakpoint"), KEY_MASK_CTRL | KEY_PERIOD), DEBUG_GOTO_NEXT_BREAKPOINT);
  2067. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_previous_breakpoint", TTR("Goto Previous Breakpoint"), KEY_MASK_CTRL | KEY_COMMA), DEBUG_GOTO_PREV_BREAKPOINT);
  2068. debug_menu->get_popup()->add_separator();
  2069. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_over", TTR("Step Over"), KEY_F10), DEBUG_NEXT);
  2070. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_into", TTR("Step Into"), KEY_F11), DEBUG_STEP);
  2071. debug_menu->get_popup()->add_separator();
  2072. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/break", TTR("Break")), DEBUG_BREAK);
  2073. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/continue", TTR("Continue")), DEBUG_CONTINUE);
  2074. debug_menu->get_popup()->add_separator();
  2075. //debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
  2076. debug_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("debugger/keep_debugger_open", TTR("Keep Debugger Open")), DEBUG_SHOW_KEEP_OPEN);
  2077. debug_menu->get_popup()->connect("item_pressed", this, "_menu_option");
  2078. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  2079. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  2080. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true);
  2081. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  2082. #if 0
  2083. window_menu = memnew( MenuButton );
  2084. menu_hb->add_child(window_menu);
  2085. window_menu->set_text(TTR("Window"));
  2086. window_menu->get_popup()->add_item(TTR("Close"),WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
  2087. window_menu->get_popup()->add_separator();
  2088. window_menu->get_popup()->add_item(TTR("Move Left"),WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
  2089. window_menu->get_popup()->add_item(TTR("Move Right"),WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
  2090. window_menu->get_popup()->add_separator();
  2091. window_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  2092. #endif
  2093. help_menu = memnew(MenuButton);
  2094. menu_hb->add_child(help_menu);
  2095. help_menu->set_text(TTR("Help"));
  2096. help_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/Contextual", TTR("Contextual Help"), KEY_MASK_SHIFT | KEY_F1), HELP_CONTEXTUAL);
  2097. help_menu->get_popup()->connect("item_pressed", this, "_menu_option");
  2098. menu_hb->add_spacer();
  2099. script_icon = memnew(TextureFrame);
  2100. menu_hb->add_child(script_icon);
  2101. script_name_label = memnew(Label);
  2102. menu_hb->add_child(script_name_label);
  2103. script_icon->hide();
  2104. script_name_label->hide();
  2105. menu_hb->add_spacer();
  2106. site_search = memnew(ToolButton);
  2107. site_search->set_text(TTR("Tutorials"));
  2108. site_search->connect("pressed", this, "_menu_option", varray(SEARCH_WEBSITE));
  2109. menu_hb->add_child(site_search);
  2110. site_search->set_tooltip(TTR("Open https://godotengine.org at tutorials section."));
  2111. class_search = memnew(ToolButton);
  2112. class_search->set_text(TTR("Classes"));
  2113. class_search->connect("pressed", this, "_menu_option", varray(SEARCH_CLASSES));
  2114. menu_hb->add_child(class_search);
  2115. class_search->set_tooltip(TTR("Search the class hierarchy."));
  2116. help_search = memnew(ToolButton);
  2117. help_search->set_text(TTR("Search Help"));
  2118. help_search->connect("pressed", this, "_menu_option", varray(SEARCH_HELP));
  2119. menu_hb->add_child(help_search);
  2120. help_search->set_tooltip(TTR("Search the reference documentation."));
  2121. menu_hb->add_child(memnew(VSeparator));
  2122. script_back = memnew(ToolButton);
  2123. script_back->connect("pressed", this, "_history_back");
  2124. menu_hb->add_child(script_back);
  2125. script_back->set_disabled(true);
  2126. script_back->set_tooltip(TTR("Go to previous edited document."));
  2127. script_forward = memnew(ToolButton);
  2128. script_forward->connect("pressed", this, "_history_forward");
  2129. menu_hb->add_child(script_forward);
  2130. script_forward->set_disabled(true);
  2131. script_forward->set_tooltip(TTR("Go to next edited document."));
  2132. tab_container->connect("tab_changed", this, "_tab_changed");
  2133. erase_tab_confirm = memnew(ConfirmationDialog);
  2134. add_child(erase_tab_confirm);
  2135. erase_tab_confirm->connect("confirmed", this, "_close_current_tab");
  2136. script_create_dialog = memnew(ScriptCreateDialog);
  2137. script_create_dialog->set_title(TTR("Create Script"));
  2138. add_child(script_create_dialog);
  2139. script_create_dialog->connect("script_created", this, "_script_created");
  2140. file_dialog_option = -1;
  2141. file_dialog = memnew(EditorFileDialog);
  2142. add_child(file_dialog);
  2143. file_dialog->connect("file_selected", this, "_file_dialog_action");
  2144. goto_line_dialog = memnew(GotoLineDialog);
  2145. add_child(goto_line_dialog);
  2146. debugger = memnew(ScriptEditorDebugger(editor));
  2147. debugger->connect("goto_script_line", this, "_goto_script_line");
  2148. debugger->connect("show_debugger", this, "_show_debugger");
  2149. disk_changed = memnew(ConfirmationDialog);
  2150. {
  2151. VBoxContainer *vbc = memnew(VBoxContainer);
  2152. disk_changed->add_child(vbc);
  2153. disk_changed->set_child_rect(vbc);
  2154. Label *dl = memnew(Label);
  2155. dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?:"));
  2156. vbc->add_child(dl);
  2157. disk_changed_list = memnew(Tree);
  2158. vbc->add_child(disk_changed_list);
  2159. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  2160. disk_changed->connect("confirmed", this, "_reload_scripts");
  2161. disk_changed->get_ok()->set_text(TTR("Reload"));
  2162. disk_changed->add_button(TTR("Resave"), !OS::get_singleton()->get_swap_ok_cancel(), "resave");
  2163. disk_changed->connect("custom_action", this, "_resave_scripts");
  2164. }
  2165. add_child(disk_changed);
  2166. script_editor = this;
  2167. quick_open = memnew(ScriptEditorQuickOpen);
  2168. add_child(quick_open);
  2169. quick_open->connect("goto_line", this, "_goto_script_line2");
  2170. Button *db = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Debugger"), debugger);
  2171. debugger->set_tool_button(db);
  2172. debugger->connect("breaked", this, "_breaked");
  2173. autosave_timer = memnew(Timer);
  2174. autosave_timer->set_one_shot(false);
  2175. add_child(autosave_timer);
  2176. grab_focus_block = false;
  2177. help_search_dialog = memnew(EditorHelpSearch);
  2178. add_child(help_search_dialog);
  2179. help_search_dialog->connect("go_to_help", this, "_help_class_goto");
  2180. help_index = memnew(EditorHelpIndex);
  2181. add_child(help_index);
  2182. help_index->connect("open_class", this, "_help_class_open");
  2183. history_pos = -1;
  2184. // debugger_gui->hide();
  2185. edit_pass = 0;
  2186. trim_trailing_whitespace_on_save = false;
  2187. }
  2188. ScriptEditor::~ScriptEditor() {
  2189. memdelete(completion_cache);
  2190. }
  2191. void ScriptEditorPlugin::edit(Object *p_object) {
  2192. if (!p_object->cast_to<Script>())
  2193. return;
  2194. script_editor->edit(p_object->cast_to<Script>());
  2195. }
  2196. bool ScriptEditorPlugin::handles(Object *p_object) const {
  2197. if (p_object->cast_to<Script>()) {
  2198. bool valid = _can_open_in_editor(p_object->cast_to<Script>());
  2199. if (!valid) { //user tried to open it by clicking
  2200. EditorNode::get_singleton()->show_warning(TTR("Built-in scripts can only be edited when the scene they belong to is loaded"));
  2201. }
  2202. return valid;
  2203. }
  2204. return p_object->is_type("Script");
  2205. }
  2206. void ScriptEditorPlugin::make_visible(bool p_visible) {
  2207. if (p_visible) {
  2208. script_editor->show();
  2209. script_editor->set_process(true);
  2210. script_editor->ensure_select_current();
  2211. } else {
  2212. script_editor->hide();
  2213. script_editor->set_process(false);
  2214. }
  2215. }
  2216. void ScriptEditorPlugin::selected_notify() {
  2217. script_editor->ensure_select_current();
  2218. }
  2219. Dictionary ScriptEditorPlugin::get_state() const {
  2220. return script_editor->get_state();
  2221. }
  2222. void ScriptEditorPlugin::set_state(const Dictionary &p_state) {
  2223. script_editor->set_state(p_state);
  2224. }
  2225. void ScriptEditorPlugin::clear() {
  2226. script_editor->clear();
  2227. }
  2228. void ScriptEditorPlugin::save_external_data() {
  2229. script_editor->save_all_scripts();
  2230. }
  2231. void ScriptEditorPlugin::apply_changes() {
  2232. script_editor->apply_scripts();
  2233. }
  2234. void ScriptEditorPlugin::restore_global_state() {
  2235. }
  2236. void ScriptEditorPlugin::save_global_state() {
  2237. }
  2238. void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  2239. script_editor->set_window_layout(p_layout);
  2240. }
  2241. void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
  2242. script_editor->get_window_layout(p_layout);
  2243. }
  2244. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  2245. return script_editor->get_breakpoints(p_breakpoints);
  2246. }
  2247. void ScriptEditorPlugin::edited_scene_changed() {
  2248. script_editor->edited_scene_changed();
  2249. }
  2250. ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
  2251. editor = p_node;
  2252. script_editor = memnew(ScriptEditor(p_node));
  2253. editor->get_viewport()->add_child(script_editor);
  2254. script_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2255. script_editor->hide();
  2256. EDITOR_DEF("text_editor/auto_reload_scripts_on_external_change", true);
  2257. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save", true));
  2258. EDITOR_DEF("text_editor/open_dominant_script_on_scene_change", true);
  2259. EDITOR_DEF("external_editor/use_external_editor", false);
  2260. EDITOR_DEF("external_editor/exec_path", "");
  2261. EDITOR_DEF("text_editor/script_temperature_enabled", true);
  2262. EDITOR_DEF("text_editor/highlight_current_script", true);
  2263. EDITOR_DEF("text_editor/script_temperature_history_size", 15);
  2264. EDITOR_DEF("text_editor/script_temperature_hot_color", Color(1, 0, 0, 0.3));
  2265. EDITOR_DEF("text_editor/script_temperature_cold_color", Color(0, 0, 1, 0.3));
  2266. EDITOR_DEF("text_editor/current_script_background_color", Color(0.81, 0.81, 0.14, 0.63));
  2267. EDITOR_DEF("text_editor/group_help_pages", true);
  2268. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/sort_scripts_by", PROPERTY_HINT_ENUM, "Name,Path"));
  2269. EDITOR_DEF("text_editor/sort_scripts_by", 0);
  2270. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/list_script_names_as", PROPERTY_HINT_ENUM, "Name,Parent Directory And Name,Full Path"));
  2271. EDITOR_DEF("text_editor/list_script_names_as", 0);
  2272. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "external_editor/exec_path", PROPERTY_HINT_GLOBAL_FILE));
  2273. EDITOR_DEF("external_editor/exec_flags", "");
  2274. }
  2275. ScriptEditorPlugin::~ScriptEditorPlugin() {
  2276. }