script_editor_plugin.cpp 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005
  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->push_item(current->get_edited_script()->cast_to<Object>());
  804. editor->save_resource_as( current->get_edited_script() );
  805. } break;
  806. case EDIT_UNDO: {
  807. current->get_text_edit()->undo();
  808. current->get_text_edit()->call_deferred("grab_focus");
  809. } break;
  810. case EDIT_REDO: {
  811. current->get_text_edit()->redo();
  812. current->get_text_edit()->call_deferred("grab_focus");
  813. } break;
  814. case EDIT_CUT: {
  815. current->get_text_edit()->cut();
  816. current->get_text_edit()->call_deferred("grab_focus");
  817. } break;
  818. case EDIT_COPY: {
  819. current->get_text_edit()->copy();
  820. current->get_text_edit()->call_deferred("grab_focus");
  821. } break;
  822. case EDIT_PASTE: {
  823. current->get_text_edit()->paste();
  824. current->get_text_edit()->call_deferred("grab_focus");
  825. } break;
  826. case EDIT_SELECT_ALL: {
  827. current->get_text_edit()->select_all();
  828. current->get_text_edit()->call_deferred("grab_focus");
  829. } break;
  830. case EDIT_MOVE_LINE_UP: {
  831. TextEdit *tx = current->get_text_edit();
  832. Ref<Script> scr = current->get_edited_script();
  833. if (scr.is_null())
  834. return;
  835. tx->begin_complex_operation();
  836. if (tx->is_selection_active())
  837. {
  838. int from_line = tx->get_selection_from_line();
  839. int from_col = tx->get_selection_from_column();
  840. int to_line = tx->get_selection_to_line();
  841. int to_column = tx->get_selection_to_column();
  842. for (int i = from_line; i <= to_line; i++)
  843. {
  844. int line_id = i;
  845. int next_id = i - 1;
  846. if (line_id == 0 || next_id < 0)
  847. return;
  848. swap_lines(tx, line_id, next_id);
  849. }
  850. int from_line_up = from_line > 0 ? from_line-1 : from_line;
  851. int to_line_up = to_line > 0 ? to_line-1 : to_line;
  852. tx->select(from_line_up, from_col, to_line_up, to_column);
  853. }
  854. else
  855. {
  856. int line_id = tx->cursor_get_line();
  857. int next_id = line_id - 1;
  858. if (line_id == 0 || next_id < 0)
  859. return;
  860. swap_lines(tx, line_id, next_id);
  861. }
  862. tx->end_complex_operation();
  863. tx->update();
  864. } break;
  865. case EDIT_MOVE_LINE_DOWN: {
  866. TextEdit *tx = current->get_text_edit();
  867. Ref<Script> scr = current->get_edited_script();
  868. if (scr.is_null())
  869. return;
  870. tx->begin_complex_operation();
  871. if (tx->is_selection_active())
  872. {
  873. int from_line = tx->get_selection_from_line();
  874. int from_col = tx->get_selection_from_column();
  875. int to_line = tx->get_selection_to_line();
  876. int to_column = tx->get_selection_to_column();
  877. for (int i = to_line; i >= from_line; i--)
  878. {
  879. int line_id = i;
  880. int next_id = i + 1;
  881. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  882. return;
  883. swap_lines(tx, line_id, next_id);
  884. }
  885. int from_line_down = from_line < tx->get_line_count() ? from_line+1 : from_line;
  886. int to_line_down = to_line < tx->get_line_count() ? to_line+1 : to_line;
  887. tx->select(from_line_down, from_col, to_line_down, to_column);
  888. }
  889. else
  890. {
  891. int line_id = tx->cursor_get_line();
  892. int next_id = line_id + 1;
  893. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  894. return;
  895. swap_lines(tx, line_id, next_id);
  896. }
  897. tx->end_complex_operation();
  898. tx->update();
  899. } break;
  900. case EDIT_INDENT_LEFT: {
  901. TextEdit *tx = current->get_text_edit();
  902. Ref<Script> scr = current->get_edited_script();
  903. if (scr.is_null())
  904. return;
  905. tx->begin_complex_operation();
  906. if (tx->is_selection_active())
  907. {
  908. tx->indent_selection_left();
  909. }
  910. else
  911. {
  912. int begin = tx->cursor_get_line();
  913. String line_text = tx->get_line(begin);
  914. // begins with tab
  915. if (line_text.begins_with("\t"))
  916. {
  917. line_text = line_text.substr(1, line_text.length());
  918. tx->set_line(begin, line_text);
  919. }
  920. // begins with 4 spaces
  921. else if (line_text.begins_with(" "))
  922. {
  923. line_text = line_text.substr(4, line_text.length());
  924. tx->set_line(begin, line_text);
  925. }
  926. }
  927. tx->end_complex_operation();
  928. tx->update();
  929. //tx->deselect();
  930. } break;
  931. case EDIT_INDENT_RIGHT: {
  932. TextEdit *tx = current->get_text_edit();
  933. Ref<Script> scr = current->get_edited_script();
  934. if (scr.is_null())
  935. return;
  936. tx->begin_complex_operation();
  937. if (tx->is_selection_active())
  938. {
  939. tx->indent_selection_right();
  940. }
  941. else
  942. {
  943. int begin = tx->cursor_get_line();
  944. String line_text = tx->get_line(begin);
  945. line_text = '\t' + line_text;
  946. tx->set_line(begin, line_text);
  947. }
  948. tx->end_complex_operation();
  949. tx->update();
  950. //tx->deselect();
  951. } break;
  952. case EDIT_CLONE_DOWN: {
  953. TextEdit *tx = current->get_text_edit();
  954. Ref<Script> scr = current->get_edited_script();
  955. if (scr.is_null())
  956. return;
  957. int from_line = tx->cursor_get_line();
  958. int to_line = tx->cursor_get_line();
  959. int column = tx->cursor_get_column();
  960. if (tx->is_selection_active()) {
  961. from_line = tx->get_selection_from_line();
  962. to_line = tx->get_selection_to_line();
  963. column = tx->cursor_get_column();
  964. }
  965. int next_line = to_line + 1;
  966. tx->begin_complex_operation();
  967. for (int i = from_line; i <= to_line; i++) {
  968. if (i >= tx->get_line_count() - 1) {
  969. tx->set_line(i, tx->get_line(i) + "\n");
  970. }
  971. String line_clone = tx->get_line(i);
  972. tx->insert_at(line_clone, next_line);
  973. next_line++;
  974. }
  975. tx->cursor_set_column(column);
  976. if (tx->is_selection_active()) {
  977. tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
  978. }
  979. tx->end_complex_operation();
  980. tx->update();
  981. } break;
  982. case EDIT_TOGGLE_COMMENT: {
  983. TextEdit *tx = current->get_text_edit();
  984. Ref<Script> scr = current->get_edited_script();
  985. if (scr.is_null())
  986. return;
  987. tx->begin_complex_operation();
  988. if (tx->is_selection_active())
  989. {
  990. int begin = tx->get_selection_from_line();
  991. int end = tx->get_selection_to_line();
  992. // End of selection ends on the first column of the last line, ignore it.
  993. if(tx->get_selection_to_column() == 0)
  994. end -= 1;
  995. for (int i = begin; i <= end; i++)
  996. {
  997. String line_text = tx->get_line(i);
  998. if (line_text.begins_with("#"))
  999. line_text = line_text.substr(1, line_text.length());
  1000. else
  1001. line_text = "#" + line_text;
  1002. tx->set_line(i, line_text);
  1003. }
  1004. }
  1005. else
  1006. {
  1007. int begin = tx->cursor_get_line();
  1008. String line_text = tx->get_line(begin);
  1009. if (line_text.begins_with("#"))
  1010. line_text = line_text.substr(1, line_text.length());
  1011. else
  1012. line_text = "#" + line_text;
  1013. tx->set_line(begin, line_text);
  1014. }
  1015. tx->end_complex_operation();
  1016. tx->update();
  1017. //tx->deselect();
  1018. } break;
  1019. case EDIT_COMPLETE: {
  1020. current->get_text_edit()->query_code_comple();
  1021. } break;
  1022. case EDIT_AUTO_INDENT: {
  1023. TextEdit *te = current->get_text_edit();
  1024. String text = te->get_text();
  1025. Ref<Script> scr = current->get_edited_script();
  1026. if (scr.is_null())
  1027. return;
  1028. int begin,end;
  1029. if (te->is_selection_active()) {
  1030. begin=te->get_selection_from_line();
  1031. end=te->get_selection_to_line();
  1032. } else {
  1033. begin=0;
  1034. end=te->get_line_count()-1;
  1035. }
  1036. scr->get_language()->auto_indent_code(text,begin,end);
  1037. te->set_text(text);
  1038. } break;
  1039. case FILE_TOOL_RELOAD:
  1040. case FILE_TOOL_RELOAD_SOFT: {
  1041. TextEdit *te = current->get_text_edit();
  1042. Ref<Script> scr = current->get_edited_script();
  1043. if (scr.is_null())
  1044. return;
  1045. scr->set_source_code(te->get_text());
  1046. scr->get_language()->reload_tool_script(scr,p_option==FILE_TOOL_RELOAD_SOFT);
  1047. } break;
  1048. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  1049. _trim_trailing_whitespace(current->get_text_edit());
  1050. } break;
  1051. case SEARCH_FIND: {
  1052. current->get_find_replace_bar()->popup_search();
  1053. } break;
  1054. case SEARCH_FIND_NEXT: {
  1055. current->get_find_replace_bar()->search_next();
  1056. } break;
  1057. case SEARCH_FIND_PREV: {
  1058. current->get_find_replace_bar()->search_prev();
  1059. } break;
  1060. case SEARCH_REPLACE: {
  1061. current->get_find_replace_bar()->popup_replace();
  1062. } break;
  1063. case SEARCH_LOCATE_FUNCTION: {
  1064. if (!current)
  1065. return;
  1066. quick_open->popup(current->get_functions());
  1067. } break;
  1068. case SEARCH_GOTO_LINE: {
  1069. goto_line_dialog->popup_find_line(current->get_text_edit());
  1070. } break;
  1071. case DEBUG_TOGGLE_BREAKPOINT: {
  1072. int line=current->get_text_edit()->cursor_get_line();
  1073. bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
  1074. current->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  1075. get_debugger()->set_breakpoint(current->get_edited_script()->get_path(),line+1,dobreak);
  1076. } break;
  1077. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  1078. List<int> bpoints;
  1079. current->get_text_edit()->get_breakpoints(&bpoints);
  1080. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  1081. int line = E->get();
  1082. bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
  1083. current->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  1084. get_debugger()->set_breakpoint(current->get_edited_script()->get_path(),line+1,dobreak);
  1085. }
  1086. }
  1087. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  1088. List<int> bpoints;
  1089. current->get_text_edit()->get_breakpoints(&bpoints);
  1090. if (bpoints.size() <= 0) {
  1091. return;
  1092. }
  1093. int line=current->get_text_edit()->cursor_get_line();
  1094. // wrap around
  1095. if (line >= bpoints[bpoints.size() - 1]) {
  1096. current->get_text_edit()->cursor_set_line(bpoints[0]);
  1097. } else {
  1098. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  1099. int bline = E->get();
  1100. if (bline > line) {
  1101. current->get_text_edit()->cursor_set_line(bline);
  1102. return;
  1103. }
  1104. }
  1105. }
  1106. } break;
  1107. case DEBUG_GOTO_PREV_BREAKPOINT: {
  1108. List<int> bpoints;
  1109. current->get_text_edit()->get_breakpoints(&bpoints);
  1110. if (bpoints.size() <= 0) {
  1111. return;
  1112. }
  1113. int line=current->get_text_edit()->cursor_get_line();
  1114. // wrap around
  1115. if (line <= bpoints[0]) {
  1116. current->get_text_edit()->cursor_set_line(bpoints[bpoints.size() - 1]);
  1117. } else {
  1118. for(List<int>::Element *E=bpoints.back();E;E=E->prev()) {
  1119. int bline = E->get();
  1120. if (bline < line) {
  1121. current->get_text_edit()->cursor_set_line(bline);
  1122. return;
  1123. }
  1124. }
  1125. }
  1126. } break;
  1127. case DEBUG_NEXT: {
  1128. if (debugger)
  1129. debugger->debug_next();
  1130. } break;
  1131. case DEBUG_STEP: {
  1132. if (debugger)
  1133. debugger->debug_step();
  1134. } break;
  1135. case DEBUG_BREAK: {
  1136. if (debugger)
  1137. debugger->debug_break();
  1138. } break;
  1139. case DEBUG_CONTINUE: {
  1140. if (debugger)
  1141. debugger->debug_continue();
  1142. } break;
  1143. case HELP_CONTEXTUAL: {
  1144. String text = current->get_text_edit()->get_selection_text();
  1145. if (text == "")
  1146. text = current->get_text_edit()->get_word_under_cursor();
  1147. if (text != "")
  1148. help_search_dialog->popup(text);
  1149. } break;
  1150. case FILE_CLOSE: {
  1151. if (current->get_text_edit()->get_version()!=current->get_text_edit()->get_saved_version()) {
  1152. erase_tab_confirm->set_text("Close and save changes?\n\""+current->get_name()+"\"");
  1153. erase_tab_confirm->popup_centered_minsize();
  1154. } else {
  1155. _close_current_tab();
  1156. }
  1157. } break;
  1158. case WINDOW_MOVE_LEFT: {
  1159. if (tab_container->get_current_tab()>0) {
  1160. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()-1);
  1161. script_list->call_deferred("select",tab_container->get_current_tab()-1);
  1162. tab_container->move_child(current,tab_container->get_current_tab()-1);
  1163. _update_script_names();
  1164. }
  1165. } break;
  1166. case WINDOW_MOVE_RIGHT: {
  1167. if (tab_container->get_current_tab()<tab_container->get_child_count()-1) {
  1168. tab_container->call_deferred("set_current_tab",tab_container->get_current_tab()+1);
  1169. script_list->call_deferred("select",tab_container->get_current_tab()+1);
  1170. tab_container->move_child(current,tab_container->get_current_tab()+1);
  1171. _update_script_names();
  1172. }
  1173. } break;
  1174. default: {
  1175. if (p_option>=WINDOW_SELECT_BASE) {
  1176. tab_container->set_current_tab(p_option-WINDOW_SELECT_BASE);
  1177. script_list->select(p_option-WINDOW_SELECT_BASE);
  1178. }
  1179. }
  1180. }
  1181. }
  1182. EditorHelp *help = tab_container->get_child(selected)->cast_to<EditorHelp>();
  1183. if (help) {
  1184. switch(p_option) {
  1185. case SEARCH_FIND: {
  1186. help->popup_search();
  1187. } break;
  1188. case SEARCH_FIND_NEXT: {
  1189. help->search_again();
  1190. } break;
  1191. case FILE_CLOSE: {
  1192. _close_current_tab();
  1193. } break;
  1194. }
  1195. }
  1196. }
  1197. void ScriptEditor::_tab_changed(int p_which) {
  1198. ensure_select_current();
  1199. }
  1200. void ScriptEditor::_notification(int p_what) {
  1201. if (p_what==NOTIFICATION_ENTER_TREE) {
  1202. editor->connect("play_pressed",this,"_editor_play");
  1203. editor->connect("pause_pressed",this,"_editor_pause");
  1204. editor->connect("stop_pressed",this,"_editor_stop");
  1205. editor->connect("script_add_function_request",this,"_add_callback");
  1206. editor->connect("resource_saved",this,"_res_saved_callback");
  1207. script_list->connect("item_selected",this,"_script_selected");
  1208. script_split->connect("dragged",this,"_script_split_dragged");
  1209. autosave_timer->connect("timeout",this,"_autosave_scripts");
  1210. {
  1211. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  1212. if (autosave_time>0) {
  1213. autosave_timer->set_wait_time(autosave_time);
  1214. autosave_timer->start();
  1215. } else {
  1216. autosave_timer->stop();
  1217. }
  1218. }
  1219. EditorSettings::get_singleton()->connect("settings_changed",this,"_editor_settings_changed");
  1220. help_search->set_icon(get_icon("Help","EditorIcons"));
  1221. site_search->set_icon(get_icon("Godot","EditorIcons"));
  1222. class_search->set_icon(get_icon("ClassList","EditorIcons"));
  1223. script_forward->set_icon(get_icon("Forward","EditorIcons"));
  1224. script_back->set_icon(get_icon("Back","EditorIcons"));
  1225. }
  1226. if (p_what==NOTIFICATION_READY) {
  1227. get_tree()->connect("tree_changed",this,"_tree_changed");
  1228. editor->connect("request_help",this,"_request_help");
  1229. }
  1230. if (p_what==NOTIFICATION_EXIT_TREE) {
  1231. editor->disconnect("play_pressed",this,"_editor_play");
  1232. editor->disconnect("pause_pressed",this,"_editor_pause");
  1233. editor->disconnect("stop_pressed",this,"_editor_stop");
  1234. }
  1235. if (p_what==MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  1236. _test_script_times_on_disk();
  1237. _update_modified_scripts_for_external_editor();
  1238. }
  1239. if (p_what==NOTIFICATION_PROCESS) {
  1240. }
  1241. }
  1242. void ScriptEditor::edited_scene_changed() {
  1243. _update_modified_scripts_for_external_editor();
  1244. }
  1245. static const Node * _find_node_with_script(const Node* p_node, const RefPtr & p_script) {
  1246. if (p_node->get_script()==p_script)
  1247. return p_node;
  1248. for(int i=0;i<p_node->get_child_count();i++) {
  1249. const Node *result = _find_node_with_script(p_node->get_child(i),p_script);
  1250. if (result)
  1251. return result;
  1252. }
  1253. return NULL;
  1254. }
  1255. Dictionary ScriptEditor::get_state() const {
  1256. // apply_scripts();
  1257. Dictionary state;
  1258. #if 0
  1259. Array paths;
  1260. int open=-1;
  1261. for(int i=0;i<tab_container->get_child_count();i++) {
  1262. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1263. if (!ste)
  1264. continue;
  1265. Ref<Script> script = ste->get_edited_script();
  1266. if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  1267. paths.push_back(script->get_path());
  1268. } else {
  1269. const Node *owner = _find_node_with_script(get_tree()->get_root(),script.get_ref_ptr());
  1270. if (owner)
  1271. paths.push_back(owner->get_path());
  1272. }
  1273. if (i==tab_container->get_current_tab())
  1274. open=i;
  1275. }
  1276. if (paths.size())
  1277. state["sources"]=paths;
  1278. if (open!=-1)
  1279. state["current"]=open;
  1280. #endif
  1281. return state;
  1282. }
  1283. void ScriptEditor::set_state(const Dictionary& p_state) {
  1284. #if 0
  1285. print_line("attempt set state: "+String(Variant(p_state)));
  1286. if (!p_state.has("sources"))
  1287. return; //bleh
  1288. Array sources = p_state["sources"];
  1289. for(int i=0;i<sources.size();i++) {
  1290. Variant source=sources[i];
  1291. Ref<Script> script;
  1292. if (source.get_type()==Variant::NODE_PATH) {
  1293. Node *owner=get_tree()->get_root()->get_node(source);
  1294. if (!owner)
  1295. continue;
  1296. script = owner->get_script();
  1297. } else if (source.get_type()==Variant::STRING) {
  1298. script = ResourceLoader::load(source,"Script");
  1299. }
  1300. if (script.is_null()) //ah well..
  1301. continue;
  1302. editor->call("_resource_selected",script);
  1303. }
  1304. if (p_state.has("current")) {
  1305. tab_container->set_current_tab(p_state["current"]);
  1306. }
  1307. #endif
  1308. }
  1309. void ScriptEditor::clear() {
  1310. #if 0
  1311. List<ScriptTextEditor*> stes;
  1312. for(int i=0;i<tab_container->get_child_count();i++) {
  1313. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1314. if (!ste)
  1315. continue;
  1316. stes.push_back(ste);
  1317. }
  1318. while(stes.size()) {
  1319. memdelete(stes.front()->get());
  1320. stes.pop_front();
  1321. }
  1322. int idx = tab_container->get_current_tab();
  1323. if (idx>=tab_container->get_child_count())
  1324. idx=tab_container->get_child_count()-1;
  1325. if (idx>=0) {
  1326. tab_container->set_current_tab(idx);
  1327. script_list->select( script_list->find_metadata(idx) );
  1328. }
  1329. #endif
  1330. }
  1331. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  1332. for(int i=0;i<tab_container->get_child_count();i++) {
  1333. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1334. if (!ste)
  1335. continue;
  1336. List<int> bpoints;
  1337. ste->get_text_edit()->get_breakpoints(&bpoints);
  1338. Ref<Script> script = ste->get_edited_script();
  1339. String base = script->get_path();
  1340. ERR_CONTINUE( base.begins_with("local://") || base=="" );
  1341. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  1342. p_breakpoints->push_back(base+":"+itos(E->get()+1));
  1343. }
  1344. }
  1345. }
  1346. void ScriptEditor::ensure_focus_current() {
  1347. if (!is_inside_tree())
  1348. return;
  1349. int cidx = tab_container->get_current_tab();
  1350. if (cidx<0 || cidx>=tab_container->get_tab_count());
  1351. Control *c = tab_container->get_child(cidx)->cast_to<Control>();
  1352. if (!c)
  1353. return;
  1354. ScriptTextEditor *ste = c->cast_to<ScriptTextEditor>();
  1355. if (!ste)
  1356. return;
  1357. ste->get_text_edit()->grab_focus();
  1358. }
  1359. void ScriptEditor::_script_selected(int p_idx) {
  1360. grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing
  1361. _go_to_tab(script_list->get_item_metadata(p_idx));
  1362. grab_focus_block=false;
  1363. }
  1364. void ScriptEditor::ensure_select_current() {
  1365. if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
  1366. Node *current = tab_container->get_child(tab_container->get_current_tab());
  1367. ScriptTextEditor *ste = current->cast_to<ScriptTextEditor>();
  1368. if (ste) {
  1369. Ref<Script> script = ste->get_edited_script();
  1370. if (!grab_focus_block && is_visible())
  1371. ste->get_text_edit()->grab_focus();
  1372. edit_menu->show();
  1373. search_menu->show();
  1374. script_search_menu->hide();
  1375. }
  1376. EditorHelp *eh = current->cast_to<EditorHelp>();
  1377. if (eh) {
  1378. edit_menu->hide();
  1379. search_menu->hide();
  1380. script_search_menu->show();
  1381. }
  1382. }
  1383. }
  1384. void ScriptEditor::_find_scripts(Node* p_base, Node* p_current, Set<Ref<Script> > &used) {
  1385. if (p_current!=p_base && p_current->get_owner()!=p_base)
  1386. return;
  1387. if (p_current->get_script_instance()) {
  1388. Ref<Script> scr = p_current->get_script();
  1389. if (scr.is_valid())
  1390. used.insert(scr);
  1391. }
  1392. for(int i=0;i<p_current->get_child_count();i++) {
  1393. _find_scripts(p_base,p_current->get_child(i),used);
  1394. }
  1395. }
  1396. struct _ScriptEditorItemData {
  1397. String name;
  1398. Ref<Texture> icon;
  1399. int index;
  1400. String tooltip;
  1401. bool used;
  1402. int category;
  1403. bool operator<(const _ScriptEditorItemData& id) const {
  1404. return category==id.category?name.nocasecmp_to(id.name)<0:category<id.category;
  1405. }
  1406. };
  1407. void ScriptEditor::_update_script_colors() {
  1408. bool enabled = EditorSettings::get_singleton()->get("text_editor/script_temperature_enabled");
  1409. if (!enabled)
  1410. return;
  1411. int hist_size = EditorSettings::get_singleton()->get("text_editor/script_temperature_history_size");
  1412. Color hot_color=EditorSettings::get_singleton()->get("text_editor/script_temperature_hot_color");
  1413. Color cold_color=EditorSettings::get_singleton()->get("text_editor/script_temperature_cold_color");
  1414. for(int i=0;i<script_list->get_item_count();i++) {
  1415. int c = script_list->get_item_metadata(i);
  1416. Node *n = tab_container->get_child(c);
  1417. if (!n)
  1418. continue;
  1419. script_list->set_item_custom_bg_color(i,Color(0,0,0,0));
  1420. if (!n->has_meta("__editor_pass")) {
  1421. continue;
  1422. }
  1423. int pass=n->get_meta("__editor_pass");
  1424. int h = edit_pass - pass;
  1425. if (h>hist_size) {
  1426. continue;
  1427. }
  1428. int non_zero_hist_size = ( hist_size == 0 ) ? 1 : hist_size;
  1429. float v = Math::ease((edit_pass-pass)/float(non_zero_hist_size),0.4);
  1430. script_list->set_item_custom_bg_color(i,hot_color.linear_interpolate(cold_color,v));
  1431. }
  1432. }
  1433. void ScriptEditor::_update_script_names() {
  1434. waiting_update_names=false;
  1435. Set<Ref<Script> > used;
  1436. Node* edited = EditorNode::get_singleton()->get_edited_scene();
  1437. if (edited) {
  1438. _find_scripts(edited,edited,used);
  1439. }
  1440. script_list->clear();
  1441. bool split_script_help = EditorSettings::get_singleton()->get("text_editor/group_help_pages");
  1442. Vector<_ScriptEditorItemData> sedata;
  1443. for(int i=0;i<tab_container->get_child_count();i++) {
  1444. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1445. if (ste) {
  1446. String name = ste->get_name();
  1447. Ref<Texture> icon = ste->get_icon();
  1448. String tooltip = ste->get_edited_script()->get_path();
  1449. _ScriptEditorItemData sd;
  1450. sd.icon=icon;
  1451. sd.name=name;
  1452. sd.tooltip=tooltip;
  1453. sd.index=i;
  1454. sd.used=used.has(ste->get_edited_script());
  1455. sd.category=0;
  1456. sedata.push_back(sd);
  1457. }
  1458. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1459. if (eh) {
  1460. String name = eh->get_class_name();
  1461. Ref<Texture> icon = get_icon("Help","EditorIcons");
  1462. String tooltip = name+" Class Reference";
  1463. _ScriptEditorItemData sd;
  1464. sd.icon=icon;
  1465. sd.name=name;
  1466. sd.tooltip=tooltip;
  1467. sd.index=i;
  1468. sd.used=false;
  1469. sd.category=split_script_help?1:0;
  1470. sedata.push_back(sd);
  1471. }
  1472. }
  1473. sedata.sort();
  1474. for(int i=0;i<sedata.size();i++) {
  1475. script_list->add_item(sedata[i].name,sedata[i].icon);
  1476. int index = script_list->get_item_count()-1;
  1477. script_list->set_item_tooltip(index,sedata[i].tooltip);
  1478. script_list->set_item_metadata(index,sedata[i].index);
  1479. if (sedata[i].used) {
  1480. script_list->set_item_custom_bg_color(index,Color(88/255.0,88/255.0,60/255.0));
  1481. }
  1482. if (tab_container->get_current_tab()==sedata[i].index) {
  1483. script_list->select(index);
  1484. script_name_label->set_text(sedata[i].name);
  1485. script_icon->set_texture(sedata[i].icon);
  1486. }
  1487. }
  1488. _update_script_colors();
  1489. }
  1490. void ScriptEditor::edit(const Ref<Script>& p_script) {
  1491. if (p_script.is_null())
  1492. return;
  1493. // see if already has it
  1494. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
  1495. if (p_script->get_path().is_resource_file() && bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor"))) {
  1496. String path = EditorSettings::get_singleton()->get("external_editor/exec_path");
  1497. String flags = EditorSettings::get_singleton()->get("external_editor/exec_flags");
  1498. List<String> args;
  1499. flags=flags.strip_edges();
  1500. if (flags!=String()) {
  1501. Vector<String> flagss = flags.split(" ",false);
  1502. for(int i=0;i<flagss.size();i++)
  1503. args.push_back(flagss[i]);
  1504. }
  1505. args.push_back(Globals::get_singleton()->globalize_path(p_script->get_path()));
  1506. Error err = OS::get_singleton()->execute(path,args,false);
  1507. if (err==OK)
  1508. return;
  1509. WARN_PRINT("Couldn't open external text editor, using internal");
  1510. }
  1511. for(int i=0;i<tab_container->get_child_count();i++) {
  1512. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1513. if (!ste)
  1514. continue;
  1515. if (ste->get_edited_script()==p_script) {
  1516. if (open_dominant || !EditorNode::get_singleton()->is_changing_scene()) {
  1517. if (tab_container->get_current_tab()!=i) {
  1518. _go_to_tab(i);
  1519. script_list->select( script_list->find_metadata(i) );
  1520. }
  1521. if (is_visible())
  1522. ste->get_text_edit()->grab_focus();
  1523. }
  1524. return;
  1525. }
  1526. }
  1527. // doesn't have it, make a new one
  1528. ScriptTextEditor *ste = memnew( ScriptTextEditor );
  1529. ste->set_edited_script(p_script);
  1530. ste->get_text_edit()->set_tooltip_request_func(this,"_get_debug_tooltip",ste);
  1531. ste->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  1532. ste->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  1533. ste->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  1534. ste->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  1535. ste->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  1536. ste->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  1537. ste->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  1538. ste->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  1539. ste->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  1540. ste->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  1541. ste->get_text_edit()->set_callhint_settings(
  1542. EditorSettings::get_singleton()->get("text_editor/put_callhint_tooltip_below_current_line"),
  1543. EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset"));
  1544. tab_container->add_child(ste);
  1545. _go_to_tab(tab_container->get_tab_count()-1);
  1546. _update_script_names();
  1547. ste->connect("name_changed",this,"_update_script_names");
  1548. if (!restoring_layout) {
  1549. EditorNode::get_singleton()->save_layout();
  1550. }
  1551. //test for modification, maybe the script was not edited but was loaded
  1552. _test_script_times_on_disk(p_script);
  1553. _update_modified_scripts_for_external_editor(p_script);
  1554. }
  1555. void ScriptEditor::save_all_scripts() {
  1556. for(int i=0;i<tab_container->get_child_count();i++) {
  1557. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1558. if (!ste)
  1559. continue;
  1560. if (trim_trailing_whitespace_on_save) {
  1561. _trim_trailing_whitespace(ste->get_text_edit());
  1562. }
  1563. if (ste->get_text_edit()->get_version()==ste->get_text_edit()->get_saved_version())
  1564. continue;
  1565. Ref<Script> script = ste->get_edited_script();
  1566. if (script.is_valid())
  1567. ste->apply_code();
  1568. if (script->get_path()!="" && script->get_path().find("local://")==-1 &&script->get_path().find("::")==-1) {
  1569. //external script, save it
  1570. editor->save_resource(script);
  1571. //ResourceSaver::save(script->get_path(),script);
  1572. }
  1573. }
  1574. }
  1575. void ScriptEditor::apply_scripts() const {
  1576. for(int i=0;i<tab_container->get_child_count();i++) {
  1577. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1578. if (!ste)
  1579. continue;
  1580. ste->apply_code();
  1581. }
  1582. }
  1583. void ScriptEditor::_editor_play() {
  1584. debugger->start();
  1585. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1586. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1587. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false );
  1588. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1589. //debugger_gui->start_listening(Globals::get_singleton()->get("debug/debug_port"));
  1590. }
  1591. void ScriptEditor::_editor_pause() {
  1592. }
  1593. void ScriptEditor::_editor_stop() {
  1594. debugger->stop();
  1595. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true );
  1596. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1597. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  1598. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1599. }
  1600. void ScriptEditor::_add_callback(Object *p_obj, const String& p_function, const StringArray& p_args) {
  1601. //print_line("add callback! hohoho"); kinda sad to remove this
  1602. ERR_FAIL_COND(!p_obj);
  1603. Ref<Script> script = p_obj->get_script();
  1604. ERR_FAIL_COND( !script.is_valid() );
  1605. editor->push_item(script.ptr());
  1606. for(int i=0;i<tab_container->get_child_count();i++) {
  1607. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1608. if (!ste)
  1609. continue;
  1610. if (ste->get_edited_script()!=script)
  1611. continue;
  1612. String code = ste->get_text_edit()->get_text();
  1613. int pos = script->get_language()->find_function(p_function,code);
  1614. if (pos==-1) {
  1615. //does not exist
  1616. ste->get_text_edit()->deselect();
  1617. pos=ste->get_text_edit()->get_line_count()+2;
  1618. String func = script->get_language()->make_function("",p_function,p_args);
  1619. //code=code+func;
  1620. ste->get_text_edit()->cursor_set_line(pos+1);
  1621. ste->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  1622. ste->get_text_edit()->insert_text_at_cursor("\n\n"+func);
  1623. }
  1624. _go_to_tab(i);
  1625. ste->get_text_edit()->cursor_set_line(pos);
  1626. ste->get_text_edit()->cursor_set_column(1);
  1627. script_list->select( script_list->find_metadata(i) );
  1628. break;
  1629. }
  1630. }
  1631. void ScriptEditor::_editor_settings_changed() {
  1632. trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/trim_trailing_whitespace_on_save");
  1633. float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
  1634. if (autosave_time>0) {
  1635. autosave_timer->set_wait_time(autosave_time);
  1636. autosave_timer->start();
  1637. } else {
  1638. autosave_timer->stop();
  1639. }
  1640. if (current_theme == "") {
  1641. current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
  1642. } else if (current_theme != EditorSettings::get_singleton()->get("text_editor/color_theme")) {
  1643. current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
  1644. EditorSettings::get_singleton()->load_text_editor_theme();
  1645. }
  1646. for(int i=0;i<tab_container->get_child_count();i++) {
  1647. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1648. if (!ste)
  1649. continue;
  1650. ste->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  1651. ste->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  1652. ste->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  1653. ste->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  1654. ste->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  1655. ste->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  1656. ste->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  1657. ste->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  1658. ste->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  1659. ste->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  1660. }
  1661. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save",true));
  1662. }
  1663. void ScriptEditor::_autosave_scripts() {
  1664. save_all_scripts();
  1665. }
  1666. void ScriptEditor::_tree_changed() {
  1667. if (waiting_update_names)
  1668. return;
  1669. waiting_update_names=true;
  1670. call_deferred("_update_script_names");
  1671. }
  1672. void ScriptEditor::_script_split_dragged(float) {
  1673. EditorNode::get_singleton()->save_layout();
  1674. }
  1675. void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
  1676. if (!bool(EDITOR_DEF("text_editor/restore_scripts_on_load",true))) {
  1677. return;
  1678. }
  1679. if (!p_layout->has_section_key("ScriptEditor","open_scripts") && !p_layout->has_section_key("ScriptEditor","open_help"))
  1680. return;
  1681. Array scripts = p_layout->get_value("ScriptEditor","open_scripts");
  1682. Array helps;
  1683. if (p_layout->has_section_key("ScriptEditor","open_help"))
  1684. helps=p_layout->get_value("ScriptEditor","open_help");
  1685. restoring_layout=true;
  1686. for(int i=0;i<scripts.size();i++) {
  1687. String path = scripts[i];
  1688. Ref<Script> scr = ResourceLoader::load(path);
  1689. if (scr.is_valid()) {
  1690. edit(scr);
  1691. }
  1692. }
  1693. for(int i=0;i<helps.size();i++) {
  1694. String path = helps[i];
  1695. _help_class_open(path);
  1696. }
  1697. for(int i=0;i<tab_container->get_child_count();i++) {
  1698. tab_container->get_child(i)->set_meta("__editor_pass",Variant());
  1699. }
  1700. if (p_layout->has_section_key("ScriptEditor","split_offset")) {
  1701. script_split->set_split_offset(p_layout->get_value("ScriptEditor","split_offset"));
  1702. }
  1703. restoring_layout=false;
  1704. }
  1705. void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
  1706. Array scripts;
  1707. Array helps;
  1708. for(int i=0;i<tab_container->get_child_count();i++) {
  1709. ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1710. if (ste) {
  1711. String path = ste->get_edited_script()->get_path();
  1712. if (!path.is_resource_file())
  1713. continue;
  1714. scripts.push_back(path);
  1715. }
  1716. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1717. if (eh) {
  1718. helps.push_back(eh->get_class_name());
  1719. }
  1720. }
  1721. p_layout->set_value("ScriptEditor","open_scripts",scripts);
  1722. p_layout->set_value("ScriptEditor","open_help",helps);
  1723. p_layout->set_value("ScriptEditor","split_offset",script_split->get_split_offset());
  1724. }
  1725. void ScriptEditor::_help_class_open(const String& p_class) {
  1726. for(int i=0;i<tab_container->get_child_count();i++) {
  1727. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1728. if (eh && eh->get_class_name()==p_class) {
  1729. _go_to_tab(i);
  1730. _update_script_names();
  1731. return;
  1732. }
  1733. }
  1734. EditorHelp * eh = memnew( EditorHelp );
  1735. eh->set_name(p_class);
  1736. tab_container->add_child(eh);
  1737. _go_to_tab(tab_container->get_tab_count()-1);
  1738. eh->go_to_class(p_class,0);
  1739. eh->connect("go_to_help",this,"_help_class_goto");
  1740. _update_script_names();
  1741. }
  1742. void ScriptEditor::_help_class_goto(const String& p_desc) {
  1743. String cname=p_desc.get_slice(":",1);
  1744. for(int i=0;i<tab_container->get_child_count();i++) {
  1745. EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
  1746. if (eh && eh->get_class_name()==cname) {
  1747. _go_to_tab(i);
  1748. eh->go_to_help(p_desc);
  1749. _update_script_names();
  1750. return;
  1751. }
  1752. }
  1753. EditorHelp * eh = memnew( EditorHelp );
  1754. eh->set_name(cname);
  1755. tab_container->add_child(eh);
  1756. _go_to_tab(tab_container->get_tab_count()-1);
  1757. eh->go_to_help(p_desc);
  1758. eh->connect("go_to_help",this,"_help_class_goto");
  1759. _update_script_names();
  1760. }
  1761. void ScriptEditor::_update_history_pos(int p_new_pos) {
  1762. Node *n = tab_container->get_current_tab_control();
  1763. if (n->cast_to<ScriptTextEditor>()) {
  1764. history[history_pos].scroll_pos=n->cast_to<ScriptTextEditor>()->get_text_edit()->get_v_scroll();
  1765. history[history_pos].cursor_column=n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_column();
  1766. history[history_pos].cursor_row=n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_line();
  1767. }
  1768. if (n->cast_to<EditorHelp>()) {
  1769. history[history_pos].scroll_pos=n->cast_to<EditorHelp>()->get_scroll();
  1770. }
  1771. history_pos=p_new_pos;
  1772. tab_container->set_current_tab(history[history_pos].control->get_index());
  1773. n = history[history_pos].control;
  1774. if (n->cast_to<ScriptTextEditor>()) {
  1775. n->cast_to<ScriptTextEditor>()->get_text_edit()->set_v_scroll(history[history_pos].scroll_pos);
  1776. n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_set_column( history[history_pos].cursor_column );
  1777. n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_set_line( history[history_pos].cursor_row );
  1778. n->cast_to<ScriptTextEditor>()->get_text_edit()->grab_focus();
  1779. }
  1780. if (n->cast_to<EditorHelp>()) {
  1781. n->cast_to<EditorHelp>()->set_scroll(history[history_pos].scroll_pos);
  1782. n->cast_to<EditorHelp>()->set_focused();
  1783. }
  1784. n->set_meta("__editor_pass",++edit_pass);
  1785. _update_script_names();
  1786. _update_history_arrows();
  1787. }
  1788. void ScriptEditor::_history_forward() {
  1789. if (history_pos<history.size()-1) {
  1790. _update_history_pos(history_pos+1);
  1791. }
  1792. }
  1793. void ScriptEditor::_history_back(){
  1794. if (history_pos>0) {
  1795. _update_history_pos(history_pos-1);
  1796. }
  1797. }
  1798. void ScriptEditor::set_scene_root_script( Ref<Script> p_script ) {
  1799. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
  1800. if (bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
  1801. return;
  1802. if (open_dominant && p_script.is_valid()) {
  1803. edit(p_script);
  1804. }
  1805. }
  1806. bool ScriptEditor::script_go_to_method(Ref<Script> p_script, const String& p_method) {
  1807. Vector<String> functions;
  1808. bool found=false;
  1809. for (int i=0;i<tab_container->get_child_count();i++) {
  1810. ScriptTextEditor *current = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
  1811. if (current && current->get_edited_script()==p_script) {
  1812. functions=current->get_functions();
  1813. found=true;
  1814. break;
  1815. }
  1816. }
  1817. if (!found) {
  1818. String errortxt;
  1819. int line=-1,col;
  1820. String text=p_script->get_source_code();
  1821. List<String> fnc;
  1822. if (p_script->get_language()->validate(text,line,col,errortxt,p_script->get_path(),&fnc)) {
  1823. for (List<String>::Element *E=fnc.front();E;E=E->next())
  1824. functions.push_back(E->get());
  1825. }
  1826. }
  1827. String method_search = p_method + ":";
  1828. for (int i=0;i<functions.size();i++) {
  1829. String function=functions[i];
  1830. if (function.begins_with(method_search)) {
  1831. edit(p_script);
  1832. int line=function.get_slice(":",1).to_int();
  1833. _goto_script_line2(line-1);
  1834. return true;
  1835. }
  1836. }
  1837. return false;
  1838. }
  1839. void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) {
  1840. auto_reload_running_scripts=p_enabled;
  1841. }
  1842. void ScriptEditor::_bind_methods() {
  1843. ObjectTypeDB::bind_method("_file_dialog_action",&ScriptEditor::_file_dialog_action);
  1844. ObjectTypeDB::bind_method("_tab_changed",&ScriptEditor::_tab_changed);
  1845. ObjectTypeDB::bind_method("_menu_option",&ScriptEditor::_menu_option);
  1846. ObjectTypeDB::bind_method("_close_current_tab",&ScriptEditor::_close_current_tab);
  1847. ObjectTypeDB::bind_method("_editor_play",&ScriptEditor::_editor_play);
  1848. ObjectTypeDB::bind_method("_editor_pause",&ScriptEditor::_editor_pause);
  1849. ObjectTypeDB::bind_method("_editor_stop",&ScriptEditor::_editor_stop);
  1850. ObjectTypeDB::bind_method("_add_callback",&ScriptEditor::_add_callback);
  1851. ObjectTypeDB::bind_method("_reload_scripts",&ScriptEditor::_reload_scripts);
  1852. ObjectTypeDB::bind_method("_resave_scripts",&ScriptEditor::_resave_scripts);
  1853. ObjectTypeDB::bind_method("_res_saved_callback",&ScriptEditor::_res_saved_callback);
  1854. ObjectTypeDB::bind_method("_goto_script_line",&ScriptEditor::_goto_script_line);
  1855. ObjectTypeDB::bind_method("_goto_script_line2",&ScriptEditor::_goto_script_line2);
  1856. ObjectTypeDB::bind_method("_breaked",&ScriptEditor::_breaked);
  1857. ObjectTypeDB::bind_method("_show_debugger",&ScriptEditor::_show_debugger);
  1858. ObjectTypeDB::bind_method("_get_debug_tooltip",&ScriptEditor::_get_debug_tooltip);
  1859. ObjectTypeDB::bind_method("_autosave_scripts",&ScriptEditor::_autosave_scripts);
  1860. ObjectTypeDB::bind_method("_editor_settings_changed",&ScriptEditor::_editor_settings_changed);
  1861. ObjectTypeDB::bind_method("_update_script_names",&ScriptEditor::_update_script_names);
  1862. ObjectTypeDB::bind_method("_tree_changed",&ScriptEditor::_tree_changed);
  1863. ObjectTypeDB::bind_method("_script_selected",&ScriptEditor::_script_selected);
  1864. ObjectTypeDB::bind_method("_script_created",&ScriptEditor::_script_created);
  1865. ObjectTypeDB::bind_method("_script_split_dragged",&ScriptEditor::_script_split_dragged);
  1866. ObjectTypeDB::bind_method("_help_class_open",&ScriptEditor::_help_class_open);
  1867. ObjectTypeDB::bind_method("_help_class_goto",&ScriptEditor::_help_class_goto);
  1868. ObjectTypeDB::bind_method("_request_help",&ScriptEditor::_help_class_open);
  1869. ObjectTypeDB::bind_method("_history_forward",&ScriptEditor::_history_forward);
  1870. ObjectTypeDB::bind_method("_history_back",&ScriptEditor::_history_back);
  1871. ObjectTypeDB::bind_method("_live_auto_reload_running_scripts",&ScriptEditor::_live_auto_reload_running_scripts);
  1872. }
  1873. ScriptEditor::ScriptEditor(EditorNode *p_editor) {
  1874. current_theme = "";
  1875. completion_cache = memnew( EditorScriptCodeCompletionCache );
  1876. restoring_layout=false;
  1877. waiting_update_names=false;
  1878. auto_reload_running_scripts=false;
  1879. editor=p_editor;
  1880. menu_hb = memnew( HBoxContainer );
  1881. add_child(menu_hb);
  1882. script_split = memnew( HSplitContainer );
  1883. add_child(script_split);
  1884. script_split->set_v_size_flags(SIZE_EXPAND_FILL);
  1885. script_list = memnew( ItemList );
  1886. script_split->add_child(script_list);
  1887. script_list->set_custom_minimum_size(Size2(0,0));
  1888. script_split->set_split_offset(140);
  1889. tab_container = memnew( TabContainer );
  1890. tab_container->set_tabs_visible(false);
  1891. script_split->add_child(tab_container);
  1892. tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
  1893. file_menu = memnew( MenuButton );
  1894. menu_hb->add_child(file_menu);
  1895. file_menu->set_text(TTR("File"));
  1896. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTR("New")), FILE_NEW);
  1897. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open", TTR("Open")), FILE_OPEN);
  1898. file_menu->get_popup()->add_separator();
  1899. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save", TTR("Save"), KEY_MASK_ALT|KEY_MASK_CMD|KEY_S), FILE_SAVE);
  1900. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as", TTR("Save As..")), FILE_SAVE_AS);
  1901. 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);
  1902. file_menu->get_popup()->add_separator();
  1903. 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);
  1904. 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);
  1905. file_menu->get_popup()->add_separator();
  1906. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/import_theme", TTR("Import Theme")), FILE_IMPORT_THEME);
  1907. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_theme", TTR("Reload Theme")), FILE_RELOAD_THEME);
  1908. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme", TTR("Save Theme")), FILE_SAVE_THEME);
  1909. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as", TTR("Save Theme As")), FILE_SAVE_THEME_AS);
  1910. file_menu->get_popup()->add_separator();
  1911. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file", TTR("Close"), KEY_MASK_CMD|KEY_W), FILE_CLOSE);
  1912. file_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1913. edit_menu = memnew( MenuButton );
  1914. menu_hb->add_child(edit_menu);
  1915. edit_menu->set_text(TTR("Edit"));
  1916. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/undo", TTR("Undo"), KEY_MASK_CMD|KEY_Z), EDIT_UNDO);
  1917. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/redo", TTR("Redo"), KEY_MASK_CMD|KEY_Y), EDIT_REDO);
  1918. edit_menu->get_popup()->add_separator();
  1919. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/cut", TTR("Cut"), KEY_MASK_CMD|KEY_X), EDIT_CUT);
  1920. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy", TTR("Copy"), KEY_MASK_CMD|KEY_C), EDIT_COPY);
  1921. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/paste", TTR("Paste"), KEY_MASK_CMD|KEY_V), EDIT_PASTE);
  1922. edit_menu->get_popup()->add_separator();
  1923. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/select_all", TTR("Select All"), KEY_MASK_CMD|KEY_A), EDIT_SELECT_ALL);
  1924. edit_menu->get_popup()->add_separator();
  1925. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/move_up", TTR("Move Up"), KEY_MASK_ALT|KEY_UP), EDIT_MOVE_LINE_UP);
  1926. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/move_down", TTR("Move Down"), KEY_MASK_ALT|KEY_DOWN), EDIT_MOVE_LINE_DOWN);
  1927. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/indent_left", TTR("Indent Left"), KEY_MASK_ALT|KEY_LEFT), EDIT_INDENT_LEFT);
  1928. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/indent_right", TTR("Indent Right"), KEY_MASK_ALT|KEY_RIGHT), EDIT_INDENT_RIGHT);
  1929. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD|KEY_K), EDIT_TOGGLE_COMMENT);
  1930. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD|KEY_B), EDIT_CLONE_DOWN);
  1931. edit_menu->get_popup()->add_separator();
  1932. #ifdef OSX_ENABLED
  1933. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL|KEY_SPACE), EDIT_COMPLETE);
  1934. #else
  1935. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD|KEY_SPACE), EDIT_COMPLETE);
  1936. #endif
  1937. 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);
  1938. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD|KEY_I), EDIT_AUTO_INDENT);
  1939. edit_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1940. edit_menu->get_popup()->add_separator();
  1941. 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);
  1942. search_menu = memnew( MenuButton );
  1943. menu_hb->add_child(search_menu);
  1944. search_menu->set_text(TTR("Search"));
  1945. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD|KEY_F), SEARCH_FIND);
  1946. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
  1947. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT|KEY_F3), SEARCH_FIND_PREV);
  1948. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace", TTR("Replace.."), KEY_MASK_CMD|KEY_R), SEARCH_REPLACE);
  1949. search_menu->get_popup()->add_separator();
  1950. 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);
  1951. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD|KEY_L), SEARCH_GOTO_LINE);
  1952. search_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1953. script_search_menu = memnew( MenuButton );
  1954. menu_hb->add_child(script_search_menu);
  1955. script_search_menu->set_text(TTR("Search"));
  1956. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD|KEY_F), SEARCH_FIND);
  1957. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
  1958. script_search_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1959. script_search_menu->hide();
  1960. debug_menu = memnew( MenuButton );
  1961. menu_hb->add_child(debug_menu);
  1962. debug_menu->set_text(TTR("Debug"));
  1963. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9), DEBUG_TOGGLE_BREAKPOINT);
  1964. 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);
  1965. 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);
  1966. 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);
  1967. debug_menu->get_popup()->add_separator();
  1968. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_over", TTR("Step Over"), KEY_F10), DEBUG_NEXT);
  1969. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_into", TTR("Step Into"), KEY_F11), DEBUG_STEP);
  1970. debug_menu->get_popup()->add_separator();
  1971. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/break", TTR("Break")), DEBUG_BREAK);
  1972. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/continue", TTR("Continue")), DEBUG_CONTINUE);
  1973. debug_menu->get_popup()->add_separator();
  1974. //debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
  1975. debug_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("debugger/keep_debugger_open", TTR("Keep Debugger Open")), DEBUG_SHOW_KEEP_OPEN);
  1976. debug_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1977. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1978. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_STEP), true );
  1979. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true );
  1980. debug_menu->get_popup()->set_item_disabled( debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true );
  1981. #if 0
  1982. window_menu = memnew( MenuButton );
  1983. menu_hb->add_child(window_menu);
  1984. window_menu->set_text(TTR("Window"));
  1985. window_menu->get_popup()->add_item(TTR("Close"),WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
  1986. window_menu->get_popup()->add_separator();
  1987. window_menu->get_popup()->add_item(TTR("Move Left"),WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
  1988. window_menu->get_popup()->add_item(TTR("Move Right"),WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
  1989. window_menu->get_popup()->add_separator();
  1990. window_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1991. #endif
  1992. help_menu = memnew( MenuButton );
  1993. menu_hb->add_child(help_menu);
  1994. help_menu->set_text(TTR("Help"));
  1995. help_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/Contextual", TTR("Contextual Help"), KEY_MASK_SHIFT|KEY_F1), HELP_CONTEXTUAL);
  1996. help_menu->get_popup()->connect("item_pressed", this,"_menu_option");
  1997. menu_hb->add_spacer();
  1998. script_icon = memnew( TextureFrame );
  1999. menu_hb->add_child(script_icon);
  2000. script_name_label = memnew( Label );
  2001. menu_hb->add_child(script_name_label);
  2002. script_icon->hide();
  2003. script_name_label->hide();
  2004. menu_hb->add_spacer();
  2005. site_search = memnew( ToolButton );
  2006. site_search->set_text(TTR("Tutorials"));
  2007. site_search->connect("pressed",this,"_menu_option",varray(SEARCH_WEBSITE));
  2008. menu_hb->add_child(site_search);
  2009. site_search->set_tooltip(TTR("Open https://godotengine.org at tutorials section."));
  2010. class_search = memnew( ToolButton );
  2011. class_search->set_text(TTR("Classes"));
  2012. class_search->connect("pressed",this,"_menu_option",varray(SEARCH_CLASSES));
  2013. menu_hb->add_child(class_search);
  2014. class_search->set_tooltip(TTR("Search the class hierarchy."));
  2015. help_search = memnew( ToolButton );
  2016. help_search->set_text(TTR("Search Help"));
  2017. help_search->connect("pressed",this,"_menu_option",varray(SEARCH_HELP));
  2018. menu_hb->add_child(help_search);
  2019. help_search->set_tooltip(TTR("Search the reference documentation."));
  2020. menu_hb->add_child( memnew( VSeparator) );
  2021. script_back = memnew( ToolButton );
  2022. script_back->connect("pressed",this,"_history_back");
  2023. menu_hb->add_child(script_back);
  2024. script_back->set_disabled(true);
  2025. script_back->set_tooltip(TTR("Go to previous edited document."));
  2026. script_forward = memnew( ToolButton );
  2027. script_forward->connect("pressed",this,"_history_forward");
  2028. menu_hb->add_child(script_forward);
  2029. script_forward->set_disabled(true);
  2030. script_forward->set_tooltip(TTR("Go to next edited document."));
  2031. tab_container->connect("tab_changed", this,"_tab_changed");
  2032. erase_tab_confirm = memnew( ConfirmationDialog );
  2033. add_child(erase_tab_confirm);
  2034. erase_tab_confirm->connect("confirmed", this,"_close_current_tab");
  2035. script_create_dialog = memnew(ScriptCreateDialog);
  2036. script_create_dialog->set_title(TTR("Create Script"));
  2037. add_child(script_create_dialog);
  2038. script_create_dialog->connect("script_created", this, "_script_created");
  2039. file_dialog_option = -1;
  2040. file_dialog = memnew( EditorFileDialog );
  2041. add_child(file_dialog);
  2042. file_dialog->connect("file_selected", this,"_file_dialog_action");
  2043. goto_line_dialog = memnew(GotoLineDialog);
  2044. add_child(goto_line_dialog);
  2045. debugger = memnew( ScriptEditorDebugger(editor) );
  2046. debugger->connect("goto_script_line",this,"_goto_script_line");
  2047. debugger->connect("show_debugger",this,"_show_debugger");
  2048. disk_changed = memnew( ConfirmationDialog );
  2049. {
  2050. VBoxContainer *vbc = memnew( VBoxContainer );
  2051. disk_changed->add_child(vbc);
  2052. disk_changed->set_child_rect(vbc);
  2053. Label *dl = memnew( Label );
  2054. dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?:"));
  2055. vbc->add_child(dl);
  2056. disk_changed_list = memnew( Tree );
  2057. vbc->add_child(disk_changed_list);
  2058. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  2059. disk_changed->connect("confirmed",this,"_reload_scripts");
  2060. disk_changed->get_ok()->set_text(TTR("Reload"));
  2061. disk_changed->add_button(TTR("Resave"),!OS::get_singleton()->get_swap_ok_cancel(),"resave");
  2062. disk_changed->connect("custom_action",this,"_resave_scripts");
  2063. }
  2064. add_child(disk_changed);
  2065. script_editor=this;
  2066. quick_open = memnew( ScriptEditorQuickOpen );
  2067. add_child(quick_open);
  2068. quick_open->connect("goto_line",this,"_goto_script_line2");
  2069. Button *db = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Debugger"),debugger);
  2070. debugger->set_tool_button(db);
  2071. debugger->connect("breaked",this,"_breaked");
  2072. autosave_timer = memnew( Timer );
  2073. autosave_timer->set_one_shot(false);
  2074. add_child(autosave_timer);
  2075. grab_focus_block=false;
  2076. help_search_dialog = memnew( EditorHelpSearch );
  2077. add_child(help_search_dialog);
  2078. help_search_dialog->connect("go_to_help",this,"_help_class_goto");
  2079. help_index = memnew( EditorHelpIndex );
  2080. add_child(help_index);
  2081. help_index->connect("open_class",this,"_help_class_open");
  2082. history_pos=-1;
  2083. // debugger_gui->hide();
  2084. edit_pass=0;
  2085. trim_trailing_whitespace_on_save = false;
  2086. }
  2087. ScriptEditor::~ScriptEditor() {
  2088. memdelete(completion_cache);
  2089. }
  2090. void ScriptEditorPlugin::edit(Object *p_object) {
  2091. if (!p_object->cast_to<Script>())
  2092. return;
  2093. script_editor->edit(p_object->cast_to<Script>());
  2094. }
  2095. bool ScriptEditorPlugin::handles(Object *p_object) const {
  2096. return p_object->is_type("Script");
  2097. }
  2098. void ScriptEditorPlugin::make_visible(bool p_visible) {
  2099. if (p_visible) {
  2100. script_editor->show();
  2101. script_editor->set_process(true);
  2102. script_editor->ensure_select_current();
  2103. } else {
  2104. script_editor->hide();
  2105. script_editor->set_process(false);
  2106. }
  2107. }
  2108. void ScriptEditorPlugin::selected_notify() {
  2109. script_editor->ensure_select_current();
  2110. }
  2111. Dictionary ScriptEditorPlugin::get_state() const {
  2112. return script_editor->get_state();
  2113. }
  2114. void ScriptEditorPlugin::set_state(const Dictionary& p_state) {
  2115. script_editor->set_state(p_state);
  2116. }
  2117. void ScriptEditorPlugin::clear() {
  2118. script_editor->clear();
  2119. }
  2120. void ScriptEditorPlugin::save_external_data() {
  2121. script_editor->save_all_scripts();
  2122. }
  2123. void ScriptEditorPlugin::apply_changes() {
  2124. script_editor->apply_scripts();
  2125. }
  2126. void ScriptEditorPlugin::restore_global_state() {
  2127. }
  2128. void ScriptEditorPlugin::save_global_state() {
  2129. }
  2130. void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  2131. script_editor->set_window_layout(p_layout);
  2132. }
  2133. void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout){
  2134. script_editor->get_window_layout(p_layout);
  2135. }
  2136. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  2137. return script_editor->get_breakpoints(p_breakpoints);
  2138. }
  2139. void ScriptEditorPlugin::edited_scene_changed() {
  2140. script_editor->edited_scene_changed();
  2141. }
  2142. ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
  2143. editor=p_node;
  2144. script_editor = memnew( ScriptEditor(p_node) );
  2145. editor->get_viewport()->add_child(script_editor);
  2146. script_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2147. script_editor->hide();
  2148. EDITOR_DEF("text_editor/auto_reload_scripts_on_external_change",true);
  2149. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save",true));
  2150. EDITOR_DEF("text_editor/open_dominant_script_on_scene_change",true);
  2151. EDITOR_DEF("external_editor/use_external_editor",false);
  2152. EDITOR_DEF("external_editor/exec_path","");
  2153. EDITOR_DEF("text_editor/script_temperature_enabled",true);
  2154. EDITOR_DEF("text_editor/script_temperature_history_size",15);
  2155. EDITOR_DEF("text_editor/script_temperature_hot_color",Color(1,0,0,0.3));
  2156. EDITOR_DEF("text_editor/script_temperature_cold_color",Color(0,0,1,0.3));
  2157. EDITOR_DEF("text_editor/group_help_pages",true);
  2158. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"external_editor/exec_path",PROPERTY_HINT_GLOBAL_FILE));
  2159. EDITOR_DEF("external_editor/exec_flags","");
  2160. }
  2161. ScriptEditorPlugin::~ScriptEditorPlugin()
  2162. {
  2163. }