script_editor_plugin.cpp 91 KB

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