script_editor_plugin.cpp 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341
  1. /*************************************************************************/
  2. /* script_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "script_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/os/file_access.h"
  33. #include "core/os/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "core/project_settings.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/find_in_files.h"
  40. #include "editor/node_dock.h"
  41. #include "editor/plugins/shader_editor_plugin.h"
  42. #include "editor/script_editor_debugger.h"
  43. #include "scene/main/viewport.h"
  44. #include "script_text_editor.h"
  45. /*** SCRIPT EDITOR ****/
  46. void ScriptEditorBase::_bind_methods() {
  47. ADD_SIGNAL(MethodInfo("name_changed"));
  48. ADD_SIGNAL(MethodInfo("edited_script_changed"));
  49. ADD_SIGNAL(MethodInfo("request_help", PropertyInfo(Variant::STRING, "topic")));
  50. ADD_SIGNAL(MethodInfo("request_open_script_at_line", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::INT, "line")));
  51. ADD_SIGNAL(MethodInfo("request_save_history"));
  52. ADD_SIGNAL(MethodInfo("go_to_help", PropertyInfo(Variant::STRING, "what")));
  53. // TODO This signal is no use for VisualScript...
  54. ADD_SIGNAL(MethodInfo("search_in_files_requested", PropertyInfo(Variant::STRING, "text")));
  55. }
  56. static bool _is_built_in_script(Script *p_script) {
  57. String path = p_script->get_path();
  58. return path.find("::") != -1;
  59. }
  60. class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache {
  61. struct Cache {
  62. uint64_t time_loaded;
  63. RES cache;
  64. };
  65. Map<String, Cache> cached;
  66. public:
  67. uint64_t max_time_cache;
  68. int max_cache_size;
  69. void cleanup() {
  70. List<Map<String, Cache>::Element *> to_clean;
  71. Map<String, Cache>::Element *I = cached.front();
  72. while (I) {
  73. if ((OS::get_singleton()->get_ticks_msec() - I->get().time_loaded) > max_time_cache) {
  74. to_clean.push_back(I);
  75. }
  76. I = I->next();
  77. }
  78. while (to_clean.front()) {
  79. cached.erase(to_clean.front()->get());
  80. to_clean.pop_front();
  81. }
  82. }
  83. virtual RES get_cached_resource(const String &p_path) {
  84. Map<String, Cache>::Element *E = cached.find(p_path);
  85. if (!E) {
  86. Cache c;
  87. c.cache = ResourceLoader::load(p_path);
  88. E = cached.insert(p_path, c);
  89. }
  90. E->get().time_loaded = OS::get_singleton()->get_ticks_msec();
  91. if (cached.size() > max_cache_size) {
  92. uint64_t older;
  93. Map<String, Cache>::Element *O = cached.front();
  94. older = O->get().time_loaded;
  95. Map<String, Cache>::Element *I = O;
  96. while (I) {
  97. if (I->get().time_loaded < older) {
  98. older = I->get().time_loaded;
  99. O = I;
  100. }
  101. I = I->next();
  102. }
  103. if (O != E) { //should never happen..
  104. cached.erase(O);
  105. }
  106. }
  107. return E->get().cache;
  108. }
  109. EditorScriptCodeCompletionCache() {
  110. max_cache_size = 128;
  111. max_time_cache = 5 * 60 * 1000; //minutes, five
  112. }
  113. virtual ~EditorScriptCodeCompletionCache() {}
  114. };
  115. void ScriptEditorQuickOpen::popup_dialog(const Vector<String> &p_functions, bool p_dontclear) {
  116. popup_centered_ratio(0.6);
  117. if (p_dontclear)
  118. search_box->select_all();
  119. else
  120. search_box->clear();
  121. search_box->grab_focus();
  122. functions = p_functions;
  123. _update_search();
  124. }
  125. void ScriptEditorQuickOpen::_text_changed(const String &p_newtext) {
  126. _update_search();
  127. }
  128. void ScriptEditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
  129. Ref<InputEventKey> k = p_ie;
  130. if (k.is_valid() && (k->get_scancode() == KEY_UP ||
  131. k->get_scancode() == KEY_DOWN ||
  132. k->get_scancode() == KEY_PAGEUP ||
  133. k->get_scancode() == KEY_PAGEDOWN)) {
  134. search_options->call("_gui_input", k);
  135. search_box->accept_event();
  136. }
  137. }
  138. void ScriptEditorQuickOpen::_update_search() {
  139. search_options->clear();
  140. TreeItem *root = search_options->create_item();
  141. for (int i = 0; i < functions.size(); i++) {
  142. String file = functions[i];
  143. if ((search_box->get_text() == "" || file.findn(search_box->get_text()) != -1)) {
  144. TreeItem *ti = search_options->create_item(root);
  145. ti->set_text(0, file);
  146. if (root->get_children() == ti)
  147. ti->select(0);
  148. }
  149. }
  150. get_ok()->set_disabled(root->get_children() == NULL);
  151. }
  152. void ScriptEditorQuickOpen::_confirmed() {
  153. TreeItem *ti = search_options->get_selected();
  154. if (!ti)
  155. return;
  156. int line = ti->get_text(0).get_slice(":", 1).to_int();
  157. emit_signal("goto_line", line - 1);
  158. hide();
  159. }
  160. void ScriptEditorQuickOpen::_notification(int p_what) {
  161. switch (p_what) {
  162. case NOTIFICATION_ENTER_TREE: {
  163. connect("confirmed", this, "_confirmed");
  164. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  165. search_box->set_clear_button_enabled(true);
  166. } break;
  167. case NOTIFICATION_EXIT_TREE: {
  168. disconnect("confirmed", this, "_confirmed");
  169. } break;
  170. }
  171. }
  172. void ScriptEditorQuickOpen::_bind_methods() {
  173. ClassDB::bind_method(D_METHOD("_text_changed"), &ScriptEditorQuickOpen::_text_changed);
  174. ClassDB::bind_method(D_METHOD("_confirmed"), &ScriptEditorQuickOpen::_confirmed);
  175. ClassDB::bind_method(D_METHOD("_sbox_input"), &ScriptEditorQuickOpen::_sbox_input);
  176. ADD_SIGNAL(MethodInfo("goto_line", PropertyInfo(Variant::INT, "line")));
  177. }
  178. ScriptEditorQuickOpen::ScriptEditorQuickOpen() {
  179. VBoxContainer *vbc = memnew(VBoxContainer);
  180. add_child(vbc);
  181. search_box = memnew(LineEdit);
  182. vbc->add_margin_child(TTR("Search:"), search_box);
  183. search_box->connect("text_changed", this, "_text_changed");
  184. search_box->connect("gui_input", this, "_sbox_input");
  185. search_options = memnew(Tree);
  186. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  187. get_ok()->set_text(TTR("Open"));
  188. get_ok()->set_disabled(true);
  189. register_text_enter(search_box);
  190. set_hide_on_ok(false);
  191. search_options->connect("item_activated", this, "_confirmed");
  192. search_options->set_hide_root(true);
  193. }
  194. /////////////////////////////////
  195. ScriptEditor *ScriptEditor::script_editor = NULL;
  196. /*** SCRIPT EDITOR ******/
  197. String ScriptEditor::_get_debug_tooltip(const String &p_text, Node *_se) {
  198. String val = debugger->get_var_value(p_text);
  199. if (val != String()) {
  200. return p_text + ": " + val;
  201. } else {
  202. return String();
  203. }
  204. }
  205. void ScriptEditor::_breaked(bool p_breaked, bool p_can_debug) {
  206. if (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor"))) {
  207. return;
  208. }
  209. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug));
  210. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug));
  211. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked);
  212. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked);
  213. for (int i = 0; i < tab_container->get_child_count(); i++) {
  214. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  215. if (!se) {
  216. continue;
  217. }
  218. se->set_debugger_active(p_breaked);
  219. }
  220. }
  221. void ScriptEditor::_show_debugger(bool p_show) {
  222. //debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), p_show);
  223. }
  224. void ScriptEditor::_script_created(Ref<Script> p_script) {
  225. editor->push_item(p_script.operator->());
  226. }
  227. void ScriptEditor::_goto_script_line2(int p_line) {
  228. ScriptEditorBase *current = _get_current_editor();
  229. if (current)
  230. current->goto_line(p_line);
  231. }
  232. void ScriptEditor::_goto_script_line(REF p_script, int p_line) {
  233. Ref<Script> script = Object::cast_to<Script>(*p_script);
  234. if (script.is_valid() && (script->has_source_code() || script->get_path().is_resource_file())) {
  235. if (edit(p_script, p_line, 0)) {
  236. editor->push_item(p_script.ptr());
  237. ScriptEditorBase *current = _get_current_editor();
  238. if (current)
  239. current->goto_line(p_line, true);
  240. }
  241. }
  242. }
  243. ScriptEditorBase *ScriptEditor::_get_current_editor() const {
  244. int selected = tab_container->get_current_tab();
  245. if (selected < 0 || selected >= tab_container->get_child_count())
  246. return NULL;
  247. return Object::cast_to<ScriptEditorBase>(tab_container->get_child(selected));
  248. }
  249. void ScriptEditor::_update_history_arrows() {
  250. script_back->set_disabled(history_pos <= 0);
  251. script_forward->set_disabled(history_pos >= history.size() - 1);
  252. }
  253. void ScriptEditor::_save_history() {
  254. if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) {
  255. Node *n = tab_container->get_current_tab_control();
  256. if (Object::cast_to<ScriptEditorBase>(n)) {
  257. history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_edit_state();
  258. }
  259. if (Object::cast_to<EditorHelp>(n)) {
  260. history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();
  261. }
  262. }
  263. history.resize(history_pos + 1);
  264. ScriptHistory sh;
  265. sh.control = tab_container->get_current_tab_control();
  266. sh.state = Variant();
  267. history.push_back(sh);
  268. history_pos++;
  269. _update_history_arrows();
  270. }
  271. void ScriptEditor::_go_to_tab(int p_idx) {
  272. Control *c = Object::cast_to<Control>(tab_container->get_child(p_idx));
  273. if (!c)
  274. return;
  275. if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) {
  276. Node *n = tab_container->get_current_tab_control();
  277. if (Object::cast_to<ScriptEditorBase>(n)) {
  278. history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_edit_state();
  279. }
  280. if (Object::cast_to<EditorHelp>(n)) {
  281. history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();
  282. }
  283. }
  284. history.resize(history_pos + 1);
  285. ScriptHistory sh;
  286. sh.control = c;
  287. sh.state = Variant();
  288. history.push_back(sh);
  289. history_pos++;
  290. tab_container->set_current_tab(p_idx);
  291. c = tab_container->get_current_tab_control();
  292. if (Object::cast_to<ScriptEditorBase>(c)) {
  293. script_name_label->set_text(Object::cast_to<ScriptEditorBase>(c)->get_name());
  294. script_icon->set_texture(Object::cast_to<ScriptEditorBase>(c)->get_icon());
  295. if (is_visible_in_tree())
  296. Object::cast_to<ScriptEditorBase>(c)->ensure_focus();
  297. Ref<Script> script = Object::cast_to<ScriptEditorBase>(c)->get_edited_resource();
  298. if (script != NULL) {
  299. notify_script_changed(script);
  300. }
  301. }
  302. if (Object::cast_to<EditorHelp>(c)) {
  303. script_name_label->set_text(Object::cast_to<EditorHelp>(c)->get_class());
  304. script_icon->set_texture(get_icon("Help", "EditorIcons"));
  305. if (is_visible_in_tree())
  306. Object::cast_to<EditorHelp>(c)->set_focused();
  307. }
  308. c->set_meta("__editor_pass", ++edit_pass);
  309. _update_history_arrows();
  310. _update_script_colors();
  311. _update_members_overview();
  312. _update_help_overview();
  313. _update_selected_editor_menu();
  314. _update_members_overview_visibility();
  315. _update_help_overview_visibility();
  316. }
  317. void ScriptEditor::_add_recent_script(String p_path) {
  318. if (p_path.empty()) {
  319. return;
  320. }
  321. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
  322. if (rc.find(p_path) != -1) {
  323. rc.erase(p_path);
  324. }
  325. rc.push_front(p_path);
  326. if (rc.size() > 10) {
  327. rc.resize(10);
  328. }
  329. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", rc);
  330. _update_recent_scripts();
  331. }
  332. void ScriptEditor::_update_recent_scripts() {
  333. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
  334. recent_scripts->clear();
  335. recent_scripts->add_shortcut(ED_SHORTCUT("script_editor/open_recent", TTR("Open Recent"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_T));
  336. recent_scripts->add_separator();
  337. String path;
  338. for (int i = 0; i < rc.size(); i++) {
  339. path = rc[i];
  340. recent_scripts->add_item(path.replace("res://", ""));
  341. }
  342. recent_scripts->add_separator();
  343. recent_scripts->add_shortcut(ED_SHORTCUT("script_editor/clear_recent", TTR("Clear Recent Files")));
  344. recent_scripts->set_as_minsize();
  345. }
  346. void ScriptEditor::_open_recent_script(int p_idx) {
  347. // clear button
  348. if (p_idx == recent_scripts->get_item_count() - 1) {
  349. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", Array());
  350. call_deferred("_update_recent_scripts");
  351. return;
  352. }
  353. // take two for the open recent button
  354. if (p_idx > 0) {
  355. p_idx -= 2;
  356. }
  357. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
  358. ERR_FAIL_INDEX(p_idx, rc.size());
  359. String path = rc[p_idx];
  360. // if its not on disk its a help file or deleted
  361. if (FileAccess::exists(path)) {
  362. List<String> extensions;
  363. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  364. if (extensions.find(path.get_extension())) {
  365. Ref<Script> script = ResourceLoader::load(path);
  366. if (script.is_valid()) {
  367. edit(script, true);
  368. return;
  369. }
  370. }
  371. Error err;
  372. Ref<TextFile> text_file = _load_text_file(path, &err);
  373. if (text_file.is_valid()) {
  374. edit(text_file, true);
  375. return;
  376. }
  377. // if it's a path then it's most likely a deleted file not help
  378. } else if (path.find("::") != -1) {
  379. // built-in script
  380. Ref<Script> script = ResourceLoader::load(path);
  381. if (script.is_valid()) {
  382. edit(script, true);
  383. return;
  384. }
  385. } else if (!path.is_resource_file()) {
  386. _help_class_open(path);
  387. return;
  388. }
  389. rc.remove(p_idx);
  390. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", rc);
  391. _update_recent_scripts();
  392. _show_error_dialog(path);
  393. }
  394. void ScriptEditor::_show_error_dialog(String p_path) {
  395. error_dialog->set_text(vformat(TTR("Can't open '%s'. The file could have been moved or deleted."), p_path));
  396. error_dialog->popup_centered_minsize();
  397. }
  398. void ScriptEditor::_close_tab(int p_idx, bool p_save, bool p_history_back) {
  399. int selected = p_idx;
  400. if (selected < 0 || selected >= tab_container->get_child_count())
  401. return;
  402. Node *tselected = tab_container->get_child(selected);
  403. ScriptEditorBase *current = Object::cast_to<ScriptEditorBase>(tab_container->get_child(selected));
  404. if (current) {
  405. if (p_save) {
  406. apply_scripts();
  407. }
  408. Ref<Script> script = current->get_edited_resource();
  409. if (script != NULL) {
  410. notify_script_close(script);
  411. }
  412. }
  413. // roll back to previous tab
  414. if (p_history_back) {
  415. _history_back();
  416. }
  417. //remove from history
  418. history.resize(history_pos + 1);
  419. for (int i = 0; i < history.size(); i++) {
  420. if (history[i].control == tselected) {
  421. history.remove(i);
  422. i--;
  423. history_pos--;
  424. }
  425. }
  426. if (history_pos >= history.size()) {
  427. history_pos = history.size() - 1;
  428. }
  429. int idx = tab_container->get_current_tab();
  430. if (current) {
  431. current->clear_edit_menu();
  432. }
  433. memdelete(tselected);
  434. if (idx >= tab_container->get_child_count())
  435. idx = tab_container->get_child_count() - 1;
  436. if (idx >= 0) {
  437. if (history_pos >= 0) {
  438. idx = history[history_pos].control->get_index();
  439. }
  440. tab_container->set_current_tab(idx);
  441. }
  442. _update_history_arrows();
  443. _update_script_names();
  444. _update_members_overview_visibility();
  445. _update_help_overview_visibility();
  446. _save_layout();
  447. }
  448. void ScriptEditor::_close_current_tab() {
  449. _close_tab(tab_container->get_current_tab());
  450. }
  451. void ScriptEditor::_close_discard_current_tab(const String &p_str) {
  452. _close_tab(tab_container->get_current_tab(), false);
  453. erase_tab_confirm->hide();
  454. }
  455. void ScriptEditor::_close_docs_tab() {
  456. int child_count = tab_container->get_child_count();
  457. for (int i = child_count - 1; i >= 0; i--) {
  458. EditorHelp *se = Object::cast_to<EditorHelp>(tab_container->get_child(i));
  459. if (se) {
  460. _close_tab(i, true, false);
  461. }
  462. }
  463. }
  464. void ScriptEditor::_copy_script_path() {
  465. ScriptEditorBase *se = _get_current_editor();
  466. RES script = se->get_edited_resource();
  467. OS::get_singleton()->set_clipboard(script->get_path());
  468. }
  469. void ScriptEditor::_close_other_tabs() {
  470. int child_count = tab_container->get_child_count();
  471. int current_idx = tab_container->get_current_tab();
  472. for (int i = child_count - 1; i >= 0; i--) {
  473. if (i == current_idx) {
  474. continue;
  475. }
  476. tab_container->set_current_tab(i);
  477. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  478. if (se) {
  479. // Maybe there are unsaved changes
  480. if (se->is_unsaved()) {
  481. _ask_close_current_unsaved_tab(se);
  482. continue;
  483. }
  484. }
  485. _close_current_tab();
  486. }
  487. }
  488. void ScriptEditor::_close_all_tabs() {
  489. int child_count = tab_container->get_child_count();
  490. for (int i = child_count - 1; i >= 0; i--) {
  491. tab_container->set_current_tab(i);
  492. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  493. if (se) {
  494. // Maybe there are unsaved changes
  495. if (se->is_unsaved()) {
  496. _ask_close_current_unsaved_tab(se);
  497. continue;
  498. }
  499. }
  500. _close_current_tab();
  501. }
  502. }
  503. void ScriptEditor::_ask_close_current_unsaved_tab(ScriptEditorBase *current) {
  504. erase_tab_confirm->set_text(TTR("Close and save changes?") + "\n\"" + current->get_name() + "\"");
  505. erase_tab_confirm->popup_centered_minsize();
  506. }
  507. void ScriptEditor::_resave_scripts(const String &p_str) {
  508. apply_scripts();
  509. for (int i = 0; i < tab_container->get_child_count(); i++) {
  510. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  511. if (!se)
  512. continue;
  513. RES script = se->get_edited_resource();
  514. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1)
  515. continue; //internal script, who cares
  516. if (trim_trailing_whitespace_on_save) {
  517. se->trim_trailing_whitespace();
  518. }
  519. if (convert_indent_on_save) {
  520. if (use_space_indentation) {
  521. se->convert_indent_to_spaces();
  522. } else {
  523. se->convert_indent_to_tabs();
  524. }
  525. }
  526. Ref<TextFile> text_file = script;
  527. if (text_file != NULL) {
  528. se->apply_code();
  529. _save_text_file(text_file, text_file->get_path());
  530. break;
  531. } else {
  532. editor->save_resource(script);
  533. }
  534. se->tag_saved_version();
  535. }
  536. disk_changed->hide();
  537. }
  538. void ScriptEditor::_reload_scripts() {
  539. for (int i = 0; i < tab_container->get_child_count(); i++) {
  540. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  541. if (!se) {
  542. continue;
  543. }
  544. RES edited_res = se->get_edited_resource();
  545. if (edited_res->get_path() == "" || edited_res->get_path().find("local://") != -1 || edited_res->get_path().find("::") != -1) {
  546. continue; //internal script, who cares
  547. }
  548. uint64_t last_date = edited_res->get_last_modified_time();
  549. uint64_t date = FileAccess::get_modified_time(edited_res->get_path());
  550. if (last_date == date) {
  551. continue;
  552. }
  553. Ref<Script> script = edited_res;
  554. if (script != NULL) {
  555. Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_class(), true);
  556. ERR_CONTINUE(!rel_script.is_valid());
  557. script->set_source_code(rel_script->get_source_code());
  558. script->set_last_modified_time(rel_script->get_last_modified_time());
  559. script->reload();
  560. }
  561. Ref<TextFile> text_file = edited_res;
  562. if (text_file != NULL) {
  563. Error err;
  564. Ref<TextFile> rel_text_file = _load_text_file(text_file->get_path(), &err);
  565. ERR_CONTINUE(!rel_text_file.is_valid());
  566. text_file->set_text(rel_text_file->get_text());
  567. text_file->set_last_modified_time(rel_text_file->get_last_modified_time());
  568. }
  569. se->reload_text();
  570. }
  571. disk_changed->hide();
  572. _update_script_names();
  573. }
  574. void ScriptEditor::_res_saved_callback(const Ref<Resource> &p_res) {
  575. for (int i = 0; i < tab_container->get_child_count(); i++) {
  576. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  577. if (!se) {
  578. continue;
  579. }
  580. RES script = se->get_edited_resource();
  581. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
  582. continue; //internal script, who cares
  583. }
  584. if (script == p_res) {
  585. se->tag_saved_version();
  586. }
  587. }
  588. _update_script_names();
  589. if (!pending_auto_reload && auto_reload_running_scripts) {
  590. call_deferred("_live_auto_reload_running_scripts");
  591. pending_auto_reload = true;
  592. }
  593. }
  594. void ScriptEditor::_live_auto_reload_running_scripts() {
  595. pending_auto_reload = false;
  596. debugger->reload_scripts();
  597. }
  598. bool ScriptEditor::_test_script_times_on_disk(RES p_for_script) {
  599. disk_changed_list->clear();
  600. TreeItem *r = disk_changed_list->create_item();
  601. disk_changed_list->set_hide_root(true);
  602. bool need_ask = false;
  603. bool need_reload = false;
  604. bool use_autoreload = bool(EDITOR_DEF("text_editor/files/auto_reload_scripts_on_external_change", false));
  605. for (int i = 0; i < tab_container->get_child_count(); i++) {
  606. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  607. if (se) {
  608. RES edited_res = se->get_edited_resource();
  609. if (p_for_script.is_valid() && edited_res.is_valid() && p_for_script != edited_res)
  610. continue;
  611. if (edited_res->get_path() == "" || edited_res->get_path().find("local://") != -1 || edited_res->get_path().find("::") != -1)
  612. continue; //internal script, who cares
  613. uint64_t last_date = edited_res->get_last_modified_time();
  614. uint64_t date = FileAccess::get_modified_time(edited_res->get_path());
  615. if (last_date != date) {
  616. TreeItem *ti = disk_changed_list->create_item(r);
  617. ti->set_text(0, edited_res->get_path().get_file());
  618. if (!use_autoreload || se->is_unsaved()) {
  619. need_ask = true;
  620. }
  621. need_reload = true;
  622. }
  623. }
  624. }
  625. if (need_reload) {
  626. if (!need_ask) {
  627. script_editor->_reload_scripts();
  628. need_reload = false;
  629. } else {
  630. disk_changed->call_deferred("popup_centered_ratio", 0.5);
  631. }
  632. }
  633. return need_reload;
  634. }
  635. void ScriptEditor::_file_dialog_action(String p_file) {
  636. switch (file_dialog_option) {
  637. case FILE_NEW_TEXTFILE: {
  638. Error err;
  639. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  640. if (err) {
  641. memdelete(file);
  642. editor->show_warning(TTR("Error writing TextFile:") + "\n" + p_file, TTR("Error!"));
  643. break;
  644. }
  645. file->close();
  646. memdelete(file);
  647. FALLTHROUGH;
  648. }
  649. case FILE_OPEN: {
  650. List<String> extensions;
  651. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  652. if (extensions.find(p_file.get_extension())) {
  653. Ref<Script> scr = ResourceLoader::load(p_file);
  654. if (!scr.is_valid()) {
  655. editor->show_warning(TTR("Error: could not load file."), TTR("Error!"));
  656. file_dialog_option = -1;
  657. return;
  658. }
  659. edit(scr);
  660. file_dialog_option = -1;
  661. return;
  662. }
  663. Error error;
  664. Ref<TextFile> text_file = _load_text_file(p_file, &error);
  665. if (error != OK) {
  666. editor->show_warning(TTR("Error could not load file."), TTR("Error!"));
  667. }
  668. if (text_file.is_valid()) {
  669. edit(text_file);
  670. file_dialog_option = -1;
  671. return;
  672. }
  673. } break;
  674. case FILE_SAVE_AS: {
  675. ScriptEditorBase *current = _get_current_editor();
  676. String path = ProjectSettings::get_singleton()->localize_path(p_file);
  677. Error err = _save_text_file(current->get_edited_resource(), path);
  678. if (err != OK) {
  679. editor->show_accept(TTR("Error saving file!"), TTR("OK"));
  680. return;
  681. }
  682. ((Resource *)current->get_edited_resource().ptr())->set_path(path);
  683. _update_script_names();
  684. } break;
  685. case THEME_SAVE_AS: {
  686. if (!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) {
  687. editor->show_warning(TTR("Error while saving theme."), TTR("Error Saving"));
  688. }
  689. } break;
  690. case THEME_IMPORT: {
  691. if (!EditorSettings::get_singleton()->import_text_editor_theme(p_file)) {
  692. editor->show_warning(TTR("Error importing theme."), TTR("Error Importing"));
  693. }
  694. } break;
  695. }
  696. file_dialog_option = -1;
  697. }
  698. Ref<Script> ScriptEditor::_get_current_script() {
  699. ScriptEditorBase *current = _get_current_editor();
  700. if (current) {
  701. Ref<Script> script = current->get_edited_resource();
  702. return script != NULL ? script : NULL;
  703. } else {
  704. return NULL;
  705. }
  706. }
  707. Array ScriptEditor::_get_open_scripts() const {
  708. Array ret;
  709. Vector<Ref<Script> > scripts = get_open_scripts();
  710. int scrits_amount = scripts.size();
  711. for (int idx_script = 0; idx_script < scrits_amount; idx_script++) {
  712. ret.push_back(scripts[idx_script]);
  713. }
  714. return ret;
  715. }
  716. void ScriptEditor::_menu_option(int p_option) {
  717. switch (p_option) {
  718. case FILE_NEW: {
  719. script_create_dialog->config("Node", "new_script");
  720. script_create_dialog->popup_centered(Size2(300, 300) * EDSCALE);
  721. } break;
  722. case FILE_NEW_TEXTFILE: {
  723. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  724. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  725. file_dialog_option = FILE_NEW_TEXTFILE;
  726. file_dialog->clear_filters();
  727. file_dialog->popup_centered_ratio();
  728. file_dialog->set_title(TTR("New TextFile..."));
  729. } break;
  730. case FILE_OPEN: {
  731. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  732. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  733. file_dialog_option = FILE_OPEN;
  734. List<String> extensions;
  735. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  736. file_dialog->clear_filters();
  737. for (int i = 0; i < extensions.size(); i++) {
  738. file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  739. }
  740. file_dialog->popup_centered_ratio();
  741. file_dialog->set_title(TTR("Open File"));
  742. return;
  743. } break;
  744. case FILE_SAVE_ALL: {
  745. if (_test_script_times_on_disk())
  746. return;
  747. save_all_scripts();
  748. } break;
  749. case SEARCH_HELP: {
  750. help_search_dialog->popup_dialog();
  751. } break;
  752. case SEARCH_WEBSITE: {
  753. OS::get_singleton()->shell_open("https://docs.godotengine.org/");
  754. } break;
  755. case REQUEST_DOCS: {
  756. OS::get_singleton()->shell_open("https://github.com/godotengine/godot-docs/issues/new");
  757. } break;
  758. case WINDOW_NEXT: {
  759. _history_forward();
  760. } break;
  761. case WINDOW_PREV: {
  762. _history_back();
  763. } break;
  764. case WINDOW_SORT: {
  765. _sort_list_on_update = true;
  766. _update_script_names();
  767. } break;
  768. case DEBUG_SHOW: {
  769. if (debugger) {
  770. bool visible = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW));
  771. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW), !visible);
  772. if (visible)
  773. debugger->hide();
  774. else
  775. debugger->show();
  776. }
  777. } break;
  778. case DEBUG_SHOW_KEEP_OPEN: {
  779. bool visible = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN));
  780. if (debugger)
  781. debugger->set_hide_on_stop(visible);
  782. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN), !visible);
  783. } break;
  784. case DEBUG_WITH_EXTERNAL_EDITOR: {
  785. bool debug_with_external_editor = !debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR));
  786. debugger->set_debug_with_external_editor(debug_with_external_editor);
  787. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR), debug_with_external_editor);
  788. } break;
  789. case TOGGLE_SCRIPTS_PANEL: {
  790. list_split->set_visible(!list_split->is_visible());
  791. }
  792. }
  793. ScriptEditorBase *current = _get_current_editor();
  794. if (current) {
  795. switch (p_option) {
  796. case FILE_SAVE: {
  797. if (_test_script_times_on_disk())
  798. return;
  799. if (trim_trailing_whitespace_on_save)
  800. current->trim_trailing_whitespace();
  801. if (convert_indent_on_save) {
  802. if (use_space_indentation) {
  803. current->convert_indent_to_spaces();
  804. } else {
  805. current->convert_indent_to_tabs();
  806. }
  807. }
  808. Ref<TextFile> text_file = current->get_edited_resource();
  809. if (text_file != NULL) {
  810. current->apply_code();
  811. _save_text_file(text_file, text_file->get_path());
  812. break;
  813. }
  814. editor->save_resource(current->get_edited_resource());
  815. } break;
  816. case FILE_SAVE_AS: {
  817. current->trim_trailing_whitespace();
  818. if (convert_indent_on_save) {
  819. if (use_space_indentation) {
  820. current->convert_indent_to_spaces();
  821. } else {
  822. current->convert_indent_to_tabs();
  823. }
  824. }
  825. Ref<TextFile> text_file = current->get_edited_resource();
  826. if (text_file != NULL) {
  827. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  828. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  829. file_dialog_option = FILE_SAVE_AS;
  830. List<String> extensions;
  831. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  832. file_dialog->clear_filters();
  833. file_dialog->set_current_dir(text_file->get_path().get_base_dir());
  834. file_dialog->set_current_file(text_file->get_path().get_file());
  835. file_dialog->popup_centered_ratio();
  836. file_dialog->set_title(TTR("Save File As..."));
  837. break;
  838. }
  839. editor->push_item(Object::cast_to<Object>(current->get_edited_resource().ptr()));
  840. editor->save_resource_as(current->get_edited_resource());
  841. } break;
  842. case FILE_TOOL_RELOAD:
  843. case FILE_TOOL_RELOAD_SOFT: {
  844. current->reload(p_option == FILE_TOOL_RELOAD_SOFT);
  845. } break;
  846. case FILE_RUN: {
  847. Ref<Script> scr = current->get_edited_resource();
  848. if (scr == NULL || scr.is_null()) {
  849. EditorNode::get_singleton()->show_warning("Can't obtain the script for running.");
  850. break;
  851. }
  852. current->apply_code();
  853. Error err = scr->reload(false); //hard reload script before running always
  854. if (err != OK) {
  855. EditorNode::get_singleton()->show_warning("Script failed reloading, check console for errors.");
  856. return;
  857. }
  858. if (!scr->is_tool()) {
  859. EditorNode::get_singleton()->show_warning("Script is not in tool mode, will not be able to run.");
  860. return;
  861. }
  862. if (!ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorScript")) {
  863. EditorNode::get_singleton()->show_warning("To run this script, it must inherit EditorScript and be set to tool mode.");
  864. return;
  865. }
  866. Ref<EditorScript> es = memnew(EditorScript);
  867. es->set_script(scr.get_ref_ptr());
  868. es->set_editor(EditorNode::get_singleton());
  869. es->_run();
  870. EditorNode::get_undo_redo()->clear_history();
  871. } break;
  872. case FILE_CLOSE: {
  873. if (current->is_unsaved()) {
  874. _ask_close_current_unsaved_tab(current);
  875. } else {
  876. _close_current_tab();
  877. }
  878. } break;
  879. case FILE_COPY_PATH: {
  880. _copy_script_path();
  881. } break;
  882. case SHOW_IN_FILE_SYSTEM: {
  883. RES script = current->get_edited_resource();
  884. FileSystemDock *file_system_dock = EditorNode::get_singleton()->get_filesystem_dock();
  885. file_system_dock->navigate_to_path(script->get_path());
  886. // Ensure that the FileSystem dock is visible.
  887. TabContainer *tab_container = (TabContainer *)file_system_dock->get_parent_control();
  888. tab_container->set_current_tab(file_system_dock->get_position_in_parent());
  889. } break;
  890. case CLOSE_DOCS: {
  891. _close_docs_tab();
  892. } break;
  893. case CLOSE_OTHER_TABS: {
  894. _close_other_tabs();
  895. } break;
  896. case CLOSE_ALL: {
  897. _close_all_tabs();
  898. } break;
  899. case DEBUG_NEXT: {
  900. if (debugger)
  901. debugger->debug_next();
  902. } break;
  903. case DEBUG_STEP: {
  904. if (debugger)
  905. debugger->debug_step();
  906. } break;
  907. case DEBUG_BREAK: {
  908. if (debugger)
  909. debugger->debug_break();
  910. } break;
  911. case DEBUG_CONTINUE: {
  912. if (debugger)
  913. debugger->debug_continue();
  914. } break;
  915. case WINDOW_MOVE_UP: {
  916. if (tab_container->get_current_tab() > 0) {
  917. tab_container->move_child(current, tab_container->get_current_tab() - 1);
  918. tab_container->set_current_tab(tab_container->get_current_tab() - 1);
  919. _update_script_names();
  920. }
  921. } break;
  922. case WINDOW_MOVE_DOWN: {
  923. if (tab_container->get_current_tab() < tab_container->get_child_count() - 1) {
  924. tab_container->move_child(current, tab_container->get_current_tab() + 1);
  925. tab_container->set_current_tab(tab_container->get_current_tab() + 1);
  926. _update_script_names();
  927. }
  928. } break;
  929. default: {
  930. if (p_option >= WINDOW_SELECT_BASE) {
  931. tab_container->set_current_tab(p_option - WINDOW_SELECT_BASE);
  932. script_list->select(p_option - WINDOW_SELECT_BASE);
  933. }
  934. }
  935. }
  936. } else {
  937. EditorHelp *help = Object::cast_to<EditorHelp>(tab_container->get_current_tab_control());
  938. if (help) {
  939. switch (p_option) {
  940. case HELP_SEARCH_FIND: {
  941. help->popup_search();
  942. } break;
  943. case HELP_SEARCH_FIND_NEXT: {
  944. help->search_again();
  945. } break;
  946. case FILE_CLOSE: {
  947. _close_current_tab();
  948. } break;
  949. case CLOSE_DOCS: {
  950. _close_docs_tab();
  951. } break;
  952. case CLOSE_OTHER_TABS: {
  953. _close_other_tabs();
  954. } break;
  955. case CLOSE_ALL: {
  956. _close_all_tabs();
  957. } break;
  958. case WINDOW_MOVE_UP: {
  959. if (tab_container->get_current_tab() > 0) {
  960. tab_container->move_child(help, tab_container->get_current_tab() - 1);
  961. tab_container->set_current_tab(tab_container->get_current_tab() - 1);
  962. _update_script_names();
  963. }
  964. } break;
  965. case WINDOW_MOVE_DOWN: {
  966. if (tab_container->get_current_tab() < tab_container->get_child_count() - 1) {
  967. tab_container->move_child(help, tab_container->get_current_tab() + 1);
  968. tab_container->set_current_tab(tab_container->get_current_tab() + 1);
  969. _update_script_names();
  970. }
  971. } break;
  972. }
  973. }
  974. }
  975. }
  976. void ScriptEditor::_theme_option(int p_option) {
  977. switch (p_option) {
  978. case THEME_IMPORT: {
  979. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  980. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  981. file_dialog_option = THEME_IMPORT;
  982. file_dialog->clear_filters();
  983. file_dialog->add_filter("*.tet");
  984. file_dialog->popup_centered_ratio();
  985. file_dialog->set_title(TTR("Import Theme"));
  986. } break;
  987. case THEME_RELOAD: {
  988. EditorSettings::get_singleton()->load_text_editor_theme();
  989. } break;
  990. case THEME_SAVE: {
  991. if (!EditorSettings::get_singleton()->save_text_editor_theme()) {
  992. editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  993. }
  994. } break;
  995. case THEME_SAVE_AS: {
  996. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  997. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  998. file_dialog_option = THEME_SAVE_AS;
  999. file_dialog->clear_filters();
  1000. file_dialog->add_filter("*.tet");
  1001. file_dialog->set_current_path(EditorSettings::get_singleton()->get_text_editor_themes_dir().plus_file(EditorSettings::get_singleton()->get("text_editor/theme/color_theme")));
  1002. file_dialog->popup_centered_ratio();
  1003. file_dialog->set_title(TTR("Save Theme As..."));
  1004. } break;
  1005. }
  1006. }
  1007. void ScriptEditor::_tab_changed(int p_which) {
  1008. ensure_select_current();
  1009. }
  1010. void ScriptEditor::_notification(int p_what) {
  1011. switch (p_what) {
  1012. case NOTIFICATION_ENTER_TREE: {
  1013. editor->connect("play_pressed", this, "_editor_play");
  1014. editor->connect("pause_pressed", this, "_editor_pause");
  1015. editor->connect("stop_pressed", this, "_editor_stop");
  1016. editor->connect("script_add_function_request", this, "_add_callback");
  1017. editor->connect("resource_saved", this, "_res_saved_callback");
  1018. script_list->connect("item_selected", this, "_script_selected");
  1019. members_overview->connect("item_selected", this, "_members_overview_selected");
  1020. help_overview->connect("item_selected", this, "_help_overview_selected");
  1021. script_split->connect("dragged", this, "_script_split_dragged");
  1022. autosave_timer->connect("timeout", this, "_autosave_scripts");
  1023. {
  1024. float autosave_time = EditorSettings::get_singleton()->get("text_editor/files/autosave_interval_secs");
  1025. if (autosave_time > 0) {
  1026. autosave_timer->set_wait_time(autosave_time);
  1027. autosave_timer->start();
  1028. } else {
  1029. autosave_timer->stop();
  1030. }
  1031. }
  1032. EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
  1033. help_search->set_icon(get_icon("HelpSearch", "EditorIcons"));
  1034. site_search->set_icon(get_icon("Instance", "EditorIcons"));
  1035. request_docs->set_icon(get_icon("Issue", "EditorIcons"));
  1036. script_forward->set_icon(get_icon("Forward", "EditorIcons"));
  1037. script_back->set_icon(get_icon("Back", "EditorIcons"));
  1038. members_overview_alphabeta_sort_button->set_icon(get_icon("Sort", "EditorIcons"));
  1039. } break;
  1040. case NOTIFICATION_READY: {
  1041. get_tree()->connect("tree_changed", this, "_tree_changed");
  1042. editor->get_inspector_dock()->connect("request_help", this, "_request_help");
  1043. editor->connect("request_help_search", this, "_help_search");
  1044. } break;
  1045. case NOTIFICATION_EXIT_TREE: {
  1046. editor->disconnect("play_pressed", this, "_editor_play");
  1047. editor->disconnect("pause_pressed", this, "_editor_pause");
  1048. editor->disconnect("stop_pressed", this, "_editor_stop");
  1049. } break;
  1050. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  1051. _test_script_times_on_disk();
  1052. _update_modified_scripts_for_external_editor();
  1053. } break;
  1054. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1055. help_search->set_icon(get_icon("HelpSearch", "EditorIcons"));
  1056. site_search->set_icon(get_icon("Instance", "EditorIcons"));
  1057. script_forward->set_icon(get_icon("Forward", "EditorIcons"));
  1058. script_back->set_icon(get_icon("Back", "EditorIcons"));
  1059. members_overview_alphabeta_sort_button->set_icon(get_icon("Sort", "EditorIcons"));
  1060. filename->add_style_override("normal", editor->get_gui_base()->get_stylebox("normal", "LineEdit"));
  1061. recent_scripts->set_as_minsize();
  1062. } break;
  1063. case CanvasItem::NOTIFICATION_VISIBILITY_CHANGED: {
  1064. if (is_visible()) {
  1065. find_in_files_button->show();
  1066. } else {
  1067. if (find_in_files->is_visible_in_tree()) {
  1068. editor->hide_bottom_panel();
  1069. }
  1070. find_in_files_button->hide();
  1071. }
  1072. } break;
  1073. default:
  1074. break;
  1075. }
  1076. }
  1077. bool ScriptEditor::can_take_away_focus() const {
  1078. ScriptEditorBase *current = _get_current_editor();
  1079. if (current)
  1080. return current->can_lose_focus_on_node_selection();
  1081. else
  1082. return true;
  1083. }
  1084. void ScriptEditor::close_builtin_scripts_from_scene(const String &p_scene) {
  1085. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1086. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1087. if (se) {
  1088. Ref<Script> script = se->get_edited_resource();
  1089. if (script == NULL || !script.is_valid())
  1090. continue;
  1091. if (script->get_path().find("::") != -1 && script->get_path().begins_with(p_scene)) { //is an internal script and belongs to scene being closed
  1092. _close_tab(i);
  1093. i--;
  1094. }
  1095. }
  1096. }
  1097. }
  1098. void ScriptEditor::edited_scene_changed() {
  1099. _update_modified_scripts_for_external_editor();
  1100. }
  1101. void ScriptEditor::notify_script_close(const Ref<Script> &p_script) {
  1102. emit_signal("script_close", p_script);
  1103. }
  1104. void ScriptEditor::notify_script_changed(const Ref<Script> &p_script) {
  1105. emit_signal("editor_script_changed", p_script);
  1106. }
  1107. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  1108. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1109. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1110. if (!se)
  1111. continue;
  1112. Ref<Script> script = se->get_edited_resource();
  1113. if (script == NULL) {
  1114. continue;
  1115. }
  1116. List<int> bpoints;
  1117. se->get_breakpoints(&bpoints);
  1118. String base = script->get_path();
  1119. ERR_CONTINUE(base.begins_with("local://") || base == "");
  1120. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  1121. p_breakpoints->push_back(base + ":" + itos(E->get() + 1));
  1122. }
  1123. }
  1124. }
  1125. void ScriptEditor::ensure_focus_current() {
  1126. if (!is_inside_tree())
  1127. return;
  1128. ScriptEditorBase *current = _get_current_editor();
  1129. if (current)
  1130. current->ensure_focus();
  1131. }
  1132. void ScriptEditor::_members_overview_selected(int p_idx) {
  1133. ScriptEditorBase *se = _get_current_editor();
  1134. if (!se) {
  1135. return;
  1136. }
  1137. // Go to the member's line and reset the cursor column. We can't change scroll_position
  1138. // directly until we have gone to the line first, since code might be folded.
  1139. se->goto_line(members_overview->get_item_metadata(p_idx));
  1140. Dictionary state = se->get_edit_state();
  1141. state["column"] = 0;
  1142. state["scroll_position"] = members_overview->get_item_metadata(p_idx);
  1143. se->set_edit_state(state);
  1144. }
  1145. void ScriptEditor::_help_overview_selected(int p_idx) {
  1146. Node *current = tab_container->get_child(tab_container->get_current_tab());
  1147. EditorHelp *se = Object::cast_to<EditorHelp>(current);
  1148. if (!se) {
  1149. return;
  1150. }
  1151. se->scroll_to_section(help_overview->get_item_metadata(p_idx));
  1152. }
  1153. void ScriptEditor::_script_selected(int p_idx) {
  1154. grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing
  1155. _go_to_tab(script_list->get_item_metadata(p_idx));
  1156. grab_focus_block = false;
  1157. }
  1158. void ScriptEditor::ensure_select_current() {
  1159. if (tab_container->get_child_count() && tab_container->get_current_tab() >= 0) {
  1160. ScriptEditorBase *se = _get_current_editor();
  1161. if (se) {
  1162. if (!grab_focus_block && is_visible_in_tree())
  1163. se->ensure_focus();
  1164. }
  1165. }
  1166. _update_selected_editor_menu();
  1167. }
  1168. void ScriptEditor::_find_scripts(Node *p_base, Node *p_current, Set<Ref<Script> > &used) {
  1169. if (p_current != p_base && p_current->get_owner() != p_base)
  1170. return;
  1171. if (p_current->get_script_instance()) {
  1172. Ref<Script> scr = p_current->get_script();
  1173. if (scr.is_valid())
  1174. used.insert(scr);
  1175. }
  1176. for (int i = 0; i < p_current->get_child_count(); i++) {
  1177. _find_scripts(p_base, p_current->get_child(i), used);
  1178. }
  1179. }
  1180. struct _ScriptEditorItemData {
  1181. String name;
  1182. String sort_key;
  1183. Ref<Texture> icon;
  1184. int index;
  1185. String tooltip;
  1186. bool used;
  1187. int category;
  1188. Node *ref;
  1189. bool operator<(const _ScriptEditorItemData &id) const {
  1190. return category == id.category ? sort_key < id.sort_key : category < id.category;
  1191. }
  1192. };
  1193. void ScriptEditor::_update_members_overview_visibility() {
  1194. ScriptEditorBase *se = _get_current_editor();
  1195. if (!se) {
  1196. members_overview_alphabeta_sort_button->set_visible(false);
  1197. members_overview->set_visible(false);
  1198. overview_vbox->set_visible(false);
  1199. return;
  1200. }
  1201. if (members_overview_enabled && se->show_members_overview()) {
  1202. members_overview_alphabeta_sort_button->set_visible(true);
  1203. members_overview->set_visible(true);
  1204. overview_vbox->set_visible(true);
  1205. } else {
  1206. members_overview_alphabeta_sort_button->set_visible(false);
  1207. members_overview->set_visible(false);
  1208. overview_vbox->set_visible(false);
  1209. }
  1210. }
  1211. void ScriptEditor::_toggle_members_overview_alpha_sort(bool p_alphabetic_sort) {
  1212. EditorSettings::get_singleton()->set("text_editor/tools/sort_members_outline_alphabetically", p_alphabetic_sort);
  1213. _update_members_overview();
  1214. }
  1215. void ScriptEditor::_update_members_overview() {
  1216. members_overview->clear();
  1217. ScriptEditorBase *se = _get_current_editor();
  1218. if (!se) {
  1219. return;
  1220. }
  1221. Vector<String> functions = se->get_functions();
  1222. if (EditorSettings::get_singleton()->get("text_editor/tools/sort_members_outline_alphabetically")) {
  1223. functions.sort();
  1224. }
  1225. for (int i = 0; i < functions.size(); i++) {
  1226. members_overview->add_item(functions[i].get_slice(":", 0));
  1227. members_overview->set_item_metadata(i, functions[i].get_slice(":", 1).to_int() - 1);
  1228. }
  1229. String path = se->get_edited_resource()->get_path();
  1230. bool built_in = !path.is_resource_file();
  1231. String name = built_in ? path.get_file() : se->get_name();
  1232. filename->set_text(name);
  1233. }
  1234. void ScriptEditor::_update_help_overview_visibility() {
  1235. int selected = tab_container->get_current_tab();
  1236. if (selected < 0 || selected >= tab_container->get_child_count()) {
  1237. help_overview->set_visible(false);
  1238. return;
  1239. }
  1240. Node *current = tab_container->get_child(tab_container->get_current_tab());
  1241. EditorHelp *se = Object::cast_to<EditorHelp>(current);
  1242. if (!se) {
  1243. help_overview->set_visible(false);
  1244. return;
  1245. }
  1246. if (help_overview_enabled) {
  1247. members_overview_alphabeta_sort_button->set_visible(false);
  1248. help_overview->set_visible(true);
  1249. overview_vbox->set_visible(true);
  1250. filename->set_text(se->get_name());
  1251. } else {
  1252. help_overview->set_visible(false);
  1253. overview_vbox->set_visible(false);
  1254. }
  1255. }
  1256. void ScriptEditor::_update_help_overview() {
  1257. help_overview->clear();
  1258. int selected = tab_container->get_current_tab();
  1259. if (selected < 0 || selected >= tab_container->get_child_count())
  1260. return;
  1261. Node *current = tab_container->get_child(tab_container->get_current_tab());
  1262. EditorHelp *se = Object::cast_to<EditorHelp>(current);
  1263. if (!se) {
  1264. return;
  1265. }
  1266. Vector<Pair<String, int> > sections = se->get_sections();
  1267. for (int i = 0; i < sections.size(); i++) {
  1268. help_overview->add_item(sections[i].first);
  1269. help_overview->set_item_metadata(i, sections[i].second);
  1270. }
  1271. }
  1272. void ScriptEditor::_update_script_colors() {
  1273. bool script_temperature_enabled = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_enabled");
  1274. bool highlight_current = EditorSettings::get_singleton()->get("text_editor/open_scripts/highlight_current_script");
  1275. int hist_size = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_history_size");
  1276. Color hot_color = get_color("accent_color", "Editor");
  1277. Color cold_color = get_color("font_color", "Editor");
  1278. for (int i = 0; i < script_list->get_item_count(); i++) {
  1279. int c = script_list->get_item_metadata(i);
  1280. Node *n = tab_container->get_child(c);
  1281. if (!n)
  1282. continue;
  1283. script_list->set_item_custom_bg_color(i, Color(0, 0, 0, 0));
  1284. bool current = tab_container->get_current_tab() == c;
  1285. if (current && highlight_current) {
  1286. script_list->set_item_custom_bg_color(i, EditorSettings::get_singleton()->get("text_editor/open_scripts/current_script_background_color"));
  1287. } else if (script_temperature_enabled) {
  1288. if (!n->has_meta("__editor_pass")) {
  1289. continue;
  1290. }
  1291. int pass = n->get_meta("__editor_pass");
  1292. int h = edit_pass - pass;
  1293. if (h > hist_size) {
  1294. continue;
  1295. }
  1296. int non_zero_hist_size = (hist_size == 0) ? 1 : hist_size;
  1297. float v = Math::ease((edit_pass - pass) / float(non_zero_hist_size), 0.4);
  1298. script_list->set_item_custom_fg_color(i, hot_color.linear_interpolate(cold_color, v));
  1299. }
  1300. }
  1301. }
  1302. void ScriptEditor::_update_script_names() {
  1303. if (restoring_layout)
  1304. return;
  1305. Set<Ref<Script> > used;
  1306. Node *edited = EditorNode::get_singleton()->get_edited_scene();
  1307. if (edited) {
  1308. _find_scripts(edited, edited, used);
  1309. }
  1310. script_list->clear();
  1311. bool split_script_help = EditorSettings::get_singleton()->get("text_editor/open_scripts/group_help_pages");
  1312. ScriptSortBy sort_by = (ScriptSortBy)(int)EditorSettings::get_singleton()->get("text_editor/open_scripts/sort_scripts_by");
  1313. ScriptListName display_as = (ScriptListName)(int)EditorSettings::get_singleton()->get("text_editor/open_scripts/list_script_names_as");
  1314. Vector<_ScriptEditorItemData> sedata;
  1315. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1316. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1317. if (se) {
  1318. Ref<Texture> icon = se->get_icon();
  1319. String path = se->get_edited_resource()->get_path();
  1320. bool built_in = !path.is_resource_file();
  1321. String name;
  1322. if (built_in) {
  1323. name = path.get_file();
  1324. String resource_name = se->get_edited_resource()->get_name();
  1325. if (resource_name != "") {
  1326. name = name.substr(0, name.find("::", 0) + 2) + resource_name;
  1327. }
  1328. } else {
  1329. name = se->get_name();
  1330. }
  1331. _ScriptEditorItemData sd;
  1332. sd.icon = icon;
  1333. sd.name = name;
  1334. sd.tooltip = path;
  1335. sd.index = i;
  1336. sd.used = used.has(se->get_edited_resource());
  1337. sd.category = 0;
  1338. sd.ref = se;
  1339. switch (sort_by) {
  1340. case SORT_BY_NAME: {
  1341. sd.sort_key = name.to_lower();
  1342. } break;
  1343. case SORT_BY_PATH: {
  1344. sd.sort_key = path;
  1345. } break;
  1346. case SORT_BY_NONE: {
  1347. sd.sort_key = "";
  1348. } break;
  1349. }
  1350. switch (display_as) {
  1351. case DISPLAY_NAME: {
  1352. sd.name = name;
  1353. } break;
  1354. case DISPLAY_DIR_AND_NAME: {
  1355. if (!path.get_base_dir().get_file().empty()) {
  1356. sd.name = path.get_base_dir().get_file().plus_file(name);
  1357. } else {
  1358. sd.name = name;
  1359. }
  1360. } break;
  1361. case DISPLAY_FULL_PATH: {
  1362. sd.name = path;
  1363. } break;
  1364. }
  1365. sedata.push_back(sd);
  1366. }
  1367. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_child(i));
  1368. if (eh) {
  1369. String name = eh->get_class();
  1370. Ref<Texture> icon = get_icon("Help", "EditorIcons");
  1371. String tooltip = vformat(TTR("%s Class Reference"), name);
  1372. _ScriptEditorItemData sd;
  1373. sd.icon = icon;
  1374. sd.name = name;
  1375. sd.sort_key = name.to_lower();
  1376. sd.tooltip = tooltip;
  1377. sd.index = i;
  1378. sd.used = false;
  1379. sd.category = split_script_help ? 1 : 0;
  1380. sd.ref = eh;
  1381. sedata.push_back(sd);
  1382. }
  1383. }
  1384. if (_sort_list_on_update && !sedata.empty()) {
  1385. sedata.sort();
  1386. // change actual order of tab_container so that the order can be rearranged by user
  1387. int cur_tab = tab_container->get_current_tab();
  1388. int prev_tab = tab_container->get_previous_tab();
  1389. int new_cur_tab = -1;
  1390. int new_prev_tab = -1;
  1391. for (int i = 0; i < sedata.size(); i++) {
  1392. tab_container->move_child(sedata[i].ref, i);
  1393. if (new_prev_tab == -1 && sedata[i].index == prev_tab) {
  1394. new_prev_tab = i;
  1395. }
  1396. if (new_cur_tab == -1 && sedata[i].index == cur_tab) {
  1397. new_cur_tab = i;
  1398. }
  1399. }
  1400. tab_container->set_current_tab(new_prev_tab);
  1401. tab_container->set_current_tab(new_cur_tab);
  1402. _sort_list_on_update = false;
  1403. }
  1404. for (int i = 0; i < sedata.size(); i++) {
  1405. script_list->add_item(sedata[i].name, sedata[i].icon);
  1406. int index = script_list->get_item_count() - 1;
  1407. script_list->set_item_tooltip(index, sedata[i].tooltip);
  1408. script_list->set_item_metadata(index, sedata[i].index);
  1409. if (sedata[i].used) {
  1410. script_list->set_item_custom_bg_color(index, Color(88 / 255.0, 88 / 255.0, 60 / 255.0));
  1411. }
  1412. if (tab_container->get_current_tab() == sedata[i].index) {
  1413. script_list->select(index);
  1414. script_name_label->set_text(sedata[i].name);
  1415. script_icon->set_texture(sedata[i].icon);
  1416. }
  1417. }
  1418. if (!waiting_update_names) {
  1419. _update_members_overview();
  1420. _update_help_overview();
  1421. } else {
  1422. waiting_update_names = false;
  1423. }
  1424. _update_members_overview_visibility();
  1425. _update_help_overview_visibility();
  1426. _update_script_colors();
  1427. }
  1428. Ref<TextFile> ScriptEditor::_load_text_file(const String &p_path, Error *r_error) {
  1429. if (r_error) {
  1430. *r_error = ERR_FILE_CANT_OPEN;
  1431. }
  1432. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1433. String path = ResourceLoader::path_remap(local_path);
  1434. TextFile *text_file = memnew(TextFile);
  1435. Ref<TextFile> text_res(text_file);
  1436. Error err = text_file->load_text(path);
  1437. if (err != OK) {
  1438. ERR_FAIL_COND_V(err != OK, RES());
  1439. }
  1440. text_file->set_file_path(local_path);
  1441. text_file->set_path(local_path, true);
  1442. if (ResourceLoader::get_timestamp_on_load()) {
  1443. text_file->set_last_modified_time(FileAccess::get_modified_time(path));
  1444. }
  1445. if (r_error) {
  1446. *r_error = OK;
  1447. }
  1448. return text_res;
  1449. }
  1450. Error ScriptEditor::_save_text_file(Ref<TextFile> p_text_file, const String &p_path) {
  1451. Ref<TextFile> sqscr = p_text_file;
  1452. ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER);
  1453. String source = sqscr->get_text();
  1454. Error err;
  1455. FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1456. if (err) {
  1457. ERR_FAIL_COND_V(err, err);
  1458. }
  1459. file->store_string(source);
  1460. if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
  1461. memdelete(file);
  1462. return ERR_CANT_CREATE;
  1463. }
  1464. file->close();
  1465. memdelete(file);
  1466. if (ResourceSaver::get_timestamp_on_save()) {
  1467. p_text_file->set_last_modified_time(FileAccess::get_modified_time(p_path));
  1468. }
  1469. _res_saved_callback(sqscr);
  1470. return OK;
  1471. }
  1472. bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_grab_focus) {
  1473. if (p_resource.is_null())
  1474. return false;
  1475. Ref<Script> script = p_resource;
  1476. // refuse to open built-in if scene is not loaded
  1477. // see if already has it
  1478. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/files/open_dominant_script_on_scene_change");
  1479. const bool should_open = open_dominant || !EditorNode::get_singleton()->is_changing_scene();
  1480. if (script != NULL && script->get_language()->overrides_external_editor()) {
  1481. if (should_open) {
  1482. Error err = script->get_language()->open_in_external_editor(script, p_line >= 0 ? p_line : 0, p_col);
  1483. if (err != OK)
  1484. ERR_PRINT("Couldn't open script in the overridden external text editor");
  1485. }
  1486. return false;
  1487. }
  1488. if ((debugger->get_dump_stack_script() != p_resource || debugger->get_debug_with_external_editor()) &&
  1489. p_resource->get_path().is_resource_file() &&
  1490. p_resource->get_class_name() != StringName("VisualScript") &&
  1491. bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor"))) {
  1492. String path = EditorSettings::get_singleton()->get("text_editor/external/exec_path");
  1493. String flags = EditorSettings::get_singleton()->get("text_editor/external/exec_flags");
  1494. List<String> args;
  1495. if (flags.size()) {
  1496. String project_path = ProjectSettings::get_singleton()->get_resource_path();
  1497. String script_path = ProjectSettings::get_singleton()->globalize_path(p_resource->get_path());
  1498. flags = flags.replacen("{line}", itos(p_line > 0 ? p_line : 0));
  1499. flags = flags.replacen("{col}", itos(p_col));
  1500. flags = flags.strip_edges().replace("\\\\", "\\");
  1501. int from = 0;
  1502. int num_chars = 0;
  1503. bool inside_quotes = false;
  1504. for (int i = 0; i < flags.size(); i++) {
  1505. if (flags[i] == '"' && (!i || flags[i - 1] != '\\')) {
  1506. if (!inside_quotes) {
  1507. from++;
  1508. }
  1509. inside_quotes = !inside_quotes;
  1510. } else if (flags[i] == '\0' || (!inside_quotes && flags[i] == ' ')) {
  1511. String arg = flags.substr(from, num_chars);
  1512. // do path replacement here, else there will be issues with spaces and quotes
  1513. arg = arg.replacen("{project}", project_path);
  1514. arg = arg.replacen("{file}", script_path);
  1515. args.push_back(arg);
  1516. from = i + 1;
  1517. num_chars = 0;
  1518. } else {
  1519. num_chars++;
  1520. }
  1521. }
  1522. }
  1523. Error err = OS::get_singleton()->execute(path, args, false);
  1524. if (err == OK)
  1525. return false;
  1526. WARN_PRINT("Couldn't open external text editor, using internal");
  1527. }
  1528. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1529. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1530. if (!se)
  1531. continue;
  1532. if ((script != NULL && se->get_edited_resource() == p_resource) || se->get_edited_resource()->get_path() == p_resource->get_path()) {
  1533. if (should_open) {
  1534. if (tab_container->get_current_tab() != i) {
  1535. _go_to_tab(i);
  1536. script_list->select(script_list->find_metadata(i));
  1537. }
  1538. if (is_visible_in_tree())
  1539. se->ensure_focus();
  1540. if (p_line > 0) {
  1541. se->goto_line(p_line - 1);
  1542. }
  1543. }
  1544. return true;
  1545. }
  1546. }
  1547. // doesn't have it, make a new one
  1548. ScriptEditorBase *se = NULL;
  1549. for (int i = script_editor_func_count - 1; i >= 0; i--) {
  1550. se = script_editor_funcs[i](p_resource);
  1551. if (se)
  1552. break;
  1553. }
  1554. ERR_FAIL_COND_V(!se, false);
  1555. bool highlighter_set = false;
  1556. for (int i = 0; i < syntax_highlighters_func_count; i++) {
  1557. SyntaxHighlighter *highlighter = syntax_highlighters_funcs[i]();
  1558. se->add_syntax_highlighter(highlighter);
  1559. if (script != NULL && !highlighter_set) {
  1560. List<String> languages = highlighter->get_supported_languages();
  1561. if (languages.find(script->get_language()->get_name())) {
  1562. se->set_syntax_highlighter(highlighter);
  1563. highlighter_set = true;
  1564. }
  1565. }
  1566. }
  1567. tab_container->add_child(se);
  1568. se->set_edited_resource(p_resource);
  1569. se->set_tooltip_request_func("_get_debug_tooltip", this);
  1570. if (se->get_edit_menu()) {
  1571. se->get_edit_menu()->hide();
  1572. menu_hb->add_child(se->get_edit_menu());
  1573. menu_hb->move_child(se->get_edit_menu(), 1);
  1574. }
  1575. if (p_grab_focus) {
  1576. _go_to_tab(tab_container->get_tab_count() - 1);
  1577. }
  1578. _sort_list_on_update = true;
  1579. _update_script_names();
  1580. _save_layout();
  1581. se->connect("name_changed", this, "_update_script_names");
  1582. se->connect("edited_script_changed", this, "_script_changed");
  1583. se->connect("request_help", this, "_help_search");
  1584. se->connect("request_open_script_at_line", this, "_goto_script_line");
  1585. se->connect("go_to_help", this, "_help_class_goto");
  1586. se->connect("request_save_history", this, "_save_history");
  1587. se->connect("search_in_files_requested", this, "_on_find_in_files_requested");
  1588. //test for modification, maybe the script was not edited but was loaded
  1589. _test_script_times_on_disk(p_resource);
  1590. _update_modified_scripts_for_external_editor(p_resource);
  1591. if (p_line > 0) {
  1592. se->goto_line(p_line - 1);
  1593. }
  1594. notify_script_changed(p_resource);
  1595. _add_recent_script(p_resource->get_path());
  1596. return true;
  1597. }
  1598. void ScriptEditor::save_all_scripts() {
  1599. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1600. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1601. if (!se)
  1602. continue;
  1603. if (convert_indent_on_save) {
  1604. if (use_space_indentation) {
  1605. se->convert_indent_to_spaces();
  1606. } else {
  1607. se->convert_indent_to_tabs();
  1608. }
  1609. }
  1610. if (trim_trailing_whitespace_on_save) {
  1611. se->trim_trailing_whitespace();
  1612. }
  1613. if (!se->is_unsaved())
  1614. continue;
  1615. RES edited_res = se->get_edited_resource();
  1616. if (edited_res.is_valid()) {
  1617. se->apply_code();
  1618. }
  1619. if (edited_res->get_path() != "" && edited_res->get_path().find("local://") == -1 && edited_res->get_path().find("::") == -1) {
  1620. Ref<TextFile> text_file = edited_res;
  1621. if (text_file != NULL) {
  1622. _save_text_file(text_file, text_file->get_path());
  1623. continue;
  1624. }
  1625. editor->save_resource(edited_res); //external script, save it
  1626. }
  1627. }
  1628. _update_script_names();
  1629. EditorFileSystem::get_singleton()->update_script_classes();
  1630. }
  1631. void ScriptEditor::apply_scripts() const {
  1632. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1633. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1634. if (!se)
  1635. continue;
  1636. se->apply_code();
  1637. }
  1638. }
  1639. void ScriptEditor::open_script_create_dialog(const String &p_base_name, const String &p_base_path) {
  1640. _menu_option(FILE_NEW);
  1641. script_create_dialog->config(p_base_name, p_base_path);
  1642. }
  1643. void ScriptEditor::_editor_play() {
  1644. debugger->start();
  1645. debug_menu->get_popup()->grab_focus();
  1646. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1647. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  1648. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false);
  1649. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  1650. }
  1651. void ScriptEditor::_editor_pause() {
  1652. }
  1653. void ScriptEditor::_editor_stop() {
  1654. debugger->stop();
  1655. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  1656. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  1657. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true);
  1658. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  1659. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1660. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1661. if (!se) {
  1662. continue;
  1663. }
  1664. se->set_debugger_active(false);
  1665. }
  1666. }
  1667. void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const PoolStringArray &p_args) {
  1668. ERR_FAIL_COND(!p_obj);
  1669. Ref<Script> script = p_obj->get_script();
  1670. ERR_FAIL_COND(!script.is_valid());
  1671. editor->push_item(script.ptr());
  1672. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1673. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1674. if (!se)
  1675. continue;
  1676. if (se->get_edited_resource() != script)
  1677. continue;
  1678. se->add_callback(p_function, p_args);
  1679. _go_to_tab(i);
  1680. script_list->select(script_list->find_metadata(i));
  1681. break;
  1682. }
  1683. }
  1684. void ScriptEditor::_save_layout() {
  1685. if (restoring_layout) {
  1686. return;
  1687. }
  1688. editor->save_layout();
  1689. }
  1690. void ScriptEditor::_editor_settings_changed() {
  1691. trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/files/trim_trailing_whitespace_on_save");
  1692. convert_indent_on_save = EditorSettings::get_singleton()->get("text_editor/indent/convert_indent_on_save");
  1693. use_space_indentation = EditorSettings::get_singleton()->get("text_editor/indent/type");
  1694. members_overview_enabled = EditorSettings::get_singleton()->get("text_editor/open_scripts/show_members_overview");
  1695. help_overview_enabled = EditorSettings::get_singleton()->get("text_editor/help/show_help_index");
  1696. _update_members_overview_visibility();
  1697. _update_help_overview_visibility();
  1698. float autosave_time = EditorSettings::get_singleton()->get("text_editor/files/autosave_interval_secs");
  1699. if (autosave_time > 0) {
  1700. autosave_timer->set_wait_time(autosave_time);
  1701. autosave_timer->start();
  1702. } else {
  1703. autosave_timer->stop();
  1704. }
  1705. if (current_theme == "") {
  1706. current_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
  1707. } else if (current_theme != EditorSettings::get_singleton()->get("text_editor/theme/color_theme")) {
  1708. current_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
  1709. EditorSettings::get_singleton()->load_text_editor_theme();
  1710. }
  1711. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1712. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1713. if (!se)
  1714. continue;
  1715. se->update_settings();
  1716. }
  1717. _update_script_colors();
  1718. _update_script_names();
  1719. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/files/auto_reload_and_parse_scripts_on_save", true));
  1720. }
  1721. void ScriptEditor::_autosave_scripts() {
  1722. save_all_scripts();
  1723. }
  1724. void ScriptEditor::_tree_changed() {
  1725. if (waiting_update_names)
  1726. return;
  1727. waiting_update_names = true;
  1728. call_deferred("_update_script_names");
  1729. }
  1730. void ScriptEditor::_script_split_dragged(float) {
  1731. _save_layout();
  1732. }
  1733. Variant ScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1734. if (tab_container->get_child_count() == 0)
  1735. return Variant();
  1736. Node *cur_node = tab_container->get_child(tab_container->get_current_tab());
  1737. HBoxContainer *drag_preview = memnew(HBoxContainer);
  1738. String preview_name = "";
  1739. Ref<Texture> preview_icon;
  1740. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(cur_node);
  1741. if (se) {
  1742. preview_name = se->get_name();
  1743. preview_icon = se->get_icon();
  1744. }
  1745. EditorHelp *eh = Object::cast_to<EditorHelp>(cur_node);
  1746. if (eh) {
  1747. preview_name = eh->get_class();
  1748. preview_icon = get_icon("Help", "EditorIcons");
  1749. }
  1750. if (!preview_icon.is_null()) {
  1751. TextureRect *tf = memnew(TextureRect);
  1752. tf->set_texture(preview_icon);
  1753. drag_preview->add_child(tf);
  1754. }
  1755. Label *label = memnew(Label(preview_name));
  1756. drag_preview->add_child(label);
  1757. set_drag_preview(drag_preview);
  1758. Dictionary drag_data;
  1759. drag_data["type"] = "script_list_element"; // using a custom type because node caused problems when dragging to scene tree
  1760. drag_data["script_list_element"] = cur_node;
  1761. return drag_data;
  1762. }
  1763. bool ScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1764. Dictionary d = p_data;
  1765. if (!d.has("type"))
  1766. return false;
  1767. if (String(d["type"]) == "script_list_element") {
  1768. Node *node = d["script_list_element"];
  1769. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  1770. if (se) {
  1771. return true;
  1772. }
  1773. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  1774. if (eh) {
  1775. return true;
  1776. }
  1777. }
  1778. if (String(d["type"]) == "nodes") {
  1779. Array nodes = d["nodes"];
  1780. if (nodes.size() == 0)
  1781. return false;
  1782. Node *node = get_node((nodes[0]));
  1783. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  1784. if (se) {
  1785. return true;
  1786. }
  1787. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  1788. if (eh) {
  1789. return true;
  1790. }
  1791. }
  1792. if (String(d["type"]) == "files") {
  1793. Vector<String> files = d["files"];
  1794. if (files.size() == 0)
  1795. return false; //weird
  1796. for (int i = 0; i < files.size(); i++) {
  1797. String file = files[i];
  1798. if (file == "" || !FileAccess::exists(file))
  1799. continue;
  1800. Ref<Script> scr = ResourceLoader::load(file);
  1801. if (scr.is_valid()) {
  1802. return true;
  1803. }
  1804. }
  1805. return true;
  1806. }
  1807. return false;
  1808. }
  1809. void ScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1810. if (!can_drop_data_fw(p_point, p_data, p_from))
  1811. return;
  1812. Dictionary d = p_data;
  1813. if (!d.has("type"))
  1814. return;
  1815. if (String(d["type"]) == "script_list_element") {
  1816. Node *node = d["script_list_element"];
  1817. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  1818. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  1819. if (se || eh) {
  1820. int new_index = script_list->get_item_at_position(p_point);
  1821. tab_container->move_child(node, new_index);
  1822. tab_container->set_current_tab(new_index);
  1823. _update_script_names();
  1824. }
  1825. }
  1826. if (String(d["type"]) == "nodes") {
  1827. Array nodes = d["nodes"];
  1828. if (nodes.size() == 0)
  1829. return;
  1830. Node *node = get_node(nodes[0]);
  1831. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  1832. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  1833. if (se || eh) {
  1834. int new_index = script_list->get_item_at_position(p_point);
  1835. tab_container->move_child(node, new_index);
  1836. tab_container->set_current_tab(new_index);
  1837. _update_script_names();
  1838. }
  1839. }
  1840. if (String(d["type"]) == "files") {
  1841. Vector<String> files = d["files"];
  1842. int new_index = script_list->get_item_at_position(p_point);
  1843. int num_tabs_before = tab_container->get_child_count();
  1844. for (int i = 0; i < files.size(); i++) {
  1845. String file = files[i];
  1846. if (file == "" || !FileAccess::exists(file))
  1847. continue;
  1848. Ref<Script> scr = ResourceLoader::load(file);
  1849. if (scr.is_valid()) {
  1850. edit(scr);
  1851. if (tab_container->get_child_count() > num_tabs_before) {
  1852. tab_container->move_child(tab_container->get_child(tab_container->get_child_count() - 1), new_index);
  1853. num_tabs_before = tab_container->get_child_count();
  1854. } else {
  1855. tab_container->move_child(tab_container->get_child(tab_container->get_current_tab()), new_index);
  1856. }
  1857. }
  1858. }
  1859. tab_container->set_current_tab(new_index);
  1860. _update_script_names();
  1861. }
  1862. }
  1863. void ScriptEditor::_unhandled_input(const Ref<InputEvent> &p_event) {
  1864. if (!is_visible_in_tree() || !p_event->is_pressed() || p_event->is_echo())
  1865. return;
  1866. if (ED_IS_SHORTCUT("script_editor/next_script", p_event)) {
  1867. int next_tab = script_list->get_current() + 1;
  1868. next_tab %= script_list->get_item_count();
  1869. _go_to_tab(script_list->get_item_metadata(next_tab));
  1870. _update_script_names();
  1871. }
  1872. if (ED_IS_SHORTCUT("script_editor/prev_script", p_event)) {
  1873. int next_tab = script_list->get_current() - 1;
  1874. next_tab = next_tab >= 0 ? next_tab : script_list->get_item_count() - 1;
  1875. _go_to_tab(script_list->get_item_metadata(next_tab));
  1876. _update_script_names();
  1877. }
  1878. if (ED_IS_SHORTCUT("script_editor/window_move_up", p_event)) {
  1879. _menu_option(WINDOW_MOVE_UP);
  1880. }
  1881. if (ED_IS_SHORTCUT("script_editor/window_move_down", p_event)) {
  1882. _menu_option(WINDOW_MOVE_DOWN);
  1883. }
  1884. }
  1885. void ScriptEditor::_script_list_gui_input(const Ref<InputEvent> &ev) {
  1886. Ref<InputEventMouseButton> mb = ev;
  1887. if (mb.is_valid() && mb->is_pressed()) {
  1888. switch (mb->get_button_index()) {
  1889. case BUTTON_MIDDLE: {
  1890. // Right-click selects automatically; middle-click does not.
  1891. int idx = script_list->get_item_at_position(mb->get_position(), true);
  1892. if (idx >= 0) {
  1893. script_list->select(idx);
  1894. _script_selected(idx);
  1895. _menu_option(FILE_CLOSE);
  1896. }
  1897. } break;
  1898. case BUTTON_RIGHT: {
  1899. _make_script_list_context_menu();
  1900. } break;
  1901. }
  1902. }
  1903. }
  1904. void ScriptEditor::_make_script_list_context_menu() {
  1905. context_menu->clear();
  1906. int selected = tab_container->get_current_tab();
  1907. if (selected < 0 || selected >= tab_container->get_child_count())
  1908. return;
  1909. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(selected));
  1910. if (se) {
  1911. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save"), FILE_SAVE);
  1912. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save_as"), FILE_SAVE_AS);
  1913. }
  1914. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_file"), FILE_CLOSE);
  1915. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_all"), CLOSE_ALL);
  1916. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_other_tabs"), CLOSE_OTHER_TABS);
  1917. context_menu->add_separator();
  1918. if (se) {
  1919. Ref<Script> scr = se->get_edited_resource();
  1920. if (scr != NULL) {
  1921. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/reload_script_soft"), FILE_TOOL_RELOAD_SOFT);
  1922. if (!scr.is_null() && scr->is_tool()) {
  1923. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/run_file"), FILE_RUN);
  1924. context_menu->add_separator();
  1925. }
  1926. }
  1927. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/copy_path"), FILE_COPY_PATH);
  1928. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/show_in_file_system"), SHOW_IN_FILE_SYSTEM);
  1929. context_menu->add_separator();
  1930. }
  1931. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_up"), WINDOW_MOVE_UP);
  1932. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_down"), WINDOW_MOVE_DOWN);
  1933. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_sort"), WINDOW_SORT);
  1934. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_scripts_panel"), TOGGLE_SCRIPTS_PANEL);
  1935. context_menu->set_position(get_global_transform().xform(get_local_mouse_position()));
  1936. context_menu->set_size(Vector2(1, 1));
  1937. context_menu->popup();
  1938. }
  1939. void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
  1940. if (!bool(EDITOR_DEF("text_editor/files/restore_scripts_on_load", true))) {
  1941. return;
  1942. }
  1943. if (!p_layout->has_section_key("ScriptEditor", "open_scripts") && !p_layout->has_section_key("ScriptEditor", "open_help"))
  1944. return;
  1945. Array scripts = p_layout->get_value("ScriptEditor", "open_scripts");
  1946. Array helps;
  1947. if (p_layout->has_section_key("ScriptEditor", "open_help"))
  1948. helps = p_layout->get_value("ScriptEditor", "open_help");
  1949. restoring_layout = true;
  1950. List<String> extensions;
  1951. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  1952. for (int i = 0; i < scripts.size(); i++) {
  1953. String path = scripts[i];
  1954. if (!FileAccess::exists(path))
  1955. continue;
  1956. if (extensions.find(path.get_extension())) {
  1957. Ref<Script> scr = ResourceLoader::load(path);
  1958. if (scr.is_valid()) {
  1959. edit(scr);
  1960. continue;
  1961. }
  1962. }
  1963. Error error;
  1964. Ref<TextFile> text_file = _load_text_file(path, &error);
  1965. if (error == OK && text_file.is_valid()) {
  1966. edit(text_file);
  1967. continue;
  1968. }
  1969. }
  1970. for (int i = 0; i < helps.size(); i++) {
  1971. String path = helps[i];
  1972. if (path == "") { // invalid, skip
  1973. continue;
  1974. }
  1975. _help_class_open(path);
  1976. }
  1977. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1978. tab_container->get_child(i)->set_meta("__editor_pass", Variant());
  1979. }
  1980. if (p_layout->has_section_key("ScriptEditor", "split_offset")) {
  1981. script_split->set_split_offset(p_layout->get_value("ScriptEditor", "split_offset"));
  1982. }
  1983. restoring_layout = false;
  1984. _update_script_names();
  1985. }
  1986. void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
  1987. Array scripts;
  1988. Array helps;
  1989. for (int i = 0; i < tab_container->get_child_count(); i++) {
  1990. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  1991. if (se) {
  1992. String path = se->get_edited_resource()->get_path();
  1993. if (!path.is_resource_file())
  1994. continue;
  1995. scripts.push_back(path);
  1996. }
  1997. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_child(i));
  1998. if (eh) {
  1999. helps.push_back(eh->get_class());
  2000. }
  2001. }
  2002. p_layout->set_value("ScriptEditor", "open_scripts", scripts);
  2003. p_layout->set_value("ScriptEditor", "open_help", helps);
  2004. p_layout->set_value("ScriptEditor", "split_offset", script_split->get_split_offset());
  2005. }
  2006. void ScriptEditor::_help_class_open(const String &p_class) {
  2007. if (p_class == "")
  2008. return;
  2009. for (int i = 0; i < tab_container->get_child_count(); i++) {
  2010. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_child(i));
  2011. if (eh && eh->get_class() == p_class) {
  2012. _go_to_tab(i);
  2013. _update_script_names();
  2014. return;
  2015. }
  2016. }
  2017. EditorHelp *eh = memnew(EditorHelp);
  2018. eh->set_name(p_class);
  2019. tab_container->add_child(eh);
  2020. _go_to_tab(tab_container->get_tab_count() - 1);
  2021. eh->go_to_class(p_class, 0);
  2022. eh->connect("go_to_help", this, "_help_class_goto");
  2023. _add_recent_script(p_class);
  2024. _sort_list_on_update = true;
  2025. _update_script_names();
  2026. _save_layout();
  2027. }
  2028. void ScriptEditor::_help_class_goto(const String &p_desc) {
  2029. String cname = p_desc.get_slice(":", 1);
  2030. for (int i = 0; i < tab_container->get_child_count(); i++) {
  2031. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_child(i));
  2032. if (eh && eh->get_class() == cname) {
  2033. _go_to_tab(i);
  2034. eh->go_to_help(p_desc);
  2035. _update_script_names();
  2036. return;
  2037. }
  2038. }
  2039. EditorHelp *eh = memnew(EditorHelp);
  2040. eh->set_name(cname);
  2041. tab_container->add_child(eh);
  2042. _go_to_tab(tab_container->get_tab_count() - 1);
  2043. eh->go_to_help(p_desc);
  2044. eh->connect("go_to_help", this, "_help_class_goto");
  2045. _add_recent_script(eh->get_class());
  2046. _sort_list_on_update = true;
  2047. _update_script_names();
  2048. _save_layout();
  2049. }
  2050. void ScriptEditor::_update_selected_editor_menu() {
  2051. for (int i = 0; i < tab_container->get_child_count(); i++) {
  2052. bool current = tab_container->get_current_tab() == i;
  2053. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  2054. if (se && se->get_edit_menu()) {
  2055. if (current)
  2056. se->get_edit_menu()->show();
  2057. else
  2058. se->get_edit_menu()->hide();
  2059. }
  2060. }
  2061. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_current_tab_control());
  2062. if (eh) {
  2063. script_search_menu->show();
  2064. } else {
  2065. script_search_menu->hide();
  2066. }
  2067. }
  2068. void ScriptEditor::_update_history_pos(int p_new_pos) {
  2069. Node *n = tab_container->get_current_tab_control();
  2070. if (Object::cast_to<ScriptEditorBase>(n)) {
  2071. history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_edit_state();
  2072. }
  2073. if (Object::cast_to<EditorHelp>(n)) {
  2074. history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();
  2075. }
  2076. history_pos = p_new_pos;
  2077. tab_container->set_current_tab(history[history_pos].control->get_index());
  2078. n = history[history_pos].control;
  2079. if (Object::cast_to<ScriptEditorBase>(n)) {
  2080. Object::cast_to<ScriptEditorBase>(n)->set_edit_state(history[history_pos].state);
  2081. Object::cast_to<ScriptEditorBase>(n)->ensure_focus();
  2082. Ref<Script> script = Object::cast_to<ScriptEditorBase>(n)->get_edited_resource();
  2083. if (script != NULL) {
  2084. notify_script_changed(script);
  2085. }
  2086. }
  2087. if (Object::cast_to<EditorHelp>(n)) {
  2088. Object::cast_to<EditorHelp>(n)->set_scroll(history[history_pos].state);
  2089. Object::cast_to<EditorHelp>(n)->set_focused();
  2090. }
  2091. n->set_meta("__editor_pass", ++edit_pass);
  2092. _update_script_names();
  2093. _update_history_arrows();
  2094. _update_selected_editor_menu();
  2095. }
  2096. void ScriptEditor::_history_forward() {
  2097. if (history_pos < history.size() - 1) {
  2098. _update_history_pos(history_pos + 1);
  2099. }
  2100. }
  2101. void ScriptEditor::_history_back() {
  2102. if (history_pos > 0) {
  2103. _update_history_pos(history_pos - 1);
  2104. }
  2105. }
  2106. Vector<Ref<Script> > ScriptEditor::get_open_scripts() const {
  2107. Vector<Ref<Script> > out_scripts = Vector<Ref<Script> >();
  2108. for (int i = 0; i < tab_container->get_child_count(); i++) {
  2109. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
  2110. if (!se)
  2111. continue;
  2112. Ref<Script> script = se->get_edited_resource();
  2113. if (script != NULL) {
  2114. out_scripts.push_back(script);
  2115. }
  2116. }
  2117. return out_scripts;
  2118. }
  2119. void ScriptEditor::set_scene_root_script(Ref<Script> p_script) {
  2120. bool open_dominant = EditorSettings::get_singleton()->get("text_editor/files/open_dominant_script_on_scene_change");
  2121. if (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor")))
  2122. return;
  2123. if (open_dominant && p_script.is_valid()) {
  2124. edit(p_script);
  2125. }
  2126. }
  2127. bool ScriptEditor::script_goto_method(Ref<Script> p_script, const String &p_method) {
  2128. int line = p_script->get_member_line(p_method);
  2129. if (line == -1)
  2130. return false;
  2131. return edit(p_script, line, 0);
  2132. }
  2133. void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) {
  2134. auto_reload_running_scripts = p_enabled;
  2135. }
  2136. void ScriptEditor::_help_search(String p_text) {
  2137. help_search_dialog->popup_dialog(p_text);
  2138. }
  2139. void ScriptEditor::_open_script_request(const String &p_path) {
  2140. Ref<Script> script = ResourceLoader::load(p_path);
  2141. if (script.is_valid()) {
  2142. script_editor->edit(script, false);
  2143. return;
  2144. }
  2145. Error err;
  2146. Ref<TextFile> text_file = script_editor->_load_text_file(p_path, &err);
  2147. if (text_file.is_valid()) {
  2148. script_editor->edit(text_file, false);
  2149. return;
  2150. }
  2151. }
  2152. int ScriptEditor::syntax_highlighters_func_count = 0;
  2153. CreateSyntaxHighlighterFunc ScriptEditor::syntax_highlighters_funcs[ScriptEditor::SYNTAX_HIGHLIGHTER_FUNC_MAX];
  2154. void ScriptEditor::register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func) {
  2155. ERR_FAIL_COND(syntax_highlighters_func_count == SYNTAX_HIGHLIGHTER_FUNC_MAX);
  2156. syntax_highlighters_funcs[syntax_highlighters_func_count++] = p_func;
  2157. }
  2158. int ScriptEditor::script_editor_func_count = 0;
  2159. CreateScriptEditorFunc ScriptEditor::script_editor_funcs[ScriptEditor::SCRIPT_EDITOR_FUNC_MAX];
  2160. void ScriptEditor::register_create_script_editor_function(CreateScriptEditorFunc p_func) {
  2161. ERR_FAIL_COND(script_editor_func_count == SCRIPT_EDITOR_FUNC_MAX);
  2162. script_editor_funcs[script_editor_func_count++] = p_func;
  2163. }
  2164. void ScriptEditor::_script_changed() {
  2165. NodeDock::singleton->update_lists();
  2166. }
  2167. void ScriptEditor::_on_find_in_files_requested(String text) {
  2168. find_in_files_dialog->set_search_text(text);
  2169. find_in_files_dialog->popup_centered_minsize();
  2170. }
  2171. void ScriptEditor::_on_find_in_files_result_selected(String fpath, int line_number, int begin, int end) {
  2172. RES res = ResourceLoader::load(fpath);
  2173. if (fpath.get_extension() == "shader") {
  2174. ShaderEditorPlugin *shader_editor = Object::cast_to<ShaderEditorPlugin>(EditorNode::get_singleton()->get_editor_data().get_editor("Shader"));
  2175. shader_editor->edit(res.ptr());
  2176. shader_editor->make_visible(true);
  2177. shader_editor->get_shader_editor()->goto_line_selection(line_number - 1, begin, end);
  2178. } else {
  2179. edit(res);
  2180. ScriptTextEditor *ste = Object::cast_to<ScriptTextEditor>(_get_current_editor());
  2181. if (ste) {
  2182. ste->goto_line_selection(line_number - 1, begin, end);
  2183. }
  2184. }
  2185. }
  2186. void ScriptEditor::_start_find_in_files(bool with_replace) {
  2187. FindInFiles *f = find_in_files->get_finder();
  2188. f->set_search_text(find_in_files_dialog->get_search_text());
  2189. f->set_match_case(find_in_files_dialog->is_match_case());
  2190. f->set_whole_words(find_in_files_dialog->is_whole_words());
  2191. f->set_folder(find_in_files_dialog->get_folder());
  2192. f->set_filter(find_in_files_dialog->get_filter());
  2193. find_in_files->set_with_replace(with_replace);
  2194. find_in_files->start_search();
  2195. editor->make_bottom_panel_item_visible(find_in_files);
  2196. }
  2197. void ScriptEditor::_on_find_in_files_modified_files(PoolStringArray paths) {
  2198. _test_script_times_on_disk();
  2199. _update_modified_scripts_for_external_editor();
  2200. }
  2201. void ScriptEditor::_bind_methods() {
  2202. ClassDB::bind_method("_file_dialog_action", &ScriptEditor::_file_dialog_action);
  2203. ClassDB::bind_method("_tab_changed", &ScriptEditor::_tab_changed);
  2204. ClassDB::bind_method("_menu_option", &ScriptEditor::_menu_option);
  2205. ClassDB::bind_method("_close_current_tab", &ScriptEditor::_close_current_tab);
  2206. ClassDB::bind_method("_close_discard_current_tab", &ScriptEditor::_close_discard_current_tab);
  2207. ClassDB::bind_method("_close_docs_tab", &ScriptEditor::_close_docs_tab);
  2208. ClassDB::bind_method("_close_all_tabs", &ScriptEditor::_close_all_tabs);
  2209. ClassDB::bind_method("_close_other_tabs", &ScriptEditor::_close_other_tabs);
  2210. ClassDB::bind_method("_open_recent_script", &ScriptEditor::_open_recent_script);
  2211. ClassDB::bind_method("_theme_option", &ScriptEditor::_theme_option);
  2212. ClassDB::bind_method("_editor_play", &ScriptEditor::_editor_play);
  2213. ClassDB::bind_method("_editor_pause", &ScriptEditor::_editor_pause);
  2214. ClassDB::bind_method("_editor_stop", &ScriptEditor::_editor_stop);
  2215. ClassDB::bind_method("_add_callback", &ScriptEditor::_add_callback);
  2216. ClassDB::bind_method("_reload_scripts", &ScriptEditor::_reload_scripts);
  2217. ClassDB::bind_method("_resave_scripts", &ScriptEditor::_resave_scripts);
  2218. ClassDB::bind_method("_res_saved_callback", &ScriptEditor::_res_saved_callback);
  2219. ClassDB::bind_method("_goto_script_line", &ScriptEditor::_goto_script_line);
  2220. ClassDB::bind_method("_goto_script_line2", &ScriptEditor::_goto_script_line2);
  2221. ClassDB::bind_method("_help_search", &ScriptEditor::_help_search);
  2222. ClassDB::bind_method("_save_history", &ScriptEditor::_save_history);
  2223. ClassDB::bind_method("_copy_script_path", &ScriptEditor::_copy_script_path);
  2224. ClassDB::bind_method("_breaked", &ScriptEditor::_breaked);
  2225. ClassDB::bind_method("_show_debugger", &ScriptEditor::_show_debugger);
  2226. ClassDB::bind_method("_get_debug_tooltip", &ScriptEditor::_get_debug_tooltip);
  2227. ClassDB::bind_method("_autosave_scripts", &ScriptEditor::_autosave_scripts);
  2228. ClassDB::bind_method("_editor_settings_changed", &ScriptEditor::_editor_settings_changed);
  2229. ClassDB::bind_method("_update_script_names", &ScriptEditor::_update_script_names);
  2230. ClassDB::bind_method("_tree_changed", &ScriptEditor::_tree_changed);
  2231. ClassDB::bind_method("_members_overview_selected", &ScriptEditor::_members_overview_selected);
  2232. ClassDB::bind_method("_help_overview_selected", &ScriptEditor::_help_overview_selected);
  2233. ClassDB::bind_method("_script_selected", &ScriptEditor::_script_selected);
  2234. ClassDB::bind_method("_script_created", &ScriptEditor::_script_created);
  2235. ClassDB::bind_method("_script_split_dragged", &ScriptEditor::_script_split_dragged);
  2236. ClassDB::bind_method("_help_class_open", &ScriptEditor::_help_class_open);
  2237. ClassDB::bind_method("_help_class_goto", &ScriptEditor::_help_class_goto);
  2238. ClassDB::bind_method("_request_help", &ScriptEditor::_help_class_open);
  2239. ClassDB::bind_method("_history_forward", &ScriptEditor::_history_forward);
  2240. ClassDB::bind_method("_history_back", &ScriptEditor::_history_back);
  2241. ClassDB::bind_method("_live_auto_reload_running_scripts", &ScriptEditor::_live_auto_reload_running_scripts);
  2242. ClassDB::bind_method("_unhandled_input", &ScriptEditor::_unhandled_input);
  2243. ClassDB::bind_method("_script_list_gui_input", &ScriptEditor::_script_list_gui_input);
  2244. ClassDB::bind_method("_toggle_members_overview_alpha_sort", &ScriptEditor::_toggle_members_overview_alpha_sort);
  2245. ClassDB::bind_method("_update_members_overview", &ScriptEditor::_update_members_overview);
  2246. ClassDB::bind_method("_script_changed", &ScriptEditor::_script_changed);
  2247. ClassDB::bind_method("_update_recent_scripts", &ScriptEditor::_update_recent_scripts);
  2248. ClassDB::bind_method("_on_find_in_files_requested", &ScriptEditor::_on_find_in_files_requested);
  2249. ClassDB::bind_method("_start_find_in_files", &ScriptEditor::_start_find_in_files);
  2250. ClassDB::bind_method("_on_find_in_files_result_selected", &ScriptEditor::_on_find_in_files_result_selected);
  2251. ClassDB::bind_method("_on_find_in_files_modified_files", &ScriptEditor::_on_find_in_files_modified_files);
  2252. ClassDB::bind_method(D_METHOD("get_drag_data_fw", "point", "from"), &ScriptEditor::get_drag_data_fw);
  2253. ClassDB::bind_method(D_METHOD("can_drop_data_fw", "point", "data", "from"), &ScriptEditor::can_drop_data_fw);
  2254. ClassDB::bind_method(D_METHOD("drop_data_fw", "point", "data", "from"), &ScriptEditor::drop_data_fw);
  2255. ClassDB::bind_method(D_METHOD("get_current_script"), &ScriptEditor::_get_current_script);
  2256. ClassDB::bind_method(D_METHOD("get_open_scripts"), &ScriptEditor::_get_open_scripts);
  2257. ClassDB::bind_method(D_METHOD("open_script_create_dialog", "base_name", "base_path"), &ScriptEditor::open_script_create_dialog);
  2258. ADD_SIGNAL(MethodInfo("editor_script_changed", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script")));
  2259. ADD_SIGNAL(MethodInfo("script_close", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script")));
  2260. }
  2261. ScriptEditor::ScriptEditor(EditorNode *p_editor) {
  2262. current_theme = "";
  2263. completion_cache = memnew(EditorScriptCodeCompletionCache);
  2264. restoring_layout = false;
  2265. waiting_update_names = false;
  2266. pending_auto_reload = false;
  2267. auto_reload_running_scripts = true;
  2268. members_overview_enabled = EditorSettings::get_singleton()->get("text_editor/open_scripts/show_members_overview");
  2269. help_overview_enabled = EditorSettings::get_singleton()->get("text_editor/help/show_help_index");
  2270. editor = p_editor;
  2271. VBoxContainer *main_container = memnew(VBoxContainer);
  2272. add_child(main_container);
  2273. menu_hb = memnew(HBoxContainer);
  2274. main_container->add_child(menu_hb);
  2275. script_split = memnew(HSplitContainer);
  2276. main_container->add_child(script_split);
  2277. script_split->set_v_size_flags(SIZE_EXPAND_FILL);
  2278. list_split = memnew(VSplitContainer);
  2279. script_split->add_child(list_split);
  2280. list_split->set_v_size_flags(SIZE_EXPAND_FILL);
  2281. script_list = memnew(ItemList);
  2282. list_split->add_child(script_list);
  2283. script_list->set_custom_minimum_size(Size2(150, 90) * EDSCALE); //need to give a bit of limit to avoid it from disappearing
  2284. script_list->set_v_size_flags(SIZE_EXPAND_FILL);
  2285. script_split->set_split_offset(140);
  2286. _sort_list_on_update = true;
  2287. script_list->connect("gui_input", this, "_script_list_gui_input", varray(), CONNECT_DEFERRED);
  2288. script_list->set_allow_rmb_select(true);
  2289. script_list->set_drag_forwarding(this);
  2290. context_menu = memnew(PopupMenu);
  2291. add_child(context_menu);
  2292. context_menu->connect("id_pressed", this, "_menu_option");
  2293. context_menu->set_hide_on_window_lose_focus(true);
  2294. overview_vbox = memnew(VBoxContainer);
  2295. overview_vbox->set_custom_minimum_size(Size2(0, 90));
  2296. overview_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  2297. list_split->add_child(overview_vbox);
  2298. buttons_hbox = memnew(HBoxContainer);
  2299. overview_vbox->add_child(buttons_hbox);
  2300. filename = memnew(Label);
  2301. filename->set_clip_text(true);
  2302. filename->set_h_size_flags(SIZE_EXPAND_FILL);
  2303. filename->add_style_override("normal", EditorNode::get_singleton()->get_gui_base()->get_stylebox("normal", "LineEdit"));
  2304. buttons_hbox->add_child(filename);
  2305. members_overview_alphabeta_sort_button = memnew(ToolButton);
  2306. members_overview_alphabeta_sort_button->set_tooltip(TTR("Toggle alphabetical sorting of the method list."));
  2307. members_overview_alphabeta_sort_button->set_toggle_mode(true);
  2308. members_overview_alphabeta_sort_button->set_pressed(EditorSettings::get_singleton()->get("text_editor/tools/sort_members_outline_alphabetically"));
  2309. members_overview_alphabeta_sort_button->connect("toggled", this, "_toggle_members_overview_alpha_sort");
  2310. buttons_hbox->add_child(members_overview_alphabeta_sort_button);
  2311. members_overview = memnew(ItemList);
  2312. overview_vbox->add_child(members_overview);
  2313. members_overview->set_allow_reselect(true);
  2314. members_overview->set_custom_minimum_size(Size2(0, 90) * EDSCALE); //need to give a bit of limit to avoid it from disappearing
  2315. members_overview->set_v_size_flags(SIZE_EXPAND_FILL);
  2316. members_overview->set_allow_rmb_select(true);
  2317. members_overview->set_drag_forwarding(this);
  2318. help_overview = memnew(ItemList);
  2319. overview_vbox->add_child(help_overview);
  2320. help_overview->set_allow_reselect(true);
  2321. help_overview->set_custom_minimum_size(Size2(0, 90) * EDSCALE); //need to give a bit of limit to avoid it from disappearing
  2322. help_overview->set_v_size_flags(SIZE_EXPAND_FILL);
  2323. tab_container = memnew(TabContainer);
  2324. tab_container->set_tabs_visible(false);
  2325. tab_container->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  2326. script_split->add_child(tab_container);
  2327. tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
  2328. ED_SHORTCUT("script_editor/window_sort", TTR("Sort"));
  2329. ED_SHORTCUT("script_editor/window_move_up", TTR("Move Up"), KEY_MASK_SHIFT | KEY_MASK_ALT | KEY_UP);
  2330. ED_SHORTCUT("script_editor/window_move_down", TTR("Move Down"), KEY_MASK_SHIFT | KEY_MASK_ALT | KEY_DOWN);
  2331. ED_SHORTCUT("script_editor/next_script", TTR("Next script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_PERIOD); // these should be KEY_GREATER and KEY_LESS but those don't work
  2332. ED_SHORTCUT("script_editor/prev_script", TTR("Previous script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_COLON);
  2333. set_process_unhandled_input(true);
  2334. file_menu = memnew(MenuButton);
  2335. menu_hb->add_child(file_menu);
  2336. file_menu->set_text(TTR("File"));
  2337. file_menu->set_switch_on_hover(true);
  2338. file_menu->get_popup()->set_hide_on_window_lose_focus(true);
  2339. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTR("New Script...")), FILE_NEW);
  2340. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new_textfile", TTR("New TextFile...")), FILE_NEW_TEXTFILE);
  2341. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open", TTR("Open...")), FILE_OPEN);
  2342. file_menu->get_popup()->add_submenu_item(TTR("Open Recent"), "RecentScripts", FILE_OPEN_RECENT);
  2343. recent_scripts = memnew(PopupMenu);
  2344. recent_scripts->set_name("RecentScripts");
  2345. file_menu->get_popup()->add_child(recent_scripts);
  2346. recent_scripts->connect("id_pressed", this, "_open_recent_script");
  2347. _update_recent_scripts();
  2348. file_menu->get_popup()->add_separator();
  2349. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save", TTR("Save"), KEY_MASK_ALT | KEY_MASK_CMD | KEY_S), FILE_SAVE);
  2350. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as", TTR("Save As...")), FILE_SAVE_AS);
  2351. 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);
  2352. file_menu->get_popup()->add_separator();
  2353. file_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);
  2354. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy_path", TTR("Copy Script Path")), FILE_COPY_PATH);
  2355. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/show_in_file_system", TTR("Show in FileSystem")), SHOW_IN_FILE_SYSTEM);
  2356. file_menu->get_popup()->add_separator();
  2357. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_previous", TTR("History Previous"), KEY_MASK_ALT | KEY_LEFT), WINDOW_PREV);
  2358. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_next", TTR("History Next"), KEY_MASK_ALT | KEY_RIGHT), WINDOW_NEXT);
  2359. file_menu->get_popup()->add_separator();
  2360. file_menu->get_popup()->add_submenu_item(TTR("Theme"), "Theme", FILE_THEME);
  2361. theme_submenu = memnew(PopupMenu);
  2362. theme_submenu->set_name("Theme");
  2363. file_menu->get_popup()->add_child(theme_submenu);
  2364. theme_submenu->connect("id_pressed", this, "_theme_option");
  2365. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/import_theme", TTR("Import Theme...")), THEME_IMPORT);
  2366. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/reload_theme", TTR("Reload Theme")), THEME_RELOAD);
  2367. theme_submenu->add_separator();
  2368. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/save_theme", TTR("Save Theme")), THEME_SAVE);
  2369. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as", TTR("Save Theme As...")), THEME_SAVE_AS);
  2370. file_menu->get_popup()->add_separator();
  2371. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_docs", TTR("Close Docs")), CLOSE_DOCS);
  2372. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file", TTR("Close"), KEY_MASK_CMD | KEY_W), FILE_CLOSE);
  2373. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_all", TTR("Close All")), CLOSE_ALL);
  2374. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_other_tabs", TTR("Close Other Tabs")), CLOSE_OTHER_TABS);
  2375. file_menu->get_popup()->add_separator();
  2376. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/run_file", TTR("Run"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_X), FILE_RUN);
  2377. file_menu->get_popup()->add_separator();
  2378. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_scripts_panel", TTR("Toggle Scripts Panel"), KEY_MASK_CMD | KEY_BACKSLASH), TOGGLE_SCRIPTS_PANEL);
  2379. file_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2380. script_search_menu = memnew(MenuButton);
  2381. menu_hb->add_child(script_search_menu);
  2382. script_search_menu->set_text(TTR("Search"));
  2383. script_search_menu->set_switch_on_hover(true);
  2384. script_search_menu->get_popup()->set_hide_on_window_lose_focus(true);
  2385. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find..."), KEY_MASK_CMD | KEY_F), HELP_SEARCH_FIND);
  2386. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), HELP_SEARCH_FIND_NEXT);
  2387. script_search_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2388. script_search_menu->hide();
  2389. debug_menu = memnew(MenuButton);
  2390. menu_hb->add_child(debug_menu);
  2391. debug_menu->set_text(TTR("Debug"));
  2392. debug_menu->set_switch_on_hover(true);
  2393. debug_menu->get_popup()->set_hide_on_window_lose_focus(true);
  2394. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_over", TTR("Step Over"), KEY_F10), DEBUG_NEXT);
  2395. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_into", TTR("Step Into"), KEY_F11), DEBUG_STEP);
  2396. debug_menu->get_popup()->add_separator();
  2397. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/break", TTR("Break")), DEBUG_BREAK);
  2398. debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/continue", TTR("Continue"), KEY_F12), DEBUG_CONTINUE);
  2399. debug_menu->get_popup()->add_separator();
  2400. //debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
  2401. debug_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("debugger/keep_debugger_open", TTR("Keep Debugger Open")), DEBUG_SHOW_KEEP_OPEN);
  2402. debug_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("debugger/debug_with_external_editor", TTR("Debug with External Editor")), DEBUG_WITH_EXTERNAL_EDITOR);
  2403. debug_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2404. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
  2405. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
  2406. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true);
  2407. debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
  2408. menu_hb->add_spacer();
  2409. script_icon = memnew(TextureRect);
  2410. menu_hb->add_child(script_icon);
  2411. script_name_label = memnew(Label);
  2412. menu_hb->add_child(script_name_label);
  2413. script_icon->hide();
  2414. script_name_label->hide();
  2415. menu_hb->add_spacer();
  2416. site_search = memnew(ToolButton);
  2417. site_search->set_text(TTR("Online Docs"));
  2418. site_search->connect("pressed", this, "_menu_option", varray(SEARCH_WEBSITE));
  2419. menu_hb->add_child(site_search);
  2420. site_search->set_tooltip(TTR("Open Godot online documentation"));
  2421. request_docs = memnew(ToolButton);
  2422. request_docs->set_text(TTR("Request Docs"));
  2423. request_docs->connect("pressed", this, "_menu_option", varray(REQUEST_DOCS));
  2424. menu_hb->add_child(request_docs);
  2425. request_docs->set_tooltip(TTR("Help improve the Godot documentation by giving feedback"));
  2426. help_search = memnew(ToolButton);
  2427. help_search->set_text(TTR("Search Help"));
  2428. help_search->connect("pressed", this, "_menu_option", varray(SEARCH_HELP));
  2429. menu_hb->add_child(help_search);
  2430. help_search->set_tooltip(TTR("Search the reference documentation."));
  2431. menu_hb->add_child(memnew(VSeparator));
  2432. script_back = memnew(ToolButton);
  2433. script_back->connect("pressed", this, "_history_back");
  2434. menu_hb->add_child(script_back);
  2435. script_back->set_disabled(true);
  2436. script_back->set_tooltip(TTR("Go to previous edited document."));
  2437. script_forward = memnew(ToolButton);
  2438. script_forward->connect("pressed", this, "_history_forward");
  2439. menu_hb->add_child(script_forward);
  2440. script_forward->set_disabled(true);
  2441. script_forward->set_tooltip(TTR("Go to next edited document."));
  2442. tab_container->connect("tab_changed", this, "_tab_changed");
  2443. erase_tab_confirm = memnew(ConfirmationDialog);
  2444. erase_tab_confirm->get_ok()->set_text(TTR("Save"));
  2445. erase_tab_confirm->add_button(TTR("Discard"), OS::get_singleton()->get_swap_ok_cancel(), "discard");
  2446. erase_tab_confirm->connect("confirmed", this, "_close_current_tab");
  2447. erase_tab_confirm->connect("custom_action", this, "_close_discard_current_tab");
  2448. add_child(erase_tab_confirm);
  2449. script_create_dialog = memnew(ScriptCreateDialog);
  2450. script_create_dialog->set_title(TTR("Create Script"));
  2451. add_child(script_create_dialog);
  2452. script_create_dialog->connect("script_created", this, "_script_created");
  2453. file_dialog_option = -1;
  2454. file_dialog = memnew(EditorFileDialog);
  2455. add_child(file_dialog);
  2456. file_dialog->connect("file_selected", this, "_file_dialog_action");
  2457. error_dialog = memnew(AcceptDialog);
  2458. add_child(error_dialog);
  2459. debugger = memnew(ScriptEditorDebugger(editor));
  2460. debugger->connect("goto_script_line", this, "_goto_script_line");
  2461. debugger->connect("show_debugger", this, "_show_debugger");
  2462. disk_changed = memnew(ConfirmationDialog);
  2463. {
  2464. VBoxContainer *vbc = memnew(VBoxContainer);
  2465. disk_changed->add_child(vbc);
  2466. Label *dl = memnew(Label);
  2467. dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?:"));
  2468. vbc->add_child(dl);
  2469. disk_changed_list = memnew(Tree);
  2470. vbc->add_child(disk_changed_list);
  2471. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  2472. disk_changed->connect("confirmed", this, "_reload_scripts");
  2473. disk_changed->get_ok()->set_text(TTR("Reload"));
  2474. disk_changed->add_button(TTR("Resave"), !OS::get_singleton()->get_swap_ok_cancel(), "resave");
  2475. disk_changed->connect("custom_action", this, "_resave_scripts");
  2476. }
  2477. add_child(disk_changed);
  2478. script_editor = this;
  2479. Button *db = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Debugger"), debugger);
  2480. debugger->set_tool_button(db);
  2481. debugger->connect("breaked", this, "_breaked");
  2482. autosave_timer = memnew(Timer);
  2483. autosave_timer->set_one_shot(false);
  2484. add_child(autosave_timer);
  2485. grab_focus_block = false;
  2486. help_search_dialog = memnew(EditorHelpSearch);
  2487. add_child(help_search_dialog);
  2488. help_search_dialog->connect("go_to_help", this, "_help_class_goto");
  2489. find_in_files_dialog = memnew(FindInFilesDialog);
  2490. find_in_files_dialog->connect(FindInFilesDialog::SIGNAL_FIND_REQUESTED, this, "_start_find_in_files", varray(false));
  2491. find_in_files_dialog->connect(FindInFilesDialog::SIGNAL_REPLACE_REQUESTED, this, "_start_find_in_files", varray(true));
  2492. add_child(find_in_files_dialog);
  2493. find_in_files = memnew(FindInFilesPanel);
  2494. find_in_files_button = editor->add_bottom_panel_item(TTR("Search Results"), find_in_files);
  2495. find_in_files->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  2496. find_in_files->connect(FindInFilesPanel::SIGNAL_RESULT_SELECTED, this, "_on_find_in_files_result_selected");
  2497. find_in_files->connect(FindInFilesPanel::SIGNAL_FILES_MODIFIED, this, "_on_find_in_files_modified_files");
  2498. find_in_files->hide();
  2499. find_in_files_button->hide();
  2500. history_pos = -1;
  2501. //debugger_gui->hide();
  2502. edit_pass = 0;
  2503. trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/files/trim_trailing_whitespace_on_save");
  2504. convert_indent_on_save = EditorSettings::get_singleton()->get("text_editor/indent/convert_indent_on_save");
  2505. use_space_indentation = EditorSettings::get_singleton()->get("text_editor/indent/type");
  2506. ScriptServer::edit_request_func = _open_script_request;
  2507. add_style_override("panel", editor->get_gui_base()->get_stylebox("ScriptEditorPanel", "EditorStyles"));
  2508. tab_container->add_style_override("panel", editor->get_gui_base()->get_stylebox("ScriptEditor", "EditorStyles"));
  2509. }
  2510. ScriptEditor::~ScriptEditor() {
  2511. memdelete(completion_cache);
  2512. }
  2513. void ScriptEditorPlugin::edit(Object *p_object) {
  2514. if (Object::cast_to<Script>(p_object)) {
  2515. Script *p_script = Object::cast_to<Script>(p_object);
  2516. String scene_path = p_script->get_path().get_slice("::", 0);
  2517. if (_is_built_in_script(p_script) && !EditorNode::get_singleton()->is_scene_open(scene_path)) {
  2518. EditorNode::get_singleton()->load_scene(scene_path);
  2519. script_editor->call_deferred("edit", p_script);
  2520. } else {
  2521. script_editor->edit(p_script);
  2522. }
  2523. }
  2524. if (Object::cast_to<TextFile>(p_object)) {
  2525. script_editor->edit(Object::cast_to<TextFile>(p_object));
  2526. }
  2527. }
  2528. bool ScriptEditorPlugin::handles(Object *p_object) const {
  2529. if (Object::cast_to<TextFile>(p_object)) {
  2530. return true;
  2531. }
  2532. if (Object::cast_to<Script>(p_object)) {
  2533. return true;
  2534. }
  2535. return p_object->is_class("Script");
  2536. }
  2537. void ScriptEditorPlugin::make_visible(bool p_visible) {
  2538. if (p_visible) {
  2539. script_editor->show();
  2540. script_editor->set_process(true);
  2541. script_editor->ensure_select_current();
  2542. } else {
  2543. script_editor->hide();
  2544. script_editor->set_process(false);
  2545. }
  2546. }
  2547. void ScriptEditorPlugin::selected_notify() {
  2548. script_editor->ensure_select_current();
  2549. }
  2550. void ScriptEditorPlugin::save_external_data() {
  2551. script_editor->save_all_scripts();
  2552. }
  2553. void ScriptEditorPlugin::apply_changes() {
  2554. script_editor->apply_scripts();
  2555. }
  2556. void ScriptEditorPlugin::restore_global_state() {
  2557. }
  2558. void ScriptEditorPlugin::save_global_state() {
  2559. }
  2560. void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  2561. script_editor->set_window_layout(p_layout);
  2562. }
  2563. void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
  2564. script_editor->get_window_layout(p_layout);
  2565. }
  2566. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  2567. return script_editor->get_breakpoints(p_breakpoints);
  2568. }
  2569. void ScriptEditorPlugin::edited_scene_changed() {
  2570. script_editor->edited_scene_changed();
  2571. }
  2572. ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
  2573. editor = p_node;
  2574. script_editor = memnew(ScriptEditor(p_node));
  2575. editor->get_viewport()->add_child(script_editor);
  2576. script_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2577. script_editor->hide();
  2578. EDITOR_DEF("text_editor/files/auto_reload_scripts_on_external_change", true);
  2579. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/files/auto_reload_and_parse_scripts_on_save", true));
  2580. EDITOR_DEF("text_editor/files/open_dominant_script_on_scene_change", true);
  2581. EDITOR_DEF("text_editor/external/use_external_editor", false);
  2582. EDITOR_DEF("text_editor/external/exec_path", "");
  2583. EDITOR_DEF("text_editor/open_scripts/script_temperature_enabled", true);
  2584. EDITOR_DEF("text_editor/open_scripts/highlight_current_script", true);
  2585. EDITOR_DEF("text_editor/open_scripts/script_temperature_history_size", 15);
  2586. EDITOR_DEF("text_editor/open_scripts/current_script_background_color", Color(1, 1, 1, 0.3));
  2587. EDITOR_DEF("text_editor/open_scripts/group_help_pages", true);
  2588. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/open_scripts/sort_scripts_by", PROPERTY_HINT_ENUM, "Name,Path,None"));
  2589. EDITOR_DEF("text_editor/open_scripts/sort_scripts_by", 0);
  2590. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/open_scripts/list_script_names_as", PROPERTY_HINT_ENUM, "Name,Parent Directory And Name,Full Path"));
  2591. EDITOR_DEF("text_editor/open_scripts/list_script_names_as", 0);
  2592. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "text_editor/external/exec_path", PROPERTY_HINT_GLOBAL_FILE));
  2593. EDITOR_DEF("text_editor/external/exec_flags", "");
  2594. ED_SHORTCUT("script_editor/open_recent", TTR("Open Recent"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_T);
  2595. ED_SHORTCUT("script_editor/clear_recent", TTR("Clear Recent Files"));
  2596. }
  2597. ScriptEditorPlugin::~ScriptEditorPlugin() {
  2598. }