script_editor_plugin.cpp 82 KB

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