script_editor_plugin.cpp 89 KB

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