script_editor_plugin.cpp 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133
  1. /*************************************************************************/
  2. /* script_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "script_editor_plugin.h"
  30. #include "tools/editor/editor_settings.h"
  31. #include "io/resource_loader.h"
  32. #include "io/resource_saver.h"
  33. #include "os/keyboard.h"
  34. #include "os/os.h"
  35. #include "tools/editor/editor_node.h"
  36. #include "tools/editor/script_editor_debugger.h"
  37. #include "globals.h"
  38. #include "os/file_access.h"
  39. #include "scene/main/viewport.h"
  40. #include "os/keyboard.h"
  41. #include "os/input.h"
  42. /*** SCRIPT EDITOR ****/
  43. static bool _can_open_in_editor(Script* p_script) {
  44. String path = p_script->get_path();
  45. if (path.find("::")!=-1) {
  46. //refuse handling this if it can't be edited
  47. bool valid=false;
  48. for(int i=0;i<EditorNode::get_singleton()->get_editor_data().get_edited_scene_count();i++) {
  49. if (path.begins_with(EditorNode::get_singleton()->get_editor_data().get_scene_path(i))) {
  50. valid=true;
  51. break;
  52. }
  53. }
  54. return valid;
  55. }
  56. return true;
  57. }
  58. class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache {
  59. struct Cache {
  60. uint64_t time_loaded;
  61. RES cache;
  62. };
  63. Map<String,Cache> cached;
  64. public:
  65. uint64_t max_time_cache;
  66. int max_cache_size;
  67. void cleanup() {
  68. List< Map<String,Cache>::Element * > to_clean;
  69. Map<String,Cache>::Element *I=cached.front();
  70. while(I) {
  71. if ((OS::get_singleton()->get_ticks_msec()-I->get().time_loaded)>max_time_cache) {
  72. to_clean.push_back(I);
  73. }
  74. I=I->next();
  75. }
  76. while(to_clean.front()) {
  77. cached.erase(to_clean.front()->get());
  78. to_clean.pop_front();
  79. }
  80. }
  81. RES get_cached_resource(const String& p_path) {
  82. Map<String,Cache>::Element *E=cached.find(p_path);
  83. if (!E) {
  84. Cache c;
  85. c.cache=ResourceLoader::load(p_path);
  86. E=cached.insert(p_path,c);
  87. }
  88. E->get().time_loaded=OS::get_singleton()->get_ticks_msec();
  89. if (cached.size()>max_cache_size) {
  90. uint64_t older;
  91. Map<String,Cache>::Element *O=cached.front();
  92. older=O->get().time_loaded;
  93. Map<String,Cache>::Element *I=O;
  94. while(I) {
  95. if (I->get().time_loaded<older) {
  96. older = I->get().time_loaded;
  97. O=I;
  98. }
  99. I=I->next();
  100. }
  101. if (O!=E) {//should never heppane..
  102. cached.erase(O);
  103. }
  104. }
  105. return E->get().cache;
  106. }
  107. EditorScriptCodeCompletionCache() {
  108. max_cache_size=128;
  109. max_time_cache=5*60*1000; //minutes, five
  110. }
  111. };
  112. #define SORT_SCRIPT_LIST
  113. void ScriptEditorQuickOpen::popup(const Vector<String>& p_functions, bool p_dontclear) {
  114. popup_centered_ratio(0.6);
  115. if (p_dontclear)
  116. search_box->select_all();
  117. else
  118. search_box->clear();
  119. search_box->grab_focus();
  120. functions=p_functions;
  121. _update_search();
  122. }
  123. void ScriptEditorQuickOpen::_text_changed(const String& p_newtext) {
  124. _update_search();
  125. }
  126. void ScriptEditorQuickOpen::_sbox_input(const InputEvent& p_ie) {
  127. if (p_ie.type==InputEvent::KEY && (
  128. p_ie.key.scancode == KEY_UP ||
  129. p_ie.key.scancode == KEY_DOWN ||
  130. p_ie.key.scancode == KEY_PAGEUP ||
  131. p_ie.key.scancode == KEY_PAGEDOWN ) ) {
  132. search_options->call("_input_event",p_ie);
  133. search_box->accept_event();
  134. }
  135. }
  136. void ScriptEditorQuickOpen::_update_search() {
  137. search_options->clear();
  138. TreeItem *root = search_options->create_item();
  139. for(int i=0;i<functions.size();i++) {
  140. String file = functions[i];
  141. if ((search_box->get_text()=="" || file.findn(search_box->get_text())!=-1)) {
  142. TreeItem *ti = search_options->create_item(root);
  143. ti->set_text(0,file);
  144. if (root->get_children()==ti)
  145. ti->select(0);
  146. }
  147. }
  148. get_ok()->set_disabled(root->get_children()==NULL);
  149. }
  150. void ScriptEditorQuickOpen::_confirmed() {
  151. TreeItem *ti = search_options->get_selected();
  152. if (!ti)
  153. return;
  154. int line = ti->get_text(0).get_slice(":",1).to_int();
  155. emit_signal("goto_line",line-1);
  156. hide();
  157. }
  158. void ScriptEditorQuickOpen::_notification(int p_what) {
  159. if (p_what==NOTIFICATION_ENTER_TREE) {
  160. connect("confirmed",this,"_confirmed");
  161. }
  162. }
  163. void ScriptEditorQuickOpen::_bind_methods() {
  164. ObjectTypeDB::bind_method(_MD("_text_changed"),&ScriptEditorQuickOpen::_text_changed);
  165. ObjectTypeDB::bind_method(_MD("_confirmed"),&ScriptEditorQuickOpen::_confirmed);
  166. ObjectTypeDB::bind_method(_MD("_sbox_input"),&ScriptEditorQuickOpen::_sbox_input);
  167. ADD_SIGNAL(MethodInfo("goto_line",PropertyInfo(Variant::INT,"line")));
  168. }
  169. ScriptEditorQuickOpen::ScriptEditorQuickOpen() {
  170. VBoxContainer *vbc = memnew( VBoxContainer );
  171. add_child(vbc);
  172. set_child_rect(vbc);
  173. search_box = memnew( LineEdit );
  174. vbc->add_margin_child(TTR("Search:"),search_box);
  175. search_box->connect("text_changed",this,"_text_changed");
  176. search_box->connect("input_event",this,"_sbox_input");
  177. search_options = memnew( Tree );
  178. vbc->add_margin_child(TTR("Matches:"),search_options,true);
  179. get_ok()->set_text(TTR("Open"));
  180. get_ok()->set_disabled(true);
  181. register_text_enter(search_box);
  182. set_hide_on_ok(false);
  183. search_options->connect("item_activated",this,"_confirmed");
  184. search_options->set_hide_root(true);
  185. }
  186. /////////////////////////////////
  187. ScriptEditor *ScriptEditor::script_editor=NULL;
  188. Vector<String> ScriptTextEditor::get_functions() {
  189. String errortxt;
  190. int line=-1,col;
  191. TextEdit *te=get_text_edit();
  192. String text = te->get_text();
  193. List<String> fnc;
  194. if (script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  195. //if valid rewrite functions to latest
  196. functions.clear();
  197. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  198. functions.push_back(E->get());
  199. }
  200. }
  201. return functions;
  202. }
  203. void ScriptTextEditor::apply_code() {
  204. if (script.is_null())
  205. return;
  206. // print_line("applying code");
  207. script->set_source_code(get_text_edit()->get_text());
  208. script->update_exports();
  209. }
  210. Ref<Script> ScriptTextEditor::get_edited_script() const {
  211. return script;
  212. }
  213. void ScriptTextEditor::_load_theme_settings() {
  214. get_text_edit()->clear_colors();
  215. /* keyword color */
  216. get_text_edit()->set_custom_bg_color(EDITOR_DEF("text_editor/background_color",Color(0,0,0,0)));
  217. get_text_edit()->add_color_override("completion_background_color", EDITOR_DEF("text_editor/completion_background_color", Color(0,0,0,0)));
  218. get_text_edit()->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/completion_selected_color", Color::html("434244")));
  219. get_text_edit()->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/completion_existing_color", Color::html("21dfdfdf")));
  220. get_text_edit()->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/completion_scroll_color", Color::html("ffffff")));
  221. get_text_edit()->add_color_override("completion_font_color", EDITOR_DEF("text_editor/completion_font_color", Color::html("aaaaaa")));
  222. get_text_edit()->add_color_override("font_color",EDITOR_DEF("text_editor/text_color",Color(0,0,0)));
  223. get_text_edit()->add_color_override("line_number_color",EDITOR_DEF("text_editor/line_number_color",Color(0,0,0)));
  224. get_text_edit()->add_color_override("caret_color",EDITOR_DEF("text_editor/caret_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. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug));
  450. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug) );
  451. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked );
  452. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked );
  453. }
  454. void ScriptEditor::_show_debugger(bool p_show) {
  455. // debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), p_show);
  456. }
  457. void ScriptEditor::_script_created(Ref<Script> p_script) {
  458. editor->push_item(p_script.operator->());
  459. }
  460. void ScriptEditor::_trim_trailing_whitespace(TextEdit *tx) {
  461. bool trimed_whitespace = false;
  462. for (int i = 0; i < tx->get_line_count(); i++) {
  463. String line = tx->get_line(i);
  464. if (line.ends_with(" ") || line.ends_with("\t")) {
  465. if (!trimed_whitespace) {
  466. tx->begin_complex_operation();
  467. trimed_whitespace = true;
  468. }
  469. int end = 0;
  470. for (int j = line.length() - 1; j > -1; j--) {
  471. if (line[j] != ' ' && line[j] != '\t') {
  472. end = j+1;
  473. break;
  474. }
  475. }
  476. tx->set_line(i, line.substr(0, end));
  477. }
  478. }
  479. if (trimed_whitespace) {
  480. tx->end_complex_operation();
  481. tx->update();
  482. }
  483. }
  484. void ScriptEditor::_goto_script_line2(int p_line) {
  485. int selected = tab_container->get_current_tab();
  486. if (selected<0 || selected>=tab_container->get_child_count())
  487. return;
  488. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  489. if (!current)
  490. return;
  491. current->get_text_edit()->cursor_set_line(p_line);
  492. }
  493. void ScriptEditor::_goto_script_line(REF p_script,int p_line) {
  494. editor->push_item(p_script.ptr());
  495. _goto_script_line2(p_line);
  496. }
  497. void ScriptEditor::_update_history_arrows() {
  498. script_back->set_disabled( history_pos<=0 );
  499. script_forward->set_disabled( history_pos>=history.size()-1 );
  500. }
  501. void ScriptEditor::_go_to_tab(int p_idx) {
  502. Node *cn = tab_container->get_child(p_idx);
  503. if (!cn)
  504. return;
  505. Control *c = cn->cast_to<Control>();
  506. if (!c)
  507. return;
  508. if (history_pos>=0 && history_pos<history.size() && history[history_pos].control==tab_container->get_current_tab_control()) {
  509. Node *n = tab_container->get_current_tab_control();
  510. if (n->cast_to<ScriptTextEditor>()) {
  511. history[history_pos].scroll_pos=n->cast_to<ScriptTextEditor>()->get_text_edit()->get_v_scroll();
  512. history[history_pos].cursor_column=n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_column();
  513. history[history_pos].cursor_row=n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_line();
  514. }
  515. if (n->cast_to<EditorHelp>()) {
  516. history[history_pos].scroll_pos=n->cast_to<EditorHelp>()->get_scroll();
  517. }
  518. }
  519. history.resize(history_pos+1);
  520. ScriptHistory sh;
  521. sh.control=c;
  522. sh.scroll_pos=0;
  523. history.push_back(sh);
  524. history_pos++;
  525. tab_container->set_current_tab(p_idx);
  526. c = tab_container->get_current_tab_control();
  527. if (c->cast_to<ScriptTextEditor>()) {
  528. script_name_label->set_text(c->cast_to<ScriptTextEditor>()->get_name());
  529. script_icon->set_texture(c->cast_to<ScriptTextEditor>()->get_icon());
  530. if (is_visible())
  531. c->cast_to<ScriptTextEditor>()->get_text_edit()->grab_focus();
  532. }
  533. if (c->cast_to<EditorHelp>()) {
  534. script_name_label->set_text(c->cast_to<EditorHelp>()->get_class_name());
  535. script_icon->set_texture(get_icon("Help","EditorIcons"));
  536. if (is_visible())
  537. c->cast_to<EditorHelp>()->set_focused();
  538. }
  539. c->set_meta("__editor_pass",++edit_pass);
  540. _update_history_arrows();
  541. _update_script_colors();
  542. }
  543. void ScriptEditor::_close_tab(int p_idx) {
  544. int selected = p_idx;
  545. if (selected<0 || selected>=tab_container->get_child_count())
  546. return;
  547. Node *tselected = tab_container->get_child(selected);
  548. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  549. if (current) {
  550. apply_scripts();
  551. }
  552. //remove from history
  553. history.resize(history_pos+1);
  554. for(int i=0;i<history.size();i++) {
  555. if (history[i].control==tselected) {
  556. history.remove(i);
  557. i--;
  558. history_pos--;
  559. }
  560. }
  561. if (history_pos>=history.size()) {
  562. history_pos=history.size()-1;
  563. }
  564. int idx = tab_container->get_current_tab();
  565. memdelete(tselected);
  566. if (idx>=tab_container->get_child_count())
  567. idx=tab_container->get_child_count()-1;
  568. if (idx>=0) {
  569. if (history_pos>=0) {
  570. idx = history[history_pos].control->get_index();
  571. }
  572. tab_container->set_current_tab(idx);
  573. //script_list->select(idx);
  574. }
  575. _update_history_arrows();
  576. _update_script_names();
  577. EditorNode::get_singleton()->save_layout();
  578. }
  579. void ScriptEditor::_close_current_tab() {
  580. _close_tab(tab_container->get_current_tab());
  581. }
  582. void ScriptEditor::_close_docs_tab() {
  583. int child_count = tab_container->get_child_count();
  584. for (int i = child_count-1; i>=0; i--) {
  585. EditorHelp *ste = tab_container->get_child(i)->cast_to<EditorHelp>();
  586. if (ste) {
  587. _close_tab(i);
  588. }
  589. }
  590. }
  591. void ScriptEditor::_resave_scripts(const String& p_str) {
  592. apply_scripts();
  593. for(int i=0;i<tab_container->get_child_count();i++) {
  594. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  595. if (!ste)
  596. continue;
  597. Ref<Script> script = ste->get_edited_script();
  598. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  599. continue; //internal script, who cares
  600. if (trim_trailing_whitespace_on_save) {
  601. _trim_trailing_whitespace(ste->get_text_edit());
  602. }
  603. editor->save_resource(script);
  604. ste->get_text_edit()->tag_saved_version();
  605. }
  606. disk_changed->hide();
  607. }
  608. void ScriptEditor::_reload_scripts(){
  609. for(int i=0;i<tab_container->get_child_count();i++) {
  610. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  611. if (!ste) {
  612. continue;
  613. }
  614. Ref<Script> script = ste->get_edited_script();
  615. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  616. continue; //internal script, who cares
  617. }
  618. uint64_t last_date = script->get_last_modified_time();
  619. uint64_t date = FileAccess::get_modified_time(script->get_path());
  620. //printf("last date: %lli vs date: %lli\n",last_date,date);
  621. if (last_date==date) {
  622. continue;
  623. }
  624. Ref<Script> rel_script = ResourceLoader::load(script->get_path(),script->get_type(),true);
  625. ERR_CONTINUE(!rel_script.is_valid());
  626. script->set_source_code( rel_script->get_source_code() );
  627. script->set_last_modified_time( rel_script->get_last_modified_time() );
  628. script->reload();
  629. ste->reload_text();
  630. }
  631. disk_changed->hide();
  632. _update_script_names();
  633. }
  634. void ScriptEditor::_res_saved_callback(const Ref<Resource>& p_res) {
  635. for(int i=0;i<tab_container->get_child_count();i++) {
  636. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  637. if (!ste) {
  638. continue;
  639. }
  640. Ref<Script> script = ste->get_edited_script();
  641. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  642. continue; //internal script, who cares
  643. }
  644. if (script==p_res) {
  645. ste->get_text_edit()->tag_saved_version();
  646. }
  647. }
  648. _update_script_names();
  649. if (!pending_auto_reload && auto_reload_running_scripts) {
  650. call_deferred("_live_auto_reload_running_scripts");
  651. pending_auto_reload=true;
  652. }
  653. }
  654. void ScriptEditor::_live_auto_reload_running_scripts() {
  655. pending_auto_reload=false;
  656. debugger->reload_scripts();
  657. }
  658. bool ScriptEditor::_test_script_times_on_disk(Ref<Script> p_for_script) {
  659. disk_changed_list->clear();
  660. TreeItem *r = disk_changed_list->create_item();
  661. disk_changed_list->set_hide_root(true);
  662. bool need_ask=false;
  663. bool need_reload=false;
  664. bool use_autoreload=bool(EDITOR_DEF("text_editor/auto_reload_scripts_on_external_change",false));
  665. for(int i=0;i<tab_container->get_child_count();i++) {
  666. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  667. if (ste) {
  668. Ref<Script> script = ste->get_edited_script();
  669. if (p_for_script.is_valid() && p_for_script!=script)
  670. continue;
  671. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  672. continue; //internal script, who cares
  673. uint64_t last_date = script->get_last_modified_time();
  674. uint64_t date = FileAccess::get_modified_time(script->get_path());
  675. //printf("last date: %lli vs date: %lli\n",last_date,date);
  676. if (last_date!=date) {
  677. TreeItem *ti = disk_changed_list->create_item(r);
  678. ti->set_text(0,script->get_path().get_file());
  679. if (!use_autoreload || ste->is_unsaved()) {
  680. need_ask=true;
  681. }
  682. need_reload=true;
  683. //r->set_metadata(0,);
  684. }
  685. }
  686. }
  687. if (need_reload) {
  688. if (!need_ask) {
  689. script_editor->_reload_scripts();
  690. need_reload=false;
  691. } else {
  692. disk_changed->call_deferred("popup_centered_ratio",0.5);
  693. }
  694. }
  695. return need_reload;
  696. }
  697. void ScriptEditor::swap_lines(TextEdit *tx, int line1, int line2)
  698. {
  699. String tmp = tx->get_line(line1);
  700. String tmp2 = tx->get_line(line2);
  701. tx->set_line(line2, tmp);
  702. tx->set_line(line1, tmp2);
  703. tx->cursor_set_line(line2);
  704. }
  705. void ScriptEditor::_file_dialog_action(String p_file) {
  706. switch (file_dialog_option) {
  707. case FILE_SAVE_THEME_AS: {
  708. if(!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) {
  709. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  710. }
  711. } break;
  712. case FILE_IMPORT_THEME: {
  713. if(!EditorSettings::get_singleton()->import_text_editor_theme(p_file)) {
  714. editor->show_warning(TTR("Error importing theme"), TTR("Error importing"));
  715. }
  716. } break;
  717. }
  718. file_dialog_option = -1;
  719. }
  720. void ScriptEditor::_menu_option(int p_option) {
  721. switch(p_option) {
  722. case FILE_NEW: {
  723. script_create_dialog->config("Node", ".gd");
  724. script_create_dialog->popup_centered(Size2(300, 300)*EDSCALE);
  725. } break;
  726. case FILE_OPEN: {
  727. editor->open_resource("Script");
  728. return;
  729. } break;
  730. case FILE_SAVE_ALL: {
  731. if (!_test_script_times_on_disk())
  732. return;
  733. save_all_scripts();
  734. #if 0
  735. for(int i=0;i<tab_container->get_child_count();i++) {
  736. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  737. if (!ste)
  738. continue;
  739. Ref<Script> script = ste->get_edited_script();
  740. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
  741. continue; //internal script, who cares
  742. editor->save_resource( script );
  743. }
  744. #endif
  745. } break;
  746. case FILE_IMPORT_THEME: {
  747. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  748. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  749. file_dialog_option = FILE_IMPORT_THEME;
  750. file_dialog->clear_filters();
  751. file_dialog->add_filter("*.tet");
  752. file_dialog->popup_centered_ratio();
  753. file_dialog->set_title(TTR("Import Theme"));
  754. } break;
  755. case FILE_RELOAD_THEME: {
  756. EditorSettings::get_singleton()->load_text_editor_theme();
  757. } break;
  758. case FILE_SAVE_THEME: {
  759. if(!EditorSettings::get_singleton()->save_text_editor_theme()) {
  760. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  761. }
  762. } break;
  763. case FILE_SAVE_THEME_AS: {
  764. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  765. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  766. file_dialog_option = FILE_SAVE_THEME_AS;
  767. file_dialog->clear_filters();
  768. file_dialog->add_filter("*.tet");
  769. file_dialog->set_current_path(EditorSettings::get_singleton()->get_settings_path() + "/text_editor_themes/" + EditorSettings::get_singleton()->get("text_editor/color_theme"));
  770. file_dialog->popup_centered_ratio();
  771. file_dialog->set_title(TTR("Save Theme As.."));
  772. } break;
  773. case SEARCH_HELP: {
  774. help_search_dialog->popup();
  775. } break;
  776. case SEARCH_CLASSES: {
  777. String current;
  778. if (tab_container->get_tab_count()>0) {
  779. EditorHelp *eh = tab_container->get_child( tab_container->get_current_tab() )->cast_to<EditorHelp>();
  780. if (eh) {
  781. current=eh->get_class_name();
  782. }
  783. }
  784. help_index->popup();
  785. if (current!="") {
  786. help_index->call_deferred("select_class",current);
  787. }
  788. } break;
  789. case SEARCH_WEBSITE: {
  790. OS::get_singleton()->shell_open("http://docs.godotengine.org/");
  791. } break;
  792. case WINDOW_NEXT: {
  793. _history_forward();
  794. } break;
  795. case WINDOW_PREV: {
  796. _history_back();
  797. } break;
  798. case DEBUG_SHOW: {
  799. if (debugger) {
  800. bool visible = debug_menu->get_popup()->is_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW) );
  801. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), !visible);
  802. if (visible)
  803. debugger->hide();
  804. else
  805. debugger->show();
  806. }
  807. } break;
  808. case DEBUG_SHOW_KEEP_OPEN: {
  809. bool visible = debug_menu->get_popup()->is_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN) );
  810. if (debugger)
  811. debugger->set_hide_on_stop(visible);
  812. debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN), !visible);
  813. } break;
  814. }
  815. int selected = tab_container->get_current_tab();
  816. if (selected<0 || selected>=tab_container->get_child_count())
  817. return;
  818. ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
  819. if (current) {
  820. switch(p_option) {
  821. case FILE_NEW: {
  822. script_create_dialog->config("Node", ".gd");
  823. script_create_dialog->popup_centered(Size2(300, 300)*EDSCALE);
  824. } break;
  825. case FILE_SAVE: {
  826. if (_test_script_times_on_disk())
  827. return;
  828. if (trim_trailing_whitespace_on_save) {
  829. _trim_trailing_whitespace(current->get_text_edit());
  830. }
  831. editor->save_resource( current->get_edited_script() );
  832. } break;
  833. case FILE_SAVE_AS: {
  834. if (trim_trailing_whitespace_on_save) {
  835. _trim_trailing_whitespace(current->get_text_edit());
  836. }
  837. editor->push_item(current->get_edited_script()->cast_to<Object>());
  838. editor->save_resource_as( current->get_edited_script() );
  839. } break;
  840. case EDIT_UNDO: {
  841. current->get_text_edit()->undo();
  842. current->get_text_edit()->call_deferred("grab_focus");
  843. } break;
  844. case EDIT_REDO: {
  845. current->get_text_edit()->redo();
  846. current->get_text_edit()->call_deferred("grab_focus");
  847. } break;
  848. case EDIT_CUT: {
  849. current->get_text_edit()->cut();
  850. current->get_text_edit()->call_deferred("grab_focus");
  851. } break;
  852. case EDIT_COPY: {
  853. current->get_text_edit()->copy();
  854. current->get_text_edit()->call_deferred("grab_focus");
  855. } break;
  856. case EDIT_PASTE: {
  857. current->get_text_edit()->paste();
  858. current->get_text_edit()->call_deferred("grab_focus");
  859. } break;
  860. case EDIT_SELECT_ALL: {
  861. current->get_text_edit()->select_all();
  862. current->get_text_edit()->call_deferred("grab_focus");
  863. } break;
  864. case EDIT_MOVE_LINE_UP: {
  865. TextEdit *tx = current->get_text_edit();
  866. Ref<Script> scr = current->get_edited_script();
  867. if (scr.is_null())
  868. return;
  869. tx->begin_complex_operation();
  870. if (tx->is_selection_active())
  871. {
  872. int from_line = tx->get_selection_from_line();
  873. int from_col = tx->get_selection_from_column();
  874. int to_line = tx->get_selection_to_line();
  875. int to_column = tx->get_selection_to_column();
  876. for (int i = from_line; i <= to_line; i++)
  877. {
  878. int line_id = i;
  879. int next_id = i - 1;
  880. if (line_id == 0 || next_id < 0)
  881. return;
  882. swap_lines(tx, line_id, next_id);
  883. }
  884. int from_line_up = from_line > 0 ? from_line-1 : from_line;
  885. int to_line_up = to_line > 0 ? to_line-1 : to_line;
  886. tx->select(from_line_up, from_col, to_line_up, to_column);
  887. }
  888. else
  889. {
  890. int line_id = tx->cursor_get_line();
  891. int next_id = line_id - 1;
  892. if (line_id == 0 || next_id < 0)
  893. return;
  894. swap_lines(tx, line_id, next_id);
  895. }
  896. tx->end_complex_operation();
  897. tx->update();
  898. } break;
  899. case EDIT_MOVE_LINE_DOWN: {
  900. TextEdit *tx = current->get_text_edit();
  901. Ref<Script> scr = current->get_edited_script();
  902. if (scr.is_null())
  903. return;
  904. tx->begin_complex_operation();
  905. if (tx->is_selection_active())
  906. {
  907. int from_line = tx->get_selection_from_line();
  908. int from_col = tx->get_selection_from_column();
  909. int to_line = tx->get_selection_to_line();
  910. int to_column = tx->get_selection_to_column();
  911. for (int i = to_line; i >= from_line; i--)
  912. {
  913. int line_id = i;
  914. int next_id = i + 1;
  915. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  916. return;
  917. swap_lines(tx, line_id, next_id);
  918. }
  919. int from_line_down = from_line < tx->get_line_count() ? from_line+1 : from_line;
  920. int to_line_down = to_line < tx->get_line_count() ? to_line+1 : to_line;
  921. tx->select(from_line_down, from_col, to_line_down, to_column);
  922. }
  923. else
  924. {
  925. int line_id = tx->cursor_get_line();
  926. int next_id = line_id + 1;
  927. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  928. return;
  929. swap_lines(tx, line_id, next_id);
  930. }
  931. tx->end_complex_operation();
  932. tx->update();
  933. } break;
  934. case EDIT_INDENT_LEFT: {
  935. TextEdit *tx = current->get_text_edit();
  936. Ref<Script> scr = current->get_edited_script();
  937. if (scr.is_null())
  938. return;
  939. tx->begin_complex_operation();
  940. if (tx->is_selection_active())
  941. {
  942. tx->indent_selection_left();
  943. }
  944. else
  945. {
  946. int begin = tx->cursor_get_line();
  947. String line_text = tx->get_line(begin);
  948. // begins with tab
  949. if (line_text.begins_with("\t"))
  950. {
  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. {
  957. line_text = line_text.substr(4, line_text.length());
  958. tx->set_line(begin, line_text);
  959. }
  960. }
  961. tx->end_complex_operation();
  962. tx->update();
  963. //tx->deselect();
  964. } break;
  965. case EDIT_INDENT_RIGHT: {
  966. TextEdit *tx = current->get_text_edit();
  967. Ref<Script> scr = current->get_edited_script();
  968. if (scr.is_null())
  969. return;
  970. tx->begin_complex_operation();
  971. if (tx->is_selection_active())
  972. {
  973. tx->indent_selection_right();
  974. }
  975. else
  976. {
  977. int begin = tx->cursor_get_line();
  978. String line_text = tx->get_line(begin);
  979. line_text = '\t' + line_text;
  980. tx->set_line(begin, line_text);
  981. }
  982. tx->end_complex_operation();
  983. tx->update();
  984. //tx->deselect();
  985. } break;
  986. case EDIT_CLONE_DOWN: {
  987. TextEdit *tx = current->get_text_edit();
  988. Ref<Script> scr = current->get_edited_script();
  989. if (scr.is_null())
  990. return;
  991. int from_line = tx->cursor_get_line();
  992. int to_line = tx->cursor_get_line();
  993. int column = tx->cursor_get_column();
  994. if (tx->is_selection_active()) {
  995. from_line = tx->get_selection_from_line();
  996. to_line = tx->get_selection_to_line();
  997. column = tx->cursor_get_column();
  998. }
  999. int next_line = to_line + 1;
  1000. tx->begin_complex_operation();
  1001. for (int i = from_line; i <= to_line; i++) {
  1002. if (i >= tx->get_line_count() - 1) {
  1003. tx->set_line(i, tx->get_line(i) + "\n");
  1004. }
  1005. String line_clone = tx->get_line(i);
  1006. tx->insert_at(line_clone, next_line);
  1007. next_line++;
  1008. }
  1009. tx->cursor_set_column(column);
  1010. if (tx->is_selection_active()) {
  1011. tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
  1012. }
  1013. tx->end_complex_operation();
  1014. tx->update();
  1015. } break;
  1016. case EDIT_TOGGLE_COMMENT: {
  1017. TextEdit *tx = current->get_text_edit();
  1018. Ref<Script> scr = current->get_edited_script();
  1019. if (scr.is_null())
  1020. return;
  1021. tx->begin_complex_operation();
  1022. if (tx->is_selection_active())
  1023. {
  1024. int begin = tx->get_selection_from_line();
  1025. int end = tx->get_selection_to_line();
  1026. // End of selection ends on the first column of the last line, ignore it.
  1027. if(tx->get_selection_to_column() == 0)
  1028. end -= 1;
  1029. for (int i = begin; i <= end; i++)
  1030. {
  1031. String line_text = tx->get_line(i);
  1032. if (line_text.begins_with("#"))
  1033. line_text = line_text.substr(1, line_text.length());
  1034. else
  1035. line_text = "#" + line_text;
  1036. tx->set_line(i, line_text);
  1037. }
  1038. }
  1039. else
  1040. {
  1041. int begin = tx->cursor_get_line();
  1042. String line_text = tx->get_line(begin);
  1043. if (line_text.begins_with("#"))
  1044. line_text = line_text.substr(1, line_text.length());
  1045. else
  1046. line_text = "#" + line_text;
  1047. tx->set_line(begin, line_text);
  1048. }
  1049. tx->end_complex_operation();
  1050. tx->update();
  1051. //tx->deselect();
  1052. } break;
  1053. case EDIT_COMPLETE: {
  1054. current->get_text_edit()->query_code_comple();
  1055. } break;
  1056. case EDIT_AUTO_INDENT: {
  1057. TextEdit *te = current->get_text_edit();
  1058. String text = te->get_text();
  1059. Ref<Script> scr = current->get_edited_script();
  1060. if (scr.is_null())
  1061. return;
  1062. int begin,end;
  1063. if (te->is_selection_active()) {
  1064. begin=te->get_selection_from_line();
  1065. end=te->get_selection_to_line();
  1066. } else {
  1067. begin=0;
  1068. end=te->get_line_count()-1;
  1069. }
  1070. scr->get_language()->auto_indent_code(text,begin,end);
  1071. te->set_text(text);
  1072. } break;
  1073. case FILE_TOOL_RELOAD:
  1074. case FILE_TOOL_RELOAD_SOFT: {
  1075. TextEdit *te = current->get_text_edit();
  1076. Ref<Script> scr = current->get_edited_script();
  1077. if (scr.is_null())
  1078. return;
  1079. scr->set_source_code(te->get_text());
  1080. scr->get_language()->reload_tool_script(scr,p_option==FILE_TOOL_RELOAD_SOFT);
  1081. } break;
  1082. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  1083. _trim_trailing_whitespace(current->get_text_edit());
  1084. } break;
  1085. case SEARCH_FIND: {
  1086. current->get_find_replace_bar()->popup_search();
  1087. } break;
  1088. case SEARCH_FIND_NEXT: {
  1089. current->get_find_replace_bar()->search_next();
  1090. } break;
  1091. case SEARCH_FIND_PREV: {
  1092. current->get_find_replace_bar()->search_prev();
  1093. } break;
  1094. case SEARCH_REPLACE: {
  1095. current->get_find_replace_bar()->popup_replace();
  1096. } break;
  1097. case SEARCH_LOCATE_FUNCTION: {
  1098. if (!current)
  1099. return;
  1100. quick_open->popup(current->get_functions());
  1101. } break;
  1102. case SEARCH_GOTO_LINE: {
  1103. goto_line_dialog->popup_find_line(current->get_text_edit());
  1104. } break;
  1105. case DEBUG_TOGGLE_BREAKPOINT: {
  1106. int line=current->get_text_edit()->cursor_get_line();
  1107. bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
  1108. current->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  1109. get_debugger()->set_breakpoint(current->get_edited_script()->get_path(),line+1,dobreak);
  1110. } break;
  1111. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  1112. List<int> bpoints;
  1113. current->get_text_edit()->get_breakpoints(&bpoints);
  1114. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  1115. int line = E->get();
  1116. bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
  1117. current->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  1118. get_debugger()->set_breakpoint(current->get_edited_script()->get_path(),line+1,dobreak);
  1119. }
  1120. }
  1121. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  1122. List<int> bpoints;
  1123. current->get_text_edit()->get_breakpoints(&bpoints);
  1124. if (bpoints.size() <= 0) {
  1125. return;
  1126. }
  1127. int line=current->get_text_edit()->cursor_get_line();
  1128. // wrap around
  1129. if (line >= bpoints[bpoints.size() - 1]) {
  1130. current->get_text_edit()->cursor_set_line(bpoints[0]);
  1131. } else {
  1132. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  1133. int bline = E->get();
  1134. if (bline > line) {
  1135. current->get_text_edit()->cursor_set_line(bline);
  1136. return;
  1137. }
  1138. }
  1139. }
  1140. } break;
  1141. case DEBUG_GOTO_PREV_BREAKPOINT: {
  1142. List<int> bpoints;
  1143. current->get_text_edit()->get_breakpoints(&bpoints);
  1144. if (bpoints.size() <= 0) {
  1145. return;
  1146. }
  1147. int line=current->get_text_edit()->cursor_get_line();
  1148. // wrap around
  1149. if (line <= bpoints[0]) {
  1150. current->get_text_edit()->cursor_set_line(bpoints[bpoints.size() - 1]);
  1151. } else {
  1152. for(List<int>::Element *E=bpoints.back();E;E=E->prev()) {
  1153. int bline = E->get();
  1154. if (bline < line) {
  1155. current->get_text_edit()->cursor_set_line(bline);
  1156. return;
  1157. }
  1158. }
  1159. }
  1160. } break;
  1161. case DEBUG_NEXT: {
  1162. if (debugger)
  1163. debugger->debug_next();
  1164. } break;
  1165. case DEBUG_STEP: {
  1166. if (debugger)
  1167. debugger->debug_step();
  1168. } break;
  1169. case DEBUG_BREAK: {
  1170. if (debugger)
  1171. debugger->debug_break();
  1172. } break;
  1173. case DEBUG_CONTINUE: {
  1174. if (debugger)
  1175. debugger->debug_continue();
  1176. } break;
  1177. case HELP_CONTEXTUAL: {
  1178. String text = current->get_text_edit()->get_selection_text();
  1179. if (text == "")
  1180. text = current->get_text_edit()->get_word_under_cursor();
  1181. if (text != "")
  1182. help_search_dialog->popup(text);
  1183. } break;
  1184. case FILE_CLOSE: {
  1185. if (current->get_text_edit()->get_version()!=current->get_text_edit()->get_saved_version()) {
  1186. erase_tab_confirm->set_text("Close and save changes?\n\""+current->get_name()+"\"");
  1187. erase_tab_confirm->popup_centered_minsize();
  1188. } else {
  1189. _close_current_tab();
  1190. }
  1191. } break;
  1192. case CLOSE_DOCS: {
  1193. _close_docs_tab();
  1194. } break;
  1195. case WINDOW_MOVE_LEFT: {
  1196. if (tab_container->get_current_tab()>0) {
  1197. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()-1);
  1198. script_list->call_deferred("select",tab_container->get_current_tab()-1);
  1199. tab_container->move_child(current,tab_container->get_current_tab()-1);
  1200. _update_script_names();
  1201. }
  1202. } break;
  1203. case WINDOW_MOVE_RIGHT: {
  1204. if (tab_container->get_current_tab()<tab_container->get_child_count()-1) {
  1205. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()+1);
  1206. script_list->call_deferred("select",tab_container->get_current_tab()+1);
  1207. tab_container->move_child(current,tab_container->get_current_tab()+1);
  1208. _update_script_names();
  1209. }
  1210. } break;
  1211. default: {
  1212. if (p_option>=WINDOW_SELECT_BASE) {
  1213. tab_container->set_current_tab(p_option-WINDOW_SELECT_BASE);
  1214. script_list->select(p_option-WINDOW_SELECT_BASE);
  1215. }
  1216. }
  1217. }
  1218. }
  1219. EditorHelp *help = tab_container->get_child(selected)->cast_to<EditorHelp>();
  1220. if (help) {
  1221. switch(p_option) {
  1222. case SEARCH_FIND: {
  1223. help->popup_search();
  1224. } break;
  1225. case SEARCH_FIND_NEXT: {
  1226. help->search_again();
  1227. } break;
  1228. case FILE_CLOSE: {
  1229. _close_current_tab();
  1230. } break;
  1231. case CLOSE_DOCS: {
  1232. _close_docs_tab();
  1233. } break;
  1234. }
  1235. }
  1236. }
  1237. void ScriptEditor::_tab_changed(int p_which) {
  1238. ensure_select_current();
  1239. }
  1240. void ScriptEditor::_notification(int p_what) {
  1241. if (p_what==NOTIFICATION_ENTER_TREE) {
  1242. editor->connect("play_pressed",this,"_editor_play");
  1243. editor->connect("pause_pressed",this,"_editor_pause");
  1244. editor->connect("stop_pressed",this,"_editor_stop");
  1245. editor->connect("script_add_function_request",this,"_add_callback");
  1246. editor->connect("resource_saved",this,"_res_saved_callback");
  1247. script_list->connect("item_selected",this,"_script_selected");
  1248. script_split->connect("dragged",this,"_script_split_dragged");
  1249. autosave_timer->connect("timeout",this,"_autosave_scripts");
  1250. {
  1251. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  1252. if (autosave_time>0) {
  1253. autosave_timer->set_wait_time(autosave_time);
  1254. autosave_timer->start();
  1255. } else {
  1256. autosave_timer->stop();
  1257. }
  1258. }
  1259. EditorSettings::get_singleton()->connect("settings_changed",this,"_editor_settings_changed");
  1260. help_search->set_icon(get_icon("Help","EditorIcons"));
  1261. site_search->set_icon(get_icon("Godot","EditorIcons"));
  1262. class_search->set_icon(get_icon("ClassList","EditorIcons"));
  1263. script_forward->set_icon(get_icon("Forward","EditorIcons"));
  1264. script_back->set_icon(get_icon("Back","EditorIcons"));
  1265. }
  1266. if (p_what==NOTIFICATION_READY) {
  1267. get_tree()->connect("tree_changed",this,"_tree_changed");
  1268. editor->connect("request_help",this,"_request_help");
  1269. }
  1270. if (p_what==NOTIFICATION_EXIT_TREE) {
  1271. editor->disconnect("play_pressed",this,"_editor_play");
  1272. editor->disconnect("pause_pressed",this,"_editor_pause");
  1273. editor->disconnect("stop_pressed",this,"_editor_stop");
  1274. }
  1275. if (p_what==MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  1276. _test_script_times_on_disk();
  1277. _update_modified_scripts_for_external_editor();
  1278. }
  1279. if (p_what==NOTIFICATION_PROCESS) {
  1280. }
  1281. }
  1282. void ScriptEditor::close_builtin_scripts_from_scene(const String& p_scene) {
  1283. for(int i=0;i<tab_container->get_child_count();i++) {
  1284. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1285. if (ste) {
  1286. Ref<Script> script = ste->get_edited_script();
  1287. if (!script.is_valid())
  1288. continue;
  1289. if (script->get_path().find("::")!=-1 && script->get_path().begins_with(p_scene)) { //is an internal script and belongs to scene being closed
  1290. _close_tab(i);
  1291. i--;
  1292. }
  1293. }
  1294. }
  1295. }
  1296. void ScriptEditor::edited_scene_changed() {
  1297. _update_modified_scripts_for_external_editor();
  1298. }
  1299. static const Node * _find_node_with_script(const Node* p_node, const RefPtr & p_script) {
  1300. if (p_node->get_script()==p_script)
  1301. return p_node;
  1302. for(int i=0;i<p_node->get_child_count();i++) {
  1303. const Node *result = _find_node_with_script(p_node->get_child(i),p_script);
  1304. if (result)
  1305. return result;
  1306. }
  1307. return NULL;
  1308. }
  1309. Dictionary ScriptEditor::get_state() const {
  1310. // apply_scripts();
  1311. Dictionary state;
  1312. #if 0
  1313. Array paths;
  1314. int open=-1;
  1315. for(int i=0;i<tab_container->get_child_count();i++) {
  1316. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1317. if (!ste)
  1318. continue;
  1319. Ref<Script> script = ste->get_edited_script();
  1320. if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  1321. paths.push_back(script->get_path());
  1322. } else {
  1323. const Node *owner = _find_node_with_script(get_tree()->get_root(),script.get_ref_ptr());
  1324. if (owner)
  1325. paths.push_back(owner->get_path());
  1326. }
  1327. if (i==tab_container->get_current_tab())
  1328. open=i;
  1329. }
  1330. if (paths.size())
  1331. state["sources"]=paths;
  1332. if (open!=-1)
  1333. state["current"]=open;
  1334. #endif
  1335. return state;
  1336. }
  1337. void ScriptEditor::set_state(const Dictionary& p_state) {
  1338. #if 0
  1339. print_line("attempt set state: "+String(Variant(p_state)));
  1340. if (!p_state.has("sources"))
  1341. return; //bleh
  1342. Array sources = p_state["sources"];
  1343. for(int i=0;i<sources.size();i++) {
  1344. Variant source=sources[i];
  1345. Ref<Script> script;
  1346. if (source.get_type()==Variant::NODE_PATH) {
  1347. Node *owner=get_tree()->get_root()->get_node(source);
  1348. if (!owner)
  1349. continue;
  1350. script = owner->get_script();
  1351. } else if (source.get_type()==Variant::STRING) {
  1352. script = ResourceLoader::load(source,"Script");
  1353. }
  1354. if (script.is_null()) //ah well..
  1355. continue;
  1356. editor->call("_resource_selected",script);
  1357. }
  1358. if (p_state.has("current")) {
  1359. tab_container->set_current_tab(p_state["current"]);
  1360. }
  1361. #endif
  1362. }
  1363. void ScriptEditor::clear() {
  1364. #if 0
  1365. List<ScriptTextEditor*> stes;
  1366. for(int i=0;i<tab_container->get_child_count();i++) {
  1367. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1368. if (!ste)
  1369. continue;
  1370. stes.push_back(ste);
  1371. }
  1372. while(stes.size()) {
  1373. memdelete(stes.front()->get());
  1374. stes.pop_front();
  1375. }
  1376. int idx = tab_container->get_current_tab();
  1377. if (idx>=tab_container->get_child_count())
  1378. idx=tab_container->get_child_count()-1;
  1379. if (idx>=0) {
  1380. tab_container->set_current_tab(idx);
  1381. script_list->select( script_list->find_metadata(idx) );
  1382. }
  1383. #endif
  1384. }
  1385. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  1386. for(int i=0;i<tab_container->get_child_count();i++) {
  1387. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1388. if (!ste)
  1389. continue;
  1390. List<int> bpoints;
  1391. ste->get_text_edit()->get_breakpoints(&bpoints);
  1392. Ref<Script> script = ste->get_edited_script();
  1393. String base = script->get_path();
  1394. ERR_CONTINUE( base.begins_with("local://") || base=="" );
  1395. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  1396. p_breakpoints->push_back(base+":"+itos(E->get()+1));
  1397. }
  1398. }
  1399. }
  1400. void ScriptEditor::ensure_focus_current() {
  1401. if (!is_inside_tree())
  1402. return;
  1403. int cidx = tab_container->get_current_tab();
  1404. if (cidx<0 || cidx>=tab_container->get_tab_count());
  1405. Control *c = tab_container->get_child(cidx)->cast_to<Control>();
  1406. if (!c)
  1407. return;
  1408. ScriptTextEditor *ste = c->cast_to<ScriptTextEditor>();
  1409. if (!ste)
  1410. return;
  1411. ste->get_text_edit()->grab_focus();
  1412. }
  1413. void ScriptEditor::_script_selected(int p_idx) {
  1414. grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing
  1415. _go_to_tab(script_list->get_item_metadata(p_idx));
  1416. grab_focus_block=false;
  1417. }
  1418. void ScriptEditor::ensure_select_current() {
  1419. if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
  1420. Node *current = tab_container->get_child(tab_container->get_current_tab());
  1421. ScriptTextEditor *ste = current->cast_to<ScriptTextEditor>();
  1422. if (ste) {
  1423. Ref<Script> script = ste->get_edited_script();
  1424. if (!grab_focus_block && is_visible())
  1425. ste->get_text_edit()->grab_focus();
  1426. edit_menu->show();
  1427. search_menu->show();
  1428. script_search_menu->hide();
  1429. }
  1430. EditorHelp *eh = current->cast_to<EditorHelp>();
  1431. if (eh) {
  1432. edit_menu->hide();
  1433. search_menu->hide();
  1434. script_search_menu->show();
  1435. }
  1436. }
  1437. }
  1438. void ScriptEditor::_find_scripts(Node* p_base, Node* p_current, Set<Ref<Script> > &used) {
  1439. if (p_current!=p_base && p_current->get_owner()!=p_base)
  1440. return;
  1441. if (p_current->get_script_instance()) {
  1442. Ref<Script> scr = p_current->get_script();
  1443. if (scr.is_valid())
  1444. used.insert(scr);
  1445. }
  1446. for(int i=0;i<p_current->get_child_count();i++) {
  1447. _find_scripts(p_base,p_current->get_child(i),used);
  1448. }
  1449. }
  1450. struct _ScriptEditorItemData {
  1451. String name;
  1452. Ref<Texture> icon;
  1453. int index;
  1454. String tooltip;
  1455. bool used;
  1456. int category;
  1457. bool operator<(const _ScriptEditorItemData& id) const {
  1458. return category==id.category?name.nocasecmp_to(id.name)<0:category<id.category;
  1459. }
  1460. };
  1461. void ScriptEditor::_update_script_colors() {
  1462. bool enabled = EditorSettings::get_singleton()->get("text_editor/script_temperature_enabled");
  1463. if (!enabled)
  1464. return;
  1465. int hist_size = EditorSettings::get_singleton()->get("text_editor/script_temperature_history_size");
  1466. Color hot_color=EditorSettings::get_singleton()->get("text_editor/script_temperature_hot_color");
  1467. Color cold_color=EditorSettings::get_singleton()->get("text_editor/script_temperature_cold_color");
  1468. for(int i=0;i<script_list->get_item_count();i++) {
  1469. int c = script_list->get_item_metadata(i);
  1470. Node *n = tab_container->get_child(c);
  1471. if (!n)
  1472. continue;
  1473. script_list->set_item_custom_bg_color(i,Color(0,0,0,0));
  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. void ScriptEditor::_update_script_names() {
  1488. waiting_update_names=false;
  1489. Set<Ref<Script> > used;
  1490. Node* edited = EditorNode::get_singleton()->get_edited_scene();
  1491. if (edited) {
  1492. _find_scripts(edited,edited,used);
  1493. }
  1494. script_list->clear();
  1495. bool split_script_help = EditorSettings::get_singleton()->get("text_editor/group_help_pages");
  1496. Vector<_ScriptEditorItemData> sedata;
  1497. for(int i=0;i<tab_container->get_child_count();i++) {
  1498. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1499. if (ste) {
  1500. String name = ste->get_name();
  1501. Ref<Texture> icon = ste->get_icon();
  1502. String tooltip = ste->get_edited_script()->get_path();
  1503. _ScriptEditorItemData sd;
  1504. sd.icon=icon;
  1505. sd.name=name;
  1506. sd.tooltip=tooltip;
  1507. sd.index=i;
  1508. sd.used=used.has(ste->get_edited_script());
  1509. sd.category=0;
  1510. sedata.push_back(sd);
  1511. }
  1512. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1513. if (eh) {
  1514. String name = eh->get_class_name();
  1515. Ref<Texture> icon = get_icon("Help","EditorIcons");
  1516. String tooltip = name+" Class Reference";
  1517. _ScriptEditorItemData sd;
  1518. sd.icon=icon;
  1519. sd.name=name;
  1520. sd.tooltip=tooltip;
  1521. sd.index=i;
  1522. sd.used=false;
  1523. sd.category=split_script_help?1:0;
  1524. sedata.push_back(sd);
  1525. }
  1526. }
  1527. sedata.sort();
  1528. for(int i=0;i<sedata.size();i++) {
  1529. script_list->add_item(sedata[i].name,sedata[i].icon);
  1530. int index = script_list->get_item_count()-1;
  1531. script_list->set_item_tooltip(index,sedata[i].tooltip);
  1532. script_list->set_item_metadata(index,sedata[i].index);
  1533. if (sedata[i].used) {
  1534. script_list->set_item_custom_bg_color(index,Color(88/255.0,88/255.0,60/255.0));
  1535. }
  1536. if (tab_container->get_current_tab()==sedata[i].index) {
  1537. script_list->select(index);
  1538. script_name_label->set_text(sedata[i].name);
  1539. script_icon->set_texture(sedata[i].icon);
  1540. }
  1541. }
  1542. _update_script_colors();
  1543. }
  1544. void ScriptEditor::edit(const Ref<Script>& p_script) {
  1545. if (p_script.is_null())
  1546. return;
  1547. // refuse to open built-in if scene is not loaded
  1548. // see if already has it
  1549. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
  1550. if (p_script->get_path().is_resource_file() && bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor"))) {
  1551. String path = EditorSettings::get_singleton()->get("external_editor/exec_path");
  1552. String flags = EditorSettings::get_singleton()->get("external_editor/exec_flags");
  1553. List<String> args;
  1554. flags=flags.strip_edges();
  1555. if (flags!=String()) {
  1556. Vector<String> flagss = flags.split(" ",false);
  1557. for(int i=0;i<flagss.size();i++)
  1558. args.push_back(flagss[i]);
  1559. }
  1560. args.push_back(Globals::get_singleton()->globalize_path(p_script->get_path()));
  1561. Error err = OS::get_singleton()->execute(path,args,false);
  1562. if (err==OK)
  1563. return;
  1564. WARN_PRINT("Couldn't open external text editor, using internal");
  1565. }
  1566. for(int i=0;i<tab_container->get_child_count();i++) {
  1567. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1568. if (!ste)
  1569. continue;
  1570. if (ste->get_edited_script()==p_script) {
  1571. if (open_dominant || !EditorNode::get_singleton()->is_changing_scene()) {
  1572. if (tab_container->get_current_tab()!=i) {
  1573. _go_to_tab(i);
  1574. script_list->select( script_list->find_metadata(i) );
  1575. }
  1576. if (is_visible())
  1577. ste->get_text_edit()->grab_focus();
  1578. }
  1579. return;
  1580. }
  1581. }
  1582. // doesn't have it, make a new one
  1583. ScriptTextEditor *ste = memnew( ScriptTextEditor );
  1584. ste->set_edited_script(p_script);
  1585. ste->get_text_edit()->set_tooltip_request_func(this,"_get_debug_tooltip",ste);
  1586. ste->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  1587. ste->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  1588. ste->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  1589. ste->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  1590. ste->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  1591. ste->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  1592. ste->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  1593. ste->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  1594. ste->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  1595. ste->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  1596. ste->get_text_edit()->set_callhint_settings(
  1597. EditorSettings::get_singleton()->get("text_editor/put_callhint_tooltip_below_current_line"),
  1598. EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset"));
  1599. tab_container->add_child(ste);
  1600. _go_to_tab(tab_container->get_tab_count()-1);
  1601. _update_script_names();
  1602. ste->connect("name_changed",this,"_update_script_names");
  1603. if (!restoring_layout) {
  1604. EditorNode::get_singleton()->save_layout();
  1605. }
  1606. //test for modification, maybe the script was not edited but was loaded
  1607. _test_script_times_on_disk(p_script);
  1608. _update_modified_scripts_for_external_editor(p_script);
  1609. }
  1610. void ScriptEditor::save_all_scripts() {
  1611. for(int i=0;i<tab_container->get_child_count();i++) {
  1612. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1613. if (!ste)
  1614. continue;
  1615. if (trim_trailing_whitespace_on_save) {
  1616. _trim_trailing_whitespace(ste->get_text_edit());
  1617. }
  1618. if (ste->get_text_edit()->get_version()==ste->get_text_edit()->get_saved_version())
  1619. continue;
  1620. Ref<Script> script = ste->get_edited_script();
  1621. if (script.is_valid())
  1622. ste->apply_code();
  1623. if (script->get_path()!="" && script->get_path().find("local://")==-1 &&script->get_path().find("::")==-1) {
  1624. //external script, save it
  1625. editor->save_resource(script);
  1626. //ResourceSaver::save(script->get_path(),script);
  1627. }
  1628. }
  1629. }
  1630. void ScriptEditor::apply_scripts() const {
  1631. for(int i=0;i<tab_container->get_child_count();i++) {
  1632. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1633. if (!ste)
  1634. continue;
  1635. ste->apply_code();
  1636. }
  1637. }
  1638. void ScriptEditor::_editor_play() {
  1639. debugger->start();
  1640. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1641. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1642. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false );
  1643. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1644. //debugger_gui->start_listening(Globals::get_singleton()->get("debug/debug_port"));
  1645. }
  1646. void ScriptEditor::_editor_pause() {
  1647. }
  1648. void ScriptEditor::_editor_stop() {
  1649. debugger->stop();
  1650. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1651. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1652. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  1653. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1654. }
  1655. void ScriptEditor::_add_callback(Object *p_obj, const String& p_function, const StringArray& p_args) {
  1656. //print_line("add callback! hohoho"); kinda sad to remove this
  1657. ERR_FAIL_COND(!p_obj);
  1658. Ref<Script> script = p_obj->get_script();
  1659. ERR_FAIL_COND( !script.is_valid() );
  1660. editor->push_item(script.ptr());
  1661. for(int i=0;i<tab_container->get_child_count();i++) {
  1662. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1663. if (!ste)
  1664. continue;
  1665. if (ste->get_edited_script()!=script)
  1666. continue;
  1667. String code = ste->get_text_edit()->get_text();
  1668. int pos = script->get_language()->find_function(p_function,code);
  1669. if (pos==-1) {
  1670. //does not exist
  1671. ste->get_text_edit()->deselect();
  1672. pos=ste->get_text_edit()->get_line_count()+2;
  1673. String func = script->get_language()->make_function("",p_function,p_args);
  1674. //code=code+func;
  1675. ste->get_text_edit()->cursor_set_line(pos+1);
  1676. ste->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  1677. ste->get_text_edit()->insert_text_at_cursor("\n\n"+func);
  1678. }
  1679. _go_to_tab(i);
  1680. ste->get_text_edit()->cursor_set_line(pos);
  1681. ste->get_text_edit()->cursor_set_column(1);
  1682. script_list->select( script_list->find_metadata(i) );
  1683. break;
  1684. }
  1685. }
  1686. void ScriptEditor::_editor_settings_changed() {
  1687. trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/trim_trailing_whitespace_on_save");
  1688. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  1689. if (autosave_time>0) {
  1690. autosave_timer->set_wait_time(autosave_time);
  1691. autosave_timer->start();
  1692. } else {
  1693. autosave_timer->stop();
  1694. }
  1695. if (current_theme == "") {
  1696. current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
  1697. } else if (current_theme != EditorSettings::get_singleton()->get("text_editor/color_theme")) {
  1698. current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
  1699. EditorSettings::get_singleton()->load_text_editor_theme();
  1700. }
  1701. for(int i=0;i<tab_container->get_child_count();i++) {
  1702. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1703. if (!ste)
  1704. continue;
  1705. ste->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  1706. ste->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  1707. ste->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  1708. ste->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  1709. ste->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  1710. ste->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  1711. ste->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  1712. ste->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  1713. ste->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  1714. ste->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  1715. }
  1716. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save",true));
  1717. }
  1718. void ScriptEditor::_autosave_scripts() {
  1719. save_all_scripts();
  1720. }
  1721. void ScriptEditor::_tree_changed() {
  1722. if (waiting_update_names)
  1723. return;
  1724. waiting_update_names=true;
  1725. call_deferred("_update_script_names");
  1726. }
  1727. void ScriptEditor::_script_split_dragged(float) {
  1728. EditorNode::get_singleton()->save_layout();
  1729. }
  1730. void ScriptEditor::_unhandled_input(const InputEvent& p_event) {
  1731. if (p_event.key.pressed || !is_visible()) return;
  1732. if (ED_IS_SHORTCUT("script_editor/next_script", p_event)) {
  1733. int next_tab = script_list->get_current() + 1;
  1734. next_tab %= script_list->get_item_count();
  1735. _go_to_tab(script_list->get_item_metadata(next_tab));
  1736. _update_script_names();
  1737. }
  1738. if (ED_IS_SHORTCUT("script_editor/prev_script", p_event)) {
  1739. int next_tab = script_list->get_current() - 1;
  1740. next_tab = next_tab >= 0 ? next_tab : script_list->get_item_count() - 1;
  1741. _go_to_tab(script_list->get_item_metadata(next_tab));
  1742. _update_script_names();
  1743. }
  1744. }
  1745. void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
  1746. if (!bool(EDITOR_DEF("text_editor/restore_scripts_on_load",true))) {
  1747. return;
  1748. }
  1749. if (!p_layout->has_section_key("ScriptEditor","open_scripts") && !p_layout->has_section_key("ScriptEditor","open_help"))
  1750. return;
  1751. Array scripts = p_layout->get_value("ScriptEditor","open_scripts");
  1752. Array helps;
  1753. if (p_layout->has_section_key("ScriptEditor","open_help"))
  1754. helps=p_layout->get_value("ScriptEditor","open_help");
  1755. restoring_layout=true;
  1756. for(int i=0;i<scripts.size();i++) {
  1757. String path = scripts[i];
  1758. if (!FileAccess::exists(path))
  1759. continue;
  1760. Ref<Script> scr = ResourceLoader::load(path);
  1761. if (scr.is_valid()) {
  1762. edit(scr);
  1763. }
  1764. }
  1765. for(int i=0;i<helps.size();i++) {
  1766. String path = helps[i];
  1767. _help_class_open(path);
  1768. }
  1769. for(int i=0;i<tab_container->get_child_count();i++) {
  1770. tab_container->get_child(i)->set_meta("__editor_pass",Variant());
  1771. }
  1772. if (p_layout->has_section_key("ScriptEditor","split_offset")) {
  1773. script_split->set_split_offset(p_layout->get_value("ScriptEditor","split_offset"));
  1774. }
  1775. restoring_layout=false;
  1776. }
  1777. void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
  1778. Array scripts;
  1779. Array helps;
  1780. for(int i=0;i<tab_container->get_child_count();i++) {
  1781. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1782. if (ste) {
  1783. String path = ste->get_edited_script()->get_path();
  1784. if (!path.is_resource_file())
  1785. continue;
  1786. scripts.push_back(path);
  1787. }
  1788. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1789. if (eh) {
  1790. helps.push_back(eh->get_class_name());
  1791. }
  1792. }
  1793. p_layout->set_value("ScriptEditor","open_scripts",scripts);
  1794. p_layout->set_value("ScriptEditor","open_help",helps);
  1795. p_layout->set_value("ScriptEditor","split_offset",script_split->get_split_offset());
  1796. }
  1797. void ScriptEditor::_help_class_open(const String& p_class) {
  1798. for(int i=0;i<tab_container->get_child_count();i++) {
  1799. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1800. if (eh && eh->get_class_name()==p_class) {
  1801. _go_to_tab(i);
  1802. _update_script_names();
  1803. return;
  1804. }
  1805. }
  1806. EditorHelp * eh = memnew( EditorHelp );
  1807. eh->set_name(p_class);
  1808. tab_container->add_child(eh);
  1809. _go_to_tab(tab_container->get_tab_count()-1);
  1810. eh->go_to_class(p_class,0);
  1811. eh->connect("go_to_help",this,"_help_class_goto");
  1812. _update_script_names();
  1813. }
  1814. void ScriptEditor::_help_class_goto(const String& p_desc) {
  1815. String cname=p_desc.get_slice(":",1);
  1816. for(int i=0;i<tab_container->get_child_count();i++) {
  1817. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1818. if (eh && eh->get_class_name()==cname) {
  1819. _go_to_tab(i);
  1820. eh->go_to_help(p_desc);
  1821. _update_script_names();
  1822. return;
  1823. }
  1824. }
  1825. EditorHelp * eh = memnew( EditorHelp );
  1826. eh->set_name(cname);
  1827. tab_container->add_child(eh);
  1828. _go_to_tab(tab_container->get_tab_count()-1);
  1829. eh->go_to_help(p_desc);
  1830. eh->connect("go_to_help",this,"_help_class_goto");
  1831. _update_script_names();
  1832. }
  1833. void ScriptEditor::_update_history_pos(int p_new_pos) {
  1834. Node *n = tab_container->get_current_tab_control();
  1835. if (n->cast_to<ScriptTextEditor>()) {
  1836. history[history_pos].scroll_pos=n->cast_to<ScriptTextEditor>()->get_text_edit()->get_v_scroll();
  1837. history[history_pos].cursor_column=n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_column();
  1838. history[history_pos].cursor_row=n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_line();
  1839. }
  1840. if (n->cast_to<EditorHelp>()) {
  1841. history[history_pos].scroll_pos=n->cast_to<EditorHelp>()->get_scroll();
  1842. }
  1843. history_pos=p_new_pos;
  1844. tab_container->set_current_tab(history[history_pos].control->get_index());
  1845. n = history[history_pos].control;
  1846. if (n->cast_to<ScriptTextEditor>()) {
  1847. n->cast_to<ScriptTextEditor>()->get_text_edit()->set_v_scroll(history[history_pos].scroll_pos);
  1848. n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_set_column( history[history_pos].cursor_column );
  1849. n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_set_line( history[history_pos].cursor_row );
  1850. n->cast_to<ScriptTextEditor>()->get_text_edit()->grab_focus();
  1851. }
  1852. if (n->cast_to<EditorHelp>()) {
  1853. n->cast_to<EditorHelp>()->set_scroll(history[history_pos].scroll_pos);
  1854. n->cast_to<EditorHelp>()->set_focused();
  1855. }
  1856. n->set_meta("__editor_pass",++edit_pass);
  1857. _update_script_names();
  1858. _update_history_arrows();
  1859. }
  1860. void ScriptEditor::_history_forward() {
  1861. if (history_pos<history.size()-1) {
  1862. _update_history_pos(history_pos+1);
  1863. }
  1864. }
  1865. void ScriptEditor::_history_back(){
  1866. if (history_pos>0) {
  1867. _update_history_pos(history_pos-1);
  1868. }
  1869. }
  1870. void ScriptEditor::set_scene_root_script( Ref<Script> p_script ) {
  1871. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
  1872. if (bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
  1873. return;
  1874. if (open_dominant && p_script.is_valid() && _can_open_in_editor(p_script.ptr())) {
  1875. edit(p_script);
  1876. }
  1877. }
  1878. bool ScriptEditor::script_go_to_method(Ref<Script> p_script, const String& p_method) {
  1879. Vector<String> functions;
  1880. bool found=false;
  1881. for (int i=0;i<tab_container->get_child_count();i++) {
  1882. ScriptTextEditor *current = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1883. if (current && current->get_edited_script()==p_script) {
  1884. functions=current->get_functions();
  1885. found=true;
  1886. break;
  1887. }
  1888. }
  1889. if (!found) {
  1890. String errortxt;
  1891. int line=-1,col;
  1892. String text=p_script->get_source_code();
  1893. List<String> fnc;
  1894. if (p_script->get_language()->validate(text,line,col,errortxt,p_script->get_path(),&fnc)) {
  1895. for (List<String>::Element *E=fnc.front();E;E=E->next())
  1896. functions.push_back(E->get());
  1897. }
  1898. }
  1899. String method_search = p_method + ":";
  1900. for (int i=0;i<functions.size();i++) {
  1901. String function=functions[i];
  1902. if (function.begins_with(method_search)) {
  1903. edit(p_script);
  1904. int line=function.get_slice(":",1).to_int();
  1905. _goto_script_line2(line-1);
  1906. return true;
  1907. }
  1908. }
  1909. return false;
  1910. }
  1911. void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) {
  1912. auto_reload_running_scripts=p_enabled;
  1913. }
  1914. void ScriptEditor::_bind_methods() {
  1915. ObjectTypeDB::bind_method("_file_dialog_action",&ScriptEditor::_file_dialog_action);
  1916. ObjectTypeDB::bind_method("_tab_changed",&ScriptEditor::_tab_changed);
  1917. ObjectTypeDB::bind_method("_menu_option",&ScriptEditor::_menu_option);
  1918. ObjectTypeDB::bind_method("_close_current_tab",&ScriptEditor::_close_current_tab);
  1919. ObjectTypeDB::bind_method("_close_docs_tab", &ScriptEditor::_close_docs_tab);
  1920. ObjectTypeDB::bind_method("_editor_play",&ScriptEditor::_editor_play);
  1921. ObjectTypeDB::bind_method("_editor_pause",&ScriptEditor::_editor_pause);
  1922. ObjectTypeDB::bind_method("_editor_stop",&ScriptEditor::_editor_stop);
  1923. ObjectTypeDB::bind_method("_add_callback",&ScriptEditor::_add_callback);
  1924. ObjectTypeDB::bind_method("_reload_scripts",&ScriptEditor::_reload_scripts);
  1925. ObjectTypeDB::bind_method("_resave_scripts",&ScriptEditor::_resave_scripts);
  1926. ObjectTypeDB::bind_method("_res_saved_callback",&ScriptEditor::_res_saved_callback);
  1927. ObjectTypeDB::bind_method("_goto_script_line",&ScriptEditor::_goto_script_line);
  1928. ObjectTypeDB::bind_method("_goto_script_line2",&ScriptEditor::_goto_script_line2);
  1929. ObjectTypeDB::bind_method("_breaked",&ScriptEditor::_breaked);
  1930. ObjectTypeDB::bind_method("_show_debugger",&ScriptEditor::_show_debugger);
  1931. ObjectTypeDB::bind_method("_get_debug_tooltip",&ScriptEditor::_get_debug_tooltip);
  1932. ObjectTypeDB::bind_method("_autosave_scripts",&ScriptEditor::_autosave_scripts);
  1933. ObjectTypeDB::bind_method("_editor_settings_changed",&ScriptEditor::_editor_settings_changed);
  1934. ObjectTypeDB::bind_method("_update_script_names",&ScriptEditor::_update_script_names);
  1935. ObjectTypeDB::bind_method("_tree_changed",&ScriptEditor::_tree_changed);
  1936. ObjectTypeDB::bind_method("_script_selected",&ScriptEditor::_script_selected);
  1937. ObjectTypeDB::bind_method("_script_created",&ScriptEditor::_script_created);
  1938. ObjectTypeDB::bind_method("_script_split_dragged",&ScriptEditor::_script_split_dragged);
  1939. ObjectTypeDB::bind_method("_help_class_open",&ScriptEditor::_help_class_open);
  1940. ObjectTypeDB::bind_method("_help_class_goto",&ScriptEditor::_help_class_goto);
  1941. ObjectTypeDB::bind_method("_request_help",&ScriptEditor::_help_class_open);
  1942. ObjectTypeDB::bind_method("_history_forward",&ScriptEditor::_history_forward);
  1943. ObjectTypeDB::bind_method("_history_back",&ScriptEditor::_history_back);
  1944. ObjectTypeDB::bind_method("_live_auto_reload_running_scripts",&ScriptEditor::_live_auto_reload_running_scripts);
  1945. ObjectTypeDB::bind_method("_unhandled_input",&ScriptEditor::_unhandled_input);
  1946. }
  1947. ScriptEditor::ScriptEditor(EditorNode *p_editor) {
  1948. current_theme = "";
  1949. completion_cache = memnew( EditorScriptCodeCompletionCache );
  1950. restoring_layout=false;
  1951. waiting_update_names=false;
  1952. auto_reload_running_scripts=false;
  1953. editor=p_editor;
  1954. menu_hb = memnew( HBoxContainer );
  1955. add_child(menu_hb);
  1956. script_split = memnew( HSplitContainer );
  1957. add_child(script_split);
  1958. script_split->set_v_size_flags(SIZE_EXPAND_FILL);
  1959. script_list = memnew( ItemList );
  1960. script_split->add_child(script_list);
  1961. script_list->set_custom_minimum_size(Size2(0,0));
  1962. script_split->set_split_offset(140);
  1963. tab_container = memnew( TabContainer );
  1964. tab_container->set_tabs_visible(false);
  1965. script_split->add_child(tab_container);
  1966. tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
  1967. ED_SHORTCUT("script_editor/next_script", TTR("Next script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_GREATER);
  1968. ED_SHORTCUT("script_editor/prev_script", TTR("Previous script"), KEY_MASK_CMD | KEY_LESS);
  1969. set_process_unhandled_input(true);
  1970. file_menu = memnew( MenuButton );
  1971. menu_hb->add_child(file_menu);
  1972. file_menu->set_text(TTR("File"));
  1973. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTR("New")), FILE_NEW);
  1974. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open", TTR("Open")), FILE_OPEN);
  1975. file_menu->get_popup()->add_separator();
  1976. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save", TTR("Save"), KEY_MASK_ALT|KEY_MASK_CMD|KEY_S), FILE_SAVE);
  1977. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as", TTR("Save As..")), FILE_SAVE_AS);
  1978. 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);
  1979. file_menu->get_popup()->add_separator();
  1980. 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);
  1981. 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);
  1982. file_menu->get_popup()->add_separator();
  1983. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/import_theme", TTR("Import Theme")), FILE_IMPORT_THEME);
  1984. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_theme", TTR("Reload Theme")), FILE_RELOAD_THEME);
  1985. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme", TTR("Save Theme")), FILE_SAVE_THEME);
  1986. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as", TTR("Save Theme As")), FILE_SAVE_THEME_AS);
  1987. file_menu->get_popup()->add_separator();
  1988. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_docs", TTR("Close Docs")), CLOSE_DOCS);
  1989. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file", TTR("Close"), KEY_MASK_CMD | KEY_W), FILE_CLOSE);
  1990. file_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1991. edit_menu = memnew( MenuButton );
  1992. menu_hb->add_child(edit_menu);
  1993. edit_menu->set_text(TTR("Edit"));
  1994. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/undo", TTR("Undo"), KEY_MASK_CMD|KEY_Z), EDIT_UNDO);
  1995. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/redo", TTR("Redo"), KEY_MASK_CMD|KEY_Y), EDIT_REDO);
  1996. edit_menu->get_popup()->add_separator();
  1997. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/cut", TTR("Cut"), KEY_MASK_CMD|KEY_X), EDIT_CUT);
  1998. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy", TTR("Copy"), KEY_MASK_CMD|KEY_C), EDIT_COPY);
  1999. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/paste", TTR("Paste"), KEY_MASK_CMD|KEY_V), EDIT_PASTE);
  2000. edit_menu->get_popup()->add_separator();
  2001. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/select_all", TTR("Select All"), KEY_MASK_CMD|KEY_A), EDIT_SELECT_ALL);
  2002. edit_menu->get_popup()->add_separator();
  2003. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/move_up", TTR("Move Up"), KEY_MASK_ALT|KEY_UP), EDIT_MOVE_LINE_UP);
  2004. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/move_down", TTR("Move Down"), KEY_MASK_ALT|KEY_DOWN), EDIT_MOVE_LINE_DOWN);
  2005. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/indent_left", TTR("Indent Left"), KEY_MASK_ALT|KEY_LEFT), EDIT_INDENT_LEFT);
  2006. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/indent_right", TTR("Indent Right"), KEY_MASK_ALT|KEY_RIGHT), EDIT_INDENT_RIGHT);
  2007. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD|KEY_K), EDIT_TOGGLE_COMMENT);
  2008. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD|KEY_B), EDIT_CLONE_DOWN);
  2009. edit_menu->get_popup()->add_separator();
  2010. #ifdef OSX_ENABLED
  2011. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL|KEY_SPACE), EDIT_COMPLETE);
  2012. #else
  2013. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD|KEY_SPACE), EDIT_COMPLETE);
  2014. #endif
  2015. 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);
  2016. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD|KEY_I), EDIT_AUTO_INDENT);
  2017. edit_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  2018. edit_menu->get_popup()->add_separator();
  2019. 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);
  2020. search_menu = memnew( MenuButton );
  2021. menu_hb->add_child(search_menu);
  2022. search_menu->set_text(TTR("Search"));
  2023. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD|KEY_F), SEARCH_FIND);
  2024. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
  2025. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT|KEY_F3), SEARCH_FIND_PREV);
  2026. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace", TTR("Replace.."), KEY_MASK_CMD|KEY_R), SEARCH_REPLACE);
  2027. search_menu->get_popup()->add_separator();
  2028. 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);
  2029. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD|KEY_L), SEARCH_GOTO_LINE);
  2030. search_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  2031. script_search_menu = memnew( MenuButton );
  2032. menu_hb->add_child(script_search_menu);
  2033. script_search_menu->set_text(TTR("Search"));
  2034. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD|KEY_F), SEARCH_FIND);
  2035. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
  2036. script_search_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  2037. script_search_menu->hide();
  2038. debug_menu = memnew( MenuButton );
  2039. menu_hb->add_child(debug_menu);
  2040. debug_menu->set_text(TTR("Debug"));
  2041. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9), DEBUG_TOGGLE_BREAKPOINT);
  2042. 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);
  2043. 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);
  2044. 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);
  2045. debug_menu->get_popup()->add_separator();
  2046. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_over", TTR("Step Over"), KEY_F10), DEBUG_NEXT);
  2047. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_into", TTR("Step Into"), KEY_F11), DEBUG_STEP);
  2048. debug_menu->get_popup()->add_separator();
  2049. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/break", TTR("Break")), DEBUG_BREAK);
  2050. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/continue", TTR("Continue")), DEBUG_CONTINUE);
  2051. debug_menu->get_popup()->add_separator();
  2052. //debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
  2053. debug_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("debugger/keep_debugger_open", TTR("Keep Debugger Open")), DEBUG_SHOW_KEEP_OPEN);
  2054. debug_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  2055. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  2056. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  2057. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  2058. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  2059. #if 0
  2060. window_menu = memnew( MenuButton );
  2061. menu_hb->add_child(window_menu);
  2062. window_menu->set_text(TTR("Window"));
  2063. window_menu->get_popup()->add_item(TTR("Close"),WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
  2064. window_menu->get_popup()->add_separator();
  2065. window_menu->get_popup()->add_item(TTR("Move Left"),WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
  2066. window_menu->get_popup()->add_item(TTR("Move Right"),WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
  2067. window_menu->get_popup()->add_separator();
  2068. window_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  2069. #endif
  2070. help_menu = memnew( MenuButton );
  2071. menu_hb->add_child(help_menu);
  2072. help_menu->set_text(TTR("Help"));
  2073. help_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/Contextual", TTR("Contextual Help"), KEY_MASK_SHIFT|KEY_F1), HELP_CONTEXTUAL);
  2074. help_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  2075. menu_hb->add_spacer();
  2076. script_icon = memnew( TextureFrame );
  2077. menu_hb->add_child(script_icon);
  2078. script_name_label = memnew( Label );
  2079. menu_hb->add_child(script_name_label);
  2080. script_icon->hide();
  2081. script_name_label->hide();
  2082. menu_hb->add_spacer();
  2083. site_search = memnew( ToolButton );
  2084. site_search->set_text(TTR("Tutorials"));
  2085. site_search->connect("pressed",this,"_menu_option",varray(SEARCH_WEBSITE));
  2086. menu_hb->add_child(site_search);
  2087. site_search->set_tooltip(TTR("Open https://godotengine.org at tutorials section."));
  2088. class_search = memnew( ToolButton );
  2089. class_search->set_text(TTR("Classes"));
  2090. class_search->connect("pressed",this,"_menu_option",varray(SEARCH_CLASSES));
  2091. menu_hb->add_child(class_search);
  2092. class_search->set_tooltip(TTR("Search the class hierarchy."));
  2093. help_search = memnew( ToolButton );
  2094. help_search->set_text(TTR("Search Help"));
  2095. help_search->connect("pressed",this,"_menu_option",varray(SEARCH_HELP));
  2096. menu_hb->add_child(help_search);
  2097. help_search->set_tooltip(TTR("Search the reference documentation."));
  2098. menu_hb->add_child( memnew( VSeparator) );
  2099. script_back = memnew( ToolButton );
  2100. script_back->connect("pressed",this,"_history_back");
  2101. menu_hb->add_child(script_back);
  2102. script_back->set_disabled(true);
  2103. script_back->set_tooltip(TTR("Go to previous edited document."));
  2104. script_forward = memnew( ToolButton );
  2105. script_forward->connect("pressed",this,"_history_forward");
  2106. menu_hb->add_child(script_forward);
  2107. script_forward->set_disabled(true);
  2108. script_forward->set_tooltip(TTR("Go to next edited document."));
  2109. tab_container->connect("tab_changed", this,"_tab_changed");
  2110. erase_tab_confirm = memnew( ConfirmationDialog );
  2111. add_child(erase_tab_confirm);
  2112. erase_tab_confirm->connect("confirmed", this,"_close_current_tab");
  2113. script_create_dialog = memnew(ScriptCreateDialog);
  2114. script_create_dialog->set_title(TTR("Create Script"));
  2115. add_child(script_create_dialog);
  2116. script_create_dialog->connect("script_created", this, "_script_created");
  2117. file_dialog_option = -1;
  2118. file_dialog = memnew( EditorFileDialog );
  2119. add_child(file_dialog);
  2120. file_dialog->connect("file_selected", this,"_file_dialog_action");
  2121. goto_line_dialog = memnew(GotoLineDialog);
  2122. add_child(goto_line_dialog);
  2123. debugger = memnew( ScriptEditorDebugger(editor) );
  2124. debugger->connect("goto_script_line",this,"_goto_script_line");
  2125. debugger->connect("show_debugger",this,"_show_debugger");
  2126. disk_changed = memnew( ConfirmationDialog );
  2127. {
  2128. VBoxContainer *vbc = memnew( VBoxContainer );
  2129. disk_changed->add_child(vbc);
  2130. disk_changed->set_child_rect(vbc);
  2131. Label *dl = memnew( Label );
  2132. dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?:"));
  2133. vbc->add_child(dl);
  2134. disk_changed_list = memnew( Tree );
  2135. vbc->add_child(disk_changed_list);
  2136. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  2137. disk_changed->connect("confirmed",this,"_reload_scripts");
  2138. disk_changed->get_ok()->set_text(TTR("Reload"));
  2139. disk_changed->add_button(TTR("Resave"),!OS::get_singleton()->get_swap_ok_cancel(),"resave");
  2140. disk_changed->connect("custom_action",this,"_resave_scripts");
  2141. }
  2142. add_child(disk_changed);
  2143. script_editor=this;
  2144. quick_open = memnew( ScriptEditorQuickOpen );
  2145. add_child(quick_open);
  2146. quick_open->connect("goto_line",this,"_goto_script_line2");
  2147. Button *db = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Debugger"),debugger);
  2148. debugger->set_tool_button(db);
  2149. debugger->connect("breaked",this,"_breaked");
  2150. autosave_timer = memnew( Timer );
  2151. autosave_timer->set_one_shot(false);
  2152. add_child(autosave_timer);
  2153. grab_focus_block=false;
  2154. help_search_dialog = memnew( EditorHelpSearch );
  2155. add_child(help_search_dialog);
  2156. help_search_dialog->connect("go_to_help",this,"_help_class_goto");
  2157. help_index = memnew( EditorHelpIndex );
  2158. add_child(help_index);
  2159. help_index->connect("open_class",this,"_help_class_open");
  2160. history_pos=-1;
  2161. // debugger_gui->hide();
  2162. edit_pass=0;
  2163. trim_trailing_whitespace_on_save = false;
  2164. }
  2165. ScriptEditor::~ScriptEditor() {
  2166. memdelete(completion_cache);
  2167. }
  2168. void ScriptEditorPlugin::edit(Object *p_object) {
  2169. if (!p_object->cast_to<Script>())
  2170. return;
  2171. script_editor->edit(p_object->cast_to<Script>());
  2172. }
  2173. bool ScriptEditorPlugin::handles(Object *p_object) const {
  2174. if (p_object->cast_to<Script>()) {
  2175. bool valid = _can_open_in_editor(p_object->cast_to<Script>());
  2176. if (!valid) { //user tried to open it by clicking
  2177. EditorNode::get_singleton()->show_warning(TTR("Built-in scripts can only be edited when the scene they belong to is loaded"));
  2178. }
  2179. return valid;
  2180. }
  2181. return p_object->is_type("Script");
  2182. }
  2183. void ScriptEditorPlugin::make_visible(bool p_visible) {
  2184. if (p_visible) {
  2185. script_editor->show();
  2186. script_editor->set_process(true);
  2187. script_editor->ensure_select_current();
  2188. } else {
  2189. script_editor->hide();
  2190. script_editor->set_process(false);
  2191. }
  2192. }
  2193. void ScriptEditorPlugin::selected_notify() {
  2194. script_editor->ensure_select_current();
  2195. }
  2196. Dictionary ScriptEditorPlugin::get_state() const {
  2197. return script_editor->get_state();
  2198. }
  2199. void ScriptEditorPlugin::set_state(const Dictionary& p_state) {
  2200. script_editor->set_state(p_state);
  2201. }
  2202. void ScriptEditorPlugin::clear() {
  2203. script_editor->clear();
  2204. }
  2205. void ScriptEditorPlugin::save_external_data() {
  2206. script_editor->save_all_scripts();
  2207. }
  2208. void ScriptEditorPlugin::apply_changes() {
  2209. script_editor->apply_scripts();
  2210. }
  2211. void ScriptEditorPlugin::restore_global_state() {
  2212. }
  2213. void ScriptEditorPlugin::save_global_state() {
  2214. }
  2215. void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  2216. script_editor->set_window_layout(p_layout);
  2217. }
  2218. void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout){
  2219. script_editor->get_window_layout(p_layout);
  2220. }
  2221. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  2222. return script_editor->get_breakpoints(p_breakpoints);
  2223. }
  2224. void ScriptEditorPlugin::edited_scene_changed() {
  2225. script_editor->edited_scene_changed();
  2226. }
  2227. ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
  2228. editor=p_node;
  2229. script_editor = memnew( ScriptEditor(p_node) );
  2230. editor->get_viewport()->add_child(script_editor);
  2231. script_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2232. script_editor->hide();
  2233. EDITOR_DEF("text_editor/auto_reload_scripts_on_external_change",true);
  2234. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save",true));
  2235. EDITOR_DEF("text_editor/open_dominant_script_on_scene_change",true);
  2236. EDITOR_DEF("external_editor/use_external_editor",false);
  2237. EDITOR_DEF("external_editor/exec_path","");
  2238. EDITOR_DEF("text_editor/script_temperature_enabled",true);
  2239. EDITOR_DEF("text_editor/script_temperature_history_size",15);
  2240. EDITOR_DEF("text_editor/script_temperature_hot_color",Color(1,0,0,0.3));
  2241. EDITOR_DEF("text_editor/script_temperature_cold_color",Color(0,0,1,0.3));
  2242. EDITOR_DEF("text_editor/group_help_pages",true);
  2243. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"external_editor/exec_path",PROPERTY_HINT_GLOBAL_FILE));
  2244. EDITOR_DEF("external_editor/exec_flags","");
  2245. }
  2246. ScriptEditorPlugin::~ScriptEditorPlugin()
  2247. {
  2248. }