script_editor_plugin.cpp 76 KB

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