script_editor_plugin.cpp 88 KB

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