script_editor_plugin.cpp 87 KB

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